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


import cn.hutool.core.bean.BeanUtil;
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.ExcelData;
import com.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.bean.invoice.*;
import com.zfire.mall.manager.bean.listV2.dto.InvoiceProjectDTO;
import com.zfire.mall.manager.bean.sale.SaleOrderVO;
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.invoice.InvoiceLogic;
import com.zfire.mall.manager.logic.invoice.InvoiceRefundLogic;
import com.zfire.mall.manager.plus.entity.EnginOrder;
import com.zfire.mall.manager.plus.entity.InvoiceApplication;
import com.zfire.mall.manager.plus.entity.InvoiceApplicationOrder;
import com.zfire.mall.manager.plus.service.InvoiceApplicationOrderService;
import com.zfire.mall.manager.plus.service.InvoiceApplicationService;
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;

/**
 * @author CHANGE
 */
@Slf4j
@RestController
@Api(value = "申请发货单", tags = {"申请发货单"})
@RequestMapping(value = "/invoice", produces = "application/json; charset=utf-8")
public class InvoiceController {


    @Autowired
    InvoiceLogic invoiceLogic;

    @Autowired
    RedisLockRegistry redisLockRegistry;

    @Autowired
    InvoiceApplicationService invoiceApplicationService;

    @Autowired
    InvoiceApplicationOrderService invoiceApplicationOrderService;

    @Autowired
    InvoiceRefundLogic invoiceRefundLogic;

    @Autowired
    CommonLogic commonLogic;



    @ApiOperation(value = "零售添加发货订单列表")
    @GetMapping("/listOrder")
    public ResponseHelper<IPage<InvoiceOrder>> listOrder(
            @ApiParam(value = "订单号",required = false) @RequestParam(required = false) String id,
            @ApiParam(value = "仓库id",required = false) @RequestParam(required = false) String correspondId,
            @ApiParam(value = "类别名称",required = false) @RequestParam(required = false) String categoryName,
            @ApiParam(value = "规格型号",required = false) @RequestParam(required = false) String specification,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) throws Exception {
        IPage<InvoiceOrder> invoiceOrder = invoiceLogic.listOrder(specification,id,categoryName,correspondId,pageNum, pageSize);
        return ResponseHelper.success(invoiceOrder);
    }

    @ApiOperation(value = "零售申请发货单列表")
    @GetMapping("/list")
    public ResponseHelper<IPage<InvoiceBean>> list(
            @ApiParam(required = false, value = "发货申请单") @RequestParam(required = false) String id,
            @ApiParam(required = false, value = "1 非计划单 0计划单") @RequestParam(required = false) Integer automaticStatus,
            @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) 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 createBy,
            @ApiParam(required = false, value = "发货开始时间") @RequestParam(required = false) String startTime,
            @ApiParam(required = false, value = "物料名称") @RequestParam(required = false) String productName,
            @ApiParam(required = false, value = "发货结束时间") @RequestParam(required = false) String endTime,
            @ApiParam(required = false, value = "经销商编号") @RequestParam(required = false) String customerNumber,
            @ApiParam(required = false, value = "存货分类") @RequestParam(required = false) String categoryName,
            @ApiParam(required = false, value = "存货分类Id") @RequestParam(required = false) String categoryId,
            @ApiParam(required = false, value = "表体业务员") @RequestParam(required = false) String serviceId,
            @ApiParam(required = false, value = "业务员") @RequestParam(required = false) String k3ServiceId,
            @ApiParam(required = false, value = "审核人") @RequestParam(required = false) String approvalName,
            @ApiParam(required = false, value = "1 申请发货单   2退货单 3 直调发货单") @RequestParam(required = false) Integer type,
            @ApiParam(required = false, value = "单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=关闭") @RequestParam(required = false) String examineStatus,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) throws Exception {
        IPage<InvoiceBean> invoiceBean = invoiceLogic.list(automaticStatus,createBy,approvalName,categoryId,k3ServiceId,mainOrderId,id,categoryName,type, customerName, correspondId,correspondName, productName, specification,
                startTime, endTime, customerNumber, examineStatus,null, pageNum, pageSize,serviceId);
        return ResponseHelper.success(invoiceBean);
    }


    @ZfireList
    @PostMapping("/listRetailV2")
    @ApiOperation("零售申请发货单-列表V2")
    public ResponseHelper<IPage<InvoiceRetailVO>> listRetailV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<InvoiceRetailVO> list = invoiceLogic.listRetailV2(zfireParamBean,null);
        return ResponseHelper.success(list, new TypeReference<InvoiceRetailVO>() {});
    }

    @PostMapping("/listRetailV2/export")
    @ApiOperation("零售申请发货单-列表V2导出")
    public void listRetailV2Export(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<InvoiceRetailVO> list = invoiceLogic.listRetailV2(zfireParamBean,null);
        //3.导出
        FieldUtils.exportData(list.getRecords(),zfireParam.getExportFields(),request,response);
    }


    @ZfireList
    @PostMapping("/listRetailV3")
    @ApiOperation("零售申请发货单-列表V3")
    public ResponseHelper<IPage<InvoiceRetailVO>> listRetailV3(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<InvoiceRetailVO> list = invoiceLogic.listRetailV2(zfireParamBean,1);
        return ResponseHelper.success(list, new TypeReference<InvoiceRetailVO>() {});
    }

    @PostMapping("/listRetailV3/export")
    @ApiOperation("零售申请发货单-列表V3导出")
    public void listRetailV3Export(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<InvoiceRetailVO> list = invoiceLogic.listRetailV2(zfireParamBean,1);
        //3.导出
        FieldUtils.exportData(list.getRecords(),zfireParam.getExportFields(),request,response);
    }



    @ApiOperation(value = "零售申请发货单详情")
    @GetMapping("/detail")
    public ResponseHelper<InvoiceBody> detail(
            @ApiParam(required = true, value = "id") @RequestParam(required = true) String id
    ) throws Exception {
        InvoiceBody invoiceBody = invoiceLogic.detail(id);
        return ResponseHelper.success(invoiceBody);
    }

    @PostMapping("/add")
    @ApiOperation("零售申请发货单添加")
    public ResponseHelper add(@RequestBody InvoiceRequestJson invoiceRequest, HttpServletRequest request) throws Exception {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_APPLICATION+adminUser.getCustomerId());
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            invoiceLogic.add(invoiceRequest);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation("零售申请发货单编辑")
    public ResponseHelper update(@RequestBody InvoiceRequestJson invoiceRequest, HttpServletRequest request) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_APPLICATION + invoiceRequest.getId());
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            InvoiceApplication invoiceApplicationServiceById = invoiceApplicationService.getById(invoiceRequest.getId());
            //申请发货单
            if (invoiceApplicationServiceById.getType().equals(1))
                invoiceLogic.update(invoiceRequest, request);
            //退货单
            if (invoiceApplicationServiceById.getType().equals(2))
                invoiceRefundLogic.update(invoiceRequest, request);
            //直调单
            if (invoiceApplicationServiceById.getType().equals(3))
                invoiceRefundLogic.updateRak(invoiceRequest, request);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/delete")
    @ApiOperation("零售申请发货单删除")
    public ResponseHelper delete(
            @ApiParam(required = true, value = "申请发货单ID") @RequestParam(required = true) String id
            , HttpServletRequest request) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_APPLICATION + id);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }

            InvoiceApplication invoiceApplicationServiceById = invoiceApplicationService.getById(id);
            if (invoiceApplicationServiceById.getType().equals(1))
                invoiceLogic.delete(id);
            if (invoiceApplicationServiceById.getType().equals(2))
                invoiceRefundLogic.delete(id);
            if (invoiceApplicationServiceById.getType().equals(3))
                invoiceRefundLogic.deleteRak(id);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/giveUp")
    @ApiOperation("零售申请发货单弃审")
    public ResponseHelper<String> giveUp(
            @ApiParam(required = true, value = "申请发货单ID") @RequestParam(required = true) String id
            , HttpServletRequest request) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_APPLICATION + id);
        String orderId = "";

        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }

            AdminUserCom adminUser = commonLogic.getAdminUser();
            if(adminUser.getIsCustomer()){
                throw new RemoteServiceException("暂无权限");
            }
            InvoiceApplication invoiceApplicationServiceById = invoiceApplicationService.getById(id);
            if (invoiceApplicationServiceById.getType().equals(1)) {
                orderId = invoiceLogic.giveUp(id);
            }
            if (invoiceApplicationServiceById.getType().equals(2))
                invoiceRefundLogic.giveUp(id);
            if (invoiceApplicationServiceById.getType().equals(3))
                orderId = invoiceRefundLogic.giveUpRak(id);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success(orderId);
    }

    @PostMapping("/submit")
    @ApiOperation("零售申请发货单申请撤回")
    public ResponseHelper submit(
            @ApiParam(required = true, value = "申请发货单ID") @RequestParam(required = true) String id,
            @ApiParam(required = false, value = "单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=关闭") @RequestParam(required = false) String examineStatus,
            HttpServletRequest request) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_APPLICATION + id);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }

            InvoiceApplication invoiceApplicationServiceById = invoiceApplicationService.getById(id);
            if (invoiceApplicationServiceById.getType().equals(1))
                invoiceLogic.submit(id, examineStatus);
            if (invoiceApplicationServiceById.getType().equals(2))
                invoiceRefundLogic.submit(id, examineStatus, request);
            if (invoiceApplicationServiceById.getType().equals(3))
                invoiceRefundLogic.submit(id, examineStatus, request);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/approvalInvoice")
    @ApiOperation("零售申请发货单审核")
    public ResponseHelper approvalInvoice(
            @RequestBody InvoiceRequestJson invoiceRequestJson,
            HttpServletRequest request) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_APPLICATION + invoiceRequestJson.getId());
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            AdminUserCom adminUser = commonLogic.getAdminUser();
            if(adminUser.getIsCustomer()){
                throw new RemoteServiceException("暂无权限");
            }
            InvoiceApplication invoiceApplicationServiceById = invoiceApplicationService.getById(invoiceRequestJson.getId());
            //正常申请发货单
            if (invoiceApplicationServiceById.getType().equals(1)) {
                if (invoiceApplicationServiceById.getAutomaticStatus()){
                    return ResponseHelper.success();
                }
                invoiceLogic.approvalInvoice(invoiceRequestJson);
            }
            //申请退货单
            if (invoiceApplicationServiceById.getType().equals(2))
                invoiceRefundLogic.approvalInvoice(invoiceRequestJson, request);
            //直调发货单
            if (invoiceApplicationServiceById.getType().equals(3))
                invoiceRefundLogic.approvalInvoiceRak(invoiceRequestJson, request);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/allApprovalInvoice")
    @ApiOperation("批量零售申请发货单审核")
    public ResponseHelper approvalInvoice(
            @ApiParam(required = true, value = "申请发货单ID") @RequestParam(required = true) List<String> ids,
            @ApiParam(required = false, value = "单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=关闭") @RequestParam(required = false) String examineStatus,
            @ApiParam(required = false, value = "审批说明") @RequestParam(required = false) String approvalRemark,
            HttpServletRequest request) throws Exception {
        List<String> list = ids.stream().distinct().collect(Collectors.toList());
        for (String id : list) {
            Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_APPLICATION + id);
            try {
                if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                    throw new RemoteServiceException("系统繁忙，请稍后再试");
                }
                AdminUserCom adminUser = commonLogic.getAdminUser();
                if(adminUser.getIsCustomer()){
                    throw new RemoteServiceException("暂无权限");
                }
                InvoiceApplication invoiceApplicationServiceById = invoiceApplicationService.getById(id);
                List<InvoiceApplicationOrder> listApplication = invoiceApplicationOrderService.lambdaQuery().eq(InvoiceApplicationOrder::getInvoiceId, invoiceApplicationServiceById.getId()).list();
                if (!invoiceApplicationServiceById.getExamineStatus().equals("OK")) {

                    InvoiceRequestJson invoiceRequestJson = BeanUtil.toBean(invoiceApplicationServiceById, InvoiceRequestJson.class);
                    invoiceRequestJson.setApprovalRemark(examineStatus);
                    invoiceRequestJson.setExamineStatus(examineStatus);
                    List<InvoiceApplicationOrderRequest> invoiceApplicationOrderRequests = BeanUtil.copyToList(listApplication, InvoiceApplicationOrderRequest.class);
                    invoiceRequestJson.setOrders(invoiceApplicationOrderRequests);

                    //正常申请发货单
                    if (invoiceApplicationServiceById.getType().equals(1)) {
                        if (invoiceApplicationServiceById.getAutomaticStatus()){
                            return ResponseHelper.success();
                        }
                        invoiceLogic.approvalInvoice(invoiceRequestJson);
                    }
                    //申请退货单
                    if (invoiceApplicationServiceById.getType().equals(2))
                        invoiceRefundLogic.approvalInvoice(invoiceRequestJson, request);
                    //直调发货单
                    if (invoiceApplicationServiceById.getType().equals(3))
                        invoiceRefundLogic.approvalInvoiceRak(invoiceRequestJson, request);
                }
            } finally {
                obtain.unlock();
            }
        }

        return ResponseHelper.success();
    }




    @GetMapping("/stockNumber")
    @ApiOperation("库存数量")
    public ResponseHelper<Integer> stockNumber(
            @ApiParam(required = true, value = "仓位ID") @RequestParam(required = true) List<String> StockIds,
            @ApiParam(required = true, value = "物料ID") @RequestParam(required = true) String materialId
            ) throws Exception {
        Integer number = invoiceLogic.stockNumber(StockIds,materialId);
        return ResponseHelper.success(number);
    }

    @ApiOperation(value = "零售申请订单列表导出")
    @GetMapping("/listExport")
    public void export(
            @ApiParam(required = false, value = "发货申请单") @RequestParam(required = false) 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) 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 startTime,
            @ApiParam(required = false, value = "物料名称") @RequestParam(required = false) String productName,
            @ApiParam(required = false, value = "发货结束时间") @RequestParam(required = false) String endTime,
            @ApiParam(required = false, value = "经销商编号") @RequestParam(required = false) String customerNumber,
            @ApiParam(required = false, value = "存货分类") @RequestParam(required = false) String categoryName,
            @ApiParam(required = false, value = "业务员") @RequestParam(required = false) String serviceId,
            @ApiParam(required = false, value = "存货分类Id") @RequestParam(required = false) String categoryId,
            @ApiParam(required = false, value = "业务员") @RequestParam(required = false) String approvalName,
            @ApiParam(required = false, value = "制单人") @RequestParam(required = false) String createBy,
            @ApiParam(required = false, value = "1 非计划单 0计划单") @RequestParam(required = false) Integer automaticStatus,

            @ApiParam(required = false, value = "业务员") @RequestParam(required = false) String k3ServiceId,
            @ApiParam(required = false, value = "1 申请发货单   2退货单 3 直调发货单") @RequestParam(required = false) Integer type,
            @ApiParam(required = false, value = "单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=关闭") @RequestParam(required = false) String examineStatus,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        IPage<InvoiceBean> invoiceBean = invoiceLogic.list(automaticStatus,createBy,approvalName,categoryId,k3ServiceId,mainOrderId,id,categoryName,type, customerName,correspondId,correspondName, productName, specification, startTime, endTime,
                customerNumber, examineStatus,null, 1, -1,serviceId);

        ExcelData excelData = invoiceLogic.listExport(invoiceBean.getRecords());
        ExcelUtils.exportExcel(request,response,"零售发货申请单.xlsx",excelData);
    }


    //////////////////////////////////////////工程申请发货单///////////////////////////




    @ApiOperation(value = "工程添加发货订单列表")
    @GetMapping("/listProjectOrder")
    public ResponseHelper<IPage<InvoiceProjectOrder>> listProjectOrder(
            @ApiParam(value = "工程订单号",required = false) @RequestParam(required = false) String enginOrderNo,
            @ApiParam(value = "工程登录号",required = false) @RequestParam(required = false) String  refEnginRecordNo,
            @ApiParam(value = "工程名称",required = false) @RequestParam(required = false) String refProjectName,
            @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 = "仓库id",required = false) @RequestParam(required = false) String correspondId,
            @ApiParam(value = "类别名称",required = false) @RequestParam(required = false) String categoryName,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) throws Exception {
        IPage<InvoiceProjectOrder> invoiceProjectOrder = invoiceLogic.listProjectOrder(enginOrderNo,refEnginRecordNo,categoryName, refProjectName,specification,startTime,endTime,pageNum, pageSize,correspondId);
        return ResponseHelper.success(invoiceProjectOrder);
    }

    @ApiOperation(value = "工程申请发货单详细信息")
    @GetMapping("/listProjectOrderDetail")
    public ResponseHelper<EnginOrder> listProjectOrderDetail(
            @ApiParam(value = "工程订单号",required = true) @RequestParam(required = true) String enginOrderNo
    ) throws Exception {
        EnginOrder enginOrder = invoiceLogic.listProjectOrderDetail(enginOrderNo);
        return ResponseHelper.success(enginOrder);
    }


    @ApiOperation(value = "工程申请发货单列表")
    @GetMapping("/listProject")
    public ResponseHelper<IPage<InvoiceProjectBean>> listProject(
            @ApiParam(required = false, value = "发货申请单") @RequestParam(required = false) String id,
            @ApiParam(required = false, value = "销售订单号") @RequestParam(required = false) String mainOrderId,
            @ApiParam(required = false, value = "经销商名称") @RequestParam(required = false) String customerName,
            @ApiParam(required = false, value = "使用单位") @RequestParam(required = false) String refUseUnit,
            @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 startTime,
            @ApiParam(required = false, value = "物料名称") @RequestParam(required = false) String productName,
            @ApiParam(required = false, value = "发货结束时间") @RequestParam(required = false) String endTime,
            @ApiParam(required = false, value = "工程登录编号") @RequestParam(required = false) String refEnginRecordNo,
            @ApiParam(required = false, value = "经销商编号") @RequestParam(required = false) String customerNumber,
            @ApiParam(required = false, value = "2家用工程  3商用工程") @RequestParam(required = false) String orderType,
            @ApiParam(required = false, value = "业务员") @RequestParam(required = false) String serviceId,
            @ApiParam(required = false, value = "表头业务员") @RequestParam(required = false) String k3ServiceId,
            @ApiParam(required = false, value = "存货分类") @RequestParam(required = false) String categoryId,
            @ApiParam(required = false, value = "审核人") @RequestParam(required = false) String approvalName,
            @ApiParam(required = false, value = "区域") @RequestParam(required = false) String refRegionWork,
            @ApiParam(required = false, value = "制单人") @RequestParam(required = false) String createBy,
            @ApiParam(required = false, value = "1 非计划单 0计划单") @RequestParam(required = false) Integer automaticStatus,
            @ApiParam(required = false, value = "1 申请发货单   2退货单 3 直调发货单") @RequestParam(required = false) Integer type,
            @ApiParam(required = false, value = "单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=关闭") @RequestParam(required = false) String examineStatus,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) throws Exception {
        IPage<InvoiceProjectBean> invoiceBean = invoiceLogic.listProject(automaticStatus,createBy,refRegionWork,approvalName,categoryId,k3ServiceId,refUseUnit,mainOrderId,id, customerName,type, correspondId,correspondName, productName, specification, startTime,
                endTime, customerNumber, examineStatus, orderType, pageNum, pageSize,refEnginRecordNo,serviceId);
        return ResponseHelper.success(invoiceBean);
    }


    @ZfireList
    @PostMapping("/listProjectV2")
    @ApiOperation("工程申请发货单-列表V2")
    public ResponseHelper<IPage<InvoiceProjectVO>> listProjectV2(
            @RequestBody InvoiceProjectDTO zfireParamBean
    ) throws RemoteServiceException {

        IPage<InvoiceProjectVO> list = invoiceLogic.listProjectV2(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<InvoiceProjectVO>() {});
    }

    @PostMapping("/listProjectV2/export")
    @ApiOperation("工程申请发货单-列表V2导出")
    public void listProjectV2Export(
            @RequestBody InvoiceProjectDTO zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<InvoiceProjectVO> list = invoiceLogic.listProjectV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(),zfireParam.getExportFields(),request,response);
    }



    @ApiOperation(value = "工程申请发货单详情")
    @GetMapping("/detailProject")
    public ResponseHelper<InvoiceProjectBody> detailProject(
            @ApiParam(required = true, value = "id") @RequestParam(required = true) String id
    ) throws Exception {
        InvoiceProjectBody invoiceBody = invoiceLogic.detailProject(id);
        return ResponseHelper.success(invoiceBody);
    }

    @PostMapping("/addProject")
    @ApiOperation("工程申请发货单添加")
    public ResponseHelper addProject(@RequestBody InvoiceRequestJson invoiceRequest, HttpServletRequest request) throws Exception {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_APPLICATION+adminUser.getCustomerId());
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            invoiceLogic.addProject(invoiceRequest);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/updateProject")
    @ApiOperation("工程申请发货单编辑")
    public ResponseHelper updateProject(@RequestBody InvoiceRequestJson invoiceRequest, HttpServletRequest request) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_APPLICATION + invoiceRequest.getId());
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }

            InvoiceApplication invoiceApplicationServiceById = invoiceApplicationService.getById(invoiceRequest.getId());

            if (invoiceApplicationServiceById.getType().equals(1)) {
                if (invoiceApplicationServiceById.getAutomaticStatus()){
                    return ResponseHelper.success();
                }
                invoiceLogic.updateProject(invoiceRequest, request);

            }
            if (invoiceApplicationServiceById.getType().equals(2))
                invoiceRefundLogic.update(invoiceRequest, request);
            if (invoiceApplicationServiceById.getType().equals(3)) {
                if (invoiceApplicationServiceById.getAutomaticStatus()){
                    return ResponseHelper.success();
                }
                invoiceRefundLogic.updateRak(invoiceRequest, request);
            }
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/deleteProject")
    @ApiOperation("工程申请发货单删除")
    public ResponseHelper deleteProject(
            @ApiParam(required = true, value = "申请发货单ID") @RequestParam(required = true) String id
            , HttpServletRequest request) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_APPLICATION + id);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }

            InvoiceApplication invoiceApplicationServiceById = invoiceApplicationService.getById(id);
            if (invoiceApplicationServiceById.getType().equals(1)) {
                if (invoiceApplicationServiceById.getAutomaticStatus()){
                    return ResponseHelper.success();
                }
                invoiceLogic.deleteProject(id);
            }
            if (invoiceApplicationServiceById.getType().equals(2))
                invoiceRefundLogic.delete(id);
            if (invoiceApplicationServiceById.getType().equals(3)) {
                if (invoiceApplicationServiceById.getAutomaticStatus()){
                    return ResponseHelper.success();
                }
                invoiceRefundLogic.deleteRak(id);
            }
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/submitProject")
    @ApiOperation("工程申请发货单申请撤回")
    public ResponseHelper submitProject(
            @ApiParam(required = true, value = "申请发货单ID") @RequestParam(required = true) String id,
            @ApiParam(required = false, value = "单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=关闭") @RequestParam(required = false) String examineStatus,
            HttpServletRequest request) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_APPLICATION + id);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }

            InvoiceApplication invoiceApplicationServiceById = invoiceApplicationService.getById(id);
            if (invoiceApplicationServiceById.getType().equals(1)) {
                if (invoiceApplicationServiceById.getAutomaticStatus()) {
                    return ResponseHelper.success();
                }
                invoiceLogic.submitProject(id, examineStatus);
            }
            if (invoiceApplicationServiceById.getType().equals(2))
                invoiceRefundLogic.submit(id, examineStatus, request);
            if (invoiceApplicationServiceById.getType().equals(3)) {
                if (invoiceApplicationServiceById.getAutomaticStatus()){
                    return ResponseHelper.success();
                }
                invoiceRefundLogic.submit(id, examineStatus, request);
            }
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/approvalInvoiceProject")
    @ApiOperation("工程申请发货单审核")
    public ResponseHelper approvalInvoiceProject(
            @RequestBody InvoiceRequestJson invoiceRequestJson,
            HttpServletRequest request) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_APPLICATION + invoiceRequestJson.getId());
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            AdminUserCom adminUser = commonLogic.getAdminUser();
            if(adminUser.getIsCustomer()){
                throw new RemoteServiceException("暂无权限");
            }
            InvoiceApplication invoiceApplicationServiceById = invoiceApplicationService.getById(invoiceRequestJson.getId());
            if (invoiceApplicationServiceById.getType().equals(1)) {
                if (invoiceApplicationServiceById.getAutomaticStatus()){
                    return ResponseHelper.success();
                }
                invoiceLogic.approvalInvoiceProject(invoiceRequestJson);
            }
            if (invoiceApplicationServiceById.getType().equals(2))
                invoiceRefundLogic.approvalInvoice(invoiceRequestJson, request);
            if (invoiceApplicationServiceById.getType().equals(3)) {
                if (invoiceApplicationServiceById.getAutomaticStatus()){
                    return ResponseHelper.success();
                }
                invoiceRefundLogic.approvalInvoiceRak(invoiceRequestJson, request);
            }
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/allApprovalInvoiceProject")
    @ApiOperation("批量工程申请发货单审核")
    public ResponseHelper approvalInvoiceProject(
            @ApiParam(required = true, value = "申请发货单ID") @RequestParam(required = true) List<String> ids,
            @ApiParam(required = false, value = "单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=关闭") @RequestParam(required = false) String examineStatus,
            @ApiParam(required = false, value = "审批说明") @RequestParam(required = false) String approvalRemark,
            HttpServletRequest request) throws Exception {
        List<String> list = ids.stream().distinct().collect(Collectors.toList());
        for (String id : list) {
            Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_APPLICATION + id);
            try {
                if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                    throw new RemoteServiceException("系统繁忙，请稍后再试");
                }
                AdminUserCom adminUser = commonLogic.getAdminUser();
                if(adminUser.getIsCustomer()){
                    throw new RemoteServiceException("暂无权限");
                }
                InvoiceApplication invoiceApplicationServiceById = invoiceApplicationService.getById(id);
                List<InvoiceApplicationOrder> listApplication = invoiceApplicationOrderService.lambdaQuery().eq(InvoiceApplicationOrder::getInvoiceId, invoiceApplicationServiceById.getId()).list();
                if (!invoiceApplicationServiceById.getExamineStatus().equals("OK")) {

                    InvoiceRequestJson invoiceRequestJson = BeanUtil.toBean(invoiceApplicationServiceById, InvoiceRequestJson.class);
                    invoiceRequestJson.setApprovalRemark(examineStatus);
                    invoiceRequestJson.setExamineStatus(examineStatus);
                    List<InvoiceApplicationOrderRequest> invoiceApplicationOrderRequests = BeanUtil.copyToList(listApplication, InvoiceApplicationOrderRequest.class);
                    invoiceRequestJson.setOrders(invoiceApplicationOrderRequests);
                    if (invoiceApplicationServiceById.getType().equals(1)) {
                        if (invoiceApplicationServiceById.getAutomaticStatus()){
                            return ResponseHelper.success();
                        }
                        invoiceLogic.approvalInvoiceProject(invoiceRequestJson);
                    }

                    if (invoiceApplicationServiceById.getType().equals(2))
                        invoiceRefundLogic.approvalInvoice(invoiceRequestJson, request);

                    if (invoiceApplicationServiceById.getType().equals(3)) {
                        if (invoiceApplicationServiceById.getAutomaticStatus()){
                            return ResponseHelper.success();
                        }
                        invoiceRefundLogic.approvalInvoiceRak(invoiceRequestJson, request);
                    }
                }
            } finally {
                obtain.unlock();
            }
        }

        return ResponseHelper.success();
    }



    @ApiOperation(value = "工程申请订单列表导出")
    @GetMapping("/listProjectExport")
    public void listProjectExport(
            @ApiParam(required = false, value = "发货申请单") @RequestParam(required = false) String id,
            @ApiParam(required = false, value = "销售订单号") @RequestParam(required = false) String mainOrderId,
            @ApiParam(required = false, value = "经销商名称") @RequestParam(required = false) String customerName,
            @ApiParam(required = false, value = "使用单位") @RequestParam(required = false) String refUseUnit,
            @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 startTime,
            @ApiParam(required = false, value = "物料名称") @RequestParam(required = false) String productName,
            @ApiParam(required = false, value = "发货结束时间") @RequestParam(required = false) String endTime,
            @ApiParam(required = false, value = "工程信息单号") @RequestParam(required = false) String refEnginRecordNo,
            @ApiParam(required = false, value = "经销商编号") @RequestParam(required = false) String customerNumber,
            @ApiParam(required = false, value = "2家用工程  3商用工程") @RequestParam(required = false) String orderType,
            @ApiParam(required = false, value = "1 申请发货单   2退货单 3 直调发货单") @RequestParam(required = false) Integer type,
            @ApiParam(required = false, value = "业务员") @RequestParam(required = false) String serviceId,
            @ApiParam(required = false, value = "表头业务员") @RequestParam(required = false) String k3ServiceId,
            @ApiParam(required = false, value = "存货分类Id") @RequestParam(required = false) String categoryId,
            @ApiParam(required = false, value = "审核人") @RequestParam(required = false) String approvalName,
            @ApiParam(required = false, value = "区域") @RequestParam(required = false) String refRegionWork,
            @ApiParam(required = false, value = "制单人") @RequestParam(required = false) String createBy,
            @ApiParam(required = false, value = "1 非计划单 0计划单") @RequestParam(required = false) Integer automaticStatus,

            @ApiParam(required = false, value = "单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=关闭") @RequestParam(required = false) String examineStatus,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        IPage<InvoiceProjectBean> invoiceBean = invoiceLogic.listProject(automaticStatus,createBy,refRegionWork,approvalName,categoryId,k3ServiceId,refUseUnit,mainOrderId,id, customerName,type, correspondId,correspondName, productName, specification, startTime,
                endTime, customerNumber, examineStatus, orderType, 1, -1,refEnginRecordNo,serviceId);
        ExcelData excelData = invoiceLogic.listProjectExport(invoiceBean.getRecords());
        ExcelUtils.exportExcel(request,response,"工程发货申请单.xlsx",excelData);
    }

    @PostMapping("/projectGiveUp")
    @ApiOperation("工程申请发货单弃审")
    public ResponseHelper<String> projectGiveUp(
            @ApiParam(required = true, value = "申请发货单ID") @RequestParam(required = true) String id
            , HttpServletRequest request) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.INVOICE_APPLICATION + id);
        String order = "";
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            AdminUserCom adminUser = commonLogic.getAdminUser();
            if(adminUser.getIsCustomer()){
                throw new RemoteServiceException("暂无权限");
            }
            InvoiceApplication invoiceApplicationServiceById = invoiceApplicationService.getById(id);
            if (invoiceApplicationServiceById.getType().equals(1)) {
                if (invoiceApplicationServiceById.getAutomaticStatus()){
                    return ResponseHelper.success();
                }
                order = invoiceLogic.projectGiveUp(id);
            }
            if (invoiceApplicationServiceById.getType().equals(2))
                invoiceRefundLogic.giveUp(id);
            if (invoiceApplicationServiceById.getType().equals(3)) {
                if (invoiceApplicationServiceById.getAutomaticStatus()){
                    return ResponseHelper.success();
                }
                order = invoiceRefundLogic.giveUpRak(id);
            }
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success(order);
    }





    @ApiOperation(value = "订单查看发货单和退货单")
    @GetMapping("/listInvoiceRetreat")
    public ResponseHelper<List<InvoiceRetreatOrder>> listInvoiceRetreat(
            @ApiParam(required = false, value = "销售订单号") @RequestParam(required = false) String mainOrderId
    ) throws Exception {
        List<InvoiceRetreatOrder> invoiceBean = invoiceLogic.listInvoiceRetreat(mainOrderId);
        return ResponseHelper.success(invoiceBean);
    }



    @ApiOperation(value = "申请发货单汇总列表")
    @GetMapping("/listInvoice")
    public ResponseHelper<IPage<InvoiceBean>> listInvoice(
            @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 materialOldNumber,
            @ApiParam(required = false, value = "规格型号") @RequestParam(required = false) String specification,
            @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 createStartTime,
            @ApiParam(required = false, value = "创建结束时间") @RequestParam(required = false) String createEndTime,
            @ApiParam(required = false, value = "发货申请单") @RequestParam(required = false) String id,
            @ApiParam(required = false, value = "订单查询") @RequestParam(required = false) String mainOrderId,
            @ApiParam(required = false, value = "经销商名称") @RequestParam(required = false) String customerName,
            @ApiParam(required = false, value = "经销商编号") @RequestParam(required = false) String customerNumber,
            @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 categoryName,
            @ApiParam(required = false, value = "存货分类Id") @RequestParam(required = false) String categoryId,
            @ApiParam(required = false, value = "业务员") @RequestParam(required = false) String serviceId,
            @ApiParam(required = false, value = "工程登录单号") @RequestParam(required = false) String refEnginRecordNo,
            @ApiParam(required = false, value = "表头业务员") @RequestParam(required = false) String k3ServiceId,
            @ApiParam(required = false, value = "审核开始时间") @RequestParam(required = false) String approvalStartTime,
            @ApiParam(required = false, value = "审核结束时间") @RequestParam(required = false) String approvalEndTime,
            @ApiParam(required = false, value = "预约提货打印1打印 0未打印") @RequestParam(required = false) Integer printStatus,
            @ApiParam(required = false, value = "发货汇总 是否打印 1打印 0未打印") @RequestParam(required = false) Integer invoicePrintStatus,
            @ApiParam(required = false, value = "制单人") @RequestParam(required = false) String createBy,
            @ApiParam(required = false, value = "GZ 广州  FS佛山") @RequestParam(required = false) String discriminate,
            @ApiParam(required = false, value = "1 申请发货单   2退货单 3 直调发货单") @RequestParam(required = false) Integer type,
            @ApiParam(required = false, value = "单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=关闭") @RequestParam(required = false) String examineStatus,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) throws Exception {
        IPage<InvoiceBean> invoiceBean = invoiceLogic.listInvoice(invoicePrintStatus,createBy,refEnginRecordNo,discriminate,materialOldNumber
                ,printStatus,type,approvalStartTime,approvalEndTime,categoryId,k3ServiceId,mainOrderId,id,categoryName, customerName,
                correspondId,correspondName, materialName,materialNumber,createStartTime,
                createEndTime, specification, startTime,
                endTime, customerNumber, examineStatus, pageNum, pageSize,serviceId);
        return ResponseHelper.success(invoiceBean);
    }


    @ZfireList
    @PostMapping("/listV2")
    @ApiOperation("申请发货单汇总-列表V2")
    public ResponseHelper<IPage<InvoiceVO>> listV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<InvoiceVO> list = invoiceLogic.listV2(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<InvoiceVO>() {});
    }

    @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<InvoiceVO> list = invoiceLogic.listV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(),zfireParam.getExportFields(),request,response);
    }

    @ApiOperation(value = "发货总数量V2")
    @PostMapping("/listInvoiceNumberV2")
    public ResponseHelper<InvoiceNumber> listInvoiceNumberV2(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        InvoiceNumber invoiceBean = invoiceLogic.listInvoiceNumberV2(zfireParamBean);
        return ResponseHelper.success(invoiceBean, new TypeReference<InvoiceNumber>() {});
    }







    @ApiOperation(value = "发货总数量")
    @GetMapping("/listInvoiceNumber")
    public ResponseHelper<InvoiceNumber> listInvoiceNumber(
            @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 materialOldNumber,
            @ApiParam(required = false, value = "规格型号") @RequestParam(required = false) String specification,
            @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 createStartTime,
            @ApiParam(required = false, value = "创建结束时间") @RequestParam(required = false) String createEndTime,
            @ApiParam(required = false, value = "发货申请单") @RequestParam(required = false) String id,
            @ApiParam(required = false, value = "订单查询") @RequestParam(required = false) String mainOrderId,
            @ApiParam(required = false, value = "经销商名称") @RequestParam(required = false) String customerName,
            @ApiParam(required = false, value = "经销商编号") @RequestParam(required = false) String customerNumber,
            @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 categoryName,
            @ApiParam(required = false, value = "存货分类Id") @RequestParam(required = false) String categoryId,
            @ApiParam(required = false, value = "业务员") @RequestParam(required = false) String serviceId,
            @ApiParam(required = false, value = "表头业务员") @RequestParam(required = false) String k3ServiceId,
            @ApiParam(required = false, value = "预约提货打印1打印 0未打印") @RequestParam(required = false) Integer printStatus,
            @ApiParam(required = false, value = "发货汇总 是否打印 1打印 0未打印") @RequestParam(required = false) Integer invoicePrintStatus,
            @ApiParam(required = false, value = "GZ 广州  FS佛山") @RequestParam(required = false) String discriminate,
            @ApiParam(required = false, value = "工程登录单号") @RequestParam(required = false) String refEnginRecordNo,
            @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 createBy,
            @ApiParam(required = false, value = "1 申请发货单   2退货单 3 直调发货单") @RequestParam(required = false) Integer type,
            @ApiParam(required = false, value = "单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=关闭") @RequestParam(required = false) String examineStatus

    ) throws Exception {
        InvoiceNumber invoiceBean = invoiceLogic.listInvoiceNumber(invoicePrintStatus,createBy,refEnginRecordNo,discriminate,materialOldNumber,printStatus,type,approvalStartTime,approvalEndTime,categoryId,k3ServiceId,mainOrderId,id,categoryName, customerName, correspondId,correspondName, materialName,materialNumber,createStartTime,
                createEndTime, specification, startTime,
                endTime, customerNumber, examineStatus, 1, -1,serviceId);
        return ResponseHelper.success(invoiceBean);
    }


    @ApiOperation(value = "申请发货单汇总列表-导出")
    @GetMapping("/exportInvoice")
    public void exportInvoice(
            @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 materialOldNumber,
            @ApiParam(required = false, value = "规格型号") @RequestParam(required = false) String specification,
            @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 createStartTime,
            @ApiParam(required = false, value = "创建结束时间") @RequestParam(required = false) String createEndTime,
            @ApiParam(required = false, value = "发货申请单") @RequestParam(required = false) String id,
            @ApiParam(required = false, value = "经销商名称") @RequestParam(required = false) String customerName,
            @ApiParam(required = false, value = "经销商编号") @RequestParam(required = false) String customerNumber,
            @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 categoryName,
            @ApiParam(required = false, value = "业务员") @RequestParam(required = false) String serviceId,
            @ApiParam(required = false, value = "存货分类Id") @RequestParam(required = false) String categoryId,
            @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 mainOrderId,
            @ApiParam(required = false, value = "表头业务员") @RequestParam(required = false) String k3ServiceId,
            @ApiParam(required = false, value = "制单人") @RequestParam(required = false) String createBy,

            @ApiParam(required = false, value = "预约提货打印1打印 0未打印") @RequestParam(required = false) Integer printStatus,
            @ApiParam(required = false, value = "发货汇总 是否打印 1打印 0未打印") @RequestParam(required = false) Integer invoicePrintStatus,
            @ApiParam(required = false, value = "GZ 广州  FS佛山") @RequestParam(required = false) String discriminate,
            @ApiParam(required = false, value = "工程登录单号") @RequestParam(required = false) String refEnginRecordNo,
            @ApiParam(required = false, value = "1 申请发货单   2退货单 3 直调发货单") @RequestParam(required = false) Integer type,
            @ApiParam(required = false, value = "单据状态 SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=关闭") @RequestParam(required = false) String examineStatus,
            HttpServletRequest request,HttpServletResponse response
    ) throws Exception {
        IPage<InvoiceBean> invoiceBean = invoiceLogic.listInvoiceHuizong(invoicePrintStatus,createBy,refEnginRecordNo,discriminate,materialOldNumber,printStatus,type,approvalStartTime,approvalEndTime,categoryId,k3ServiceId,mainOrderId,id,categoryName, customerName, correspondId,correspondName, materialName,materialNumber,createStartTime,
                createEndTime, specification, startTime,
                endTime, customerNumber, examineStatus, 1, -1,serviceId);
        ExcelData excelData = invoiceLogic.exportData(invoiceBean.getRecords());
        ExcelUtils.exportExcel(request,response,"申请发货单汇总列表.xlsx",excelData);
    }


    @PostMapping("/printInvoice")
    @ApiOperation("增加打印次数")
    public ResponseHelper printAdd(@ApiParam(value = "增加次数得产品详细ID", required = true) @RequestParam(required = true) List<String> ids, HttpServletRequest request) throws InterruptedException {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.PRINT_PICK_MANAGER);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            invoiceLogic.printAdd(ids, request);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }
}