package com.zfire.mall.manager.controller.policy;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.zfire.mall.manager.annotation.ZfireList;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.listV2.OrderPlanCustomerVO;
import com.zfire.mall.manager.bean.listV2.PolicyCustomerVO;
import com.zfire.mall.manager.bean.listV2.PolicyVO;
import com.zfire.mall.manager.bean.listV2.dto.PolicyDTO;
import com.zfire.mall.manager.bean.policy.*;
import com.zfire.mall.manager.bean.supplier.KingDeeSupplierVO;
import com.zfire.mall.manager.constant.Constant;
import com.zfire.mall.manager.enums.OrderTypeEnum;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.policy.PolicyLogic;
import com.zfire.mall.manager.plus.entity.*;
import com.zfire.mall.manager.utils.CommonUtils;
import com.zfire.mall.manager.utils.excel.ExcelUtils;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import com.zfire.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@RestController
@Api(value = "销售政策API", tags ={"销售政策API"} )
@RequestMapping(value = "/policy", produces = "application/json; charset=utf-8")
public class PolicyController {

    @Autowired
    PolicyLogic policyLogic;

    @ZfireList
    @PostMapping("/list/v2")
    @ApiOperation("政策-列表V2")
    public ResponseHelper<IPage<PolicyVO>> listV2(
            @RequestBody PolicyDTO policyDTO
    ) throws RemoteServiceException {
        IPage<PolicyVO> policyVOIPage = policyLogic.listV2(policyDTO);
        return ResponseHelper.success(policyVOIPage, new TypeReference<PolicyVO>() {});
    }

    @PostMapping("/list/v2/export")
    @ApiOperation("政策列表V2-导出")
    public void listExport(
            @RequestBody PolicyDTO policyDTO,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(policyDTO);
        //2.查询要导出的内容
        IPage<PolicyVO> list = policyLogic.listV2(policyDTO);
        //3.导出
        FieldUtils.exportData(list.getRecords(),zfireParam.getExportFields(),request,response);
    }



    @GetMapping("/list")
    @ApiOperation("销售政策列表")
    public ResponseHelper<IPage<Policy>> list(
            @ApiParam(value = "经销商ID",required = false) @RequestParam(required = false) String customerId,
            @ApiParam(value = "经销商编码",required = false) @RequestParam(required = false) String customerNumber,
            @ApiParam(value = "编号",required = false) @RequestParam(required = false) String code,
            @ApiParam(value = "标题",required = false) @RequestParam(required = false) String title,
            @ApiParam(value = "备注",required = false) @RequestParam(required = false) String remark,
            @ApiParam(value = "生效时间开始",required = false) @RequestParam(required = false) String startTime1,
            @ApiParam(value = "生效时间结束",required = false) @RequestParam(required = false) String endTime1,
            @ApiParam(value = "结束时间开始",required = false) @RequestParam(required = false) String startTime2,
            @ApiParam(value = "结束时间结束",required = false) @RequestParam(required = false) String endTime2,
            @ApiParam(value = "制表时间开始",required = false) @RequestParam(required = false) String startCreateTime,
            @ApiParam(value = "制表时间结束",required = false) @RequestParam(required = false) String endCreateTime,
            @ApiParam(value = "制表人",required = false) @RequestParam(required = false) String createBy,
            @ApiParam(value = "审核人",required = false) @RequestParam(required = false) String examineBy,
            @ApiParam(value = "状态 1=已生效 0=未生效",required = false) @RequestParam(required = false) Boolean status,
            @ApiParam(value = "销售政策类型(PROVISION=配提  LIMIT=限量)",required = false) @RequestParam(required = false) String type,
            @ApiParam(value = "机型规格",required = false) @RequestParam(required = false) String specification,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ){
        IPage<Policy> list = policyLogic.list(customerId,customerNumber,code, title, remark, startTime1, endTime1, startTime2, endTime2,
                startCreateTime, endCreateTime, createBy, examineBy, status, type,specification, pageNum, pageSize);
        return ResponseHelper.success(list);
    }


    @GetMapping("/export")
    @ApiOperation("导出销售政策列表")
    public void export(
            @ApiParam(value = "经销商ID",required = false) @RequestParam(required = false) String customerId,
            @ApiParam(value = "经销商编码",required = false) @RequestParam(required = false) String customerNumber,
            @ApiParam(value = "名称") @RequestParam(required = false) String code,
            @ApiParam(value = "型号") @RequestParam(required = false) String title,
            @ApiParam(value = "编码") @RequestParam(required = false) String remark,
            @ApiParam(value = "生效时间开始") @RequestParam(required = false) String startTime1,
            @ApiParam(value = "生效时间结束") @RequestParam(required = false) String endTime1,
            @ApiParam(value = "结束时间开始") @RequestParam(required = false) String startTime2,
            @ApiParam(value = "结束时间结束") @RequestParam(required = false) String endTime2,
            @ApiParam(value = "制表时间开始") @RequestParam(required = false) String startCreateTime,
            @ApiParam(value = "制表时间结束") @RequestParam(required = false) String endCreateTime,
            @ApiParam(value = "制表人") @RequestParam(required = false) String createBy,
            @ApiParam(value = "审核人") @RequestParam(required = false) String examineBy,
            @ApiParam(value = "状态 1=已生效 0=未生效") @RequestParam(required = false) Boolean status,
            @ApiParam(value = "销售政策类型(PROVISION=配提  LIMIT=限量)") @RequestParam(required = false) String type,
            @ApiParam(value = "机型规格",required = false) @RequestParam(required = false) String specification,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        IPage<Policy> list = policyLogic.list(customerId,customerNumber,code, title, remark, startTime1, endTime1, startTime2, endTime2,
                startCreateTime, endCreateTime, createBy, examineBy, status, type,specification,1,-1);
        ExcelData excelData = policyLogic.exportData(list.getRecords());
        ExcelUtils.exportExcel(request,response,"销售政策订单.xlsx",excelData);
    }


    @GetMapping("/detail")
    @ApiOperation("获取销售政策详情")
    public ResponseHelper<Policy> detail(
            @ApiParam(value = "销售政策id") @RequestParam(required = false) String policyId
    ){
        Policy detail = policyLogic.detail(policyId);
        return ResponseHelper.success(detail);
    }


    @GetMapping("/id")
    @ApiOperation("获取销售政策编号")
    public ResponseHelper<String> id(){
        return ResponseHelper.success("P"+IdWorker.getIdStr());
    }

    @GetMapping("/download")
    @ApiOperation("下载销售政策模板")
    public void download(
            HttpServletResponse response
    ) throws IOException {
        CommonUtils.downloadFile("pocily_goods.xlsx",response);
    }


    @GetMapping("/downloadCustomer")
    @ApiOperation("下载模板-政策经销商")
    public void downloadCutomer(
            HttpServletResponse response
    ) throws IOException {
        CommonUtils.downloadFile("policy_customer.xlsx",response);
    }

    @GetMapping("/limit/download")
    @ApiOperation("经销商购买上限-下载模板")
    public void downloadLimit(
            HttpServletResponse response
    ) throws IOException {
        CommonUtils.downloadFile("经销商购买上限.xlsx",response);
    }

    @PostMapping("/limit/import")
    @ApiOperation("经销商购买上限-导入")
    public ResponseHelper importLimit(
            @RequestParam String policyId,
            MultipartFile file
    ) throws IOException {
        List<Object> objects = ExcelUtils.importExcel(file);
        policyLogic.importCustomerLimit(policyId,objects);
        return ResponseHelper.success();
    }

    @GetMapping("/limit/list")
    @ApiOperation("经销商购买上限-列表")
    public ResponseHelper<IPage<PolicyCustomerLimitBean>> importLimit(
            @ApiParam(value = "政策id",required = true) @RequestParam String policyId,
            @ApiParam(value = "经销商编号/经销商名称",required = false) @RequestParam(required = false) String keyword,
            @ApiParam(value = "经销商编号/经销商名称",required = false) @RequestParam(required = false) String specification,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize
    ) throws IOException {
        IPage<PolicyCustomerLimitBean> limitPage = policyLogic.limitList(policyId, keyword,specification, pageNum, pageSize);
        return ResponseHelper.success(limitPage);
    }

    @PostMapping("/limit/save")
    @ApiOperation("经销商购买上限-保存(有id修改，无id新增)")
    public ResponseHelper saveLimit(
        @Valid @RequestBody PolicyLimitBean policyLimitBeans
    ){
        policyLogic.saveLimit(policyLimitBeans);
        return ResponseHelper.success();
    }

    @PostMapping("/limit/delete")
    @ApiOperation("经销商购买上限-删除")
    public ResponseHelper saveLimit(
            @Valid @RequestParam String id
    ){
        policyLogic.deleteLimit(id);
        return ResponseHelper.success();
    }

    @GetMapping("/limit/detail")
    @ApiOperation("经销商购买上限-详情")
    public ResponseHelper<PolicyCustomerLimit> limitDetail(@RequestParam String id){
        PolicyCustomerLimit policyCustomerLimit = policyLogic.detailLimit(id);
        return ResponseHelper.success(policyCustomerLimit);
    }







    @PostMapping("/limit/group/save")
    @ApiOperation("经销商配提套数上限-保存(有id修改，无id新增)")
    public ResponseHelper saveLimitGroup(
            @RequestBody PolicyGroupLimit policyGroupLimit
    ){
        policyLogic.saveGroupLimit(policyGroupLimit);
        return ResponseHelper.success();
    }

    @PostMapping("/limit/group/delete")
    @ApiOperation("经销商配提套数上限-删除")
    public ResponseHelper deleteLimitGroup(
            @RequestParam String id
    ){
        policyLogic.deleteGroupLimit(id);
        return ResponseHelper.success();
    }

    @GetMapping("/limit/group/detail")
    @ApiOperation("经销商配提套数上限-详情")
    public ResponseHelper<PolicyGroupLimit> limitGroupDetail(@RequestParam String id){
        PolicyGroupLimit policyGroupLimit = policyLogic.detailGroupLimit(id);
        return ResponseHelper.success(policyGroupLimit);
    }

    @GetMapping("/limit/group/download")
    @ApiOperation("经销商配提套数上限-下载模板")
    public void downloadLimit2(
            HttpServletResponse response
    ) throws IOException {
        CommonUtils.downloadFile("经销商配提套数上限.xlsx",response);
    }

    @PostMapping("/limit/group/import")
    @ApiOperation("经销商配提套数上限-导入")
    public ResponseHelper importLimit2(
            @RequestParam String policyId,
            MultipartFile file
    ) throws IOException {
        List<Object> objects = ExcelUtils.importExcel(file);
        policyLogic.importCustomerLimit2(policyId,objects);
        return ResponseHelper.success();
    }
    @GetMapping("/limit/group/list")
    @ApiOperation("经销商配提套数购买上限-列表")
    public ResponseHelper<IPage<PolicyGroupLimitBean>> limitGroupList(
            @ApiParam(value = "政策id",required = true) @RequestParam String policyId,
            @ApiParam(value = "经销商编号/经销商名称",required = false) @RequestParam(required = false) String keyword,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize
    ) throws IOException {
        IPage<PolicyGroupLimitBean> page = policyLogic.limitGroupList(policyId, keyword, pageNum, pageSize);
        return ResponseHelper.success(page);
    }
    @GetMapping("/limit/group/export")
    @ApiOperation("经销商配提套数购买上限-导出")
    public void exportCustomerLimit2(
            @ApiParam(value = "政策id",required = true) @RequestParam String policyId,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        IPage<PolicyGroupLimitBean> page = policyLogic.limitGroupList(policyId, null, 1, -1);
        ExcelData excelData = policyLogic.exportGroupLimit(page.getRecords());
        ExcelUtils.exportExcel(request,response,"经销商配提套数上限.xlsx",excelData);
    }

    @PostMapping("/limit/group/batchupdate")
    @ApiOperation("经销商配提套数上限-批量修改配提套数")
    public ResponseHelper importLimit(
            @ApiParam(value = "经销商配提套数上限ids",required = true) @RequestParam List<String> ids,
            @ApiParam(value = "最小起提数",required = false) @RequestParam(required = false) Integer minBuyNum,
            @ApiParam(value = "最大可提组数",required = false) @RequestParam(required = false) Integer maxBuyNum,
            @ApiParam(value = "配提套数上限",required = false) @RequestParam(required = false) Integer limitGroupNum
    ) throws IOException {
        policyLogic.batchGroupUpdateLimit(ids,minBuyNum,maxBuyNum,limitGroupNum);
        return ResponseHelper.success();
    }


    @PostMapping("/limit/batchupdate")
    @ApiOperation("经销商购买上限-批量修改起提数/提数数")
    public ResponseHelper importLimit(
            @ApiParam(value = "经销商上限ids",required = true) @RequestParam List<String> ids,
            @ApiParam(value = "购买量上限/提货量上限",required = false) @RequestParam(required = false) Integer limitQty
    ) throws IOException {

        policyLogic.batchUpdateLimit(ids,limitQty);
        return ResponseHelper.success();
    }

    @PostMapping("/batch/material/update")
    @ApiOperation("政策-批量修改机型")
    public ResponseHelper importLimit(
            @ApiParam(value = "政策id",required = true) @RequestParam List<String> policyIds,
            @ApiParam(value = "旧物料id",required = false) @RequestParam(required = false) String oldMaterialId,
            @ApiParam(value = "新物料id",required = false) @RequestParam(required = false) String newMaterialId
    ) throws IOException {
        policyLogic.batchUpdateMaterial(policyIds,oldMaterialId,newMaterialId);
        return ResponseHelper.success();
    }

    @PostMapping("/batch/delay/update")
    @ApiOperation("政策-批量修改延期时间")
    public ResponseHelper importLimit(
            @RequestBody List<PolicyDelayBean> list
    ) throws IOException {
        policyLogic.batchUpdateDelay(list);
        return ResponseHelper.success();
    }


    @PostMapping("/importCustomer")
    @ApiOperation("导入经销商")
    public ResponseHelper<List<KingDeeCustomer>>  importCustomer(MultipartFile file) throws IOException {
        List<Object> objects = ExcelUtils.importExcel(file);
        List<KingDeeCustomer> kingDeeCustomers = policyLogic.importCustomer(objects);
        return ResponseHelper.success(kingDeeCustomers);
    }

    @PostMapping("/add")
    @ApiOperation("新增政策和经销商")
    public ResponseHelper add(@RequestBody PolicyBean policyBean){
        policyLogic.add(policyBean);
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation("编辑政策和经销商")
    public ResponseHelper update(@RequestBody PolicyBean policyBean){
        policyLogic.update(policyBean);
        return ResponseHelper.success();
    }

    @PostMapping("/delete")
    @ApiOperation("删除政策和经销商")
    public ResponseHelper delete(@RequestParam String id){
        policyLogic.delete(id);
        return ResponseHelper.success();
    }

    @PostMapping("/cancel")
    @ApiOperation("政策-作废")
    public ResponseHelper cancel(@RequestParam String id){
        policyLogic.cancel(id);
        return ResponseHelper.success();
    }

    @PostMapping("/condition/add")
    @ApiOperation("新增政策条件")
    public ResponseHelper addCondition(@RequestBody PolicyConditionAdd policyConditionAdd){
        policyLogic.addCondition(policyConditionAdd);
        return ResponseHelper.success();
    }

    @PostMapping("/condition/update")
    @ApiOperation("修改政策条件")
    public ResponseHelper addCondition(
            @ApiParam(value = "id",required = true) @RequestParam String id,
            @ApiParam(value = "条件名称",required = true) @RequestParam String name
    ){
        policyLogic.updateCondition(id,name);
        return ResponseHelper.success();
    }

    @PostMapping("/condition/delete")
    @ApiOperation("删除政策条件")
    public ResponseHelper deleteCondition(@RequestParam String id){
        policyLogic.deleteCondition(id);
        return ResponseHelper.success();
    }

    @GetMapping("/condition/detail")
    @ApiOperation("政策条件-详情")
    public ResponseHelper<PolicyCondition> conditionDetail(@RequestParam String id){
        PolicyCondition policyCondition = policyLogic.detailCondition(id);
        return ResponseHelper.success(policyCondition);
    }



    @PostMapping("/policy/submit")
    @ApiOperation("提审政策")
    public ResponseHelper submit(
            @ApiParam(value = "政策id") @RequestParam(required = true) String policyId
    ){
        policyLogic.submit(policyId);
        return ResponseHelper.success();
    }

    @PostMapping("/examine")
    @ApiOperation("审核销售政策")
    public ResponseHelper examine(
            @ApiParam(value = "政策id",required = true) @RequestParam(required = true) String policyId,
            @ApiParam(value = "审核 OK=通过  FAIL=不通过",required = true) @RequestParam(required = true) String examineStatus,
            @ApiParam(value = "审核备注",required = false) @RequestParam(required = false) String examineRemark
    ){
        policyLogic.examine(policyId,examineStatus,examineRemark);
        return ResponseHelper.success();
    }

    @GetMapping("/material/list")
    @ApiOperation("政策机型-列表")
    public ResponseHelper<IPage<PolicyMaterialBean>> materialList(
            @ApiParam(value = "政策id",required = true) @RequestParam(required = true) String policyId,
            @ApiParam(value = "机型规格",required = false) @RequestParam(required = false) String specification,
            @ApiParam(value = "是否配提(true=是，false=否)",required = false) @RequestParam(required = false) Boolean isProvision,
            @ApiParam(value = "是否启用(true=是，false=否)",required = false) @RequestParam(required = false) Boolean status,
            @ApiParam(value = "销售类型编号",required = false) @RequestParam(required = false) String saleTypeCode,
            @ApiParam(value = "价格类型",required = false) @RequestParam(required = false) String priceType,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ){
        IPage<PolicyMaterialBean> policyMaterialIPage = policyLogic.materialList(policyId,specification,isProvision,status,saleTypeCode,priceType, pageNum, pageSize);
        return ResponseHelper.success(policyMaterialIPage);
    }

    @PostMapping("/material/save")
    @ApiOperation("政策机型-保存")
    public ResponseHelper addMaterial(
            @RequestBody List<PolicyMaterialAddBean> policyMaterialAddBeans
    ){
        policyLogic.saveMaterial(policyMaterialAddBeans);
        return ResponseHelper.success();
    }


    @PostMapping("/material/clone")
    @ApiOperation("政策-克隆")
    public ResponseHelper clone(
           @ApiParam(value = "政策id",required = true) @RequestParam(required = true) String policyId
    ){
        policyLogic.clonePolicy(policyId);
        return ResponseHelper.success();
    }

    @PostMapping("/abandon")
    @ApiOperation("政策弃审")
    public ResponseHelper abandon(
            @ApiParam(value = "policyId",required = true) @RequestParam String policyId
    ) throws Exception {
        policyLogic.abandon(policyId);
        return ResponseHelper.success();
    }



    @GetMapping("/type1/material/list")
    @ApiOperation("政策-限定机型列表")
    public ResponseHelper<IPage<PolicyConditionMaterialBean>> type1MaterialList(
            @ApiParam(value = "政策id",required = true) @RequestParam(required = true) String policyId,
            @ApiParam(value = "销售类型编号",required = false) @RequestParam(required = false) String saleTypeCode,
            @ApiParam(value = "机型型号",required = false) @RequestParam(required = false) String specification,
            @ApiParam(value = "机型状态 true=启用 false=未启用",required = false) @RequestParam(required = false) Boolean status,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ){
        IPage<PolicyConditionMaterialBean> policyMaterialIPage = policyLogic.type1MaterialList(policyId,saleTypeCode,specification,status, pageNum, pageSize);
        return ResponseHelper.success(policyMaterialIPage);
    }



    @GetMapping("/customer/list")
    @ApiOperation("政策经销商列表")
    public ResponseHelper<IPage<PolicyCustomer>> customerList(
            @ApiParam(value = "政策id",required = true) @RequestParam(required = true) String policyId,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ){
        IPage<PolicyCustomer> policyCustomerIPage = policyLogic.customerList(policyId, pageNum, pageSize);
        return ResponseHelper.success(policyCustomerIPage);
    }

    @GetMapping("/condition/material/list")
    @ApiOperation("政策条件的机型列表")
    public ResponseHelper<IPage<PolicyConditionMaterialBean>> conditionList(
            @ApiParam(value = "政策条件的id",required = true) @RequestParam(required = true) String policyConditionId,
            @ApiParam(value = "比例类型 1=比例第一个值的商品 2=比例第二个值的商品",required = true) @RequestParam(required = true) Integer popType,
            @ApiParam(value = "机型型号",required = false) @RequestParam(required = false) String specification,
            @ApiParam(value = "经销商id",required = false) @RequestParam(required = false) String customerId,
            @ApiParam(value = "机型状态 true=启用 false=未启用",required = false) @RequestParam(required = false) Boolean status,
            @ApiParam(value = "价格类型",required = false) @RequestParam(required = false) String priceType,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize
    ){
        IPage<PolicyConditionMaterialBean> policyConditionMaterialIPage = policyLogic.conditionMaterialList(policyConditionId, popType,specification,
                customerId, status,priceType,pageNum, pageSize);
        return ResponseHelper.success(policyConditionMaterialIPage);
    }

    @GetMapping("/condition/list")
    @ApiOperation("政策条件列表")
    public ResponseHelper<List<PolicyCondition>> conditionList(
            @ApiParam(value = "政策id") @RequestParam(required = true) String policyId
    ){
        List<PolicyCondition> policyConditions = policyLogic.conditionList(policyId);
        return ResponseHelper.success(policyConditions);
    }




    @GetMapping("/condition/get")
    @ApiOperation("根据政策id和物料id反查政策条件")
    public ResponseHelper<PolicyCustomer> getCondition(
            @ApiParam(value = "政策id",required = true) @RequestParam(required = true) String policyId,
            @ApiParam(value = "物料id",required = true) @RequestParam(required = true) String materialId
    ){
        PolicyCondition condition = policyLogic.getCondition(policyId, materialId);
        return ResponseHelper.success(condition);
    }

    @GetMapping("/customer/list/v2")
    @ApiOperation("政策经销商列表")
    public ResponseHelper<IPage<PolicyCustomer>> customerListV2(
            @ApiParam(value = "政策id",required = true) @RequestParam(required = true) String policyId,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ){
        IPage<PolicyCustomer> policyCustomerIPage = policyLogic.customerList(policyId, pageNum, pageSize);
        return ResponseHelper.success(policyCustomerIPage);
    }

    @ZfireList
    @PostMapping("/customer_count/list")
    @ApiOperation("政策-列表V2")
    public ResponseHelper<IPage<PolicyCustomerVO>> customerListV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<PolicyCustomerVO> page = policyLogic.customerListV2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<PolicyCustomerVO>() {});
    }

    @GetMapping("/customer_count/list")
    @ApiOperation("政策对应经销商列表")
    public ResponseHelper<IPage<PolicyBean>> customerList(
            @ApiParam(value = "政策编号",required = false) @RequestParam(required = false) String code,
            @ApiParam(value = "政策说明",required = false) @RequestParam(required = false) String remark,
            @ApiParam(value = "表头备注",required = false) @RequestParam(required = false) String title,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ){
        IPage<PolicyBean> policyBeanIPage = policyLogic.queryPolicyCount(code, remark, title, pageNum, pageSize);
        return ResponseHelper.success(policyBeanIPage);
    }


    @PostMapping("/customer_count/batch")
    @ApiOperation("政策对应经销商-批量设置提货数量/批量备注")
    public ResponseHelper customerList(
            @ApiParam(value = "政策对应经销商id",required = true) @RequestParam(required = true) List<String> policyCustomerIds,
            @ApiParam(value = "备注",required = false) @RequestParam(required = false) String remark,
            @ApiParam(value = "最大提货数量",required = false) @RequestParam(required = false) Integer limitTakeNum
    ){
        policyLogic.batchSetup(policyCustomerIds, limitTakeNum, remark);
        return ResponseHelper.success();
    }


    @PostMapping("/material/delete")
    @ApiOperation("政策机型-删除")
    public ResponseHelper examine(
            @ApiParam(value = "政策产品id") @RequestParam(required = true) String policyMaterialId
    ){
        policyLogic.deleteMaterial(policyMaterialId);
        return ResponseHelper.success();
    }

    @PostMapping("/material/status")
    @ApiOperation("政策机型-启用/作废")
    public ResponseHelper status(
            @ApiParam(value = "政策产品id") @RequestParam(required = true) String policyMaterialId,
            @ApiParam(value = "true=启用 false=作废") @RequestParam(required = true) Boolean status
    ){
        policyLogic.statusMaterial(policyMaterialId,status);
        return ResponseHelper.success();
    }

    @PostMapping("/material/batch/cancal")
    @ApiOperation("批量作废政策机型")
    public ResponseHelper deleteCondition(
            @ApiParam(value = "政策id",required = true) @RequestParam List<String> policyIds,
            @ApiParam(value = "物料id",required = true) @RequestParam List<String> materialIds
    ){
        policyLogic.batchMaterialCancel(policyIds,materialIds);
        return ResponseHelper.success();
    }




    @PostMapping("/material/import")
    @ApiOperation("导入政策产品")
    public ResponseHelper importMaterial(
            @ApiParam(value = "政策id") @RequestParam(required = true) String policyId,
            MultipartFile file
    ) throws IOException {
        List<Object> objects = ExcelUtils.importExcel(file);
        policyLogic.importMaterial(policyId,objects);
        return ResponseHelper.success();
    }


    @GetMapping("/customer_count/export")
    @ApiOperation("政策对应经销商导出")
    public void customerExport(
            @ApiParam(value = "政策编号",required = false) @RequestParam(required = false) String code,
            @ApiParam(value = "政策说明",required = false) @RequestParam(required = false) String remark,
            @ApiParam(value = "表头备注",required = false) @RequestParam(required = false) String title,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        IPage<PolicyBean> policyBeanIPage = policyLogic.queryPolicyCount(code, remark, title, 1, -1);
        ExcelData excelData = policyLogic.customerExport(policyBeanIPage.getRecords());
        ExcelUtils.exportExcel(request,response,"政策对应经销商.xlsx",excelData);
    }


    @GetMapping("/customer/export")
    @ApiOperation("导出-政策经销商")
    public void exportCustomer(
            @ApiParam(value = "政策id",required = true) @RequestParam(required = true) String policyId,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        IPage<PolicyCustomer> policyCustomerIPage = policyLogic.customerList(policyId, 1, -1);
        ExcelData excelData = policyLogic.exportCustomer(policyCustomerIPage.getRecords());
        ExcelUtils.exportExcel(request,response,"政策经销商.xlsx",excelData);
    }


    @GetMapping("/material/export")
    @ApiOperation("导出-政策货品")
    public void exportMaterial(
            @ApiParam(value = "政策id",required = true) @RequestParam(required = true) String policyId,
            @ApiParam(value = "规格",required = false) @RequestParam(required = false) String specification,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        IPage<PolicyMaterialBean> policyMaterialBeanIPage = policyLogic.materialList(policyId, specification,
                null,null,null,null,1, -1);
        ExcelData excelData = policyLogic.exportMaterial(policyMaterialBeanIPage.getRecords());
        ExcelUtils.exportExcel(request,response,"政策货品.xlsx",excelData);
    }

    @GetMapping("/limit/export")
    @ApiOperation("导出-经销商上限")
    public void exportCustomerLimit(
            @ApiParam(value = "政策id",required = true) @RequestParam(required = true) String policyId,
            @ApiParam(value = "经销商编号/经销商名称",required = false) @RequestParam(required = false) String keyword,
            @ApiParam(value = "经销商编号/经销商名称",required = false) @RequestParam(required = false) String specification,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        IPage<PolicyCustomerLimitBean> page = policyLogic.limitList(policyId, keyword,specification,1, -1);
        ExcelData excelData = policyLogic.exportLimit(page.getRecords());
        ExcelUtils.exportExcel(request,response,"经销商上限.xlsx",excelData);
    }


}
