package com.sckw.contract.task;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sckw.contract.dao.KwcContractLogisticsMapper;
import com.sckw.contract.dao.KwcContractTradeMapper;
import com.sckw.contract.model.entity.KwcContractLogistics;
import com.sckw.contract.model.entity.KwcContractTrade;
import com.sckw.contract.model.vo.req.ESignCallBackReqVo;
import com.sckw.contract.service.CommonBusinessService;
import com.sckw.core.model.enums.ContractStatusEnum;
import com.sckw.core.utils.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author czh
 * @desc 同步合同状态任务
 * @date 2023/8/25
 */
@Slf4j
@Component
@EnableScheduling
public class SynchronousContractStatusTask {

    @Autowired
    private KwcContractTradeMapper kwcContractTradeMapper;

    @Autowired
    private KwcContractLogisticsMapper kwcContractLogisticsMapper;

    @Autowired
    private CommonBusinessService commonBusinessService;

    @Value("${eSignDetail}")
    private String eSignDetail;

    @Scheduled(cron = "0/10 * * * * ?")
    private void process() {
        synchronousTrade();
        synchronousLogistics();
    }

    private void synchronousTrade() {
        LambdaQueryWrapper<KwcContractTrade> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNotNull(KwcContractTrade::getBusinessId).
                ne(KwcContractTrade::getStatus, ContractStatusEnum.SIGNED);
        List<KwcContractTrade> kwcContractTradeList = kwcContractTradeMapper.selectList(wrapper);
        if (CollectionUtils.isNotEmpty(kwcContractTradeList)) {
            Map<String, Object> map = new HashMap<>(2);
            for (KwcContractTrade kwcContractTrade : kwcContractTradeList) {
                String businessId = kwcContractTrade.getBusinessId();
                map.put("flowId", businessId);
                String res = HttpUtil.get(eSignDetail, map);
                Long id = kwcContractTrade.getId();
                log.info("合同{}  返参:{}", id, res);
                JSONObject jsonObject = JSON.parseObject(res);
                if (jsonObject.getBoolean("status")) {
                    JSONObject data = jsonObject.getJSONObject("data");
                    //合同签署状态 0 - 草稿  1 - 签署中 2 - 完成   3 - 撤销   5 - 过期（签署截至日期到期后触发）  - 拒签
                    Integer signFlowStatus = data.getInteger("signFlowStatus");
                    if (signFlowStatus.equals(2)) {
                        ESignCallBackReqVo eSignCallBackReqVo = new ESignCallBackReqVo();
                        eSignCallBackReqVo.setContractId(kwcContractTrade.getId());
                        eSignCallBackReqVo.setStatus(ContractStatusEnum.SIGNED.getCode());
                        eSignCallBackReqVo.setRemark("定时任务轮询更新");
                        commonBusinessService.approval(eSignCallBackReqVo);
                    }
                }
            }
        }
    }

    private void synchronousLogistics() {
        LambdaQueryWrapper<KwcContractLogistics> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNotNull(KwcContractLogistics::getBusinessId).
                ne(KwcContractLogistics::getStatus, ContractStatusEnum.SIGNED);
        List<KwcContractLogistics> kwcContractLogisticsList = kwcContractLogisticsMapper.selectList(wrapper);
        if (CollectionUtils.isNotEmpty(kwcContractLogisticsList)) {
            Map<String, Object> map = new HashMap<>(2);
            for (KwcContractLogistics kwcContractLogistics : kwcContractLogisticsList) {
                String businessId = kwcContractLogistics.getBusinessId();
                map.put("flowId", businessId);
                String res = HttpUtil.get(eSignDetail, map);
                Long id = kwcContractLogistics.getId();
                log.info("合同{}  返参:{}", id, res);
                JSONObject jsonObject = JSON.parseObject(res);
                if (jsonObject.getBoolean("status")) {
                    JSONObject data = jsonObject.getJSONObject("data");
                    //合同签署状态 0 - 草稿  1 - 签署中 2 - 完成   3 - 撤销   5 - 过期（签署截至日期到期后触发）  - 拒签
                    Integer signFlowStatus = data.getInteger("signFlowStatus");
                    if (signFlowStatus.equals(2)) {
                        ESignCallBackReqVo eSignCallBackReqVo = new ESignCallBackReqVo();
                        eSignCallBackReqVo.setContractId(kwcContractLogistics.getId());
                        eSignCallBackReqVo.setStatus(ContractStatusEnum.SIGNED.getCode());
                        eSignCallBackReqVo.setRemark("定时任务轮询更新");
                        commonBusinessService.approval(eSignCallBackReqVo);
                    }
                }
            }
        }
    }
}
