package cn.pluss.platform.task;

import cn.pluss.platform.channel.MerchantAuditService;
import cn.pluss.platform.entity.Account;
import cn.pluss.platform.entity.JftMercPaymentChannel;
import cn.pluss.platform.entity.JftMerchantRateRecord;
import cn.pluss.platform.entity.MerchantRateNewRecord;
import cn.pluss.platform.jft.JftBankCardService;
import cn.pluss.platform.jft.JftMerchantRateRecordService;
import cn.pluss.platform.mapper.JftMercPaymentChannelMapper;
import cn.pluss.platform.mapper.UserAppMapper;
import cn.pluss.platform.merchant.JftMercPaymentChannelService;
import cn.pluss.platform.ysExtension.v20220615.AbstractYsResultService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author djh
 */
@Slf4j
@Component("ysMerAuditExtensionV2Handler")
public class YsMerAuditExtensionV2Handler implements IJftMercAuditHandler {

    @Autowired
    private MerchantAuditService ysAuditServiceExtensionV2;

    @Autowired
    private JftMerchantRateRecordService jmrrService;

    @Autowired
    private JftMercPaymentChannelService jpcService;

    @Resource
    private JftMercPaymentChannelMapper jpcMapper;

    @Autowired
    private SxfMerAuditHandler sxfMerAuditHandler;

    @Autowired
    private UserAppMapper userAppMapper;

    @Autowired
    private JftBankCardService jbcService;

    @Override
    @Async
    @Transactional(rollbackFor = Exception.class)
    public void auditCallback(JSONObject auditResult) {
        if (auditResult == null) {
            return;
        }

        String notifyType = auditResult.getString("notifyType");

        switch (notifyType) {
            case "00":
                // 进件通知
                handleAudit(auditResult);
                break;
            case "01":
                // 报备通知
                handleReport(auditResult);
                break;
            case "02":
                // 变更通知
                handleChange(auditResult);
                break;
            default:
                break;
        }
    }

    private void handleAudit(JSONObject auditResult) {
        String status = auditResult.getString("status");

        // 进件成功
        JSONObject urm = auditResult.getJSONObject("urm");
        if ("00".equals(status)) {
            // 进件成功
            ((AbstractYsResultService) ysAuditServiceExtensionV2).success(urm);
        }

        if ("20".equals(status)) {
            // 进件成功
            ((AbstractYsResultService) ysAuditServiceExtensionV2).remainSigned(urm);
        }

        if ("90".equals(status)) {
            ((AbstractYsResultService) ysAuditServiceExtensionV2).fail(urm);
        }
    }

    private void handleReport(JSONObject auditResult) {
        // 此处不做报备处理
    }

    private void handleChange(JSONObject auditResult) {
        String status = auditResult.getString("status");

        // 变更
        JSONObject change = auditResult.getJSONObject("change");
        String changeFlowId = change.getString("changeFlowId");
        String nogoRemark = change.getString("nogoRemark");

        LambdaQueryWrapper<JftMerchantRateRecord> qWrapper = Wrappers.lambdaQuery();
        qWrapper.eq(JftMerchantRateRecord::getRemark, changeFlowId);
        JftMerchantRateRecord jmrr = jmrrService.getOne(qWrapper);
        if (jmrr != null) {
            // 费率变更
            if ("00".equals(status)) {
                //费率变更成功
                jmrrService.onSuccess(jmrr);
            } else {
                // 费率变更失败
                jmrrService.onFail(jmrr);
            }

            return;
        }

        // 变更通知
        JftMercPaymentChannel jpc = jpcMapper.selectByChangeFlowId(changeFlowId);

        if (jpc != null) {
            // 结算卡变更
            if ("00".equals(status)) {
                // 结算卡变更成功
                jbcService.editSuccess(jpc);
            } else {
                // 结算卡变更失败
                jbcService.editFail(jpc, nogoRemark);
            }

            return;
        }
    }
}
