package cn.pluss.platform.task;

import cn.pluss.platform.entity.MerchantChannelStatus;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.merchantChannelStatus.MerchantChannelStatusService;
import cn.pluss.platform.ys.YsService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

/**
 * @author djh
 */
@Slf4j
@Component("ysOldMerAuditHandler")
public class YsOldMerAuditHandler extends BaseMerAuditHandler<YsOldMerAuditHandler> {

    @Autowired
    private YsService ysService;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private MerchantChannelStatusService channelStatusService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MerchantChannelStatus auditCallback(JSONObject auditResult) {
        log.info("=============回调参数=============");
        log.info(auditResult.toJSONString());
        val qWrapper = new LambdaQueryWrapper<MerchantChannelStatus>()
                .eq(MerchantChannelStatus::getApplicationId, auditResult.get("sysFlowId"))
                .eq(MerchantChannelStatus::getChannel, 4);
        val msc = channelStatusService.getOne(qWrapper);
        msc.setExtra(auditResult);
        val status = auditResult.getString("status");
        val code = auditResult.getString("code");
        val note = auditResult.getString("note");

        if (!"00".equals(code)) {
            throw new MsgException("【" + msc.getMerchantCode() + "】商户信息查询失败");
        }

        if ("03".equals(status)) {
            return null;
        }

        msc.setRemark(note);
        if ("90".equals(status)) {
            msc.setStatus(MerchantChannelStatus.AUDIT_STATUS_REJECT);
        }

        msc.setRemark(note);
        if ("04".equals(status)) {
            msc.setStatus(MerchantChannelStatus.AUDIT_STATUS_FIRST_TRIAL_SUCCESS);
        }

        msc.setRemark(note);
        if ("05".equals(status)) {
            msc.setStatus(MerchantChannelStatus.AUDIT_STATUS_SUCCESS);
            String mercId = auditResult.getString("mercId");
            msc.setMerchantId(mercId);
        }

        channelStatusService.updateById(msc);

        return msc;
    }

    @Override
    public void afterAudit(JSONObject auditResult) {
        String bizResponseJson = auditResult.getString("bizResponseJson");
        JSONObject bizResponse = JSON.parseObject(bizResponseJson);
        MerchantChannelStatus channelStatus = new MerchantChannelStatus()
                .setMerchantCode(bizResponse.getString("outMerchantNo"))
                .setChannel(4);
        channelStatus = channelStatusService.getOne(new QueryWrapper<>(channelStatus));

        // 进件审核通过后，初审或复审通过, 发起电子合同请求
        if (MerchantChannelStatus.AUDIT_STATUS_FIRST_TRIAL_SUCCESS.equals(channelStatus.getStatus())
                || MerchantChannelStatus.AUDIT_STATUS_SUCCESS.equals(channelStatus.getStatus())) {
            reqProtocol(channelStatus);
        }
    }

    /**
     * 发起签约请求
     */
    private void reqProtocol(MerchantChannelStatus channelStatus) {
//        try {
//            val bizJson = new JSONObject();
//            bizJson.put("sysFlowId", channelStatus.getApplicationId());
//
//            val result = ysService.reqOldVerApiAudit(YsOldConfig.SEND_SMS_CODE, bizJson, "Merchant", "sendSmsCode");
//            val code = result.getString("code");
//            val note = result.getString("note");
//            if (!"00".equals(code)) {
//                log.warn("发送签约短信请求失败");
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            log.warn("发送签约短信请求失败");
//        }
    }
}
