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.StringUtils;
import com.jianshui.income.factory.IIncomeRequestFactory;
import com.jianshui.income.factory.IIncomeResponseFactory;
import com.jianshui.income.factory.ICheckServiceFactory;
import com.jianshui.income.service.ICheckInvoice;
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;

/**
 * @Description 发票查验控制器
 * @Author 巩权林
 * @Date 2021/4/16 8:35
 **/

@Api("进项接口")
@RestController
@RequestMapping
@Slf4j
public class InvoiceCheckController {

    @Autowired
    private ICompanyserviceService iCompanyserviceService;

    @Autowired
    private IServiceManageService serviceManageService;

    @Autowired
    private IIncomeRequestFactory incomeRequestFactory;

    @Autowired
    private IIncomeResponseFactory incomeResponseFactory;

    @Autowired
    private ICheckServiceFactory incomeServiceFactory;

    @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/income/v1/invoice_check/single/{identity}", "/checkinvoice/single"})
    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("income", companyservice.getCompanyid());
        IIncomeRequestService incomeRequestService = incomeRequestFactory.getService(requestAdapterKey);
        JSONObject invoiceCheck = incomeRequestService.decrypt(request, companyservice);

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("income", companyservice.getCompanyid());
        ICheckInvoice incomeService = incomeServiceFactory.getService(serviceKey);
        AjaxResult result = incomeService.checkInvoiceSingle(companyservice, invoiceCheck);
        if (result.isSuccess()) {
            JSONObject resp = result.getJsonData();
            Integer robUser = resp.getInteger("robUserMoney");
            if (robUser != null && robUser >= 1) {
                serviceManageService.companyConsume("income", companyservice.getCompanyid());
            }
        }

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

    @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/income/v1/invoice_check/multil/{identity}", "/checkinvoice/multil"})
    public Object multil(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("income", companyservice.getCompanyid());
        IIncomeRequestService incomeRequestService = incomeRequestFactory.getService(requestAdapterKey);
        JSONObject invoiceCheck = incomeRequestService.decrypt(request, companyservice);

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("income", companyservice.getCompanyid());
        ICheckInvoice incomeService = incomeServiceFactory.getService(serviceKey);
        AjaxResult result = incomeService.checkInvoiceMultil(companyservice, invoiceCheck);

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

    @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/income/v1/invoice_check/multilreqult/{identity}", "/checkinvoice/multilreqult"})
    public Object multilreqult(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("income", companyservice.getCompanyid());
        IIncomeRequestService incomeRequestService = incomeRequestFactory.getService(requestAdapterKey);
        JSONObject invoiceCheck = incomeRequestService.decrypt(request, companyservice);

        // 获得请求实例，并且进行扣费
        String serviceKey = serviceManageService.getCompanyServiceSupplier("income", companyservice.getCompanyid());
        ICheckInvoice incomeService = incomeServiceFactory.getService(serviceKey);
        AjaxResult result = incomeService.checkInvoiceMultilResult(companyservice, invoiceCheck);

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

}
