CZT
2023-10-09 40b614432756a9dbaf99cda39f5fae19c05d7031
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package com.fzzy.async.fzzy40.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.bstek.dorado.util.DateUtils;
import com.fzzy.api.Constant;
import com.fzzy.api.entity.Api1105;
import com.fzzy.api.entity.Api1302;
import com.fzzy.api.entity.ApiLog;
import com.fzzy.api.service.ApiCommonService;
import com.fzzy.api.utils.ContextUtil;
import com.fzzy.api.view.repository.Api1302Rep;
import com.fzzy.api.view.repository.ApiLogRep;
import com.fzzy.async.fzzy40.entity.Fz40Grain;
import com.fzzy.async.fzzy40.repository.Fzzy40Sync1302Rep;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.Date;
import java.util.List;
 
 
/**
 * 温湿度检测数据同步
 *
 * @author czt
 * @date 2022-09-07 14:41
 */
@Slf4j
@Component
public class Fzzy40Sync1302 {
 
    @Autowired
    private Fzzy40Sync1302Rep fzzySync1302Rep;
    @Autowired
    private ApiCommonService commonService;
    @Autowired
    private Api1302Rep api1302Rep;
    @Autowired
    private ApiLogRep apiLogRep;
 
 
    /**
     * 同步并封装保存温湿度检测数据
     *
     * @param deptId 系统对应库区编码
     * @param start  起始时间
     * @param end    截止时间
     */
    public void syncData(String kqdm, String deptId, Date start, Date end) {
 
        log.info("-------------1302接口数据开始同步------------------");
        //同步数据,只记录失败的信息
        ApiLog apiLog = new ApiLog();
        apiLog.setType(ApiLog.TYPE_SYNC);
        apiLog.setKqdm(deptId);
        apiLog.setUploadTime(new Date());
        apiLog.setInteId(Constant.API_CODE_1302);
        apiLog.setStatus(99);
        apiLog.setId(ContextUtil.getUUID());
        try {
            List<Fz40Grain> list = fzzySync1302Rep.findByReceiveDate(start, end);
            log.info(DateUtils.format("yyyy-MM-dd HH:mm:ss", start));
            log.info(DateUtils.format("yyyy-MM-dd HH:mm:ss", end));
            if (null == list || list.isEmpty()) {
                log.info("1302---同步数据:没有查询到需要同步的数据");
                return;
            }
            log.info(JSONObject.toJSONString(list));
            Date syncTime = new Date();
            Api1302 api1302;
            Api1105 api1105;
            List<Api1302> api1302List;
            for (Fz40Grain fz40Grain : list) {
                //获取货位信息
                api1105 = commonService.getApi1105Cache(fz40Grain.getDepotId());
                if (null == api1105) {
                    continue;
                }
 
                //由货位代码+检测日期     (yyyyMMdd) +4 位顺序号组成  --2019 1128 0819
                api1302 = new Api1302();
                api1302.setWsdjcdh(api1105.getHwdm() + fz40Grain.getBatchId());
                api1302.setJcsj(fz40Grain.getReceiveDate());
                api1302.setHwdm(api1105.getHwdm());
 
                api1302.setCfww(fz40Grain.getTempOut() == null ? 0.00 : fz40Grain.getTempOut());
                if (null == fz40Grain.getHumidityOut() || fz40Grain.getHumidityOut() < 0) {
                    api1302.setCfws(-1);
                } else {
                    api1302.setCfws(fz40Grain.getHumidityOut());
                }
 
                api1302.setCfnw(fz40Grain.getTempIn() == null ? 0.00 : fz40Grain.getTempIn());
                if (null == fz40Grain.getHumidityIn() || fz40Grain.getHumidityIn() < 0) {
                    api1302.setCfns(-1);
                } else {
                    api1302.setCfns(fz40Grain.getHumidityIn());
                }
 
                api1302.setLszgw(fz40Grain.getTempMax() == null ? 0.00 : fz40Grain.getTempMax());
                api1302.setLspjw(fz40Grain.getTempAve() == null ? 0.00 : fz40Grain.getTempAve());
                api1302.setLszdw(fz40Grain.getTempMin() == null ? 0.00 : fz40Grain.getTempMin());
 
                //判断粮食最低温、平均温、最高温是否符合逻辑
                if(api1302.getLszdw() >= api1302.getLspjw()){
                    continue;
                }
                if(api1302.getLspjw() >= api1302.getLszgw()){
                    continue;
                }
 
                //温度集合
                if (StringUtils.isEmpty(fz40Grain.getCableCir())) {
                    api1302.setLswdzjh(getTempPointList1(fz40Grain.getPoints(), fz40Grain.getCable()));
                    api1302.setLssdzjh(getHumPointList1(fz40Grain.getPoints(), fz40Grain.getCable()));
                } else {
                    api1302.setLswdzjh(getTempPointList2(fz40Grain.getPoints(), fz40Grain.getCable(), fz40Grain.getCableCir()));
                    api1302.setLssdzjh(getHumPointList2(fz40Grain.getPoints(), fz40Grain.getCable(), fz40Grain.getCableCir()));
                }
 
                api1302.setZhgxsj(fz40Grain.getReceiveDate());
 
                api1302.setBizId(fz40Grain.getBatchId());
                api1302.setKqdm(api1105.getKqdm());
                api1302.setSyncTime(syncTime);
                api1302List = api1302Rep.getDataByWsdjcdh(api1302.getWsdjcdh());
                if (null == api1302List || api1302List.isEmpty()) {
                    api1302.setCzbz(Constant.CZBZ_I);
                } else {
                    api1302.setCzbz(api1302List.get(0).getCzbz());
                }
                log.info("1302---同步数据:" + api1302.toString());
                api1302Rep.save(api1302);
            }
 
        } catch (Exception e) {
            log.error("---同步失败----{}", e);
            apiLog.setResult("同步失败:" + e.getMessage());
            apiLogRep.save(apiLog);
        }
 
    }
 
    /**
     * 获取平方仓的温度值集合,格式:温度值,层数,行数,列数|温度值,层数,行数,列数|温度值,层数,行数,列数
     *
     * @return
     */
    private String getTempPointList1(String point, String cable) {
        String[] cab = cable.split("-");
        int c = Integer.valueOf(cab[0]);
        int h = Integer.valueOf(cab[1]);
        int l = Integer.valueOf(cab[2]);
 
        String[] points = point.split(",");
        String result = "";
        for (int i = 1; i <= l; i++) {
            for (int j = 1; j <= h; j++) {
                for (int z = 1; z <= c; z++) {
                    result += ("|" + points[(i * j - 1) * c + z - 1] + "," + z + "," + j + "," + i);
                }
            }
        }
        return result.substring(1);
    }
 
    /**
     * 获取筒仓的温度值集合,格式:温度值,圈数,点数,层数|温度值,圈数,点数,层数|温度值,圈数,点数,层数
     *
     * @return
     */
    private String getTempPointList2(String point, String cable, String cableCir) {
        String[] cab = cable.split("-");
        String[] cabCir = cableCir.split("-");
 
        int q = cab.length;  //圈数
        int c = Integer.valueOf(cabCir[0]);  //层数
        int g = 0;  //圈根数
        int g1 = 1;  //圈起始根数
        int g2 = 0;  //圈截止根数
 
        String[] points = point.split(",");
        String result = "";
        for (int i = 1; i <= q; i++) {
            g1 += g2;
            g = Integer.valueOf(cab[i - 1]);
            g2 += g;
            for (int j = g1; j <= g2; j++) {
                for (int z = 1; z <= c; z++) {
                    result += ("|" + points[(j - 1) * c + z - 1] + "," + i + "," + j + "," + z);
                }
            }
 
        }
        return result.substring(1);
    }
 
    /**
     * 获取平方仓的湿度值集合,格式:湿度,层数,行数,列数|湿度,层数,行数,列数|湿度,层数,行数,列数
     *
     * @return
     */
    private String getHumPointList1(String point, String cable) {
        String[] cab = cable.split("-");
        int c = Integer.valueOf(cab[0]);
        int h = Integer.valueOf(cab[1]);
        int l = Integer.valueOf(cab[2]);
 
        // String[] points = point.split(",");
        String result = "";
        for (int i = 1; i <= l; i++) {
            for (int j = 1; j <= h; j++) {
                for (int z = 1; z <= c; z++) {
                    result += ("|" + "-1" + "," + z + "," + j + "," + i);
                }
            }
        }
        return result.substring(1);
    }
 
    /**
     * 获取筒仓的湿度值集合,格式:湿度,圈数,点数,层数|湿度,圈数,点数,层数|湿度,圈数,点数,层数
     *
     * @return
     */
    private String getHumPointList2(String point, String cable, String cableCir) {
        String[] cab = cable.split("-");
        String[] cabCir = cableCir.split("-");
 
        int q = cab.length;  //圈数
        int c = Integer.valueOf(cabCir[0]);  //层数
        int g = 0;  //圈根数
        int g1 = 1;  //圈起始根数
        int g2 = 0;  //圈截止根数
 
        //  String[] points = point.split(",");
        String result = "";
        for (int i = 1; i <= q; i++) {
            g1 += g2;
            g = Integer.valueOf(cab[i - 1]);
            g2 += g;
            for (int j = g1; j <= g2; j++) {
                for (int z = 1; z <= c; z++) {
                    result += ("|" + "-1" + "," + i + "," + j + "," + z);
                }
            }
 
        }
        return result.substring(1);
    }
}