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
package com.qianwen.smartman.modules.visual.service.impl;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.stream.Collectors;
 
import org.springframework.stereotype.Service;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.qianwen.core.tool.utils.DateUtil;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.core.tool.utils.StringUtil;
import com.qianwen.smartman.common.cache.cps.TimeSliceCache;
import com.qianwen.smartman.common.cache.cps.WorkstationCache;
import com.qianwen.smartman.common.constant.CalendarConstant;
import com.qianwen.smartman.common.constant.CommonConstant;
import com.qianwen.smartman.common.constant.MdcConstant;
import com.qianwen.smartman.common.enums.DefaultWcsEnum;
import com.qianwen.smartman.common.enums.GlobalWcsTypeEnum;
import com.qianwen.smartman.common.enums.WcsDataTypeEnums;
import com.qianwen.smartman.common.enums.WorkstationParamTypeEnum;
import com.qianwen.smartman.common.utils.Lambda;
import com.qianwen.smartman.common.utils.LocalDateTimeUtils;
import com.qianwen.smartman.modules.smis.dto.CalendarShiftTimeSlicesDTO;
import com.qianwen.smartman.modules.smis.dto.WorkstationAlarmDTO;
import com.qianwen.smartman.modules.smis.entity.GlobalWcs;
import com.qianwen.smartman.modules.smis.entity.Workstation;
import com.qianwen.smartman.modules.smis.entity.WorkstationWcs;
import com.qianwen.smartman.modules.smis.service.IGlobalWcsService;
import com.qianwen.smartman.modules.smis.service.IWorkstationService;
import com.qianwen.smartman.modules.smis.service.IWorkstationWcsService;
import com.qianwen.smartman.modules.smis.vo.RealTimeStatusVO;
import com.qianwen.smartman.modules.mdc.entity.SuperAggregateState;
import com.qianwen.smartman.modules.mdc.enums.OpenTypeEnums;
import com.qianwen.smartman.modules.mdc.enums.ProductivityTypeEnum;
import com.qianwen.smartman.modules.mdc.mapper.SuperAggregateStateMapper;
import com.qianwen.smartman.modules.mdc.service.ISuperAggregateStateService;
import com.qianwen.smartman.modules.mdc.utils.FilterOffUtils;
import com.qianwen.smartman.modules.mdc.vo.StatusTimeTopVO;
import com.qianwen.smartman.modules.system.entity.SeriesItem;
import com.qianwen.smartman.modules.system.vo.ChartDataTableVO;
import com.qianwen.smartman.modules.system.vo.ChartGanttStatusDataVO;
import com.qianwen.smartman.modules.system.vo.ChartNameValueDataVO;
import com.qianwen.smartman.modules.system.vo.ChartSeriesDataVO;
import com.qianwen.smartman.modules.system.vo.ICard;
import com.qianwen.smartman.modules.system.vo.WorkstationChartSeriesDataVO;
import com.qianwen.smartman.modules.visual.dto.VisualTimeDTO;
import com.qianwen.smartman.modules.visual.service.IVisualStatusService;
import com.qianwen.smartman.modules.visual.utils.VisualUtils;
import com.qianwen.smartman.modules.visual.vo.TimeStatusVO;
import com.qianwen.smartman.modules.visual.vo.VisualBaseVO;
import com.qianwen.smartman.modules.visual.vo.VisualWorkStationStatusVO;
import com.qianwen.smartman.modules.visual.vo.WorkStationAlarmVO;
import com.qianwen.smartman.modules.visual.wrapper.StatusWrapper;
 
@Service
 
public class VisualStatusServiceImpl implements IVisualStatusService {
    private final ISuperAggregateStateService superAggregateStateService;
    private final IWorkstationService workstationService;
    private final StatusWrapper wrapper;
    private final SuperAggregateStateMapper superAggregateStateMapper;
    private final IGlobalWcsService globalWcsService;
    private final IWorkstationWcsService workstationWcsService;
    private final Integer OUT_OF_SHIFT = -1;
 
    public VisualStatusServiceImpl(final ISuperAggregateStateService superAggregateStateService, final IWorkstationService workstationService, final StatusWrapper wrapper, final SuperAggregateStateMapper superAggregateStateMapper, final IGlobalWcsService globalWcsService, final IWorkstationWcsService workstationWcsService) {
        this.superAggregateStateService = superAggregateStateService;
        this.workstationService = workstationService;
        this.wrapper = wrapper;
        this.superAggregateStateMapper = superAggregateStateMapper;
        this.globalWcsService = globalWcsService;
        this.workstationWcsService = workstationWcsService;
    }
 
    
    public ChartGanttStatusDataVO getVisualStatusRecord(VisualBaseVO visualBaseVO) {
        List<Long> workStationIdList = visualBaseVO.getWorkStationIdList();
        String timeLevel = visualBaseVO.getTimeLevel();
        VisualTimeDTO visualTimeDTO = VisualUtils.getTimeSectionByLevel(timeLevel);
        List<Workstation> workstations = this.workstationService.workStationList(workStationIdList);
        List<SuperAggregateState> superAggregateStateList = this.superAggregateStateService.getStatusByTimeSection(workStationIdList, visualTimeDTO.getStartTime(), visualTimeDTO.getEndTime());
        return this.wrapper.entityVO(workstations, superAggregateStateList);
    }
 
    
    public ChartSeriesDataVO getDeviceStatusDuration(TimeStatusVO timeStatusVO, Integer status) {
        if (Func.isEmpty(timeStatusVO.getWorkStationIdList())) {
            return new ChartSeriesDataVO();
        }
        List<Long> workStationIdList = timeStatusVO.getWorkStationIdList();
        String timeLevel = timeStatusVO.getTimeLevel();//小时数,如24小时
        Integer top = timeStatusVO.getTop();//查询多少个
        VisualTimeDTO visualTimeDTO = VisualUtils.getTimeSectionByLevel(timeLevel);
        List<StatusTimeTopVO> statusTimeByWcs = this.superAggregateStateService.getStatusTimeByWcs(workStationIdList, visualTimeDTO.getStartTime(), visualTimeDTO.getEndTime(), status, top);
        return this.wrapper.deviceStatusDuration(statusTimeByWcs, status, top);
    }
 
    
    public ChartNameValueDataVO getStatusDistribution(VisualBaseVO vo) {
        List<Long> workStationIdList = vo.getWorkStationIdList();
        String timeLevel = vo.getTimeLevel();
        VisualTimeDTO visualTimeDTO = VisualUtils.getTimeSectionByLevel(timeLevel);
        List<SuperAggregateState> data = this.superAggregateStateService.getStatusByTimeSection(workStationIdList, visualTimeDTO.getStartTime(), visualTimeDTO.getEndTime());
        return this.wrapper.entityToChartName(data);
    }
 
    
    public ChartNameValueDataVO getOeeAnalysis(VisualBaseVO vo) {
        List<Long> workStationIdList = vo.getWorkStationIdList();
        String timeLevel = vo.getTimeLevel();
        VisualTimeDTO visualTimeDTO = VisualUtils.getTimeSectionByLevel(timeLevel);
        List<SuperAggregateState> data = this.superAggregateStateService.getOeeAnalysis(workStationIdList, visualTimeDTO.getStartTime(), visualTimeDTO.getEndTime());
        return this.wrapper.entityToOee(data);
    }
 
    /*
    public ChartDataTableVO getWorkstationRealStatus(VisualBaseVO visualBaseVO) {
        List<Long> workStationList = visualBaseVO.getWorkStationIdList();
        Map<String, Object> header = new LinkedHashMap<>();
        int[] index = {0};
        if (workStationList == null) {
            workStationList = Collections.EMPTY_LIST;
        }
        List<ICard> list = workStationList.stream().map(item -> {
            return this.workstationService.getDetail(item);
        }).map(item2 -> {
            return new VisualWorkStationStatusVO(item2.getId(), item2.getMachineId(), item2.getName(), null, item2.getAvatar());
        }).map(item3 -> {
            List<RealTimeStatusVO> realTimeProperties = this.workstationService.getRealTimeProperties(item3.getId(), item3.getMachineId());
            realTimeProperties.stream().filter((v0) -> {
                return v0.getBigScreen();
            }).map(data -> {
                item3.put(data.getKey() + index[0], data.getValue());
                return data;
            }).filter(data2 -> {
                return !WorkstationParamTypeEnum.isAlarm(data2.getWcsDataType());
            }).filter(data3 -> {
                return WorkstationParamTypeEnum.of(data3.getWcsDataType()) != WorkstationParamTypeEnum.STATE;
            }).forEach(data4 -> {
                header.put(data4.getKey() + index[0], data4.getName());
                index[0] = index[0] + 1;
            });
            DefaultWcsEnum wcsEnum = getWorkstationDefaultWcsEnum(realTimeProperties);
            item3.setBkColor(wcsEnum == null ? null : WorkstationCache.getDefaultWcsColor(Func.toStr(wcsEnum.getCode())));
            item3.setStatusCode(wcsEnum == null ? null : wcsEnum.getCode().toString());
            return item3;
        }).collect(Collectors.toList());
        ChartDataTableVO chartDataTableVO = new ChartDataTableVO().setData(list).setHeader(header);
        return chartDataTableVO;
    }*/
 
    
    public DefaultWcsEnum getWorkstationDefaultWcsEnum(final List<RealTimeStatusVO> realTimeProperties) {
        return (DefaultWcsEnum) realTimeProperties.stream().filter(p -> {
            return p.getWcsDataType() != null;
        }).filter(p2 -> {
            return WorkstationParamTypeEnum.STATE.getType().equals(p2.getWcsDataType());
        }).filter((v0) -> {
            return v0.getBigScreen();
        }).findFirst().map(data -> {
            return Integer.valueOf(Func.toInt(data.getValue()));
        }).map(value -> {
            return DefaultWcsEnum.ofDefault(value, DefaultWcsEnum.OFFLINE);
        }).orElse(null);
    }
 
    /*
    public ChartDataTableVO getWorkstationAlarm(final VisualBaseVO vo) {
        List<Long> workStationList = vo.getWorkStationIdList();
        List<ICard> voList = new ArrayList<>();
        workStationList.stream().map(item -> {
            return this.workstationService.getDetail(item);
        }).map(item2 -> {
            List<RealTimeStatusVO> realTimeProperties = this.workstationService.getRealTimeProperties(item2.getId(), item2.getMachineId());
            realTimeProperties.stream().filter(p -> {
                return WorkstationParamTypeEnum.ALARM.getType().equals(p.getWcsDataType()) || WorkstationParamTypeEnum.ALARM_NO.getType().equals(p.getWcsDataType());
            }).forEach(data -> {
                List<WorkstationAlarmDTO> list;
                if (Func.isEmpty(data.getValue())) {
                    list = Collections.emptyList();
                } else if (data.getValue().startsWith("{")) {
                    list = Collections.singletonList(JSON.parseObject(data.getValue(), WorkstationAlarmDTO.class));
                } else {
                    list = (List) JSON.parseObject(data.getValue(), new TypeReference<List<WorkstationAlarmDTO>>() { // from class: org.springblade.modules.visual.service.impl.VisualStatusServiceImpl.1
                    }, new Feature[0]);
                }
                List<WorkstationAlarmDTO> alarmDTOList = list;
                alarmDTOList.stream().forEach(alarm -> {
                    voList.add(new WorkStationAlarmVO(item2.getName(), alarm.getCode(), alarm.getMsg()));
                });
            });
            return item2;
        }).collect(Collectors.toList());
        ChartDataTableVO chartDataTableVO = new ChartDataTableVO().setData(voList).addHeader("code", "报警编码").addHeader(CommonConstant.ALARM_MSG, "报警信息");
        return chartDataTableVO;
    }
    */
    
    public ChartNameValueDataVO getAverageEfficiencyAnalysis(VisualBaseVO vo, ProductivityTypeEnum productivityTypeEnum) {
        List<Long> workStationIdList = vo.getWorkStationIdList();
        List<Workstation> workstations = this.workstationService.listByIds(workStationIdList);
        Date date = new Date();
        if (Func.isNotEmpty(workstations)) {
            String calendarCode = workstations.get(0).getCalendarCode();
            CalendarShiftTimeSlicesDTO timeSliceShift = TimeSliceCache.getTimeSliceShift(calendarCode, date);
            if (Func.isNotEmpty(timeSliceShift)) {
                String factoryDate = timeSliceShift.getFactoryDate();
                Integer shiftIndex = timeSliceShift.getShiftIndex();
                if (this.OUT_OF_SHIFT.equals(shiftIndex)) {
                    return buildEmptyChartNameValueDataVO(productivityTypeEnum);
                }
                List<SuperAggregateState> shiftStatusData = this.superAggregateStateService.getShiftStatusData(workStationIdList, factoryDate, shiftIndex);
                return this.wrapper.entityToEfficiencyAverageVo(FilterOffUtils.filterOffDay(shiftStatusData, OpenTypeEnums.OEE), productivityTypeEnum);
            }
        }
        return buildEmptyChartNameValueDataVO(productivityTypeEnum);
    }
 
    public ChartNameValueDataVO buildEmptyChartNameValueDataVO(ProductivityTypeEnum productivityTypeEnum) {
        ChartNameValueDataVO chartNameValueDataVO = new ChartNameValueDataVO();
        switch (productivityTypeEnum) {
            case ALARM:
                chartNameValueDataVO.addValue("平均报警率", "");
                break;
            case RUNNING:
                chartNameValueDataVO.addValue("平均运行率", "");
                break;
            default:
                chartNameValueDataVO.addValue("平均稼动率", "");
                break;
        }
        return chartNameValueDataVO;
    }
 
    
    public ChartSeriesDataVO getEfficiencyAnalysisShiftTop(TimeStatusVO timeStatusVO, ProductivityTypeEnum productivityTypeEnum) {
        List<Workstation> workstations = this.workstationService.listByIds(timeStatusVO.getWorkStationIdList());
        Date date = new Date();
        if (Func.isNotEmpty(workstations)) {
            String calendarCode = workstations.get(0).getCalendarCode();
            CalendarShiftTimeSlicesDTO timeSliceShift = TimeSliceCache.getTimeSliceShift(calendarCode, date);
            if (Func.isNotEmpty(timeSliceShift)) {
                String factoryDate = timeSliceShift.getFactoryDate();
                Integer shiftIndex = timeSliceShift.getShiftIndex();
                if (this.OUT_OF_SHIFT.equals(shiftIndex)) {
                    return new ChartSeriesDataVO();
                }
                List<SuperAggregateState> shiftStatusData = this.superAggregateStateService.getShiftStatusData(timeStatusVO.getWorkStationIdList(), factoryDate, shiftIndex);
                return this.wrapper.entityToEfficiencyTopVo(FilterOffUtils.filterOffDay(shiftStatusData, OpenTypeEnums.OEE), productivityTypeEnum, timeStatusVO.getTop(), workstations, "当前班次稼动率");
            }
        }
        return new ChartSeriesDataVO();
    }
 
    
    public ChartNameValueDataVO getGroupRunning(VisualBaseVO vo, ProductivityTypeEnum productivityTypeEnum) {
        List<Long> workStationIdList = vo.getWorkStationIdList();
        if (Func.isEmpty(workStationIdList)) {
            return new ChartNameValueDataVO().addValue(MdcConstant.GROUP_RUNNING, "");
        }
        List<Workstation> workstations = this.workstationService.listByIds(workStationIdList);
        if (Func.isEmpty(workstations)) {
            return new ChartNameValueDataVO().addValue(MdcConstant.GROUP_RUNNING, "");
        }
        Date date = new Date();
        String calendarCode = (String) workstations.stream().map((v0) -> {
            return v0.getCalendarCode();
        }).filter(c -> {
            return Func.isNotEmpty(c) && !CalendarConstant.DEFAULT_CALENDAR.equals(c);
        }).distinct().findAny().orElse("");
        CalendarShiftTimeSlicesDTO timeSliceShift = TimeSliceCache.getTimeSliceShift(calendarCode, date);
        if (Func.isNotEmpty(timeSliceShift) && !timeSliceShift.getShiftIndex().equals(-1)) {
            String factoryDate = timeSliceShift.getFactoryDate();
            Integer shiftIndex = timeSliceShift.getShiftIndex();
            if (this.OUT_OF_SHIFT.equals(shiftIndex)) {
                return new ChartNameValueDataVO().addValue(MdcConstant.GROUP_RUNNING, "");
            }
            List<SuperAggregateState> shiftStatusData = this.superAggregateStateService.getShiftStatusData(workStationIdList, factoryDate, shiftIndex);
            Integer code = DefaultWcsEnum.RUNNING.getCode();
            List<SuperAggregateState> aggregateStates = (List) shiftStatusData.stream().filter(c2 -> {
                return code.equals(c2.getWcs());
            }).collect(Collectors.toList());
            return this.wrapper.entityToGroupRunning(aggregateStates, Integer.valueOf(workstations.size()));
        }
        return new ChartNameValueDataVO().addValue(MdcConstant.GROUP_RUNNING, "");
    }
 
    
    public ChartGanttStatusDataVO getNatureStatusRecord(VisualBaseVO visualBaseVO) {
        List<Long> workStationIdList = visualBaseVO.getWorkStationIdList();
        List<Workstation> workstations = this.workstationService.workStationList(workStationIdList);
        LocalDateTime now = LocalDateTime.now();
        List<SuperAggregateState> superAggregateStateList = this.superAggregateStateService.getStatusByCondition(workStationIdList, LocalDateTimeUtils.getDayStart(now), LocalDateTimeUtils.getDayEnd(now));
        return this.wrapper.entityVO(workstations, superAggregateStateList);
    }
 
    
    public ChartNameValueDataVO getStatusToDayDistribution(VisualBaseVO vo) {
        List<Long> workStationIdList = vo.getWorkStationIdList();
        LocalDate now = LocalDate.now();
        LocalDateTime startTime = LocalDateTime.of(now, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(now, LocalTime.MAX);
        List<SuperAggregateState> data = this.superAggregateStateService.getStatusByCondition(workStationIdList, startTime, endTime);
        return this.wrapper.entityToChartName(data);
    }
 
    
    public ChartNameValueDataVO getStatusShiftDistribution(VisualBaseVO vo) {
        List<Long> workStationIdList = vo.getWorkStationIdList();
        List<Workstation> workstations = this.workstationService.listByIds(workStationIdList);
        List<GlobalWcs> defaultWcs = WorkstationCache.getDefaultWcs();
        ChartNameValueDataVO res = new ChartNameValueDataVO();
        if (Func.isEmpty(workstations)) {
            defaultWcs.forEach(w -> {
                res.addValue(w.getName(), Double.valueOf(0.0d), "", w.getColor());
            });
            return res;
        }
        List<SuperAggregateState> states = Lists.newArrayList();
        Map<String, List<Workstation>> calendarCodeMap = workstations.stream().filter(c -> {
            return Func.isNotEmpty(c.getCalendarCode());
        }).collect(Collectors.groupingBy((v0) -> {
            return v0.getCalendarCode();
        }));
        calendarCodeMap.forEach((calendarCode, workstationList) -> {
            CalendarShiftTimeSlicesDTO timeSliceShift = TimeSliceCache.getTimeSliceShift(calendarCode, DateUtil.now());
            List<Long> ids =  workstationList.stream().map((v0) -> {
                return v0.getId();
            }).collect(Collectors.toList());
            if (Func.isNotEmpty(timeSliceShift) && !this.OUT_OF_SHIFT.equals(timeSliceShift.getShiftIndex())) {
                String factoryDate = timeSliceShift.getFactoryDate();
                Integer shiftIndex = timeSliceShift.getShiftIndex();
                List<SuperAggregateState> data = this.superAggregateStateService.getStatusByFactory(ids, factoryDate, shiftIndex);
                states.addAll(data);
            }
        });
        if (Func.isNotEmpty(states)) {
            return this.wrapper.entityToChartName(states);
        }
        return res;
    }
 
    
    public ChartGanttStatusDataVO getEquipmentStatusRecord(VisualBaseVO visualBaseVO) {
        List<Long> workStationIdList = visualBaseVO.getWorkStationIdList();
        List<SuperAggregateState> stateList = new ArrayList<>();
        List<Workstation> workstationList = this.workstationService.list(Wrappers.<Workstation>lambdaQuery().in(Workstation::getId, workStationIdList));
        /*
        List<Workstation> workstationList = this.workstationService.list((Wrapper) Wrappers.lambdaQuery().in((v0) -> {
            return v0.getId();
        }, workStationIdList));*/
        if (Func.isNotEmpty(workstationList)) {
            Map<String, List<Workstation>> calendarCodeMap = workstationList.stream().collect(Collectors.groupingBy((v0) -> {
                return v0.getCalendarCode();
            }));
            calendarCodeMap.forEach((k, v) -> {
                List<Long> workstationIds = v.stream().map((v0) -> {
                    return v0.getId();
                }).collect(Collectors.toList());
                CalendarShiftTimeSlicesDTO timeSliceShift = TimeSliceCache.getTimeSliceShift(k, new Date());
                if (timeSliceShift != null && !this.OUT_OF_SHIFT.equals(timeSliceShift.getShiftIndex())) {
                    List<SuperAggregateState> superAggregateOutputList = this.superAggregateStateMapper.getEquipmentStatusRecord(workstationIds, timeSliceShift.getShiftIndex(), Integer.valueOf(Func.toInt(timeSliceShift.getFactoryDate().replaceAll("-", ""))));
                    if (Func.isNotEmpty(superAggregateOutputList)) {
                        stateList.addAll(superAggregateOutputList);
                    }
                }
            });
        }
        List<SuperAggregateState> collect = stateList.stream().peek(s -> {
            if (Func.isNull(s.getEndTime())) {
                s.setEndTime(new Timestamp(DateUtil.now().getTime()));
            }
        }).collect(Collectors.toList());
        return this.wrapper.entityVO(workstationList, collect);
    }
 
    
    public ChartSeriesDataVO getTodayStatusStatistics(VisualBaseVO vo, ProductivityTypeEnum productivityTypeEnum) {
        if (Func.isEmpty(vo.getWorkStationIdList())) {
            return new ChartSeriesDataVO();
        }
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime dayStart = LocalDateTimeUtils.getDayStart(now);
        LocalDateTime dayEnd = LocalDateTimeUtils.getDayEnd(now);
        List<Workstation> workstations = this.workstationService.workStationList(vo.getWorkStationIdList());
        List<SuperAggregateState> statusByCondition = this.superAggregateStateService.getStatusByCondition(vo.getWorkStationIdList(), dayStart, dayEnd);
        List<SuperAggregateState> collect = (List) FilterOffUtils.filterOffDay(statusByCondition, OpenTypeEnums.OEE).stream().filter(s -> {
            return s.getRps().intValue() > 0;
        }).collect(Collectors.toList());
        return this.wrapper.entityToEfficiencyTopVo(collect, productivityTypeEnum, Integer.valueOf(workstations.size()), workstations, "自然日设备稼动率");
    }
 
    
    public ChartNameValueDataVO getTodayAverageStatusStatistics(VisualBaseVO vo, ProductivityTypeEnum productivityTypeEnum) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime dayStart = LocalDateTimeUtils.getDayStart(now);
        LocalDateTime dayEnd = LocalDateTimeUtils.getDayEnd(now);
        List<SuperAggregateState> statusByCondition = this.superAggregateStateService.getStatusByCondition(vo.getWorkStationIdList(), dayStart, dayEnd);
        List<SuperAggregateState> collect = FilterOffUtils.filterOffDay(statusByCondition, OpenTypeEnums.OEE).stream().filter(s -> {
            return s.getRps().intValue() > 0;
        }).collect(Collectors.toList());
        return this.wrapper.entityToEfficiencyAverageVo(collect, productivityTypeEnum);
    }
 
    
    public ChartSeriesDataVO getEfficiencyAnalysisTop(TimeStatusVO timeStatusVO, ProductivityTypeEnum productivityTypeEnum) {
        List<Long> workStationIdList = timeStatusVO.getWorkStationIdList();
        if (Func.isEmpty(workStationIdList)) {
            return new ChartSeriesDataVO();
        }
        List<Workstation> workstations = this.workstationService.listByIds(workStationIdList);
        String timeLevel = timeStatusVO.getTimeLevel();
        VisualTimeDTO visualTimeDTO = VisualUtils.getTimeSectionByLevel(timeLevel);
        List<SuperAggregateState> statusByCondition = this.superAggregateStateService.getStatusByTimeSection(timeStatusVO.getWorkStationIdList(), visualTimeDTO.getStartTime(), visualTimeDTO.getEndTime());
        List<SuperAggregateState> collect = FilterOffUtils.filterOffDay(statusByCondition, OpenTypeEnums.OEE).stream().filter(s -> {
            return s.getRps().intValue() > 0;
        }).collect(Collectors.toList());
        return this.wrapper.entityToEfficiencyTopVo(collect, productivityTypeEnum, timeStatusVO.getTop(), workstations, StringUtil.format("近{}小时稼动率排名", new Object[]{timeLevel}));
    }
 
    
    public ChartNameValueDataVO getCurrMonthOee(VisualBaseVO vo) {
        List<Long> ids = vo.getWorkStationIdList();
        if (Func.isEmpty(ids)) {
            return new ChartNameValueDataVO().addValue(MdcConstant.MONTH_OEE_NAME, Double.valueOf(0.0d));
        }
        LocalDate now = LocalDate.now();
        int monthValue = now.getMonthValue();
        List<SuperAggregateState> data = this.superAggregateStateService.getCurrMonthState(ids, Integer.valueOf(monthValue));
        return this.wrapper.entityToMonthOee(FilterOffUtils.filterOffDay(data, OpenTypeEnums.OEE));
    }
 
    
    public ChartNameValueDataVO getCurrMonthRunning(VisualBaseVO vo) {
        List<Long> ids = vo.getWorkStationIdList();
        if (Func.isEmpty(ids)) {
            return new ChartNameValueDataVO().addValue(MdcConstant.MONTH_RUNNING_NAME, Double.valueOf(0.0d));
        }
        LocalDate now = LocalDate.now();
        int monthValue = now.getMonthValue();
        List<SuperAggregateState> data = this.superAggregateStateService.getCurrMonthState(ids, Integer.valueOf(monthValue));
        return this.wrapper.entityToMonthRunning(data);
    }
 
    
    public ChartSeriesDataVO getCurrMonthAlarm(VisualBaseVO vo) {
        List<Long> ids = vo.getWorkStationIdList();
        if (Func.isEmpty(ids)) {
            ChartSeriesDataVO res = new ChartSeriesDataVO();
            res.setCategories(Lists.newArrayList());
            res.setSeries(Lists.newArrayList());
            return res;
        }
        List<Workstation> workstations = this.workstationService.listByIds(ids);
        if (Func.isEmpty(workstations)) {
            ChartSeriesDataVO res2 = new ChartSeriesDataVO();
            res2.setCategories(Lists.newArrayList());
            res2.setSeries(Lists.newArrayList());
            return res2;
        }
        Map<Long, Workstation> workstationMap = (Map) workstations.stream().collect(Collectors.toMap((v0) -> {
            return v0.getId();
        }, v -> {
            return v;
        }));
        List<Long> workIds =  workstations.stream().map((v0) -> {
            return v0.getId();
        }).collect(Collectors.toList());
        LocalDate now = LocalDate.now();
        int monthValue = now.getMonthValue();
        List<SuperAggregateState> data = this.superAggregateStateService.getCurrMonthAlarmState(workIds, Integer.valueOf(monthValue), DefaultWcsEnum.ALARM.getCode());
        return getChartSeriesDataVO(workstations, workstationMap, FilterOffUtils.filterOffDay(data, OpenTypeEnums.TIME_USED_ANALYSIS), "月设备报警时长排名");
    }
 
    private ChartSeriesDataVO getChartSeriesDataVO(List<Workstation> workstations, Map<Long, Workstation> workstationMap, List<SuperAggregateState> data, String name) {
        Map<Long, Long> workMap = data.stream().collect(Collectors.groupingBy((v0) -> {
            return v0.getWorkstationId();
        }, TreeMap::new, Collectors.mapping((v0) -> {
            return v0.getDurationCollect();
        }, Collectors.reducing(0L, (v0, v1) -> {
            return Long.sum(v0, v1);
        }))));
        List<Map.Entry<Long, Long>> list = Lists.newArrayList(workMap.entrySet());
        list.sort((o1, o2) -> {
            return Math.toIntExact(((Long) o2.getValue()).longValue() - ((Long) o1.getValue()).longValue());
        });
        WorkstationChartSeriesDataVO res = new WorkstationChartSeriesDataVO();
        List<Workstation> codes = Lists.newArrayList();
        SeriesItem<String> item = new SeriesItem<>();
        List<String> xData = Lists.newArrayList();
        for (Map.Entry<Long, Long> entry : list) {
            Long id = entry.getKey();
            Workstation workstation = workstationMap.get(id);
            if (Func.notNull(workstation)) {
                codes.add(workstation);
                xData.add(secondToHour(entry.getValue()) + "");
            }
        }
        for (Workstation workstation2 : workstations) {
            Long duration = workMap.get(workstation2.getId());
            if (Func.isNull(duration)) {
                codes.add(workstation2);
                xData.add("0");
            }
        }
        item.setName(name);
        item.setData(xData);
        res.generateCategories(codes);
        res.setSeries(Lists.newArrayList(new SeriesItem[]{item}));
        return res;
    }
 
    
    public ChartSeriesDataVO getCurrWeekAlarm(VisualBaseVO vo) {
        List<Long> ids = vo.getWorkStationIdList();
        if (Func.isEmpty(ids)) {
            ChartSeriesDataVO res = new ChartSeriesDataVO();
            res.setCategories(Lists.newArrayList());
            res.setSeries(Lists.newArrayList());
            return res;
        }
        Integer week = LocalDateTimeUtils.getWeek(LocalDate.now());
        List<Workstation> workstations = this.workstationService.listByIds(ids);
        if (Func.isEmpty(workstations)) {
            ChartSeriesDataVO res2 = new ChartSeriesDataVO();
            res2.setCategories(Lists.newArrayList());
            res2.setSeries(Lists.newArrayList());
            return res2;
        }
        List<Long> workIds = workstations.stream().map((v0) -> {
            return v0.getId();
        }).collect(Collectors.toList());
        List<SuperAggregateState> data = this.superAggregateStateService.getCurrWeekAlarmState(workIds, week, DefaultWcsEnum.ALARM.getCode());
        List<SuperAggregateState> data2 = FilterOffUtils.filterOffDay(data, OpenTypeEnums.TIME_USED_ANALYSIS);
        Map<Long, Workstation> workstationMap = workstations.stream().collect(Collectors.toMap((v0) -> {
            return v0.getId();
        }, v -> {
            return v;
        }));
        return getChartSeriesDataVO(workstations, workstationMap, data2, "周设备报警时长排名");
    }
 
    private double secondToHour(Long second) {
        BigDecimal bg = new BigDecimal(second.longValue());
        BigDecimal bg2 = new BigDecimal(3600000);
        return bg.divide(bg2, 2, RoundingMode.HALF_UP).doubleValue();
    }
 
    
    public ChartNameValueDataVO getWorkstationRealStatusChart(VisualBaseVO vo) {
        Map<Long, String> settings = this.workstationWcsService.list(Lambda.<WorkstationWcs>in(WorkstationWcs::getWorkstationId, vo.getWorkStationIdList())
                .eq(WorkstationWcs::getDataType, WcsDataTypeEnums.WcsDataType.STATE.getCode()).select(WorkstationWcs::getCollectSettingItem, WorkstationWcs::getWorkstationId ))
                .stream().collect(Collectors.toMap(WorkstationWcs::getWorkstationId, WorkstationWcs::getCollectSettingItem));
        /*
        Map<Long, String> settings = (Map) this.workstationWcsService.list(((ExtraLambdaQueryWrapper) Lambda.in((v0) -> {
            return v0.getWorkstationId();
        }, vo.getWorkStationIdList()).eq((v0) -> {
            return v0.getDataType();
        }, WcsDataTypeEnums.WcsDataType.STATE.getCode())).select((v0) -> {
            return v0.getCollectSettingItem();
        }, (v0) -> {
            return v0.getWorkstationId();
        })).stream().collect(Collectors.toMap((v0) -> {
            return v0.getWorkstationId();
        }, (v0) -> {
            return v0.getCollectSettingItem();
        }));*/
        Map<String, Long> statusCollect = vo.getWorkStationIdList().stream().map(id -> {
            return WorkstationCache.getWorkstationRealTime(id, (String) settings.getOrDefault(id, MdcConstant.DEVICE_STATUS));
        }).filter((v0) -> {
            return Objects.nonNull(v0);
        }).collect(Collectors.groupingBy((v0) -> {
            return v0.getV();
        }, Collectors.counting()));
        ChartNameValueDataVO chartNameValueDataVO = new ChartNameValueDataVO();
        this.globalWcsService.getGlobalWcsList(GlobalWcsTypeEnum.DEFAULT).forEach(item -> {
            long count = ((Long) statusCollect.getOrDefault(item.getCode(), 0L)).longValue();
            chartNameValueDataVO.addValue(item.getName(), Long.valueOf(count), Integer.parseInt(item.getCode()));
        });
        return chartNameValueDataVO;
    }
}