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


import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.annotation.ZfireList;
import com.zfire.mall.manager.bean.DirectOrderBean;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.OrderExecBean;
import com.zfire.mall.manager.bean.PoySaleCountBean;
import com.zfire.mall.manager.bean.listV2.*;
import com.zfire.mall.manager.bean.listV2.dto.EnginOrderDTO;
import com.zfire.mall.manager.bean.listV2.dto.ExamineZfireParamBean;
import com.zfire.mall.manager.bean.retail.*;
import com.zfire.mall.manager.constant.Constant;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.retail.RetailOrderLogic;
import com.zfire.mall.manager.plus.service.RetailOrderService;
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.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

@Slf4j
@RestController
@Api(value = "零售单销售政策单API", tags ={"零售单销售政策单API"} )
@RequestMapping(value = "/retail", produces = "application/json; charset=utf-8")
public class RetailController {
    @Autowired
    RetailOrderService retailOrderService;
    @Autowired
    RetailOrderLogic retailOrderLogic;
    @Autowired
    RedisLockRegistry redisLockRegistry;


    @ZfireList
    @PostMapping("/list/v2")
    @ApiOperation("其他零售单列表-V2")
    public ResponseHelper<IPage<RetailOrderVO>> listRetailV2(
            @RequestBody ExamineZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<RetailOrderVO> page = retailOrderLogic.retailOrderListV2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<RetailOrderVO>() {});
    }

    @PostMapping("/list/v2/export")
    @ApiOperation("其他零售单列表V2-导出")
    public void listRetailV2Export(
            @RequestBody ExamineZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<RetailOrderVO> page = retailOrderLogic.retailOrderListV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(),zfireParam.getExportFields(),request,response);
    }

    @ZfireList
    @PostMapping("/listP/v2")
    @ApiOperation("政策单列表-V2")
    public ResponseHelper<IPage<RetailOrderPolicyVO>> listPV2(
            @RequestBody ExamineZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<RetailOrderPolicyVO> page = retailOrderLogic.policyOrderListV2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<RetailOrderPolicyVO>() {});
    }

    @PostMapping("/listP/v2/export")
    @ApiOperation("政策单列表V2-导出")
    public void listPV2Export(
            @RequestBody ExamineZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<RetailOrderPolicyVO> page = retailOrderLogic.policyOrderListV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(),zfireParam.getExportFields(),request,response);
    }






    @GetMapping("/list")
    @ApiOperation("列表")
    public ResponseHelper<IPage<RetailOrderList>> list(
            @ApiParam(value = "订单号",required = false) @RequestParam(required = false) String id,
            @ApiParam(value = "政策编号",required = false) @RequestParam(required = false) String policyCode,
            @ApiParam(value = "政策说明",required = false) @RequestParam(required = false) String policyRemark,
            @ApiParam(value = "表体业务员id",required = false) @RequestParam(required = false) String serviceId,
            @ApiParam(value = "表头业务员id",required = false) @RequestParam(required = false) String k3ServiceId,
            @ApiParam(value = "是否直调(true/false)",required = false) @RequestParam(required = false) Boolean isDirectTransfer,
            @ApiParam(value = "单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过",required = false) @RequestParam(required = false) String examineStatus,
            @ApiParam(value = "类型：1=普通零售单 2=政策零售单",required = false) @RequestParam(required = false) Integer type,
            @ApiParam(value = "金蝶编码",required = false) @RequestParam(required = false) String materialOldNumber,
            @ApiParam(value = "物料编码",required = false) @RequestParam(required = false) String materialCode,
            @ApiParam(value = "产品名称",required = false) @RequestParam(required = false) String materialName,
            @ApiParam(value = "规格型号",required = false) @RequestParam(required = false) String specification,
            @ApiParam(value = "经销商",required = false) @RequestParam(required = false) String customer,
            @ApiParam(value = "开始时间",required = false) @RequestParam(required = false) String startTime,
            @ApiParam(value = "结束时间",required = false) @RequestParam(required = false) String endTime,
            @ApiParam(value = "制表人",required = false) @RequestParam(required = false) String createBy,
            @ApiParam(value = "审核人",required = false) @RequestParam(required = false) String examineBy,
            @ApiParam(value = "仓库",required = false) @RequestParam(required = false) String stockId,
            @ApiParam(value = "存货类别编号",required = false) @RequestParam(required = false) String k3CategoryNumber,
            @ApiParam(value = "钱包",required = false) @RequestParam(required = false) String customerWalletId,
            @ApiParam(value = "销售类型",required = false) @RequestParam(required = false) String saleTypeId,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ){
        IPage<RetailOrderList> list = retailOrderLogic.list(customerWalletId,saleTypeId,id,policyCode,policyRemark,serviceId,k3ServiceId,examineStatus, type,materialOldNumber,materialCode, materialName, specification, customer, startTime, endTime, createBy, examineBy,stockId,isDirectTransfer,k3CategoryNumber, pageNum, pageSize);
        return ResponseHelper.success(list);
    }

    @GetMapping("/listCount")
    @ApiOperation("统计")
    public ResponseHelper<PoySaleCountBean> list(
            @ApiParam(value = "订单号",required = false) @RequestParam(required = false) String id,
            @ApiParam(value = "业务员id",required = false) @RequestParam(required = false) String serviceId,
            @ApiParam(value = "是否直调(true/false)",required = false) @RequestParam(required = false) Boolean isDirectTransfer,
            @ApiParam(value = "单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过",required = false) @RequestParam(required = false) String examineStatus,
            @ApiParam(value = "类型：1=普通零售单 2=政策零售单",required = false) @RequestParam(required = false) Integer type,
            @ApiParam(value = "金蝶编码",required = false) @RequestParam(required = false) String materialOldNumber,
            @ApiParam(value = "物料编码",required = false) @RequestParam(required = false) String materialCode,
            @ApiParam(value = "产品名称",required = false) @RequestParam(required = false) String materialName,
            @ApiParam(value = "规格型号",required = false) @RequestParam(required = false) String specification,
            @ApiParam(value = "经销商",required = false) @RequestParam(required = false) String customer,
            @ApiParam(value = "开始时间",required = false) @RequestParam(required = false) String startTime,
            @ApiParam(value = "结束时间",required = false) @RequestParam(required = false) String endTime,
            @ApiParam(value = "制表人",required = false) @RequestParam(required = false) String createBy,
            @ApiParam(value = "审核人",required = false) @RequestParam(required = false) String examineBy,
            @ApiParam(value = "仓库",required = false) @RequestParam(required = false) String stockId
    ){
        PoySaleCountBean list = retailOrderLogic.listCount(id,serviceId,examineStatus, type,materialOldNumber,materialCode, materialName, specification, customer,
                startTime, endTime, createBy, examineBy,stockId,isDirectTransfer);
        return ResponseHelper.success(list);
    }



    @GetMapping("/export")
    @ApiOperation("导出")
    public void export(
            @ApiParam(value = "订单号",required = false) @RequestParam(required = false) String id,
            @ApiParam(value = "政策编号",required = false) @RequestParam(required = false) String policyCode,
            @ApiParam(value = "政策说明",required = false) @RequestParam(required = false) String policyRemark,
            @ApiParam(value = "表体业务员id",required = false) @RequestParam(required = false) String serviceId,
            @ApiParam(value = "表头业务员id",required = false) @RequestParam(required = false) String k3ServiceId,
            @ApiParam(value = "类型：1=普通零售单 2=政策零售单",required = false) @RequestParam(required = false) Integer type,
            @ApiParam(value = "金蝶编码",required = false) @RequestParam(required = false) String materialOldNumber,
            @ApiParam(value = "物料编码",required = false) @RequestParam(required = false) String materialCode,
            @ApiParam(value = "是否直调(true/false)",required = false) @RequestParam(required = false) Boolean isDirectTransfer,
            @ApiParam(value = "单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=已关闭",required = false) @RequestParam(required = false) String examineStatus,
            @ApiParam(value = "产品名称",required = false) @RequestParam(required = false) String materialName,
            @ApiParam(value = "规格型号",required = false) @RequestParam(required = false) String specification,
            @ApiParam(value = "经销商",required = false) @RequestParam(required = false) String customer,
            @ApiParam(value = "开始时间",required = false) @RequestParam(required = false) String startTime,
            @ApiParam(value = "结束时间",required = false) @RequestParam(required = false) String endTime,
            @ApiParam(value = "制表人",required = false) @RequestParam(required = false) String createBy,
            @ApiParam(value = "审核人",required = false) @RequestParam(required = false) String examineBy,
            @ApiParam(value = "仓库",required = false) @RequestParam(required = false) String stockId,
            @ApiParam(value = "存货类别编号",required = false) @RequestParam(required = false) String k3CategoryNumber,
            @ApiParam(value = "钱包",required = false) @RequestParam(required = false) String customerWalletId,
            @ApiParam(value = "销售类型",required = false) @RequestParam(required = false) String saleTypeId,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        IPage<RetailOrderList> list = retailOrderLogic.list(customerWalletId,saleTypeId,id,policyCode,policyRemark,serviceId,k3ServiceId,examineStatus, type,materialOldNumber,materialCode,
                materialName, specification, customer,
                startTime, endTime, createBy, examineBy,stockId,isDirectTransfer,k3CategoryNumber,1, -1);
        ExcelData export = retailOrderLogic.export(list.getRecords(), type);

        String fileName = type == 1? "其他零售订单":"销售政策订单";

        ExcelUtils.exportExcel(request,response,fileName + ".xlsx",export);
    }


    @GetMapping("/detail")
    @ApiOperation("详情")
    public ResponseHelper<RetailOrderBean> detail(
            @ApiParam(value = "id") @RequestParam(required = false) String id
    ){
        RetailOrderBean detail = retailOrderLogic.detail(id);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/transfer/over")
    @ApiOperation("直调完成")
    public ResponseHelper transferOver(
            @ApiParam(value = "id") @RequestParam(required = false) String id
    ) throws InterruptedException {
        retailOrderLogic.transferOver(id);
        return ResponseHelper.success();
    }



    @PostMapping("/add")
    @ApiOperation("新增")
    public ResponseHelper add(
            @RequestBody RetailOrderAdd retailOrderAdd) throws Exception {
        retailOrderLogic.add(retailOrderAdd);
        return ResponseHelper.success();
    }


    @PostMapping("/update")
    @ApiOperation("修改")
    public ResponseHelper update(
            @RequestBody RetailOrderAdd retailOrderAdd) throws InterruptedException {

        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_RETAIL + retailOrderAdd.getId());
        if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
            return ResponseHelper.error("系统繁忙，请稍后再试");
        }
        try {
            retailOrderLogic.update(retailOrderAdd);
        }finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/delete")
    @ApiOperation("删除")
    public ResponseHelper delete(
            @RequestParam String id) throws InterruptedException {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_RETAIL + id);
        if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
            return ResponseHelper.error("系统繁忙，请稍后再试");
        }
        try {
            retailOrderLogic.delete(id);
        }finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/tuiding")
    @ApiOperation("退订")
    public ResponseHelper tuiding(
            @RequestBody RetailOrderRefundBean retailOrderRefundBean) throws InterruptedException {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_RETAIL + retailOrderRefundBean.getId());
        if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
            return ResponseHelper.error("系统繁忙，请稍后再试");
        }
        try {
            retailOrderLogic.tuiding(retailOrderRefundBean);
        }finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }


    @PostMapping("/submit")
    @ApiOperation("提审")
    public ResponseHelper submit(
            @ApiParam(value = "id") @RequestParam(required = false) String id
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_RETAIL + id);
        if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
            return ResponseHelper.error("系统繁忙，请稍后再试");
        }
        try {
            retailOrderLogic.submit(id);
        }finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }


    @PostMapping("/submit/cancel")
    @ApiOperation("撤回")
    public ResponseHelper submitCancel(
            @ApiParam(value = "id") @RequestParam(required = false) String id
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_RETAIL + id);
        if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
            return ResponseHelper.error("系统繁忙，请稍后再试");
        }
        try {
            retailOrderLogic.submitCancel(id);
        }finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/examine")
    @ApiOperation("审核与批量审核")
    public ResponseHelper examine(
            @ApiParam(value = "id",required = true) @RequestParam(required = true) List<String> id,
            @ApiParam(value = "审核结果 OK=通过 FAIL=不通过",required = false) @RequestParam(required = false) String examineStatus,
            @ApiParam(value = "审核备注",required = false) @RequestParam(required = false) String examineRemark,
            @ApiParam(value = "表头备注",required = false) @RequestParam(required = false) String remark,
            @ApiParam(value = "文件编号",required = false) @RequestParam(required = false) String fileNo,
            @ApiParam(value = "业务员",required = false) @RequestParam(required = false) String serviceId
    ) throws Exception {

        id = id.stream().distinct().collect(Collectors.toList());

        for(String idd : id) {
            Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_RETAIL + idd);
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                return ResponseHelper.error("系统繁忙，请稍后再试");
            }
            try {
                retailOrderLogic.examine(idd, examineStatus, examineRemark,remark,fileNo,serviceId);
            }finally {
                obtain.unlock();
            }
        }
        return ResponseHelper.success();
    }


    @PostMapping("/abandon")
    @ApiOperation("弃审")
    public ResponseHelper abandon(
            @ApiParam(value = "id",required = true) @RequestParam(required = true) String id
    ) throws InterruptedException {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_RETAIL + id);
        if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
            return ResponseHelper.error("系统繁忙，请稍后再试");
        }
        try {
            retailOrderLogic.abandon(id);
        }finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }


    @GetMapping("/product/list")
    @ApiOperation("可选的机型列表")
    public ResponseHelper<IPage<ProductPriceBean>> productList(
            @ApiParam(value = "品类id",required = false) @RequestParam(required = false) String mainId,
            @ApiParam(value = "销售类型id",required = false) @RequestParam(required = false) String saleId,
            @ApiParam(value = "物料编号",required = false) @RequestParam(required = false) String materialCode,
            @ApiParam(value = "物料名称",required = false) @RequestParam(required = false) String materialName,
            @ApiParam(value = "规格型号",required = false) @RequestParam(required = false) String specification,
            @ApiParam(value = "客户id",required = false) @RequestParam(required = false) String customerId,
            @ApiParam(value = "价格1",required = false) @RequestParam(required = false) BigDecimal price1,
            @ApiParam(value = "价格2",required = false) @RequestParam(required = false) BigDecimal price2,
            @ApiParam(value = "单据类型",required = false) @RequestParam(required = false, defaultValue = "GENERAL") String billType,
            @ApiParam(value = "物料旧编号",required = false) @RequestParam(required = false) List<String> oldNumbers,
            @ApiParam(value = "页号",required = true) @RequestParam(required = true) Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam(required = true) Integer pageSize
    ){
        IPage<ProductPriceBean> productPriceBeanIPage = retailOrderLogic
                .queryProductList(mainId, saleId, materialCode, materialName, specification, price1, price2, customerId, oldNumbers, billType, pageNum, pageSize);
        return ResponseHelper.success(productPriceBeanIPage);
    }


    @PostMapping("/refund")
    @ApiOperation("退单")
    public ResponseHelper refund(
            @ApiParam(value = "id") @RequestParam(required = false) String id,
            @ApiParam(value = "审核结果 OK=通过 FAIL=不通过") @RequestParam(required = false) String examineStatus,
            @ApiParam(value = "审核备注") @RequestParam(required = false) String examineRemark
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_RETAIL + id);
        if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
            return ResponseHelper.error("系统繁忙，请稍后再试");
        }
        try {
            retailOrderLogic.examine(id,examineStatus,examineRemark,null,null,null);
        }finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }


    @PostMapping("/close")
    @ApiOperation("关闭")
    public ResponseHelper close(
            @ApiParam(value = "id") @RequestParam(required = false) String id
    ) throws InterruptedException {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_RETAIL + id);
        if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
            return ResponseHelper.error("系统繁忙，请稍后再试");
        }
        try {
            retailOrderLogic.close(id);
        }finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }


    @PostMapping("/direct/deliver")
    @ApiOperation("直调发货")
    public ResponseHelper directDeliver(
            @ApiParam(value = "直调发货",required = true) @RequestBody DirectOrderBean directOrderBean
    ) throws InterruptedException {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_DIRECT_TRANSFER + directOrderBean.getOrderNo());
        if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
            return ResponseHelper.error("系统繁忙，请稍后再试");
        }
        try {
            retailOrderLogic.directDeliver(directOrderBean);
        }finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @ZfireList
    @PostMapping("/exec/policy/list/v2")
    @ApiOperation("销售执行明细列表-政策零售订单-V2")
    public ResponseHelper<IPage<OrderExecPolicyVO>> execPolicyListV2(@RequestBody ZfireParamBean zfireParamBean){
        IPage<OrderExecPolicyVO> page = retailOrderLogic.queryRetailOrderExecPolicyV2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<OrderExecPolicyVO>() {});
    }
    @ZfireList
    @PostMapping("/exec/retail/v2")
    @ApiOperation("销售执行明细列表-零售订单-V2")
    public ResponseHelper<IPage<OrderExecRetailVO>> execRetailListV2(@RequestBody ZfireParamBean zfireParamBean){
        IPage<OrderExecRetailVO> page = retailOrderLogic.queryRetailOrderExecRetailV2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<OrderExecRetailVO>() {});
    }

    @PostMapping("/exec/policy/export/v2")
    @ApiOperation("销售执行明细列表-政策零售订单-V2-导出")
    public void listExportPolicy(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<OrderExecPolicyVO> page = retailOrderLogic.queryRetailOrderExecPolicyV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(),zfireParam.getExportFields(),request,response);
    }

    @PostMapping("/exec/retail/export/v2")
    @ApiOperation("销售执行明细列表-政策零售订单-V2-导出")
    public void listExportRetail(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<OrderExecRetailVO> page = retailOrderLogic.queryRetailOrderExecRetailV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(),zfireParam.getExportFields(),request,response);
    }



    @ZfireList
    @PostMapping("/exec/engin/list/v2")
    @ApiOperation("销售执行明细列表-工程单订单-V2")
    public ResponseHelper<IPage<OrderEnginVO>> execRetailListV2(@RequestBody EnginOrderDTO enginOrderDTO){
        IPage<OrderEnginVO> page = retailOrderLogic.queryEnginOrderV2(enginOrderDTO);
        return ResponseHelper.success(page, new TypeReference<OrderEnginVO>() {});
    }

    @PostMapping("/exec/engin/export/v2")
    @ApiOperation("销售执行明细列表-工程单订单-V2导出")
    public void listExportPolicy(
            @RequestBody EnginOrderDTO enginOrderDTO,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(enginOrderDTO);
        //2.查询要导出的内容
        IPage<OrderEnginVO> page = retailOrderLogic.queryEnginOrderV2(enginOrderDTO);
        //3.导出
        FieldUtils.exportData(page.getRecords(),zfireParam.getExportFields(),request,response);
    }





    @GetMapping("/exec/list")
    @ApiOperation("销售单执行明细")
    public ResponseHelper<IPage<OrderExecBean>> execList(
            @ApiParam(value = "旧编码",required = false) @RequestParam(required = false) String materialOldNumber,
            @ApiParam(value = "销售单号",required = false) @RequestParam(required = false) String id,
            @ApiParam(value = "经销商名称",required = false) @RequestParam(required = false) String customerName,
            @ApiParam(value = "辅材名称",required = false) @RequestParam(required = false) String materialName,
            @ApiParam(value = "规格型号",required = false) @RequestParam(required = false) String specification,
            @ApiParam(value = "制单时间开始时间",required = false) @RequestParam(required = false) String startTime,
            @ApiParam(value = "制单时间结束时间",required = false) @RequestParam(required = false) String endTime,
            @ApiParam(value = "订单日期开始时间",required = false) @RequestParam(required = false) String orderStartTime,
            @ApiParam(value = "订单日期结束时间",required = false) @RequestParam(required = false) String orderEndTime,
            @ApiParam(value = "表头业务员名称",required = false) @RequestParam(required = false) String serviceName,
            @ApiParam(value = "表体业务员名称",required = false) @RequestParam(required = false) String itemServiceName,
            @ApiParam(value = "归属业务员名称",required = false) @RequestParam(required = false) String newServiceName,
            @ApiParam(value = "类型(1=普通零售 2=政策零售 TRADE=商用工程 HOME=家用工程)",required = true) @RequestParam(required = true) String type,
            @ApiParam(value = "存货分类",required = false) @RequestParam(required = false) List<String> k3CategoryNumber,
            @ApiParam(value = "工程名",required = false) @RequestParam(required = false) String projectName,
            @ApiParam(value = "工程登录编号",required = false) @RequestParam(required = false) String enginRecordNo,
            @ApiParam(value = "销售政策编号",required = false) @RequestParam(required = false) String policyCode,
            @ApiParam(value = "销售政策说明",required = false) @RequestParam(required = false) String policyRemark,
            @ApiParam(value = "发货是否完成 true=是 false=否",required = false) @RequestParam(required = false) Boolean isOver,
            @ApiParam(value = "是否置换过 true=是 false=否",required = false) @RequestParam(required = false) Boolean isDisplaceOrder,
            @ApiParam(value = "现金钱包",required = false) @RequestParam(required = false) List<String> walletIds1,
            @ApiParam(value = "返利钱包",required = false) @RequestParam(required = false) List<String> walletIds2,
            @ApiParam(value = "价格类型",required = false) @RequestParam(required = false) String priceType,
            @ApiParam(value = "是否剔除仓储收费",required = false) @RequestParam(required = false) String isCost,
            @ApiParam(value = "货满有效期开始",required = false) @RequestParam(required = false) String costStartTime,
            @ApiParam(value = "货满有效期结束",required = false) @RequestParam(required = false) String costEndTime,
            @ApiParam(value = "使用单位",required = false) @RequestParam(required = false) String refUseUnit,
            @ApiParam(value = "页号",required = true) @RequestParam(required = true) Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam(required = true) Integer pageSize
    ){
        IPage<OrderExecBean> orderExecBeanIPage = retailOrderLogic.queryRetailOrderExec(costStartTime,costEndTime,refUseUnit,isCost,materialOldNumber, id, customerName, materialName,specification,
                startTime, endTime, orderStartTime,orderEndTime, serviceName,itemServiceName,newServiceName, type,k3CategoryNumber,projectName,enginRecordNo,
                policyCode,policyRemark,isOver,isDisplaceOrder,walletIds1,walletIds2,priceType, pageNum, pageSize);
        return ResponseHelper.success(orderExecBeanIPage);
    }


    @GetMapping("/exec/list/total")
    @ApiOperation("销售单执行明细-汇总")
    public ResponseHelper<IPage<OrderExecBean>> execListTotal(
            @ApiParam(value = "旧编码",required = false) @RequestParam(required = false) String materialOldNumber,
            @ApiParam(value = "销售单号",required = false) @RequestParam(required = false) String id,
            @ApiParam(value = "政策编号",required = false) @RequestParam(required = false) String policyCode,
            @ApiParam(value = "政策说明",required = false) @RequestParam(required = false) String policyRemark,
            @ApiParam(value = "经销商名称",required = false) @RequestParam(required = false) String customerName,
            @ApiParam(value = "辅材名称",required = false) @RequestParam(required = false) String materialName,
            @ApiParam(value = "规格型号",required = false) @RequestParam(required = false) String specification,
            @ApiParam(value = "开始时间",required = false) @RequestParam(required = false) String startTime,
            @ApiParam(value = "结束时间",required = false) @RequestParam(required = false) String endTime,
            @ApiParam(value = "订单开始时间",required = false) @RequestParam(required = false) String orderStartTime,
            @ApiParam(value = "订单结束时间",required = false) @RequestParam(required = false) String orderEndTime,
            @ApiParam(value = "表头业务员名称",required = false) @RequestParam(required = false) String serviceName,
            @ApiParam(value = "表体业务员名称",required = false) @RequestParam(required = false) String itemServiceName,
            @ApiParam(value = "归属业务员名称",required = false) @RequestParam(required = false) String newServiceName,
            @ApiParam(value = "类型(1=普通零售 2=政策零售 TRADE=商用工程 HOME=家用工程)",required = true) @RequestParam(required = true) String type,
            @ApiParam(value = "机型分类编号",required = false) @RequestParam(required = false) List<String> k3CategoryNumber,
            @ApiParam(value = "工程名",required = false) @RequestParam(required = false) String projectName,
            @ApiParam(value = "工程登录编号",required = false) @RequestParam(required = false) String enginRecordNo,
            @ApiParam(value = "发货是否完成 true=是 false=否",required = false) @RequestParam(required = false) Boolean isOver,
            @ApiParam(value = "是否置换过 true=是 false=否",required = false) @RequestParam(required = false) Boolean isDisplaceOrder,
            @ApiParam(value = "现金钱包",required = false) @RequestParam(required = false) List<String> walletIds1,
            @ApiParam(value = "返利钱包",required = false) @RequestParam(required = false) List<String> walletIds2,
            @ApiParam(value = "价格类型",required = false) @RequestParam(required = false) String priceType,
            @ApiParam(value = "是否剔除仓储收费",required = false) @RequestParam(required = false) String isCost,
            @ApiParam(value = "货满有效期开始",required = false) @RequestParam(required = false) String costStartTime,
            @ApiParam(value = "货满有效期结束",required = false) @RequestParam(required = false) String costEndTime,
            @ApiParam(value = "使用单位",required = false) @RequestParam(required = false) String refUseUnit,
            @ApiParam(value = "页号",required = true) @RequestParam(required = true) Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam(required = true) Integer pageSize
    ){
        IPage<OrderExecBean> orderExecBeanIPage = retailOrderLogic.queryRetailOrderExecTotal(costStartTime,costEndTime,refUseUnit,isCost,materialOldNumber, id, customerName, materialName,specification,
                startTime, endTime,orderStartTime,orderEndTime, serviceName,itemServiceName,newServiceName, type,k3CategoryNumber,projectName,enginRecordNo,
                policyCode,policyRemark,isOver,isDisplaceOrder,walletIds1,walletIds2,priceType, pageNum, pageSize);
        return ResponseHelper.success(orderExecBeanIPage);
    }


    @GetMapping("/exec/listTotalExport")
    @ApiOperation("导出销售单执行明细-汇总")
    public void execListExportTotal(
            @ApiParam(value = "旧编码",required = false) @RequestParam(required = false) String materialOldNumber,
            @ApiParam(value = "销售单号",required = false) @RequestParam(required = false) String id,
            @ApiParam(value = "政策编号",required = false) @RequestParam(required = false) String policyCode,
            @ApiParam(value = "政策说明",required = false) @RequestParam(required = false) String policyRemark,
            @ApiParam(value = "经销商名称",required = false) @RequestParam(required = false) String customerName,
            @ApiParam(value = "辅材名称",required = false) @RequestParam(required = false) String materialName,
            @ApiParam(value = "规格型号",required = false) @RequestParam(required = false) String specification,
            @ApiParam(value = "制单时间开始时间",required = false) @RequestParam(required = false) String startTime,
            @ApiParam(value = "制单时间结束时间",required = false) @RequestParam(required = false) String endTime,
            @ApiParam(value = "订单日期开始时间",required = false) @RequestParam(required = false) String orderStartTime,
            @ApiParam(value = "订单日期结束时间",required = false) @RequestParam(required = false) String orderEndTime,
            @ApiParam(value = "表头业务员名称",required = false) @RequestParam(required = false) String serviceName,
            @ApiParam(value = "表体业务员名称",required = false) @RequestParam(required = false) String itemServiceName,
            @ApiParam(value = "归属业务员名称",required = false) @RequestParam(required = false) String newServiceName,
            @ApiParam(value = "类型(1=普通零售 2=政策零售 TRADE=商用工程 HOME=家用工程)",required = true) @RequestParam(required = true) String type,
            @ApiParam(value = "机型分类编号",required = false) @RequestParam(required = false) List<String> k3CategoryNumber,
            @ApiParam(value = "工程名",required = false) @RequestParam(required = false) String projectName,
            @ApiParam(value = "工程登录编号",required = false) @RequestParam(required = false) String enginRecordNo,
            @ApiParam(value = "发货是否完成 true=是 false=否",required = false) @RequestParam(required = false) Boolean isOver,
            @ApiParam(value = "是否置换过 true=是 false=否",required = false) @RequestParam(required = false) Boolean isDisplaceOrder,
            @ApiParam(value = "现金钱包",required = false) @RequestParam(required = false) List<String> walletIds1,
            @ApiParam(value = "返利钱包",required = false) @RequestParam(required = false) List<String> walletIds2,
            @ApiParam(value = "价格类型",required = false) @RequestParam(required = false) String priceType,
            @ApiParam(value = "是否剔除仓储收费",required = false) @RequestParam(required = false) String isCost,
            @ApiParam(value = "货满有效期开始",required = false) @RequestParam(required = false) String costStartTime,
            @ApiParam(value = "货满有效期结束",required = false) @RequestParam(required = false) String costEndTime,
            @ApiParam(value = "使用单位",required = false) @RequestParam(required = false) String refUseUnit,
            HttpServletRequest request, HttpServletResponse response
    )  throws  Exception{
        IPage<OrderExecBean> list = retailOrderLogic.queryRetailOrderExecTotal(costStartTime,costEndTime,refUseUnit,isCost,materialOldNumber, id, customerName, materialName,specification,
                startTime, endTime,orderStartTime,orderEndTime, serviceName,itemServiceName,newServiceName, type,
                k3CategoryNumber,projectName, enginRecordNo,policyCode,policyRemark,
                isOver,isDisplaceOrder,walletIds1,walletIds2,priceType,1, -1);
        ExcelData excelData = retailOrderLogic.exportExecList(list.getRecords());
        ExcelUtils.exportExcel(request,response,"销售单执行明细汇总.xlsx",excelData);

    }



    @GetMapping("/exec/listExport")
    @ApiOperation("导出销售单执行明细")
    public void execListExport(
            @ApiParam(value = "旧编码",required = false) @RequestParam(required = false) String materialOldNumber,
            @ApiParam(value = "销售单号",required = false) @RequestParam(required = false) String id,
            @ApiParam(value = "经销商名称",required = false) @RequestParam(required = false) String customerName,
            @ApiParam(value = "辅材名称",required = false) @RequestParam(required = false) String materialName,
            @ApiParam(value = "规格型号",required = false) @RequestParam(required = false) String specification,
            @ApiParam(value = "制单时间开始时间",required = false) @RequestParam(required = false) String startTime,
            @ApiParam(value = "制单时间结束时间",required = false) @RequestParam(required = false) String endTime,
            @ApiParam(value = "订单日期开始时间",required = false) @RequestParam(required = false) String orderStartTime,
            @ApiParam(value = "订单日期结束时间",required = false) @RequestParam(required = false) String orderEndTime,
            @ApiParam(value = "表头业务员名称",required = false) @RequestParam(required = false) String serviceName,
            @ApiParam(value = "表体业务员名称",required = false) @RequestParam(required = false) String itemServiceName,
            @ApiParam(value = "归属业务员名称",required = false) @RequestParam(required = false) String newServiceName,
            @ApiParam(value = "类型(1=普通零售 2=政策零售 TRADE=商用工程 HOME=家用工程)",required = true) @RequestParam(required = true) String type,
            @ApiParam(value = "机型分类编号",required = false) @RequestParam(required = false) List<String> k3CategoryNumber,
            @ApiParam(value = "工程名",required = false) @RequestParam(required = false) String projectName,
            @ApiParam(value = "工程登录编号",required = false) @RequestParam(required = false) String enginRecordNo,
            @ApiParam(value = "销售政策编号",required = false) @RequestParam(required = false) String policyCode,
            @ApiParam(value = "销售政策说明",required = false) @RequestParam(required = false) String policyRemark,
            @ApiParam(value = "发货是否完成 true=是 false=否",required = false) @RequestParam(required = false) Boolean isOver,
            @ApiParam(value = "是否置换过 true=是 false=否",required = false) @RequestParam(required = false) Boolean isDisplaceOrder,
            @ApiParam(value = "现金钱包",required = false) @RequestParam(required = false) List<String> walletIds1,
            @ApiParam(value = "返利钱包",required = false) @RequestParam(required = false) List<String> walletIds2,
            @ApiParam(value = "价格类型",required = false) @RequestParam(required = false) String priceType,
            @ApiParam(value = "是否剔除仓储收费",required = false) @RequestParam(required = false) String isCost,
            @ApiParam(value = "货满有效期开始",required = false) @RequestParam(required = false) String costStartTime,
            @ApiParam(value = "货满有效期结束",required = false) @RequestParam(required = false) String costEndTime,
            @ApiParam(value = "使用单位",required = false) @RequestParam(required = false) String refUseUnit,

            HttpServletRequest request, HttpServletResponse response
    )  throws  Exception{
        IPage<OrderExecBean> list = retailOrderLogic.queryRetailOrderExec(costStartTime,costEndTime,refUseUnit,isCost,materialOldNumber, id, customerName, materialName,specification,
                startTime, endTime,orderStartTime,orderEndTime,
                serviceName,itemServiceName,newServiceName, type, k3CategoryNumber,projectName, enginRecordNo,policyCode,policyRemark,
                isOver,isDisplaceOrder,walletIds1,walletIds2,priceType,1, -1);
        ExcelData excelData = retailOrderLogic.exportExecList(list.getRecords());
        ExcelUtils.exportExcel(request,response,"销售单执行明细.xlsx",excelData);

    }


    @PostMapping("/initOld")
    @ApiOperation("初始化旧数据")
    public ResponseHelper initOld(
            MultipartFile file
    ) throws InterruptedException, IOException {
        List<Object> objects = ExcelUtils.importExcel(file);
        retailOrderLogic.importInit(objects);
        return ResponseHelper.success();
    }
}
