package com.jianshui.api.exception;

import com.jianshui.common.core.domain.AjaxResult;
import com.jianshui.common.core.domain.entity.Companyservice;
import com.jianshui.common.exception.jianshui.*;
import com.jianshui.common.utils.StringUtils;
import com.jianshui.income.factory.IIncomeResponseFactory;
import com.jianshui.income.service.IIncomeResponseService;
import com.jianshui.invoice.factory.IInvoiceResponseFactory;
import com.jianshui.invoice.service.IInvoiceResponseService;
import com.jianshui.invoiceall.factory.IInvoceiAllServiceFactory;
import com.jianshui.invoiceall.factory.IInvoiceAllResponseFactory;
import com.jianshui.invoiceall.service.IInvoiceAllResponseService;
import com.jianshui.system.event.evemts.JianshuiBillInfoCalucateExceptionEvent;
import com.jianshui.system.service.IServiceManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Description 简税异常拦截器
 * @Author 巩权林
 * @Date 2022-03-28 17:24
 **/
@RestControllerAdvice
@Slf4j
public class JianshuiExceptionHandler {

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    @Autowired
    private IInvoiceResponseFactory invoiceResponseFactory;

    @Autowired
    private IIncomeResponseFactory incomeResponseFactory;

    @Autowired
    private IServiceManageService serviceManageService;

    @Autowired
    private IInvoiceAllResponseFactory invoiceAllResponseFactory;

    /**
     * 简税异常捕获
     *
     * @param e
     * @return
     */
    @ExceptionHandler(JianshuiServiceException.class)
    public AjaxResult handleJianshuiServiceException(JianshuiServiceException e) {
        e.printStackTrace();
        log.error("【简税异常】捕获异常，错误信息'{}'.", e.getMessage());
        return AjaxResult.error(e.getMessage());
    }

    /**
     * 参数校验错误异常捕获
     *
     * @param e
     * @return
     */
    @ExceptionHandler(JianshuiParamErrorException.class)
    public Object handleJianshuiParamErrorException(JianshuiParamErrorException e) throws Exception {
        e.printStackTrace();
        log.error("【简税异常】参数校验错误，错误信息'{}'.", e.getMessage());
        Companyservice companyservice = e.getCompanyservice();
        if (companyservice == null) {
            return AjaxResult.error(e.getMessage());
        }
        String responseAdapterKey = serviceManageService.getResponseAdapterKey(e.getServiceKey(), companyservice.getCompanyid());
        if (StringUtils.equals(e.getServiceKey(), "invoice")) {
            IInvoiceResponseService invoiceResponseService = invoiceResponseFactory.getService(responseAdapterKey);
            return invoiceResponseService.response(e, companyservice);
        }

        if (StringUtils.equals(e.getServiceKey(), "income")) {
            IIncomeResponseService invoiceResponseService = incomeResponseFactory.getService(responseAdapterKey);
            return invoiceResponseService.response(e, companyservice);
        }

        // 数科-金四
        if (StringUtils.equals(e.getServiceKey(), "invoiceall")) {
            IInvoiceAllResponseService invoiceResponseService = invoiceAllResponseFactory.getService(responseAdapterKey);
            return invoiceResponseService.response(e, companyservice);
        }

        return AjaxResult.error("非法请求");

    }

    /**
     * 服务过期异常捕获
     *
     * @param e
     * @return
     */
    @ExceptionHandler(JianshuiServiceExpireException.class)
    public AjaxResult handleJianshuiServiceExpireException(JianshuiServiceExpireException e) {
        e.printStackTrace();
        log.error("【简税异常】服务过期错误，错误信息'{}'.", e.getMessage());
        return AjaxResult.error(e.getMessage());
    }

    /**
     * 无服务异常捕获
     *
     * @param e
     * @return
     */
    @ExceptionHandler(JianshuiNoServiceException.class)
    public AjaxResult handleJianshuiNoServiceException(JianshuiNoServiceException e) {
        e.printStackTrace();
        log.error("【简税异常】无服务异常错误，错误信息'{}'.", e.getMessage());
        return AjaxResult.error(e.getMessage());
    }

    /**
     * 服务配置异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(JianshuiServiceConfigErrorException.class)
    public AjaxResult handleJianshuiServiceConfigErrorException(JianshuiServiceConfigErrorException e) {
        e.printStackTrace();
        log.error("【简税异常】服务配置错误，错误信息'{}'.", e.getMessage());
        return AjaxResult.error(e.getMessage());
    }

    /**
     * 无可用次数异常捕获
     *
     * @param e
     * @return
     */
    @ExceptionHandler(JianshuiServiceNoSurplusException.class)
    public AjaxResult handleJianshuiServiceNoSurplusException(JianshuiServiceNoSurplusException e) {
        e.printStackTrace();
        log.error("【简税异常】无可用次数错误，错误信息'{}'.", e.getMessage());
        return AjaxResult.error(e.getMessage());
    }

    /**
     * 计算税额、不含税金额、含税金额异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(JianshuiBillInfoCalucateException.class)
    public AjaxResult handleJianshuiBillInfoCalucateException(JianshuiBillInfoCalucateException e) {
        e.printStackTrace();
        String errorMsg = StringUtils.format("【简税异常】计算不含税金额、税额、金额异常，错误信息'{}',CompanyId'{}',订单信息{}", e.getMessage(), e.getCompanyservice().getCompanyid(), e.getBillInfoJson());
        log.error(errorMsg);
        JianshuiBillInfoCalucateExceptionEvent event = new JianshuiBillInfoCalucateExceptionEvent(this, errorMsg, e.getCompanyservice(), e.getBillInfoJson());
        applicationEventPublisher.publishEvent(event);
        return AjaxResult.error(e.getMessage());
    }

    /**
     * 无可用次数异常捕获
     *
     * @param e
     * @return
     */
    @ExceptionHandler(JianshiSystemErrorException.class)
    public AjaxResult handleJianshiSystemErrorException(JianshiSystemErrorException e) {
        e.printStackTrace();
        log.error("【简税异常】系统级异常'{}'.", e.getMessage());
        return AjaxResult.error(e.getMessage());
    }
}
