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

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.annotation.ApiNotAuth;
import com.zfire.mall.manager.annotation.ZfireList;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.PoySaleCountBean;
import com.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.bean.listV2.LogisticsConfigurationVO;
import com.zfire.mall.manager.bean.sale.*;
import com.zfire.mall.manager.bean.stock.StockBeanCustomerToDay;
import com.zfire.mall.manager.bean.stock.StockVO;
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.common.CommonLogic;
import com.zfire.mall.manager.logic.saletype.SaleOrderLogic;
import com.zfire.mall.manager.logic.saletype.SaleRetreatOrderLogic;
import com.zfire.mall.manager.plus.entity.SaleType;
import com.zfire.mall.manager.plus.entity.SalesOrder;
import com.zfire.mall.manager.plus.service.SalesOrderService;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.stream.Collectors;

@Slf4j
@RestController
@Api(value = "销售出库单", tags ={"销售出库单"} )
@RequestMapping(value = "/sale/order", produces = "application/json; charset=utf-8")
public class SaleOrderController {

    @Autowired
    SaleOrderLogic saleOrderLogic;

    @Autowired
    RedisLockRegistry redisLockRegistry;

   @Autowired
   SalesOrderService salesOrderService;

   @Autowired
   SaleRetreatOrderLogic saleRetreatOrderLogic;

   @Autowired
   CommonLogic commonLogic;


    @ApiOperation(value = "统计")
    @GetMapping("/listCount")
    public ResponseHelper<PoySaleCountBean> listCount(
            @ApiParam(required = false, value = "发货申请单") @RequestParam(required = false) String orderNo,
            @ApiParam(required = false, value = "经销商名称") @RequestParam(required = false) String customerName,
            @ApiParam(required = false, value = "仓库ID") @RequestParam(required = false) String correspondId,
            @ApiParam(required = false, value = "仓库名称") @RequestParam(required = false) String correspondName,
            @ApiParam(required = false, value = "规格型号") @RequestParam(required = false) String specification,
            @ApiParam(required = false, value = "物料名称") @RequestParam(required = false) String materialName,
            @ApiParam(required = false, value = "物料编码") @RequestParam(required = false) String materialNumber,
            @ApiParam(required = false, value = "经销商编号") @RequestParam(required = false) String customerNumber,
            @ApiParam(required = false, value = "单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=关闭") @RequestParam(required = false) String examineStatus,
            @ApiParam(required = false, value = "单据开始时间") @RequestParam(required = false) String startTime,
            @ApiParam(required = false, value = "单据结束时间") @RequestParam(required = false) String endTime
    ) throws Exception {
        PoySaleCountBean list = saleOrderLogic.listCount(orderNo,correspondName,materialNumber, customerName,correspondId,specification,materialName,customerNumber, examineStatus,startTime,endTime);
        return ResponseHelper.success(list);
    }

    @ApiOperation(value = "列表")
    @GetMapping("/list")
    public ResponseHelper<IPage<SaleOrderBean>> list(
            @ApiParam(required = false, value = "发货申请单") @RequestParam(required = false) List<String> orderNo,
            @ApiParam(required = false, value = "销售出库单号") @RequestParam(required = false) List<String> id,
            @ApiParam(required = false, value = "订单号") @RequestParam(required = false) String mainOrderId,
            @ApiParam(required = false, value = "经销商名称") @RequestParam(required = false) String customerName,
            @ApiParam(required = false, value = "仓库ID") @RequestParam(required = false) List<String> correspondId,
            @ApiParam(required = false, value = "仓库名称") @RequestParam(required = false) String correspondName,
            @ApiParam(required = false, value = "规格型号") @RequestParam(required = false) String specification,
            @ApiParam(required = false, value = "物料名称") @RequestParam(required = false) String materialName,
            @ApiParam(required = false, value = "物料编码") @RequestParam(required = false) String materialNumber,
            @ApiParam(required = false, value = "经销商编号") @RequestParam(required = false) String customerNumber,
            @ApiParam(required = false, value = "单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=关闭") @RequestParam(required = false) String examineStatus,
            @ApiParam(required = false, value = "单据开始时间") @RequestParam(required = false) String startTime,
            @ApiParam(required = false, value = "单据结束时间") @RequestParam(required = false) String endTime,
            @ApiParam(required = false, value = "倒叙打印时间 1倒叙 不传默认创建时间") @RequestParam(required = false) String printTimeDesc,
            @ApiParam(required = false, value = "打印开始时间") @RequestParam(required = false) String printStartTime,
            @ApiParam(required = false, value = "打印结束时间") @RequestParam(required = false) String printEndTime,
            @ApiParam(required = false, value = "推送状态  0未推送 1推送成功 -1失败推送") @RequestParam(required = false) Integer syncStatus,
            @ApiParam(required = false, value = "制单人") @RequestParam(required = false) String createBy,
            @ApiParam(required = false, value = "审核开始时间") @RequestParam(required = false) String approvalStartTime,
            @ApiParam(required = false, value = "审核结束时间") @RequestParam(required = false) String approvalEndTime,
            @ApiParam(required = false, value = "存货类别ID") @RequestParam(required = false) String categoryId,
            @ApiParam(required = false, value = "发货开始时间") @RequestParam(required = false) String invoiceStartTime,
            @ApiParam(required = false, value = "发货结束时间") @RequestParam(required = false) String invoiceEndTime,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) throws Exception {
        IPage<SaleOrderBean> list = saleOrderLogic.list(categoryId,invoiceStartTime,invoiceEndTime,createBy,syncStatus,printStartTime,printEndTime,printTimeDesc,approvalStartTime,approvalEndTime,mainOrderId,id,orderNo,correspondName,materialNumber, customerName,correspondId,specification,materialName,customerNumber, examineStatus,startTime,endTime, pageNum, pageSize);
        return ResponseHelper.success(list);
    }


    @ZfireList
    @PostMapping("/listV2")
    @ApiOperation("执行明细销售出库单-列表V2")
    public ResponseHelper<IPage<SaleOrderVO>> listV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<SaleOrderVO> list = saleOrderLogic.listV2(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<SaleOrderVO>() {});
    }

    @PostMapping("/listV2/export")
    @ApiOperation("执行明细销售出库单-列表V2导出")
    public void listV2Export(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<SaleOrderVO> list = saleOrderLogic.listV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(),zfireParam.getExportFields(),request,response);
    }

    @ZfireList
    @PostMapping("/listV3")
    @ApiOperation("销售出库单-列表listV3")
    public ResponseHelper<IPage<SaleOrderVOV>> listV3(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<SaleOrderVOV> list = saleOrderLogic.listV3(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<SaleOrderVOV>() {});
    }

    @PostMapping("/listV3/export")
    @ApiOperation("销售出库单-列表listV3导出")
    public void listV3(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<SaleOrderVOV> list = saleOrderLogic.listV3(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(),zfireParam.getExportFields(),request,response);
    }


    @ApiOperation(value = "销售出库单查询发货单是否有弃审")
    @GetMapping("/listOrderNo")
    public ResponseHelper<String> listOrderNo(
            @ApiParam(required = false, value = "发货申请单") @RequestParam(required = false) List<String> orderNo

    ) throws Exception {
        String list = saleOrderLogic.listOrderNo(orderNo);
        return ResponseHelper.success(list);
    }


    @ApiOperation(value = "详情")
    @GetMapping("/detail")
    public ResponseHelper<SaleOrderBody> detail(
            @ApiParam(required = true, value = "id") @RequestParam(required = true) String id
    ) throws Exception {
        SaleOrderBody detail = saleOrderLogic.detail(id);
        return ResponseHelper.success(detail);
    }



    @ApiOperation(value = "审批")
    @PostMapping("/approval")
    public ResponseHelper<SaleType> approval(
            @RequestBody(required = true) SalesOrder salesOrder
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.PICK_MANAGER+salesOrder.getId());
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            AdminUserCom adminUser = commonLogic.getAdminUser();
            if(adminUser.getIsCustomer()){
                throw new RemoteServiceException("暂无权限");
            }
            SalesOrder byId = salesOrderService.getById(salesOrder.getId());
            if (byId.getType().equals(1))
                saleOrderLogic.approval(salesOrder);

            if (byId.getType().equals(2))
                saleRetreatOrderLogic.approval(salesOrder);

        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }


    @ApiOperation(value = "批量审批")
    @PostMapping("/allApproval")
    public ResponseHelper<SaleType> approval(
            @ApiParam(required = false, value = "销售出库单id") @RequestParam(required = false) List<String> ids,
            @ApiParam(value = "单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=已关闭",required = true) @RequestParam(required = true) String examineStatus,
            @ApiParam(value = "备注",required = false) @RequestParam(required = false) String approvalRemark
    ) throws Exception {
            List<String> collect = ids.stream().distinct().collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(collect) && collect.size() > 20)
                throw new RemoteServiceException("系统繁忙，请稍后再试");

        AdminUserCom adminUser = commonLogic.getAdminUser();
        if(adminUser.getIsCustomer()){
            throw new RemoteServiceException("暂无权限");
        }
            for (String id : collect) {
                Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.PICK_MANAGER + id);
                try {
                    if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                        throw new RemoteServiceException("系统繁忙，请稍后再试");
                    }
                SalesOrder salesOrder = new SalesOrder();
                salesOrder.setId(id);
                salesOrder.setExamineStatus(examineStatus);
                salesOrder.setApprovalRemark(approvalRemark);
                SalesOrder byId = salesOrderService.getById(id);

                saleOrderLogic.approvalJudge(id);

                if (byId.getType().equals(1))
                    saleOrderLogic.approval(salesOrder);

                if (byId.getType().equals(2))
                    saleRetreatOrderLogic.approval(salesOrder);
                } finally {
                    obtain.unlock();
                }
            }
        return ResponseHelper.success();
    }

    @ApiOperation(value = "反审")
    @PostMapping("/trialApproval")
    public ResponseHelper<SaleType> trialApproval(
            @ApiParam(required = false, value = "销售出库单id") @RequestParam(required = false) String id,
            @ApiParam(value = "单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=已关闭",required = false) @RequestParam(required = false) String examineStatus,
            @ApiParam(value = "备注",required = false) @RequestParam(required = false) String approvalRemark
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.PICK_MANAGER+id);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            AdminUserCom adminUser = commonLogic.getAdminUser();
            if(adminUser.getIsCustomer()){
                throw new RemoteServiceException("暂无权限");
            }
            SalesOrder byId = salesOrderService.getById(id);
            if (byId.getType().equals(1))
                saleOrderLogic.trialApproval(id,examineStatus,approvalRemark);
            if (byId.getType().equals(2))
                saleRetreatOrderLogic.trialApproval(id,examineStatus,approvalRemark);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }


    @ApiOperation(value = "销售出库单导出")
    @GetMapping("/export")
    public void export(
            @ApiParam(required = false, value = "发货申请单") @RequestParam(required = false) List<String> orderNo,
            @ApiParam(required = false, value = "销售出库单号") @RequestParam(required = false) List<String> id,
            @ApiParam(required = false, value = "订单号") @RequestParam(required = false) String mainOrderId,
            @ApiParam(required = false, value = "经销商名称") @RequestParam(required = false) String customerName,
            @ApiParam(required = false, value = "仓库ID") @RequestParam(required = false) List<String> correspondId,
            @ApiParam(required = false, value = "仓库名称") @RequestParam(required = false) String correspondName,
            @ApiParam(required = false, value = "规格型号") @RequestParam(required = false) String specification,
            @ApiParam(required = false, value = "物料名称") @RequestParam(required = false) String materialName,
            @ApiParam(required = false, value = "物料编码") @RequestParam(required = false) String materialNumber,
            @ApiParam(required = false, value = "经销商编号") @RequestParam(required = false) String customerNumber,
            @ApiParam(required = false, value = "单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=关闭") @RequestParam(required = false) String examineStatus,
            @ApiParam(required = false, value = "单据开始时间") @RequestParam(required = false) String startTime,
            @ApiParam(required = false, value = "倒叙打印时间 1倒叙 不传默认创建时间") @RequestParam(required = false) String printTimeDesc,
            @ApiParam(required = false, value = "打印开始时间") @RequestParam(required = false) String printStartTime,
            @ApiParam(required = false, value = "打印结束时间") @RequestParam(required = false) String printEndTime,
            @ApiParam(required = false, value = "单据结束时间") @RequestParam(required = false) String endTime,
            @ApiParam(required = false, value = "审核开始时间") @RequestParam(required = false) String approvalStartTime,
            @ApiParam(required = false, value = "审核结束时间") @RequestParam(required = false) String approvalEndTime,
            @ApiParam(required = false, value = "存货类别ID") @RequestParam(required = false) String categoryId,
            @ApiParam(required = false, value = "发货开始时间") @RequestParam(required = false) String invoiceStartTime,
            @ApiParam(required = false, value = "发货结束时间") @RequestParam(required = false) String invoiceEndTime,
            @ApiParam(required = false, value = "推送状态  0未推送 1推送成功 -1失败推送") @RequestParam(required = false) Integer syncStatus,
            @ApiParam(required = false, value = "制单人") @RequestParam(required = false) String createBy,

            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        IPage<SaleOrderBean> list = saleOrderLogic.list(categoryId,invoiceStartTime,invoiceEndTime,createBy,syncStatus,printStartTime,printEndTime,printTimeDesc,approvalStartTime,approvalEndTime,mainOrderId,id,orderNo,correspondName,materialNumber, customerName,correspondId,specification
                ,materialName,customerNumber, examineStatus,startTime,endTime, 1, -1);
        ExcelData excelData = saleOrderLogic.export(list.getRecords());
        ExcelUtils.exportExcel(request,response,"销售出库单列表.xlsx",excelData);
    }


    @ApiOperation(value = "出库列表销售出库单导出")
    @GetMapping("/exportChuku")
    public void exportChuKu(
            @ApiParam(required = false, value = "发货申请单") @RequestParam(required = false) List<String> orderNo,
            @ApiParam(required = false, value = "销售出库单号") @RequestParam(required = false) List<String> id,
            @ApiParam(required = false, value = "订单号") @RequestParam(required = false) String mainOrderId,
            @ApiParam(required = false, value = "经销商名称") @RequestParam(required = false) String customerName,
            @ApiParam(required = false, value = "仓库ID") @RequestParam(required = false) List<String> correspondId,
            @ApiParam(required = false, value = "仓库名称") @RequestParam(required = false) String correspondName,
            @ApiParam(required = false, value = "规格型号") @RequestParam(required = false) String specification,
            @ApiParam(required = false, value = "物料名称") @RequestParam(required = false) String materialName,
            @ApiParam(required = false, value = "物料编码") @RequestParam(required = false) String materialNumber,
            @ApiParam(required = false, value = "经销商编号") @RequestParam(required = false) String customerNumber,
            @ApiParam(required = false, value = "倒叙打印时间 1倒叙 不传默认创建时间") @RequestParam(required = false) String printTimeDesc,
            @ApiParam(required = false, value = "单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=关闭") @RequestParam(required = false) String examineStatus,
            @ApiParam(required = false, value = "单据开始时间") @RequestParam(required = false) String startTime,
            @ApiParam(required = false, value = "单据结束时间") @RequestParam(required = false) String endTime,
            @ApiParam(required = false, value = "审核开始时间") @RequestParam(required = false) String approvalStartTime,
            @ApiParam(required = false, value = "审核结束时间") @RequestParam(required = false) String approvalEndTime,
            @ApiParam(required = false, value = "打印开始时间") @RequestParam(required = false) String printStartTime,
            @ApiParam(required = false, value = "打印结束时间") @RequestParam(required = false) String printEndTime,
            @ApiParam(required = false, value = "存货类别ID") @RequestParam(required = false) String categoryId,
            @ApiParam(required = false, value = "发货开始时间") @RequestParam(required = false) String invoiceStartTime,
            @ApiParam(required = false, value = "发货结束时间") @RequestParam(required = false) String invoiceEndTime,
            @ApiParam(required = false, value = "推送状态  0未推送 1推送成功 -1失败推送") @RequestParam(required = false) Integer syncStatus,
            @ApiParam(required = false, value = "制单人") @RequestParam(required = false) String createBy,

            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        IPage<SaleOrderBean> list = saleOrderLogic.list(categoryId,invoiceStartTime,invoiceEndTime,createBy,syncStatus,printStartTime,printEndTime,printTimeDesc,approvalStartTime,approvalEndTime,mainOrderId,id,orderNo
                ,correspondName,materialNumber, customerName,correspondId,specification,materialName,customerNumber, examineStatus,startTime,endTime, 1, -1);
        ExcelData excelData = saleOrderLogic.exportChuKu(list.getRecords());
        ExcelUtils.exportExcel(request,response,"销售出库单列表.xlsx",excelData);
    }


    @ApiOperation(value = "审批进入判断")
    @PostMapping("/approvalJudge")
    public ResponseHelper approvalJudge(
            @ApiParam(required = false, value = "出库单号") @RequestParam(required = false) String id
    ) throws Exception {

            saleOrderLogic.approvalJudge(id);

        return ResponseHelper.success();
    }



    @ApiOperation(value = "仓储费报表列表")
    @GetMapping("/listStorageFee")
    public ResponseHelper<IPage<StorageFeeBean>> listStorageFee(
            @ApiParam(required = false, value = "发货申请单") @RequestParam(required = false) List<String> orderNo,
            @ApiParam(required = false, value = "销售出库单号") @RequestParam(required = false) List<String> id,
            @ApiParam(required = false, value = "订单类型 TRADE=商用 HOME=家用 RETAIL=零售单 RETAIL_POLICY 销售政策单  PERMU_HOME = 置换单家用        PERMU_TRADE 置换商用   PERMU_RETAIL  置换零售   PERMU_RETAIL_POLICY 置换销售政策单  REQUISITION_RETAIL 调拨单零售   REQUISITION_RETAIL_POLICY 调拨单销售政策REQUISITION_TRADE 调拨单商用 REQUISITION_HOME 调拨单家用") @RequestParam(required = false) String orderType,
            @ApiParam(required = false, value = "订单号") @RequestParam(required = false) String mainOrderId,
            @ApiParam(required = false, value = "订单开始时间") @RequestParam(required = false) String startTime,
            @ApiParam(required = false, value = "订单结束时间") @RequestParam(required = false) String endTime,
            @ApiParam(required = false, value = "发货开始时间") @RequestParam(required = false) String invoiceStartTime,
            @ApiParam(required = false, value = "发货结束时间") @RequestParam(required = false) String invoiceEndTime,
            @ApiParam(required = false, value = "仓库ID") @RequestParam(required = false) List<String> correspondId,
            @ApiParam(required = false, value = "物料分类编码") @RequestParam(required = false) List<String> categoryNum,
            @ApiParam(required = false, value = "经销商名称") @RequestParam(required = false) String customerName,
            @ApiParam(required = false, value = "经销商编号") @RequestParam(required = false) String customerNumber,
            @ApiParam(required = false, value = "物料名称") @RequestParam(required = false) String materialName,
            @ApiParam(required = false, value = "物料编码") @RequestParam(required = false) String materialNumber,
            @ApiParam(required = false, value = "规格型号") @RequestParam(required = false) String specification,
            @ApiParam(required = false, value = "审核开始时间") @RequestParam(required = false) String approvalStartTime,
            @ApiParam(required = false, value = "审核结束时间") @RequestParam(required = false) String approvalEndTime,
            @ApiParam(required = false, value = "出库单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=关闭") @RequestParam(required = false) String saleExamineStatus,
            @ApiParam(required = false, value = "出库开始时间") @RequestParam(required = false) String saleStartTime,
            @ApiParam(required = false, value = "出库结束时间") @RequestParam(required = false) String saleEndTime,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) throws Exception {
        IPage<StorageFeeBean> list = saleOrderLogic.listStorageFee(categoryNum,approvalStartTime,approvalEndTime,saleExamineStatus,saleStartTime,saleEndTime,invoiceStartTime,invoiceEndTime,mainOrderId,id,orderNo,materialNumber, customerName,correspondId,specification,materialName,customerNumber
                ,startTime,endTime, pageNum, pageSize);
        return ResponseHelper.success(list);
    }

    @ApiOperation(value = "仓储费报表导出")
    @GetMapping("/listStorageFeeExport")
    public void listStorageFeeExport(
            @ApiParam(required = false, value = "发货申请单") @RequestParam(required = false) List<String> orderNo,
            @ApiParam(required = false, value = "销售出库单号") @RequestParam(required = false) List<String> id,
            @ApiParam(required = false, value = "订单类型 TRADE=商用 HOME=家用 RETAIL=零售单 RETAIL_POLICY 销售政策单  PERMU_HOME = 置换单家用        PERMU_TRADE 置换商用   PERMU_RETAIL  置换零售   PERMU_RETAIL_POLICY 置换销售政策单  REQUISITION_RETAIL 调拨单零售   REQUISITION_RETAIL_POLICY 调拨单销售政策REQUISITION_TRADE 调拨单商用 REQUISITION_HOME 调拨单家用") @RequestParam(required = false) String orderType,
            @ApiParam(required = false, value = "订单号") @RequestParam(required = false) String mainOrderId,
            @ApiParam(required = false, value = "订单开始时间") @RequestParam(required = false) String startTime,
            @ApiParam(required = false, value = "订单结束时间") @RequestParam(required = false) String endTime,
            @ApiParam(required = false, value = "发货开始时间") @RequestParam(required = false) String invoiceStartTime,
            @ApiParam(required = false, value = "发货结束时间") @RequestParam(required = false) String invoiceEndTime,
            @ApiParam(required = false, value = "物料分类编码") @RequestParam(required = false) List<String> categoryNum,
            @ApiParam(required = false, value = "仓库ID") @RequestParam(required = false) List<String> correspondId,
            @ApiParam(required = false, value = "经销商名称") @RequestParam(required = false) String customerName,
            @ApiParam(required = false, value = "经销商编号") @RequestParam(required = false) String customerNumber,
            @ApiParam(required = false, value = "物料名称") @RequestParam(required = false) String materialName,
            @ApiParam(required = false, value = "物料编码") @RequestParam(required = false) String materialNumber,
            @ApiParam(required = false, value = "规格型号") @RequestParam(required = false) String specification,
            @ApiParam(required = false, value = "审核开始时间") @RequestParam(required = false) String approvalStartTime,
            @ApiParam(required = false, value = "审核结束时间") @RequestParam(required = false) String approvalEndTime,
            @ApiParam(required = false, value = "出库单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=关闭") @RequestParam(required = false) String saleExamineStatus,
            @ApiParam(required = false, value = "出库开始时间") @RequestParam(required = false) String saleStartTime,
            @ApiParam(required = false, value = "出库结束时间") @RequestParam(required = false) String saleEndTime,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        IPage<StorageFeeBean> list = saleOrderLogic.listStorageFee(categoryNum,approvalStartTime,approvalEndTime,saleExamineStatus,saleStartTime,saleEndTime,invoiceStartTime,invoiceEndTime,mainOrderId,id,orderNo,materialNumber, customerName,correspondId,specification,materialName,customerNumber
                ,startTime,endTime, 1, -1);
        ExcelData excelData = saleOrderLogic.listStorageFeeExport(list.getRecords());
        ExcelUtils.exportExcel(request,response,"仓储费报表.xlsx",excelData);
    }


    @ZfireList
    @PostMapping("/listStorageFeeV2")
    @ApiOperation("仓储费报表-列表V2")
    public ResponseHelper<IPage<StorageFeeBeanVO>> listStorageFeeV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<StorageFeeBeanVO> list = saleOrderLogic.listStorageFeeV2(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<StorageFeeBeanVO>() {});
    }

    @PostMapping("/listStorageFeeV2/export")
    @ApiOperation("仓储费报表-列表V2导出")
    public void listStorageFeeV2Export(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<StorageFeeBeanVO> list = saleOrderLogic.listStorageFeeV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(),zfireParam.getExportFields(),request,response);
    }


    @ApiNotAuth
    @ApiOperation(value = "同步销售出库单")
    @GetMapping("/syncOrder")
    public void stockInSyncOrder(

    ) throws Exception {
        saleOrderLogic.syncSalesOrder();

    }


    @ApiNotAuth
    @ApiOperation(value = "同步格匠采购入库单")
    @GetMapping("/syncInvoiceOrder")
    public void stockInSyncInvoiceOrder(

    ) throws Exception {

        saleOrderLogic.stockInSyncInvoiceOrder();

    }

    @ApiNotAuth
    @ApiOperation(value = "同步其他入库单")
    @GetMapping("/syncOtherOrder")
    public void stockInSyncOtherOrder(

    ) throws Exception {
        saleOrderLogic.stockInSyncOtherOrder();

    }
}
