ws183
2025-04-28 b306d1106b915bb13fd7a02217ae9c65de2fd03d
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package com.ld.igds.protocol.grainplug;
 
import com.alibaba.fastjson.JSONObject;
import com.bstek.bdf2.core.model.DefaultDept;
import com.ld.igds.common.CoreCommonService;
import com.ld.igds.constant.Constant;
import com.ld.igds.io.notify.NotifyGrainInvoker;
import com.ld.igds.models.Depot;
import com.ld.igds.models.DepotConf;
import com.ld.igds.models.DicSysConf;
import com.ld.igds.models.Grain;
import com.ld.igds.protocol.grainplug.data.GrainRespData;
import com.ld.igds.sys.service.SysDeptService;
import com.ld.igds.view.service.HDepotService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
 
/**
 * 粮情解析
 *
 * @author chen
 */
@Slf4j
@Component(GrainPlugAnalysis.BEAN_ID)
public class GrainPlugAnalysis {
 
    /**
     * 匹配仓库编码
     */
    public static final String BEAN_ID = "plug.grainPlugAnalysis";
 
    public static Map<String, String> depotMap = new HashMap<String, String>();
 
    //中粮吉林粮情采集--仓库编码匹配
    static {
        depotMap.put("59", "0040");
        depotMap.put("60", "0041");
        depotMap.put("61", "0042");
        depotMap.put("62", "0043");
        depotMap.put("63", "0044");
        depotMap.put("45", "0045");
        depotMap.put("46", "0046");
        depotMap.put("47", "0047");
        depotMap.put("48", "0048");
        depotMap.put("49", "0049");
        depotMap.put("50", "0050");
        depotMap.put("51", "0051");
        depotMap.put("52", "0052");
        depotMap.put("53", "0053");
        depotMap.put("54", "0054");
        depotMap.put("55", "0055");
        depotMap.put("64", "0056");
        depotMap.put("65", "0057");
        depotMap.put("66", "0058");
    }
 
    @Autowired
    private CoreCommonService commonService;
    @Autowired
    private NotifyGrainInvoker notifyGrainInvoker;
    @Autowired
    private SysDeptService deptService;
    @Autowired
    private HDepotService depotService;
 
    /**
     * 粮情返回信息解析
     *
     * @param request
     */
    public String analysisGrain(JSONObject request) {
 
        GrainRespData grainRespData = JSONObject.parseObject(request.toString(), GrainRespData.class);
 
        if (grainRespData == null) {
            log.warn("粮情插件------>>>平台:粮情数据为空,无法进行解析");
            return null;
        }
 
        if (StringUtils.isEmpty(grainRespData.getDeptId())
                || StringUtils.isEmpty(grainRespData.getHouseId())) {
            log.warn("粮情插件----->>>平台:库区编码={},仓库ID={},粮情数据信息不全,报文无法进行解析", grainRespData.getDeptId(), grainRespData.getHouseId());
            return null;
        }
 
        //获取库区信息
        DefaultDept dept = deptService.getDeptById(grainRespData.getDeptId());
        if (dept == null) {
            log.warn("粮情插件----->>>平台:库区编码={},系统获取不到库区信息,报文无法进行解析", grainRespData.getDeptId());
            return null;
        }
 
        String depotId = depotMap.get(grainRespData.getHouseId());
        grainRespData.setHouseId(depotId);
        //获取仓库信息
        Depot depot = commonService.getCacheDepot(dept.getCompanyId(), depotId);
        if (depot == null) {
            log.warn("粮情插件----->>>平台:仓库ID={},系统获取不到仓库信息,报文无法进行解析", grainRespData.getHouseId());
            return null;
        }
 
        DicSysConf sysConf = commonService.getCacheSysConf(depot.getCompanyId());
        DepotConf depotConf = commonService.getCacheDepotConf(depot.getCompanyId(), depotId);
 
        //若仓库配置为空,则新增配置信息
        if (depotConf == null) {
            depotConf = new DepotConf();
            depotConf.setCompanyId(depot.getCompanyId());
            depotConf.setDepotId(depot.getId());
            depotService.saveConf(depotConf);
            depotService.flushConfCache(depot.getCompanyId());
        }
 
        //直接解析-保存
        doSave(grainRespData, depotConf, sysConf, depot);
        return null;
    }
 
    private void doSave(GrainRespData grainData, DepotConf depotConf, DicSysConf sysConf, Depot depot) {
        Grain grain = new Grain();
        //设置批次号
        grain.setBatchId(DateFormatUtils.format(grainData.getTime(), "yyyyMMddHHmm"));
        //采集时间
        grain.setReceiveDate(grainData.getTime());
        //层行列
        String cable = grainData.getLays() + "-" + grainData.getRows() + "-" + grainData.getCols();
        grain.setCable(cable);
        //设置筒仓规则
        if (StringUtils.isNotEmpty(grainData.getLayout()) && StringUtils.isNotEmpty(grainData.getLayerPerCircle())) {
            grain.setCable(grainData.getLayout());
            grain.setCableCir(grainData.getLayerPerCircle());
 
        }
        //仓库编码、组织编码、库区编码
        grain.setDepotId(depotConf.getDepotId());
        grain.setCompanyId(depot.getCompanyId());
        grain.setDeptId(grainData.getDeptId());
 
        //温湿度
        grain.setTempIn(grainData.getTIn());
        grain.setHumidityIn(grainData.getHIn());
        grain.setTempOut(grainData.getTOut());
        grain.setHumidityOut(grainData.getHOut());
        //粮均温
        grain.setTempAve(grainData.getTAvg());
 
        //设置粮情点位数据
        String pointsData = grainData.getPoints();
        if(null != depotConf.getStartPoint() &&Constant.GRAIN_START_POINT_BELOW.equals(depotConf.getStartPoint())){
            //如果粮情电缆开始点位从下面开始,则每根电缆上下翻转
            pointsData = reversalUpAndDown(pointsData, cable);
        }
        grain.setPoints(pointsData);
 
        String[] points = grainData.getPoints().split(",");
        //计算粮高温和粮低温
        Double tempMax = 0.0;
        Double tempMin = 50.0;
        Double temp;
        for (String point : points) {
            temp = Double.valueOf(point);
            if (temp > tempMax) {
                tempMax = temp;
            }
            if (temp != Constant.ERROR_TEMP && temp < tempMin) {
                tempMin = temp;
            }
        }
 
        grain.setTempMax(tempMax);
        grain.setTempMin(tempMin);
 
        //判断有没有超过高温
        if (null != depotConf.getTempMax() && grain.getTempMax() > depotConf.getTempMax()) {
            grain.setRemark("粮情检测有高温点");
        }
 
        // 用户封装好数据即可
        String result = notifyGrainInvoker.analysisSuccess(grain, depot, sysConf);
 
        if (null != result) {
            log.info("粮情插件----->>>平台:粮情解析完成,保存失败-仓库={},批次={},失败信息={}", depot.getName(), grain.getBatchId(), result);
        } else {
            log.info("粮情插件----->>>平台:粮情解析完成-仓库={},批次={}", depot.getName(), grain.getBatchId());
        }
    }
 
    /**
     * 电缆从下面开始时,将粮情电缆上下翻转
     *
     * @param pointsData 粮情数据
     * @param cable  层行列配置,如:4-7-11
     * @return
     */
    private String reversalUpAndDown(String pointsData, String cable){
        String str = "";
        if(StringUtils.isEmpty(cable)){
            str = pointsData;
        }
        String[] attCable = cable.split("-");
        int cableZ = Integer.valueOf(attCable[0]); //层
 
        String[] points = pointsData.split(",");
        for(int i = 0; i <= points.length - cableZ; i += cableZ){
            for(int j = cableZ -1; j >= 0; j --){
                str += points[i + j];
                str += ",";
            }
        }
        return str;
    }
 
}