package cn.superdesk.uniorder.controller._thirdpart.szhtxx.paperInvoice;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.superdesk.uniorder.common.bizScene.invoice.customer.InvoiceApplyCustomerScene;
import cn.superdesk.uniorder.common.constants.ResponseMsgConstants;
import cn.superdesk.uniorder.common.enums.common.CommonFormStateEnum;
import cn.superdesk.uniorder.common.enums.invoice.InvoiceInvoicedIsRedWriteOffEnum;
import cn.superdesk.uniorder.common.enums.invoice.customer.InvoiceApplyBecomeInvalidEnum;
import cn.superdesk.uniorder.common.enums.invoice.customer.InvoiceApplyCustomerBackAccountStatusEnum;
import cn.superdesk.uniorder.common.enums.invoice.customer.InvoiceApplyCustomerInvoiceProgressingStatusEnum;
import cn.superdesk.uniorder.common.enums.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxKplxEnum;
import cn.superdesk.uniorder.common.enums.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceResponseCodeEnum;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.rest.response.ResponseResultOuterUtils;
import cn.superdesk.uniorder.config.mail.MailAccountConfigFactory;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.invoice.customer.InvoiceApply;
import cn.superdesk.uniorder.dao.entity.sys.notify.SysNotify;
import cn.superdesk.uniorder.dto.param.thirdpart.szhtxx.common.becomeInvalid.SzhtxxInvoiceFptxxOpBecomeInvalidForm;
import cn.superdesk.uniorder.dto.param.thirdpart.szhtxx.common.singleBilling.SzhtxxInvoiceFptxxOpSingleBillingForm;
import cn.superdesk.uniorder.dto.param.thirdpart.szhtxx.common.taxDeviceInfo.SzhtxxPaperInvoiceOpTaxDeviceInfoForm;
import cn.superdesk.uniorder.dto.param.thirdpart.szhtxx.common.taxInfo.SzhtxxPaperInvoiceOpTaxInfoForm;
import cn.superdesk.uniorder.dto.param.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxQueryCriteria;
import cn.superdesk.uniorder.dto.vo.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxDetailVo;
import cn.superdesk.uniorder.dto.vo.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxListItemVo;
import cn.superdesk.uniorder.dto.vo.thirdpart.szhtxx.paperInvoice.SzhtxxPaperInvoiceFptxxOpBecomeInvalidFakeVo;
import cn.superdesk.uniorder.dto.vo.thirdpart.szhtxx.paperInvoice.SzhtxxPaperInvoiceFptxxOpBecomeInvalidVo;
import cn.superdesk.uniorder.dto.vo.thirdpart.szhtxx.paperInvoice.SzhtxxPaperInvoiceFptxxOpRedWriteVo;
import cn.superdesk.uniorder.dubbo.consumer._common.ProviderEnvConstantDubboCustomerService;
import cn.superdesk.uniorder.dubbo.consumer.invoice.customer.InvoiceApplyCustomerDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.thirdpart.szhtxx.paperInvoice.SzhtxxPaperInvoiceFptxxDubboConsumerService;
import cn.superdesk.uniorder.openapicall.thirdpart.szhtxx.paperInvoice.dto.request.SID121TaxInfo.SID121TaxInfoRequestBody;
import cn.superdesk.uniorder.openapicall.thirdpart.szhtxx.paperInvoice.dto.request.SID1SingleBilling.SID1SingleBillingRequestBody;
import cn.superdesk.uniorder.openapicall.thirdpart.szhtxx.paperInvoice.dto.request.SID25TaxDeviceInfo.SID25TaxDeviceInfoRequestBody;
import cn.superdesk.uniorder.openapicall.thirdpart.szhtxx.paperInvoice.dto.request.SID4ToInvalid.SID4ToInvalidRequestBody;
import cn.superdesk.uniorder.openapicall.thirdpart.szhtxx.paperInvoice.dto.request.SID5SelectBillInfo.SID5SelectBillInfoRequestBody;
import cn.superdesk.uniorder.openapicall.thirdpart.szhtxx.paperInvoice.dto.response.SID25TaxDeviceInfo.SID25TaxDeviceInfoResponseBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.TemplateEngine;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 第三方-深圳航信纸质票
 * @Author: linjinyu
 * @CreateDate: 2022/4/27 12:13 下午
 * @UpdateUser: linjinyu
 * @UpdateDate: 2022/4/27 12:13 下午
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "第三方-深圳航信纸质票", tags = {"第三方-深圳航信纸质票"})
@Slf4j
@RestController
@RequestMapping("/thirdpart/szhtxx/paperInvoiceFptxx")
public class SzhtxxPaperInvoiceFptxxController extends BaseController {

    @Autowired
    private SzhtxxElectronicInvoiceFptxxDubboConsumerService szhtxxElectronicInvoiceFptxxDubboConsumerService;
    @Autowired
    private SzhtxxPaperInvoiceFptxxDubboConsumerService      szhtxxPaperInvoiceFptxxDubboConsumerService;


    @Autowired
    private InvoiceApplyCustomerDubboConsumerService invoiceApplyCustomerDubboConsumerService;

    @Autowired
    private MailAccountConfigFactory mailAccountConfigFactory;
    @Autowired
    private TemplateEngine           templateEngine;

    @Resource
    private ProviderEnvConstantDubboCustomerService providerEnvConstantDubboCustomerService;

//    发票上传、红字信息申请、下载：无测试环境 mark by linjinyu at 20220427

    /**************************************** op红冲发票 start ****************************************/

    /**
     * SingleBillingRequestBodyKpContent
     *
     * @param form
     * @return
     */
    @ApiOperation(value = "op红冲发票-Request(纸质专用发票)(深圳航信纸质票)")
    @RequestMapping(value = "/opRedWrite/request", method = RequestMethod.POST)
    @ResponseBody
    public R<SzhtxxPaperInvoiceFptxxOpRedWriteVo> opRedWriteRequest(@RequestBody SzhtxxInvoiceFptxxOpSingleBillingForm form) {

        R<SzhtxxPaperInvoiceFptxxOpRedWriteVo> result = new R<>();

        form.setFormState(CommonFormStateEnum.预览);
        form.setSzhtxxElectronicInvoiceFptxxKplxEnum(SzhtxxElectronicInvoiceFptxxKplxEnum.红冲发票);

        try {


            /******************************************** 初始化 start ********************************************/

            InvoiceApply entity = invoiceApplyCustomerDubboConsumerService.findByInvoiceApplySn(form.getFpqqlsh(), getToken());

            List<SzhtxxElectronicInvoiceFptxxListItemVo> resultList = szhtxxElectronicInvoiceFptxxDubboConsumerService.queryGenericParam(
                    SzhtxxElectronicInvoiceFptxxQueryCriteria
                            .builder()
                            .szhtxxElectronicInvoiceFptxxId(form.getSzhtxxElectronicInvoiceFptxxId())
                            .build(),
                    getToken()
            );

            if (CollUtil.isEmpty(resultList)) {
                return ResponseResultOuterUtils.createErrorResult("500", "未查找到相关的信息");
            }

            SzhtxxElectronicInvoiceFptxxListItemVo fptxxListItemVo = resultList.get(0);

            if (StrUtil.isBlank(fptxxListItemVo.getRedTaxInfoSn())) {
                return ResponseResultOuterUtils.createErrorResult("500", "请先申请红字信息表编号");
            }

            /******************************************** 初始化   end ********************************************/

            /**************************************** 蓝票主体 start ****************************************/

            SzhtxxElectronicInvoiceFptxxDetailVo fptxxOrigin = szhtxxElectronicInvoiceFptxxDubboConsumerService.detailGenericParamSzhtxxElectronicInvoiceFptxx(
                    SzhtxxElectronicInvoiceFptxxQueryCriteria.builder()
                            .fpqqlsh(form.getFpqqlsh())
                            .szhtxxElectronicInvoiceFptxxId(form.getSzhtxxElectronicInvoiceFptxxId())
                            .build(),
                    getToken()
            );

            /**************************************** 蓝票主体 start ****************************************/


            /******************************************** 红票主体 start ********************************************/

            SID1SingleBillingRequestBody sid1SingleBillingRequestBody = szhtxxPaperInvoiceFptxxDubboConsumerService.opRedWriteRequest(
                    form,
                    fptxxListItemVo,
                    entity,
                    getToken()
            );

            /******************************************** 红票主体   end ********************************************/

            result.setSuccess(true);
            result.setData(SzhtxxPaperInvoiceFptxxOpRedWriteVo.builder()
                    .fptxxOrigin(fptxxOrigin)
                    .fttxxRedWrite(sid1SingleBillingRequestBody.packing())
                    .build());

        } catch (Exception e) {

            result.setSuccess(false);
            result.setMessage(ResponseMsgConstants.ERROR_UNKNOW);
            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();
        }

        return result;
    }

    /**
     * op红冲发票-op确认红冲
     * |---- 线下开票 ---- 与之差别就是 不提交
     * |---- 深圳航信电子发票 ---- 与之差别就是 不提交
     * |---- 深圳航信纸质发票
     *
     * @param id
     * @param form
     * @return
     */
    @ApiOperation(value = "op红冲发票-op确认红冲(纸质专用发票)(深圳航信纸质发票)")
    @RequestMapping(value = "/opRedWrite/opConfirm/launchBefore/checkUpinvoiceMode/szhtxxPaperInvoice", method = RequestMethod.POST)
    @ResponseBody
    public R<String> opOngoingOpConfirmLaunchBeforeCheckUp(@RequestBody SzhtxxInvoiceFptxxOpSingleBillingForm form) {

        log.info("opOngoingOpConfirmLaunchBeforeCheckUp:{}", JSONUtil.toJsonStr(form));

        if (StrUtil.isBlank(form.getInvoiceApplySn())) {
            return ResponseResultOuterUtils.createErrorResult("500", "参数错误，需填写开票申请编号[invoiceApplySn]");
        }

        InvoiceApply invoiceApplyCustomerEntity = invoiceApplyCustomerDubboConsumerService.findByInvoiceApplySn(form.getInvoiceApplySn(), getToken());

        return opConfirmLaunchBeforeCheckUp(invoiceApplyCustomerEntity, form);
    }


    /**
     * op确认红冲(前检查)
     *
     * @param invoiceApplyCustomerEntity
     * @param form
     * @return
     */
    private R<String> opConfirmLaunchBeforeCheckUp(InvoiceApply invoiceApplyCustomerEntity, SzhtxxInvoiceFptxxOpSingleBillingForm form) {

        log.info("opConfirmLaunchBeforeCheckUp:\n{}\n{}", JSONUtil.toJsonStr(invoiceApplyCustomerEntity), JSONUtil.toJsonStr(form));

        if (ObjectUtil.isNull(invoiceApplyCustomerEntity)) {
            return ResponseResultOuterUtils.createErrorResult("500", "该发票所属开票申请不存在");
        }

        /**************************************** 检查 start ****************************************/

        // TODO mark by linjinyu at 2022-05-151 06:06:54 -
//        【【结算中心-测试环境】积分结算-金税盘发票-【确认红冲】需添加多用户操作时的判断】https://www.tapd.cn/33003868/bugtrace/bugs/view?bug_id=1133003868001005320
//        2.4.4.3.3.13 【确认红冲】
//        （2）服务端需判断：
//·该原发票所属的开票申请存在，否则不允许确认红冲，1.5s提示“该发票所属开票申请不存在”；--
//·该原发票所属的开票申请的申请状态为“非已红冲”，否则不允许确认红冲，1.5s提示“该发票所属开票申请的申请状态已红冲，请检查”；--
//·该原发票的“是否红冲”为“否”，否则不允许确认红冲，1.5s提示“该发票已红冲，请勿重复红冲”；--
//·该原发票的“作废标志”为“未作废”，否则不允许确认红冲，1.5s提示“该发票已作废，不允许红冲”；【新增判断】
//·根据当前开票方式判断：【新增“深圳航信纸质发票”判断点】

//        AccountingScoreCustomer accountingScoreCustomer = accountingScoreCustomerDubboConsumerService.findByAccountingOrderSn(accountingOrderSn);
//
//        if (ObjectUtil.isNull(accountingScoreCustomer)) {
//            return ResponseResultOuterUtils.createErrorResult("500", "对账单数据异常[对账单003]");
//        }
//
//        if (accountingScoreCustomer.getAccountingStatus().compareTo(AccountingOrderCustomerAccountingStatusEnum.已确认.getKey()) != 0) {
//            return ResponseResultOuterUtils.createErrorResult("500", "该开票申请所属的对账单状态未确认或已取消，请检查");
//        }
//
//
        Integer[] invoiceProgressingStatusNeed = {
                InvoiceApplyCustomerInvoiceProgressingStatusEnum.已红冲.getKey(),
                InvoiceApplyCustomerInvoiceProgressingStatusEnum.已作废.getKey(),

        };
        if (ArrayUtil.contains(invoiceProgressingStatusNeed, invoiceApplyCustomerEntity.getInvoiceStatus())) {
            return ResponseResultOuterUtils.createErrorResult("500", "该发票所属开票申请的申请状态已红冲，请检查");
        }
//
//
//        Integer[] makeInvoiceStatusNeed = {
//                InvoiceApplyCustomerMakeInvoiceStatusEnum.待开票.getKey(),
//                InvoiceApplyCustomerMakeInvoiceStatusEnum.开票中.getKey()
//        };
//        if (!ArrayUtil.contains(makeInvoiceStatusNeed, invoiceApplyCustomerEntity.getReceiveInvoiceStatus())) {
//            return ResponseResultOuterUtils.createErrorResult("500", "该开票申请的开票状态开票中或已开票，请检查");
//        }

        /**************************************** 蓝票主体 start ****************************************/

//        SzhtxxElectronicInvoiceFptxxDetailVo fptxxOrigin = szhtxxElectronicInvoiceFptxxDubboConsumerService.detailGenericParamSzhtxxElectronicInvoiceFptxx(
//                SzhtxxElectronicInvoiceFptxxQueryCriteria.builder()
//                        .fpqqlsh(form.getFpqqlsh())
//                        .szhtxxElectronicInvoiceFptxxId(form.getSzhtxxElectronicInvoiceFptxxId())
//                        .build(),
//                getToken()
//        );

        List<SzhtxxElectronicInvoiceFptxxListItemVo> resultList = szhtxxElectronicInvoiceFptxxDubboConsumerService.queryGenericParam(
                SzhtxxElectronicInvoiceFptxxQueryCriteria.builder()
                        .szhtxxElectronicInvoiceFptxxId(form.getSzhtxxElectronicInvoiceFptxxId())
                        .build(),
                getToken()
        );

        if (CollUtil.isEmpty(resultList)) {
            return ResponseResultOuterUtils.createErrorResult("500", "未查找到相关的信息");
        }

        SzhtxxElectronicInvoiceFptxxListItemVo fptxxListItemVo = resultList.get(0);

        if (InvoiceInvoicedIsRedWriteOffEnum.是.getKey().equals(fptxxListItemVo.getIsRedWriteOff())) {
            return ResponseResultOuterUtils.createErrorResult("500", "该发票已红冲，请勿重复红冲");
        }

        if (InvoiceApplyBecomeInvalidEnum.已作废.getKey().equals(fptxxListItemVo.getFlagVoidSign())) {
            return ResponseResultOuterUtils.createErrorResult("500", "该发票已作废，不允许红冲");
        }

        /**************************************** 蓝票主体 start ****************************************/

        return ResponseResultOuterUtils.createSuccesResult();


        /**************************************** 检查   end ****************************************/
    }

    @ApiOperation(value = "op红冲发票-Response(纸质专用发票)(深圳航信纸质票)")
    @RequestMapping(value = "/opRedWrite/response", method = RequestMethod.POST)
    @ResponseBody
    public R<String> opRedWriteResponse(@RequestBody SzhtxxInvoiceFptxxOpSingleBillingForm form) {

        form.setFormState(CommonFormStateEnum.提交);
        form.setSzhtxxElectronicInvoiceFptxxKplxEnum(SzhtxxElectronicInvoiceFptxxKplxEnum.红冲发票);

        try {
            InvoiceApply invoiceApplyEntity = invoiceApplyCustomerDubboConsumerService.findByInvoiceApplySn(form.getFpqqlsh(), getToken());

            List<SzhtxxElectronicInvoiceFptxxListItemVo> resultList = szhtxxElectronicInvoiceFptxxDubboConsumerService.queryGenericParam(
                    SzhtxxElectronicInvoiceFptxxQueryCriteria
                            .builder()
                            .szhtxxElectronicInvoiceFptxxId(form.getSzhtxxElectronicInvoiceFptxxId())
                            .build(),
                    getToken()
            );

            if (CollUtil.isEmpty(resultList)) {
                return ResponseResultOuterUtils.createErrorResult("500", "未查找到相关的信息");
            }

            SzhtxxElectronicInvoiceFptxxListItemVo szhtxxElectronicInvoiceFptxxListItemVo = resultList.get(0);

            SysNotify sysNotify = szhtxxPaperInvoiceFptxxDubboConsumerService.opRedWriteResponse(
                    form,
                    szhtxxElectronicInvoiceFptxxListItemVo,
                    invoiceApplyEntity,
                    getToken()
            );

            if (sysNotify.getTitle().equals(SzhtxxElectronicInvoiceResponseCodeEnum.SUCCESS.getReturnCode())) {
                return ResponseResultOuterUtils.createSuccesResult(sysNotify.getContent());
            } else {
                return ResponseResultOuterUtils.createErrorResult("500", sysNotify.getContent());
            }

        } catch (Exception e) {

            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();

            return ResponseResultOuterUtils.createErrorResult("500", ResponseMsgConstants.ERROR_UNKNOW);
        }
    }

    /**************************************** op红冲发票   end ****************************************/


    /**************************************** op作废发票 start ****************************************/

    @ApiOperation(value = "op作废发票-Request(纸质专用发票)(深圳航信纸质票)")
    @RequestMapping(value = "/opBecomeInvalid/request", method = RequestMethod.POST)
    @ResponseBody
    public R<SzhtxxPaperInvoiceFptxxOpBecomeInvalidVo> opBecomeInvalidRequest(@RequestBody SzhtxxInvoiceFptxxOpBecomeInvalidForm form) {

        R<SzhtxxPaperInvoiceFptxxOpBecomeInvalidVo> result = new R<>();

        form.setFormState(CommonFormStateEnum.预览);

        try {


            /******************************************** 初始化 start ********************************************/

            InvoiceApply entity = invoiceApplyCustomerDubboConsumerService.findByInvoiceApplySn(form.getFpqqlsh(), getToken());

            List<SzhtxxElectronicInvoiceFptxxListItemVo> resultList = szhtxxElectronicInvoiceFptxxDubboConsumerService
                    .queryGenericParam(
                            SzhtxxElectronicInvoiceFptxxQueryCriteria.builder()
                                    .szhtxxElectronicInvoiceFptxxId(form.getSzhtxxElectronicInvoiceFptxxId())
                                    .build(),
                            getToken()
                    );

            if (CollUtil.isEmpty(resultList)) {
                return ResponseResultOuterUtils.createErrorResult("500", "未查找到相关的信息");
            }

            SzhtxxElectronicInvoiceFptxxListItemVo fptxxListItemVo = resultList.get(0);

            /******************************************** 初始化   end ********************************************/

            /**************************************** 蓝票主体 start ****************************************/

            SzhtxxElectronicInvoiceFptxxDetailVo fptxxOrigin = szhtxxElectronicInvoiceFptxxDubboConsumerService.detailGenericParamSzhtxxElectronicInvoiceFptxx(
                    SzhtxxElectronicInvoiceFptxxQueryCriteria.builder()
                            .fpqqlsh(form.getFpqqlsh())
                            .szhtxxElectronicInvoiceFptxxId(form.getSzhtxxElectronicInvoiceFptxxId())
                            .build(),
                    getToken()
            );

            /**************************************** 蓝票主体 start ****************************************/


            /******************************************** 红票主体 start ********************************************/

            SID4ToInvalidRequestBody sid4ToInvalidRequestBody = szhtxxPaperInvoiceFptxxDubboConsumerService.opBecomeInvalidRequest(
                    form,
                    fptxxListItemVo,
                    entity,
                    getToken()
            );

            /******************************************** 红票主体   end ********************************************/

            result.setSuccess(true);
            result.setData(SzhtxxPaperInvoiceFptxxOpBecomeInvalidVo.builder()
                    .fptxxOrigin(fptxxOrigin)
                    .fttxxBecomeInvalid(sid4ToInvalidRequestBody.packing())
                    .build());

        } catch (Exception e) {

            result.setSuccess(false);
            result.setMessage(ResponseMsgConstants.ERROR_UNKNOW);
            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();
        }

        return result;
    }

    @ApiOperation(value = "op作废发票-Response(纸质专用发票)(深圳航信纸质票)")
    @RequestMapping(value = "/opBecomeInvalid/response", method = RequestMethod.POST)
    @ResponseBody
    public R<String> opBecomeInvalidResponse(@RequestBody SzhtxxInvoiceFptxxOpBecomeInvalidForm form) {

        form.setFormState(CommonFormStateEnum.提交);

        try {

            /**************************************** 检查 start ****************************************/

            InvoiceApply invoiceApplyEntity = invoiceApplyCustomerDubboConsumerService.findByInvoiceApplySn(form.getFpqqlsh(), getToken());

            if (ObjectUtil.isNull(invoiceApplyEntity)) {
                return ResponseResultOuterUtils.createErrorResult("500", "该开票申请不存在");
            }

            if (!ArrayUtil.contains(
                    InvoiceApplyCustomerScene.statusV2_1_Array_InvoiceProgressingSceneStateCheck(),
                    invoiceApplyEntity.getInvoiceStatus())
            ) {
                return ResponseResultOuterUtils.createErrorResult("500", "该开票申请状态发生变更，请返回检查");
            }

            List<SzhtxxElectronicInvoiceFptxxListItemVo> resultList = szhtxxElectronicInvoiceFptxxDubboConsumerService
                    .queryGenericParam(
                            SzhtxxElectronicInvoiceFptxxQueryCriteria
                                    .builder()
                                    .szhtxxElectronicInvoiceFptxxId(form.getSzhtxxElectronicInvoiceFptxxId())
                                    .build(),
                            getToken()
                    );

            if (CollUtil.isEmpty(resultList)) {
                return ResponseResultOuterUtils.createErrorResult("500", "未查找到相关的信息");
            }

            SzhtxxElectronicInvoiceFptxxListItemVo szhtxxElectronicInvoiceFptxxListItemVo = resultList.get(0);

            if (ObjectUtil.isNotNull(szhtxxElectronicInvoiceFptxxListItemVo.getFlagVoidSign())
                    && szhtxxElectronicInvoiceFptxxListItemVo.getFlagVoidSign().compareTo(InvoiceApplyBecomeInvalidEnum.未作废.getKey()) != 0) {
                return ResponseResultOuterUtils.createErrorResult("500", "该发票已作废，请检查");
            }

            /**************************************** 检查   end ****************************************/

            /**************************************** 业务 start ****************************************/

            SysNotify sysNotify = szhtxxPaperInvoiceFptxxDubboConsumerService
                    .opBecomeInvalidResponse(
                            form,
                            szhtxxElectronicInvoiceFptxxListItemVo,
                            invoiceApplyEntity,
                            getToken()
                    );

            // .equals(SzhtxxElectronicInvoiceResponseCodeEnum.SUCCESS.getReturnCode())
            if ("6011".equals(sysNotify.getTitle())) {
//                return ResponseResultOuterUtils.createSuccesResult(sysNotify.getContent());
                return ResponseResultOuterUtils.createSuccesResult("该发票已作废");
            } else {
                return ResponseResultOuterUtils.createErrorResult("500", sysNotify.getContent());
            }

            /**************************************** 业务   end ****************************************/


        } catch (Exception e) {

            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();

            return ResponseResultOuterUtils.createErrorResult("500", ResponseMsgConstants.ERROR_UNKNOW);
        }
    }

    /**************************************** op作废发票   end ****************************************/

    /**************************************** 金税盘页面-发起-op发票作废状态修复(纸质专用发票)(深圳航信纸质票)   end ****************************************/

    @ApiOperation(value = "op发票作废状态修复-Request(纸质专用发票)(深圳航信纸质票)")
    @RequestMapping(value = "/opBecomeInvalid/fake/request", method = RequestMethod.POST)
    @ResponseBody
    public R<SzhtxxPaperInvoiceFptxxOpBecomeInvalidFakeVo> opBecomeInvalidFakeRequest(
            @RequestBody SzhtxxInvoiceFptxxOpBecomeInvalidForm form) {

        R<SzhtxxPaperInvoiceFptxxOpBecomeInvalidFakeVo> result = new R<>();

        form.setFormState(CommonFormStateEnum.预览);

        try {


            /******************************************** 初始化&检查 start ********************************************/

            InvoiceApply invoiceApplyEntity = invoiceApplyCustomerDubboConsumerService.findByInvoiceApplySn(form.getFpqqlsh(), getToken());

            // ·该开票申请存在，若不存在，1.5s提示“该开票申请不存在”；
            if (ObjectUtil.isNull(invoiceApplyEntity)) {
                return ResponseResultOuterUtils.createErrorResult("500", "该开票申请不存在");
            }

            // ·该开票申请的申请状态为“待领票/待寄票/待回款”，若非“待领票/待寄票/待回款”，1.5s提示“该开票申请状态发生变更，请返回检查”；
            if (!ArrayUtil.contains(
                    InvoiceApplyCustomerScene.statusV2_1_Array_InvoiceProgressingSceneStateCheck(),
                    invoiceApplyEntity.getInvoiceStatus())
            ) {
                return ResponseResultOuterUtils.createErrorResult("500", "该开票申请状态发生变更，请返回检查");
            }


            // ·该发票的回款状态为“未回款”，若非“未回款”，1.5s提示“只允许作废未回款的发票，已回款的发票请线下处理”；
            Integer[] backAccountStatusContains = {
                    InvoiceApplyCustomerBackAccountStatusEnum.未回款.getKey()
            };

            if (!ArrayUtil.contains(
                    backAccountStatusContains,
                    invoiceApplyEntity.getBackAccountStatus())
            ) {
                return ResponseResultOuterUtils.createErrorResult("500", "只允许作废未回款的发票，已回款的发票请线下处理");
            }

            List<SzhtxxElectronicInvoiceFptxxListItemVo> resultList = szhtxxElectronicInvoiceFptxxDubboConsumerService
                    .queryGenericParam(
                            SzhtxxElectronicInvoiceFptxxQueryCriteria.builder()
                                    .szhtxxElectronicInvoiceFptxxId(form.getSzhtxxElectronicInvoiceFptxxId())
                                    .build(),
                            getToken()
                    );

            if (CollUtil.isEmpty(resultList)) {
                return ResponseResultOuterUtils.createErrorResult("500", "该开票申请不存在");
            }

            SzhtxxElectronicInvoiceFptxxListItemVo szhtxxElectronicInvoiceFptxxListItemVo = resultList.get(0);

            // ·该发票的作废状态为“未作废”，若已作废，1.5s提示“该发票已作废，请检查”；
            Integer[] invoiceApplyBecomeInvalidContains = {
                    InvoiceApplyBecomeInvalidEnum.未作废.getKey()
            };
            if (!ArrayUtil.contains(
                    invoiceApplyBecomeInvalidContains,
                    szhtxxElectronicInvoiceFptxxListItemVo.getFlagVoidSign())
            ) {
                return ResponseResultOuterUtils.createErrorResult("500", "该发票已作废，请检查");
            }

            /******************************************** 初始化&检查   end ********************************************/

            /**************************************** 蓝票主体 start ****************************************/

            SzhtxxElectronicInvoiceFptxxDetailVo fptxxOrigin = szhtxxElectronicInvoiceFptxxDubboConsumerService
                    .detailGenericParamSzhtxxElectronicInvoiceFptxx(
                            SzhtxxElectronicInvoiceFptxxQueryCriteria
                                    .builder()
                                    .fpqqlsh(form.getFpqqlsh())
                                    .szhtxxElectronicInvoiceFptxxId(form.getSzhtxxElectronicInvoiceFptxxId())
                                    .build(),
                            getToken()
                    );

            /**************************************** 蓝票主体 start ****************************************/

            /******************************************** 给前端 start ********************************************/

            SID5SelectBillInfoRequestBody sid5SelectBillInfoRequestBody = szhtxxPaperInvoiceFptxxDubboConsumerService
                    .opBecomeInvalidFakeRequest(
                            form,
                            szhtxxElectronicInvoiceFptxxListItemVo,
                            invoiceApplyEntity,
                            getToken()
                    );

            /******************************************** 给前端   end ********************************************/

            result.setSuccess(true);
            result.setData(SzhtxxPaperInvoiceFptxxOpBecomeInvalidFakeVo
                    .builder()
                    .fptxxOrigin(fptxxOrigin)
                    .selectFttxxBecomeInvalid(sid5SelectBillInfoRequestBody.packing())
                    .build());

        } catch (Exception e) {

            result.setSuccess(false);
            result.setMessage(ResponseMsgConstants.ERROR_UNKNOW);
            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();
        }

        return result;
    }

    @ApiOperation(value = "op发票作废状态修复-Response(纸质专用发票)(深圳航信纸质票)")
    @RequestMapping(value = "/opBecomeInvalid/fake/response", method = RequestMethod.POST)
    @ResponseBody
    public R<String> opBecomeInvalidFakeResponse(@RequestBody SzhtxxInvoiceFptxxOpBecomeInvalidForm form) {

        form.setFormState(CommonFormStateEnum.提交);

        try {

            /******************************************** 初始化&检查 start ********************************************/

            InvoiceApply invoiceApplyEntity = invoiceApplyCustomerDubboConsumerService.findByInvoiceApplySn(form.getFpqqlsh(), getToken());

            // ·该开票申请存在，若不存在，1.5s提示“该开票申请不存在”；
            if (ObjectUtil.isNull(invoiceApplyEntity)) {
                return ResponseResultOuterUtils.createErrorResult("500", "该开票申请不存在");
            }

            // ·该开票申请的申请状态为“待领票/待寄票/待回款”，若非“待领票/待寄票/待回款”，1.5s提示“该开票申请状态发生变更，请返回检查”；
            if (!ArrayUtil.contains(
                    InvoiceApplyCustomerScene.statusV2_1_Array_InvoiceProgressingSceneStateCheck(),
                    invoiceApplyEntity.getInvoiceStatus())
            ) {
                return ResponseResultOuterUtils.createErrorResult("500", "该开票申请状态发生变更，请返回检查");
            }


            // ·该发票的回款状态为“未回款”，若非“未回款”，1.5s提示“只允许作废未回款的发票，已回款的发票请线下处理”；
            Integer[] backAccountStatusContains = {
                    InvoiceApplyCustomerBackAccountStatusEnum.未回款.getKey()
            };

            if (!ArrayUtil.contains(
                    backAccountStatusContains,
                    invoiceApplyEntity.getBackAccountStatus())
            ) {
                return ResponseResultOuterUtils.createErrorResult("500", "只允许作废未回款的发票，已回款的发票请线下处理");
            }

            List<SzhtxxElectronicInvoiceFptxxListItemVo> resultList = szhtxxElectronicInvoiceFptxxDubboConsumerService
                    .queryGenericParam(
                            SzhtxxElectronicInvoiceFptxxQueryCriteria.builder()
                                    .szhtxxElectronicInvoiceFptxxId(form.getSzhtxxElectronicInvoiceFptxxId())
                                    .build(),
                            getToken()
                    );

            if (CollUtil.isEmpty(resultList)) {
                return ResponseResultOuterUtils.createErrorResult("500", "该开票申请不存在");
            }

            SzhtxxElectronicInvoiceFptxxListItemVo szhtxxElectronicInvoiceFptxxListItemVo = resultList.get(0);

            // ·该发票的作废状态为“未作废”，若已作废，1.5s提示“该发票已作废，请检查”；
            Integer[] invoiceApplyBecomeInvalidContains = {
                    InvoiceApplyBecomeInvalidEnum.未作废.getKey()
            };
            if (!ArrayUtil.contains(
                    invoiceApplyBecomeInvalidContains,
                    szhtxxElectronicInvoiceFptxxListItemVo.getFlagVoidSign())
            ) {
                return ResponseResultOuterUtils.createErrorResult("500", "该发票已作废，请检查");
            }

            /******************************************** 初始化&检查   end ********************************************/

            /**************************************** 业务 start ****************************************/

            SysNotify sysNotify = szhtxxPaperInvoiceFptxxDubboConsumerService
                    .opBecomeInvalidFakeResponse(
                            form,
                            szhtxxElectronicInvoiceFptxxListItemVo,
                            invoiceApplyEntity,
                            getToken()
                    );

            // .equals(SzhtxxElectronicInvoiceResponseCodeEnum.SUCCESS.getReturnCode())
            if ("0".equals(sysNotify.getTitle())) {
//                return ResponseResultOuterUtils.createSuccesResult(sysNotify.getContent());
                return ResponseResultOuterUtils.createSuccesResult("该发票已作废");
            } else {
                return ResponseResultOuterUtils.createErrorResult(sysNotify.getTitle(), sysNotify.getContent());
            }

            /**************************************** 业务   end ****************************************/


        } catch (Exception e) {

            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();

            return ResponseResultOuterUtils.createErrorResult("500", ResponseMsgConstants.ERROR_UNKNOW);
        }
    }

    /**************************************** 金税盘页面-发起-op发票作废状态修复(纸质专用发票)(深圳航信纸质票)   end ****************************************/

    /**************************************** op金税盘信息查询 start ****************************************/

    @ApiOperation(value = "op金税盘信息查询Request(调用深圳航信纸质票-SID-121_金税盘信息查询接口)", notes = "v2.2")
    @RequestMapping(value = "/opCallSzhtxxPaperInvoiceApiTaxInfo/request", method = RequestMethod.POST)
    @ResponseBody
    public R<SID121TaxInfoRequestBody> opCallSzhtxxPaperInvoiceApiTaxInfoRequest(@RequestBody SzhtxxPaperInvoiceOpTaxInfoForm form) {

        form.setFormState(CommonFormStateEnum.预览);

        return ResponseResultOuterUtils.createSuccesResult(
                "",
                szhtxxPaperInvoiceFptxxDubboConsumerService.opCallTaxInfoApiRequest(form, getToken())
        );


    }

    @ApiOperation(value = "op金税盘信息查询Response(调用深圳航信纸质票-SID-121_金税盘信息查询接口)", notes = "v2.2")
    @RequestMapping(value = "/opCallSzhtxxPaperInvoiceApiTaxInfo/response", method = RequestMethod.POST)
    @ResponseBody
    public R<String> opCallSzhtxxPaperInvoiceApiTaxInfoResponse(@RequestBody SzhtxxPaperInvoiceOpTaxInfoForm form) {

        form.setFormState(CommonFormStateEnum.提交);

        SysNotify sysNotify = szhtxxPaperInvoiceFptxxDubboConsumerService.opCallTaxInfoApiResponse(form, getToken());

//        SzhtxxPaperInvoiceResponseCodeEnum.SUCCESS.getReturnCode()
//        返回码 0 表示成功获取
//        其它表示非成功获取
        if ("0".equals(sysNotify.getTitle())) {
            return ResponseResultOuterUtils.createSuccesResult(sysNotify.getContent());
        } else {
            return ResponseResultOuterUtils.createErrorResult("500", sysNotify.getContent());
        }

    }

    /**************************************** op金税盘信息查询   end ****************************************/

    /**************************************** op金税盘设备查询 start ****************************************/

    @ApiOperation(value = "op金税盘设备查询Request(调用深圳航信纸质票-SID-25_金税盘设备查询接口)", notes = "v2.2")
    @RequestMapping(value = "/opCallSzhtxxPaperInvoiceApiTaxDeviceInfo/request", method = RequestMethod.POST)
    @ResponseBody
    public R<SID25TaxDeviceInfoRequestBody> opCallSzhtxxPaperInvoiceApiTaxDeviceInfoRequest(@RequestBody SzhtxxPaperInvoiceOpTaxDeviceInfoForm form) {

        form.setFormState(CommonFormStateEnum.预览);

        SID25TaxDeviceInfoRequestBody sid25TaxDeviceInfoRequestBody = szhtxxPaperInvoiceFptxxDubboConsumerService.opCallTaxDeviceInfoApiRequest(form, getToken());

        sid25TaxDeviceInfoRequestBody.packing();

        return ResponseResultOuterUtils.createSuccesResult("", sid25TaxDeviceInfoRequestBody);


    }

    @ApiOperation(value = "op金税盘设备查询Response(调用深圳航信纸质票-SID-25_金税盘设备查询接口)", notes = "v2.2")
    @RequestMapping(value = "/opCallSzhtxxPaperInvoiceApiTaxDeviceInfo/response", method = RequestMethod.POST)
    @ResponseBody
    public R<SID25TaxDeviceInfoResponseBody> opCallSzhtxxPaperInvoiceApiTaxDeviceInfoResponse(@RequestBody SzhtxxPaperInvoiceOpTaxDeviceInfoForm form) {

        form.setFormState(CommonFormStateEnum.提交);

        SID25TaxDeviceInfoResponseBody sid25TaxDeviceInfoResponseBody = szhtxxPaperInvoiceFptxxDubboConsumerService.opCallTaxDeviceInfoApiResponse(form, getToken());

////        SzhtxxPaperInvoiceResponseCodeEnum.SUCCESS.getReturnCode()
////        返回码 0 表示成功获取
////        其它表示非成功获取
//        if ("0".equals(sysNotify.getTitle())) {
//            return ResponseResultOuterUtils.createSuccesResult(sysNotify.getContent());
//        } else {
//            return ResponseResultOuterUtils.createErrorResult("500", sysNotify.getContent());
//        }

        return ResponseResultOuterUtils.createSuccesResult("", sid25TaxDeviceInfoResponseBody);

    }

    /**************************************** op金税盘设备查询   end ****************************************/


}
