jinpengyong
2022-12-16 2b088b86c7ebf9e02a25d46520265fb7fd5b85f2
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
package com.moral.api.service.impl;
 
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;
 
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.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.mapper.DustldMapper;
import com.moral.api.mapper.HistorySecondCruiserMapper;
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;
 
@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<Double> dust = historySecondCruiserMapper.getDust(start, end, mac);
                list.addAll(dust);
            }
        }
        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;
    }
 
    /**
     * 查询尘负荷高值
     * @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;
    }
 
}