南昌市物联网技防平台-后台
Administrator
2021-04-07 eb3440d7c0c7baef5e6fc888fd0076b1787643d9
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
package org.springblade.jfpt.parcel.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.models.auth.In;
import org.springblade.jfpt.alarm.constant.AlarmTimeConstant;
import org.springblade.jfpt.animalheat.util.ImageUtil;
import org.springblade.jfpt.parcel.service.ParcelService;
import org.springblade.jfpt.parcel.util.DateUtils;
import org.springblade.jfpt.parcel.util.HttpClientUtils;
import org.springblade.jfpt.parcel.util.JacksonUtil;
import org.springblade.jfpt.parcel.util.PageList;
import org.springblade.jfpt.parcel.vo.ConditionVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
 
import static org.springblade.core.tool.utils.DateUtil.now;
 
/**
 * 包裹服务实现层
 */
@Service
public class ParcelServiceImpl implements ParcelService {
    @Value("${PARCEL_KEY}")
    private String PARCEL_KEY;
 
    @Value("${PARCEL_SECRET}")
    private String PARCEL_SECRET;
 
    @Value("${PARCEL_CONTRABAND_URL}")
    private String PARCEL_CONTRABAND_URL;
 
    @Value("${PARCEL_URL}")
    private String PARCEL_URL;
 
    @Value("${PARCEL_PICTURE_URL}")
    private String PARCEL_PICTURE_URL;
 
    @Value("${PARCEL_DETAIL_RUL}")
    private String PARCEL_DETAIL_RUL;
 
    @Value("${PARCEL_KIND_URL}")
    private String PARCEL_KIND_URL;
 
    @Value("${PARCEL_PRE_URL}")
    private String PARCEL_PRE_URL;
 
 
 
    /**
     * 获取违禁包裹统计数据,远程调用接口
     * @param conditionVo 查询条件对象  status 状态码 0:本天    1:近7天   2:本月  3:本周
     * @return
     */
    @Override
    public List<Map<String, Object>> parcelKindStatis(ConditionVo conditionVo) {
        //1.判空
        if (null!=conditionVo.getStatus()) {
            //获取数据
            List<Object> objectList = getParcelData(conditionVo,PARCEL_CONTRABAND_URL,PARCEL_KEY,PARCEL_SECRET);
            Map<String,String> mapCode = new HashMap<>();
            //遍历集合,存储所有的违禁包裹编码
            for (Object object : objectList) {
                //取出list里面的值转为map
                Map<String, Object> objectMap = (Map<String, Object>) object;
                //将所有的违禁包裹编码存起来
                mapCode.put(objectMap.get("objCode").toString(),objectMap.get("objCode").toString());
            }
            //遍历违禁编码map数据
            List<Map<String,Object>> list = new ArrayList<>();
            for (String value:mapCode.values()) {
                Integer count = 0;
                HashMap<String, Object> map = new HashMap<>();
                for (Object object : objectList) {
                    Map<String, Object> objectMap = (Map<String, Object>) object;//取出list里面的值转为map
                    //将所有的违禁包裹编码存起来
                    if (value.equals(objectMap.get("objCode").toString())){
                        count+=Integer.parseInt(objectMap.get("objCount").toString());
                        map.put("name",objectMap.get("objName").toString());
                        map.put("value",count);
                    }
                }
                list.add(map);
            }
            return list;
        }
        return null;
    }
 
    /**
     * 获取包裹统计数据,远程调用接口获取数据
     * @param conditionVo 查询条件对象  status 状态码 0:本天    1:本周   2:本月
     * @return
     */
    @Override
    public Object getParcelStatis(ConditionVo conditionVo) {
        List<Object> list = new ArrayList<>();//返回
        //1.判空
        if (null!=conditionVo.getStatus()) {
            //2.获取数据
            List<Object> objectList = getParcelData(conditionVo, PARCEL_URL, PARCEL_KEY, PARCEL_SECRET);
            if (null != objectList) {
                //3.创建时间集合,包裹集合数据
                List<String> listTime = new ArrayList<>();
                List<Map<String, Object>> parcelList = new ArrayList<>();
                if (conditionVo.getStatus() == 1) {
                    //3.获取今天之前的每一天数组   2021-3-12 arsn修改
                    listTime = DateUtils.pastDay(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
                    List<String> percelTimeList = new ArrayList<>();
                    for (Object object : objectList) {
                        //4.取出list里面的值转为map
                        Map<String, Object> objectMap = (Map<String, Object>) object;
                        //5.将远程时间数据存入集合
                        percelTimeList.add(objectMap.get("dateStr").toString());
                    }
                    for (String time : listTime) {
                        //6.创建map,用于存储包裹数据
                        HashMap<String, Object> hashMap = new HashMap<>();
                        //7.如果远程数据哪天数据没有,则为0
                        if (!percelTimeList.contains(time)) {
                            hashMap.put("dateStr", time);
                            hashMap.put("checkCount", 0);
                            hashMap.put("dangerCount", 0);
                            parcelList.add(hashMap);
                        } else {
                            //8.遍历,分别取出数据
                            for (Object object : objectList) {
                                //9.取出list里面的值转为map
                                Map<String, Object> objectMap = (Map<String, Object>) object;
                                String dateStr = objectMap.get("dateStr").toString();  //安检时间
                                Integer checkCount = Integer.parseInt(objectMap.get("checkCount").toString()); //安检包裹数量
                                Integer dangerCount = Integer.parseInt(objectMap.get("dangerCount").toString()); //违禁包裹数量
                                //10.判断,存入数据
                                if (time.equals(dateStr)) {
                                    Set<Map.Entry<String, Object>> entries = objectMap.entrySet();
                                    for (Map.Entry<String, Object> m : entries) {
                                        if (m.getKey().equals("checkCount")) {
                                            hashMap.put("checkCount", checkCount);
                                            hashMap.put("dateStr", dateStr);
                                        }
                                        if (m.getKey().equals("dangerCount")) {
                                            hashMap.put("dangerCount", dangerCount);
                                        }
                                    }
                                }
                            }
                            //11.加入到集合中
                            parcelList.add(hashMap);
                        }
                    }
                }
                //12.封装数据
                list.add(listTime);
                list.add(parcelList);
                list.add(getKindServenDataList(conditionVo));
                //13.返回
                return list;
            }
        }
        return null;
    }
 
 
    /**
     * 数据请求处理
     * @param conditionVo  条件
     * @param url url
     * @param key 请求Key
     * @param secret 请求 secret
     * @return
     */
    private List<Object> getParcelData(ConditionVo conditionVo, String url, String key, String secret) {
        //1.创建map对象,用于存储请求接口的秘钥
        Map<String, Object> map = new HashMap<>();
        String startDate = null;
        String endDate = null;
        //2.2查询本天的数据
        if (null!=conditionVo.getStatus()) {
            if (conditionVo.getStatus() == 0) {
                //3.获取本天开始时间和结束时间
                startDate = DateUtils.getToday();
                endDate = DateUtils.getToday();
            }
            //2.2查询过去6天及当天的的数据
            if (conditionVo.getStatus() == 1) {
                //3.获取过去6天及当天开始时间和结束时间
                startDate = DateUtils.getPastDate(6, now());
                endDate = DateUtils.getToday();
            }
            //2.4查询本周的数据
            if (conditionVo.getStatus() == 3) {
                //3.获取过去6天及当天开始时间和结束时间
                startDate = DateUtils.getWeekStart();
                endDate = DateUtils.getWeekEnd();
            }
            //2.2查询本月的数据
            if (conditionVo.getStatus() == 2) {
                //3.获取本月开始时间和结束时间
                startDate = DateUtils.getMonthStart();
                endDate = DateUtils.getMonthEnd();
            }
        }
        //2.5 按时间条件查询
        if (null!=conditionVo.getStartTime() && conditionVo.getStartTime()!=""
            && null != conditionVo.getEndTime() && conditionVo.getEndTime()!=""){
            startDate = conditionVo.getStartTime().substring(0,10);
            System.out.println("startDate = " + startDate);
            endDate = conditionVo.getEndTime().substring(0,10);
            System.out.println("endDate = " + endDate);
        }
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        //4.远程调用数据
        String params = HttpClientUtils.httpPost(url, key, secret, map);
        //5.数据转换
        Map<String, Object> data = (Map<String, Object>) JSONObject.parse(params);
        if (data.get("succ").toString().equals("ok")){
            //6.返回数据
            return JSON.parseArray(data.get("result").toString());
        }
        return null;
    }
 
 
    /**
     * 查询包裹,违禁品总数
     * @param conditionVo status 状态码 0:本天    1:本周   2:本月
     * @return
     */
    @Override
    public List<Integer> selectParcelCount(ConditionVo conditionVo) {
        List<Integer> list = new ArrayList<>();
        //获取包裹数据
        List<Object> parcelData = getParcelData(conditionVo, PARCEL_URL, PARCEL_KEY, PARCEL_SECRET);
        if (null!=parcelData) {
            //查询当天的包裹总数
            Integer dangerCount = null;
            Integer contrabandCount = null;
            conditionVo.setStatus(0);
            if (conditionVo.getStatus() == 0) {
                for (Object object : parcelData) {
                    //取出list里面的值转为map
                    Map<String, Object> objectMap = (Map<String, Object>) object;
                    dangerCount = Integer.parseInt(objectMap.get("dangerCount").toString()); //违禁包裹数量
                    contrabandCount = Integer.parseInt(objectMap.get("contrabandCount").toString()); //违禁物品数量
                }
            }
            //封装数据
            list.add(dangerCount);
            list.add(contrabandCount);
            //返回数据
            return list;
        }
        return null;
    }
 
    /**
     * 查询7天内违禁品每天的数量(需筛选危险品)
     * @return
     */
    @Override
    public Map<String, Object> selectParcelData(ConditionVo conditionVo) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("parcelData",getKindServenDataList(conditionVo));
        return map;
    }
 
    /**
     * 获取危险违禁品7天内每天的数量集合
     * @return
     */
    public List<Integer> getKindServenDataList(ConditionVo conditionVo){
        if (null==conditionVo){
            conditionVo.setStatus(1);//近7天status 为 1
        }
        //获取数据
        List<Object> objectList = getParcelData(conditionVo,PARCEL_CONTRABAND_URL,PARCEL_KEY,PARCEL_SECRET);
        if (null!=objectList) {
            //遍历违禁编码map数据
            List<String> times = DateUtils.pastDay(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            List<Integer> list = new ArrayList<>();
            for (String time : times) {
                Integer count = 0;
                //遍历集合数据获取每一天的count
                for (Object object : objectList) {
                    Map<String, Object> objectMap = (Map<String, Object>) object;//取出list里面的值转为map
                    //对比时间和objCode,危险品,刀具,枪支等
                    if (time.equals(objectMap.get("dateStr").toString())) {
//                    //枪支
//                    if (objectMap.get("objCode").toString().equals("gan")) {
//                        count += Integer.parseInt(objectMap.get("objCount").toString());
//                    }
//                    //刀具
//                    if (objectMap.get("objCode").toString().equals("knife")) {
//                        count += Integer.parseInt(objectMap.get("objCount").toString());
//                    }
//                    //爆炸物
//                    if (objectMap.get("objCode").toString().equals("boomExplosive")) {
//                        count += Integer.parseInt(objectMap.get("objCount").toString());
//                    }
                        //3-20 修改不筛选危险品,由第三方筛选好
                        count += Integer.parseInt(objectMap.get("objCount").toString());
                    }
                }
                list.add(count);
            }
            return list;
        }
        return null;
    }
 
    /**
     * 获取图片地址
     * @param imgUrl 图片Url
     * @return
     */
    @Override
    public Map<String, Object> getParcelPic(String imgUrl) {
        //截取图片imgKey信息
        String[] imgKeys = imgUrl.split("=", imgUrl.length());
        String imgKey = imgKeys[1];
        //封装成map对象
        HashMap<String, String> map = new HashMap<>();
        map.put("imgKey",imgKey);
        //执行http远程调用
        String s = HttpClientUtils.doGetHeaderPictureBase64(PARCEL_PICTURE_URL, PARCEL_KEY, PARCEL_SECRET, map);
        //获取图片的长宽
        Map<String, Object> imageMap = ImageUtil.getBase64ImageWidthAndHeight(s);
        //封装数据
        imageMap.put("imgBase64",s);
        //返回
        return imageMap;
    }
 
    /**
     * 获取包裹明细分页数据,远程调用api获取数据
     * @param conditionVo  请求参数对象
     * @return
     */
    @Override
    public Object getParcelDetailPage(ConditionVo conditionVo) {
        Map<String,Object> result =(Map<String,Object>) getParcelDataPageList(conditionVo, PARCEL_DETAIL_RUL, PARCEL_KEY, PARCEL_SECRET).get("result");
        if (null!=result) {
//            List<Object> dataList = JSONArray.parseArray(result.get("data").toString());
//            System.out.println("dataList = " + dataList);
//            //遍历集合
//            for (Object data:dataList) {
//                Map<String,Object> parcelData =(Map<String,Object>)data;
//                String imgUrl = parcelData.get("imgUrl").toString();
//                //查询图片  PARCEL_PRE_URL
//                //截取图片imgKey信息
//                String[] imgKeys = imgUrl.split("=", imgUrl.length());
//                String imgKey = imgKeys[1];
//                //封装成map对象
//                HashMap<String, String> map = new HashMap<>();
//                map.put("imgKey",imgKey);
//                //执行http远程调用
//                String s = HttpClientUtils.doGetHeaderPictureBase64(PARCEL_PICTURE_URL, PARCEL_KEY, PARCEL_SECRET, map);
//                //封装数据
//                if (null!=s && s!="") {
//                    parcelData.put("imgUrl", s);
//                }
//            }
            return JSONObject.parse(result.toString());
 
        }
        return null;
    }
 
 
    /**
     * 获取包裹违禁品明细分页数据,远程调用api获取数据
     * @param conditionVo  请求参数对象
     * @return
     */
    @Override
    public Object getParcelkindDetailPage(ConditionVo conditionVo) {
        //获取违禁品数据
        String result = getParcelDataPageList(conditionVo, PARCEL_KIND_URL, PARCEL_KEY, PARCEL_SECRET).get("result").toString();
        if (null!=result) {
            return JSONObject.parse(result);
        }
        return null;
    }
 
    /**
     * 远程调用api 获取违禁品详细数据
     * @param conditionVo 条件
     * @param url 违禁品详细数据Url
     * @param key key
     * @param secret 秘钥
     * @return
     */
    public Map<String,Object> getParcelDataPageList(ConditionVo conditionVo, String url, String key, String secret){
        //1.创建map对象存储数据
        HashMap<String, Object> map = new HashMap<>();
        //2.条件判断
        if (null!=conditionVo.getStartTime()){
            map.put("startTime", conditionVo.getStartTime());
        }
        if (null!=conditionVo.getEndTime()){
            map.put("endTime", conditionVo.getEndTime());
        }
        if (null!=conditionVo.getDecisioDiagramResult()){
            map.put("decisioDiagramResult", conditionVo.getDecisioDiagramResult());
        }
        if (null!=conditionVo.getCurrentPage()){
            map.put("currentPage", conditionVo.getCurrentPage());
        }
        if (null!=conditionVo.getPageSize()){
            map.put("pageSize", conditionVo.getPageSize());
        }
        //3.远程调用api 获取数据
        String params = HttpClientUtils.httpPost(url, key, secret, map);
        Map<String,Object> paramsMap = (Map<String, Object>) JSONObject.parse(params);
        if (paramsMap.get("succ").toString().equals("ok")){
            //4返回数据
            return paramsMap;
        }
        //4返回数据
        return null;
    }
 
    /**
     * 查询当前时间段违禁品总个数
     * @param conditionVo
     * @return
     */
    @Override
    public Integer selParcelTimeCount(ConditionVo conditionVo) {
        return selectParcelCount(conditionVo).get(1);
    }
 
    /**
     *查询当前时间段区间时间违禁品个数
     * @param conditionVo
     * @return
     */
    @Override
    public List<Integer> selParcelTimeDis(ConditionVo conditionVo) {
        ArrayList<List<Object>> objectArrayList = new ArrayList<>();
        //设置页码数
        conditionVo.setCurrentPage(1);
        conditionVo.setPageSize(100);
        //获取数据
        Map<String,Object> result =(Map<String,Object>) getParcelDataPageList(conditionVo, PARCEL_KIND_URL, PARCEL_KEY, PARCEL_SECRET).get("result");
        if (null!=result) {
            List<Object> dataList = JSONArray.parseArray(result.get("data").toString());
            objectArrayList.add(dataList);
            //取第一次获取到的数据总数,每次取100条,多于100分批次取
            int totalSize = Integer.parseInt(result.get("totalSize").toString());
            int i = totalSize/100+1;
            if (i>1){
                for (int j=1;j<i;j++) {
                    conditionVo.setCurrentPage(j+1);
                    //获取数据
                    Map<String,Object> result0 =(Map<String,Object>) getParcelDataPageList(conditionVo, PARCEL_KIND_URL, PARCEL_KEY, PARCEL_SECRET).get("result");
                    List<Object> dataLists = JSONArray.parseArray(result0.get("data").toString());
                    objectArrayList.add(dataLists);
                }
            }
            //合并集合
            List<Object> collect = objectArrayList.stream().flatMap(List::stream).collect(Collectors.toList());
            //筛选数据,返回数据
            return getParcelTimeDis(collect);
        }
        return Arrays.asList(0,0,0,0,0,0,0,0,0,0,0,0);
    }
 
    /**
     * 筛选数据,分类返回
     * @param collect 集合数据
     * @return
     */
    private List<Integer> getParcelTimeDis(List<Object> collect) {
        List<Integer> list = new ArrayList<>();
        int count02 = 0;
        int count0204 = 0;
        int count0406 = 0;
        int count0608 = 0;
        int count0810 = 0;
        int count1012 = 0;
        int count1214 = 0;
        int count1416 = 0;
        int count1618 = 0;
        int count1820 = 0;
        int count2022 = 0;
        int count2224 = 0;
        //遍历集合
        for (Object data:collect) {
            Map<String,Object> parcelData =(Map<String,Object>)data;
            try {
                long hours = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(parcelData.get("createTime").toString()).getHours();
                if (hours>0 && hours< AlarmTimeConstant.TWO){
                    count02+=1;
                }
                if (hours>=AlarmTimeConstant.TWO && hours<AlarmTimeConstant.FOUR){
                    count0204+=1;
                }
                if (hours>=AlarmTimeConstant.FOUR && hours<AlarmTimeConstant.SIX){
                    count0406+=1;
                }
                if (hours>=AlarmTimeConstant.SIX && hours<AlarmTimeConstant.EIGHT){
                    count0608+=1;
                }
                if (hours>=AlarmTimeConstant.EIGHT && hours<AlarmTimeConstant.TEN){
                    count0810+=1;
                }
                if (hours>=AlarmTimeConstant.TEN && hours<AlarmTimeConstant.TWEKVE){
                    count1012+=1;
                }
                if (hours>=AlarmTimeConstant.TWEKVE && hours<AlarmTimeConstant.FOURTEEN){
                    count1214+=1;
                }
                if (hours>=AlarmTimeConstant.FOURTEEN && hours<AlarmTimeConstant.SIXTEEN){
                    count1416+=1;
                }
                if (hours>=AlarmTimeConstant.SIXTEEN && hours<AlarmTimeConstant.EIGHTEEN){
                    count1618+=1;
                }
                if (hours>=AlarmTimeConstant.EIGHTEEN && hours<AlarmTimeConstant.TWENTY){
                    count1820+=1;
                }
                if (hours>=AlarmTimeConstant.TWENTY && hours<AlarmTimeConstant.TWENTY_TWO){
                    count2022+=1;
                }
                if (hours>=AlarmTimeConstant.TWENTY_TWO && hours<AlarmTimeConstant.TWENTY_FOUR){
                    count2224+=1;
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        //封装数据
        list.add(count02);
        list.add(count0204);
        list.add(count0406);
        list.add(count0608);
        list.add(count0810);
        list.add(count1012);
        list.add(count1214);
        list.add(count1416);
        list.add(count1618);
        list.add(count1820);
        list.add(count2022);
        list.add(count2224);
        //返回数据
        return list;
    }
}