燕山石化溯源三维电子沙盘-【后端】-服务
13693261870
2023-10-31 a9f99d3f074472e1e16ec6109e5d819ca8dcf4d0
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package com.yssh.service;
 
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
 
import com.yssh.entity.*;
import com.yssh.mapper.*;
import com.yssh.utils.CalculateUtils;
import com.yssh.utils.DateUtils;
import com.yssh.utils.StringUtils;
import com.yssh.utils.TableStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
 
import com.google.common.collect.Lists;
 
import javax.annotation.Resource;
 
@Service
public class SuYuanService {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
 
    @Resource
    private SuYuanMapper suYuanMapper;
 
    @Resource
    private AsyncService asyncService;
 
    @Resource
    private DictRecordMapper dictRecordMapper;
 
    @Resource
    private CommonService commonService;
 
    @Resource
    private FeedbackMapper feedbackMapper;
 
    @Resource
    private QxshMapper qxshMapper;
 
    @Resource
    private LocationMapper locationMapper;
 
    @Resource
    private LocationService locService;
 
    private static HashMap<String, String> sites = new HashMap<>();
 
    //@Transactional
    @Async("threadPoolTaskExecutor")
    public void insertSuYuanDatasAsync(List<SuYuan> lists, String time) throws Exception {
        //插入数据
        List<List<SuYuan>> list = Lists.partition(lists, AsyncService.BATCH_INSERT_NUMBER);
        CountDownLatch countDownLatch = new CountDownLatch(list.size());
        for (List<SuYuan> corpReserveList : list) {
            asyncService.executeAsync(TableStrategy.getTableStrategy(time), corpReserveList, suYuanMapper, countDownLatch);
        }
        countDownLatch.await();
        logger.info(lists.size() + "条数据入库完成-----");
    }
 
    public void insertSuYuanDatas(List<SuYuan> lists, String time) {
        List<List<SuYuan>> subLists = Lists.partition(lists, AsyncService.BATCH_INSERT_NUMBER);
        for (List<SuYuan> sub : subLists) {
            suYuanMapper.batchInsert(TableStrategy.getTableStrategy(time), sub);
        }
 
        logger.info("------ " + time + ".csv," + lists.size() + " 条数据已入库 ------");
    }
 
    public Integer isTableExists(String tableName) {
        return suYuanMapper.isTableExists(tableName);
    }
 
    public int createNewTable(String tableName) {
        return suYuanMapper.createTable(tableName);
    }
 
    public List<SuYuan2d> selectSuYuan2d(Date date) {
        String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, date);
        List<String> ids2d = CalculateUtils.assembleId(commonService.getCheckPoints2d());
        DictRecord dictRecord = dictRecordMapper.selectByCreateTime(Long.parseLong(time));
        if (StringUtils.isNull(dictRecord)) {
            return null;
        }
        return suYuanMapper.get2d(dictRecord.getTableName(), ids2d);
    }
 
    public List<SuYuan3d> selectSuYuan3d(String name, Date date) {
        MonitorPointPosition checkPoint = commonService.select3dCheckPointByName(name);
        if (StringUtils.isNull(checkPoint)) {
            return null;
        }
        int range = 10;
        String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, date);
        List<String> ids3d = CalculateUtils.aloneCrosswiseExtend(checkPoint, range);
        DictRecord dictRecord = dictRecordMapper.selectByCreateTime(Long.parseLong(time));
        if (StringUtils.isNull(dictRecord)) {
            return null;
        }
        return suYuanMapper.get3d(dictRecord.getTableName(), ids3d);
    }
 
    public Map<String, Object> selectSuYuan100(String name, Date date) {
        Map<String, Object> result = new HashMap<>();
        MonitorPointPosition checkPoint = commonService.select3dCheckPointByName(name);
        if (StringUtils.isNull(checkPoint)) {
            return null;
        }
        int range = 10;
        String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, date);
        List<String> ids3d = CalculateUtils.aloneCrosswiseExtend(checkPoint, range);
        DictRecord dictRecord = dictRecordMapper.selectByCreateTime(Long.parseLong(time));
        if (StringUtils.isNull(dictRecord)) {
            return null;
        }
        if (suYuanMapper.isTableExists(dictRecord.getTableName()) == 0) return null;
 
        List<DistanceSuYuan> list = suYuanMapper.getDistanceSuYuan(dictRecord.getTableName(), ids3d);
        AtomicInteger i = new AtomicInteger(0);
        list.stream().forEach(s -> {
            s.setName(checkPoint.getName());
            i.getAndIncrement();
            s.setVocsName(checkPoint.getName() + "-" + i.longValue());
        });
        if (StringUtils.isNotEmpty(list)) {
            Collections.sort(list);
            DistanceSuYuan max = list.get(0);
            FeedbackDetail feedbackDetail = new FeedbackDetail(null, dictRecord.getTableName(),
                    max.getName(), max.getId(), max.getVocsName(), max.getVocsValue(), null, null, null, DateUtils.getNowDate());
            feedbackMapper.insert(feedbackDetail);
            result.put("feedbackId", feedbackDetail.getId());
        }
        result.put("data", list);
        return result;
    }
 
    public Map<String, Object> selectSuYuan200(String name, Date date) {
        Map<String, Object> result = new HashMap<String, Object>();
        MonitorPointPosition checkPoint = commonService.select3dCheckPointByName(name);
        if (StringUtils.isNull(checkPoint)) {
            return null;
        }
        int range = 20;
        String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, date);
        List<String> ids3d = CalculateUtils.aloneCrosswiseExtend(checkPoint, range);
        DictRecord dictRecord = dictRecordMapper.selectByCreateTime(Long.parseLong(time));
        if (StringUtils.isNull(dictRecord)) {
            return null;
        }
        if (suYuanMapper.isTableExists(dictRecord.getTableName()) == 0) return null;
 
        List<DistanceSuYuan> list = suYuanMapper.getDistanceSuYuan(dictRecord.getTableName(), ids3d);
        AtomicInteger i = new AtomicInteger(0);
        list.stream().forEach(s -> {
            s.setName(checkPoint.getName());
            i.getAndIncrement();
            s.setVocsName(checkPoint.getName() + "-" + i.longValue());
        });
        if (StringUtils.isNotEmpty(list)) {
            Collections.sort(list);
            DistanceSuYuan max = list.get(0);
            FeedbackDetail feedbackDetail = new FeedbackDetail(null, dictRecord.getTableName(),
                    max.getName(), max.getId(), max.getVocsName(), max.getVocsValue(), null, null, null, DateUtils.getNowDate());
            feedbackMapper.insert(feedbackDetail);
            result.put("feedbackId", feedbackDetail.getId());
        }
        result.put("data", list);
        return result;
    }
 
    public Map<String, Object> selectSuYuan300(String name, Date date) {
        Map<String, Object> result = new HashMap<String, Object>();
        MonitorPointPosition checkPoint = commonService.select3dCheckPointByName(name);
        if (StringUtils.isNull(checkPoint)) {
            return null;
        }
        int range = 30;
        String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, date);
        List<String> ids3d = CalculateUtils.aloneCrosswiseExtend(checkPoint, range);
        DictRecord dictRecord = dictRecordMapper.selectByCreateTime(Long.parseLong(time));
        if (StringUtils.isNull(dictRecord)) {
            return null;
        }
        if (suYuanMapper.isTableExists(dictRecord.getTableName()) == 0) return null;
 
        List<DistanceSuYuan> list = suYuanMapper.getDistanceSuYuan(dictRecord.getTableName(), ids3d);
        AtomicInteger i = new AtomicInteger(0);
        list.stream().forEach(s -> {
            s.setName(checkPoint.getName());
            i.getAndIncrement();
            s.setVocsName(checkPoint.getName() + "-" + i.longValue());
        });
        if (StringUtils.isNotEmpty(list)) {
            Collections.sort(list);
            DistanceSuYuan max = list.get(0);
            FeedbackDetail feedbackDetail = new FeedbackDetail(null, dictRecord.getTableName(),
                    max.getName(), max.getId(), max.getVocsName(), max.getVocsValue(), null, null, null, DateUtils.getNowDate());
            feedbackMapper.insert(feedbackDetail);
            result.put("feedbackId", feedbackDetail.getId());
        }
        result.put("data", list);
        return result;
    }
 
    public Map<String, Object> selectSuYuan500(String name, Date date) {
        Map<String, Object> result = new HashMap<>();
        MonitorPointPosition checkPoint = commonService.select3dCheckPointByName(name);
        if (StringUtils.isNull(checkPoint)) {
            return null;
        }
        int range = 50;
        String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, date);
        List<String> ids3d = CalculateUtils.aloneCrosswiseExtend(checkPoint, range);
        DictRecord dictRecord = dictRecordMapper.selectByCreateTime(Long.parseLong(time));
        if (StringUtils.isNull(dictRecord)) {
            return null;
        }
        if (suYuanMapper.isTableExists(dictRecord.getTableName()) == 0) return null;
 
        List<DistanceSuYuan> list = suYuanMapper.getDistanceSuYuan(dictRecord.getTableName(), ids3d);
        AtomicInteger i = new AtomicInteger(0);
        list.stream().forEach(s -> {
            s.setName(checkPoint.getName());
            i.getAndIncrement();
            s.setVocsName(checkPoint.getName() + "-" + i.longValue());
        });
        if (StringUtils.isNotEmpty(list)) {
            Collections.sort(list);
            DistanceSuYuan max = list.get(0);
            FeedbackDetail feedbackDetail = new FeedbackDetail(null, dictRecord.getTableName(),
                    max.getName(), max.getId(), max.getVocsName(), max.getVocsValue(), null, null, null, DateUtils.getNowDate());
            feedbackMapper.insert(feedbackDetail);
            result.put("feedbackId", feedbackDetail.getId());
        }
        result.put("data", list);
        return result;
    }
 
    public List<SuYuanMonitorData> getMonitorData(String name) {
        MonitorPointPosition checkPoint = commonService.select3dCheckPointByName(name);
        if (StringUtils.isNull(checkPoint)) {
            return null;
        }
 
        /*List<DictRecord> recordList = dictRecordMapper.selectDictRecordList(new DictRecord());
 
        List<String> tableNames = new ArrayList<String>();
        for (DictRecord dr : recordList) {
            if (suYuanMapper.isTableExists(dr.getTableName()) > 0) {
                tableNames.add(dr.getTableName());
            }
        }
 
        return suYuanMapper.getMonitorData(tableNames, checkPoint.getX() + "_" + checkPoint.getY() + "_" + checkPoint.getZ());*/
 
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        String end = DateUtils.getYyyyMmDdHh(calendar.getTime()); //dateFormat.format(calendar.getTime());
 
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.add(Calendar.DATE, -7);
        String start = DateUtils.getYyyyMmDdHh(calendar.getTime());
 
        return suYuanMapper.getNewMonitorData(checkPoint.getName(), start, end);
    }
 
    public int updateVocsName(Date date, String id, String vocsName) {
        String table = "su_yuan_" + DateUtils.getYyyyMmDdHh(date); // new Date()
        if (isTableExists(table) == 0) {
            return 0;
        }
 
        //return suYuanMapper.updateVocsName(table, id, vocsName);
        return 1;
    }
 
    public List<Qxsh> queryQxsh(Date date) {
        String time = DateUtils.getYyyyMmDdHh(date);
 
        return qxshMapper.selectByTime(time);
    }
 
    public List<List<Coordinate>> selectRects(Double[] x, Double[] y) {
        List<List<Coordinate>> list = new ArrayList<>();
        for (int i = 0; i < x.length; i++) {
            list.add(CalculateUtils.calcRect(x[i], y[i]));
        }
 
        return list;
    }
 
    public SuYuan700 selectSuYuan700ById(String id, Date date) {
        id = id.substring(0, id.lastIndexOf("_") + 1) + "0";
        String time = DateUtils.getYyyyMmDdHhMmSs(date).substring(0, 13) + ":00:00";
 
        return suYuanMapper.selectSuYuan700ById(id, time);
    }
 
    public SuYuan700 selectSuYuan46ById(String id, Date date) {
        id = id.substring(0, id.lastIndexOf("_") + 1) + "0";
        String time = DateUtils.getYyyyMmDdHhMmSs(date).substring(0, 13) + ":00:00";
 
        return suYuanMapper.selectSuYuan46ById(id, time);
    }
 
    public String selectAddrByXY(double x, double y) {
        // 1米=0.0000089932
        List<Location> locations = locationMapper.selectByXY(x, y);
        if (null != locations && locations.size() > 0) {
            return locations.get(0).getName();
        }
 
        /*locations = locService.selectVocAddrs(x, y);
        if (null != locations && locations.size() > 0) {
            return locations.get(0).getName();
        }*/
 
        return null;
    }
 
    public String selectSuYuanIdByName(String name) {
        if (sites.containsKey(name)) {
            return sites.get(name);
        }
 
        MonitorPointPosition mpp = commonService.select3dCheckPointByName(name);
        if (null == mpp) {
            return null;
        }
 
        String suYuanId = mpp.getId().substring(0, mpp.getId().lastIndexOf("_") + 1) + "0";
        if (!sites.containsKey(name)) {
            sites.put(name, suYuanId);
        }
 
        return suYuanId;
    }
}