yangys
2024-11-15 d66fe6d46cdbaeb88e68ad96da6deb0b35cd131b
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
package com.qianwen.smartman.modules.mdc.service.impl;
 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.qianwen.core.log.exception.ServiceException;
import com.qianwen.core.mp.support.Query;
import com.qianwen.core.oss.model.BladeFile;
import com.qianwen.core.tool.jackson.JsonUtil;
import com.qianwen.core.tool.utils.DateUtil;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.smartman.common.cache.cps.TimeSliceCache;
import com.qianwen.smartman.common.cache.cps.WorkstationCache;
import com.qianwen.smartman.common.constant.DateConstant;
import com.qianwen.smartman.common.constant.ExcelConstant;
import com.qianwen.smartman.common.constant.MdcConstant;
import com.qianwen.smartman.common.enums.WcsDataTypeEnums;
import com.qianwen.smartman.common.utils.ListUtils;
import com.qianwen.smartman.common.utils.MessageUtils;
import com.qianwen.smartman.modules.smis.dto.CalendarShiftDTO;
import com.qianwen.smartman.modules.smis.dto.CalendarShiftTimeSlicesDTO;
import com.qianwen.smartman.modules.smis.dto.WorkstationWcsDmpDTO;
import com.qianwen.smartman.modules.smis.entity.GlobalWcs;
import com.qianwen.smartman.modules.smis.entity.Workstation;
import com.qianwen.smartman.modules.smis.service.ICalendarService;
import com.qianwen.smartman.modules.smis.service.IWorkstationService;
import com.qianwen.smartman.modules.smis.service.WorkstationDatapointsService;
import com.qianwen.smartman.modules.smis.utils.ThrowFun;
import com.qianwen.smartman.modules.smis.vo.ShiftTimeDetailVO;
import com.qianwen.smartman.modules.smis.vo.WorkstationDatapointsVO;
import com.qianwen.smartman.modules.mdc.dto.DatapointDTO;
import com.qianwen.smartman.modules.mdc.dto.GroupWorkDTO;
import com.qianwen.smartman.modules.mdc.dto.NewParamDTO;
import com.qianwen.smartman.modules.mdc.dto.ParamDTO;
import com.qianwen.smartman.modules.mdc.dto.ProParamResDTO;
import com.qianwen.smartman.modules.mdc.dto.ProcessParameterRealVO;
import com.qianwen.smartman.modules.mdc.dto.ProcessParameterVO;
import com.qianwen.smartman.modules.mdc.dto.ProcessWcsDTO;
import com.qianwen.smartman.modules.mdc.dto.StatusDTO;
import com.qianwen.smartman.modules.mdc.entity.WorkstationCollectData;
import com.qianwen.smartman.modules.mdc.enums.ProcessParamEnum;
//import com.qianwen.smartman.modules.mdc.mapper.SuperCollectJsonMapper;
import com.qianwen.smartman.modules.mdc.mapper.SuperProcessParameterMapper;
import com.qianwen.smartman.modules.mdc.service.IProcessParameterService;
import com.qianwen.smartman.modules.mdc.service.ProcessParameterHelperService;
import com.qianwen.smartman.modules.mdc.utils.ExcelStrategyUtil;
import com.qianwen.smartman.modules.mdc.vo.AllShiftTimeDetail;
import com.qianwen.smartman.modules.mdc.vo.CollectParamResVO;
import com.qianwen.smartman.modules.mdc.vo.CollectParamSearchVO;
import com.qianwen.smartman.modules.mdc.vo.ProParamSheetVO;
import com.qianwen.smartman.modules.mdc.vo.ProcessParameterItemSearchVO;
import com.qianwen.smartman.modules.mdc.vo.ProcessParameterResVO;
import com.qianwen.smartman.modules.mdc.vo.ProcessParameterSearchVO;
import com.qianwen.smartman.modules.mdc.vo.ShiftTimeVO;
import com.qianwen.smartman.modules.mdc.vo.WorkstationShiftSearchVO;
import com.qianwen.smartman.modules.mdc.vo.excel.ProcessParamExcelVO;
import com.qianwen.smartman.modules.resource.builder.oss.OssBuilder;
 
import cn.hutool.core.lang.Tuple;
import cn.hutool.core.util.ObjectUtil;
 
@Service
public class ProcessParameterServiceImpl implements IProcessParameterService {
    private static final Logger log = LoggerFactory.getLogger(ProcessParameterServiceImpl.class);
    //@Autowired
    //private SuperCollectJsonMapper collectJsonMapper;
    @Autowired
    private IWorkstationService workstationService;
    @Autowired
    private OssBuilder ossBuilder;
    @Autowired
    private SuperProcessParameterMapper parameterMapper;
    @Autowired
    private ICalendarService calendarService;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private WorkstationDatapointsService dpService;
    
    @Autowired
    private ProcessParameterHelperService helperService;
    
    @Override
    public ProcessParameterResVO queryProcessParameterChart(ProcessParameterSearchVO vo) {
        ProcessParamEnum methodEnum = vo.getMethodEnum();
        switch (methodEnum) {
            case HOUR:
                return buildProcessDate(vo);
            case SHIFT:
                return buildProcessShift(vo);
            default:
                return new ProcessParameterResVO();
        }
    }
 
    @Override
    public List<GlobalWcs> listGlobalWcs() {
        return WorkstationCache.getDefaultWcs();
    }
 
    @Override
    public IPage<ProcessParameterRealVO> pageProcessParameter(Query query, ProcessParameterItemSearchVO vo) {
        int size = query.getSize().intValue();
        int current = query.getCurrent().intValue();
        long startTime = vo.getStartTime().getTime();
        long endTime = vo.getEndTime().getTime();
        
        String workstationId = vo.getWorkstationId();
        Integer flag = vo.getFlag();
        String item = vo.getItem();
        long total = Optional.ofNullable(this.parameterMapper.countProcessParameter(item, Long.valueOf(startTime), Long.valueOf(endTime), workstationId)).orElse(0L).longValue();
        Page<ProcessParameterRealVO> page = new Page<>(current, size, total);
        if (total == 0) {
            return page;
        }
        
        List<ProcessParameterVO> res = this.parameterMapper.pageProcessParameter(size,(current - 1) * size, startTime, endTime, item, workstationId);
        Map<String, String> map = getWcsMap();
        List<ProcessParameterRealVO> collect = res.stream().map(c -> {
            String value;
            if (MdcConstant.FLAG.equals(flag)) {
                value = (String) map.get(c.getValue());
            } else {
                value = c.getValue();
            }
            return ProcessParameterRealVO.builder().time(DateUtil.format(c.getTime(), "yyyy-MM-dd HH:mm:ss.SSS")).collectItem(c.getCollectItem()).value(value).build();
        }).collect(Collectors.toList());
        page.setRecords(collect);
        return page;
    }
    /*
    @Override
    public List<WorkstationWcsDmpDTO> processParam(String workstationId) {
        
        
        //获取展示在过程参数中的变量
        List<WorkstationWcsDmpDTO> dps = new ArrayList<>();
        if(ObjectUtil.isEmpty(workstationId)) {
            return Collections.emptyList(); 
        }
        WorkstationDatapointsVO dpVo = dpService.getDatapoints(Long.parseLong(workstationId));
        if(ObjectUtil.isEmpty(dpVo.getDpConfig())) {
            return Collections.emptyList();
        }
        
        JSONArray dpArrJson = JSONArray.parseArray(dpVo.getDpConfig());
        
        String dpName;
        JSONObject dp;
        for(int i=0; i<dpArrJson.size();i++) {
            dp = dpArrJson.getJSONObject(i);
            
            
            Boolean isProcessParam = false;
            if(dp.containsKey("isProcessParam")) {
                isProcessParam = dp.getBoolean("isProcessParam");
            }
            if(!isProcessParam) {
                //不是过程参数显示的,跳过
                continue;
            }
            WorkstationWcsDmpDTO dto = new WorkstationWcsDmpDTO();
            dto.setDescription(dp.getString("dpLabel"));
            
            dpName = dp.getString("dpName");
            dto.setName(dpName);
            
            if("DeviceStatus".equals(dpName)) {
                dto.setWcsDataType(WcsDataTypeEnums.WcsDataType.STATE.getCode());
            }else if("Output".equals(dpName)) {
                dto.setWcsDataType(WcsDataTypeEnums.WcsDataType.YIELD.getCode());
            }
            dps.add(dto);
        }
        return dps;
    }
    */
    @Override
    public List<DatapointDTO> processParams(String workstationId) {
        //获取展示在过程参数中的变量
        if(ObjectUtil.isEmpty(workstationId)) {
            return Collections.emptyList(); 
        }
        WorkstationDatapointsVO dpVo = dpService.getDatapoints(Long.parseLong(workstationId));
        if(ObjectUtil.isEmpty(dpVo.getDpConfig())) {
            return Collections.emptyList();
        }
        
        //JSONArray dpArrJson = JSONArray.parseArray(dpVo.getDpConfig());
        List<DatapointDTO> list = JSONObject.parseArray(dpVo.getDpConfig(),DatapointDTO.class);
        
        return list.stream().filter(dp -> dp.getIsProcessParam()!=null && dp.getIsProcessParam()).collect(Collectors.toList());
        
    }
    /*
    @Override
    public ProParamSheetVO queryProcessParameterSheet(ProcessParameterSearchVO vo, Boolean isFilterProcessParameter) {
        ProcessParamEnum methodEnum = vo.getMethodEnum();
        switch (methodEnum) {
            case HOUR:
                return buildProcessHourSheet(vo, isFilterProcessParameter);
            case SHIFT:
                return buildProcessShiftSheet(vo, isFilterProcessParameter);
            default:
                return new ProParamSheetVO();
        }
    }*/
    /*
    @Override
    public BladeFile exportProcessParam(ProcessParamExcelVO vo) {
        try {
            return exportByHour(vo);
        } catch (Exception e) {
            log.error("exportProcessParam异常",e);
            throw new RuntimeException(e);
        }
    }
    */
    @Override
    public ShiftTimeVO getShiftTime(ProcessParameterSearchVO vo) {
        String workstationId = vo.getWorkstationId();
        LocalDate queryTime = vo.getQueryTime();
        Integer shift = vo.getShift();
        Workstation workstation = (Workstation) this.workstationService.getById(Func.toLong(workstationId));
        return (ShiftTimeVO) Optional.ofNullable(workstation).map(w -> {
            CalendarShiftTimeSlicesDTO timeSliceShift = TimeSliceCache.getTimeSliceShift(workstation.getCalendarCode(), queryTime, shift);
            return (ShiftTimeVO) Optional.ofNullable(timeSliceShift).map(time -> {
                return ShiftTimeVO.builder().startTime(time.getStartTime()).endTime(time.getEndTime()).build();
            }).orElse(new ShiftTimeVO());
        }).orElse(new ShiftTimeVO());
    }
 
    @Override
    public CollectParamResVO<?> queryProcessParameterOneChart(CollectParamSearchVO vo) {
        Date startTime = vo.getStartTime();
        Date endTime = vo.getEndTime();
        if (Func.isNull(startTime) || Func.isNull(endTime)) {
            throw new ServiceException(MessageUtils.message("mdc.process.time.not.null", new Object[0]));
        }
        Date now = DateUtil.now();
        if (startTime.after(now)) {
            return new CollectParamResVO<>();
        }
 
        Long workstationId = Long.parseLong(vo.getWorkstationId());
        List<ProcessParameterVO> processParameterList = getProcessParameterList(startTime, endTime, workstationId, vo.getDpDTO());
        return getCollectParamResVO(vo.getDpDTO(), processParameterList);
    }
 
    private CollectParamResVO<?> getCollectParamResVO(DatapointDTO dpDTO, List<ProcessParameterVO> processParameterList) {//WorkstationWcsDmpDTO dmpDTO
        //状态特殊处理?
        //if (WcsDataTypeEnums.WcsDataType.STATE.getCode().equals(dmpDTO.getWcsDataType())) {//yys
        if ("DeviceStatus".equals(dpDTO.getDpName())) {//状态,需要特殊处理
            Map<String, String> colorMap = Maps.newHashMap();
            Map<String, String> nameMap = Maps.newHashMap();
            Set<String> codes = Sets.newHashSet();
            buildWcsMap(colorMap, nameMap, codes);
            List<StatusDTO> list = processParameterList.stream().filter(c -> codes.contains(c.getValue())).map(param -> StatusDTO.builder().time(DateUtil.formatDateTime(DateUtil.fromMilliseconds(param.getRealTime().longValue()))).name((String)nameMap.get(param.getValue())).color((String)colorMap.get(param.getValue())).value(param.getValue()).build()).collect(Collectors.toList());
            /*
            List<StatusDTO> collect =  processParameterList.stream().filter(c -> {
                return codes.contains(c.getValue());
            }).map(param -> {
                return StatusDTO.builder().time(DateUtil.formatDateTime(DateUtil.fromMilliseconds(param.getRealTime().longValue()))).name((String) nameMap.get(param.getValue())).color((String) colorMap.get(param.getValue())).value(param.getValue()).build();
            }).collect(Collectors.toList());*/
            return CollectParamResVO.<StatusDTO>builder()
                    .collectItem(dpDTO.getDpLabel())
                    .collectRealItem(dpDTO.getDpName())
                    .data(list)
                    .build();
            //return CollectParamResVO.builder().collectItem(dmpDTO.getDescription()).collectRealItem(dmpDTO.getName()).data(list).build();
        }
        //空指针
        List<ParamDTO> collect = processParameterList.stream().map(param -> ParamDTO.builder().time(DateUtil.formatDateTime(DateUtil.fromMilliseconds(param.getRealTime().longValue()))).value(param.getValue()).build()).collect(Collectors.toList());
        /*
        return CollectParamResVO.<ParamDTO>builder()
          .collectItem(dmpDTO.getDescription())
          .collectRealItem(dmpDTO.getName())
          .data(collect)
          .build();
       */
        return CollectParamResVO.<ParamDTO>builder()
                .collectItem(dpDTO.getDpLabel())
                .collectRealItem(dpDTO.getDpName())
                .data(collect)
                .build();
    }
 
    private void buildWcsMap(Map<String, String> colorMap, Map<String, String> nameMap, Set<String> codes) {
        List<GlobalWcs> globalWcsList = listGlobalWcs();
        for (GlobalWcs globalWcs : globalWcsList) {
            colorMap.put(globalWcs.getCode(), globalWcs.getColor());
            nameMap.put(globalWcs.getCode(), globalWcs.getName());
            codes.add(globalWcs.getCode());
        }
    }
 
    /**
     * 获取过程参数列表
     * @param startTime
     * @param endTime
     * @param workstationId
     * @param dmpDTO
     * @return
     */
    private List<ProcessParameterVO> getProcessParameterList(Date startTime, Date endTime, Long workstationId, DatapointDTO dpDTO) {//WorkstationWcsDmpDTO dmpDTO
        ProcessParameterVO firstStatue = oldFirstStatue(startTime, dpDTO.getDpName(), workstationId);
        ProcessParameterVO endStatue = oldLastStatue(endTime, dpDTO.getDpName(), workstationId);
        
        List<ProcessParameterVO> processParameterList = oldOneCollectList(startTime, endTime, dpDTO.getDpName(), workstationId);
        if (Func.isNotEmpty(processParameterList)) {
            if (Func.notNull(firstStatue) && !firstStatue.getRealTime().equals(startTime.getTime())) {
                firstStatue.setRealTime(startTime.getTime());
                processParameterList.add(0, firstStatue);
            }
            if (Func.notNull(endStatue) && !endStatue.getRealTime().equals(Long.valueOf(endTime.getTime()))) {
                endStatue.setRealTime(Long.valueOf(Math.min(DateUtil.now().getTime(), endTime.getTime())));
                processParameterList.add(endStatue);
            }
        } else {
            if (Func.notNull(firstStatue)) {
                firstStatue.setRealTime(Long.valueOf(startTime.getTime()));
                processParameterList.add(firstStatue);
            }
            if (Func.notNull(endStatue)) {
                endStatue.setRealTime(Long.valueOf(Math.min(DateUtil.now().getTime(), endTime.getTime())));
                processParameterList.add(endStatue);
            }
        }
        return processParameterList;
    }
 
    @Override
    public List<AllShiftTimeDetail> getAllShiftTime(WorkstationShiftSearchVO vo) {
        String workstationId = vo.getWorkstationId();
        List<LocalDate> dates = vo.getDates();
        Workstation workstation = (Workstation) this.workstationService.getById(Long.valueOf(Func.toLong(workstationId)));
        LocalDate firstDays = ((LocalDate) ListUtils.getFirst(dates)).minusDays(1L);
        LocalDate lastDays = ((LocalDate) ListUtils.getLast(dates)).plusDays(1L);
        dates.add(firstDays);
        dates.add(lastDays);
        List<ShiftTimeDetailVO> shiftDetailDates = this.calendarService.getShiftDetailDates(workstation.getCalendarCode(),  dates.stream().distinct().collect(Collectors.toList()));
        if (Func.isEmpty(shiftDetailDates)) {
            return Lists.newArrayList();
        }
        return  shiftDetailDates.stream().map(shiftTime -> {
            LocalDate date = shiftTime.getCalendarDate();
            LocalDateTime startTime = LocalDateTime.of(date, LocalTime.MIN).plusMinutes(shiftTime.getShiftStartTime().intValue());
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MIN).plusMinutes(shiftTime.getShiftEndTime().intValue());
            String indexName = shiftTime.getIndexName();
            if (Func.isBlank(indexName)) {
                indexName = MessageUtils.message("calendar.page.shift.model.shift", new Object[0]) + shiftTime.getShiftIndex();
            }
            return AllShiftTimeDetail.builder().color(shiftTime.getColour()).shiftIndex(shiftTime.getShiftIndex()).shiftName(indexName).startTime(DateUtil.toDate(startTime)).endTime(DateUtil.toDate(endTime)).build();
        }).sorted(Comparator.comparing((v0) -> {
            return v0.getStartTime();
        })).collect(Collectors.toList());
    }
 
    private ProcessParameterResVO buildProcessDate(ProcessParameterSearchVO vo) {
        Date startTime = vo.getStartTime();
        Date endTime = vo.getEndTime();
        if (Func.isNull(startTime) || Func.isNull(endTime)) {
            throw new ServiceException(MessageUtils.message("mdc.process.time.not.null", new Object[0]));
        }
        String workstationId = vo.getWorkstationId();
        List<WorkstationWcsDmpDTO> vos = WorkstationCache.getWorkstationWcsList(workstationId).stream().filter((v0) -> {
            return v0.getProcessParameter();
        }).collect(Collectors.toList());
        if (Func.isEmpty(vos)) {
            return new ProcessParameterResVO();
        }
        Map<String, WorkstationWcsDmpDTO> collectItemMap = vos.stream().collect(Collectors.toMap((v0) -> {
            return v0.getName();
        }, v -> {
            return v;
        }));
        log.info("工位的采集项:{}", vos);
        WorkstationWcsDmpDTO wcs = WorkstationCache.getDmpStatus(workstationId);
        List<String> collectItems = vos.stream().map((v0) -> {
            return v0.getName();
        }).collect(Collectors.toList());
        List<ProcessParameterVO> dtoList = oldProcessParameterList(startTime, endTime, collectItems, workstationId);
        if (Func.isEmpty(dtoList)) {
            return new ProcessParameterResVO();
        }
        return entityVO(dtoList, wcs, collectItemMap);
    }
 
    private ProcessParameterResVO buildProcessShift(ProcessParameterSearchVO vo) {
        Integer shift = vo.getShift();
        if (Func.isNull(shift)) {
            throw new ServiceException(MessageUtils.message("mdc.process.shift.not.null", new Object[0]));
        }
        LocalDate queryTime = vo.getQueryTime();
        if (Func.isNull(queryTime)) {
            throw new ServiceException(MessageUtils.message("mdc.process.time.not.null", new Object[0]));
        }
        String workstationId = vo.getWorkstationId();
        Workstation workstation = (Workstation) this.workstationService.getById(workstationId);
        if (Func.isNull(workstation)) {
            return new ProcessParameterResVO();
        }
        List<WorkstationWcsDmpDTO> vos = WorkstationCache.getWorkstationWcsList(workstationId).stream().filter((v0) -> {
            return v0.getProcessParameter();
        }).collect(Collectors.toList());
        if (Func.isEmpty(vos)) {
            return new ProcessParameterResVO();
        }
        Map<String, WorkstationWcsDmpDTO> collectItemMap = vos.stream().collect(Collectors.toMap((v0) -> {
            return v0.getName();
        }, v -> {
            return v;
        }));
        log.info("工位的采集项:{}", vos);
        WorkstationWcsDmpDTO wcs = WorkstationCache.getDmpStatus(workstationId);
        CalendarShiftTimeSlicesDTO timeSliceShift = TimeSliceCache.getTimeSliceShift(workstation.getCalendarCode(), queryTime, shift);
        return (ProcessParameterResVO) Optional.ofNullable(timeSliceShift).map(time -> {
            Date startTime = time.getStartTime();
            Date endTime = time.getEndTime();
            List<String> collectItems = vos.stream().map((v0) -> {
                return v0.getName();
            }).collect(Collectors.toList());
            List<ProcessParameterVO> parameterList = oldProcessParameterList(startTime, endTime, collectItems, workstationId);
            if (Func.isEmpty(parameterList)) {
                return new ProcessParameterResVO();
            }
            return entityVO(parameterList, wcs, collectItemMap);
        }).orElse(new ProcessParameterResVO());
    }
    /*
    private ProParamSheetVO buildProcessHourSheet(ProcessParameterSearchVO vo, Boolean isFilterProcessParameter) {
        Date startTime = vo.getStartTime();
        Date endTime = vo.getEndTime();
        if (Func.isNull(startTime) || Func.isNull(endTime)) {
            throw new ServiceException(MessageUtils.message("mdc.process.time.not.null", new Object[0]));
        }
        String workstationId = vo.getWorkstationId();
        Workstation workstation = (Workstation) this.workstationService.getById(workstationId);
        if (Func.isNull(workstation)) {
            return new ProParamSheetVO();
        }
        List<WorkstationWcsDmpDTO> vos = getWorkstationWcsDmpDTO(isFilterProcessParameter, workstationId);
        if (isFilterProcessParameter.booleanValue()) {
            vos = vos.stream().filter((v0) -> {
                return v0.getProcessParameter();
            }).collect(Collectors.toList());
        }
        if (Func.isEmpty(vos)) {
            return new ProParamSheetVO();
        }
        WorkstationWcsDmpDTO wcs = WorkstationCache.getDmpStatus(workstationId);
        List<NewParamDTO> dtoList = processParameterList(startTime, endTime, workstationId);
        if (Func.isEmpty(dtoList)) {
            return new ProParamSheetVO();
        }
        return buildSheetVO(vos, dtoList, wcs, getWcsMap());
    }*/
    /*
    private ProParamSheetVO buildProcessShiftSheet(ProcessParameterSearchVO vo, Boolean isFilterProcessParameter) {
        Integer shift = vo.getShift();
        LocalDate queryTime = vo.getQueryTime();
        if (Func.isEmpty(queryTime)) {
            throw new ServiceException(MessageUtils.message("mdc.process.time.not.null", new Object[0]));
        }
        String workstationId = vo.getWorkstationId();
        Workstation workstation = (Workstation) this.workstationService.getById(workstationId);
        if (Func.isEmpty(workstation)) {
            return new ProParamSheetVO();
        }
        CalendarShiftTimeSlicesDTO timeSliceShift = TimeSliceCache.getTimeSliceShift(workstation.getCalendarCode(), queryTime, shift);
        if (Func.isEmpty(timeSliceShift)) {
            return new ProParamSheetVO();
        }
        List<WorkstationWcsDmpDTO> vos = getWorkstationWcsDmpDTO(isFilterProcessParameter, workstationId);
        if (Func.isEmpty(vos)) {
            return new ProParamSheetVO();
        }
        WorkstationWcsDmpDTO wcs = WorkstationCache.getDmpStatus(workstationId);
        List<NewParamDTO> dtoList = processParameterList(timeSliceShift.getStartTime(), timeSliceShift.getEndTime(), workstationId);
        if (Func.isEmpty(dtoList)) {
            return new ProParamSheetVO();
        }
        return buildSheetVO(vos, dtoList, wcs, getWcsMap());
    }*/
    /*
    private BladeFile exportByHour(ProcessParamExcelVO vo) throws Exception {
        Date startTime = vo.getStartTime();
        Date endTime = vo.getEndTime();
        ThrowFun.isTrue(Func.isEmpty(startTime) || Func.isEmpty(endTime)).throwMessage(MessageUtils.message("mdc.process.time.not.null", new Object[0]));
        List<WorkstationWcsDmpDTO> collectItem = vo.getCollectItem();
        List<List<String>> head = buildExcelHeadByHour(collectItem);
        Long workstationId = vo.getWorkstationId();
        List<List<Object>> content = buildExcelContentByHour(workstationId, collectItem, startTime, endTime);
        String fileName = String.format("%s-%s.xlsx", ExcelConstant.PROCESS_PARAMETER, DateUtil.time());
        MultipartFile multipartFile = ExcelStrategyUtil.customerStyleExport(fileName, ExcelConstant.SHEET, head, content);
        return this.ossBuilder.tempTemplate().putFile(multipartFile.getOriginalFilename(), multipartFile);
    }
    */
    /*
    private List<List<Object>> buildExcelContentByHour(Long workstationId, List<WorkstationWcsDmpDTO> collectItem, Date startTime, Date endTime) {
        Workstation workstation = (Workstation) this.workstationService.getById(workstationId);
        Map<Long, GroupWorkDTO> groupWorkMap = this.workstationService.queryGroupWorkStation(Lists.newArrayList(new Long[]{workstation.getId()}));
        GroupWorkDTO groupWorkDTO = groupWorkMap.getOrDefault(workstation.getId(), new GroupWorkDTO());
        String groupName = groupWorkDTO.getName();
        String id = String.valueOf(workstationId);
        WorkstationWcsDmpDTO wcs = WorkstationCache.getDmpStatus(id);
        List<CalendarShiftDTO> timeShiftAll = this.calendarService.getTimeShiftAll(workstation.getCalendarCode(), startTime, endTime);
        log.info("班次信息: {}", timeShiftAll);
        List<NewParamDTO> parameterList = processParameterList(startTime, endTime, id);
        if (Func.isEmpty(parameterList)) {
            return Lists.newArrayList();
        }
        return buildExcelContentByHour(collectItem, parameterList, wcs, workstation, groupName, timeShiftAll);
    }*/
 
    private List<List<Object>> buildExcelContentByHour(List<WorkstationWcsDmpDTO> collectItem, List<NewParamDTO> parameterList, WorkstationWcsDmpDTO wcs, Workstation workstation, String groupName, List<CalendarShiftDTO> timeShiftAll) {
        List<Tuple> times = timeShiftAll.stream().map(calendarShiftDTO -> {
            Date startTime = DateUtil.plusMinutes(DateUtil.parse(calendarShiftDTO.getCalendarDate(), DateConstant.PATTERN_DATE), calendarShiftDTO.getShiftStartTime().intValue());
            Date endTime = DateUtil.plusMinutes(DateUtil.parse(calendarShiftDTO.getCalendarDate(), DateConstant.PATTERN_DATE), calendarShiftDTO.getShiftEndTime().intValue());
            long start = startTime.getTime();
            long end = endTime.getTime();
            String name = calendarShiftDTO.getIndexName();
            return new Tuple(new Object[]{Long.valueOf(start), Long.valueOf(end), Func.isNotBlank(name) ? name : MessageUtils.message("calendar.page.shift.model.shift", new Object[0]) + calendarShiftDTO.getShiftIndex()});
        }).collect(Collectors.toList());
        List<List<Object>> res = Lists.newArrayList();
        String code = workstation.getCode();
        String name = workstation.getName();
        Map<String, String> wcsMap = getWcsMap();
        parameterList.forEach(dto -> {
            Long time = dto.getTs();
            Map<String, Object> valueMap = JsonUtil.toMap(dto.getV());
            ArrayList<Object> newArrayList = Lists.newArrayList();
            newArrayList.add(groupName);
            newArrayList.add(code);
            newArrayList.add(name);
            String shiftName = getShiftName(time.longValue(), times);
            newArrayList.add(shiftName);
            String firstContent = DateUtil.format(DateUtil.fromMilliseconds(time.longValue()), "yyyy-MM-dd HH:mm:ss.SSS");
            newArrayList.add(firstContent);
            buildContent(collectItem, valueMap, wcs, wcsMap, newArrayList);
            res.add(newArrayList);
        });
        return res;
    }
 
    private List<List<String>> buildExcelHeadByHour(List<WorkstationWcsDmpDTO> collectItem) {
        List<List<String>> head = Lists.newArrayList();
        head.add(Lists.newArrayList(new String[]{MessageUtils.message("excel.import.process.parameter.group", new Object[0])}));
        head.add(Lists.newArrayList(new String[]{MessageUtils.message("excel.import.process.parameter.code", new Object[0])}));
        head.add(Lists.newArrayList(new String[]{MessageUtils.message("excel.import.process.parameter.name", new Object[0])}));
        head.add(Lists.newArrayList(new String[]{MessageUtils.message("calendar.page.shift.model.shift", new Object[0])}));
        head.add(Lists.newArrayList(new String[]{MessageUtils.message("calendar.page.shift.model.time", new Object[0])}));
        for (WorkstationWcsDmpDTO dto : collectItem) {
            head.add(Lists.newArrayList(new String[]{dto.getDescription()}));
        }
        return head;
    }
    /*
    private List<NewParamDTO> processParameterList(Date startTime, Date endTime, String workstationId) {
        return this.collectJsonMapper.queryProcessParameter(workstationId, Long.valueOf(startTime.getTime()), Long.valueOf(endTime.getTime()));
    }
    */
    private List<ProcessParameterVO> oldProcessParameterList(Date startTime, Date endTime, List<String> collectItems, String workstationId) {
        return this.parameterMapper.queryProcessParameter(workstationId, collectItems, Long.valueOf(startTime.getTime()), Long.valueOf(endTime.getTime()));
    }
 
    private ProcessParameterVO oldFirstStatue(Date startTime, String item, Long workstationId) {
        //ProcessParameterVO vo = this.parameterMapper.oldFirstStatue(workstationId, item, Long.valueOf(startTime.getTime()));
        ProcessParameterVO vo = helperService.queryLastParameterLessThenTime(workstationId, item, startTime.getTime());
        if (Func.notNull(vo)) {
            vo.setCollectItem(item);
        }
        return vo;
    }
 
    /**
     * 
     * @param endTime
     * @param item 数据点名称,如Output
     * @param workstationId
     * @return
     */
    private ProcessParameterVO oldLastStatue(Date endTime, String item, Long workstationId) {
        //ProcessParameterVO vo = this.parameterMapper.oldLastStatue(workstationId, item, Long.valueOf(endTime.getTime()));
        ProcessParameterVO vo = helperService.getLastParameterGreaterThenTime(workstationId, item, endTime.getTime());
        if (Func.notNull(vo)) {
            vo.setCollectItem(item);
        }
        return vo;
    }
 
    /**
     * 
     * @param startTime
     * @param endTime
     * @param item 数据点名称,如Output
     * @param workstationId
     * @return
     */
    private List<ProcessParameterVO> oldOneCollectList(Date startTime, Date endTime, String item, Long workstationId) {
        List<ProcessParameterVO> list = this.parameterMapper.oldOneCollectList(workstationId, item, startTime.getTime(), endTime.getTime());
        list.forEach(p -> {p.setRealTime(p.getTime().getTime());});
        return list;
    }
 
    private ProcessParameterResVO entityVO(List<ProcessParameterVO> dtoList, WorkstationWcsDmpDTO wcs, Map<String, WorkstationWcsDmpDTO> collectItemMap) {
        List<GlobalWcs> globalWcsList = listGlobalWcs();
        Map<String, String> colorMap = new HashMap<>(globalWcsList.size());
        Map<String, String> nameMap = new HashMap<>(globalWcsList.size());
        Set<String> codes = Sets.newHashSet();
        for (GlobalWcs globalWcs : globalWcsList) {
            colorMap.put(globalWcs.getCode(), globalWcs.getColor());
            nameMap.put(globalWcs.getCode(), globalWcs.getName());
            codes.add(globalWcs.getCode());
        }
        Map<String, List<ProcessParameterVO>> map = dtoList.stream().collect(Collectors.groupingBy((v0) -> {
            return v0.getCollectItem();
        }));
        List<ProParamResDTO> res = Lists.newArrayList();
        map.forEach((k, v) -> {
            ProParamResDTO dto;
            if (Func.isNotEmpty(wcs) && wcs.getName().equals(k)) {
                List<ProcessWcsDTO> collect = v.stream().filter(c -> {
                    return codes.contains(c.getValue());
                }).map(c2 -> {
                    return ProcessWcsDTO.builder().realTime(c2.getRealTime()).time(DateUtil.format(c2.getTime(), "yyyy-MM-dd HH:mm:ss.SSS")).name((String) nameMap.get(c2.getValue())).value(c2.getValue()).color((String) colorMap.get(c2.getValue())).build();
                }).sorted(Comparator.comparing((v0) -> {
                    return v0.getTime();
                })).collect(Collectors.toList());
                dto = ProParamResDTO.builder().collectRealItem(k).collectItem(wcs.getDescription()).dataType(wcs.getWcsDataType()).statusData(collect).build();
            } else {
                List<ProcessParameterRealVO> collect2 =  v.stream().map(c3 -> {
                    return ProcessParameterRealVO.builder().realTime(c3.getRealTime()).time(DateUtil.format(c3.getTime(), "yyyy-MM-dd HH:mm:ss.SSS")).collectItem(c3.getCollectItem()).value(c3.getValue()).build();
                }).sorted(Comparator.comparing((v0) -> {
                    return v0.getTime();
                })).collect(Collectors.toList());
                dto = ProParamResDTO.builder().dataType(((WorkstationWcsDmpDTO) collectItemMap.get(k)).getDataType()).collectRealItem(k).collectItem(((WorkstationWcsDmpDTO) collectItemMap.get(k)).getDescription()).data(collect2).build();
            }
            res.add(dto);
        });
        ProcessParameterResVO process = new ProcessParameterResVO();
        process.setRes(res);
        return process;
    }
 
    private String getShiftName(long time, List<Tuple> times) {
        for (Tuple tuple : times) {
            long start = ((Long) tuple.get(0)).longValue();
            long end = ((Long) tuple.get(1)).longValue();
            if (start <= time && time <= end) {
                return (String) tuple.get(2);
            }
        }
        return "";
    }
 
    private ProParamSheetVO buildSheetVO(List<WorkstationWcsDmpDTO> vos, List<NewParamDTO> dtoList, WorkstationWcsDmpDTO wcs, Map<String, String> wcsMap) {
        List<String> head = buildSheetHead(vos);
        List<List<Object>> content = buildSheetContent(vos, dtoList, wcs, wcsMap);
        return ProParamSheetVO.builder().head(head).content(content).build();
    }
 
    private List<String> buildSheetHead(List<WorkstationWcsDmpDTO> vos) {
        List<String> res = Lists.newArrayList(new String[]{MessageUtils.message("calendar.page.shift.model.time", new Object[0])});
        for (WorkstationWcsDmpDTO dto : vos) {
            res.add(dto.getDescription());
        }
        return res;
    }
 
    private List<List<Object>> buildSheetContent(List<WorkstationWcsDmpDTO> vos, List<NewParamDTO> dtoList, WorkstationWcsDmpDTO wcs, Map<String, String> wcsMap) {
        List<List<Object>> res = Lists.newArrayList();
        dtoList.forEach(dto -> {
            Long time = dto.getTs();
            Map<String, Object> valueMap = JsonUtil.toMap(dto.getV());
            String firstContent = DateUtil.format(DateUtil.fromMilliseconds(time.longValue()), "yyyy-MM-dd HH:mm:ss.SSS");
            ArrayList<Object> newArrayList = Lists.newArrayList(new Object[]{firstContent});
            buildContent(vos, valueMap, wcs, wcsMap, newArrayList);
            res.add(newArrayList);
        });
        return res;
    }
 
    private void buildContent(List<WorkstationWcsDmpDTO> vos, Map<String, Object> valueMap, WorkstationWcsDmpDTO wcs, Map<String, String> wcsMap, List<Object> content) {
        for (WorkstationWcsDmpDTO dto : vos) {
            String name = dto.getName();
            String value = Func.toStr(valueMap.get(name));
            if (Func.notNull(value)) {
                if (Func.isNotEmpty(wcs) && name.equals(wcs.getName())) {
                    content.add(wcsMap.getOrDefault(value, "-"));
                } else {
                    content.add(value);
                }
            } else {
                content.add("-");
            }
        }
    }
 
    private Map<String, String> getWcsMap() {
        List<GlobalWcs> globalWcs = listGlobalWcs();
        return globalWcs.stream().collect(Collectors.toMap((v0) -> {
            return v0.getCode();
        }, (v0) -> {
            return v0.getName();
        }));
    }
 
    private List<WorkstationWcsDmpDTO> getWorkstationWcsDmpDTO(Boolean isFilterProcessParameter, String workstationId) {
        List<WorkstationWcsDmpDTO> vos = WorkstationCache.getWorkstationWcsList(workstationId);
        if (isFilterProcessParameter.booleanValue()) {
            vos = vos.stream().filter((v0) -> {
                return v0.getProcessParameter();
            }).collect(Collectors.toList());
        }
        return vos;
    }
 
    /*
    @Override
    public List<WorkstationCollectData> lastParameter(long workstationId) {
        //yys
        LastProcessParam lp = this.parameterMapper.getLastParameters(workstationId+"");
        if(lp == null) {
            return Collections.emptyList();
        }
        
        List<WorkstationCollectData> result = new ArrayList<>();
        
        //解析json为对象列表
        JSONObject paramsObj = JSONObject.parseObject(lp.getParamJson());
        paramsObj.keySet().forEach(k -> {
            JSONObject itemObj = paramsObj.getJSONObject(k);
            WorkstationCollectData item = WorkstationCollectData.builder().ts(itemObj.getLong("time")).n(k).v(itemObj.getString("value")).workstationId(lp.getWorkstationId()).build();
            
            result.add(item);
        });
        
        return result;
    }*/
    
    /**
     * 获取工位最新参数值
     * @param workstationId 工位id
     * @param item 参数名称,如DeviceStatus/Output
     * @param time 时间,参数应小于该时间
     * @return
     */
    /*
    public ProcessParameterVO getLastParameterLessThanTimeOld_notuse(long workstationId,String item,Long time) {
        //yys 
        LastProcessParam lp = this.parameterMapper.lastParameterLessThanTime(workstationId,item,time);
        if(lp == null) {
            return null;
        }
        ProcessParameterVO vo = new ProcessParameterVO();
        //解析json为对象列表
        JSONObject paramsObj = JSONObject.parseObject(lp.getParamJson());
        
        if(paramsObj.containsKey(item)) {
            JSONObject itemObj = paramsObj.getJSONObject(item);
            
            Long lastTime = itemObj.getLong("time");
            vo.setTime(new Timestamp(lastTime));
            vo.setRealTime(lastTime);
            vo.setValueCollect(itemObj.getString("value"));
        }
        
        return vo;
    }*/
    @DS("iotdb")
    public ProcessParameterVO queryLastParameterLessThenTime1(long workstationId,String item,Long time) {
        /**
         *tdengine实现:oldFirstStatue
         * select last(ts) as realTime,
               last(v)  as value_collect
        from iot_data.super_collect_data
        where n = #{item}
          and ts &lt; #{startTime}
          and workstation_id = #{workstationId}
         */
        ProcessParameterVO vo = null;
        String sql ="select max_time(n) as ts,last_value(n) as n,last_value(v) as v,last_value(workstation_id) as workstationId from root.f2.process_param_"+workstationId+"_"+item+" where time<"+time+" align by device limit 1";
        List<ProcessParameterVO> list =  jdbcTemplate.query(sql, new RowMapper<ProcessParameterVO>() {
 
            @Override
            public ProcessParameterVO mapRow(ResultSet rs, int rowNum) throws SQLException {
                ProcessParameterVO p = new ProcessParameterVO();
                p.setRealTime(rs.getLong("ts"));
                p.setValueCollect(rs.getString("v"));
                
                return p;
            }
            
        });
        if(!list.isEmpty()) {
            vo = list.get(0);
        }
        return vo;
    }
    
    /**
     * 获取工位最新参数值(采集时间应大于指定时间)
     * @param workstationId
     * @param item  参数名称,如DeviceStatus/Output
     * @param time 指定的时间
     * @return
     */
    @DS("iotdb")
    public ProcessParameterVO getLastParameterGreaterThenTime1(long workstationId,String item,Long time) {
        /*
         oldLastStatue
        select last(ts) as realTime,
               last(v)  as value_collect
        from iot_data.super_collect_data
        where n = #{item}
          and ts &gt; #{endTime}
          and workstation_id = #{workstationId}
         * */
        /*
        LastProcessParam lp = this.parameterMapper.lastParameterGreaterThanTime(workstationId,item,time);
        if(lp == null) {
            return null;
        }
        ProcessParameterVO vo = new ProcessParameterVO();
        //解析json为对象列表
        JSONObject paramsObj = JSONObject.parseObject(lp.getParamJson());
        
        if(paramsObj.containsKey(item)) {
            JSONObject itemObj = paramsObj.getJSONObject(item);
            
            Long lastTime = itemObj.getLong("time");
            vo.setTime(new Timestamp(lastTime));
            vo.setRealTime(lastTime);
            vo.setValueCollect(itemObj.getString("value"));
        }
        
        return vo;*/
        
        ProcessParameterVO vo = null;
        String sql ="select max_time(n) as ts,last_value(n) as n,last_value(v) as v,last_value(workstation_id) as workstationId from root.f2.process_param_"+workstationId+"_"+item+" where time>"+time+" align by device limit 1";
        List<ProcessParameterVO> list =  jdbcTemplate.query(sql, new RowMapper<ProcessParameterVO>() {
 
            @Override
            public ProcessParameterVO mapRow(ResultSet rs, int rowNum) throws SQLException {
                ProcessParameterVO p = new ProcessParameterVO();
                p.setRealTime(rs.getLong("ts"));
                p.setValueCollect(rs.getString("v"));
                
                return p;
            }
            
        });
        if(!list.isEmpty()) {
            vo = list.get(0);
        }
        return vo;
        
        
    }
 
    @DS("iotdb")
    @Override
    public List<WorkstationCollectData> queryLastParameter(long workstationId) {
        String sql ="select max_time(n) as ts,last_value(n) as n,last_value(v) as v,last_value(workstation_id) as workstationId from root.f2.process_param_"+workstationId+"_* align by device";
        List<WorkstationCollectData> list =  jdbcTemplate.query(sql, new RowMapper<WorkstationCollectData>() {
 
            @Override
            public WorkstationCollectData mapRow(ResultSet rs, int rowNum) throws SQLException {
                WorkstationCollectData data =  WorkstationCollectData.builder().ts(rs.getLong("ts")).n(rs.getString("n")).v(rs.getString("v")).workstationId(workstationId).build();
                
                return data;
            }
            
        });
        return list;
    }
}