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

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.CommonCountBean;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.ReceivableOrderBean;
import com.zfire.mall.manager.bean.listV2.CustomerWalletRecordVO;
import com.zfire.mall.manager.bean.listV2.CustomerWalletTransferVO;
import com.zfire.mall.manager.bean.listV2.ReceivableItemVO;
import com.zfire.mall.manager.bean.listV2.dto.ExamineZfireParamBean;
import com.zfire.mall.manager.bean.rece.ReceivableItemBean;
import com.zfire.mall.manager.bean.wallet.AmountCountBean;
import com.zfire.mall.manager.bean.wallet.CustomerTotalAmountExportBean;
import com.zfire.mall.manager.bean.wallet.CustomerWalletBean;
import com.zfire.mall.manager.commonmapper.wallet.WalletCommonMapper;
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.k3.bill.K3OtherReceLogic;
import com.zfire.mall.manager.logic.wallet.RebateOrderLogic;
import com.zfire.mall.manager.logic.wallet.WalletLogic;
import com.zfire.mall.manager.plus.entity.CustomerWalletReconciliation;
import com.zfire.mall.manager.plus.entity.CustomerWalletRecord;
import com.zfire.mall.manager.plus.entity.CustomerWalletTransfer;
import com.zfire.mall.manager.plus.entity.KingDeeWallet;
import com.zfire.mall.manager.utils.CommonUtils;
import com.zfire.mall.manager.utils.excel.ExcelUtils;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import com.zfire.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

@Slf4j
@RestController
@Api(value = "财务会计管理API", tags ={"财务会计管理API"} )
@RequestMapping(value = "/finance", produces = "application/json; charset=utf-8")
public class FinanceController {

    @Autowired
    WalletLogic walletLogic;
    @Autowired
    WalletCommonMapper walletCommonMapper;
    @Autowired
    K3OtherReceLogic k3OtherReceLogic;
    @Autowired
    RebateOrderLogic rebateOrderLogic;
    @Autowired
    RedisLockRegistry redisLockRegistry;



    @PostMapping("/bindMain")
    @ApiOperation("绑定品类")
    public ResponseHelper bindMain(
            @ApiParam(value = "钱包id",required = true) @RequestParam(required = true) String id,
            @ApiParam(value = "是否为折让钱包 true=是 false=否",required = true) @RequestParam(required = true) Boolean isZr,
            @ApiParam(value = "是否可使用返利 true=是 false=否",required = true) @RequestParam(required = true) Boolean isRebate
    ){
        walletLogic.bindMain(id,isZr,isRebate);
        return ResponseHelper.success();
    }

    @PostMapping("/init")
    @ApiOperation(value = "经销商一键生成钱包")
    public ResponseHelper init(
            @ApiParam(value = "经销商id",required = true) @RequestParam(required = true) String customerId
    ) throws Exception {
        walletLogic.initByK3Customer(customerId);
        return ResponseHelper.success();
    }

    @GetMapping("/customer/list")
    @ApiOperation(value = "经销商钱包列表")
    public ResponseHelper<List<CustomerWalletBean>> customerWalletList(
            @ApiParam(value = "经销商id",required = true) @RequestParam(required = true) String customerId,
            @ApiParam(value = "钱包类型：COMMONLY=普通钱包 REBATE=返利钱包",required = true) @RequestParam(required = true) String type,
            @ApiParam(value = "true=有记录钱包",required = false) @RequestParam(required = false) Boolean hasRecord
    ) throws Exception {
        List<CustomerWalletBean> customerWalletBeans = walletLogic.queryWalletByCustomerId(customerId, type,hasRecord);
        return ResponseHelper.success(customerWalletBeans);
    }

    @GetMapping("/list")
    @ApiOperation(value = "钱包列表(非返利钱包)")
    public ResponseHelper<List<KingDeeWallet>> list(
            @ApiParam(value = "钱包名称",required = false) @RequestParam(required = false) String walletName
    ) throws Exception {
        List<KingDeeWallet> list = walletLogic.list(walletName);
        return ResponseHelper.success(list);
    }


    @ZfireList
    @PostMapping("/standing/book/list/v2")
    @ApiOperation("对账列表V2")
    public ResponseHelper<IPage<CustomerWalletRecordVO>> customerListV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<CustomerWalletRecordVO> page = walletLogic.standingBookListV2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<CustomerWalletRecordVO>() {});
    }

    @PostMapping("/standing/book/v2/export")
    @ApiOperation("对账列表V2-导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<CustomerWalletRecordVO> page = walletLogic.standingBookListV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(),zfireParam.getExportFields(),request,response);
    }



    @GetMapping("/standing/book/list")
    @ApiOperation(value = "台账列表")
    public ResponseHelper<IPage<CustomerWalletRecord>> standingBookList(
            @ApiParam(value = "经销商id",required = false) @RequestParam(required = false) String customerId,
            @ApiParam(value = "经销商编号",required = false) @RequestParam(required = false) String customerNumber,
            @ApiParam(value = "经销商名称",required = false) @RequestParam(required = false) String customerName,
            @ApiParam(value = "钱包类型（COMMONLY=普通钱包 REBATE=返利钱包）",required = false) @RequestParam(required = false) String type,
            @ApiParam(value = "经销商钱包id",required = false) @RequestParam(required = false) String customerWalletId,
            @ApiParam(value = "钱包id",required = false) @RequestParam(required = false) String walletRebateId,
            @ApiParam(value = "单据号",required = false) @RequestParam(required = false) String billNo,
            @ApiParam(value = "工程登录编号",required = false) @RequestParam(required = false) String enginRecordNo,
            @ApiParam(value = "单据类型",required = false) @RequestParam(required = false) String billType,
            @ApiParam(value = "开始时间",required = false) @RequestParam(required = false) String startTime,
            @ApiParam(value = "结束时间",required = false) @RequestParam(required = false) String endTime,
            @ApiParam(value = "是否对账 true=是,fasle=否",required = false) @RequestParam(required = false) Boolean isReconciliation,
            @ApiParam(value = "摘要说明",required = false) @RequestParam(required = false) String remark,
            @ApiParam(value = "页号",required = false) @RequestParam(required = false) Integer pageNum,
            @ApiParam(value = "页大小",required = false) @RequestParam(required = false) Integer pageSize
    ) throws Exception {
        IPage<CustomerWalletRecord> page = walletLogic.standingBookList(customerId,customerNumber,customerName, type,
                customerWalletId,walletRebateId,
                billNo,enginRecordNo, startTime, endTime,isReconciliation,billType,remark, pageNum, pageSize);
        return ResponseHelper.success(page);
    }


    @GetMapping("/standing/book/amountCount")
    @ApiOperation(value = "台账列表统计")
    public ResponseHelper<AmountCountBean> amountCount(
            @ApiParam(value = "经销商id",required = false) @RequestParam(required = false) String customerId,
            @ApiParam(value = "经销商编号",required = false) @RequestParam(required = false) String customerNumber,
            @ApiParam(value = "经销商名称",required = false) @RequestParam(required = false) String customerName,
            @ApiParam(value = "钱包类型（COMMONLY=普通钱包 REBATE=返利钱包）",required = false) @RequestParam(required = false) String type,
            @ApiParam(value = "经销商钱包id",required = false) @RequestParam(required = false) String customerWalletId,
            @ApiParam(value = "钱包id",required = false) @RequestParam(required = false) String walletRebateId,
            @ApiParam(value = "单据号",required = false) @RequestParam(required = false) String billNo,
            @ApiParam(value = "单据类型",required = false) @RequestParam(required = false) String billType,
            @ApiParam(value = "开始时间",required = false) @RequestParam(required = false) String startTime,
            @ApiParam(value = "结束时间",required = false) @RequestParam(required = false) String endTime,
            @ApiParam(value = "是否对账 true=是,fasle=否",required = false) @RequestParam(required = false) Boolean isReconciliation
    )  {
        AmountCountBean bean = walletLogic.amountCount(customerId,customerNumber,customerName, type,
                customerWalletId,walletRebateId, billNo, startTime, endTime,isReconciliation,billType);
        return ResponseHelper.success(bean);
    }


    @ZfireList
    @PostMapping("/transfer/list/v2")
    @ApiOperation(value = "返利互转列表-V2")
    public ResponseHelper<IPage<CustomerWalletTransferVO>> transerListV2(
            @RequestBody ExamineZfireParamBean zfireParamBean
    ) throws Exception {
        IPage<CustomerWalletTransferVO> page = walletLogic.transferListV2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<CustomerWalletTransferVO>() {});
    }

    @PostMapping("/transfer/list/listExport/v2")
    @ApiOperation("返利互转列表V2-导出")
    public void transerListV2Export(
            @RequestBody ExamineZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<CustomerWalletTransferVO> page = walletLogic.transferListV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(),zfireParam.getExportFields(),request,response);
    }



    @GetMapping("/transfer/list")
    @ApiOperation(value = "返利互转-列表")
    public ResponseHelper<IPage<CustomerWalletTransfer>> transerList(
            @ApiParam(value = "单号",required = false) @RequestParam(required = false) String id,
            @ApiParam(value = "SAVE=保存 WAIT=待审核 OK_ONE=初审通过  FAIL_ONE=初审不通过 OK=复核通过 FAIL=不通过 CLOSE=已关闭 REJECT=已驳回",required = false) @RequestParam(required = false) String examineStatus,
            @ApiParam(value = "返利类型(返利钱包名)",required = false) @RequestParam(required = false) String walletName,
            @ApiParam(value = "开始时间",required = false) @RequestParam(required = false) String startTime,
            @ApiParam(value = "结束时间",required = false) @RequestParam(required = false) String endTime,
            @ApiParam(value = "经销商名称",required = false) @RequestParam(required = false) String customerName,
            @ApiParam(value = "页号",required = false) @RequestParam(required = false) Integer pageNum,
            @ApiParam(value = "页大小",required = false) @RequestParam(required = false) Integer pageSize
    ) throws Exception {
        IPage<CustomerWalletTransfer> customerWalletTransferIPage = walletLogic.transferList(id, examineStatus,walletName, startTime, endTime, customerName,pageNum, pageSize);
        return ResponseHelper.success(customerWalletTransferIPage);
    }

    @GetMapping("/transfer/listCount")
    @ApiOperation(value = "返利互转-统计")
    public ResponseHelper<CommonCountBean> transerListCount(
            @ApiParam(value = "单号",required = false) @RequestParam(required = false) String id,
            @ApiParam(value = "SAVE=保存 WAIT=待审核 OK_ONE=初审通过  FAIL_ONE=初审不通过 OK=复核通过 FAIL=不通过 CLOSE=已关闭",required = false) @RequestParam(required = false) String examineStatus,
            @ApiParam(value = "返利类型(返利钱包名)",required = false) @RequestParam(required = false) String walletName,
            @ApiParam(value = "经销商名称",required = false) @RequestParam(required = false) String customerName,
            @ApiParam(value = "开始时间",required = false) @RequestParam(required = false) String startTime,
            @ApiParam(value = "结束时间",required = false) @RequestParam(required = false) String endTime
    ) throws Exception {
        CommonCountBean bean = walletLogic.transferListCount(id, examineStatus,walletName, startTime, endTime,customerName);
        return ResponseHelper.success(bean);
    }


    @GetMapping("/transfer/detail")
    @ApiOperation(value = "返利互转-详情")
    public ResponseHelper<IPage<CustomerWalletTransfer>> transerList(
            @ApiParam(value = "单号",required = false) @RequestParam(required = false) String id
    ) throws Exception {
        CustomerWalletTransfer customerWalletTransfer = walletLogic.transferDetail(id);
        return ResponseHelper.success(customerWalletTransfer);
    }


    @PostMapping("/transfer/add")
    @ApiOperation(value = "返利互转-新增")
    public ResponseHelper add(
            @ApiParam(value = "发起方钱包id",required = true) @RequestParam(required = true) String initiatorWalletRebateId,
            @ApiParam(value = "发起方备注",required = false) @RequestParam(required = false) String initiatorRemark,
            @ApiParam(value = "接收方钱包id",required = true) @RequestParam(required = true) String receiverWalletRebateId,
            @ApiParam(value = "接收方备注",required = false) @RequestParam(required = false) String receiverRemark,
            @ApiParam(value = "接收方返利金额",required = true) @RequestParam(required = true) BigDecimal amount,
            @ApiParam(value = "接收方折让金额",required = false) @RequestParam(required = false) BigDecimal allowanceAmount,
            @ApiParam(value = "发起方返利金额",required = true) @RequestParam(required = true) BigDecimal initiatorAmount,
            @ApiParam(value = "单据日期",required = false) @RequestParam(required = false) String theTime,
            @ApiParam(value = "表头备注",required = false) @RequestParam(required = false) String remark
    ) throws Exception {
        walletLogic.add(initiatorWalletRebateId,receiverWalletRebateId,amount,initiatorRemark,receiverRemark,allowanceAmount,initiatorAmount,remark);
        return ResponseHelper.success();
    }


    @PostMapping("/transfer/update")
    @ApiOperation(value = "返利互转-修改")
    public ResponseHelper update(
            @ApiParam(value = "id",required = true) @RequestParam(required = true) String id,
            @ApiParam(value = "发起方钱包id",required = true) @RequestParam(required = true) String initiatorWalletRebateId,
            @ApiParam(value = "发起方备注",required = false) @RequestParam(required = false) String initiatorRemark,
            @ApiParam(value = "接收方钱包id",required = true) @RequestParam(required = true) String receiverWalletRebateId,
            @ApiParam(value = "接收方备注",required = false) @RequestParam(required = false) String receiverRemark,
            @ApiParam(value = "接收方返利金额",required = true) @RequestParam(required = true) BigDecimal amount,
            @ApiParam(value = "接收方折让金额",required = false) @RequestParam(required = false) BigDecimal allowanceAmount,
            @ApiParam(value = "发起方返利金额",required = true) @RequestParam(required = true) BigDecimal initiatorAmount,
            @ApiParam(value = "单据日期",required = false) @RequestParam(required = false) String theTime,
            @ApiParam(value = "表头备注",required = false) @RequestParam(required = false) String remark
    ) throws Exception {
        walletLogic.update(id,initiatorWalletRebateId,receiverWalletRebateId,amount,initiatorRemark,receiverRemark,theTime,allowanceAmount,initiatorAmount,remark);
        return ResponseHelper.success();
    }


    @PostMapping("/transfer/apply")
    @ApiOperation(value = "返利互转-提审")
    public ResponseHelper apply(
            @ApiParam(value = "id",required = true) @RequestParam(required = true) String id
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_REBATE + id);
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("系统繁忙，请稍后再试");
        }
        try {
            walletLogic.walletTransferApplyExamine(id);
        }finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/transfer/cancel")
    @ApiOperation(value = "返利互转-取消")
    public ResponseHelper transferCancel(
            @ApiParam(value = "id",required = true) @RequestParam(required = true) String id
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_REBATE + id);
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("系统繁忙，请稍后再试");
        }
        try {
            walletLogic.transferCancel(id);
        }finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/transfer/examine")
    @ApiOperation(value = "返利互转-审核")
    public ResponseHelper apply(
            @ApiParam(value = "id",required = true) @RequestParam(required = true) String id,
            @ApiParam(value = "审核状态 OK=通过 FAIL=不通过",required = true) @RequestParam(required = true) String examineStatus,
            @ApiParam(value = "审核备注",required = true) @RequestParam(required = true) String remark
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_REBATE + id);
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("系统繁忙，请稍后再试");
        }
        try {
            walletLogic.walletTransferExamine(id, examineStatus, remark);
        }finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }


    @GetMapping("/total")
    @ApiOperation(value = "财务汇总-字段简单，直接看返回结果接")
    public ResponseHelper<List<Map<String,Object>>> countTotal(
            @ApiParam(value = "websitId",required = false) @RequestParam(required = false) String websitId,
            @ApiParam(value = "钱包类型 COMMONLY=现金钱包 REBATE=返利钱包",required = false) @RequestParam(required = false) String type
    ) throws Exception {
        List<Map<String, Object>> maps = walletLogic.queryTotalAmount(websitId,type);
        return ResponseHelper.success(maps);
    }


    @GetMapping("/totalCustomer")
    @ApiOperation(value = "财务汇总-字段简单，直接看返回结果接")
    public ResponseHelper<List<Map<String,Object>>> countTotal(
            @ApiParam(value = "经销商编号",required = false) @RequestParam(required = false) String customerNumber,
            @ApiParam(value = "经销商名称",required = false) @RequestParam(required = false) String customerName,
            @ApiParam(value = "区域",required = false) @RequestParam(required = false) String area,
            @ApiParam(value = "钱包id",required = false) @RequestParam(required = false) String walletId
    ) throws Exception {
        List<Map<String, Object>> maps = walletLogic.queryCustomerTotalAmount(customerNumber,customerName,area,walletId);
        return ResponseHelper.success(maps);
    }


    @ZfireList
    @PostMapping("/other/rece/list/v2")
    @ApiOperation(value = "其他应收单列表-V2")
    public ResponseHelper<IPage<ReceivableItemVO>> listV2(
            @RequestBody ExamineZfireParamBean zfireParamBean
    ) throws Exception {
        IPage<ReceivableItemVO> page = walletLogic.otherReceListV2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<ReceivableItemVO>() {});
    }

    @PostMapping("/other/rece/listExport/v2")
    @ApiOperation("其他应收单列表V2-导出")
    public void listExport(
            @RequestBody ExamineZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<ReceivableItemVO> list = walletLogic.otherReceListV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(),zfireParam.getExportFields(),request,response);
    }

    @GetMapping("/other/rece/list")
    @ApiOperation("其他应收单-列表")
    public ResponseHelper<IPage<ReceivableItemBean>> list(
            @ApiParam(value = "来源",required = false) @RequestParam(required = false) String source,
            @ApiParam(value = "单据编号",required = false) @RequestParam(required = false) String code,
            @ApiParam(value = "往来单位",required = false) @RequestParam(required = false) String userName,
            @ApiParam(value = "审核人",required = false) @RequestParam(required = false) String examineBy,
            @ApiParam(value = "SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=已关闭",required = false) @RequestParam(required = false) String examineStatus,
            @ApiParam(value = "业务日期开始时间",required = false) @RequestParam(required = false) String startTime,
            @ApiParam(value = "业务日期结束时间",required = false) @RequestParam(required = false) String endTime,
            @ApiParam(value = "pageNum",required = true) @RequestParam(required = true) Integer pageNum,
            @ApiParam(value = "pageSize",required = true) @RequestParam(required = true) Integer pageSize
    ) throws Exception {
        IPage<ReceivableItemBean> page = walletLogic.otherReceList(source, code, userName,examineBy, examineStatus, startTime, endTime, pageNum, pageSize);
        return ResponseHelper.success(page);
    }

    @GetMapping("/other/rece/close")
    @ApiOperation("其他应收单-关闭")
    public ResponseHelper close(
            @ApiParam(value = "id",required = true) @RequestParam(required = true) String id
    ) throws InterruptedException {
        walletLogic.close(id);
        return ResponseHelper.success();
    }

    @GetMapping("/other/rece/detail")
    @ApiOperation("其他应收单-详情")
    public ResponseHelper<ReceivableOrderBean> detail(
            @ApiParam(value = "id",required = true) @RequestParam(required = true) String id
    ){
        ReceivableOrderBean receivableOrderBean = walletLogic.otherReceDetail(id);
        return ResponseHelper.success(receivableOrderBean);
    }

    @PostMapping("/other/rece/add")
    @ApiOperation("其他应收单-新增")
    public ResponseHelper add(
            @ApiParam(value = "receivableOrderBean",required = true) @RequestBody(required = true) ReceivableOrderBean receivableOrderBean
    ){
        return walletLogic.addOtherRece(receivableOrderBean);
    }

    @GetMapping("/other/rec/download")
    @ApiOperation("下载其他应收单模板")
    public void download(
            HttpServletResponse response
    ) throws IOException {
        CommonUtils.downloadFile("其他应收单.xlsx",response);
    }

    @PostMapping("/other/rece/update")
    @ApiOperation("其他应收单-修改")
    public ResponseHelper update(
            @ApiParam(value = "receivableOrderBean",required = true) @RequestBody(required = true) ReceivableOrderBean receivableOrderBean
    ){
        walletLogic.updateOtherRece(receivableOrderBean);
        return ResponseHelper.success();
    }


    @PostMapping("/other/rece/apply")
    @ApiOperation("其他应收单-提审")
    public ResponseHelper applyOtherRece(
            @ApiParam(value = "id",required = true) @RequestParam(required = true) List<String> id
    ) throws InterruptedException {
        for (String s : id) {
            Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_OTHER + s);
            if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            try {
                walletLogic.applyExamineOtherRece(s);
            }finally {
                obtain.unlock();
            }
        }

        return ResponseHelper.success();
    }

    @PostMapping("/other/rece/cancelApply")
    @ApiOperation("其他应收单-取消提审")
    public ResponseHelper cancelApplyOtherRece(
            @ApiParam(value = "id",required = true) @RequestParam(required = true) String id
    ) throws InterruptedException {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_OTHER + id);
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("系统繁忙，请稍后再试");
        }
        try {
            walletLogic.cancelApplyOtherRece(id);
        }finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/other/rece/examine")
    @ApiOperation("其他应收单-审核")
    public ResponseHelper examineOtherRece(
            @ApiParam(value = "id",required = true) @RequestParam(required = true) List<String> id,
            @ApiParam(value = "OK=通过 FAIL=不通过",required = true) @RequestParam(required = true) String examineStatus,
            @ApiParam(value = "审核备注",required = false) @RequestParam(required = false) String examineRemark
    ) throws InterruptedException {
        for (String s : id) {
            Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_OTHER + s);
            if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            try {
                walletLogic.examineOtherRece(s, examineStatus, examineRemark);
            }finally {
                obtain.unlock();
            }
        }

        return ResponseHelper.success();
    }


    @PostMapping("/other/rece/abandon")
    @ApiOperation("其他应收单-弃审")
    public ResponseHelper abandon(
            @ApiParam(value = "id",required = true) @RequestParam(required = true) String id
    ) throws InterruptedException {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_OTHER + id);
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("系统繁忙，请稍后再试");
        }
        try {
            walletLogic.abandon(id, false);
        }finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }


    @PostMapping("/other/rece/delete")
    @ApiOperation("其他应收单-删除")
    public ResponseHelper examineOtherRece(
            @ApiParam(value = "ids",required = true) @RequestParam(required = true) List<String> ids) {
        walletLogic.deleteOther(ids);
        return ResponseHelper.success();
    }


    @GetMapping("/other/rece/listExport")
    @ApiOperation("其他应收单-导出")
    public void listExport(
            @ApiParam(value = "来源",required = false) @RequestParam(required = false) String source,
            @ApiParam(value = "单据编号",required = false) @RequestParam(required = false) String code,
            @ApiParam(value = "往来单位",required = false) @RequestParam(required = false) String userName,
            @ApiParam(value = "审核人",required = false) @RequestParam(required = false) String examineBy,
            @ApiParam(value = "SAVE=保存 WAIT=待审核 OK=通过 FAIL=不通过 CLOSE=已关闭",required = false) @RequestParam(required = false) String examineStatus,
            @ApiParam(value = "业务日期开始时间",required = false) @RequestParam(required = false) String startTime,
            @ApiParam(value = "业务日期结束时间",required = false) @RequestParam(required = false) String endTime,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        IPage<ReceivableItemBean> page = walletLogic.otherReceList(source, code, userName,examineBy, examineStatus, startTime, endTime, 1, -1);
        ExcelData excelData = walletLogic.otherReceListExport(page.getRecords());
        ExcelUtils.exportExcel(request,response,"其他应收单.xlsx",excelData);
    }


    @PostMapping("/rece/import/data")
    @ApiOperation("导入其他应收单-返回数据")
    public ResponseHelper<ReceivableOrderBean> importRebateOrderData(MultipartFile file) throws Exception {
        List<Object> objects = ExcelUtils.importExcel(file);
        ReceivableOrderBean  receivableOrderBean = rebateOrderLogic.importReceivableOrderData(objects);
        return ResponseHelper.success(receivableOrderBean);
    }


    @PostMapping("/rece/import/dataImport")
    @ApiOperation("导入其他应收单-插入")
    public ResponseHelper dataImportData(MultipartFile file) throws Exception {
        List<Object> objects = ExcelUtils.importExcel(file);
        rebateOrderLogic.dataImportData(objects);
        return ResponseHelper.success();
    }


    @GetMapping("/totalCustomer/export")
    @ApiOperation(value = "财务汇总-字段简单，直接看返回结果接")
    public void countTotalExport(
            @ApiParam(value = "经销商编号",required = false) @RequestParam(required = false) String customerNumber,
            @ApiParam(value = "经销商名称",required = false) @RequestParam(required = false) String customerName,
            @ApiParam(value = "区域",required = false) @RequestParam(required = false) String area,
            @ApiParam(value = "钱包ID",required = false) @RequestParam(required = false) String walletId,
            @ApiParam(value = "是否根据经销商汇总导出(true/false)",required = false) @RequestParam(required = false) Boolean byCustomer,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        List<CustomerTotalAmountExportBean> beans = walletLogic.queryCustomerTotalAmountExport(customerNumber, customerName, area, walletId,byCustomer);
        ExcelData excelData = walletLogic.countTotalExport(beans);
        ExcelUtils.exportExcel(request,response,"经销商余额汇总.xlsx",excelData);
    }

    @PostMapping("/standing/book/check")
    @ApiOperation(value = "一键对账")
    public ResponseHelper standingBookCheck(@RequestParam List<String> ids)  {
        walletLogic.standingBookLCheck(ids);
        return ResponseHelper.success();
    }


    @GetMapping("/standing/book/export")
    @ApiOperation(value = "台账列表-导出")
    public void standingBookExport(
            @ApiParam(value = "经销商id",required = false) @RequestParam(required = false) String customerId,
            @ApiParam(value = "经销商编号",required = false) @RequestParam(required = false) String customerNumber,
            @ApiParam(value = "经销商名称",required = false) @RequestParam(required = false) String customerName,
            @ApiParam(value = "钱包类型（COMMONLY=普通钱包 REBATE=返利钱包）",required = false) @RequestParam(required = false) String type,
            @ApiParam(value = "经销商钱包id",required = false) @RequestParam(required = false) String customerWalletId,
            @ApiParam(value = "钱包id",required = false) @RequestParam(required = false) String walletRebateId,
            @ApiParam(value = "单据号",required = false) @RequestParam(required = false) String billNo,
            @ApiParam(value = "工程登录编号",required = false) @RequestParam(required = false) String enginRecordNo,
            @ApiParam(value = "单据类型",required = false) @RequestParam(required = false) String billType,
            @ApiParam(value = "开始时间",required = false) @RequestParam(required = false) String startTime,
            @ApiParam(value = "结束时间",required = false) @RequestParam(required = false) String endTime,
            @ApiParam(value = "是否对账 true=是,fasle=否",required = false) @RequestParam(required = false) Boolean isReconciliation,
            @ApiParam(value = "摘要说明",required = false) @RequestParam(required = false) String remark,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        log.info("standingBookExport type:" + type);
        IPage<CustomerWalletRecord> page = walletLogic.standingBookList(customerId,customerNumber,customerName, type, customerWalletId,walletRebateId,
                billNo, enginRecordNo, startTime, endTime, isReconciliation,billType,remark,1,-1);
        ExcelData excelData = walletLogic.standingBookExport(page.getRecords(),type);
        String fileName = "货款台账";
        if(("REBATE").equals(type)){
            fileName = "返利台账";
        }
        ExcelUtils.exportExcel(request,response,fileName + ".xlsx",excelData);
    }


    @GetMapping("/reconciliate/list")
    @ApiOperation(value = "月份对账列表")
    public ResponseHelper<List<CustomerWalletReconciliation>> reconciliationList(
            @ApiParam(value = "经销商id",required = false) @RequestParam(required = false) String customerId,
            @ApiParam(value = "经销商编号",required = false) @RequestParam(required = false) String customerNumber,
            @ApiParam(value = "经销商名称",required = false) @RequestParam(required = false) String customerName,
            @ApiParam(value = "钱包类型（COMMONLY=普通钱包 REBATE=返利钱包）",required = false) @RequestParam(required = false) String type,
            @ApiParam(value = "经销商钱包id",required = false) @RequestParam(required = false) String customerWalletId,
            @ApiParam(value = "钱包id",required = false) @RequestParam(required = false) String walletRebateId,
            @ApiParam(value = "是否对账 true=是,fasle=否",required = false) @RequestParam(required = false) Boolean isReconciliation,
            @ApiParam(value = "页号",required = false) @RequestParam(required = false) Integer pageNum,
            @ApiParam(value = "页大小",required = false) @RequestParam(required = false) Integer pageSize
    ) throws Exception {
        IPage<CustomerWalletReconciliation> page = walletLogic.reconciliationList(customerId,customerNumber,customerName, type,
                customerWalletId,walletRebateId, pageNum, pageSize,isReconciliation);
        return ResponseHelper.success(page);
    }


    @PostMapping("/reconciliate/check")
    @ApiOperation(value = "月度一键对账")
    public ResponseHelper reconciliateCheck(@RequestParam List<String> ids)  {
        walletLogic.reconciliateCheck(ids);
        return ResponseHelper.success();
    }


    @PostMapping("/reconciliate/generate/month")
    @ApiOperation(value = "生成月度账单")
    public ResponseHelper generateMouthReconciliate(
            @ApiParam(value = "月份",required = false) @RequestParam(required = false) String month,
            @ApiParam(value = "钱包类型（COMMONLY=普通钱包 REBATE=返利钱包）",required = false) @RequestParam(required = false) String type
    )  {
        walletLogic.generateMonthReconciliate(month,type);
        return ResponseHelper.success();
    }


}
