package com.dy.pipIrrModel.timingTask; 
 | 
  
 | 
import com.dy.common.schedulerTask.SchedulerTaskSupport; 
 | 
import com.dy.pipIrrGlobal.util.Org; 
 | 
import com.dy.pipIrrGlobal.util.OrgListenerSupport; 
 | 
import lombok.extern.slf4j.Slf4j; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.beans.factory.annotation.Value; 
 | 
import org.springframework.boot.context.event.ApplicationReadyEvent; 
 | 
import org.springframework.context.ApplicationListener; 
 | 
import org.springframework.core.io.ResourceLoader; 
 | 
import org.springframework.lang.NonNull; 
 | 
import org.springframework.stereotype.Component; 
 | 
  
 | 
import java.util.HashMap; 
 | 
import java.util.List; 
 | 
  
 | 
/** 
 | 
 * @Author: liurunyu 
 | 
 * @Date: 2025/8/6 9:32 
 | 
 * @Description 
 | 
 * 作物蒸腾自动计算 
 | 
 */ 
 | 
@Slf4j 
 | 
@Component 
 | 
public class ModelListener extends OrgListenerSupport implements ApplicationListener<ApplicationReadyEvent> { 
 | 
  
 | 
    protected static final String orgKey = "tag" ; 
 | 
    private static final String JobName = "modelCalculateJob" ; 
 | 
    private static final String JobGroupName = "modelCalculateGroup" ; 
 | 
    private static final Integer ThreadPoolMaxCount = 1 ;//线程池线程最大个数 
 | 
    private static final Integer ThreadPoolPriority = 5 ;//线程优先级 
 | 
  
 | 
    /** 
 | 
     * 计算开始小时,必须0点或之后,见autoCalculateJob中计算,所用数据是昨天的 
 | 
     */ 
 | 
    @Value("${model-calculate.startHour: 0}") 
 | 
    protected Integer startHour;//计算开始时刻--小时 
 | 
  
 | 
    @Value("${model-calculate.startMinute: 10}") 
 | 
    protected Integer startMinute;//计算开始时刻--分钟 
 | 
  
 | 
    @Autowired 
 | 
    protected ResourceLoader resourceLoader ; 
 | 
  
 | 
    /** 
 | 
     * SpringBoot容器已经准备好了,执行下面方法 
 | 
     * @param event 事件 
 | 
     */ 
 | 
    @Override 
 | 
    public void onApplicationEvent(@NonNull ApplicationReadyEvent event) { 
 | 
        try { 
 | 
            //等1秒,等待com.alibaba.druid.pool.DruidDataSource初始化完成 
 | 
            Thread.sleep(1000L); 
 | 
        } catch (InterruptedException e) { 
 | 
            e.printStackTrace(); 
 | 
        }finally { 
 | 
            try{ 
 | 
                super.init(resourceLoader); 
 | 
                this.start(event); 
 | 
            }catch (Exception e){ 
 | 
                log.error("注册模型计算定时任务出错", e); 
 | 
            } 
 | 
  
 | 
        } 
 | 
    } 
 | 
    /** 
 | 
     * 初始化 
 | 
     */ 
 | 
    private void start(ApplicationReadyEvent event) throws Exception{ 
 | 
        if(startHour < 0 || startHour > 5){ 
 | 
            startHour = 0 ; 
 | 
        } 
 | 
        List<Org.OrgVo> orgList = Org.OrgList ; 
 | 
        if(orgList != null && orgList.size() >0){ 
 | 
            SchedulerTaskSupport.setThreadPoolPro(ThreadPoolMaxCount * orgList.size(), ThreadPoolPriority); 
 | 
            for(Org.OrgVo vo : orgList){ 
 | 
                HashMap<String , Object> jobDataMap    = new HashMap<String , Object>() ; 
 | 
                jobDataMap.put(orgKey, vo.tag) ; 
 | 
                SchedulerTaskSupport.addDailyJob(JobName + vo.tag, JobGroupName, CalculateJob.class, jobDataMap, startHour, startMinute ) ; 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
} 
 |