package com.hfkj.schedule;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.channel.tianque.service.TianQueLedgerService;
import com.hfkj.entity.*;
import com.hfkj.service.BsMerLedgerService;
import com.hfkj.sysenum.*;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className: MerLedgerSchedule
 * @author: HuRui
 * @date: 2023/9/25
 **/
@Configuration
public class MerLedgerSchedule {

    private static final Logger log = LoggerFactory.getLogger(MerLedgerSchedule.class);

    @Resource
    private BsMerLedgerService merLedgerService;
    @Resource
    private TianQueLedgerService tianQueLedgerService;

    /**
     * 审批状态查询
     */
    @Scheduled(cron="0 0/1 * * * ?")   //每1分钟执行一次
    public void merLedgerApplyment() {
        Map<String, Object> param = new HashMap<>();
        param.put("status", MerLedgerStatusEnum.status2.getNumber());
        // 查询审核中的数据
        List<BsMerLedger> merLedgerList = merLedgerService.getMerLedgerList(param);
        for (BsMerLedger merLedger : merLedgerList) {
            try {
                if (merLedger.getPlatformType().equals(PlatformTypeEnum.type5.getNumber())) {
                    // 查询签约结果
                    JSONObject signContract = tianQueLedgerService.querySignContract(merLedger.getCupNo());
                    // 签约状态，枚举值  00：已发送链接，未签约 01：签约失败  02：签约成功  03：未签约  04：审批中
                    if (signContract.getString("signResult").equals("01")) {
                        merLedger.setStatus(MerLedgerStatusEnum.status3.getNumber());
                        merLedger.setRejectReason("签约失败");
                        merLedgerService.editMerLedger(merLedger);

                    } else if (signContract.getString("signResult").equals("02")) {
                        merLedger.setStatus(MerLedgerStatusEnum.status1.getNumber());
                        merLedgerService.editMerLedger(merLedger);
                    }
                }
            } catch (Exception e) {

            }
        }
    }

}
