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

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.idempotent.core.annotation.Idempotent;
import cn.iocoder.yudao.module.visit.controller.admin.product.vo.ProductImportExcelVO;
import cn.iocoder.yudao.module.visit.controller.admin.product.vo.ProductImportRespVO;
import cn.iocoder.yudao.module.visit.controller.admin.product.vo.ProductSimpleReqVO;
import cn.iocoder.yudao.module.visit.service.product.ProductService;
import com.fhs.common.utils.StringUtil;
import io.swagger.v3.oas.annotations.Parameters;
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;
import org.springframework.web.multipart.MultipartFile;

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

    @Resource
    private CustomerInfoService customerInfoService;
    @Resource
    private ProductService  productService;

    @PostMapping("/create")
    @Operation(summary = "创建客户信息")
    @PreAuthorize("@ss.hasPermission('visit:customer-info:create')")
    @Idempotent(timeout = 10, message = "重复请求，请稍后重试")
    public CommonResult<Long> createCustomerInfo(@Valid @RequestBody CustomerInfoSaveReqVO createReqVO) {
        return success(customerInfoService.createCustomerInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户信息")
    @PreAuthorize("@ss.hasPermission('visit:customer-info:update')")
    @Idempotent(timeout = 10, message = "重复请求，请稍后重试")
    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')")
    @Idempotent(timeout = 10, message = "重复请求，请稍后重试")
    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);
        CustomerInfoRespVO bean = BeanUtils.toBean(customerInfo, CustomerInfoRespVO.class);
//        if (!StringUtil.isEmpty(customerInfo.getProductIds())){
//            List<ProductSimpleReqVO> productSimpleReqVO = productService.getProductByIds(customerInfo.getProductIds());
//            bean.setProductList(productSimpleReqVO);
//        }
        return success(bean);
    }

    @GetMapping("/getByCompanyName")
    @Operation(summary = "通过公司名称带出基本信息")
    @Parameter(name = "companyName", description = "公司名称", required = true, example = "郑州小牛")
    public CommonResult<CustomerInfoRespVO> getCustomerInfoByCompanyName(@RequestParam("companyName") String companyName) {
        CustomerInfoDO customerInfo = customerInfoService.getCustomerInfoByCompanyName(companyName);
        CustomerInfoRespVO bean = BeanUtils.toBean(customerInfo, CustomerInfoRespVO.class);
        return success(bean);
    }


    @GetMapping("/getByPhone")
    @Operation(summary = "通过手机号带出基本信息")
    @Parameter(name = "contact", description = "联系方式", required = true, example = "156")
    public CommonResult<List<CustomerInfoRespVO>> getCustomerInfoByContact(@RequestParam("contact") String contact) {
        List<CustomerInfoDO> customerInfo = customerInfoService.getCustomerInfoByContact(contact);
        List<CustomerInfoRespVO> bean = BeanUtils.toBean(customerInfo, CustomerInfoRespVO.class);
        return success(bean);
    }

    @GetMapping("/getAll")
    @Operation(summary = "查出所有")
    public CommonResult<List<CustomerInfoRespVO>> getAllCustomerInfo() {
        List<CustomerInfoDO> customerInfo = customerInfoService.getAllCustomerInfo();
        List<CustomerInfoRespVO> bean = BeanUtils.toBean(customerInfo, CustomerInfoRespVO.class);
        return success(bean);
    }

    @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));
    }


    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入客户信息模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<CustomerImportExcelVO> list = Collections.singletonList(
                CustomerImportExcelVO.builder().customerName("张三").companyName("xxx有限公司").contact("13678944321")
                        .customerType(0).department("0").build()
        );
        // 输出
        ExcelUtils.write(response, "客户信息导入模板.xls", "客户信息列表", CustomerImportExcelVO.class, list);
    }

    @PostMapping("/import")
    @Operation(summary = "导入客户信息")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('visit:customer-info:import')")
    @Idempotent(timeout = 10, message = "重复请求，请稍后重试")
    public CommonResult<CustomerImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                         @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<CustomerImportExcelVO> list = ExcelUtils.read(file, CustomerImportExcelVO.class);
        return success(customerInfoService.importCustomerList(list, updateSupport));
    }



}