package cn.iocoder.yudao.module.visit.controller.admin.customerinfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.visit.controller.admin.customerinfo.vo.*;
import cn.iocoder.yudao.module.visit.dal.dataobject.customerinfo.CustomerInfoDO;
import cn.iocoder.yudao.module.visit.service.customerinfo.CustomerInfoService;

@Tag(name = "管理后台 - 客户信息")
@RestController
@RequestMapping("/visit/customer-info")
@Validated
public class CustomerInfoController {

    @Resource
    private CustomerInfoService customerInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建客户信息")
    @PreAuthorize("@ss.hasPermission('visit:customer-info:create')")
    public CommonResult<Long> createCustomerInfo(@Valid @RequestBody CustomerInfoSaveReqVO createReqVO) {
        return success(customerInfoService.createCustomerInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户信息")
    @PreAuthorize("@ss.hasPermission('visit:customer-info:update')")
    public CommonResult<Boolean> updateCustomerInfo(@Valid @RequestBody CustomerInfoSaveReqVO updateReqVO) {
        customerInfoService.updateCustomerInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('visit:customer-info:delete')")
    public CommonResult<Boolean> deleteCustomerInfo(@RequestParam("id") Long id) {
        customerInfoService.deleteCustomerInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('visit:customer-info:query')")
    public CommonResult<CustomerInfoRespVO> getCustomerInfo(@RequestParam("id") Long id) {
        CustomerInfoDO customerInfo = customerInfoService.getCustomerInfo(id);
        return success(BeanUtils.toBean(customerInfo, CustomerInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户信息分页")
    @PreAuthorize("@ss.hasPermission('visit:customer-info:query')")
    public CommonResult<PageResult<CustomerInfoRespVO>> getCustomerInfoPage(@Valid CustomerInfoPageReqVO pageReqVO) {
        PageResult<CustomerInfoDO> pageResult = customerInfoService.getCustomerInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CustomerInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户信息 Excel")
    @PreAuthorize("@ss.hasPermission('visit:customer-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCustomerInfoExcel(@Valid CustomerInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CustomerInfoDO> list = customerInfoService.getCustomerInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户信息.xls", "数据", CustomerInfoRespVO.class,
                        BeanUtils.toBean(list, CustomerInfoRespVO.class));
    }

}