package com.jianshui.api.controller.http.invoiceall.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.utils.StringUtils;
import com.jianshui.invoiceall.domain.dto.*;
import com.jianshui.invoiceall.domain.dto.jcsk.AutonomousAuthorizationDTO;
import com.jianshui.invoiceall.domain.dto.jcsk.InvoiceAllSubmitNoteDTO;
import com.jianshui.invoiceall.factory.IInvoceiAllServiceFactory;
import com.jianshui.invoiceall.factory.IInvoiceAllRequestFactory;
import com.jianshui.invoiceall.factory.IInvoiceAllResponseFactory;
import com.jianshui.invoiceall.service.AutonomousSqService;
import com.jianshui.invoiceall.service.IInvoiceAll;
import com.jianshui.invoiceall.service.IInvoiceAllRequestService;
import com.jianshui.invoiceall.service.IInvoiceAllResponseService;
import com.jianshui.invoiceall.service.impl.adapter.request.AisinoIncomeRequestAdapterImpl;
import com.jianshui.invoiceall.service.impl.adapter.response.AisinoIncomeResponseAdapterImpl;
import com.jianshui.system.service.ICompanyserviceService;
import com.jianshui.system.service.IServiceManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description 全电接口-金财数科
 **/

@Api("金四-金财数科-全电接口")
@RestController
@RequestMapping
@Slf4j
public class InvoiceAllController {

    /** 基础*/
    @Autowired
    private ICompanyserviceService iCompanyserviceService;

    @Autowired
    private IServiceManageService serviceManageService;

    /** 金四*/
    @Autowired
    private IInvoiceAllRequestFactory invoiceAllRequestFactory;

    @Autowired
    private IInvoiceAllResponseFactory invoiceAllResponseFactory;

    @Autowired
    private IInvoceiAllServiceFactory iInvoceiAllServiceFactory;
    @Autowired
    private AutonomousSqService autonomousSqService;

    //入口报文适配器标志 这个东西是AisinoIncomeRequestAdapterImpl 中间的那块儿
    private final static String SERVICE_KEY = "invoiceall";



    @ApiOperation("税号切地区")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "inoviceAllQdLoginDTO", value = "请求体",dataType = "java.lang.Void", required = true)})
    @GetMapping("/api/invoice_all/v1/nsrsbhConvertAddr")
    public Object nsrsbhConvertAddr(@RequestParam String nsrsbh) throws Exception{
        return autonomousSqService.nsrsbhConvertAddr(nsrsbh);
    }


    @ApiOperation("自主授权")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "inoviceAllQdLoginDTO", value = "请求体",dataType = "java.lang.Void", required = true)})
    @PostMapping("/api/invoice_all/v1/autonomousAuthorization")
    public Object autonomousAuthorization(@RequestBody AutonomousAuthorizationDTO autonomousAuthorization) throws Exception{
        return autonomousSqService.autonomousAuthorization(autonomousAuthorization);
    }

    @ApiOperation("全电登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "inoviceAllQdLoginDTO", value = "请求体",dataType = "java.lang.Void", required = true)})
    @PostMapping("/api/invoice_all/v1/qdLogin")
    public Object qdLogin(@RequestBody InoviceAllQdLoginDTO inoviceAllQdLoginDTO) throws Exception{
        return autonomousSqService.qdLogin(inoviceAllQdLoginDTO);
    }


    @ApiOperation("提交短信验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "inoviceAllQdLoginDTO", value = "请求体",dataType = "java.lang.Void", required = true)})
    @PostMapping("/api/invoice_all/v1/submitNote")
    public Object submitNote(@RequestBody InvoiceAllSubmitNoteDTO invoiceAllSubmitNoteDTO) throws Exception{
        return autonomousSqService.submitNote(invoiceAllSubmitNoteDTO);
    }

    @ApiOperation("全电登录获取回调结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "java.lang.Void", example = "1130", required = true),
            @ApiImplicitParam(name = "inoviceAllQdLoginDTO", value = "请求体",dataType = "java.lang.Void", required = true)})
    @PostMapping("/api/invoice_all/v1/submitNoteResult")
    public Object submitNoteResult(@RequestBody InvoiceAllSubmitNoteDTO invoiceAllSubmitNoteDTO) throws Exception{
        return autonomousSqService.submitNoteResult(invoiceAllSubmitNoteDTO);
    }


    @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_all/v1/registration/{identity}", "/api/invoice_all/v1/registration"})
    public Object registration(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);
        }

        // 获得入口报文适配器
        IInvoiceAllRequestService incomeRequestService = new AisinoIncomeRequestAdapterImpl();
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);

        // 指定请求实例
        IInvoiceAll invoiceAllService = iInvoceiAllServiceFactory.getService("elephant_invoiceall");

        InvoiceAllYhdjDTO invoiceAllYhdjDTO = requestBody.toJavaObject(InvoiceAllYhdjDTO.class);
        AjaxResult result = invoiceAllService.registration(companyservice, invoiceAllYhdjDTO);

        //返回报文处理器
        IInvoiceAllResponseService incomeResponseService = new AisinoIncomeResponseAdapterImpl();

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


    @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_all/v1/generalQuery/{identity}", "/api/invoice_all/v1/generalQuery"})
    public Object generalQuery(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(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllRequestService incomeRequestService = invoiceAllRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAll invoiceAllService = iInvoceiAllServiceFactory.getService(serviceKey);

        InvoiceAllQueryDTO invoiceAllYhdjDTO = requestBody.toJavaObject(InvoiceAllQueryDTO.class);
        AjaxResult result = invoiceAllService.generalQuery(companyservice, invoiceAllYhdjDTO);

        //返回处理 增加调用次数
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }

        //返回报文处理器
        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllResponseService incomeResponseService = invoiceAllResponseFactory.getService(responseAdapterKey);

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


    @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_all/v1/getFileUrl/{identity}", "/api/invoice_all/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(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllRequestService incomeRequestService = invoiceAllRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAll invoiceAllService = iInvoceiAllServiceFactory.getService(serviceKey);

        InvoiceAllFileAcquisitionDTO invoiceAllFileAcquisitionDTO= requestBody.toJavaObject(InvoiceAllFileAcquisitionDTO.class);
        AjaxResult result = invoiceAllService.getFileUrl(companyservice, invoiceAllFileAcquisitionDTO);

        //返回处理 增加调用次数
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }

        //返回报文处理器
        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllResponseService incomeResponseService = invoiceAllResponseFactory.getService(responseAdapterKey);

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


    @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_all/v1/getFileAcquisition/{identity}", "/api/invoice_all/v1/getFileAcquisition"})
    public Object getFileAcquisition(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(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllRequestService incomeRequestService = invoiceAllRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAll invoiceAllService = iInvoceiAllServiceFactory.getService(serviceKey);

        InvoiceAllFileAcquisitionOnDTO invoiceAllFileAcquisitionDTO= requestBody.toJavaObject(InvoiceAllFileAcquisitionOnDTO.class);
        AjaxResult result = invoiceAllService.getFileAcquisitionOn(companyservice, invoiceAllFileAcquisitionDTO);

        //返回处理 增加调用次数
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }

        //返回报文处理器
        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllResponseService incomeResponseService = invoiceAllResponseFactory.getService(responseAdapterKey);

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



    @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_all/v1/addInvoice/{identity}", "/api/invoice_all/v1/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(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllRequestService incomeRequestService = invoiceAllRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAll invoiceAllService = iInvoceiAllServiceFactory.getService(serviceKey);

        
        AjaxResult result = invoiceAllService.addInvoice(companyservice, requestBody);

        //返回处理 增加调用次数
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }

        //返回报文处理器
        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllResponseService incomeResponseService = invoiceAllResponseFactory.getService(responseAdapterKey);

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



    @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_all/v1/qrcode/{identity}", "/api/invoice_all/v1/qrcode"})
    public Object qrcode(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(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllRequestService incomeRequestService = invoiceAllRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAll invoiceAllService = iInvoceiAllServiceFactory.getService(serviceKey);

        
        AjaxResult result = invoiceAllService.qrcode(companyservice, requestBody);

        //返回处理 增加调用次数
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }

        //返回报文处理器
        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllResponseService incomeResponseService = invoiceAllResponseFactory.getService(responseAdapterKey);

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


    @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_all/v1/qrcodeResult/{identity}", "/api/invoice_all/v1/qrcodeResult"})
    public Object qrcodeResult(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(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllRequestService incomeRequestService = invoiceAllRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAll invoiceAllService = iInvoceiAllServiceFactory.getService(serviceKey);


        AjaxResult result = invoiceAllService.qrcodeResult(companyservice, requestBody);

        //返回处理 增加调用次数
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }

        //返回报文处理器
        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllResponseService incomeResponseService = invoiceAllResponseFactory.getService(responseAdapterKey);

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


    @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_all/v1/query/{identity}", "/api/invoice_all/v1/query"})
    public Object query(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(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllRequestService incomeRequestService = invoiceAllRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAll invoiceAllService = iInvoceiAllServiceFactory.getService(serviceKey);

        
        AjaxResult result = invoiceAllService.query(companyservice, requestBody);

        //返回处理 增加调用次数
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }

        //返回报文处理器
        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllResponseService incomeResponseService = invoiceAllResponseFactory.getService(responseAdapterKey);

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

    @ApiOperation("获取发票打印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_all/v1/print_xml/{identity}", "/api/invoice_all/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(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllRequestService incomeRequestService = invoiceAllRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAll invoiceAllService = iInvoceiAllServiceFactory.getService(serviceKey);

        
        AjaxResult result = invoiceAllService.getPrintXML(companyservice, requestBody);

        //返回处理 增加调用次数
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }

        //返回报文处理器
        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllResponseService incomeResponseService = invoiceAllResponseFactory.getService(responseAdapterKey);

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

    @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_all/v1/add_red_invoice/{identity}", "/api/invoice_all/v1/add_red_invoice"})
    public Object addRedInvoice(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(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllRequestService incomeRequestService = invoiceAllRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAll invoiceAllService = iInvoceiAllServiceFactory.getService(serviceKey);

        
        AjaxResult result = invoiceAllService.addRedInvoice(companyservice, requestBody);

        //返回处理 增加调用次数
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }

        //返回报文处理器
        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllResponseService incomeResponseService = invoiceAllResponseFactory.getService(responseAdapterKey);

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

    @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_all/v1/issueFastRedInvoice/{identity}", "/api/invoice_all/v1/issueFastRedInvoice"})
    public Object issueFastRedInvoice(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(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllRequestService incomeRequestService = invoiceAllRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAll invoiceAllService = iInvoceiAllServiceFactory.getService(serviceKey);

        InvoiceAllKshcDTO invoiceAllKshcDTO= requestBody.toJavaObject(InvoiceAllKshcDTO.class);
        AjaxResult result = invoiceAllService.issueFastRedInvoice(companyservice, invoiceAllKshcDTO);

        //返回处理 增加调用次数
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }

        //返回报文处理器
        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllResponseService incomeResponseService = invoiceAllResponseFactory.getService(responseAdapterKey);

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





    @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_all/v1/invoice_statistics/{identity}", "/api/invoice_all/v1/invoice_statistics"})
    public Object invoiceStatistics(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(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllRequestService incomeRequestService = invoiceAllRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAll invoiceAllService = iInvoceiAllServiceFactory.getService(serviceKey);

        
        AjaxResult result = invoiceAllService.invoiceStatic(companyservice, requestBody);

        //返回处理 增加调用次数
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }

        //返回报文处理器
        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllResponseService incomeResponseService = invoiceAllResponseFactory.getService(responseAdapterKey);

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




    @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_all/v1/uploadRedInfo/{identity}", "/api/invoice_all/v1/uploadRedInfo"})
    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(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllRequestService incomeRequestService = invoiceAllRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAll invoiceAllService = iInvoceiAllServiceFactory.getService(serviceKey);

        InvoiceAllSqhzDTO invoiceAllKshcDTO= requestBody.toJavaObject(InvoiceAllSqhzDTO.class);
        AjaxResult result = invoiceAllService.uploadRedInfo(companyservice, invoiceAllKshcDTO);

        //返回处理 增加调用次数
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }

        //返回报文处理器
        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllResponseService incomeResponseService = invoiceAllResponseFactory.getService(responseAdapterKey);

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

    @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_all/v1/queryRedInfo/{identity}", "/api/invoice_all/v1/queryRedInfo"})
    public Object queryRedInfo(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(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllRequestService incomeRequestService = invoiceAllRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAll invoiceAllService = iInvoceiAllServiceFactory.getService(serviceKey);

        InvoiceAllCxhzDTO invoiceAllCxhzDTO= requestBody.toJavaObject(InvoiceAllCxhzDTO.class);
        AjaxResult result = invoiceAllService.queryRedInfo(companyservice, invoiceAllCxhzDTO);

        //返回处理 增加调用次数
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }

        //返回报文处理器
        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllResponseService incomeResponseService = invoiceAllResponseFactory.getService(responseAdapterKey);

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

    @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_all/v1/auditRedInfo/{identity}", "/api/invoice_all/v1/auditRedInfo"})
    public Object auditRedInfo(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(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllRequestService incomeRequestService = invoiceAllRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAll invoiceAllService = iInvoceiAllServiceFactory.getService(serviceKey);

        InvoiceAllShhzDTO invoiceAllShhzDTO= requestBody.toJavaObject(InvoiceAllShhzDTO.class);
        AjaxResult result = invoiceAllService.auditRedInfo(companyservice, invoiceAllShhzDTO);

        //返回处理 增加调用次数
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }

        //返回报文处理器
        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllResponseService incomeResponseService = invoiceAllResponseFactory.getService(responseAdapterKey);

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



    @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_all/v1/queryRedInfoDetail/{identity}", "/api/invoice_all/v1/queryRedInfoDetail"})
    public Object queryRedInfoDetail(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(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllRequestService incomeRequestService = invoiceAllRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAll invoiceAllService = iInvoceiAllServiceFactory.getService(serviceKey);

        InvoiceAllRedInvoiceDetailDTO invoiceAllRedInvoiceDetailDTO= requestBody.toJavaObject(InvoiceAllRedInvoiceDetailDTO.class);
        AjaxResult result = invoiceAllService.queryRedInfoDetail(companyservice, invoiceAllRedInvoiceDetailDTO);

        //返回处理 增加调用次数
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }

        //返回报文处理器
        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllResponseService incomeResponseService = invoiceAllResponseFactory.getService(responseAdapterKey);

        return incomeResponseService.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_all/v1/queryInvoiceBeRed/{identity}", "/api/invoice_all/v1/queryInvoiceBeRed"})
    public Object queryInvoiceBeRed(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(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllRequestService incomeRequestService = invoiceAllRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAll invoiceAllService = iInvoceiAllServiceFactory.getService(serviceKey);

        InvoiceAllCanRedDTO invoiceAllCanRedDTO = requestBody.toJavaObject(InvoiceAllCanRedDTO.class);
        AjaxResult result = invoiceAllService.queryInvoiceBeRed(companyservice, invoiceAllCanRedDTO);

        //返回处理 增加调用次数
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }

        //返回报文处理器
        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IInvoiceAllResponseService incomeResponseService = invoiceAllResponseFactory.getService(responseAdapterKey);

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






























}
