package cn.superdesk.uniorder.dubbo.provider.invoice.customer;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.superdesk.uniorder.common.enums.accounting.AccountingOrderTypeDimEnum;
import cn.superdesk.uniorder.common.enums.invoice.common.InvoiceTypeMoudelXModeEnum;
import cn.superdesk.uniorder.common.exception.UnifiedOrderRuntimeException;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.invoice.customer.InvoiceApply;
import cn.superdesk.uniorder.dao.entity.order.ExpressCompany;
import cn.superdesk.uniorder.dao.entity.sys.notify.SysNotify;
import cn.superdesk.uniorder.dto.param.invoice.customer.*;
import cn.superdesk.uniorder.dto.vo.invoice.customer.*;
import cn.superdesk.uniorder.dto.vo.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxListItemVo;
import cn.superdesk.uniorder.dubbo.facade.invoice.customer.DubboInvoiceApplyCustomerService;
import cn.superdesk.uniorder.dubbo.facade.sys.notify.DubboSysNotifyService;
import cn.superdesk.uniorder.event.invoiceApplyCustomerTrigger.InvoiceApplyCustomerTriggerEventPublisher;
import cn.superdesk.uniorder.event.invoiceApplyCustomerTrigger.TailUpdateAccountingScoreCustomerFieldInvoiceStatusEvent;
import cn.superdesk.uniorder.executor.scheduler.kafka._inner.InvoiceApplyCustomerKafkaEventPublisher;
import cn.superdesk.uniorder.executor.scheduler.kafka._inner.dto.invoice.customer.InvoiceApplyCustomerKafkaEventCheckAndUpdateAccountingOrderFieldInvoiceStatusEvent;
import cn.superdesk.uniorder.service.invoice.customer.InvoiceApplyCustomerExportExcelService;
import cn.superdesk.uniorder.service.invoice.customer.InvoiceApplyCustomerOpCreateService;
import cn.superdesk.uniorder.service.invoice.customer.InvoiceApplyCustomerService;
import cn.superdesk.uniorder.service.order._leaveover.ExpressCompanyService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;


/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/3/17 下午6:34
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/3/17 下午6:34
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service(version = "1.0.0", retries = -1, timeout = 10000)
public class DubboInvoiceApplyCustomerServiceImpl implements DubboInvoiceApplyCustomerService {

    @Autowired
    private InvoiceApplyCustomerService            invoiceApplyCustomerService;
    @Autowired
    private InvoiceApplyCustomerOpCreateService    invoiceApplyCustomerOpCreateService;
    @Autowired
    private InvoiceApplyCustomerExportExcelService invoiceApplyCustomerExportExcelService;

    @Autowired
    private ExpressCompanyService expressCompanyService;

    @Autowired
    private InvoiceApplyCustomerTriggerEventPublisher invoiceApplyCustomerTriggerEventPublisher;
    @Autowired
    private InvoiceApplyCustomerKafkaEventPublisher   invoiceApplyCustomerKafkaEventPublisher;


    /****************************************** 引用自身 Dubbo provider start ******************************************/

    @Reference(version = "1.0.0", timeout = 10000, retries = -1)
    private DubboSysNotifyService dubboSysNotifyService;

    /****************************************** 引用自身 Dubbo provider   end ******************************************/

    public InvoiceApply findById(Long invoiceApplyId) {
        return invoiceApplyCustomerService.findById(invoiceApplyId);
    }

    public List<InvoiceApply> findByIds(List<Long> invoiceApplyIds) {
        return invoiceApplyCustomerService.findByIds(invoiceApplyIds);
    }

    public int updateInvoiceApplyByPrimaryKeySelective(InvoiceApply invoiceApply) {
        return invoiceApplyCustomerService.updateInvoiceApplyByPrimaryKeySelective(invoiceApply);
    }

    @Override
    public List<InvoiceApplyCustomerSelectScoreVo> invoiceApplySelectFocusUnifiedDimOrder(Map<String, Object> params) throws UnifiedOrderRuntimeException {
        return invoiceApplyCustomerOpCreateService.invoiceApplySelectFocusUnifiedDimOrder(params);
    }

    @Override
    public List<InvoiceApplyCustomerSelectScoreVo> invoiceApplySelectFocusUnifiedDimGoods(Map<String, Object> params) throws UnifiedOrderRuntimeException {
        return invoiceApplyCustomerOpCreateService.invoiceApplySelectFocusUnifiedDimGoods(params);
    }

    @Override
    public List<InvoiceApplyCustomerSelectScoreVo> invoiceApplySelectFocusScoreDimCredit(InvoiceApplyCustomerQueryCriteria criteria) throws UnifiedOrderRuntimeException {
        return invoiceApplyCustomerOpCreateService.invoiceApplySelectFocusScoreDimCredit(criteria);
    }

    @Override
    public InvoiceApplyCustomerSelectScoreStruct invoiceApplySelectFocusScoreDimCreditV2_1(InvoiceApplyCustomerQueryCriteria criteria, LoginSysUser loginSysUser) throws UnifiedOrderRuntimeException {
        return invoiceApplyCustomerOpCreateService.invoiceApplySelectFocusScoreDimCreditV2_1(criteria, loginSysUser);
    }


    /**
     * 此方法定义为只支持 InvoiceTypeMoudelXModeEnum.线下开票
     *
     * @param form
     * @param loginSysUser
     * @throws UnifiedOrderRuntimeException
     */
    @Override
    public void opInvoiceApply(InvoiceApplyCommonOpApplyForm form, LoginSysUser loginSysUser) throws UnifiedOrderRuntimeException {

        log.info("发票申请 {}", AnJsonUtils.toPrettyJson(form));

        // 这地方开启了事务
        InvoiceApplyCustomerKafkaEventCheckAndUpdateAccountingOrderFieldInvoiceStatusEvent event = invoiceApplyCustomerOpCreateService.opInvoiceApply(form, loginSysUser, InvoiceTypeMoudelXModeEnum.线下开票);

        // 更新对账单 开票申请状态
        invoiceApplyCustomerKafkaEventPublisher.checkAndUpdateAccountingOrderFieldInvoiceStatus(event);


    }

    public R<InvoiceApplyFocusScoreOpApplyReportV2_1> opInvoiceApplyV2_1(InvoiceApplyFocusScoreOpApplyFormV2_1 form, LoginSysUser loginSysUser) {
        return invoiceApplyCustomerOpCreateService.opInvoiceApplyV2_1(form, loginSysUser);
    }


    /****************************************** 发票申请管理 start ******************************************/
    @Override
    public R<List<InvoiceApplyCustomerPageVo>> findViewByPageFocusUnified(Map<String, Object> params, int pageNum, int pageSize) {
        return invoiceApplyCustomerService.findViewByPageFocusUnified(params, pageNum, pageSize);
    }

    @Override
    public InvoiceApplyCustomerHeadDataVo findViewHeadDataFocusUnified(Map<String, Object> params) throws UnifiedOrderRuntimeException {
        return invoiceApplyCustomerService.findViewHeadDataFocusUnified(params);
    }

    @Override
    public R<List<InvoiceApplyCustomerPageVo>> findViewByPageFocusScore(InvoiceApplyCustomerQueryCriteria queryCriteria, int pageNum, int pageSize) {
        return invoiceApplyCustomerService.findViewByPageFocusScore(queryCriteria, pageNum, pageSize);
    }

    @Override
    public InvoiceApplyCustomerHeadDataVo findViewHeadDataFocusScore(InvoiceApplyCustomerQueryCriteria queryCriteria) throws UnifiedOrderRuntimeException {
        return invoiceApplyCustomerService.findViewHeadDataFocusScore(queryCriteria);
    }


    @Override
    public R<List<InvoiceApplyCustomerPageVo>> findViewByPageFocusScoreV2_1(InvoiceApplyCustomerQueryCriteriaV2_1 queryCriteria, LoginSysUser loginSysUser) {
        return invoiceApplyCustomerService.pageQueryInvoiceApplyListFocusScoreV2_1(queryCriteria);
    }

    /**
     * 积分结算 发票申请管理 op导出列表
     *
     * @param queryCriteria
     * @param loginSysUser
     * @return
     */
    @Override
    public String exportInvoiceApplyListExcelV2_1(InvoiceApplyCustomerQueryCriteriaV2_1 queryCriteria, LoginSysUser loginSysUser) {

        // 异步操作 包裹 mark by linjinyu at 20210317
        log.info("\n异步操作 包裹 外层 exportInvoiceApplyListExcelV2_1 params: \n{}", JSONUtil.toJsonStr(queryCriteria));

        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {

            try {

                log.info("\n异步操作 包裹 内层 exportInvoiceApplyListExcelV2_1 params: \n{}", JSONUtil.toJsonStr(queryCriteria));

                // 三步走 mark by linjinyu at 20210317
                // 1. 数据
                // 2. 生成excel
                // 3. 通知

                List<InvoiceApplyCustomerPageVo> resultList = invoiceApplyCustomerService.queryInvoiceApplyListFocusScoreV2_1(queryCriteria);

                SysNotify sysNotify = invoiceApplyCustomerExportExcelService.exportInvoiceApplyListExcelV2_1(resultList);

                // 通知
                dubboSysNotifyService.notifyExportExcelInvoiceApplyFocusUnifiedDimGoods(sysNotify, loginSysUser);

            } catch (Exception e2) {
                log.error("\n异步操作 包裹 内层 exportInvoiceApplyListExcelV2_1{} -> 执行 异常:{}", JSONUtil.toJsonStr(queryCriteria), e2);
            }
        });

        return "导出任务已经接收,正在处理中...";

    }

    @Override
    public InvoiceApplyCustomerHeadDataVoV2_1 findViewHeadDataFocusScoreV2_1(InvoiceApplyCustomerQueryCriteriaV2_1 queryCriteriaV2_1, LoginSysUser loginSysUser) throws UnifiedOrderRuntimeException {
        return invoiceApplyCustomerService.queryInvoiceApplyHeadDataFocusScoreV2_1(queryCriteriaV2_1);
    }


    @Override
    public R<List<InvoiceApplyCustomerPageVo>> findInvoiceListByPageFocusScoreV2_1(InvoiceApplyCustomerQueryCriteriaV2_1 queryCriteriaV2_1, LoginSysUser loginSysUser) throws UnifiedOrderRuntimeException {
        return invoiceApplyCustomerService.findInvoiceListByPageFocusScoreV2_1(queryCriteriaV2_1, loginSysUser);
    }

    @Override
    public List<InvoiceApplyCustomerPageVo> findInvoiceListFocusScoreV2_1(InvoiceApplyCustomerQueryCriteriaV2_1 queryCriteriaV2_1, LoginSysUser loginSysUser) {
        return invoiceApplyCustomerService.findInvoiceListFocusScoreV2_1(queryCriteriaV2_1, loginSysUser);
    }

    @Override
    public InvoiceApplyCustomerHeadDataVoV2_1 findInvoiceHeadDataFocusScoreV2_1(InvoiceApplyCustomerQueryCriteriaV2_1 queryCriteria, LoginSysUser loginSysUser) throws UnifiedOrderRuntimeException {
        return invoiceApplyCustomerService.findInvoiceHeadDataFocusScoreV2_1(queryCriteria, loginSysUser);
    }

    /****************************************** 发票申请管理   end ******************************************/


    /****************************************** 客户结算 开票申请 列表导出 Excel数据 start ******************************************/
    /**
     * 客户结算-发票申请管理-opt 导出报表 获取数据
     *
     * @param params
     * @return
     * @throws UnifiedOrderRuntimeException
     */
    @Override
    public List<ExportExcelInvoiceApplyForceUnifiedDimGoodsVo> findInvoiceApplyFocusUnifiedDimGoods(Map<String, Object> params) throws UnifiedOrderRuntimeException {
        return invoiceApplyCustomerService.findInvoiceApplyFocusUnifiedDimGoods(params);
    }

    /**
     * 积分结算-发票申请管理-opt 导出报表 获取数据
     *
     * @param queryCriteria
     * @return
     * @throws UnifiedOrderRuntimeException
     */
    @Override
    public List<ExportExcelInvoiceApplyCustomerForceScoreDimCreditVo> findInvoiceApplyFocusScoreDimCredit(InvoiceApplyCustomerQueryCriteria queryCriteria) throws UnifiedOrderRuntimeException {


        // 添加补刀 默认为 0 全部
        if (ObjectUtil.isNull(queryCriteria.getCreditAccountingState())) {
//            queryCriteria.setCreditAccountingState(CommonAccountingStateEnum.已对账.getKey());
            queryCriteria.setCreditAccountingState(0);
        }

        List<ExportExcelInvoiceApplyCustomerForceScoreDimCreditVo> resultList = invoiceApplyCustomerService.findInvoiceApplyFocusScoreDimCredit(queryCriteria);

        return resultList;
    }


    /**
     * TODO 未实现 这个获取数据都是有问题的 目前这个有个问题 Mapper 里边还是 订单维度的 表关联方式 mark by linjinyu at 20210317
     *
     * @param params
     * @param loginSysUser
     * @return
     * @throws UnifiedOrderRuntimeException
     */
    @Override
    public String exportExcelInvoiceApplyCustomerFocusUnifiedDimGoods(Map<String, Object> params, LoginSysUser loginSysUser) throws UnifiedOrderRuntimeException {

        // 异步操作 包裹 mark by linjinyu at 20210317
        log.info("\n异步操作 包裹 外层 exportExcelInvoiceApplyFocusUnifiedDimGoods params: \n{}", JSONUtil.toJsonStr(params));

        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {

            try {

                log.info("\n异步操作 包裹 内层 exportExcelInvoiceApplyFocusUnifiedDimGoods params: \n{}", JSONUtil.toJsonStr(params));

                // 三步走 mark by linjinyu at 20210317
                // 1. 数据
                // 2. 生成excel
                // 3. 通知

                List<ExportExcelInvoiceApplyForceUnifiedDimGoodsVo> resultList = invoiceApplyCustomerService.findInvoiceApplyFocusUnifiedDimGoods(params);

                SysNotify sysNotify = invoiceApplyCustomerExportExcelService.exportExcelInvoiceApplyFocusUnifiedDimGoods(resultList);

                // 通知
                dubboSysNotifyService.notifyExportExcelInvoiceApplyFocusUnifiedDimGoods(sysNotify, loginSysUser);

            } catch (Exception e2) {
                log.error("\n异步操作 包裹 内层 exportExcelInvoiceApplyFocusUnifiedDimGoods{} -> 执行 异常:{}", JSONUtil.toJsonStr(params), e2);
            }
        });

        return "导出任务已经接收,正在处理中...";

    }

    private static Map<Integer, Function<List<ExportExcelInvoiceApplyCustomerForceScoreDimCreditVo>, SysNotify>> modeOfExportExcelInvoiceApplyCustomerFocusScoreDimCredit = new HashMap<>();

    @Override
    public String exportExcelInvoiceApplyCustomerFocusScoreDimCredit(
            InvoiceApplyCustomerQueryCriteria queryCriteria,
            LoginSysUser loginSysUser) throws UnifiedOrderRuntimeException {


        // TODO 排序：同预开票申请内按流水的下单时间“升序”依次排列，同一时间随机排列；

        // 初始化
        // 侨城汇
        //	|---- 1. 搜索框 条件 结算集
        //	|---- 2. 搜索框 条件 结算集 后 界面上 勾选 要以 开票申请编号 分 sheet 分合并前 合并后
        //	|---- 3. 搜索框 条件 结算集 后 界面上 勾选 要以 开票申请编号 分 sheet 财务专用
        modeOfExportExcelInvoiceApplyCustomerFocusScoreDimCredit.put(1, (resultList) -> invoiceApplyCustomerExportExcelService.exportExcelInvoiceApplyFocusScoreDimCreditMode1(resultList));
        modeOfExportExcelInvoiceApplyCustomerFocusScoreDimCredit.put(2, (resultList) -> invoiceApplyCustomerExportExcelService.exportExcelInvoiceApplyFocusScoreDimCreditMode2(resultList));
        modeOfExportExcelInvoiceApplyCustomerFocusScoreDimCredit.put(3, (resultList) -> invoiceApplyCustomerExportExcelService.exportExcelInvoiceApplyFocusScoreDimCreditMode3(resultList));

        // 异步操作 包裹 mark by linjinyu at 20210317
        log.info("\n异步操作 包裹 外层 exportExcelInvoiceApplyFocusScoreDimCredit params: \n{}", JSONUtil.toJsonStr(queryCriteria));

        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {

            try {

                log.info("\n异步操作 包裹 内层 exportExcelInvoiceApplyFocusScoreDimCredit params: \n{}", JSONUtil.toJsonStr(queryCriteria));

                // 三步走 mark by linjinyu at 20210317
                // 1. 数据
                // 2. 生成excel
                // 3. 通知


                //【【结算中心-测试环境】积分结算-发票申请管理-勾选导出报表排序错误】https://www.tapd.cn/33003868/bugtrace/bugs/view?bug_id=1133003868001004728
                // 操作：积分结算-发票申请管理-勾选导出报表-CAA2022031717220096
                // 期望：
                // 排序：同预开票申请内按流水的下单时间“升序”依次排列，同一时间随机排列；
                queryCriteria.setOrderByClause(InvoiceApplyCustomerQueryCriteria.ORDER_BY_EXPORT_EXCEL);

                List<ExportExcelInvoiceApplyCustomerForceScoreDimCreditVo> resultList = invoiceApplyCustomerService.findInvoiceApplyFocusScoreDimCredit(queryCriteria);

                // 处理张雪的一个写都写不好的弱智逻辑 群里确认了 原话 按照我写的规则来就行了 20211210
//				发票申请管理操作按钮:"勾选导出报表(合并前后的备注字段取值都需要处理)/导出开票数据"导出数据的备注取值规则
//				规则:
//				1、取申请开票时输入的备注字段
//				2、当备注内容为空时默认显示该发票对应的对账单编号,若是备注有内容则显示备注内容和对账单编号

                resultList.forEach(item -> {
                    if (StrUtil.isNotBlank(queryCriteria.getRemark())) {

                        item.setRemark(queryCriteria.getRemark() + "&" + item.getAccountingOrderSn());
//						item.setRemarkRule(criteria.getRemark());
                    } else {
                        item.setRemark(item.getAccountingOrderSn());
//						item.setRemarkRule(item.getAccountingOrderSn());
                    }
                });

//				log.info("\n\n\nexportExcelInvoiceApplyFocusScoreDimCredit 数据准备 : {}\n\n\n", JsonUtils.toJson(resultList));

//				SysNotify sysNotify = invoiceApplyExportExcelService.exportExcelInvoiceApplyFocusScoreDimCreditMode1(resultList);
                SysNotify sysNotify = modeOfExportExcelInvoiceApplyCustomerFocusScoreDimCredit.get(queryCriteria.getMode()).apply(resultList);
                sysNotify.setRemark(queryCriteria.getRemark());

                // 通知
                dubboSysNotifyService.notifyExportExcelInvoiceApplyFocusScoreDimCredit(sysNotify, loginSysUser);

            } catch (Exception e2) {
                log.error("\n异步操作 包裹 内层 exportExcelInvoiceApplyFocusScoreDimCredit{} -> 执行 异常:{}", JSONUtil.toJsonStr(queryCriteria), e2);
            }
        });

        return "导出任务已经接收,正在处理中...";

    }

    /****************************************** 客户结算 开票申请 列表导出 Excel数据 start ******************************************/


    /****************************************** 客户结算 开票申请 发票管理 详情 start ******************************************/
    @Override
    public InvoiceApplyCustomerDetailDimGoodsVo getUnifiedCustomerInvoice2GoodsDetail(Map<String, Object> params) throws UnifiedOrderRuntimeException {
        return invoiceApplyCustomerService.getUnifiedCustomerInvoice2GoodsDetail(params);
    }

    @Override
    public String exportExcelUnifiedCustomerInvoice2GoodsDetail(Map<String, Object> params, LoginSysUser loginSysUser) throws UnifiedOrderRuntimeException {

        // 异步操作 包裹 mark by linjinyu at 20210223
        log.info("\n异步操作 包裹 外层 exportUnifiedCustomerInvoice2GoodsDetail params: \n{}", JSONUtil.toJsonStr(params));

        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {

            try {

                log.info("\n异步操作 包裹 内层 exportUnifiedCustomerInvoice2GoodsDetail params: \n{}", JSONUtil.toJsonStr(params));

                // 三步走 mark by linjinyu at 20210305
                // 1. 数据
                // 2. 生成excel
                // 3. 通知

                InvoiceApplyCustomerDetailDimGoodsVo invoiceApplyDetailDimGoodsVo = invoiceApplyCustomerService.getUnifiedCustomerInvoice2GoodsDetail(params);

                SysNotify sysNotify = invoiceApplyCustomerExportExcelService.exportExcelUnifiedCustomerInvoice2GoodsDetail(invoiceApplyDetailDimGoodsVo);

                // 通知
                dubboSysNotifyService.notifyExportExcelUnifiedCustomerInvoice2GoodsDetail(sysNotify, loginSysUser);

            } catch (Exception e2) {
                log.error("\n异步操作 包裹 内层 exportUnifiedCustomerInvoice2GoodsDetail{} -> 执行 异常:{}", JSONUtil.toJsonStr(params), e2);
            }
        });

        return "导出任务已经接收,正在处理中...";


    }

    @Override
    public InvoiceApplyCustomerDetailDimCreditVo getInvoiceApplyCustomerDetailDimCredit(
            InvoiceApplyCustomerQueryCriteria criteria,
            LoginSysUser loginSysUser) throws UnifiedOrderRuntimeException {
        return invoiceApplyCustomerService.getInvoiceApplyCustomerDetailDimCredit(criteria, loginSysUser);
    }

    @Override
    public InvoiceApplyCustomerDetailDimCreditVoV2_1 getInvoiceApplyCustomerDetailDimCreditV2_1(
            InvoiceApplyCustomerQueryCriteriaV2_1 criteria,
            LoginSysUser loginSysUser) throws UnifiedOrderRuntimeException {
        return invoiceApplyCustomerService.getInvoiceApplyCustomerDetailDimCreditV2_1(criteria, loginSysUser);
    }

    /**
     * @param criteria
     * @param loginSysUser
     * @return
     * @throws UnifiedOrderRuntimeException
     */
    @Override
    public String exportExcelUnifiedCustomerInvoice2CreditDetail(InvoiceApplyCustomerQueryCriteria criteria, LoginSysUser loginSysUser) throws UnifiedOrderRuntimeException {


        // 异步操作 包裹 mark by linjinyu at 20210223
        log.info("\n异步操作 包裹 外层 exportExcelUnifiedCustomerInvoice2CreditDetail params: \n{}", JSONUtil.toJsonStr(criteria));

        CompletableFuture.runAsync(() -> {

            try {
                // 三步走 mark by linjinyu at 20210305
                // 1. 数据
                // 2. 生成excel
                // 3. 通知
                InvoiceApplyCustomerDetailDimCreditVo invoiceApplyDetailDimCreditVo = invoiceApplyCustomerService.getInvoiceApplyCustomerDetailDimCredit(criteria, loginSysUser);

                SysNotify sysNotify = invoiceApplyCustomerExportExcelService.exportExcelUnifiedCustomerInvoice2CreditDetail(invoiceApplyDetailDimCreditVo);

                // 通知
                dubboSysNotifyService.notifyExportExcelUnifiedCustomerInvoice2CreditDetail(sysNotify, loginSysUser);


            } catch (Exception e2) {
                log.error("\n异步操作 包裹 内层 exportExcelUnifiedCustomerInvoice2CreditDetail {} -> 执行 异常:{}", JSONUtil.toJsonStr(criteria), e2);
            }
        });

        return "导出任务已经接收,正在处理中...";
    }

    /****************************************** 客户结算 开票申请 发票管理 详情 start ******************************************/


    /****************************************** 第三方软件导出类 start ******************************************/
    /**
     * exportExcelUnifiedCustomerInvoice2GoodsDetail
     *
     * @param invoiceApplyId
     * @param loginSysUser
     * @return
     * @throws UnifiedOrderRuntimeException
     */
    @Override
    public String exportExcelDimUnifiedInvoiceRegister4E6(Long invoiceApplyId, LoginSysUser loginSysUser) throws UnifiedOrderRuntimeException {

        log.info("\n异步操作 包裹 外层 exportExcelDimUnifiedInvoiceRegister4E6 params: \n{}", invoiceApplyId);

        Map<String, Object> params = new HashMap<>();

        // 发票申请Id invoiceApplyId
        params.put("id", invoiceApplyId);

        CompletableFuture.runAsync(() -> {

            try {
                // 三步走 mark by linjinyu at 20210305
                // 1. 数据
                // 2. 生成excel
                // 3. 通知
                InvoiceApplyCustomerDetailDimGoodsVo invoiceApplyDetailDimGoodsVo = invoiceApplyCustomerService.getUnifiedCustomerInvoice2GoodsDetail(params);

                SysNotify sysNotify = invoiceApplyCustomerExportExcelService.exportExcelDimUnifiedInvoiceRegister4E6(invoiceApplyDetailDimGoodsVo);

                // 通知
                dubboSysNotifyService.notifyExportExcelDimUnifiedInvoiceRegister4E6(sysNotify, loginSysUser);


            } catch (Exception e2) {
                log.error("\n异步操作 包裹 内层 exportExcelDimUnifiedInvoiceRegister4E6 {} -> 执行 异常:{}", invoiceApplyId, e2);
            }
        });

        return "导出任务已经接收,正在处理中...";

    }

    /****************************************** 第三方软件导出类   end ******************************************/


    @Override
    public String importInvoiceExcel(String token, List<Map<String, Object>> list) throws UnifiedOrderRuntimeException {
        return invoiceApplyCustomerService.importInvoiceExcel(list, token);
    }

    @Override
    public String importSendInvoiceExcel(String token, List<Map<String, Object>> list) throws UnifiedOrderRuntimeException {
        return invoiceApplyCustomerService.importSendInvoiceExcel(list, token);
    }


    @Override
    public List<ExpressCompany> findExpressCompany() throws UnifiedOrderRuntimeException {
        return expressCompanyService.findAllExpressCompany();
    }

    @Override
    public R<List<InvoiceApply>> findInvoiceByPageNew(Map<String, Object> params, int pageNum, int pageSize) {
        return invoiceApplyCustomerService.findInvoiceByPageNew(params, pageNum, pageSize);
    }

    @Override
    public List<InvoiceApply> findByOrderSn(Map<String, Object> invQuery) {
        return invoiceApplyCustomerService.findByOrderSn(invQuery);
    }

    @Override
    public List<InvoiceApply> findByAccountingOrderSn(String accountingOrderSn, AccountingOrderTypeDimEnum accountingOrderTypeDimEnum) {
        return invoiceApplyCustomerService.findByAccountingOrderSn(accountingOrderSn, accountingOrderTypeDimEnum);
    }

    @Override
    public InvoiceApply findByInvoiceApplySn(String invoiceApplySn, LoginSysUser loginSysUser) {
        return invoiceApplyCustomerService.findByInvoiceApplySn(invoiceApplySn, loginSysUser);
    }

    @Override
    public Map<String, InvoiceApply> findByOrderSnList(List<String> orderSnList) {

        return invoiceApplyCustomerService.findInvoiceByOrderSnList(orderSnList);

    }


    @Override
    public void validatedInvoiceApplyRelationTable() {


        invoiceApplyCustomerService.validatedInvoiceApplyRelationTable();
    }


    @Override
    public void opAudit(Map<String, Object> params, LoginSysUser loginSysUser) throws UnifiedOrderRuntimeException {

        // 这地方开启了事务
        TailUpdateAccountingScoreCustomerFieldInvoiceStatusEvent event = invoiceApplyCustomerService.opAudit(params, loginSysUser);

        invoiceApplyCustomerTriggerEventPublisher.triggerTailUpdateAccountingOrderFieldInvoiceStatus(event);
    }

    /**
     * 删除发票申请 关联删除 此申请 X 流水
     *
     * @param invoiceApplyId
     * @param loginSysUser
     * @return
     */
    @Override
    public int opDelete(Long invoiceApplyId, LoginSysUser loginSysUser) {
        return invoiceApplyCustomerService.opDelete(invoiceApplyId, loginSysUser);
    }

    /**
     * @param form
     * @param invoiceApplyEntity
     * @param fptxxListItemVo
     * @param loginSysUser
     * @return
     * @throws UnifiedOrderRuntimeException
     */
    @Override
    public int opAgainSzhtxxElectronicInvoice(
            InvoiceApplyCustomerOpAgainInvoiceApplyForm form,
            InvoiceApply invoiceApplyEntity,
            SzhtxxElectronicInvoiceFptxxListItemVo fptxxListItemVo,
            LoginSysUser loginSysUser) throws UnifiedOrderRuntimeException {


        // 这地方开启了事务
        TailUpdateAccountingScoreCustomerFieldInvoiceStatusEvent event = invoiceApplyCustomerService.opAgainSzhtxxElectronicInvoice(form, invoiceApplyEntity, fptxxListItemVo, loginSysUser);

        invoiceApplyCustomerTriggerEventPublisher.triggerTailUpdateAccountingOrderFieldInvoiceStatus(event);

        return 1;
    }

    @Override
    public int opAgainSzhtxxPaperInvoice(
            InvoiceApplyCustomerOpAgainInvoiceApplyForm form,
            InvoiceApply invoiceApplyEntity,
            SzhtxxElectronicInvoiceFptxxListItemVo fptxxListItemVo,
            LoginSysUser loginSysUser) throws UnifiedOrderRuntimeException {

        // 这地方开启了事务
        TailUpdateAccountingScoreCustomerFieldInvoiceStatusEvent event = invoiceApplyCustomerService.opAgainSzhtxxElectronicInvoice(form, invoiceApplyEntity, fptxxListItemVo, loginSysUser);

        invoiceApplyCustomerTriggerEventPublisher.triggerTailUpdateAccountingOrderFieldInvoiceStatus(event);

        return 1;
    }


    /**
     * opt 开票登记
     *
     * @param params
     * @param loginSysUser
     * @throws UnifiedOrderRuntimeException
     */
    @Override
    public void opRegister(Map<String, Object> params, LoginSysUser loginSysUser) throws UnifiedOrderRuntimeException {
        invoiceApplyCustomerService.opRegister(params, loginSysUser);
    }

    @Override
    public int opModifyInvoiceTitle(InvoiceApplyCustomerOpModifyInvoiceTitleForm form, LoginSysUser loginSysUser) {
        return invoiceApplyCustomerService.opModifyInvoiceTitle(form, loginSysUser);
    }

    /**
     * opt 寄票登记
     *
     * @param sendInvoiceRegisterList
     * @param loginSysUser
     * @throws UnifiedOrderRuntimeException
     */
    @Override
    public void sendInvoiceRegister(List<Map> sendInvoiceRegisterList, LoginSysUser loginSysUser) throws UnifiedOrderRuntimeException {
        invoiceApplyCustomerService.invoiceRegisterSend(sendInvoiceRegisterList, loginSysUser);
    }

    /****************************************** 补丁程序 start ******************************************/

    @Override
    public void patchInvoiceApplyCustomerFixV2_1() {
        invoiceApplyCustomerService.patchInvoiceApplyCustomerFixV2_1();
    }

}
