liurunyu
2025-08-20 6ee52a5f212a52b4a0d41bff3cddc519ddb66997
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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<VoCrops> 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<VoWeatherMaxMinTmp> 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 ;
    }
}