CZT
2023-11-27 c206acfaedc69c390fb67daa81bc686f58a212ef
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.ld.igds.io.impl;
 
import com.ld.igds.constant.BizType;
import com.ld.igds.constant.Constant;
import com.ld.igds.constant.GrainFrequence;
import com.ld.igds.grain.dto.GrainItemInfo;
import com.ld.igds.grain.service.CoreGrainService;
import com.ld.igds.io.constant.OrderRespEnum;
import com.ld.igds.io.notify.NotifyGrainInvoker;
import com.ld.igds.models.*;
import com.ld.igds.order.ExeOrderService;
import com.ld.igds.order.data.ExeRequest;
import com.ld.igds.weather.CoreWeatherService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.List;
 
@Slf4j
@Component
public class NotifyGrainInvokerImpl extends NotifyWebParent implements NotifyGrainInvoker {
 
    @Autowired
    private CoreGrainService grainService;
    @Autowired
    private CoreWeatherService weatherService;
    @Autowired
    private ExeOrderService exeRequestService;
 
    @Override
    public String analysisSuccess(Grain grain, List<GrainItemInfo> listGrainItems, DepotConf depotConf, DicSysConf sysConf, boolean isNotifyWeb, ExeRequest exeRequest) {
        grain = addWeatherInfo(grain);
        try {
            // 如果是一日多次
            if (null == depotConf) {
                grainService.saveGrain(grain);
            } else {
                if (depotConf.getGrainFreq().equals(
                        GrainFrequence.FREQ_01.getCode())) {
                    grainService.saveGrain(grain);
                } else {
                    grainService.saveOrUpdateGrain(grain);
                }
            }
            String notifyMsg = depotConf.getDepotName() + " 粮情采集并解析成功,批次号="
                    + grain.getBatchId();
 
            //完成
            exeRequest.setMsg(notifyMsg);
            exeRequestService.completeCache(exeRequest, false);
 
            notifyWeb(depotConf.getCompanyId(), OrderRespEnum.MSG_SUCCESS, BizType.GRAIN, notifyMsg);
 
            log.info(depotConf.getDepotName() + "粮情解析成功:" + grain.getBatchId());
 
            // 采集点保存,采集点的异常信息不考虑在粮情内部,直接忽略
            if (StringUtils.isNotEmpty(sysConf.getGrainAddPoint())
                    && Constant.YN_Y.equals(sysConf.getGrainAddPoint())) {
                grainService.addGrainItems(listGrainItems, grain);
 
            }
        } catch (Exception e) {
            String notifyMsg = depotConf.getDepotName() + "-保存失败,请勿2分钟内重复采集";
 
            //完成
            exeRequest.setMsg(notifyMsg);
            exeRequestService.completeCache(exeRequest, true);
 
            notifyWeb(depotConf.getCompanyId(), OrderRespEnum.MSG_ERROR, BizType.GRAIN, notifyMsg);
 
            log.error(depotConf.getDepotName() + "粮情解析保存失败:" + e);
        }
        return null;
    }
 
    @Override
    public String analysisSuccess(Grain grain, Depot depot, DicSysConf sysConf) {
        grain = addWeatherInfo(grain);
 
        try {
            grainService.saveOrUpdateGrain(grain);
 
            String notifyMsg = depot.getName() + " 收到推送粮情信息并解析成功,批次号=" + grain.getBatchId();
 
            notifyWeb(grain.getCompanyId(), OrderRespEnum.MSG_SUCCESS, BizType.GRAIN, notifyMsg);
 
        } catch (Exception e) {
            String notifyMsg = depot.getName() + "-保存失败,请勿2分钟内重复采集";
            this.notifyWeb(grain.getCompanyId(), OrderRespEnum.MSG_ERROR, BizType.GRAIN, notifyMsg);
 
            log.error("粮情解析保存失败:" + e);
            return notifyMsg;
        }
        return null;
    }
 
    @Override
    public String analysisSuccess(Grain grain, ExeRequest exeRequest, DicSysConf sysConf) {
 
        grain = addWeatherInfo(grain);
        try {
            grainService.saveOrUpdateGrain(grain);
 
            String notifyMsg = exeRequest.getDepotName() + " 粮情采集并解析成功,批次号=" + grain.getBatchId();
 
            //完成
            exeRequest.setMsg(notifyMsg);
            exeRequestService.completeCache(exeRequest, false);
 
        } catch (Exception e) {
            String notifyMsg = exeRequest.getDepotName() + "-保存失败,请勿2分钟内重复采集";
 
            //完成
            exeRequest.setMsg(notifyMsg);
            exeRequestService.completeCache(exeRequest, true);
 
            return notifyMsg;
        }
        return null;
    }
 
    @Override
    public String analysisSuccess(Grain grain, ExeRequest exeRequest, DepotConf depotConf, DicSysConf sysConf) {
 
        grain = addWeatherInfo(grain);
        try {
            grainService.saveOrUpdateGrain(grain);
 
            String notifyMsg = exeRequest.getDepotName() + " 粮情采集并解析成功,批次号=" + grain.getBatchId();
 
            //完成
            exeRequest.setMsg(notifyMsg);
            exeRequestService.completeCache(exeRequest, false);
 
            notifyWeb(depotConf.getCompanyId(), OrderRespEnum.MSG_SUCCESS, BizType.GRAIN, notifyMsg);
            log.info(depotConf.getDepotName() + "粮情解析成功:" + grain.getBatchId());
        } catch (Exception e) {
            String notifyMsg = exeRequest.getDepotName() + "-保存失败,请勿2分钟内重复采集";
 
            //完成
            exeRequest.setMsg(notifyMsg);
            exeRequestService.completeCache(exeRequest, true);
 
            notifyWeb(depotConf.getCompanyId(), OrderRespEnum.MSG_ERROR, BizType.GRAIN, notifyMsg);
 
            log.error(depotConf.getDepotName() + "粮情解析保存失败:" + e);
            return notifyMsg;
        }
        return null;
    }
 
 
    // 从缓存中获取当前的外温度外部湿度
    private Grain addWeatherInfo(Grain grain) {
        if(grain.getHumidityOut() != null && grain.getTempOut() != null){
            return grain;
        }
        WeatherInfo weatherInfo = weatherService.getCacheWeather(grain.getCompanyId());
        Double tOut = -100.0, hOut = -100.0;
        if (null != weatherInfo) {
            if (weatherInfo.getTemp().indexOf("℃") > 0) {
                weatherInfo.setTemp(weatherInfo.getTemp().replace("℃", ""));
            }
            if (weatherInfo.getHumidity().indexOf("%") > 0) {
                weatherInfo.setHumidity(weatherInfo.getHumidity().replace("%",
                        ""));
            }
            tOut = Double.valueOf(weatherInfo.getTemp());
            hOut = Double.valueOf(weatherInfo.getHumidity());
            grain.setWeather(weatherInfo.getWeather());
        }
        if(grain.getHumidityOut() == null || grain.getHumidityOut() == 0.0){
            grain.setHumidityOut(hOut);
        }
        if(grain.getTempOut() == null || grain.getTempOut() == 0.0){
            grain.setTempOut(tOut);
        }
        return grain;
    }
 
}