jiazx0107@163.com
2023-05-31 7a7ccfd74dbdff086d2aabe5117ec1dc6f804d91
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
package com.ld.igds.common.manager;
 
import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.model.DefaultDept;
import com.ld.igds.common.CoreCommonService;
import com.ld.igds.common.CoreDeviceIotService;
import com.ld.igds.common.CoreDeviceService;
import com.ld.igds.common.CoreDicService;
import com.ld.igds.common.dto.PosDto;
import com.ld.igds.constant.*;
import com.ld.igds.data.BaseParam;
import com.ld.igds.data.Page;
import com.ld.igds.data.PageResponse;
import com.ld.igds.data.SimpleDepot;
import com.ld.igds.grain.dto.GrainData;
import com.ld.igds.grain.service.CoreGrainService;
import com.ld.igds.inout.dto.InoutParam;
import com.ld.igds.models.*;
import com.ld.igds.sys.service.DicTriggerService;
import com.ld.igds.sys.service.SysDeptService;
import com.ld.igds.util.ContextUtil;
import com.ld.igds.util.DateUtil;
import com.ld.igds.util.SystemUtil;
import com.ld.igds.util.RedisUtil;
import com.ld.igds.warn.data.WarnData;
import com.ld.igds.warn.data.WarnParam;
import com.ld.igds.warn.service.CoreWarnService;
import com.ld.igds.weather.CoreWeatherService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 逻辑管理,用于实现控制层的逻辑
 */
@Slf4j
@Component
public class CommonManager {
 
    @Autowired
    private CoreCommonService coreCommonService;
    @Autowired
    private DicTriggerService dicTriggerService;
    @Autowired
    private CoreDicService coreDicService;
    @Autowired
    private CoreDeviceService coreDeviceService;
    @Autowired
    private CoreDeviceIotService deviceIotService;
    @Autowired
    private CoreGrainService coreGrainService;
    @Autowired
    private CoreWeatherService weatherService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private CoreWarnService coreWarnService;
    @Resource
    private RedisUtil redisUtil;
 
    /**
     * 获取入库作业中,仓库下拉框,值获取状态=非满仓的粮库
     *
     * @return
     */
    public List<SimpleDepot> getCacheDeptIn(String companyId, String deptId) {
 
        List<SimpleDepot> result = new ArrayList<>();
        List<Depot> list = coreCommonService.getCacheDepotList(companyId, deptId);
 
        for (Depot depot : list) {
            if (DepotStatus.STATUS_3.getCode().equals(depot.getDepotStatus())) continue;
            if (DepotStatus.STATUS_31.getCode().equals(depot.getDepotStatus())) continue;
            if (DepotStatus.STATUS_32.getCode().equals(depot.getDepotStatus())) continue;
            if (DepotStatus.STATUS_33.getCode().equals(depot.getDepotStatus())) continue;
            if (DepotStatus.STATUS_34.getCode().equals(depot.getDepotStatus())) continue;
 
            result.add(new SimpleDepot(depot));
        }
 
        return result;
    }
 
    /**
     * 获取出库下拉框列表,自动屏蔽空仓
     *
     * @return
     */
    public List<SimpleDepot> getCacheDeptOut(String companyId, String deptId) {
        List<SimpleDepot> result = new ArrayList<>();
        List<Depot> list = coreCommonService.getCacheDepotList(companyId, deptId);
 
        for (Depot depot : list) {
            if (DepotStatus.STATUS_1.getCode().equals(depot.getDepotStatus())) continue;
            if (DepotStatus.STATUS_2.getCode().equals(depot.getDepotStatus())) continue;
 
            result.add(new SimpleDepot(depot));
        }
 
        return result;
    }
 
    /**
     * 根据部门编码获取下属的所有库区集合
     *
     * @param deptId
     * @return
     */
    public List<DefaultDept> getDeptListByDeptId(String deptId) {
 
        if (StringUtils.isEmpty(deptId)) {
            return null;
        }
        String companyId = ContextUtil.getCompanyId();
        List<DefaultDept> list = sysDeptService.getCacheDept(companyId);
        if (list == null || list.isEmpty()) {
            return null;
        }
 
        List<DefaultDept> deptList = new ArrayList<>();
 
        for (DefaultDept dept : list) {
            if (StringUtils.isNotEmpty(dept.getParentId()) &&
                    dept.getParentId().contains(deptId) &&
                    dept.getType().equals(Constant.DEPT_TYPE_20)) {
                deptList.add(dept);
            }
        }
 
        return deptList;
    }
 
    /**
     * 查询部门下属所有仓库列表
     *
     * @param companyId
     * @param deptId
     * @return
     */
    public List<Depot> listDepotByDeptId(String companyId, String deptId) {
        return coreCommonService.getCacheDepotList(companyId, deptId);
    }
 
    /**
     * 查询警告信息
     *
     * @param deptId
     * @return
     */
    public List<WarnData> listWarnByDeptId(String deptId) {
        WarnParam param = new WarnParam();
        param.setCompanyId(ContextUtil.getCompanyId());
        if (StringUtils.isNotEmpty(deptId)) {
            param.setDeptId(deptId + "%");
        }
        param.setStart(DateUtil.getNewByDay(new Date(), -7));
        param.setEnd(new Date());
        return coreWarnService.queryWarn(param);
    }
 
 
    /**
     * 根据分库编号获取当前分库下仓库列表
     *
     * @param deptId
     * @return
     */
    public List<Depot> listDepot(String deptId) {
 
        IUser user = ContextUtil.getLoginUser();
 
        if (StringUtils.isEmpty(deptId)) {
            deptId = ContextUtil.subDeptId(user);
        }
 
        List<Depot> list = coreCommonService.getCacheDepotList(user.getCompanyId(), deptId);
 
        if (null == list || list.isEmpty()) return null;
 
        return list;
    }
 
    /**
     * 获取当前人所在实时部门下的仓库列表
     *
     * @param isDelOther 是否删除其他类型仓库
     * @return
     */
    public List<Depot> listDepot(boolean isDelOther, String deptId) {
        String companyId = ContextUtil.getCompanyId();
 
        if (StringUtils.isEmpty(deptId) || "NULL".equalsIgnoreCase(deptId)) {
            deptId = ContextUtil.subDeptId(null);
        }
 
        List<Depot> list;
        list = coreCommonService.getCacheDepotList(companyId, deptId);
        if (null == list || list.isEmpty()) {
            return list;
        }
        // 删除非仓库列表
        if (isDelOther) {
            return list
                    .stream()
                    .filter(item -> !item.getDepotType().equals(
                            DepotType.TYPE_99.getCode()))
                    .collect(Collectors.toList());
        }
 
        return list;
    }
 
    /**
     * 根据分库编号获取当前分库下仓库列表
     *
     * @param isDelOther
     * @return
     */
    public List<Depot> listDepot(boolean isDelOther) {
        return listDepot(isDelOther, null);
    }
 
 
    public PageResponse<String> updateDevicePos(List<PosDto> list) {
        if (null == list || list.isEmpty()) {
            return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(),
                    "没有获取到需要更新信息!");
        }
 
        try {
            // 首先根据参数删除原来的信息
            String companyId = ContextUtil.getCompanyId();
 
            // 开始保存新的数据
            for (PosDto posDto : list) {
                posDto.setId(ContextUtil.getUUID());
                posDto.setBizTag("DEVICE");
                posDto.setCompanyId(companyId);
                coreDeviceService.updatePos(posDto.getBizId(), posDto.getPosX(), posDto.getPosY());
            }
 
            // 跟新缓存
            coreDeviceService.refreshCache(companyId);
 
        } catch (Exception e) {
            log.error("后端异常:{}", e);
            return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(), "后端异常:"
                    + e.getMessage());
        }
        return new PageResponse<>(RespCodeEnum.CODE_0000.getCode(),
                "设备位置信息更新成功!");
 
 
    }
 
    public PageResponse<String> updateVerbPos(List<PosDto> list) {
        if (null == list || list.isEmpty()) {
            return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(),
                    "没有获取到需要更新信息!");
        }
        log.debug("前端传递过过来的设备位置信息={}", list.toString());
 
        try {
            // 首先根据参数删除原来的信息
            String companyId = ContextUtil.getCompanyId();
 
            // 开始保存新的数据
            for (PosDto posDto : list) {
                posDto.setId(ContextUtil.getUUID());
                posDto.setBizTag(BizType.AREATION.getCode());
                posDto.setCompanyId(companyId);
 
                // 先删除原有的配置
                //coreDicService.delPosByBizId(posDto);
 
                // 后添加新数据
                //coreDicService.addPos(posDto);
            }
 
            // 跟新缓存
            coreDeviceService.refreshCache(companyId);
 
        } catch (Exception e) {
            log.error("后端异常:{}", e);
            return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(), "后端异常:"
                    + e.getMessage());
        }
        return new PageResponse<>(RespCodeEnum.CODE_0000.getCode(),
                "设备位置信息更新成功!");
    }
 
    public PageResponse<String> updateTempPos(List<PosDto> list) {
        if (null == list || list.isEmpty()) {
            return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(),
                    "没有获取到需要更新信息!");
        }
        log.debug("前端传递过过来的设备位置信息={}", list.toString());
        try {
            // 首先根据参数删除原来的信息
            String companyId = ContextUtil.getCompanyId();
            // 开始保存新的数据
            for (PosDto posDto : list) {
                posDto.setId(ContextUtil.getUUID());
                posDto.setBizTag(BizType.TEMPCONTROL.getCode());
                posDto.setCompanyId(companyId);
 
                // 先删除原有的配置
                //coreDicService.delPosByBizId(posDto);
                // 后添加新数据
                //coreDicService.addPos(posDto);
            }
 
            // 跟新缓存
            coreDeviceService.refreshCache(companyId);
 
        } catch (Exception e) {
            log.error("后端异常:{}", e);
            return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(), "后端异常:"
                    + e.getMessage());
        }
        return new PageResponse<>(RespCodeEnum.CODE_0000.getCode(),
                "设备位置信息更新成功!");
    }
 
    public PageResponse<String> updateN2Pos(List<PosDto> list) {
        if (null == list || list.isEmpty()) {
            return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(),
                    "没有获取到需要更新信息!");
        }
        log.debug("前端传递过过来的设备位置信息={}", list.toString());
        try {
            // 首先根据参数删除原来的信息
            String companyId = ContextUtil.getCompanyId();
            // 开始保存新的数据
            for (PosDto posDto : list) {
                posDto.setId(ContextUtil.getUUID());
                posDto.setBizTag(BizType.N2.getCode());
                posDto.setCompanyId(companyId);
 
                // 先删除原有的配置
                //coreDicService.delPosByBizId(posDto);
 
                // 后添加新数据
                //coreDicService.addPos(posDto);
 
                coreDeviceService.updatePos(posDto.getBizId(), posDto.getPosX(), posDto.getPosY());
            }
 
            // 跟新缓存
            coreDeviceService.refreshCache(companyId);
 
        } catch (Exception e) {
            log.error("后端异常:{}", e);
            return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(), "后端异常:"
                    + e.getMessage());
        }
        return new PageResponse<>(RespCodeEnum.CODE_0000.getCode(),
                "设备位置信息更新成功!");
    }
 
    public PageResponse<String> updateDoorPos(List<PosDto> list) {
        if (null == list || list.isEmpty()) {
            return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(),
                    "没有获取到需要更新信息!");
        }
        log.debug("前端传递过过来的设备位置信息={}", list.toString());
        try {
            // 首先根据参数删除原来的信息
            String companyId = ContextUtil.getCompanyId();
            // 开始保存新的数据
            for (PosDto posDto : list) {
                posDto.setId(ContextUtil.getUUID());
                posDto.setBizTag(BizType.SECURITY.getCode());
                posDto.setCompanyId(companyId);
 
                // 先删除原有的配置
                coreDicService.delPosByBizId(posDto);
 
                // 后添加新数据
                coreDicService.addPos(posDto);
            }
            // 跟新缓存
            coreDeviceService.refreshCache(companyId);
        } catch (Exception e) {
            log.error("后端异常:{}", e);
            return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(), "后端异常:"
                    + e.getMessage());
        }
        return new PageResponse<>(RespCodeEnum.CODE_0000.getCode(),
                "设备位置信息更新成功!");
    }
 
    public PageResponse<String> updateGrainPos(List<PosDto> list) {
        if (null == list || list.isEmpty()) {
            return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(),
                    "没有获取到需要更新信息!");
        }
        log.debug("前端传递过过来的设备位置信息={}", list.toString());
        try {
            // 首先根据参数删除原来的信息
            String companyId = ContextUtil.getCompanyId();
            // 开始保存新的数据
            for (PosDto posDto : list) {
                posDto.setId(ContextUtil.getUUID());
                posDto.setBizTag(BizType.TEMPCONTROL.getCode());
                posDto.setCompanyId(companyId);
 
                // 先删除原有的配置
                coreDicService.delPosByBizId(posDto);
 
                // 后添加新数据
                coreDicService.addPos(posDto);
            }
 
            // 跟新缓存
            deviceIotService.refreshCache(companyId);
 
        } catch (Exception e) {
            log.error("后端异常:{}", e);
            return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(), "后端异常:"
                    + e.getMessage());
        }
        return new PageResponse<>(RespCodeEnum.CODE_0000.getCode(),
                "设备位置信息更新成功!");
    }
 
    public DicSlogan getDicSlogan() {
        Map<String, Object> parameter = new HashMap<>();
        parameter.put("companyId", ContextUtil.getCompanyId());
        parameter.put("status", Constant.YN_Y);
        return coreDicService.getDicSlogan(parameter);
    }
 
    /**
     * 根据父编码获取下拉框信息
     *
     * @param parentCode
     * @param companyId  为空默认系统配置
     * @return
     */
    public List<DicTrigger> getListByParent(String parentCode, String companyId) {
        if (null == companyId) {
            companyId = ContextUtil.getCompanyId();
        }
        return dicTriggerService.getCacheDicByParent(companyId, parentCode);
    }
 
    public WeatherInfo getWeather() {
        return weatherService.getCacheWeather(ContextUtil.getCompanyId());
    }
 
    public PageResponse<GrainData> queryGrainData(BaseParam param) {
        if (StringUtils.isEmpty(param.getDepotId())) {
            return new PageResponse<>(
                    RespCodeEnum.CODE_1111.getCode(), "没有获取到所选择的仓库信息!");
        }
 
        if (StringUtils.isEmpty(param.getCompanyId())) {
            param.setCompanyId(ContextUtil.getCompanyId());
        }
 
        Map<String, GrainData> result = coreGrainService.getCacheGrainDateMap(param.getCompanyId(), param.getDeptId());
 
        if (null == result || result.isEmpty()) {
            return new PageResponse<>(
                    RespCodeEnum.CODE_1111.getCode(), "缓存中没有获取到当前仓库的粮情信息!");
        }
 
        GrainData data = result.get(param.getDepotId());
 
        if (null == data) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(),
                    "缓存中没有获取到当前仓库的粮情信息!");
        }
        return new PageResponse<>(RespCodeEnum.CODE_0000, data);
    }
 
 
    public DepotConf getDepotConf(String companyId, String depotId) {
        return coreCommonService.getCacheDepotConf(companyId, depotId);
    }
 
 
    /**
     * 获取当前人所在实际库点部门
     *
     * @param user   当前登录用户
     * @param deptId 当前用户所在分库编号,可以为空
     * @return
     */
    public DefaultDept getSubDept(IUser user, String deptId) {
 
        if (StringUtils.isEmpty(deptId)) {
            deptId = ContextUtil.subDeptId(user);
        }
        return sysDeptService.getSubDept(user, deptId);
    }
 
    /**
     * 获取系统配置文档
     *
     * @param companyId
     * @return
     */
    public DicSysConf getSysConf(String companyId) {
        if (StringUtils.isEmpty(companyId)) {
            companyId = ContextUtil.getCompanyId();
        }
        return coreCommonService.getCacheSysConf(companyId);
    }
 
    /**
     * 给仓库添加 鸟瞰图的位置坐标
     *
     * @param companyId
     * @param listDepot
     * @return
     */
    public List<Depot> updateDepotPos(String companyId, List<Depot> listDepot) {
        if (null == listDepot)
            return null;
        if (listDepot.isEmpty())
            return listDepot;
 
        // 获取所有坐标信息
        List<PosDto> posDtoList = coreDicService.getPosByBizTag(companyId, PosDto.BIZ_TAG_DEPOT);
 
        if (null == posDtoList || posDtoList.isEmpty())
            return listDepot;
 
        for (Depot depot : listDepot) {
            for (PosDto posDto : posDtoList) {
                if (depot.getId().equals(posDto.getBizId())) {
                    depot.setPosX(posDto.getPosX());
                    depot.setPosY(posDto.getPosY());
                    break;
                }
            }
        }
 
        return listDepot;
    }
 
 
    /**
     * 根据用户请求信息,根据当前客户电脑IP,获取出入库称重上次选择的地磅
     *
     * @param httpRequest
     * @return
     */
    public String getInoutWeightByClient(HttpServletRequest httpRequest) {
        String userIp = SystemUtil.getIP(httpRequest);
        log.debug("----------------根据用户IP获取选择-地磅----{}", userIp);
 
        String key = "INOUT:WEIGHT:" + userIp;
 
        return (String) redisUtil.get(key);
    }
 
    /**
     * 根据用户请求信息,根据当前客户电脑IP,把用户选择的地磅存在缓存中
     *
     * @param httpRequest
     * @return
     */
    public void setInoutWeightByClient(HttpServletRequest httpRequest, String sort) {
        String userIp = SystemUtil.getIP(httpRequest);
        log.debug("----------------根据用户IP更新选择-地磅----{}---{}", userIp, sort);
        String key = "INOUT:WEIGHT:" + userIp;
        redisUtil.set(key, sort);
    }
 
    /**
     * 用户选择默认查看制氮机,进行客户端信息缓存保留,针对多个制氮机时候生效
     *
     * @param httpRequest
     * @param sn
     */
    public void setSelectN2MacByClient(HttpServletRequest httpRequest, String sn) {
        String userIp = SystemUtil.getIP(httpRequest);
        log.debug("----------------根据用户IP更新选择-制氮机----{}---{}", userIp, sn);
        String key = "N2:MAC:" + userIp;
        redisUtil.set(key, sn);
    }
 
 
    /**
     * 真多多个制氮机时候,根据当前电脑之前查看的制氮机获取之前的选择
     *
     * @param httpRequest
     * @return
     */
    public String getSelectN2MacByClient(HttpServletRequest httpRequest) {
        String userIp = SystemUtil.getIP(httpRequest);
        log.debug("----------------根据用户IP获取选择-制氮机----{}", userIp);
 
        String key = "N2:MAC:" + userIp;
 
        return (String) redisUtil.get(key);
    }
 
 
}