package com.jianshui.api.controller.http.income.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.DateUtils;
import com.jianshui.common.utils.StringUtils;
import com.jianshui.income.domain.dto.*;
import com.jianshui.income.factory.IAuthServiceFactory;
import com.jianshui.income.factory.IIncomeRequestFactory;
import com.jianshui.income.factory.IIncomeResponseFactory;
import com.jianshui.income.service.IAuthInvoice;
import com.jianshui.income.service.IIncomeRequestService;
import com.jianshui.income.service.IIncomeResponseService;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 发票认证控制器
 *
 * @author xkng
 * @date 2022/9/9 15:54
 */
@Api("进项认证接口")
@RestController
@RequestMapping("/api/income/v1/invoice_auth")
@Slf4j
public class InvoiceAuthController {

    @Autowired
    private ICompanyserviceService iCompanyserviceService;

    @Autowired
    private IServiceManageService serviceManageService;

    @Autowired
    private IIncomeRequestFactory incomeRequestFactory;

    @Autowired
    private IIncomeResponseFactory incomeResponseFactory;

    @Autowired
    private IAuthServiceFactory collectServiceFactory;


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


    @ApiOperation("状态采集")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "string", example = "", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", required = true)})
    @PostMapping({"/statusAcquisition/{identity}", "/statusAcquisition"})
    public Object statusAcquisition(HttpServletRequest request, String identity) throws Exception {

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

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

        // 获得入口报文适配器
        String requestAdapterKey = serviceManageService.getRequestAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IIncomeRequestService incomeRequestService = incomeRequestFactory.getService(requestAdapterKey);
        //调用入口报文适配器里面的解密
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);

        // 获得请求实例（根据companyservicemanager配置获取），并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IAuthInvoice iAuthInvoice = collectServiceFactory.getServiceAuth(serviceKey);

        //请求
        IncomeStatusCollectionDTO incomeStatusCollectionDTO = requestBody.toJavaObject(IncomeStatusCollectionDTO.class);
        AjaxResult result = iAuthInvoice.statusAcquisition(incomeStatusCollectionDTO, companyservice, null, null);

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

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

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


    @ApiOperation("申请认证")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "string", example = "", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", required = true)})
    @PostMapping({"/applyForCertification/{identity}", "/applyForCertification"})
    public Object applyForCertification(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());
        IIncomeRequestService incomeRequestService = incomeRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);


        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IAuthInvoice iAuthInvoice = collectServiceFactory.getServiceAuth(serviceKey);

        //请求
        IncomeApplyLegalizePrimaryDTO incomeApplyLegalizePrimaryDTO = requestBody.toJavaObject(IncomeApplyLegalizePrimaryDTO.class);
        AjaxResult result = iAuthInvoice.applyLegalize(incomeApplyLegalizePrimaryDTO, companyservice);

        //返回处理
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }
        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IIncomeResponseService incomeResponseService = incomeResponseFactory.getService(responseAdapterKey);

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


    @ApiOperation("获取认证结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "string", example = "", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", required = true)})
    @PostMapping({"/getCertificationResult/{identity}", "/getCertificationResult"})
    public Object getCertificationResult(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());
        IIncomeRequestService incomeRequestService = incomeRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);


        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IAuthInvoice iAuthInvoice = collectServiceFactory.getServiceAuth(serviceKey);

        //请求
        IncomeApplyLegalizeResultDTO incomeApplyLegalizeResultDTO = requestBody.toJavaObject(IncomeApplyLegalizeResultDTO.class);
        AjaxResult result = iAuthInvoice.applyLegalizeResult(incomeApplyLegalizeResultDTO, companyservice);

        //返回处理
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }
        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IIncomeResponseService incomeResponseService = incomeResponseFactory.getService(responseAdapterKey);

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


    @ApiOperation("申请确认")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "string", example = "", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", required = true)})
    @PostMapping({"/applicationConfirmation/{identity}", "/applicationConfirmation"})
    public Object applicationConfirmation(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());
        IIncomeRequestService incomeRequestService = incomeRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);


        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IAuthInvoice iAuthInvoice = collectServiceFactory.getServiceAuth(serviceKey);

        //请求
        IncomeApplyConfirmDTO incomeApplyConfirmDTO = requestBody.toJavaObject(IncomeApplyConfirmDTO.class);
        AjaxResult result = iAuthInvoice.applyConfirm(incomeApplyConfirmDTO, companyservice);

        //返回处理
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }
        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IIncomeResponseService incomeResponseService = incomeResponseFactory.getService(responseAdapterKey);

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


    @ApiOperation("获取确认结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "string", example = "", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", required = true)})
    @PostMapping({"/getConfirmationResult/{identity}", "/getConfirmationResult"})
    public Object getConfirmationResult(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());
        IIncomeRequestService incomeRequestService = incomeRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);


        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IAuthInvoice iAuthInvoice = collectServiceFactory.getServiceAuth(serviceKey);

        //请求
        IncomeApplyConfirmResultDTO incomeStatusCollectionDTO = requestBody.toJavaObject(IncomeApplyConfirmResultDTO.class);
        AjaxResult result = iAuthInvoice.applyConfirmResult(incomeStatusCollectionDTO, companyservice);

        //返回处理
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }
        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IIncomeResponseService incomeResponseService = incomeResponseFactory.getService(responseAdapterKey);

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

    @ApiOperation("获取企业档案")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "string", example = "", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", required = true)})
    @PostMapping({"/getCorporateProfile/{identity}", "/getCorporateProfile"})
    public Object getCorporateProfile(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());
        IIncomeRequestService incomeRequestService = incomeRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);


        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IAuthInvoice iAuthInvoice = collectServiceFactory.getServiceAuth(serviceKey);

        //请求
        IncomeGetCompanyInfoDTO incomeStatusCollectionDTO = requestBody.toJavaObject(IncomeGetCompanyInfoDTO.class);
        AjaxResult result = iAuthInvoice.getCompanyInfo(incomeStatusCollectionDTO, companyservice);

        //返回处理
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }
        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IIncomeResponseService incomeResponseService = incomeResponseFactory.getService(responseAdapterKey);

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


    @ApiOperation("重置密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "string", example = "", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", required = true)})
    @PostMapping({"/restPassword/{identity}", "/restPassword"})
    public Object restPassword(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());
        IIncomeRequestService incomeRequestService = incomeRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);


        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IAuthInvoice iAuthInvoice = collectServiceFactory.getServiceAuth(serviceKey);

        //请求
        IncomeResetPasswordDTO incomeResetPasswordDTO = requestBody.toJavaObject(IncomeResetPasswordDTO.class);
        AjaxResult result = iAuthInvoice.resetPassword(incomeResetPasswordDTO, companyservice);

        //返回处理
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }
        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IIncomeResponseService incomeResponseService = incomeResponseFactory.getService(responseAdapterKey);

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


    @ApiOperation("设置密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identity", value = "身份认证", dataType = "string", example = "", required = true),
            @ApiImplicitParam(name = "order", value = "请求体", required = true)})
    @PostMapping({"/setPassword/{identity}", "/setPassword"})
    public Object setPassword(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());
        IIncomeRequestService incomeRequestService = incomeRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);


        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IAuthInvoice iAuthInvoice = collectServiceFactory.getServiceAuth(serviceKey);

        //请求
        IncomeSetPasswordDTO incomeSetPasswordDTO = requestBody.toJavaObject(IncomeSetPasswordDTO.class);
        AjaxResult result = iAuthInvoice.setPassword(incomeSetPasswordDTO, companyservice);

        //返回处理
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }
        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IIncomeResponseService incomeResponseService = incomeResponseFactory.getService(responseAdapterKey);

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


    /** 全面票采集部分 */
    // TODO: 2022/9/22

    /**
     * 全票面采集
     *
     * @param request
     * @param identity
     * @return
     * @throws Exception
     */
    @PostMapping({"/get_invoice/{identity}", "/get_invoice"})
    public Object single(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());
        IIncomeRequestService incomeRequestService = incomeRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);


        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IAuthInvoice incomeService = collectServiceFactory.getServiceAuth(serviceKey);
        // 13位时间戳转10位
        String CJKSRQ = requestBody.getString("CJKSRQ");
        if (StringUtils.isNotEmpty(CJKSRQ)) {
            Date temp = DateUtils.parseDate(StringUtils.trim(CJKSRQ), "yyyyMMddHHmmss");
            requestBody.put("CJKSRQ", temp);
        }
        String CJJSRQ = requestBody.getString("CJJSRQ");
        if (StringUtils.isNotEmpty(CJJSRQ)) {
            Date temp = DateUtils.parseDate(StringUtils.trim(CJJSRQ), "yyyyMMddHHmmss");
            requestBody.put("CJJSRQ", temp);
        }
        IncomeGetInvoiceDTO incomeGetInvoiceDTO = requestBody.toJavaObject(IncomeGetInvoiceDTO.class);
        AjaxResult result = incomeService.getInvoice(incomeGetInvoiceDTO, companyservice);
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }

        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IIncomeResponseService incomeResponseService = incomeResponseFactory.getService(responseAdapterKey);
        return incomeResponseService.response(result, companyservice, "get_invoice");
    }

    /**
     * 主信息采集
     *
     * @param request
     * @param identity
     * @return
     * @throws Exception
     */
    @PostMapping({"/master_information/{identity}", "/master_information"})
    public Object masterInformation(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());
        IIncomeRequestService incomeRequestService = incomeRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);


        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IAuthInvoice incomeService = collectServiceFactory.getServiceAuth(serviceKey);
        IncomeMasterInformationDTO incomeGetInvoiceDTO = requestBody.toJavaObject(IncomeMasterInformationDTO.class);
        AjaxResult result = incomeService.masterInformation(incomeGetInvoiceDTO, companyservice);
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }

        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IIncomeResponseService incomeResponseService = incomeResponseFactory.getService(responseAdapterKey);
        return incomeResponseService.response(result, companyservice, "master_information");
    }

    /**
     * 获取统计信息
     *
     * @param request
     * @param identity
     * @return
     * @throws Exception
     */
    @PostMapping({"/get_count_info/{identity}", "/get_count_info"})
    public Object getCountInfo(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());
        IIncomeRequestService incomeRequestService = incomeRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);


        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IAuthInvoice incomeService = collectServiceFactory.getServiceAuth(serviceKey);
        IncomeCountInfoDTO incomeGetInvoiceDTO = requestBody.toJavaObject(IncomeCountInfoDTO.class);
        AjaxResult result = incomeService.getCountInfo(incomeGetInvoiceDTO, companyservice);
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }

        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IIncomeResponseService incomeResponseService = incomeResponseFactory.getService(responseAdapterKey);
        return incomeResponseService.response(result, companyservice, "get_count_info");
    }


    /**
     * 申请统计
     *
     * @param request
     * @param identity
     * @return
     * @throws Exception
     */
    @PostMapping({"/apply_count/{identity}", "/apply_count"})
    public Object applyCount(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());
        IIncomeRequestService incomeRequestService = incomeRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);


        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IAuthInvoice incomeService = collectServiceFactory.getServiceAuth(serviceKey);
        IncomeApplyCountDTO incomeGetInvoiceDTO = requestBody.toJavaObject(IncomeApplyCountDTO.class);
        AjaxResult result = incomeService.applyCount(incomeGetInvoiceDTO, companyservice);
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }

        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IIncomeResponseService incomeResponseService = incomeResponseFactory.getService(responseAdapterKey);
        return incomeResponseService.response(result, companyservice, "apply_count");
    }

    /**
     * 获取统计结果
     *
     * @param request
     * @param identity
     * @return
     * @throws Exception
     */
    @PostMapping({"/apply_count_result/{identity}", "/apply_count_result"})
    public Object applyCountResult(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());
        IIncomeRequestService incomeRequestService = incomeRequestFactory.getService(requestAdapterKey);
        JSONObject requestBody = incomeRequestService.decrypt(request, companyservice);


        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier(SERVICE_KEY, companyservice.getCompanyid());
        IAuthInvoice incomeService = collectServiceFactory.getServiceAuth(serviceKey);
        IncomeApplyCountResultDTO incomeGetInvoiceDTO = requestBody.toJavaObject(IncomeApplyCountResultDTO.class);
        AjaxResult result = incomeService.applyCountResult(incomeGetInvoiceDTO, companyservice);
        if (result.isSuccess()) {
            serviceManageService.companyConsume(SERVICE_KEY, companyservice.getCompanyid());
        }

        String responseAdapterKey = serviceManageService.getResponseAdapterKey(SERVICE_KEY, companyservice.getCompanyid());
        IIncomeResponseService incomeResponseService = incomeResponseFactory.getService(responseAdapterKey);
        return incomeResponseService.response(result, companyservice, "apply_count_result");
    }


}
