package com.jkcredit.invoice.credit;

import com.jkcredit.invoice.common.DataResult;
import com.jkcredit.invoice.component.StatisRequestIdTimeComp;
import com.jkcredit.invoice.credit.interserver.CustomerInterLowerService;
import com.jkcredit.invoice.credit.interserver.NoCarInterService;
import com.jkcredit.invoice.credit.interserver.SelfCarInterService;
import com.jkcredit.invoice.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 接口过滤器
 * @author mumuxigua
 */
@Service("interfaceCheckServer")
@Slf4j
public class InterfaceCheckServer {

    @Autowired
    CustomerInterLowerService customerInterLowerService;

    @Autowired
    SelfCarInterService selfCarInterService;
    @Autowired
    NoCarInterService noCarInterService;
    @Autowired
    StatisRequestIdTimeComp statisRequestIdTimeComp;

    /**
     * 跳转到对应的Service服务处理逻辑
     * @param appKey
     * @param api
     * @param data
     * @param requestid
     * @return
     */
    public DataResult doJumpHandler(String appKey, String api, String data, String requestid) {
        log.info("[-InterfaceCheckServer.doJumpHandler-] request appKey=" + appKey + " ,api=" + api + " ,data=" + StringUtil.removeJSonField(data,"base64Str") + " ,requestid=" + requestid);
        long startTime = System.currentTimeMillis();
        DataResult resultTimeOut = new DataResult();
        resultTimeOut.setData(3);
        resultTimeOut.setCode(200);
        resultTimeOut.setRequestid(requestid);
        resultTimeOut.setMsg("网络异常");
        DataResult result;
        result = getDataResult(appKey, api, data, requestid, resultTimeOut);
        log.info("[-InterfaceCheckServer.doJumpHandler-] result appKey=" + appKey + " ,api=" + api + " ,data=" + StringUtil.removeJSonField(data,"base64Str") + " ,requestid=" + requestid + " ,result=" + result + " ,costTime=" + (System.currentTimeMillis() - startTime));
        return result;

    }

    private DataResult getDataResult(String appKey, String api, String data, String requestid, DataResult resultTimeOut) {
        DataResult result;
        switch (api) {
            //----------------------------自有车、无车公共接口---------------------------------//
            //无车、自有车企业注册
            case "COMPANY_ADD_V1":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = customerInterLowerService.customeInterRec(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //无车、自有车协议上传
            case "PROTOCOL_ADD_V1":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = customerInterLowerService.customeProtocolUpLoad(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //无车、自有车企业查询
            case "COMPANY_QUERY_V1":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = customerInterLowerService.customeInterRecQuery(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //自有车 无车、协议上传状态查询
            case "PROTOCOL_STATUS_V1":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = customerInterLowerService.customeProtocolStatus(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //----------------------------自有车下游接口---------------------------------//
            //自有车 用户卡列表查询
            case "CARD_BIND_QUERY_LIST_V1":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = selfCarInterService.customeretcquery(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //自有车 卡信息查询
            case "CARD_QUERY_CARD_V1":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = selfCarInterService.customerQueryEtcInfo(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //自有车 下发短信验证码
            case "CARD_BINDING_V1":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = selfCarInterService.customeretcrec(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //自有车 卡绑定接口 渠道调用此接口，上传用户收到的短信验证码
            case "CARD_VALID_CODE_V1":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = selfCarInterService.customeretcrecvalid(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //自有车 交易查询接口 渠道通过此接口可以查询单张卡连续90天内的交易（待开票、开票中、已开票）
            case "CARD_TRADE_V1":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = selfCarInterService.getTradeList(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //自有车 申请开票接口 渠道通过此接口可以对该公司绑定的单张卡连续90天内的交易进行开票。
            case "B2B_INVOICE_APPLY_V1":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = selfCarInterService.applInvoice(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //自有车 已开发票查询接口 渠道通过此接口可以根据该公司绑定的单张卡查询此卡在某个月开具的发票。
            case "B2B_INVOICE_QUERY_V1":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = selfCarInterService.getSelfCarInvoicesByTime(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //自有车 已开发票查询接口 根据开票id进行查询。
            case "B2B_INVOICE_APPL_QUERY_V1":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = selfCarInterService.getSelfCarInvoicesByAppl(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //自有车 发票下载 渠道通过此接口可以下载某公司某个月份开具的发票。
            case "B2B_INVOICE_PACKAGE_V1":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = selfCarInterService.getSelfCarInvoicePackage(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //自有车 卡解绑接口。
            case "CARD_UNBIND_V1":
                result = selfCarInterService.customerCarUnRec(appKey, api, data, requestid);
                break;
            //自有车 用户卡换绑。
            case "B2B_CHANGE_CARD_V1":
                result = selfCarInterService.customerCardChange(appKey, api, data, requestid);
                break;
            //自有车 用户卡换绑查询。
            case "B2B_CHANGE_CARD_QUERY_V1":
                result = selfCarInterService.customerCardChangeQuery(appKey, api, data, requestid);
                break;
            //自有车 按交易查申请。
            case "B2B_QUERY_TRADE_APPLY_V1":
                result = selfCarInterService.tradeApplyQuery(appKey, api, data, requestid);
                break;
            //----------------------------无车下游接口---------------------------------//
            //无车 车辆备案接口
            case "VEHICLE_REGISTER":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = noCarInterService.customerCarRec(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //无车 车辆备案查询接口
            case "VEHICLE_REGISTER_QUERY":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = noCarInterService.customeRecUpperQuery(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //无车 实时运单开始指令
            case "WAY_BILL_START":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + StringUtil.removeJSonField(data,"base64Str") + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = noCarInterService.noCarBillStart(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //无车 实时运单结束指令
            case "WAY_BILL_END":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + StringUtil.removeJSonField(data,"base64Str") + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = noCarInterService.noCarBillEnd(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //无车 历史运单开始指令
            case "WAY_BILL_HISTORY_START":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + StringUtil.removeJSonField(data,"base64Str") + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = noCarInterService.noCarHisWaybillStart(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //无车 历史运单结束指令
            case "WAY_BILL_HISTORY_END":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + StringUtil.removeJSonField(data,"base64Str") + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = noCarInterService.noCarHisWaybillEnd(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //无车  运单号查询发票数据
            case "WAY_BILL_NUM_FIND_INVOICE":
                result = noCarInterService.noCarVoiceQuery(appKey, api, data, requestid);
                break;
            //无车  获取未查询过发票的运单编号
            case "FIND_NO_SEARCH_NUM":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = noCarInterService.noCarNoVoiceQuery(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //无车   账号余额查询接口
            case "BALANCE_QUERY":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = noCarInterService.balanceQuery(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //无车  红冲发票查询接口
            case "RED_INK_INVOICE_QUERY":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = noCarInterService.redInkInvoiceQuery(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //无车  上传企业开票经办人
            case "COMPANY_COMPANYDETAILADD":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = noCarInterService.companyDetailAdd(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //无车  变更企业开票经办人
            case "COMPANY_COMPANYOPERATOREDIT":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = noCarInterService.operatorEdit(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //无车  确认变更企业开票经办人
            case "COMPANY_COMPANYOPERATOREDITCONFIRM":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = noCarInterService.operatorEditConfirm(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //无车  上传企业开票经办人证明附件
            case "COMPANY_UPLOADOPERATOREVIDENCE":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = noCarInterService.operatorEvidence(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //无车  运单开票前发送验证码
            case "WAYBILL_OPERATORSENDCODE":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = noCarInterService.operatorSendCode(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //无车  运单开票前短信确认
            case "WAYBILL_OPERATORVALIDCODE":
                if (StatisRequestIdTimeComp.isLimit) {
                    log.info("InterfaceCheckServer.doJumpHandler,appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid + "=被拒绝");
                    result = resultTimeOut;
                } else {
                    statisRequestIdTimeComp.putReQuestIdAndTime(requestid);
                    result = noCarInterService.operatorValidCode(appKey, api, data, requestid);
                    statisRequestIdTimeComp.removeReQuestIdAndTime(requestid);
                }
                break;
            //无车  运单号查询发票数据 透传上游
            case "WAY_BILL_NUM_FIND_OWNER_INVOICE":
                result = noCarInterService.noCarVoiceOwnerQuery(appKey, api, data, requestid);
                break;
            default:
                result = null;
                break;
        }
        return result;
    }
}
