package cn.iocoder.yudao.module.crm.controller.admin.crmcustomer;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.crm.controller.admin.crmcustomer.vo.*;
import cn.iocoder.yudao.module.crm.dal.dataobject.crmcustomer.CrmCustomerDO;
import cn.iocoder.yudao.module.crm.service.crmcustomer.CrmCustomerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 客户")
@RestController
@RequestMapping("/crm/customer")
@Validated
public class CrmCustomerController {

    @Resource
    private CrmCustomerService customerService;

    @PostMapping("/create")
    @Operation(summary = "创建客户")
    // @PreAuthorize("@ss.hasPermission('crm:customer:create')")
    public CommonResult<Long> createCustomer(@Valid @RequestBody CrmCustomerSaveReqVO createReqVO) {
        return success(customerService.createCustomer(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户")
    // @PreAuthorize("@ss.hasPermission('crm:customer:update')")
    public CommonResult<Boolean> updateCustomer(@Valid @RequestBody CrmCustomerSaveReqVO updateReqVO) {
        customerService.updateCustomer(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/do-open")
    @Operation(summary = "投入公海")
    @Parameter(name = "id", description = "编号", required = true)
    //// @PreAuthorize("@ss.hasPermission('crm:customer:doopen')")
    public CommonResult<Boolean> openCustomer(@RequestParam("id") Long id) {
        customerService.openCustomer(id);
        return success(true);
    }

    @GetMapping("/receive")
    @Operation(summary = "公海领取客户")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> receiveCustomer(@RequestParam("id") Long id) {
        customerService.receiveCustomer(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('crm:customer:query')")
    public CommonResult<CrmCustomerRespVO> getCustomer(@RequestParam("id") Long id) {
        CrmCustomerDO customer = customerService.getCustomer(id);
        return success(BeanUtils.toBean(customer, CrmCustomerRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户分页")
    // @PreAuthorize("@ss.hasPermission('crm:customer:query')")
    public CommonResult<PageResult<CrmCustomerRespVO>> getCustomerPage(@Valid CrmCustomerPageReqVO pageReqVO) {
        PageResult<CrmCustomerDO> pageResult = customerService.getCustomerPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CrmCustomerRespVO.class));
    }

    @GetMapping("/page2")
    @Operation(summary = "获得查重客户分页")
    // @PreAuthorize("@ss.hasPermission('crm:customer:query')")
    public CommonResult<PageResult<CrmCustomerRespVO>> getCustomerPage2(@Valid CrmCustomerPageReqVO pageReqVO) {
        return success(customerService.getCustomerPage2(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户 Excel")
    // @PreAuthorize("@ss.hasPermission('crm:customer:export')")
    public void exportCustomerExcel(@Valid CrmCustomerPageReqVO pageReqVO,
                                    HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CrmCustomerDO> list = customerService.getCustomerPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户.xls", "数据", CrmCustomerRespVO.class,
                BeanUtils.toBean(list, CrmCustomerRespVO.class));
    }

    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<CrmCustomerImportVO> list = Arrays.asList(
                CrmCustomerImportVO.builder().name("yshop").mobile("18888888888").telephone("18888888888").tags("重要,厉害")
                        .remark("yshop").detailAddress("详细地址").weixin("wechat").qq("100000").build(),
                CrmCustomerImportVO.builder().name("yshop2").mobile("18888888888=0").telephone("18888888880").tags("重要2,厉害")
                        .remark("yshop2").detailAddress("详细地址2").weixin("wechat2").qq("1000002").build()
        );
        // 输出
        ExcelUtils.write(response, "客户导入模板.xls", "客户列表", CrmCustomerImportVO.class, list);
    }

    @PostMapping("/import")
    @Operation(summary = "导入用户")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "deptId", description = "部门ID"),
            @Parameter(name = "adminIds", description = "用户ID 多个逗号隔开"),
            @Parameter(name = "averageType", description = "分配方式")
    })
    public CommonResult<CustomerImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                          @RequestParam("adminIds") String adminIds,
                                                          @RequestParam("averageType") Integer averageType,
                                                          @RequestParam(value = "repeatConfirmationFlag", defaultValue = "0") Integer repeatConfirmationFlag) throws Exception {
        List<CrmCustomerImportVO> list = ExcelUtils.read(file, CrmCustomerImportVO.class);
        return success(customerService.importList(list, adminIds, averageType, repeatConfirmationFlag));
    }

    @PostMapping("/recover")
    @Operation(summary = "资料回收")
    public CommonResult<Boolean> recoverCustomer(@Valid @RequestBody CrmCustomerRecoverVO recoverVO) {
        customerService.recover(recoverVO);
        return success(true);
    }

    @PostMapping("/transfer")
    @Operation(summary = "转移客户")
    public CommonResult<Boolean> transferCustomer(@Valid @RequestBody CrmCustomerTransferVO transferVO) {
        customerService.transfer(transferVO);
        return success(true);
    }

    @PostMapping("/send-sms")
    @Operation(summary = "发送短信")
    public CommonResult<Boolean> sendSms(@Valid @RequestBody SmsTemplateSendVO sendVO) {
        customerService.sendSms(sendVO);
        return success(true);
    }

    @PostMapping("/send-mail")
    @Operation(summary = "发送邮件")
    public CommonResult<Boolean> sendMail(@Valid @RequestBody MailTemplateSendVO sendVO) {
        customerService.sendMail(sendVO);
        return success(true);
    }


}
