cjl
2023-11-02 a0a6f95ec5b92be03385eea83b64bf97fcbfa17e
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package com.moral.api.service.impl;
 
 
import com.moral.api.exception.BusinessException;
import com.moral.api.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.Serializable;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 
import com.moral.api.entity.Dustld;
import com.moral.api.entity.ManageCoordinate;
import com.moral.api.entity.MaxRoad;
import com.moral.api.pojo.dto.historySecondCruiser.DustldDTO;
import com.moral.api.pojo.dust.DustForm;
import com.moral.api.pojo.dust.TimeForm;
import com.moral.api.service.DustldService;
import com.moral.util.DateUtils;
import com.moral.util.FileUtils;
import com.moral.util.TokenUtils;
 
@Service
@Slf4j
public class DustldServiceImpl implements DustldService {
 
 
    @Autowired
    private HistorySecondCruiserMapper  historySecondCruiserMapper;
 
    @Autowired
    private DustldMapper dustldMapper;
 
    /**
     * 保存日报信息
     * @param params
     * @param file
     * @return
     * @throws ParseException
     */
    @Override
    public Integer getDailyDustld(Map<String, Object> params,MultipartFile file ) {
 
        //获取jar包所在目录
        ApplicationHome applicationHome = new ApplicationHome(getClass());
        //在jar包所在目录下生成一个upload文件夹用来存储上传的图片
        String path = applicationHome.getSource().getParentFile().toString() + "/static/img";
 
 
        String mac = params.get("mac").toString();
        String time3 = params.get("time3").toString();
        String time4 = params.get("time4").toString();
        String table = params.get("table").toString();
        String substring = time3.substring(0, 10);
        //路段名
        String road = params.get("road").toString();
 
        ArrayList<Double> list = new ArrayList<>();
        List<TimeForm> tables = JSONArray.parseArray(table, TimeForm.class);
 
        if (!ObjectUtils.isEmpty(tables)){
            for (TimeForm timeForm : tables) {
                String start = timeForm.getStart();
                String end = timeForm.getEnd();
                List<String> dust = historySecondCruiserMapper.getDust(start, end, mac);
                ArrayList<Double> rsDouble = new ArrayList<>();
                for (String s : dust) {
                    double aDouble = Double.parseDouble(s);
 
                    if (aDouble>=0 && aDouble<40 ){
                        aDouble = aDouble + 170;
                    }else if (aDouble>=40 && aDouble<60){
                        aDouble = aDouble + 130;
                    }else if (aDouble>=60 && aDouble<100 ){
                        aDouble = aDouble + 110;
                    }else if (aDouble>=100 && aDouble<150){
                        aDouble = aDouble + 70;
                    }else if (aDouble>=150 && aDouble<180){
                        aDouble = aDouble + 30;
                    }else {
                        aDouble= aDouble+0;
                    }
                    rsDouble.add(aDouble);
                }
                list.addAll(rsDouble);
            }
        }
        String format = "";
        if (!ObjectUtils.isEmpty(list)){
            Double collect = (list.stream().collect(Collectors.averagingDouble(Double::doubleValue)))/1000;
            format = new DecimalFormat("0.000").format(collect);
        }
 
        QueryWrapper<Dustld> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mac",mac).likeRight("time",substring);
        Dustld dustld = dustldMapper.selectOne(queryWrapper);
 
       if (ObjectUtils.isEmpty(dustld)){
           Dustld dustld1 = new Dustld();
           if (!ObjectUtils.isEmpty(file)){
               HashMap<String, Object> hashMap = new HashMap<>();
               String fileType = file.getContentType();
               if ("image/jpg".equals(fileType) || "image/png".equals(fileType) || "image/jpeg".equals(fileType)){
                   //获取文件名
                   String fileName = file.getOriginalFilename();
                   //获取文件后缀名
                   String suffixName = fileName.substring(fileName.lastIndexOf("."));
                   //每一次都需要
                   fileName.substring(fileName.lastIndexOf("."));
                   //重新生成文件名
                   fileName = UUID.randomUUID() + suffixName;
                   //图片上传
                   if (FileUtils.upload(file, path, fileName)) {
                       hashMap.put("file1",fileName);
                       String s = JSON.toJSONString(hashMap);
                       dustld1.setImages(s);
                   }
               }
           }
 
           HashMap<String, Object> map = new HashMap<>();
 
           map.put(road,format);
           String value = JSON.toJSONString(map);
           dustld1.setMac(mac);
           dustld1.setTime(DateUtils.getDate(time3,DateUtils.yyyy_MM_dd_HH_mm_ss_EN));
           dustld1.setValue(value);
           dustld1.setEndTime(DateUtils.getDate(time4,DateUtils.yyyy_MM_dd_HH_mm_ss_EN));
           dustldMapper.insert(dustld1);
           return 200;
       }else {
           if (!ObjectUtils.isEmpty(file)){
               HashMap<String, Object> hashMap = new HashMap<>();
               String fileType = file.getContentType();
               if ("image/jpg".equals(fileType) || "image/png".equals(fileType) || "image/jpeg".equals(fileType)){
                   //获取文件名
                   String fileName = file.getOriginalFilename();
                   //获取文件后缀名
                   String suffixName = fileName.substring(fileName.lastIndexOf("."));
                   //每一次都需要
                   fileName.substring(fileName.lastIndexOf("."));
                   //重新生成文件名
                   fileName = UUID.randomUUID() + suffixName;
                   //图片上传
                   if (FileUtils.upload(file, path, fileName)) {
                       hashMap.put("file1",fileName);
                       String s = JSON.toJSONString(hashMap);
                       dustld.setImages(s);
                   }
               }
           }
           String value = dustld.getValue();
           JSONObject jsonObject = JSONObject.parseObject(value);
           jsonObject.put(road,format);
           String rsValue = JSONObject.toJSONString(jsonObject);
           dustld.setValue(rsValue);
           dustldMapper.updateById(dustld);
           return 200;
       }
 
    }
 
    /**
     * 下载日报
     * @param id
     * @return
     */
    @Override
    public Map<String,Object> dailyDustld(Integer id,List<MultipartFile> files) {
        //获取jar包所在目录
        ApplicationHome applicationHome = new ApplicationHome(getClass());
        //在jar包所在目录下生成一个upload文件夹用来存储上传的图片
        String path = applicationHome.getSource().getParentFile().toString() + "/static/img";
 
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<DustForm> list1 = new ArrayList<>();
        ArrayList<DustForm> list2= new ArrayList<>();
 
        Dustld dustld = dustldMapper.selectById(id);
        if (ObjectUtils.isEmpty(dustld)){
            return null;
        }
        String pathList = getList(path, files);
            String images = dustld.getImages();
            if (ObjectUtils.isEmpty(images)){
                HashMap<String, Object> map1 = new HashMap<>();
                map1.put("file1","");
                if (pathList==null){
                    map1.put("file2","");
                }else {
                    map1.put("file2",pathList);
                }
 
                String s = JSON.toJSONString(map1);
                dustld.setImages(s);
                dustldMapper.updateById(dustld);
            }else {
                JSONObject jsonObject = JSONObject.parseObject(images);
                if (ObjectUtils.isEmpty(pathList)){
                    jsonObject.put("file2","");
                }else {
                    jsonObject.put("file2",pathList);
                }
                String s = JSON.toJSONString(jsonObject);
                dustld.setImages(s);
                dustldMapper.updateById(dustld);
            }
 
 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
        String stsrtTime = sdf.format(dustld.getTime());
        String substring = stsrtTime.substring(5, 14);
        String endTime = sdf.format(dustld.getEndTime());
        String substring1 = endTime.substring(5, 14);
 
        String value = dustld.getValue();
        JSONObject jsonObject = JSONObject.parseObject(value);
        Iterator<Map.Entry<String, Object>> iterator = jsonObject.entrySet().iterator();
        while (iterator.hasNext()) {
            DustForm dustForm = new DustForm();
            Map.Entry entry = (Map.Entry) iterator.next();
            dustForm.setRoad(entry.getKey().toString());
            dustForm.setValue(Double.parseDouble(entry.getValue().toString()));
            double aDouble = Double.parseDouble(entry.getValue().toString());
            list2.add(dustForm);
            if (aDouble>=0.3){
                list1.add(dustForm);
            }
        }
 
        //排序
        list1.sort(Comparator.comparing(DustForm::getValue).reversed());
        list2.sort(Comparator.comparing(DustForm::getValue).reversed());
        map.put("list1",list1);
        map.put("list2",list2);
        map.put("time",substring+"-"+substring1);
        map.put("images",JSON.parseObject(dustld.getImages()));
        return map;
    }
 
    @Autowired
    private ManageCoordinateMapper manageCoordinateMapper;
    @Autowired
    private MaxRoadMapper maxRoadMapper;
    @Autowired
    private OrganizationMapper organizationMapper;
 
    @Override
    @Transactional
    public Map<String, Object> dailyDustlds(Map<String, Object> params) {
 
        Map<String, Object> userInfo = (Map<String, Object>) TokenUtils.getUserInfo();
        Map<String, Object> orgInfo = (Map<String, Object>) userInfo.get("organization");
        Integer orgId = (Integer) orgInfo.get("id");
        String mac = params.get("mac").toString();
        List<Integer> orgList = organizationMapper.orgIdSpecialDevList(orgId,mac);
        if(CollectionUtils.isEmpty(orgList)){
            throw new BusinessException("该设备没有路段组织信息!");
        }
        String time1 = params.get("startTime").toString();
        String dateString1 = DateUtils.stringToDateString(time1, DateUtils.yyyy_MM_dd_HH_mm_ss_EN, DateUtils.yyyy_MM_dd_HH_mm_CN);
        String rsTime1 = dateString1.substring(5, 14);
        String time2 = params.get("endTime").toString();
        String dateString2 = DateUtils.stringToDateString(time2, DateUtils.yyyy_MM_dd_HH_mm_ss_EN, DateUtils.yyyy_MM_dd_HH_mm_CN);
        String rsTime2 = dateString2.substring(5, 14);
        HashMap<String, Object> rsMap = new HashMap<>();
        List<Map<String, Object>> dusts = historySecondCruiserMapper.getDusts(params);
        Map<String, List<DustldDTO>> collect = manageCoordinateMapper.CompareTo(orgList.get(0)).stream().collect(Collectors.groupingBy(o -> o.getName()));
        if (ObjectUtils.isEmpty(dusts) || ObjectUtils.isEmpty(collect)){
            return null;
        }
        Set<String> strings = collect.keySet();
        ArrayList<DustForm> list1 = new ArrayList<>();
        //所有的高值路段
        StringBuilder builder = new StringBuilder();
 
        int i = 1;
        long timestamp = System.currentTimeMillis();
        for (String string : strings) {
            DustForm dustForm = new DustForm();
            ArrayList<Double> doubleArrayList = new ArrayList<>();
            List<DustldDTO> dustldDTOS = collect.get(string);
            for (DustldDTO dustldDTO : dustldDTOS) {
                String flyLat = dustldDTO.getFlyLat();
                String flyLon = dustldDTO.getFlyLon();
                if (flyLon==null && flyLat==null){
                    continue;
                }
                double latDouble1 = Double.parseDouble(flyLat);
                double lonDouble1 = Double.parseDouble(flyLon);
                for (Map<String, Object> dust : dusts) {
                    String flyLat1 = Objects.nonNull(dust.get("flyLat")) ? dust.get("flyLat").toString() :"0";
                    String flyLon1 = Objects.nonNull(dust.get("flyLon")) ? dust.get("flyLon").toString() :"0";
                    double latDouble = Double.parseDouble(flyLat1);
                    double lonDouble = Double.parseDouble(flyLon1);
//                    String flyLon1 = dust.get("flyLon").toString();
                    if (latDouble1==latDouble && lonDouble1==lonDouble){
                        Double dustld = Objects.nonNull(dust.get("dustld"))?Double.parseDouble(dust.get("dustld").toString()):0d;
 
                        if (dustld>=0 && dustld<40 ){
                            dustld = dustld + 170;
                        }else if (dustld>=40 && dustld<60){
                            dustld = dustld + 130;
                        }else if (dustld>=60 && dustld<100 ){
                            dustld = dustld + 110;
                        }else if (dustld>=100 && dustld<150){
                            dustld = dustld + 70;
                        }else if (dustld>=150 && dustld<180){
                            dustld = dustld + 30;
                        }
                        doubleArrayList.add(dustld);
                        break;
                    }
                }
            }
            if (ObjectUtils.isEmpty(doubleArrayList)){
                continue;
            }
            Double ListAva = doubleArrayList.stream() .collect(Collectors.averagingDouble(Double::doubleValue));
            double rsAvg = new BigDecimal(ListAva/1000).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
            dustForm.setRoad(string);
            dustForm.setValue(rsAvg);
            list1.add(dustForm);
            if (rsAvg>=0.3){
             builder.append("("+i+")"+string);
             i++;
             //添加高值路段
                QueryWrapper<MaxRoad> wrapper = new QueryWrapper<>();
                wrapper.eq("name",string);
                wrapper.eq("time",DateUtils.getDate(time1,DateUtils.yyyy_MM_dd_EN));
                Integer integer = maxRoadMapper.selectCount(wrapper);
                if (integer==0){
                    MaxRoad maxRoad = new MaxRoad();
                    maxRoad.setData(rsAvg);
                    maxRoad.setName(string);
                    maxRoad.setTime(DateUtils.getDate(time1,DateUtils.yyyy_MM_dd_EN));
                    maxRoadMapper.insert(maxRoad);
                }
            }
        }
        long timestamp2 = System.currentTimeMillis();
        log.info(timestamp2-timestamp+"");
        //排序
        list1.sort(Comparator.comparing(DustForm::getValue).reversed());
        rsMap.put("list1",list1);
 
 
        //获取上一次的高值路段
        ArrayList<Map<String, Object>> list3 = new ArrayList<>();
        QueryWrapper<MaxRoad> wrapper = new QueryWrapper<>();
        wrapper.lt("time",DateUtils.getDate(time1,DateUtils.yyyy_MM_dd_EN));
        wrapper.orderByDesc("time");
 
        List<MaxRoad> maxRoads = maxRoadMapper.selectList(wrapper);
        String dateString3 = DateUtils.dateToDateString(maxRoads.get(0).getTime(), DateUtils.yyyy_MM_dd_CN);
        QueryWrapper<MaxRoad> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("time",maxRoads.get(0).getTime());
        List<MaxRoad> maxRoads1 = maxRoadMapper.selectList(wrapper2);
 
        //获取两次路段相同的值
        for (MaxRoad road : maxRoads1) {
            for (DustForm dustForm : list1) {
                if (road.getName().equals(dustForm.getRoad())){
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("road",road.getName());
                    map.put("value1",road.getData());
                    map.put("value2",dustForm.getValue());
                    map.put("value3",road.getData()-dustForm.getValue());
                    list3.add(map);
                }
            }
        }
        rsMap.put("data",ObjectUtils.isEmpty(builder)?"暂无高值路段":builder);
        rsMap.put("list3",list3);
        rsMap.put("time",rsTime1+"-"+rsTime2);
        rsMap.put("date1",dateString3.substring(5,10));
        rsMap.put("date2",dateString1.substring(5,10));
        return rsMap;
    }
 
    /**
     * 查询尘负荷高值
     * @param id
     * @return
     */
    @Override
    public Map<String,Object> selectDust(Integer id) {
 
        HashMap<String, Object> rsMap = new HashMap<>();
        ArrayList<DustForm> list1 = new ArrayList<>();
 
        Dustld dustld = dustldMapper.selectById(id);
        if (ObjectUtils.isEmpty(dustld)){
            return null;
        }
        String value = dustld.getValue();
        JSONObject jsonObject = JSONObject.parseObject(value);
        Iterator<Map.Entry<String, Object>> iterator = jsonObject.entrySet().iterator();
        while (iterator.hasNext()) {
            DustForm dustForm = new DustForm();
            Map.Entry entry = (Map.Entry) iterator.next();
            dustForm.setRoad(entry.getKey().toString());
            dustForm.setValue(Double.parseDouble(entry.getValue().toString()));
            double aDouble = Double.parseDouble(entry.getValue().toString());
            if (aDouble>=0.3){
                list1.add(dustForm);
            }
        }
        if (!ObjectUtils.isEmpty(list1)){
            list1.sort(Comparator.comparing(DustForm::getValue).reversed());
        }
        rsMap.put("list",list1);
        rsMap.put("count",list1.size());
        return rsMap;
    }
 
    /**
     * 查询日报记录
     * @param params
     * @return
     */
    @Override
    public List<Dustld> selectAll(Map<String, Object> params) {
        String mac = params.get("mac").toString();
        String time1 = params.get("startTime").toString();
        String time2 = params.get("endTime").toString();
 
        QueryWrapper<Dustld> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mac",mac).between("time",time1,time2);
        List<Dustld> dustlds = dustldMapper.selectList(queryWrapper);
        if (ObjectUtils.isEmpty(dustlds)){
            return null;
        }
        return dustlds;
    }
 
 
    //获取图片地址
    private String getList(String path, List<MultipartFile> files1) {
        ArrayList<String> images = new ArrayList<>();
        if (!ObjectUtils.isEmpty(files1)){
            for (MultipartFile file : files1) {
                String fileType = file.getContentType();
                if ("image/jpg".equals(fileType) || "image/png".equals(fileType) || "image/jpeg".equals(fileType)){
                    //获取文件名
                    String fileName = file.getOriginalFilename();
                    //获取文件后缀名
                    String suffixName = fileName.substring(fileName.lastIndexOf("."));
                    //每一次都需要
                    fileName.substring(fileName.lastIndexOf("."));
                    //重新生成文件名
                    fileName = UUID.randomUUID() + suffixName;
                    //图片上传
                    if (FileUtils.upload(file, path, fileName)) {
                        images.add(fileName);
                    }
                }
            }
        }
 
        if (!ObjectUtils.isEmpty(images)) {
            String image = images.stream()
                    .map(String::valueOf)
                    .collect(Collectors.joining(","));
 
            return image;
        }
        return null;
    }
 
}