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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
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.crmclues.vo.*;
import cn.iocoder.yudao.module.crm.controller.admin.crmcustomer.vo.CrmCustomerSaveReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.crmcustomer.vo.CustomerImportRespVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.crmclues.CrmCluesDO;
import cn.iocoder.yudao.module.crm.service.crmclues.CrmCluesService;
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.security.access.prepost.PreAuthorize;
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/clues")
@Validated
public class CrmCluesController {

    @Resource
    private CrmCluesService cluesService;

    @PostMapping("/create")
    @Operation(summary = "创建线索")
    // @PreAuthorize("@ss.hasPermission('crm:clues:create')")
    public CommonResult<Long> createClues(@Valid @RequestBody CrmCluesSaveReqVO createReqVO) {
        return success(cluesService.createClues(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新线索")
    // @PreAuthorize("@ss.hasPermission('crm:clues:update')")
    public CommonResult<Boolean> updateClues(@Valid @RequestBody CrmCluesSaveReqVO updateReqVO) {
        cluesService.updateClues(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得线索")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('crm:clues:query')")
    public CommonResult<CrmCluesRespVO> getClues(@RequestParam("id") Long id) {
        CrmCluesDO clues = cluesService.getClues(id);
        return success(BeanUtils.toBean(clues, CrmCluesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得线索分页")
    // @PreAuthorize("@ss.hasPermission('crm:clues:query')")
    public CommonResult<PageResult<CrmCluesRespVO>> getCluesPage(@Valid CrmCluesPageReqVO pageReqVO) {
        return success(cluesService.getCluesPage(pageReqVO));
    }

    @GetMapping("/open")
    @Operation(summary = "放入线索池")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('crm:clues:query')")
    public CommonResult<Boolean> openClues(@RequestParam("id") Long id) {
        cluesService.openClues(id);
        return success(true);
    }

    @GetMapping("/receive")
    @Operation(summary = "领取线索")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> receiveCustomer(@RequestParam("id") Long id) {
        cluesService.receiveClues(id);
        return success(true);
    }

    @PostMapping("/createCustomer")
    @Operation(summary = "转换客户")
    // @PreAuthorize("@ss.hasPermission('crm:clues:create-customer')")
    public CommonResult<Boolean> createCustomer(@Valid @RequestBody CrmCustomerSaveReqVO createReqVO) {
        cluesService.createCustomer(createReqVO);
        return success(true);
    }

    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<CrmCluesImportVO> list = Arrays.asList(
                CrmCluesImportVO.builder().name("yshop").mobile("18888888888").telephone("18888888888").tags("重要,厉害")
                        .remark("yshop").detailAddress("详细地址").build(),
                CrmCluesImportVO.builder().name("yshop2").mobile("18888888888=0").telephone("18888888880").tags("重要2,厉害")
                        .remark("yshop2").detailAddress("详细地址2").build()
        );
        // 输出
        ExcelUtils.write(response, "线索导入模板.xls", "线索列表", CrmCluesImportVO.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("deptId") String deptId,
                                                          @RequestParam("adminIds") String adminIds,
                                                          @RequestParam("averageType") Integer averageType) throws Exception {
        List<CrmCluesImportVO> list = ExcelUtils.read(file, CrmCluesImportVO.class);
        return success(cluesService.importList(list, deptId,adminIds,averageType));
    }

    @PostMapping("/transfer")
    @Operation(summary = "转移线索")
    public CommonResult<Boolean> transferCustomer(@Valid @RequestBody CrmCluesTransferVO transferVO) {
        cluesService.transfer(transferVO);
        return success(true);
    }



}
