package com.dy.pipIrrModel.modelCalculate; import com.dy.common.multiDataSource.DataSourceContext; import com.dy.common.util.DateTime; import com.dy.pipIrrGlobal.pojoMd.MdEt0; import com.dy.pipIrrGlobal.voMd.VoCrops; import com.dy.pipIrrGlobal.voPr.VoWeather; import com.dy.pipIrrGlobal.voRm.VoWeatherMaxMinTmp; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; /** * @Author: liurunyu * @Date: 2025/8/18 16:26 * @Description */ @Slf4j @Component(ModelCalculator.selfBeanName) @Scope("prototype") //采用原型模式,每次请求新建一个实例对象 public class ModelCalculator { public static final String selfBeanName = "modelCalculator"; private ModelCalculatorSv sv ; @Autowired public void setSv(ModelCalculatorSv sv){ this.sv = sv ; } public void execute(){ String orgTag = DataSourceContext.get() ; List crops = this.sv.selectAllCrops() ; if(crops != null && crops.size() > 0){ for (VoCrops crop : crops) { if(crop.weatherId != null){ try{ VoWeather voWeather = this.sv.getWeather(crop.weatherId); if(voWeather.lat != null){ this.executeOnCrop(crop, voWeather); } }catch (Exception e){ log.error("计算作物(id=" + crop.id + ")蒸腾数据时异常", e); } } } } orgTag = DataSourceContext.get() ; } private void executeOnCrop(VoCrops vo, VoWeather voWeather) throws Exception{ String yesterday_ymd = DateTime.yesterday_yyyy_MM_dd(Integer.parseInt(DateTime.yyyy()), Integer.parseInt(DateTime.MM()), Integer.parseInt(DateTime.dd())) ; //昨天 Double factor = this.getCropsFactor(vo, yesterday_ymd) ; if(factor != null){ //说明作物处于计算期(作物生长期)中 List tmps = this.sv.selectYesterdayMaxMinTemperature(vo.weatherId) ; if(tmps != null && tmps.size() > 0){ VoWeatherMaxMinTmp voMmTmp = tmps.get(0);//只能有一条记录 Double et0 = this.calculateEt0(yesterday_ymd, vo, voWeather, voMmTmp, factor) ;//计算蒸腾数据 Integer count = this.saveEt0(yesterday_ymd, vo, voWeather, voMmTmp, factor, et0) ; } } } private Double getCropsFactor(VoCrops vo, String yesterday) throws Exception{ Double factor = null ; if(vo.stopped != null && vo.stopped != 1){ if(vo.startDt != null && vo.endDt != null){ String start = DateTime.yyyy() + "-" + vo.startDt ; Long days = DateTime.daysBetweenyyyy_MM_dd(yesterday, start) ; if(days >= 0){ if(vo.life4Start != null && vo.life4End != null){ if(days >= vo.life4Start && days <= vo.life4End){ factor = vo.life4Factor ; } if(days > vo.life4End){ factor = null ; } } if(vo.life3Start != null && vo.life3End != null){ if(days >= vo.life3Start && days <= vo.life3End){ factor = vo.life3Factor ; } } if(vo.life2Start != null && vo.life2End != null){ if(days >= vo.life2Start && days <= vo.life2End){ factor = vo.life2Factor ; } } if(vo.life1Start != null && vo.life1End != null){ if(days >= vo.life1Start && days <= vo.life1End){ factor = vo.life1Factor ; } if(days < vo.life1Start){ factor = null ; } } }else{ factor = null ; } } } return factor ; } private Double calculateEt0(String yesterday_ymd, VoCrops vo, VoWeather voWeather, VoWeatherMaxMinTmp voMmTmp, Double factor) throws Exception{ Long days = DateTime.daysBetweenyyyy_MM_dd(yesterday_ymd, DateTime.yyyy() + "-01-01"); Integer dayIndex = days.intValue() + 1 ; Double fai = Hargreaves.rad(voWeather.lat); Double sunMagnetismAngular = Hargreaves.sunMagnetismAngular(dayIndex); Double sunEarthDistance = Hargreaves.sunEarthDistance(dayIndex); Double sunTimeAngular = Hargreaves.sunTimeAngular(fai, sunMagnetismAngular); Double zenithRadiation = Hargreaves.zenithRadiation(sunEarthDistance, sunTimeAngular, fai, sunMagnetismAngular); Double et0 = Hargreaves.ET0(factor, voMmTmp.maxAirTemperature, voMmTmp.minAirTemperature, zenithRadiation); return et0 ; } private Integer saveEt0(String yesterday_ymd, VoCrops vo, VoWeather voWeather, VoWeatherMaxMinTmp voMmTmp, Double factor, Double et0)throws Exception{ MdEt0 po = this.sv.selectByCropWeatherDt(vo.id, voWeather.id, yesterday_ymd); if(po == null){ Date yesterday = DateTime.dateFrom_yyyy_MM_dd(yesterday_ymd) ; return this.sv.saveEt0(vo.id, voWeather.id, yesterday, voMmTmp.maxAirTemperature, voMmTmp.minAirTemperature, factor, et0) ; } return null ; } }