package com.jianshui.api.controller.http.invoice.v1;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.jianshui.common.core.domain.AjaxResult;
import com.jianshui.common.core.domain.entity.Companyservice;
import com.jianshui.common.enums.ErrorCode;
import com.jianshui.common.exception.jianshui.JianshuiParamErrorException;
import com.jianshui.common.exception.jianshui.JianshuiServiceException;
import com.jianshui.common.utils.StringUtils;
import com.jianshui.common.utils.http.HttpHelper;
import com.jianshui.common.utils.uuid.IdUtils;
import com.jianshui.framework.manager.AsyncManager;
import com.jianshui.invoice.constant.elephant.ElephantConstants;
import com.jianshui.invoice.domain.*;
import com.jianshui.invoice.domain.ele.EleNewMessage;
import com.jianshui.invoice.domain.ele.EleOuterMessage;
import com.jianshui.invoice.domain.ele.FindRedInfoDTO;
import com.jianshui.invoice.domain.ele.HZSQDMessage;
import com.jianshui.invoice.mapper.InvoiceBackMapper;
import com.jianshui.invoice.mapper.InvoiceFileMapper;
import com.jianshui.invoice.task.AddInvoiceBatchTask;
import com.jianshui.invoice.task.InvoiceBackTask;
import com.jianshui.invoice.utils.elephant.ElephantUtils;
import com.jianshui.platform.dto.InvoiceAdd.BillInfoPDTO;
import com.jianshui.invoice.domain.dto.*;
import com.jianshui.invoice.domain.dto.api.*;
import com.jianshui.invoice.service.IInvoiceApiService;
import com.jianshui.invoice.service.IInvoiceRequestService;
import com.jianshui.invoice.factory.IInvoiceRequestFactory;
import com.jianshui.invoice.factory.IInvoiceResponseFactory;
import com.jianshui.invoice.factory.IInvoiceServiceFactory;
import com.jianshui.invoice.service.IInvoiceResponseService;
import com.jianshui.invoice.utils.ApiRequestUtils;
import com.jianshui.platform.service.InvoiceAddPService;
import com.jianshui.system.service.ICompanyserviceService;
import com.jianshui.system.service.IServiceManageService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * @Description
 * @Author 巩权林
 * @Date 2022/3/26 10:11
 **/
//@Api(tags = "A简税升级-销项接口V1.0")
@RestController
// @RequestMapping("/api/invoice/v1/invoice") // 为了兼容老版本航信接口
@Slf4j
public class InvoiceController {

    @Autowired
    private ICompanyserviceService iCompanyserviceService;

    @Autowired
    private IServiceManageService serviceManageService;

    @Autowired
    private IInvoiceRequestFactory invoiceRequestFactory;

    @Autowired
    private IInvoiceResponseFactory invoiceResponseFactory;

    @Autowired
    private IInvoiceServiceFactory invoiceServiceFactory;

    @Autowired
    private InvoiceAddPService addService;

    @Autowired
    private InvoiceBackMapper invoiceBackMapper;

    @Autowired
    private InvoiceFileMapper invoiceFileMapper;


    /**
     * 功能描述: 红字查询接口
     *
     * @param identity
     * @return : com.jianshui.invoice.domain.dto.HXResponse
     */
    @PostMapping({"/api/invoice/v1/findRedInfo", "/api/invoice/v1/findRedInfo/{identity}", "/invoice/findRedInfo"})
    public EleNewMessage findRedInfo(HttpServletRequest request, String identity) throws Exception {
        log.info("开始执行");
        if (StringUtils.isEmpty(identity)) {
            EleNewMessage eleNewMessage = new EleNewMessage();
            eleNewMessage.setStatus("9999");
            eleNewMessage.setMessage("身份认证参数为空");
            return eleNewMessage;
        }
        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            EleNewMessage eleNewMessage = new EleNewMessage();
            eleNewMessage.setStatus("9999");
            eleNewMessage.setMessage("企业信息不存在");
            return eleNewMessage;
        }
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "find_redinfo");
        // 获得请求实例，并且进行扣费
        log.info("解密数据{}", decryptResult);
        FindRedInfoDTO findRedInfoDTO = decryptResult.toJavaObject(FindRedInfoDTO.class);
        FindRedInfo findRedInfo = new FindRedInfo();
        findRedInfo.setSQBQQPCH(findRedInfoDTO.getBillNo());
        findRedInfo.setNSRSBH(findRedInfoDTO.getNSRSBH());
        log.info("转换数据{}", JSONUtil.toJsonStr(findRedInfo));
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());
        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        EleNewMessage result = invoiceService.findRedInfo(findRedInfo, companyservice);
        return result;
    }


    /**
     * API回调接口
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping({"/api/invoice/v1/callback", "/api/invoice/v1/callback/{service}"})
    public Object callback(HttpServletRequest request, String service) throws Exception {
        log.info("【销项控制器】 收到api回调请求：{}", HttpHelper.getBodyString(request));
        if (StringUtils.isEmpty(service)) {
            service = "elephant";
        }
        service = service + "_invoice";
        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(service);
        return invoiceService.addCallback(request);
    }


    /**
     * 开票回调
     *
     * @param request
     * @param service
     * @return
     * @throws Exception
     */
    @RequestMapping({"/api/invoice/v1/callback/add", "/api/invoice/v1/callback/add/{service}"})
    public Object addCallback(HttpServletRequest request, String service) throws Exception {
        log.info("【销项控制器】 收到api回调请求：{}", HttpHelper.getBodyString(request));
        if (StringUtils.isEmpty(service)) {
            service = "elephant";
        }
        service = service + "_invoice";
        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(service);
        return invoiceService.decryptCallback(request);
    }

    /**
     * API网关形式的请求
     *
     * @param request
     * @param identity
     * @return
     * @throws Exception
     */
//    @ApiOperation("API网关")
    @PostMapping({"/api/invoice/v1/gateway", "/api/invoice/v1/gateway/{identity}"})
    public Object gateway(HttpServletRequest request, String identity) throws Exception {
        // 如果identity是空的，就去看下header头里有没有
        if (StringUtils.isEmpty(identity)) {
            identity = ApiRequestUtils.getIdentityByHeader(request);
            if (StringUtils.isEmpty(identity)) {
                return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
            }
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获取请求名
        String apiMethod = ApiRequestUtils.getApiMethods(request);
        if (StringUtils.isEmpty(apiMethod)) {
            return AjaxResult.error(ErrorCode.METHOD_NOT_EXISTS);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        apiMethod = invoiceRequestService.covertMethodName(apiMethod);
        if (StringUtils.isEmpty(apiMethod)) {
            return AjaxResult.error(ErrorCode.METHOD_NOT_EXISTS);
        }

        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, apiMethod);
        HXResponse result = preHandleDecryptResult(decryptResult, apiMethod, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, apiMethod);
    }

    /**
     * 开票请求接口-（蓝字发票开具，红字发票开具）
     *
     * @param request
     * @param identity
     * @return
     * @throws Exception
     */
    @Validated
    @ApiOperation("开票请求接口-（蓝字发票开具，红字发票开具）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/add", "/api/invoice/v1/add/{identity}", "/invoice/addInvoice"})
    public Object addInvoice(HttpServletRequest request, String identity) throws Exception {
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "add");

        // 报文处理
        BillInfo billInfo = decryptResult.toJavaObject(BillInfo.class);
        if (billInfo == null) {
            throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
        }

        billInfo.setSource("2");
        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());
        billInfo.setServiceSupplierKey(serviceKey);
        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        HXResponse result = invoiceService.addInvoice(billInfo, companyservice);

//        HXResponse result = preHandleDecryptResult(decryptResult, "add", companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "add");
    }


    /**
     * 开票查询接口（发票请求流水号）
     */
    @Validated
//    @ApiOperation("开票查询接口（发票请求流水号）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/query", "/api/invoice/v1/query/{identity}", "/invoice/queryInvoice"})
    public Object queryInvoice(HttpServletRequest request, String identity) throws Exception {


        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "query");

        HXResponse result = preHandleDecryptResult(decryptResult, "query_fpqqlsh", companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "query");
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @GetMapping("/invoice/fileUrl/{id}")
    public void fileUrl(@PathVariable String id, HttpServletResponse response) throws Exception {

        if (StrUtil.isEmpty(id)) {
            return;
        }
        InvoiceFile invoiceFile = invoiceFileMapper.selectInvoiceFileById(id);
        if (BeanUtil.isEmpty(invoiceFile) || StrUtil.isEmpty(invoiceFile.getFileContent())) {
            return;
        }
        String fileName = invoiceFile.getFileName();
        String content = invoiceFile.getFileContent();

        try {
            byte[] buffer = Base64Decoder.decode(content);  // 将文件流字符串转换为字节数组
            response.reset();
            response.setCharacterEncoding("UTF-8");
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".pdf");
            response.addHeader("Content-Length", "" + buffer.length);
            response.setContentType("application/pdf");
            OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
            outputStream.write(buffer);
            outputStream.flush();
            outputStream.close();  // 关闭输出流
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }


    /**
     * 开票查询接口（订单号）
     */
    @Validated
//    @ApiOperation("开票查询接口（订单号）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/query_orderno", "/api/invoice/v1/query_orderno/{identity}", "/invoice/queryInvoiceByOrderNo"})
    public Object queryInvoiceOrderno(HttpServletRequest request, String identity) throws Exception {
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "query");

        HXResponse result = preHandleDecryptResult(decryptResult, "query_orderno", companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "query");
    }


    /**
     * 开票查询接口（范围查询）
     */
    @Validated
//    @ApiOperation("开票查询接口（范围查询）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/query_scope", "/api/invoice/v1/query_scope/{identity}", "/urlcode/queryInvoice"})
    public Object queryInvoiceScope(HttpServletRequest request, String identity) throws Exception {
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "query");

        HXResponse result = preHandleDecryptResult(decryptResult, "query_scope", companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "query");
    }


    /**
     * 作废发票
     */
    @Validated
//    @ApiOperation("作废发票")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/deprecate", "/api/invoice/v1/deprecate/{identity}", "/invoice/deleteInvoice"})
    public Object deprecateInvoice(HttpServletRequest request, String identity) throws Exception {
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "deprecate");

//        HXResponse result = preHandleDecryptResult(decryptResult, "deprecate", companyservice);
        // 调用预处理
        if (decryptResult == null) {
            throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
        }
        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());
        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        decryptResult.put("service_supplier_key", serviceKey);
        HXResponse result = invoiceService.deprecateInvoice(decryptResult, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "deprecate");
    }

    /**
     * 作废发票结果查询
     */
    @Validated
//    @ApiOperation("作废发票结果查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/deprecate_query", "/api/invoice/v1/deprecate_query/{identity}", "/invoice/deleteInvoiceQuery", "/invoice/deprecate_query"})
    public Object deprecateQueryInvoice(HttpServletRequest request, String identity) throws Exception {
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "deprecate_query");

//        HXResponse result = preHandleDecryptResult(decryptResult, "deprecate_query", companyservice);
        if (decryptResult == null) {
            throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
        }
        DeprecateQueryInvoiceDTO deprecateQueryInvoiceDTO = decryptResult.toJavaObject(DeprecateQueryInvoiceDTO.class);
        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());
        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        HXResponse result = invoiceService.deprecateQueryInvoice(deprecateQueryInvoiceDTO, companyservice);


        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "deprecate_query");
    }


    /**
     * 红字信息表上传
     */
    @Validated
//    @ApiOperation("红字信息表上传")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/upload_redinfo", "/api/invoice/v1/upload_redinfo/{identity}", "/invoice/redInvoiceApply"})
    public Object uploadRedInfo(HttpServletRequest request, String identity) throws Exception {
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "add_redinfo");

//        HXResponse result = preHandleDecryptResult(decryptResult, "add_redinfo", companyservice);

        Redinfo billInfo = decryptResult.toJavaObject(Redinfo.class);
        if (billInfo == null) {
            throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
        }
        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());
        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        billInfo.setServiceSupplierKey(serviceKey);
        HXResponse result = invoiceService.uploadRedInfo(billInfo, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "add_redinfo");
    }


    /**
     * 红字信息表下载
     */
    @Validated
//    @ApiOperation("红字信息表下载")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/download_redinfo", "/api/invoice/v1/download_redinfo/{identity}", "/invoice/redInvoiceDownload"})
    public Object downloadRedInfo(HttpServletRequest request, String identity) throws Exception {
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "download_redinfo");

        DownloadRedInfoDTO billInfo = decryptResult.toJavaObject(DownloadRedInfoDTO.class);
        if (billInfo == null) {
            throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
        }

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());
        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        billInfo.setServiceKey(serviceKey);
        HXResponse result = invoiceService.downloadRedInfo(billInfo, companyservice);

//        HXResponse result = preHandleDecryptResult(decryptResult, "download_redinfo", companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "download_redinfo");
    }

    /**
     * 发票推送接口
     */
    @Validated
//    @ApiOperation("发票推送接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/push", "/api/invoice/v1/push/{identity}"})
    public Object push(HttpServletRequest request, String identity) throws Exception {
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "push");

//        HXResponse result = preHandleDecryptResult(decryptResult, "push", companyservice);

        // 接口预处理
        BillInfo billInfo = decryptResult.toJavaObject(BillInfo.class);
        if (billInfo == null) {
            throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
        }
        billInfo.setSource("2");
        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());
        billInfo.setServiceSupplierKey(serviceKey);
        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        HXResponse result = invoiceService.pushData(billInfo, companyservice);


        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "push");
    }


    /**
     * 发票批量打印接口
     */
    @Validated
//    @ApiOperation("发票批量打印接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/batch_print", "/api/invoice/v1/batch_print/{identity}", "/invoice/batch_print"})
    public Object print(HttpServletRequest request, String identity) throws Exception {
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = new JSONObject();
        boolean useAisino = false;
        try {
            decryptResult = invoiceRequestService.decrypt(request, companyservice, "batch_print");
        } catch (JianshuiParamErrorException e) {
            // 为了处理高达那种情况，如果这里解密失败，就直接调用航信1.0接口解密
            invoiceRequestService = invoiceRequestFactory.getService("jn_aisino_invoice_request_adapter");
            decryptResult = invoiceRequestService.decrypt(request, companyservice, "batch_print");
            useAisino = true;
        }

//        HXResponse result = preHandleDecryptResult(decryptResult, "batch_print", companyservice);
        PrintDTO billInfo = decryptResult.toJavaObject(PrintDTO.class);
        if (billInfo == null) {
            throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
        }
        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());
        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        HXResponse result = invoiceService.batchPrint(billInfo, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = null;
        if (useAisino) {
            invoiceResponseService = invoiceResponseFactory.getService("jn_aisino_invoice_response_adapter");
        } else {
            invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);
        }

        return invoiceResponseService.response(result, companyservice, "batch_print");
    }

    /**
     * 打印机查询接口
     */
    @Validated
//    @ApiOperation("打印机查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/printer_list", "/api/invoice/v1/printer_list/{identity}", "/invoice/printer_list"})
    public Object printer(HttpServletRequest request, String identity) throws Exception {
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = new JSONObject();
        boolean useAisino = false;
        try {
            decryptResult = invoiceRequestService.decrypt(request, companyservice, "printer");
        } catch (JianshuiParamErrorException e) {
            // 为了处理高达那种情况，如果这里解密失败，就直接调用航信1.0接口解密
            invoiceRequestService = invoiceRequestFactory.getService("jn_aisino_invoice_request_adapter");
            decryptResult = invoiceRequestService.decrypt(request, companyservice, "printer");
            useAisino = true;
        }

//        HXResponse result = preHandleDecryptResult(decryptResult, "printer", companyservice);
        QueryPrinterDTO billInfo = decryptResult.toJavaObject(QueryPrinterDTO.class);
        if (billInfo == null) {
            throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
        }

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        HXResponse result = invoiceService.queryPrinter(billInfo, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = null;
        if (useAisino) {
            invoiceResponseService = invoiceResponseFactory.getService("jn_aisino_invoice_response_adapter");
        } else {
            invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);
        }

        return invoiceResponseService.response(result, companyservice, "printer");
    }

    /**
     * 发票签章
     */
    @Validated
//    @ApiOperation("发票签章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/sign_e_seal", "/api/invoice/v1/sign_e_seal/{identity}"})
    public Object signESeal(HttpServletRequest request, String identity) throws Exception {
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "sign_e_seal");

        HXResponse result = preHandleDecryptResult(decryptResult, "sign_e_seal", companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "sign_e_seal");
    }

    /**
     * 发票专用章导入注册
     */
    @Validated
//    @ApiOperation("发票专用章导入注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/register_e_seal", "/api/invoice/v1/register_e_seal/{identity}"})
    public Object registerESeal(HttpServletRequest request, String identity) throws Exception {
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "register_e_seal");

        HXResponse result = preHandleDecryptResult(decryptResult, "register_e_seal", companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "register_e_seal");
    }

    /**
     * 发票专用章图片生成
     */
    @Validated
//    @ApiOperation("发票专用章图片生成")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/create_e_seal", "/api/invoice/v1/create_e_seal/{identity}"})
    public Object createESeal(HttpServletRequest request, String identity) throws Exception {
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "create_e_seal");

        HXResponse result = preHandleDecryptResult(decryptResult, "create_e_seal", companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "create_e_seal");
    }


    /**
     * 统一处理报文请求
     *
     * @param decryptResult
     * @param apiMethod
     * @param companyservice
     * @return
     * @throws Exception
     */
    public HXResponse preHandleDecryptResult(JSONObject decryptResult, String apiMethod, Companyservice companyservice) throws Exception {
        // 发票开具
        if (StringUtils.equals(apiMethod, "add")) {
            BillInfo billInfo = decryptResult.toJavaObject(BillInfo.class);
            if (billInfo == null) {
                throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
            }

            billInfo.setSource("2");
            // 获得请求实例，并且进行扣费
            String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
            serviceManageService.companyConsume("invoice", companyservice.getCompanyid());
            billInfo.setServiceSupplierKey(serviceKey);
            IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
            HXResponse result = invoiceService.addInvoice(billInfo, companyservice);
            return result;
        }

        /**
         * 批量查询
         */
        if (StringUtils.equals(apiMethod, "query")) {

            BillInfo billInfo = decryptResult.toJavaObject(BillInfo.class);
            if (billInfo == null) {
                throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
            }

            // 获得请求实例，并且进行扣费
            String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
            serviceManageService.companyConsume("invoice", companyservice.getCompanyid());
            billInfo.setServiceSupplierKey(serviceKey);
            IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
            HXResponse result = invoiceService.batchQueryInvoices(billInfo, "fpqqlsh", companyservice);
            return result;
        }

        // 发票查询 发票流水号
        if (StringUtils.equals(apiMethod, "query_fpqqlsh")) {

            BillInfo billInfo = decryptResult.toJavaObject(BillInfo.class);
            if (billInfo == null) {
                throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
            }

            // 获得请求实例，并且进行扣费
            String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
            serviceManageService.companyConsume("invoice", companyservice.getCompanyid());
            billInfo.setServiceSupplierKey(serviceKey);
            IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
            HXResponse result = invoiceService.batchQueryInvoices(billInfo, "fpqqlsh", companyservice);
            return result;
        }

        // 发票查询 发票订单号
        if (StringUtils.equals(apiMethod, "query_orderno")) {

            BillInfo billInfo = decryptResult.toJavaObject(BillInfo.class);
            if (billInfo == null) {
                throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
            }

            // 获得请求实例，并且进行扣费
            String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
            serviceManageService.companyConsume("invoice", companyservice.getCompanyid());
            billInfo.setServiceSupplierKey(serviceKey);
            IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
            HXResponse result = invoiceService.batchQueryInvoices(billInfo, "orderno", companyservice);
            return result;
        }

        // 发票查询 批量范围查询
        if (StringUtils.equals(apiMethod, "query_scope")) {

            if (decryptResult == null) {
                throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
            }

            // 获得请求实例，并且进行扣费
            String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
            serviceManageService.companyConsume("invoice", companyservice.getCompanyid());
            decryptResult.put("service_supplier_key", serviceKey);
            IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
            HXResponse result = invoiceService.scopeQueryInvoices(decryptResult, companyservice);
            return result;
        }

        // 发票作废
        if (StringUtils.equals(apiMethod, "deprecate")) {

            if (decryptResult == null) {
                throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
            }

            // 获得请求实例，并且进行扣费
            String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
            serviceManageService.companyConsume("invoice", companyservice.getCompanyid());
            IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
            decryptResult.put("service_supplier_key", serviceKey);
            HXResponse result = invoiceService.deprecateInvoice(decryptResult, companyservice);
            return result;
        }

        // 发票作废
        if (StringUtils.equals(apiMethod, "deprecate_query")) {

            if (decryptResult == null) {
                throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
            }

            DeprecateQueryInvoiceDTO deprecateQueryInvoiceDTO = decryptResult.toJavaObject(DeprecateQueryInvoiceDTO.class);

            // 获得请求实例，并且进行扣费
            String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
            serviceManageService.companyConsume("invoice", companyservice.getCompanyid());
            IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
            HXResponse result = invoiceService.deprecateQueryInvoice(deprecateQueryInvoiceDTO, companyservice);
            return result;
        }

        // 红字信息表上传
        if (StringUtils.equals(apiMethod, "add_redinfo")) {

            Redinfo billInfo = decryptResult.toJavaObject(Redinfo.class);
            if (billInfo == null) {
                throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
            }

            // 获得请求实例，并且进行扣费
            String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
            serviceManageService.companyConsume("invoice", companyservice.getCompanyid());
            IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
            billInfo.setServiceSupplierKey(serviceKey);
            HXResponse result = invoiceService.uploadRedInfo(billInfo, companyservice);
            return result;
        }

        // 红字信息表下载
        if (StringUtils.equals(apiMethod, "download_redinfo")) {

            DownloadRedInfoDTO billInfo = decryptResult.toJavaObject(DownloadRedInfoDTO.class);
            if (billInfo == null) {
                throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
            }

            // 获得请求实例，并且进行扣费
            String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
            serviceManageService.companyConsume("invoice", companyservice.getCompanyid());
            IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
            billInfo.setServiceKey(serviceKey);
            HXResponse result = invoiceService.downloadRedInfo(billInfo, companyservice);
            return result;
        }

        // 发票推送
        if (StringUtils.equals(apiMethod, "push")) {
            BillInfo billInfo = decryptResult.toJavaObject(BillInfo.class);
            if (billInfo == null) {
                throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
            }

            billInfo.setSource("2");
            // 获得请求实例，并且进行扣费
            String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
            serviceManageService.companyConsume("invoice", companyservice.getCompanyid());
            billInfo.setServiceSupplierKey(serviceKey);
            IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
            HXResponse result = invoiceService.pushData(billInfo, companyservice);
            return result;
        }

        // 发票打印
        if (StringUtils.equals(apiMethod, "batch_print")) {
            PrintDTO billInfo = decryptResult.toJavaObject(PrintDTO.class);
            if (billInfo == null) {
                throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
            }

            // 获得请求实例，并且进行扣费
            String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
            serviceManageService.companyConsume("invoice", companyservice.getCompanyid());
            IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
            HXResponse result = invoiceService.batchPrint(billInfo, companyservice);
            return result;
        }

        // 发票打印机查询
        if (StringUtils.equals(apiMethod, "printer")) {
            QueryPrinterDTO billInfo = decryptResult.toJavaObject(QueryPrinterDTO.class);
            if (billInfo == null) {
                throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
            }

            // 获得请求实例，并且进行扣费
            String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
            serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

            IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
            HXResponse result = invoiceService.queryPrinter(billInfo, companyservice);
            return result;
        }


        // 发票签章
        if (StringUtils.equals(apiMethod, "sign_e_seal")) {
            SignESealDTO billInfo = decryptResult.toJavaObject(SignESealDTO.class);
            if (billInfo == null) {
                throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
            }

            // 获得请求实例，并且进行扣费
            String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
            serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

            IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
            HXResponse result = invoiceService.signInvoice(billInfo, companyservice);
            return result;
        }

        // 发票专用章导入注册
        if (StringUtils.equals(apiMethod, "register_e_seal")) {
            RegisterESealDTO billInfo = decryptResult.toJavaObject(RegisterESealDTO.class);
            if (billInfo == null) {
                throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
            }

            // 获得请求实例，并且进行扣费
            String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
            serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

            IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
            HXResponse result = invoiceService.registerESeal(billInfo, companyservice);
            return result;
        }

        // 发票专用章图片生成
        if (StringUtils.equals(apiMethod, "create_e_seal")) {
            CreateESealDTO billInfo = decryptResult.toJavaObject(CreateESealDTO.class);
            if (billInfo == null) {
                throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
            }

            // 获得请求实例，并且进行扣费
            String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
            serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

            IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
            HXResponse result = invoiceService.createESeal(billInfo, companyservice);
            return result;
        }
        return null;
    }


    /**
     * @Author: kk
     * @Description: 通用异步查询接口
     * @DateTime: 2023/6/2 17:35
     * @Params:
     * @Return
     */

    /**
     * 通用异步查询接口
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/queryResultInfo/{identity}", "/api/invoice/v1/queryResultInfo"})
    public Object queryResultInfo(HttpServletRequest request, String identity) throws Exception {
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "");

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

        InvoiceAllQueryTWODTO invoiceAllYhdjDTO = decryptResult.toJavaObject(InvoiceAllQueryTWODTO.class);
        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        HXResponse result = invoiceService.queryResultInfo(invoiceAllYhdjDTO, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "");
    }


    /**
     * 获取发票下载地址
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/getFileUrl/{identity}", "/api/invoice/v1/getFileUrl"})
    public Object getFileUrl(HttpServletRequest request, String identity) throws Exception {
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "");

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

        InvoiceAllFileAcquisitionTWODTO invoiceAllFileAcquisitionTWODTO = decryptResult.toJavaObject(InvoiceAllFileAcquisitionTWODTO.class);
        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        HXResponse result = invoiceService.getFileUrl(invoiceAllFileAcquisitionTWODTO, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "");
    }


    /**
     * 全电文件获取
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/getFileAcquisition/{identity}", "/api/invoice/v1/getFileAcquisition"})
    public Object getFileAcquisition(HttpServletRequest request, String identity) throws Exception {

        // TODO: 2023/4/6 getFileAcquisitionOn
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "");

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

        InvoiceAllFileAcquisitionOnTWODTO invoiceAllFileAcquisitionDTO = decryptResult.toJavaObject(InvoiceAllFileAcquisitionOnTWODTO.class);
        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        HXResponse result = invoiceService.getFileAcquisitionOn(invoiceAllFileAcquisitionDTO, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "");


    }

    /**
     * 获取人脸识别二维码
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/qrcode/{identity}", "/api/invoice/v1/qrcode","/invoice/qrcode"})
    public Object qrcode(HttpServletRequest request, String identity) throws Exception {

        // qrcode
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "");

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        HXResponse result = invoiceService.qrcode(decryptResult, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "");

    }


    /**
     * 获取人脸识别结果
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/qrcodeResult/{identity}", "/api/invoice/v1/qrcodeResult","/invoice/qrcodeResult"})
    public Object qrcodeResult(HttpServletRequest request, String identity) throws Exception {

        // qrcode
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "");

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        HXResponse result = invoiceService.qrcodeResult(decryptResult, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "");

    }


    /**
     * 发票查询
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/query/{identity}", "/api/invoice/v1/query"})
    public Object query(HttpServletRequest request, String identity) throws Exception {
        // query

        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "");

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        InvoiceAllFileQueryTWODTO invoiceAllFileQueryTWODTO = BeanUtil.copyProperties(decryptResult, InvoiceAllFileQueryTWODTO.class);
        HXResponse result = invoiceService.query(invoiceAllFileQueryTWODTO, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "");

    }

    /**
     * 获取发票打印XML
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/print_xml/{identity}", "/api/invoice/v1/print_xml"})
    public Object printXmlml(HttpServletRequest request, String identity) throws Exception {

        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "");

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);

        InvoiceAllPrintXMLTWODTO invoiceAllPrintXMLTWODTO = BeanUtil.copyProperties(decryptResult, InvoiceAllPrintXMLTWODTO.class);
        HXResponse result = invoiceService.getPrintXML(invoiceAllPrintXMLTWODTO, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "");

    }

    /**
     * 快速红冲
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/issueFastRedInvoice/{identity}", "/api/invoice/v1/issueFastRedInvoice"})
    public Object issueFastRedInvoice(HttpServletRequest request, String identity) throws Exception {
//       issueFastRedInvoice

        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "");

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

        InvoiceAllKshcJsonDataTWODTO invoiceAllKshcJsonDataTWODTO = decryptResult.toJavaObject(InvoiceAllKshcJsonDataTWODTO.class);
        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        HXResponse result = invoiceService.issueFastRedInvoice(invoiceAllKshcJsonDataTWODTO, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "");

    }


    /**
     * 蓝字发票统计
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/invoice_statistics/{identity}", "/api/invoice/v1/invoice_statistics"})
    public Object invoiceStatistics(HttpServletRequest request, String identity) throws Exception {
//       invoiceStatic

        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "");

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        HXResponse result = invoiceService.invoiceStatic(decryptResult, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "");

    }

    /**
     * 审核红字信息表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/auditRedInfo/{identity}", "/api/invoice/v1/auditRedInfo"})
    public Object auditRedInfo(HttpServletRequest request, String identity) throws Exception {
//        auditRedInfo

        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "");

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

        InvoiceAllShhzJsonDataTWODTO invoiceAllShhzDTO = decryptResult.toJavaObject(InvoiceAllShhzJsonDataTWODTO.class);
        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        HXResponse result = invoiceService.auditRedInfo(invoiceAllShhzDTO, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "");

    }


    /**
     * 查询红字信息表明细
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/queryRedInfoDetail/{identity}", "/api/invoice/v1/queryRedInfoDetail"})
    public Object queryRedInfoDetail(HttpServletRequest request, String identity) throws Exception {
//        auditRedInfo

        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "");

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

        InvoiceAllRedInvoiceDetailTWODTO invoiceAllRedInvoiceDetailDTO = decryptResult.toJavaObject(InvoiceAllRedInvoiceDetailTWODTO.class);
        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        HXResponse result = invoiceService.queryRedInfoDetail(invoiceAllRedInvoiceDetailDTO, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "");

    }

    /**
     * 单据传入
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/invoiceIncome/{identity}", "/api/invoice/v1/invoiceIncome"})
    public Object invoiceIncome(HttpServletRequest request, String identity) throws Exception {

        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "");

        BillInfoPDTO billInfoPDTO = BeanUtil.copyProperties(decryptResult, BillInfoPDTO.class);
        HXResponse result = addService.invoiceIssuance(billInfoPDTO);

        return result;

    }

    /** 大象数电V6 */
    /**
     * 生成动态二维码接口 （扫码开票）
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/generateDynamicCode/{identity}", "/api/invoice/v1/generateDynamicCode"})
    public Object generateDynamicCode(HttpServletRequest request, String identity) throws Exception {
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }
        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }
        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "");

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        HXResponse result = invoiceService.generateDynamicCode(decryptResult, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "");

    }

    /**
     * 红字确认单列表查询（数电专用）
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/queryRedConfirmationList/{identity}", "/api/invoice/v1/queryRedConfirmationList","/invoice/queryRedConfirmationList"})
    public Object queryRedConfirmationList(HttpServletRequest request, String identity) throws Exception {
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }
        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }
        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "");

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        HXResponse result = invoiceService.queryRedConfirmationList(decryptResult, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "");

    }

    /**
     * 红字确认单明细查询(数电专用)
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/queryRedConfirmationItemList/{identity}", "/api/invoice/v1/queryRedConfirmationItemList","/invoice/queryRedConfirmationItemList"})
    public Object queryRedConfirmationItemList(HttpServletRequest request, String identity) throws Exception {
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }
        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }
        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "");

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        HXResponse result = invoiceService.queryRedConfirmationItemList(decryptResult, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "");

    }


/** 红字确认单申请(数电专用) */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/redConfirmationApply/{identity}", "/api/invoice/v1/redConfirmationApply","/invoice/redConfirmationApply"})
    public Object RedConfirmationApply(HttpServletRequest request, String identity) throws Exception {
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }
        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }
        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "");

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        HXResponse result = invoiceService.redConfirmationApply(decryptResult, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "");

    }

    /**
     * 红字确认单确认(数电专用)
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/redConfirmationConfirm/{identity}", "/api/invoice/v1/redConfirmationConfirm","/invoice/redConfirmationConfirm"})
    public Object redConfirmationConfirm(HttpServletRequest request, String identity) throws Exception {
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }
        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }
        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "");

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        HXResponse result = invoiceService.redConfirmationConfirm(decryptResult, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "");

    }


    /**
     * 数电登录接口(数电专用接口)
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/rpaLogin/{identity}", "/api/invoice/v1/rpaLogin","/invoice/rpaLogin"})
    public Object rpaLogin(HttpServletRequest request, String identity) throws Exception {
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }
        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }
        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "");

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        HXResponse result = invoiceService.rpaLogin(decryptResult, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "");

    }


    /**
     * 数电实名认证状态查询接口(数电专用接口)
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/getRpaAuthStatus/{identity}", "/api/invoice/v1/getRpaAuthStatus"})
    public Object getRpaAuthStatus(HttpServletRequest request, String identity) throws Exception {
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }
        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }
        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "");

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        HXResponse result = invoiceService.getRpaAuthStatus(decryptResult, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "");

    }


    /**
     * 发票平台电子税局账号查询接口(数电专用接口)
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/queryRpaTaxAccount/{identity}", "/api/invoice/v1/queryRpaTaxAccount"})
    public Object queryRpaTaxAccount(HttpServletRequest request, String identity) throws Exception {
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }
        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }
        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "");

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        HXResponse result = invoiceService.queryRpaTaxAccount(decryptResult, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "");

    }


    /**
     * 导出机动车加密数据(重汽)
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/exportVehicleInvoice/{identity}", "/api/invoice/v1/exportVehicleInvoice"})
    public Object exportVehicleInvoice(HttpServletRequest request, String identity) throws Exception {
        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }
        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }
        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "");

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        HXResponse result = invoiceService.exportVehicleInvoice(decryptResult, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "");

    }


    /**
     * 批量： 开票请求接口-（蓝字发票开具，红字发票开具）
     *
     * @param request
     * @param identity
     * @return
     * @throws Exception
     */
    @Validated
    @ApiOperation("开票请求接口-（蓝字发票开具，红字发票开具）-异步接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/addInvoiceBatch", "/api/invoice/v1/addInvoiceBatch/{identity}", "/invoice/addInvoiceBatch"})
    public void addInvoiceBatch(HttpServletRequest request, String identity) throws Exception {

        log.info("[重汽批量开票接口]{},请求开票接口！", identity);
        // 前置处理
        // 获取请求内容
        Map<String, String[]> map = request.getParameterMap();
        Set<String> key = map.keySet();
        Iterator<String> iterator = key.iterator();
        JSONObject rawJson = new JSONObject();
        while (iterator.hasNext()) {
            String k = iterator.next();
            rawJson.put(k, map.get(k)[0]);
        }
        if (StringUtils.isEmpty(rawJson)) {
            log.info("[重汽批量开票接口]未获取到请求参数！identity={},e={}", identity);
            return;
        }
        String order = rawJson.getString("order");

        // 异步任务
        AsyncManager.me().execute(new AddInvoiceBatchTask(iCompanyserviceService, serviceManageService
                , invoiceRequestFactory, invoiceServiceFactory, invoiceBackMapper, identity, order));

        log.info("[重汽批量开票接口]{},执行完成！", identity);

    }


    /**
     * 邮件交付
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", dataType = "java.lang.Void", required = true)})
    @PostMapping({"/api/invoice/v1/mailDelivery/{identity}", "/api/invoice/v1/mailDelivery","/invoice/mailDelivery"})
    public Object mailDelivery(HttpServletRequest request, String identity) throws Exception {

        if (StringUtils.isEmpty(identity)) {
            return AjaxResult.error(ErrorCode.EMPTY_IDENTITY);
        }

        Companyservice companyservice = iCompanyserviceService.selectCompanyserviceByIdentity(identity);
        if (companyservice == null) {
            return AjaxResult.error(ErrorCode.COMPANY_NOT_FOUND);
        }

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceRequestService invoiceRequestService = invoiceRequestFactory.getService(requestAdapterKey);
        JSONObject decryptResult = invoiceRequestService.decrypt(request, companyservice, "");

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("invoice", companyservice.getCompanyid());
        serviceManageService.companyConsume("invoice", companyservice.getCompanyid());

        InvoiceMailDeliveryDTO mailDeliveryDTO = decryptResult.toJavaObject(InvoiceMailDeliveryDTO.class);
        IInvoiceApiService invoiceService = invoiceServiceFactory.getService(serviceKey);
        HXResponse result = invoiceService.mailDelivery(mailDeliveryDTO, companyservice);

        String responseAdapterKey = serviceManageService.getResponseAdapterKey("invoice", companyservice.getCompanyid());
        IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);

        return invoiceResponseService.response(result, companyservice, "");

    }


}
