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

import com.alibaba.fastjson.JSONObject;
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.utils.StringUtils;
import com.jianshui.common.utils.http.HttpHelper;
import com.jianshui.invoice.domain.BillInfo;
import com.jianshui.invoice.domain.Redinfo;
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.invoiceall.domain.dto.*;
import com.jianshui.invoiceall.service.IInvoiceAll;
import com.jianshui.invoiceall.service.IInvoiceAllRequestService;
import com.jianshui.invoiceall.service.IInvoiceAllResponseService;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @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;


    /**
     * 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);
    }

    @Validated
    @ApiOperation("开票请求接口-（蓝字发票开具，红字发票开具）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "string", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", 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 = "string", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", 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");
    }

    @Validated
//    @ApiOperation("开票查询接口（订单号）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "string", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", 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 = "string", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", 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 = "string", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", 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);

        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 = "string", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", required = true)})
    @PostMapping({"/api/invoice/v1/deprecate_query", "/api/invoice/v1/deprecate_query/{identity}", "/invoice/deleteInvoiceQuery"})
    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);

        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 = "string", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", 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);

        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 = "string", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", 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");

        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 = "string", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", 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);

        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 = "string", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", required = true)})
    @PostMapping({"/api/invoice/v1/batch_print", "/api/invoice/v1/batch_print/{identity}"})
    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);

        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 = "string", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", required = true)})
    @PostMapping({"/api/invoice/v1/printer_list", "/api/invoice/v1/printer_list/{identity}"})
    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);

        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 = "string", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", 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 = "string", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", 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 = "string", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", 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;
    }


    // TODO: 2023/4/6  全电接口

//    @ApiOperation("获取发票下载地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "string", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", required = true)})
    @PostMapping({"/api/invoice_all/v2/getFileUrl/{identity}", "/api/invoice_all/v2/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, "");
    }


//    @ApiOperation("全电文件获取")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "string", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", required = true)})
    @PostMapping({"/api/invoice_all/v2/getFileAcquisition/{identity}", "/api/invoice_all/v2/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, "");


    }

//    @ApiOperation("获取人脸识别二维码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "string", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", required = true)})
    @PostMapping({"/api/invoice_all/v2/qrcode/{identity}", "/api/invoice_all/v2/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, "");

    }

//    @ApiOperation("发票查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "string", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", required = true)})
    @PostMapping({"/api/invoice_all/v2/query/{identity}", "/api/invoice_all/v2/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);
        HXResponse result = invoiceService.query(decryptResult, companyservice);

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

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

    }

//    @ApiOperation("获取发票打印XML")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "string", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", required = true)})
    @PostMapping({"/api/invoice_all/v2/print_xml/{identity}", "/api/invoice_all/v2/print_xml"})
    public Object printXmlml(HttpServletRequest request, String identity) throws Exception {
//       getPrintXML

        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.getPrintXML(decryptResult, companyservice);

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

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

    }

//    @ApiOperation("快速红冲")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "string", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", required = true)})
    @PostMapping({"/api/invoice_all/v2/issueFastRedInvoice/{identity}", "/api/invoice_all/v2/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());

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

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

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

    }


//    @ApiOperation("蓝字发票统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "string", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", required = true)})
    @PostMapping({"/api/invoice_all/v2/invoice_statistics/{identity}", "/api/invoice_all/v2/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, "");

    }

//    @ApiOperation("审核红字信息表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "string", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", required = true)})
    @PostMapping({"/api/invoice_all/v2/auditRedInfo/{identity}", "/api/invoice_all/v2/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());

        InvoiceAllShhzTWODTO invoiceAllShhzDTO= decryptResult.toJavaObject(InvoiceAllShhzTWODTO.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, "");

    }


//    @ApiOperation("查询红字信息表明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "string", example = "1130", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", required = true)})
    @PostMapping({"/api/invoice_all/v2/queryRedInfoDetail/{identity}", "/api/invoice_all/v2/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, "");

    }




}
