package com.jeequan.jeepay.thirdparty.channel.aliaqf;

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.TransferSubject;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.interfaces.transfer.IChannelTransferSubjectService;
import com.jeequan.jeepay.core.model.params.aliaqf.AliaqfIsvParams;
import com.jeequan.jeepay.db.entity.TransferSubjectEntity;
import com.jeequan.jeepay.service.impl.TransferSubjectService;
import com.jeequan.jeepay.thirdparty.channel.aliaqf.model.AliAqfTransferBasicInfo;
import com.jeequan.jeepay.thirdparty.channel.alipay.AliAqfV2Service;
import com.jeequan.jeepay.thirdparty.service.ConfigContextQueryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * TODO
 *
 * @author deng
 * @since 2024/5/8
 */
@Slf4j
@Service
public class AliaqfChannelTransferSubjectService implements IChannelTransferSubjectService<AliAqfTransferBasicInfo> {

    @Autowired
    private TransferSubjectService transferSubjectService;

    @Autowired
    private AliAqfV2Service aliAqfV2Service;
    @Autowired
    private ConfigContextQueryService configContextQueryService;

    @Override
    public void addExporter(AliAqfTransferBasicInfo transferBasicInfo) {

    }

    @Override
    public void addAcceptor(AliAqfTransferBasicInfo transferBasicInfo) {
        throw new BizException("当前通道【安全发】不支持该操作");
    }

    @Override
    public void callbackProcess(JSONObject bizContent) {
        log.info("安全发的签约回调, 回调参数为{}", bizContent);
        TransferSubjectEntity subjectEntity = transferSubjectService.getByApplyId(bizContent.getString("external_agreement_no"));

        AliaqfIsvParams isvParams = (AliaqfIsvParams) configContextQueryService.queryTransferIsvParams(subjectEntity.getIsvNo(), CS.IF_CODE.ALIAQF);
        try {
            //验签
            if (!AliaqfKit.checkSign(bizContent, isvParams)) {
                log.error("安全发签约回调验签失败");
                return;
            }

            subjectEntity.setTransChannelSignNo(bizContent.getString("agreement_no"));
            subjectEntity.setSuccResParameter(bizContent.toJSONString());

            switch (bizContent.getString("status")) {
                case "NORMAL":
                    subjectEntity.setSignState((int) TransferSubject.SIGN_STATE_SUCCESS);
                    transferSubjectService.updateById(subjectEntity);
                    // 开通记账本
                    aliAqfV2Service.fundAccountbookCreate(subjectEntity.getId());
                    break;
                case "TEMP":
                    subjectEntity.setSignState((int) TransferSubject.SIGN_STATE_ON_HOLD);
                    transferSubjectService.updateById(subjectEntity);
                    break;
                case "STOP":
                    subjectEntity.setSignState((int) TransferSubject.SIGN_STATE_ON_PAUSE);
                    transferSubjectService.updateById(subjectEntity);
                    break;
                default:
                    subjectEntity.setSignState((int) TransferSubject.SIGN_STATE_UNKNOWN);
                    transferSubjectService.updateById(subjectEntity);
                    break;
            }
        } catch (Exception e) {
            log.error("安全发签约回调异常", e);
            transferSubjectService.updateById(subjectEntity);
        }
    }

    @Override
    public Object retOk(JSONObject bizContent) {
        return "SUCCESS";
    }
}
