package com.yunfeiyun.agmp.fms.scheduler;

import com.yunfeiyun.agmp.common.enums.WarnStatus;
import com.yunfeiyun.agmp.common.utils.DateUtils;
import com.yunfeiyun.agmp.fms.domain.FmsWarn;
import com.yunfeiyun.agmp.fms.mapper.FmsPlanMapper;
import com.yunfeiyun.agmp.fms.mapper.FmsWarnMapper;
import com.yunfeiyun.agmp.fms.domain.dto.FmsPlanByWarnSchedulerDto;
import com.yunfeiyun.agmp.fms.domain.dto.FmsWarnBySchedulerDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.*;

@Slf4j
@Component
public class FmsWarnScheduler {
    @Autowired
    private FmsWarnMapper fmsWarnMapper;

    @Autowired
    private FmsPlanMapper fmsPlanMapper;

    @Transactional(rollbackFor = Exception.class)
    private void warnCreate(List<FmsWarn> fmsWarnList){
        if (fmsWarnList.size() > 0){
            this.fmsWarnMapper.insertFmsWarnBatch(fmsWarnList);
        }
    }
    /**
     * 创建农事预警定时任务
     */
    @Scheduled(cron = "0 1 */1 * * ?")
    public void fmsWarnCreate() throws Exception{
        log.info("创建农事预警定时任务开始");

        Set<String> warnKeySet = new HashSet<>();
        List<FmsWarnBySchedulerDto> fmsWarnListBySchedulerDtos = this.fmsWarnMapper.selectFmsWarnListByScheduler();
        for (FmsWarnBySchedulerDto fmsWarnListBySchedulerDto: fmsWarnListBySchedulerDtos){
            System.out.println(fmsWarnListBySchedulerDto);
            if (ObjectUtils.isEmpty(fmsWarnListBySchedulerDto)){
                continue;
            }
            String key = fmsWarnListBySchedulerDto.getPlanId() + fmsWarnListBySchedulerDto.getPeriodId();
            warnKeySet.add(key);
        }
        String planEnddate = DateUtils.dateTimeNow();
        Date nowDate = DateUtils.parseDate(DateUtils.dateNow());

        List<FmsPlanByWarnSchedulerDto> fmsPlanByWarnSchedulerDtos = this.fmsPlanMapper.selectPlanListByWarnScheduler(planEnddate);;
        HashMap<String, Date> dateHashMap = new HashMap<>();

        List<FmsWarn> waitList = new ArrayList<>();
        for (FmsPlanByWarnSchedulerDto fmsPlanByWarnSchedulerDto: fmsPlanByWarnSchedulerDtos){
            String planId = fmsPlanByWarnSchedulerDto.getPlanId();
            if (!dateHashMap.containsKey(planId)){
                dateHashMap.put(planId, DateUtils.parseDate(fmsPlanByWarnSchedulerDto.getPlanPlanstartdate()));
            }
            Date periodStartdate = dateHashMap.get(planId);
            Date periodEnddate = DateUtils.addDays(periodStartdate, fmsPlanByWarnSchedulerDto.getPeriodDays());
            dateHashMap.put(planId, periodEnddate);

            if ((nowDate.compareTo(periodStartdate) >= 0) && (nowDate.compareTo(periodEnddate) <= 0)){
                String warnKey = planId + fmsPlanByWarnSchedulerDto.getPeriodId();
                if ( !warnKeySet.contains(warnKey)){
                    FmsWarn fmsWarn = new FmsWarn();
                    fmsWarn.setWarnId(fmsWarn.getId());
                    fmsWarn.setPlanId(fmsPlanByWarnSchedulerDto.getPlanId());
                    fmsWarn.setPeriodId(fmsPlanByWarnSchedulerDto.getPeriodId());
                    fmsWarn.setWarnTitle(fmsPlanByWarnSchedulerDto.getPeriodName() + "预警");
                    fmsWarn.setWarnContent(fmsPlanByWarnSchedulerDto.getPeriodRemark());
                    fmsWarn.setWarnCreatedate(planEnddate);
                    fmsWarn.setWarnHandlestatus(WarnStatus.PENDING.getCode());
                    waitList.add(fmsWarn);
                }
            }
        }
        if (waitList.size() > 0){
            this.warnCreate(waitList);
        }
        log.info("创建农事预警定时任务结束");
    }
}
