package com.zfire.mall.manager.controller.engin.deposit;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.PoySaleCountBean;
import com.zfire.mall.manager.bean.engin.deposit.DepositManageBean;
import com.zfire.mall.manager.bean.engin.deposit.DepositMangeItemBean;
import com.zfire.mall.manager.bean.engin.deposit.EnginOrderInfo;
import com.zfire.mall.manager.constant.Constant;
import com.zfire.mall.manager.enums.OrderTypeEnum;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.logic.engin.deposit.DepositManageLogic;
import com.zfire.mall.manager.plus.entity.DepositManage;
import com.zfire.mall.manager.utils.excel.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@RestController
@Api(value = "押金管理", tags ={"押金管理"} )
@RequestMapping(value = "/deposit-manage", produces = "application/json; charset=utf-8")
public class DepositMangeController {


    @Resource
    DepositManageLogic depositManageLogic;
    @Resource
    RedisLockRegistry redisLockRegistry;
    @Resource
    CommonLogic commonLogic;

    @ApiOperation(value = "押金管理列表")
    @GetMapping("/list")
    public ResponseHelper<IPage<DepositManageBean>> list(
            @ApiParam(value = "客户编码/客户名称") @RequestParam(required = false) String customerKeyword,
            @ApiParam(value = "工程订单类型") @RequestParam String enginOrderType,
            @ApiParam(value = "登录单号") @RequestParam(required = false) String refEnginRecordNo,
            @ApiParam(value = "项目名称") @RequestParam(required = false) String refProjectName,
            @ApiParam(value = "行业类别") @RequestParam(required = false) String refTradeCategory,
            @ApiParam(value = "使用单位") @RequestParam(required = false) String refUseUnit,
            @ApiParam(value = "明细厂编号") @RequestParam(required = false) String factoryNo,
            @ApiParam(value = "特价编号") @RequestParam(required = false) String specialNo,
            @ApiParam(value = "物料编码") @RequestParam(required = false) String materialNumber,
            @ApiParam(value = "产品编码") @RequestParam(required = false) String materialOldNumber,
            @ApiParam(value = "产品名称") @RequestParam(required = false) String materialName,
            @ApiParam(value = "规格型号") @RequestParam(required = false) String specification,
            @ApiParam(value = "申请日期-开始") @RequestParam(required = false) String startSubmitTime,
            @ApiParam(value = "申请日期-结束") @RequestParam(required = false) String endSubmitTime,
            @ApiParam(value = "验收日期-开始") @RequestParam(required = false) String startConfirmTime,
            @ApiParam(value = "验收日期-结束") @RequestParam(required = false) String endConfirmTime,
            @ApiParam(value = "安装日期-开始") @RequestParam(required = false) String startInstallTime,
            @ApiParam(value = "安装日期-结束") @RequestParam(required = false) String endInstallTime,
            @ApiParam(value = "资料审核日期-开始") @RequestParam(required = false) String startAcceptTime,
            @ApiParam(value = "资料审核日期-结束") @RequestParam(required = false) String endAcceptTime,
            @ApiParam(value = "创建人") @RequestParam(required = false) String createName,
            @ApiParam(value = "审核人") @RequestParam(required = false) String confirmName,
            @ApiParam(value = "是否退押 false=否 true=是") @RequestParam(required = false) Boolean isRefundDeposit,
            @ApiParam(value = "状态 SAVE=保存 WAIT=待审核 OK=通过") @RequestParam(required = false) List<String> examineStatus,
            @ApiParam(value = "业务员") @RequestParam(required = false) String serviceId,
            @ApiParam(value = "格力内部备注") @RequestParam(required = false) String geLiInerNote,
            @ApiParam(value = "跨区厂编号") @RequestParam(required = false) String refFactoryNo,
            @ApiParam(value = "区域") @RequestParam(required = false) String refRegionWork,
            @ApiParam(value = "购买单位名称") @RequestParam(required = false) String refBuyUnitName,
            @ApiParam(value = "保证函") @RequestParam(required = false) String refPromiseProvide,
            @ApiParam(value = "特价项目编号") @RequestParam(required = false) String specialItemNo,
            @ApiParam(value = "厂编号-开始") @RequestParam(required = false) String startFactoryNo,
            @ApiParam(value = "厂编号-结束") @RequestParam(required = false) String endFactoryNo,
            @ApiParam(value = "特价编号-开始") @RequestParam(required = false) String startSpecialNo,
	        @ApiParam(value = "特价编号-结束") @RequestParam(required = false) String endSpecialNo,
	        @ApiParam(value = "交单编号-开始") @RequestParam(required = false) String startBillNo,
	        @ApiParam(value = "交单编号-结束") @RequestParam(required = false) String endBillNo,
            @ApiParam(value = "寄厂日期-开始") @RequestParam(required = false) String startMailFactoryDate,
            @ApiParam(value = "寄厂日期-结束") @RequestParam(required = false) String endMailFactoryDate,
            @ApiParam(value = "寄厂年度") @RequestParam(required = false) String mailFactoryYear,
            @ApiParam(value = "确认日期-开始") @RequestParam(required = false) String startConfirmDate,
            @ApiParam(value = "确认日期-结束") @RequestParam(required = false) String endConfirmDate,
            @ApiParam(value = "结算编号-开始") @RequestParam(required = false) String startSettlementNo,
            @ApiParam(value = "结算编号-结束") @RequestParam(required = false) String endSettlementNo,
            @ApiParam(value = "结算日期-开始") @RequestParam(required = false) String startSettlementDate,
            @ApiParam(value = "结算日期-结束") @RequestParam(required = false) String endSettlementDate,
            @ApiParam(value = "结算日期 1 为空 0 不为空") @RequestParam(required = false) Integer settlementStatus,
            @ApiParam(value = "寄厂日期 1 为空 0 不为空") @RequestParam(required = false) Integer mailFactoryStatus,
            @ApiParam(value = "入厂日期-开始") @RequestParam(required = false) String startInFactoryDate,
            @ApiParam(value = "入厂日期-结束") @RequestParam(required = false) String endInFactoryDate,
            @ApiParam(value = "入厂类型") @RequestParam(required = false) String inFactoryType,
            @ApiParam(value = "订单日期-开始") @RequestParam(required = false) String startOrderDate,
            @ApiParam(value = "订单日期-结束") @RequestParam(required = false) String endOrderDate,
            @ApiParam(value = "资料状态 NORMAL=正常 AMEND=补充 WAIT_REVIEW=待复核 REVIEW=复核") @RequestParam(required = false) List<String> dataStates,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<DepositManageBean> listPage = depositManageLogic.listPage(settlementStatus,mailFactoryStatus,customerKeyword, "enginOrderNo", enginOrderType,
                refEnginRecordNo, refProjectName, refTradeCategory, refUseUnit, startSubmitTime, endSubmitTime, startConfirmTime, endConfirmTime,
                createName, confirmName, isRefundDeposit, examineStatus, serviceId, materialNumber, materialOldNumber,
                materialName, specification, factoryNo, specialNo, geLiInerNote, refFactoryNo, refRegionWork,
                startInstallTime, endInstallTime, startAcceptTime, endAcceptTime, refBuyUnitName, refPromiseProvide,
                specialItemNo, startFactoryNo, endFactoryNo, startSpecialNo, endSpecialNo, startBillNo, endBillNo,
                startMailFactoryDate, endMailFactoryDate, mailFactoryYear, startConfirmDate, endConfirmDate,
                startSettlementNo, endSettlementNo, startSettlementDate, endSettlementDate, startInFactoryDate,
                endInFactoryDate, inFactoryType, startOrderDate, endOrderDate, dataStates, pageNum, pageSize);
        return ResponseHelper.success(listPage);
    }

    @ApiOperation(value = "押金管理列表统计")
    @GetMapping("/count-list")
    public ResponseHelper<IPage<DepositManageBean>> countList(
            @ApiParam(value = "客户编码/客户名称") @RequestParam(required = false) String customerKeyword,
            @ApiParam(value = "工程订单类型") @RequestParam String enginOrderType,
            @ApiParam(value = "登录单号") @RequestParam(required = false) String refEnginRecordNo,
            @ApiParam(value = "项目名称") @RequestParam(required = false) String refProjectName,
            @ApiParam(value = "行业类别") @RequestParam(required = false) String refTradeCategory,
            @ApiParam(value = "使用单位") @RequestParam(required = false) String refUseUnit,
            @ApiParam(value = "明细厂编号") @RequestParam(required = false) String factoryNo,
            @ApiParam(value = "特价编号") @RequestParam(required = false) String specialNo,
            @ApiParam(value = "物料编码") @RequestParam(required = false) String materialNumber,
            @ApiParam(value = "产品编码") @RequestParam(required = false) String materialOldNumber,
            @ApiParam(value = "产品名称") @RequestParam(required = false) String materialName,
            @ApiParam(value = "规格型号") @RequestParam(required = false) String specification,
            @ApiParam(value = "申请日期-开始") @RequestParam(required = false) String startSubmitTime,
            @ApiParam(value = "申请日期-结束") @RequestParam(required = false) String endSubmitTime,
            @ApiParam(value = "验收日期-开始") @RequestParam(required = false) String startConfirmTime,
            @ApiParam(value = "验收日期-结束") @RequestParam(required = false) String endConfirmTime,
            @ApiParam(value = "安装日期-开始") @RequestParam(required = false) String startInstallTime,
            @ApiParam(value = "安装日期-结束") @RequestParam(required = false) String endInstallTime,
            @ApiParam(value = "资料审核日期-开始") @RequestParam(required = false) String startAcceptTime,
            @ApiParam(value = "资料审核日期-结束") @RequestParam(required = false) String endAcceptTime,
            @ApiParam(value = "创建人") @RequestParam(required = false) String createName,
            @ApiParam(value = "审核人") @RequestParam(required = false) String confirmName,
            @ApiParam(value = "是否退押 false=否 true=是") @RequestParam(required = false) Boolean isRefundDeposit,
            @ApiParam(value = "状态 SAVE=保存 WAIT=待审核 OK=通过") @RequestParam(required = false) List<String> examineStatus,
            @ApiParam(value = "业务员") @RequestParam(required = false) String serviceId,
            @ApiParam(value = "格力内部备注") @RequestParam(required = false) String geLiInerNote,
            @ApiParam(value = "跨区厂编号") @RequestParam(required = false) String refFactoryNo,
            @ApiParam(value = "区域") @RequestParam(required = false) String refRegionWork,
            @ApiParam(value = "购买单位名称") @RequestParam(required = false) String refBuyUnitName,
            @ApiParam(value = "保证函") @RequestParam(required = false) String refPromiseProvide,
            @ApiParam(value = "特价项目编号") @RequestParam(required = false) String specialItemNo,
            @ApiParam(value = "厂编号-开始") @RequestParam(required = false) String startFactoryNo,
            @ApiParam(value = "厂编号-结束") @RequestParam(required = false) String endFactoryNo,
            @ApiParam(value = "特价编号-开始") @RequestParam(required = false) String startSpecialNo,
            @ApiParam(value = "特价编号-结束") @RequestParam(required = false) String endSpecialNo,
            @ApiParam(value = "交单编号-开始") @RequestParam(required = false) String startBillNo,
            @ApiParam(value = "交单编号-结束") @RequestParam(required = false) String endBillNo,
            @ApiParam(value = "寄厂日期-开始") @RequestParam(required = false) String startMailFactoryDate,
            @ApiParam(value = "寄厂日期-结束") @RequestParam(required = false) String endMailFactoryDate,
            @ApiParam(value = "寄厂年度") @RequestParam(required = false) String mailFactoryYear,
            @ApiParam(value = "确认日期-开始") @RequestParam(required = false) String startConfirmDate,
            @ApiParam(value = "确认日期-结束") @RequestParam(required = false) String endConfirmDate,
            @ApiParam(value = "结算编号-开始") @RequestParam(required = false) String startSettlementNo,
            @ApiParam(value = "结算编号-结束") @RequestParam(required = false) String endSettlementNo,
            @ApiParam(value = "结算日期-开始") @RequestParam(required = false) String startSettlementDate,
            @ApiParam(value = "结算日期-结束") @RequestParam(required = false) String endSettlementDate,
            @ApiParam(value = "入厂日期-开始") @RequestParam(required = false) String startInFactoryDate,
            @ApiParam(value = "入厂日期-结束") @RequestParam(required = false) String endInFactoryDate,
            @ApiParam(value = "入厂类型") @RequestParam(required = false) String inFactoryType,
            @ApiParam(value = "结算日期 1 为空 0 不为空") @RequestParam(required = false) Integer settlementStatus,
            @ApiParam(value = "寄厂日期 1 为空 0 不为空") @RequestParam(required = false) Integer mailFactoryStatus,
            @ApiParam(value = "订单日期-开始") @RequestParam(required = false) String startOrderDate,
            @ApiParam(value = "订单日期-结束") @RequestParam(required = false) String endOrderDate,
            @ApiParam(value = "资料状态 NORMAL=正常 AMEND=补充 WAIT_REVIEW=待复核 REVIEW=复核") @RequestParam(required = false) List<String> dataStates
    ) throws Exception {
        PoySaleCountBean bean = depositManageLogic.countList(customerKeyword, "enginOrderNo", enginOrderType,
                refEnginRecordNo, refProjectName, refTradeCategory, refUseUnit, startSubmitTime, endSubmitTime, startConfirmTime, endConfirmTime,
                createName, confirmName, isRefundDeposit, examineStatus, serviceId, materialNumber, materialOldNumber,
                materialName, specification, factoryNo, specialNo, geLiInerNote, refFactoryNo, refRegionWork,
                startInstallTime, endInstallTime, startAcceptTime, endAcceptTime, refBuyUnitName, refPromiseProvide,
                specialItemNo, startFactoryNo, endFactoryNo, startSpecialNo, endSpecialNo, startBillNo, endBillNo,
                startMailFactoryDate, endMailFactoryDate, mailFactoryYear, startConfirmDate, endConfirmDate,
                startSettlementNo, endSettlementNo, startSettlementDate, endSettlementDate, startInFactoryDate,
                endInFactoryDate, inFactoryType, settlementStatus, mailFactoryStatus, startOrderDate, endOrderDate, dataStates);
        return ResponseHelper.success(bean);
    }

    @ApiOperation(value = "押金管理详情")
    @GetMapping("/detail")
    public ResponseHelper<DepositManageBean> detail(
            @ApiParam(value = "押金管理id", required = true) @RequestParam String id
    ) throws Exception {
        DepositManageBean depositManageBean = depositManageLogic.detail(id);
        return ResponseHelper.success(depositManageBean);
    }

    @ApiOperation(value = "工程订单信息")
    @GetMapping("/query-engin-order-info")
    public ResponseHelper<List<EnginOrderInfo>> queryEnginOrderInfo(
            @ApiParam(value = "工程登录单号", required = true) @RequestParam String refEnginRecordNo
    ) throws Exception {
        List<EnginOrderInfo> enginOrderInfos = depositManageLogic.queryEnginOrderInfo(refEnginRecordNo);
        return ResponseHelper.success(enginOrderInfos);
    }

//    @ApiOperation(value = "置换单信息")
//    @GetMapping("/query-displace-order-info")
//    public ResponseHelper<List<DisplaceOrderInfo>> queryDisplaceOrderInfo(
//            @ApiParam(value = "工程登录单号", required = true) @RequestParam String refEnginRecordNo
//    ) throws Exception {
//        List<DisplaceOrderInfo> displaceOrderInfos = depositManageLogic.queryDisplaceOrderInfo(refEnginRecordNo);
//        return ResponseHelper.success(displaceOrderInfos);
//    }

    @GetMapping("/export")
    @ApiOperation(value = "押金管理导出")
    public void export(
//            @ApiParam(value = "押金管理id") @RequestParam List<String> id,
//            @ApiParam(value = "单据类型") @RequestParam String orderType,
            @ApiParam(value = "客户编码/客户名称") @RequestParam(required = false) String customerKeyword,
            @ApiParam(value = "工程订单类型") @RequestParam String enginOrderType,
            @ApiParam(value = "登录单号") @RequestParam(required = false) String refEnginRecordNo,
            @ApiParam(value = "项目名称") @RequestParam(required = false) String refProjectName,
            @ApiParam(value = "行业类别") @RequestParam(required = false) String refTradeCategory,
            @ApiParam(value = "使用单位") @RequestParam(required = false) String refUseUnit,
            @ApiParam(value = "明细厂编号") @RequestParam(required = false) String factoryNo,
            @ApiParam(value = "特价编号") @RequestParam(required = false) String specialNo,
            @ApiParam(value = "物料编码") @RequestParam(required = false) String materialNumber,
            @ApiParam(value = "产品编码") @RequestParam(required = false) String materialOldNumber,
            @ApiParam(value = "产品名称") @RequestParam(required = false) String materialName,
            @ApiParam(value = "规格型号") @RequestParam(required = false) String specification,
            @ApiParam(value = "申请日期-开始") @RequestParam(required = false) String startSubmitTime,
            @ApiParam(value = "申请日期-结束") @RequestParam(required = false) String endSubmitTime,
            @ApiParam(value = "验收日期-开始") @RequestParam(required = false) String startConfirmTime,
            @ApiParam(value = "验收日期-结束") @RequestParam(required = false) String endConfirmTime,
            @ApiParam(value = "安装日期-开始") @RequestParam(required = false) String startInstallTime,
            @ApiParam(value = "安装日期-结束") @RequestParam(required = false) String endInstallTime,
            @ApiParam(value = "资料审核日期-开始") @RequestParam(required = false) String startAcceptTime,
            @ApiParam(value = "资料审核日期-结束") @RequestParam(required = false) String endAcceptTime,
            @ApiParam(value = "创建人") @RequestParam(required = false) String createName,
            @ApiParam(value = "审核人") @RequestParam(required = false) String confirmName,
            @ApiParam(value = "是否退押 false=否 true=是") @RequestParam(required = false) Boolean isRefundDeposit,
            @ApiParam(value = "状态 SAVE=保存 WAIT=待审核 OK=通过") @RequestParam(required = false) List<String> examineStatus,
            @ApiParam(value = "业务员") @RequestParam(required = false) String serviceId,
            @ApiParam(value = "格力内部备注") @RequestParam(required = false) String geLiInerNote,
            @ApiParam(value = "跨区厂编号") @RequestParam(required = false) String refFactoryNo,
            @ApiParam(value = "区域") @RequestParam(required = false) String refRegionWork,
            @ApiParam(value = "购买单位名称") @RequestParam(required = false) String refBuyUnitName,
            @ApiParam(value = "保证函") @RequestParam(required = false) String refPromiseProvide,
            @ApiParam(value = "特价项目编号") @RequestParam(required = false) String specialItemNo,
            @ApiParam(value = "厂编号-开始") @RequestParam(required = false) String startFactoryNo,
            @ApiParam(value = "厂编号-结束") @RequestParam(required = false) String endFactoryNo,
            @ApiParam(value = "特价编号-开始") @RequestParam(required = false) String startSpecialNo,
            @ApiParam(value = "特价编号-结束") @RequestParam(required = false) String endSpecialNo,
	        @ApiParam(value = "交单编号-开始") @RequestParam(required = false) String startBillNo,
	        @ApiParam(value = "交单编号-结束") @RequestParam(required = false) String endBillNo,
            @ApiParam(value = "寄厂日期-开始") @RequestParam(required = false) String startMailFactoryDate,
            @ApiParam(value = "寄厂日期-结束") @RequestParam(required = false) String endMailFactoryDate,
            @ApiParam(value = "寄厂年度") @RequestParam(required = false) String mailFactoryYear,
            @ApiParam(value = "确认日期-开始") @RequestParam(required = false) String startConfirmDate,
            @ApiParam(value = "确认日期-结束") @RequestParam(required = false) String endConfirmDate,
            @ApiParam(value = "结算编号-开始") @RequestParam(required = false) String startSettlementNo,
            @ApiParam(value = "结算编号-结束") @RequestParam(required = false) String endSettlementNo,
            @ApiParam(value = "结算日期-开始") @RequestParam(required = false) String startSettlementDate,
            @ApiParam(value = "结算日期-结束") @RequestParam(required = false) String endSettlementDate,
            @ApiParam(value = "入厂日期-开始") @RequestParam(required = false) String startInFactoryDate,
            @ApiParam(value = "入厂日期-结束") @RequestParam(required = false) String endInFactoryDate,
            @ApiParam(value = "结算日期 1 为空 0 不为空") @RequestParam(required = false) Integer settlementStatus,
            @ApiParam(value = "寄厂日期 1 为空 0 不为空") @RequestParam(required = false) Integer mailFactoryStatus,
            @ApiParam(value = "入厂类型") @RequestParam(required = false) String inFactoryType,
            @ApiParam(value = "订单日期-开始") @RequestParam(required = false) String startOrderDate,
            @ApiParam(value = "订单日期-结束") @RequestParam(required = false) String endOrderDate,
            @ApiParam(value = "资料状态 NORMAL=正常 AMEND=补充 WAIT_REVIEW=待复核 REVIEW=复核") @RequestParam(required = false) List<String> dataStates,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        IPage<DepositManageBean> listPage = depositManageLogic.listPage(settlementStatus,mailFactoryStatus,customerKeyword,
                "enginOrderNo", enginOrderType, refEnginRecordNo, refProjectName, refTradeCategory, refUseUnit,
                startSubmitTime, endSubmitTime, startConfirmTime, endConfirmTime, createName, confirmName, isRefundDeposit,
                examineStatus, serviceId, materialNumber, materialOldNumber, materialName, specification, factoryNo,
                specialNo, geLiInerNote, refFactoryNo, refRegionWork, startInstallTime, endInstallTime, startAcceptTime,
                endAcceptTime, refBuyUnitName, refPromiseProvide, specialItemNo, startFactoryNo, endFactoryNo, startSpecialNo,
                endSpecialNo, startBillNo, endBillNo, startMailFactoryDate, endMailFactoryDate, mailFactoryYear, startConfirmDate,
                endConfirmDate, startSettlementNo, endSettlementNo, startSettlementDate, endSettlementDate, startInFactoryDate,
                endInFactoryDate, inFactoryType, startOrderDate, endOrderDate, dataStates, 1, -1);
        ExcelData excelData;
        if (OrderTypeEnum.HOME.getKey().equals(enginOrderType)) {
            excelData = depositManageLogic.exportHome(listPage.getRecords());
        } else {
            excelData = depositManageLogic.exportTrade(listPage.getRecords());
        }

        ExcelUtils.exportExcel(request,response,"工程押金列表.xlsx",excelData);
    }

    @PostMapping("/import")
    @ApiOperation("押金管理导入")
    public ResponseHelper<String> importToll(
            @ApiParam(required = true, value = "附件") @RequestPart("file") MultipartFile file,
            HttpServletRequest request
    ) throws RemoteServiceException, IOException, ParseException {
        InputStream inputStream = file.getInputStream();
        depositManageLogic.importDepositInfo(inputStream,request);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "申请退押")
    @PostMapping("/apply-refund-deposit")
    public ResponseHelper homeSubmit(
            @ApiParam(required = true, value = "押金管理") @RequestBody DepositManageBean depositManageBean
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.DEPOSIT_MANAGE + depositManageBean.getRefEnginRecordNo());
        try {
            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
                DepositManage depositManage = depositManageLogic.applyRefundDeposit(depositManageBean);
                // 同步工程机数据
                depositManageLogic.syncEnginData(depositManage);
            }
        } catch(Exception e) {
            log.error("【工程押金处理】失败",e);
            throw e;
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @ApiOperation(value = "编辑退押")
    @PostMapping("/edit")
    public ResponseHelper edit(
            @ApiParam(required = true, value = "押金管理") @RequestBody DepositManageBean depositManageBean
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.DEPOSIT_MANAGE + depositManageBean.getRefEnginRecordNo());
        try {
            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
                depositManageLogic.edit(depositManageBean);
            }
        } catch(Exception e) {
            log.error("【工程押金处理】失败",e);
            throw e;
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @ApiOperation(value = "审批退押")
    @PostMapping("/examine")
    public ResponseHelper<DepositManageBean> examine(
            @ApiParam(required = true, value = "押金管理") @RequestBody DepositManageBean depositManageBean
    ) throws Exception {
        if (commonLogic.getAdminUser(true).getIsCustomer()) {
            throw new RemoteServiceException("暂无权限操作");
        }
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.DEPOSIT_MANAGE + depositManageBean.getRefEnginRecordNo());
        try {
            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
                DepositManage depositManage = depositManageLogic.examine(depositManageBean);
                // 回传工程机系统数据
                depositManageLogic.depositConfirmUpdateAgencyBills(depositManage, depositManageBean);
                return ResponseHelper.success(depositManageLogic.nextWaitOrder(depositManageBean));
            }
        } catch(Exception e) {
            log.error("【工程审批退押处理】失败",e);
            throw e;
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @ApiOperation(value = "二次退押")
    @PostMapping("/second-refund")
    public ResponseHelper<DepositManageBean> secondRefund(
            @ApiParam(required = true, value = "押金管理") @RequestBody DepositManageBean depositManageBean
    ) throws Exception {
        if (commonLogic.getAdminUser(true).getIsCustomer()) {
            throw new RemoteServiceException("暂无权限操作");
        }
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.DEPOSIT_MANAGE + depositManageBean.getRefEnginRecordNo());
        try {
            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
                depositManageLogic.secondRefund(depositManageBean);
                // 回传工程机系统数据
                return ResponseHelper.success("二次退押成功");
            }
        } catch(Exception e) {
            log.error("【工程二次退押处理】失败",e);
            throw e;
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }


    /**
     * 压缩远程服务器文件
     */
    @GetMapping("/downZip")
    @ApiOperation("下载押金管理附件")
    public void downloadZip(@ApiParam(value = "押金管理id", required = true) @RequestParam String parentId,
                            @ApiParam(value = "数据位置") @RequestParam(required = false) String dataPosition,
                            HttpServletResponse response)  throws IOException{
        depositManageLogic.downZipFile(parentId, dataPosition, response);
    }


//    @GetMapping("/sync-accept-date")
//    @ApiOperation("同步工程机系统审核资料日期")
//    public ResponseHelper syncAcceptDate() throws Exception {
//        depositManageLogic.syncEnginData(depositManage.getDepositManageId());
//        return ResponseHelper.success();
//    }

    @PostMapping("/bach-condition-examine-home")
    @ApiOperation("批量审核家用")
    public ResponseHelper bachExamineHome(@RequestParam List<String> ids) throws Exception {
        if (commonLogic.getAdminUser(true).getIsCustomer()) {
            throw new RemoteServiceException("暂无权限操作");
        }
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.DEPOSIT_MANAGE + "1");
        try {
            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
                return ResponseHelper.success(depositManageLogic.bachConditionExamineHome(ids));
            }
        } catch(Exception e) {
            log.error("【工程信息单处理】失败",e);
            throw e;
        } finally {
            obtain.unlock();
        }
        throw new RemoteServiceException("批量审核异常！");
    }

    @PostMapping("/online-compute-diff")
    @ApiOperation("在线计算收差金额")
    public ResponseHelper onlineComputeDiff(
            @ApiParam(value = "押金管理明细", required = true) @RequestBody DepositMangeItemBean depositManageItem
    ) throws Exception {
        return ResponseHelper.success(depositManageLogic.onlineComputeDiff(depositManageItem));
    }

    @ApiOperation(value = "补充资料")
    @PostMapping("/amend-data")
    public ResponseHelper amendData(
            @ApiParam(required = true, value = "押金管理") @RequestBody DepositManageBean depositManageBean
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.DEPOSIT_MANAGE + depositManageBean.getRefEnginRecordNo());
        try {
            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
                DepositManage depositManage = depositManageLogic.amendData(depositManageBean);
            }
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @ApiOperation(value = "复核资料")
    @PostMapping("/review-data")
    public ResponseHelper<DepositManageBean> reviewData(
            @ApiParam(required = true, value = "押金管理") @RequestBody DepositManageBean depositManageBean
    ) throws Exception {
        if (commonLogic.getAdminUser(true).getIsCustomer()) {
            throw new RemoteServiceException("暂无权限操作");
        }
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.DEPOSIT_MANAGE + depositManageBean.getRefEnginRecordNo());
        try {
            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
                depositManageLogic.reviewData(depositManageBean);
                return ResponseHelper.success();
            }
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @ApiOperation(value = "更新信息")
    @PostMapping("/update-engin-info")
    public ResponseHelper updateEnginInfo() throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.DEPOSIT_MANAGE);
        try {
            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
                // 同步工程机数据
                depositManageLogic.updateEnginInfo();
            }
        } catch(Exception e) {
            log.error("【工程押金处理】失败",e);
            throw e;
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

}
