k94314517
2024-08-26 c29ce426a6ce314bc26a23cd92d226cc01825783
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
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
package com.doumee.service.business.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.model.PageData;
import com.doumee.core.model.PageWrap;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.DateUtil;
import com.doumee.core.utils.PositionUtil;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.*;
import com.doumee.dao.business.join.PlatformJobJoinMapper;
import com.doumee.dao.business.join.PlatformJoinMapper;
import com.doumee.dao.business.model.*;
import com.doumee.dao.web.reqeust.JobDetailDTO;
import com.doumee.dao.web.reqeust.JobOperateDTO;
import com.doumee.dao.web.reqeust.LineUpDetailDTO;
import com.doumee.dao.web.reqeust.SignInDTO;
import com.doumee.dao.web.response.DriverHomeVO;
import com.doumee.dao.web.response.LineUpVO;
import com.doumee.service.business.PlatformJobService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * 月台调度作业信息表Service实现
 * @author 江蹄蹄
 * @date 2024/06/28 10:03
 */
@Service
public class PlatformJobServiceImpl implements PlatformJobService {
 
    @Autowired
    private PlatformJobMapper platformJobMapper;
 
    @Autowired
    private PlatformDeviceMapper platformDeviceMapper;
 
    @Autowired
    private PlatformJobJoinMapper platformJobJoinMapper;
 
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
 
 
    @Autowired
    private PlatformJoinMapper platformJoinMapper;
 
    @Autowired
    private PlatformLogMapper platformLogMapper;
 
    @Autowired
    private PlatformWmsJobMapper platformWmsJobMapper;
 
    @Autowired
    private PlatformWmsDetailMapper platformWmsDetailMapper;
 
    @Autowired
    private PlatformGroupMapper platformGroupMapper;
 
    @Autowired
    private PlatformShowParamMapper platformShowParamMapper;
 
 
    @Override
    public Integer create(PlatformJob platformJob) {
        platformJobMapper.insert(platformJob);
        return platformJob.getId();
    }
 
    @Override
    public void deleteById(Integer id) {
        platformJobMapper.deleteById(id);
    }
 
    @Override
    public void delete(PlatformJob platformJob) {
        UpdateWrapper<PlatformJob> deleteWrapper = new UpdateWrapper<>(platformJob);
        platformJobMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        platformJobMapper.deleteBatchIds(ids);
    }
 
    @Override
    public void updateById(PlatformJob platformJob) {
        platformJobMapper.updateById(platformJob);
    }
 
    @Override
    public void updateByIdInBatch(List<PlatformJob> platformJobs) {
        if (CollectionUtils.isEmpty(platformJobs)) {
            return;
        }
        for (PlatformJob platformJob: platformJobs) {
            this.updateById(platformJob);
        }
    }
 
    @Override
    public PlatformJob findById(Integer id) {
        return platformJobMapper.selectById(id);
    }
 
    @Override
    public PlatformJob findOne(PlatformJob platformJob) {
        QueryWrapper<PlatformJob> wrapper = new QueryWrapper<>(platformJob);
        return platformJobMapper.selectOne(wrapper);
    }
 
    @Override
    public List<PlatformJob> findList(PlatformJob platformJob) {
        QueryWrapper<PlatformJob> wrapper = new QueryWrapper<>(platformJob);
        return platformJobMapper.selectList(wrapper);
    }
  
    @Override
    public PageData<PlatformJob> findPage(PageWrap<PlatformJob> pageWrap) {
        IPage<PlatformJob> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        MPJLambdaWrapper<PlatformJob> queryWrapper = new MPJLambdaWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        queryWrapper
                .selectAll(PlatformJob.class)
                .selectAs(Platform::getName,PlatformJob::getPlatformName)
                .selectAs(Platform::getWorkRate,PlatformJob::getWorkRate)
                .leftJoin(Platform.class,Platform::getId,PlatformJob::getPlatformId)
                .eq(pageWrap.getModel().getId() != null, PlatformJob::getId, pageWrap.getModel().getId())
                .eq(pageWrap.getModel().getCreator() != null, PlatformJob::getCreator, pageWrap.getModel().getCreator())
                .ge(pageWrap.getModel().getCreateDate() != null, PlatformJob::getCreateDate, Utils.Date.getStart(pageWrap.getModel().getCreateDate()))
                .le(pageWrap.getModel().getCreateDate() != null, PlatformJob::getCreateDate, Utils.Date.getEnd(pageWrap.getModel().getCreateDate()))
                .eq(pageWrap.getModel().getEditor() != null, PlatformJob::getEditor, pageWrap.getModel().getEditor())
                .ge(pageWrap.getModel().getEditDate() != null, PlatformJob::getEditDate, Utils.Date.getStart(pageWrap.getModel().getEditDate()))
                .le(pageWrap.getModel().getEditDate() != null, PlatformJob::getEditDate, Utils.Date.getEnd(pageWrap.getModel().getEditDate()))
                .eq(pageWrap.getModel().getIsdeleted() != null, PlatformJob::getIsdeleted, pageWrap.getModel().getIsdeleted())
                .eq(pageWrap.getModel().getName() != null, PlatformJob::getName, pageWrap.getModel().getName())
                .eq(pageWrap.getModel().getRemark() != null, PlatformJob::getRemark, pageWrap.getModel().getRemark())
                .eq(pageWrap.getModel().getStatus() != null, PlatformJob::getStatus, pageWrap.getModel().getStatus())
                .eq(pageWrap.getModel().getSortnum() != null, PlatformJob::getSortnum, pageWrap.getModel().getSortnum())
                .eq(pageWrap.getModel().getCode() != null, PlatformJob::getCode, pageWrap.getModel().getCode())
                .eq(pageWrap.getModel().getBillCode() != null, PlatformJob::getBillCode, pageWrap.getModel().getBillCode())
                .eq(pageWrap.getModel().getDriverId() != null, PlatformJob::getDriverId, pageWrap.getModel().getDriverId())
                .eq(pageWrap.getModel().getDriverName() != null, PlatformJob::getDriverName, pageWrap.getModel().getDriverName())
                .eq(pageWrap.getModel().getPlateNum() != null, PlatformJob::getPlateNum, pageWrap.getModel().getPlateNum())
                .eq(pageWrap.getModel().getDrivierPhone() != null, PlatformJob::getDrivierPhone, pageWrap.getModel().getDrivierPhone())
                .ge(pageWrap.getModel().getSignDate() != null, PlatformJob::getSignDate, Utils.Date.getStart(pageWrap.getModel().getSignDate()))
                .le(pageWrap.getModel().getSignDate() != null, PlatformJob::getSignDate, Utils.Date.getEnd(pageWrap.getModel().getSignDate()))
                .eq(pageWrap.getModel().getSingType() != null, PlatformJob::getSingType, pageWrap.getModel().getSingType())
                .eq(pageWrap.getModel().getSignDistance() != null, PlatformJob::getSignDistance, pageWrap.getModel().getSignDistance())
                .eq(pageWrap.getModel().getPlatformNames() != null, PlatformJob::getPlatformNames, pageWrap.getModel().getPlatformNames())
                .eq(pageWrap.getModel().getPlatforms() != null, PlatformJob::getPlatforms, pageWrap.getModel().getPlatforms())
                .eq(pageWrap.getModel().getPlatformId() != null, PlatformJob::getPlatformId, pageWrap.getModel().getPlatformId())
                .ge(pageWrap.getModel().getInwaitDate() != null, PlatformJob::getInwaitDate, Utils.Date.getStart(pageWrap.getModel().getInwaitDate()))
                .le(pageWrap.getModel().getInwaitDate() != null, PlatformJob::getInwaitDate, Utils.Date.getEnd(pageWrap.getModel().getInwaitDate()))
                .eq(pageWrap.getModel().getInwaitUserId() != null, PlatformJob::getInwaitUserId, pageWrap.getModel().getInwaitUserId())
                .ge(pageWrap.getModel().getCallDate() != null, PlatformJob::getCallDate, Utils.Date.getStart(pageWrap.getModel().getCallDate()))
                .le(pageWrap.getModel().getCallDate() != null, PlatformJob::getCallDate, Utils.Date.getEnd(pageWrap.getModel().getCallDate()))
                .eq(pageWrap.getModel().getCallUserId() != null, PlatformJob::getCallUserId, pageWrap.getModel().getCallUserId())
                .eq(pageWrap.getModel().getCallInfo() != null, PlatformJob::getCallInfo, pageWrap.getModel().getCallInfo())
                .ge(pageWrap.getModel().getStartDate() != null, PlatformJob::getStartDate, Utils.Date.getStart(pageWrap.getModel().getStartDate()))
                .le(pageWrap.getModel().getStartDate() != null, PlatformJob::getStartDate, Utils.Date.getEnd(pageWrap.getModel().getStartDate()))
                .ge(pageWrap.getModel().getDoneDate() != null, PlatformJob::getDoneDate, Utils.Date.getStart(pageWrap.getModel().getDoneDate()))
                .le(pageWrap.getModel().getDoneDate() != null, PlatformJob::getDoneDate, Utils.Date.getEnd(pageWrap.getModel().getDoneDate()))
                .ge(pageWrap.getModel().getErrorDate() != null, PlatformJob::getErrorDate, Utils.Date.getStart(pageWrap.getModel().getErrorDate()))
                .le(pageWrap.getModel().getErrorDate() != null, PlatformJob::getErrorDate, Utils.Date.getEnd(pageWrap.getModel().getErrorDate()))
                .eq(pageWrap.getModel().getErrorUserId() != null, PlatformJob::getErrorUserId, pageWrap.getModel().getErrorUserId())
                .eq(pageWrap.getModel().getErrorInfo() != null, PlatformJob::getErrorInfo, pageWrap.getModel().getErrorInfo())
                .ge(pageWrap.getModel().getTransPlatformDate() != null, PlatformJob::getTransPlatformDate, Utils.Date.getStart(pageWrap.getModel().getTransPlatformDate()))
                .le(pageWrap.getModel().getTransPlatformDate() != null, PlatformJob::getTransPlatformDate, Utils.Date.getEnd(pageWrap.getModel().getTransPlatformDate()))
                .eq(pageWrap.getModel().getTransPlatformUserId() != null, PlatformJob::getTransPlatformUserId, pageWrap.getModel().getTransPlatformUserId())
                .eq(pageWrap.getModel().getTransPlatformInfo() != null, PlatformJob::getTransPlatformInfo, pageWrap.getModel().getTransPlatformInfo())
                .ge(pageWrap.getModel().getInDate() != null, PlatformJob::getInDate, Utils.Date.getStart(pageWrap.getModel().getInDate()))
                .le(pageWrap.getModel().getInDate() != null, PlatformJob::getInDate, Utils.Date.getEnd(pageWrap.getModel().getInDate()))
                .ge(pageWrap.getModel().getOutDate() != null, PlatformJob::getOutDate, Utils.Date.getStart(pageWrap.getModel().getOutDate()))
                .le(pageWrap.getModel().getOutDate() != null, PlatformJob::getOutDate, Utils.Date.getEnd(pageWrap.getModel().getOutDate()))
                .eq(pageWrap.getModel().getOutType() != null, PlatformJob::getOutType, pageWrap.getModel().getOutType())
                .eq(pageWrap.getModel().getOutHkstatus() != null, PlatformJob::getOutHkstatus, pageWrap.getModel().getOutHkstatus())
                .ge(pageWrap.getModel().getOutHkdate() != null, PlatformJob::getOutHkdate, Utils.Date.getStart(pageWrap.getModel().getOutHkdate()))
                .le(pageWrap.getModel().getOutHkdate() != null, PlatformJob::getOutHkdate, Utils.Date.getEnd(pageWrap.getModel().getOutHkdate()))
                .eq(pageWrap.getModel().getOutHkinfo() != null, PlatformJob::getOutHkinfo, pageWrap.getModel().getOutHkinfo())
                .eq(pageWrap.getModel().getInHkstatus() != null, PlatformJob::getInHkstatus, pageWrap.getModel().getInHkstatus())
                .eq(pageWrap.getModel().getNHkinfo() != null, PlatformJob::getNHkinfo, pageWrap.getModel().getNHkinfo())
                .ge(pageWrap.getModel().getInHkdate() != null, PlatformJob::getInHkdate, Utils.Date.getStart(pageWrap.getModel().getInHkdate()))
                .le(pageWrap.getModel().getInHkdate() != null, PlatformJob::getInHkdate, Utils.Date.getEnd(pageWrap.getModel().getInHkdate()))
                .eq(pageWrap.getModel().getType() != null, PlatformJob::getType, pageWrap.getModel().getType())
                .like(pageWrap.getModel().getCarCodeFront() != null, PlatformJob::getCarCodeFront, pageWrap.getModel().getCarCodeFront())
                .like(pageWrap.getModel().getCarCodeBack() != null, PlatformJob::getCarCodeBack, pageWrap.getModel().getCarCodeBack())
                .eq(pageWrap.getModel().getCompanyId() != null, PlatformJob::getCompanyId, pageWrap.getModel().getCompanyId())
                .eq(pageWrap.getModel().getCompanyNamePath() != null, PlatformJob::getCompanyNamePath, pageWrap.getModel().getCompanyNamePath())
                .ge(pageWrap.getModel().getCancelDate() != null, PlatformJob::getCancelDate, Utils.Date.getStart(pageWrap.getModel().getCancelDate()))
                .le(pageWrap.getModel().getCancelDate() != null, PlatformJob::getCancelDate, Utils.Date.getEnd(pageWrap.getModel().getCancelDate()))
                .eq(pageWrap.getModel().getCancelUserId() != null, PlatformJob::getCancelUserId, pageWrap.getModel().getCancelUserId())
                .eq(pageWrap.getModel().getCancelInfo() != null, PlatformJob::getCancelInfo, pageWrap.getModel().getCancelInfo())
                .eq(pageWrap.getModel().getReason() != null, PlatformJob::getReason, pageWrap.getModel().getReason())
                .eq(pageWrap.getModel().getContractNum() != null, PlatformJob::getContractNum, pageWrap.getModel().getContractNum())
                .ge(pageWrap.getModel().getArriveDate() != null, PlatformJob::getArriveDate, Utils.Date.getStart(pageWrap.getModel().getArriveDate()))
                .le(pageWrap.getModel().getArriveDate() != null, PlatformJob::getArriveDate, Utils.Date.getEnd(pageWrap.getModel().getArriveDate()))
                .eq(pageWrap.getModel().getInType() != null, PlatformJob::getInType, pageWrap.getModel().getInType())
                .eq(pageWrap.getModel().getTotalNum() != null, PlatformJob::getTotalNum, pageWrap.getModel().getTotalNum())
                .eq(pageWrap.getModel().getPlatformGroupId() != null, PlatformJob::getPlatformGroupId, pageWrap.getModel().getPlatformGroupId())
        ;
 
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
       IPage<PlatformJob> platformJobIPage = platformJobJoinMapper.selectJoinPage(page,PlatformJob.class,queryWrapper);
        platformJobIPage.getRecords().forEach(i->{
            i.dealTime();
            this.getWmsJobData(i);
        });
        return PageData.from(platformJobIPage);
//        PageData<PlatformJob> pageData = PageData.from(platformJobMapper.selectPage(page, queryWrapper));
//        pageData.getRecords().forEach(i->{
//            i.dealTime();
//            this.getWmsJobData(i);
//        });
//        return pageData;
    }
 
 
    public void getWmsJobData(PlatformJob platformJob){
        if(Constants.equalsInteger(platformJob.getType(),Constants.platformJobType.zycxh)
        || Constants.equalsInteger(platformJob.getType(),Constants.platformJobType.zyczh)
        || Constants.equalsInteger(platformJob.getType(),Constants.platformJobType.sgscxh)) {
 
            PlatformWmsJob platformWmsJob = platformWmsJobMapper.selectOne(new QueryWrapper<PlatformWmsJob>().lambda()
                    .eq(PlatformWmsJob::getIsdeleted,Constants.ZERO)
                    .eq(PlatformWmsJob::getJobId,platformJob.getId())
                    .orderByDesc(PlatformWmsJob::getId)
                    .last(" limit 1")
            );
            if(Objects.isNull(platformWmsJob)){
                return;
            }
            List<PlatformWmsDetail> platformWmsDetailList = platformWmsDetailMapper.selectList(new QueryWrapper<PlatformWmsDetail>().lambda()
                    .eq(PlatformWmsDetail::getIsdeleted,Constants.ZERO)
                    .eq(PlatformWmsDetail::getJobId,platformJob.getId()));
            if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(platformWmsDetailList)){
                platformJob.setTotalNum(
                        platformWmsDetailList.stream().map(m->m.getIoQty()).reduce(BigDecimal.ZERO,BigDecimal::add)
                );
                platformWmsJob.setPlatformWmsDetailList(platformWmsDetailList);
                platformJob.setPlatformWmsJob(platformWmsJob);
            }
        } 
    }
 
 
    public void queryWaitNum(PlatformJob platformJob){
        if(Constants.equalsInteger(platformJob.getStatus(),Constants.PlatformJobStatus.WAIT_CALL.getKey())){
            //查询前方排队数量
            List<PlatformJob> lineUpNum =  platformJobMapper.selectList(new QueryWrapper<PlatformJob>().lambda()
                    .eq(PlatformJob::getIsdeleted,Constants.ZERO)
                    .in(PlatformJob::getStatus,Constants.PlatformJobStatus.WAIT_CALL.getKey()
                            ,Constants.PlatformJobStatus.IN_WAIT.getKey()
                            ,Constants.PlatformJobStatus.TRANSFERING.getKey())
                    .gt(PlatformJob::getSignDate,platformJob.getSignDate())
                    .eq(PlatformJob::getPlatformGroupId,platformJob.getPlatformGroupId())
                    .like(PlatformJob::getArriveDate,DateUtil.getDate(platformJob.getArriveDate(),"yyyy-MM-dd")));
            platformJob.setLineUpNum(lineUpNum.size());
            BigDecimal sumWorkRate = platformJob.getTotalNum();
            for (PlatformJob linePlatformJob:lineUpNum) {
                this.getWmsJobData(linePlatformJob);
                sumWorkRate = sumWorkRate.add(linePlatformJob.getTotalNum());
            }
            //计算预计等待时间
            List<Platform> platformList = platformJoinMapper.selectList(new QueryWrapper<Platform>().lambda().eq(Platform::getIsdeleted,Constants.ZERO).eq(Platform::getGroupId,platformJob.getPlatformGroupId()));
            BigDecimal workRate = platformList.stream().map(m->m.getWorkRate()).reduce(BigDecimal.ZERO,BigDecimal::add);
            if(sumWorkRate.compareTo(BigDecimal.ZERO) == Constants.ZERO|| workRate.compareTo(BigDecimal.ZERO)  == Constants.ZERO ){
                BigDecimal sumMinute = sumWorkRate.divide(workRate,1, RoundingMode.HALF_DOWN).multiply(BigDecimal.valueOf(60L));
                Integer sumMinuteInteger = sumMinute.intValue();
                Integer hours = sumMinuteInteger/60;
                Integer minus = sumMinuteInteger%60;
                String  waitTime = "预计等待:";
                if(!Constants.equalsInteger(hours,Constants.ZERO)){
                    waitTime = waitTime + hours + "小时";
                }
                if(!Constants.equalsInteger(hours,Constants.ZERO)){
                    waitTime = waitTime + minus + "分钟";
                }
                platformJob.setWaitTime(waitTime);
            }
        }else if(Constants.equalsInteger(platformJob.getStatus(),Constants.PlatformJobStatus.WORKING.getKey())){
            //查询最后开始任务的月台记录  日志表  因为存在异常挂起 转移 等问题
            PlatformLog platformLog = platformLogMapper.selectOne(new QueryWrapper<PlatformLog>().lambda()
                    .eq(PlatformLog::getJobId,platformJob.getId())
                    .eq(PlatformLog::getObjType,Constants.PlatformJobStatus.WORKING.getKey())
                    .orderByDesc(PlatformLog::getCreateDate)
                    .last(" limit 1")
            );
            if(Objects.nonNull(platformLog)){
                BigDecimal sumMinute = platformJob.getTotalNum().divide(platformJob.getWorkRate(),1, RoundingMode.HALF_DOWN).multiply(BigDecimal.valueOf(60L));
                platformJob.setFinishTimeStr(DateUtil.DateToStr(DateUtil.afterMinutesDate(platformLog.getCreateDate(),sumMinute.intValue()),"HH:mm"));
            }
        }
    }
    
    
    @Override
    public long count(PlatformJob platformJob) {
        QueryWrapper<PlatformJob> wrapper = new QueryWrapper<>(platformJob);
        return platformJobMapper.selectCount(wrapper);
    }
 
    //TODO
    @Override
    public DriverHomeVO getDriverHome(LoginUserInfo loginUserInfo){
        DriverHomeVO driverHomeVO = new DriverHomeVO();
        //TODO 轮播图
 
        List<PlatformJob> platformJobList =  platformJobJoinMapper.selectJoinList(PlatformJob.class,new MPJLambdaWrapper<PlatformJob>()
                .selectAll(PlatformJob.class)
                .selectAs(Platform::getName,PlatformJob::getPlatformName)
                .selectAs(Platform::getWorkRate,PlatformJob::getWorkRate)
                .leftJoin(Platform.class,Platform::getId,PlatformJob::getPlatformId)
                .eq(PlatformJob::getIsdeleted,Constants.ZERO)
                .eq(PlatformJob::getDrivierPhone,loginUserInfo.getMobile())
                .like(PlatformJob::getArriveDate,DateUtil.getCurrDate())
                .orderByDesc(PlatformJob::getId)
        );
        for (PlatformJob platformJob:platformJobList) {
            //处理WSM数量
            this.getWmsJobData(platformJob);
            //查询前方排队数量
            this.queryWaitNum(platformJob);
        }
 
 
        driverHomeVO.setPlatformJobList(platformJobList);
 
        // 园区导览图  图片
        driverHomeVO.setReservationMap(systemDictDataBiz.queryByCode(Constants.PLATFORM,Constants.PLATFORM_GUIDEMAP).getCode());
        // 预约指南 文本
        driverHomeVO.setBookingTips(systemDictDataBiz.queryByCode(Constants.PLATFORM,Constants.PLATFORM_BOOKING_TIPS).getCode());
 
        return driverHomeVO;
    }
 
 
    /**
     * 距离签到
     * @param signInDTO
     */
    @Override
    @Transactional(rollbackFor = {BusinessException.class,Exception.class})
    public void signIn(SignInDTO signInDTO){
        if(Objects.isNull(signInDTO)
            || Objects.isNull(signInDTO.getSignType())
            || Objects.isNull(signInDTO.getJobId())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        PlatformJob platformJob = platformJobMapper.selectById(signInDTO.getJobId());
        if(Objects.isNull(platformJob)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(!Constants.equalsInteger(platformJob.getStatus(),Constants.PlatformJobStatus.WART_SIGN_IN.getKey())){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"业务状态已流转,请刷新查看");
        }
        PlatformJob oldPlatformJob = new PlatformJob();
        BeanUtils.copyProperties(platformJob,oldPlatformJob);
 
        if(Constants.equalsInteger(signInDTO.getSignType(),Constants.ZERO)){
            this.distanceSignIn(signInDTO,platformJob);
        }else if(Constants.equalsInteger(signInDTO.getSignType(),Constants.ONE)){
            this.sceneSignIn(signInDTO);
        }
        platformJob.setSignDate(new Date());
        platformJob.setSingType(Constants.ZERO);
        platformJob.setStatus(Constants.PlatformJobStatus.WAIT_CALL.getKey());
        platformJobMapper.updateById(platformJob);
        //存储操作日志
        savePlatformLog(Constants.PlatformJobStatus.WAIT_CALL.getKey(),oldPlatformJob,platformJob);
    }
 
 
    public void distanceSignIn(SignInDTO signInDTO , PlatformJob platformJob){
        if(Objects.isNull(signInDTO.getLat())
                || Objects.isNull(signInDTO.getLnt())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"经纬度信息异常");
        }
        //获取签到点的经纬度
        Double lat = Double.parseDouble(systemDictDataBiz.queryByCode(Constants.PLATFORM,Constants.SIGN_IN_PLACE_LAT).getCode());
        Double lnt = Double.parseDouble(systemDictDataBiz.queryByCode(Constants.PLATFORM,Constants.SIGN_IN_PLACE_LNT).getCode());
        BigDecimal distance = new BigDecimal(systemDictDataBiz.queryByCode(Constants.PLATFORM,Constants.SIGN_IN_PLACE_DISTANCE).getCode());
        //获取2个点的距离X米
        Double getDistanceDouble = PositionUtil.getDistance(signInDTO.getLnt(),signInDTO.getLat(),lnt,lat);
        //转换km
        BigDecimal getDistance = BigDecimal.valueOf(getDistanceDouble).divide(new BigDecimal(1000),2,BigDecimal.ROUND_HALF_UP);
        if(distance.compareTo(getDistance)<Constants.ZERO){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"超出可签到距离,可签到距离["+distance+"]公里]");
        }
        platformJob.setSignDistance(BigDecimal.valueOf(getDistanceDouble));
    }
 
    public void sceneSignIn(SignInDTO signInDTO){
        if( StringUtils.isNotBlank(signInDTO.getQrCodeKey())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        if(!signInDTO.getQrCodeKey().equals(systemDictDataBiz.queryByCode(Constants.PLATFORM,Constants.SIGN_IN_QRCODE).getCode())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"二维码已过期,请刷新重试");
        }
    }
 
 
    @Override
    public PlatformJob getDetail(JobDetailDTO jobDetailDTO){
        if(Objects.isNull(jobDetailDTO)
        || Objects.isNull(jobDetailDTO.getJobId())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        PlatformJob platformJob  = platformJobMapper.selectById(jobDetailDTO.getJobId());
        if(Objects.isNull(platformJob)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(Constants.equalsInteger(platformJob.getStatus(),Constants.PlatformJobStatus.WART_SIGN_IN.getKey())){
            //待签到 计算距离
            if(Objects.nonNull(jobDetailDTO.getLnt())&&Objects.nonNull(jobDetailDTO.getLat())){
                //获取签到点的经纬度
                Double lat = Double.parseDouble(systemDictDataBiz.queryByCode(Constants.PLATFORM,Constants.SIGN_IN_PLACE_LAT).getCode());
                Double lnt = Double.parseDouble(systemDictDataBiz.queryByCode(Constants.PLATFORM,Constants.SIGN_IN_PLACE_LNT).getCode());
                //获取2个点的距离X米
                Double getDistanceDouble = PositionUtil.getDistance(jobDetailDTO.getLnt(),jobDetailDTO.getLat(),lnt,lat);
                //转换km
                BigDecimal getDistance = BigDecimal.valueOf(getDistanceDouble).divide(new BigDecimal(1000),2,BigDecimal.ROUND_HALF_UP);
                platformJob.setGetDistance(getDistance);
            }
        }else if( Constants.equalsInteger(platformJob.getStatus(),Constants.PlatformJobStatus.WAIT_CALL.getKey())
                || Constants.equalsInteger(platformJob.getStatus(),Constants.PlatformJobStatus.IN_WAIT.getKey())
        ){
            //查询前方排队数量
            this.queryWaitNum(platformJob);
        }else if(Constants.equalsInteger(platformJob.getStatus(),Constants.PlatformJobStatus.WORKING.getKey())){
            //作业月台信息
            Platform platform = platformJoinMapper.selectById(platformJob.getPlatformId());
            if(Objects.nonNull(platform)){
                platformJob.setPlatformName(platform.getName());
            }
 
        }
        platformJob.dealTime();
        this.getWorkTime(platformJob);
        this.getWmsJobData(platformJob);
        return platformJob;
    }
 
 
 
    @Override
    public LineUpVO lineUpDetail(LineUpDetailDTO lineUpDetailDTO){
        if(Objects.isNull(lineUpDetailDTO)
        || Objects.isNull(lineUpDetailDTO.getQueryType())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        LineUpVO lineUpVO = new LineUpVO();
        //查询所有任务
        List<PlatformJob> platformJobList = platformJobJoinMapper.selectJoinList(PlatformJob.class,
                new MPJLambdaWrapper<PlatformJob>()
                        .selectAll(PlatformJob.class)
                        .eq(PlatformJob::getIsdeleted,Constants.ZERO)
                        .eq(Constants.equalsInteger(lineUpDetailDTO.getQueryType(),Constants.ZERO)
                                ,PlatformJob::getDrivierPhone,lineUpDetailDTO.getMobile())
                        .eq(Constants.equalsInteger(lineUpDetailDTO.getQueryType(),Constants.ONE)
                                ,PlatformJob::getId,lineUpDetailDTO.getJobId())
                        .in(PlatformJob::getStatus,Constants.PlatformJobStatus.WAIT_CALL.getKey(),
                                Constants.PlatformJobStatus.IN_WAIT.getKey(),
                                Constants.PlatformJobStatus.CALLED.getKey())
                        .orderByAsc(PlatformJob::getSignDate)
        );
        //获取所有月台组
        List<Integer> platformIdList = platformJobList.stream().map(m->m.getPlatformGroupId()).collect(Collectors.toList());
        List<Platform> platformList = platformJoinMapper.selectList(new QueryWrapper<Platform>().lambda()
                .eq(Platform::getIsdeleted,Constants.ZERO)
                .in(Platform::getId,platformIdList));
 
        for (Platform platform:platformList) {
            //查询本月台组下 自己的数据 最早签到的
            PlatformJob platformJob = platformJobList.stream().filter(i->Constants.equalsInteger(i.getPlatformId(),platform.getId())).findFirst().orElse(null);
            //查询在当前月台组下 签到排队车辆数据
            List<PlatformJob> platformJobSignInList = platformJobJoinMapper.selectJoinList(PlatformJob.class,
                    new MPJLambdaWrapper<PlatformJob>()
                            .selectAll(PlatformJob.class)
                            .eq(PlatformJob::getIsdeleted,Constants.ZERO)
                            .eq(PlatformJob::getPlatformGroupId,platform.getGroupId())
                            .ge(Objects.nonNull(platformJob),PlatformJob::getSignDate,DateUtil.getDate(platformJob.getSignDate(),"yyyy-MM-dd HH:mm:ss"))
                            .in(PlatformJob::getStatus,Constants.PlatformJobStatus.WAIT_CALL.getKey(),
                                    Constants.PlatformJobStatus.IN_WAIT.getKey(),
                                    Constants.PlatformJobStatus.CALLED.getKey())
                            .orderByDesc(PlatformJob::getSignDate));
            platform.setSignJobList(platformJobSignInList);
            //查询当前月台组下
            List<PlatformJob> platformJobWorkList = platformJobJoinMapper.selectJoinList(PlatformJob.class,
                    new MPJLambdaWrapper<PlatformJob>()
                            .selectAll(PlatformJob.class)
                            .eq(PlatformJob::getIsdeleted,Constants.ZERO)
                            .eq(PlatformJob::getPlatformGroupId,platform.getGroupId())
                            .eq(PlatformJob::getStatus,Constants.PlatformJobStatus.WORKING.getKey())
                            .orderByDesc(PlatformJob::getSignDate));
            platform.setWorkJobList(platformJobWorkList);
        }
        lineUpVO.setPlatformList(platformList);
        return lineUpVO;
    }
 
 
    /**
     * 获取已作业时间
     * @param platformJob
     */
    public void getWorkTime(PlatformJob platformJob){
        List<PlatformLog> platformLogList = platformLogMapper.selectList(new QueryWrapper<PlatformLog>()
                .lambda().eq(PlatformLog::getIsdeleted,Constants.ZERO)
                .eq(PlatformLog::getJobId,platformJob.getId())
                .ne(PlatformLog::getParam3,"0")
        );
        Long optTime = 0L;
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(platformLogList)){
            List<Long> param3List = platformLogList.stream().map(m->Long.valueOf(m.getParam3())).collect(Collectors.toList());
            for (Long val:param3List) {
                optTime = optTime + val;
            }
        }
        if(Constants.equalsInteger(platformJob.getStatus(),Constants.PlatformJobStatus.WORKING.getKey())){
            //查询最后一次开始的日志数据
            PlatformLog lastBeginPlatform = platformLogMapper.selectOne(new QueryWrapper<PlatformLog>().lambda()
                    .eq(PlatformLog::getIsdeleted,Constants.ZERO)
                    .eq(PlatformLog::getObjType,Constants.PlatformJobStatus.WORKING.getKey())
                    .eq(PlatformLog::getJobId,platformJob.getId())
                    .orderByDesc(PlatformLog::getCreateDate)
                    .last(" limit 1 ")
            );
            if(Objects.nonNull(lastBeginPlatform)){
                String v = Long.toString(System.currentTimeMillis() - lastBeginPlatform.getCreateDate().getTime() / 1000) ;
                optTime = optTime + Long.valueOf(v);
            }
        }
        platformJob.setWorkTime(optTime);
    }
 
    /**
     * 通知入园
     */
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public void platformInPark(JobOperateDTO jobOperateDTO){
        if(Objects.isNull(jobOperateDTO)
                || Objects.isNull(jobOperateDTO.getJobId())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        PlatformJob platformJob = platformJobMapper.selectById(jobOperateDTO.getJobId());
        if(Objects.isNull(platformJob)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(!Constants.equalsInteger(platformJob.getStatus(),Constants.PlatformJobStatus.WAIT_CALL.getKey())){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,业务状态已流转!");
        }
        PlatformJob oldPlatformJob = new PlatformJob();
        BeanUtils.copyProperties(platformJob,oldPlatformJob);
 
        platformJob.setInwaitDate(new Date());
        platformJob.setInwaitUserId(jobOperateDTO.getLoginUserInfo().getId());
        platformJob.setStatus(Constants.PlatformJobStatus.IN_WAIT.getKey());
        platformJob.setEditDate(new Date());
        platformJobMapper.updateById(platformJob);
 
        if(Constants.equalsInteger(platformJob.getType(),Constants.TWO)
        || Constants.equalsInteger(platformJob.getType(),Constants.THREE)
        || Constants.equalsInteger(platformJob.getType(),Constants.FOUR)){
            //TODO 下发入园权限
 
        }
 
        //存储操作日志
        savePlatformLog(Constants.PlatformJobStatus.WAIT_CALL.getKey(),oldPlatformJob,platformJob);
 
    }
 
    /**
     * 月台叫号
     */
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public void platformCallNumber(JobOperateDTO jobOperateDTO){
        if(Objects.isNull(jobOperateDTO)
                || Objects.isNull(jobOperateDTO.getJobId())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        PlatformJob platformJob = platformJobMapper.selectById(jobOperateDTO.getJobId());
        if(Objects.isNull(platformJob)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(!(Constants.equalsInteger(platformJob.getStatus(),Constants.PlatformJobStatus.WAIT_CALL.getKey())
                || Constants.equalsInteger(platformJob.getStatus(),Constants.PlatformJobStatus.IN_WAIT.getKey())
                || Constants.equalsInteger(platformJob.getStatus(),Constants.PlatformJobStatus.TRANSFERING.getKey())
        ) ){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,业务状态已流转!");
        }
 
        PlatformJob oldPlatformJob = new PlatformJob();
        BeanUtils.copyProperties(platformJob,oldPlatformJob);
 
        platformJob.setCallDate(new Date());
        platformJob.setCallUserId(jobOperateDTO.getLoginUserInfo().getId());
        platformJob.setStatus(Constants.PlatformJobStatus.CALLED.getKey());
        platformJob.setEditDate(new Date());
        platformJobMapper.updateById(platformJob);
        //存储操作日志
        savePlatformLog(Constants.PlatformJobStatus.CALLED.getKey(),oldPlatformJob,platformJob);
    }
 
 
    /**
     * 转移月台
     * @param jobOperateDTO
     */
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public void platformMove(JobOperateDTO jobOperateDTO){
        if(Objects.isNull(jobOperateDTO)
        || Objects.isNull(jobOperateDTO.getJobId())
                || Objects.isNull(jobOperateDTO.getPlatformId())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        PlatformJob platformJob = platformJobMapper.selectById(jobOperateDTO.getJobId());
        if(Objects.isNull(platformJob)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if( !( Constants.equalsInteger(platformJob.getStatus(),Constants.PlatformJobStatus.WORKING.getKey())
            || Constants.equalsInteger(platformJob.getStatus(),Constants.PlatformJobStatus.EXCEPTION.getKey())
        )){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,业务状态已流转!");
        }
        if(Constants.equalsInteger(platformJob.getPlatformId(),jobOperateDTO.getPlatformId())){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"无法转移本月台");
        }
 
        PlatformJob oldPlatformJob = new PlatformJob();
        BeanUtils.copyProperties(platformJob,oldPlatformJob);
 
        platformJob.setTransPlatformDate(new Date());
        platformJob.setTransPlatformUserId(jobOperateDTO.getLoginUserInfo().getId());
        platformJob.setPlatformId(jobOperateDTO.getPlatformId());
        platformJob.setStatus(Constants.PlatformJobStatus.TRANSFERING.getKey());
        platformJob.setEditDate(new Date());
        platformJobMapper.updateById(platformJob);
 
        //TODO 调起WMS 通知月台转移
        //存储操作日志
        savePlatformLog(Constants.PlatformJobStatus.TRANSFERING.getKey(),oldPlatformJob,platformJob);
    }
 
 
    /**
     * 月台过号
     */
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public void platformOverNumber(JobOperateDTO jobOperateDTO){
        if(Objects.isNull(jobOperateDTO)
                || Objects.isNull(jobOperateDTO.getJobId())
                || Objects.isNull(jobOperateDTO.getPlatformId())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        PlatformJob platformJob = platformJobMapper.selectById(jobOperateDTO.getJobId());
        if(Objects.isNull(platformJob)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(!Constants.equalsInteger(platformJob.getStatus(),Constants.PlatformJobStatus.WORKING.getKey())){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,业务状态已流转!");
        }
 
        PlatformJob oldPlatformJob = new PlatformJob();
        BeanUtils.copyProperties(platformJob,oldPlatformJob);
 
        platformJob.setStatus(Constants.PlatformJobStatus.WART_SIGN_IN.getKey());
        platformJob.setEditDate(new Date());
        platformJobMapper.updateById(platformJob);
        //存储操作日志
        savePlatformLog(Constants.PlatformJobStatus.WART_SIGN_IN.getKey(),oldPlatformJob,platformJob);
 
 
    }
 
 
    /**
     * 异常挂起
     */
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public void platformErr(JobOperateDTO jobOperateDTO){
        if(Objects.isNull(jobOperateDTO)
                || Objects.isNull(jobOperateDTO.getJobId())
                || Objects.isNull(jobOperateDTO.getPlatformId())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        PlatformJob platformJob = platformJobMapper.selectById(jobOperateDTO.getJobId());
        if(Objects.isNull(platformJob)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(!Constants.equalsInteger(platformJob.getStatus(),Constants.PlatformJobStatus.WORKING.getKey())){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,业务状态已流转!");
        }
 
        PlatformJob oldPlatformJob = new PlatformJob();
        BeanUtils.copyProperties(platformJob,oldPlatformJob);
 
        platformJob.setErrorDate(new Date());
        platformJob.setErrorUserId(jobOperateDTO.getLoginUserInfo().getId());
        platformJob.setStatus(Constants.PlatformJobStatus.EXCEPTION.getKey());
        platformJob.setEditDate(new Date());
        platformJobMapper.updateById(platformJob);
        //存储操作日志
        savePlatformLog(Constants.PlatformJobStatus.EXCEPTION.getKey(),oldPlatformJob,platformJob);
    }
 
 
    /**
     * 开始作业
     */
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public void beginWork(JobOperateDTO jobOperateDTO){
        if(Objects.isNull(jobOperateDTO)
                || Objects.isNull(jobOperateDTO.getJobId())
                || Objects.isNull(jobOperateDTO.getPlatformId())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        PlatformJob platformJob = platformJobMapper.selectById(jobOperateDTO.getJobId());
        if(Objects.isNull(platformJob)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(!Constants.equalsInteger(platformJob.getStatus(),Constants.PlatformJobStatus.CALLED.getKey())){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,业务状态已流转!");
        }
        platformJobMapper.update(null,new UpdateWrapper<PlatformJob>()
                .lambda()
                .set(Objects.isNull(platformJob.getStartDate()),PlatformJob::getStartDate,new Date())
                .set(PlatformJob::getEditDate,new Date())
                .set(PlatformJob::getStatus,Constants.PlatformJobStatus.WORKING.getKey())
                .eq(PlatformJob::getId,jobOperateDTO.getJobId())
        );
 
        PlatformJob oldPlatformJob = new PlatformJob();
        BeanUtils.copyProperties(platformJob,oldPlatformJob);
        if(Objects.isNull(platformJob.getStartDate())){
            platformJob.setStartDate(new Date());
        }
        platformJob.setStatus(Constants.PlatformJobStatus.WORKING.getKey());
        platformJob.setEditDate(new Date());
        platformJobMapper.updateById(platformJob);
        //存储操作日志
        savePlatformLog(Constants.PlatformJobStatus.WORKING.getKey(),oldPlatformJob,platformJob);
    }
 
 
    /**
     * 完成作业
     */
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public void finishWork(JobOperateDTO jobOperateDTO){
        if(Objects.isNull(jobOperateDTO)
                || Objects.isNull(jobOperateDTO.getJobId())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        PlatformJob platformJob = platformJobMapper.selectById(jobOperateDTO.getJobId());
        if(Objects.isNull(platformJob)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(!Constants.equalsInteger(platformJob.getStatus(),Constants.PlatformJobStatus.WORKING.getKey())){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,业务状态已流转!");
        }
        PlatformJob oldPlatformJob = new PlatformJob();
        BeanUtils.copyProperties(platformJob,oldPlatformJob);
 
        platformJob.setDoneDate(new Date());
        platformJob.setStatus(Constants.PlatformJobStatus.EXCEPTION.getKey());
        platformJob.setEditDate(new Date());
        platformJobMapper.updateById(platformJob);
        //存储操作日志
        savePlatformLog(Constants.PlatformJobStatus.EXCEPTION.getKey(),oldPlatformJob,platformJob);
 
        if(Constants.equalsInteger(platformJob.getType(),Constants.TWO)){
            //TODO 外协车装货 查询TMS 电子锁情况
 
        }else if(Constants.equalsInteger(platformJob.getType(),Constants.ONE) || Constants.equalsInteger(platformJob.getType(),Constants.FOUR)){
            //TODO 外协车卸货 或者 市公司车卸货 则根据任务情况进行下发离园权限
 
        }
    }
 
 
    /**
     * 月台相机推送 开始作业
     * @param jobOperateDTO
     */
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public void cameraStartWork(JobOperateDTO jobOperateDTO){
        if(Objects.isNull(jobOperateDTO)
                || Objects.isNull(jobOperateDTO.getDeviceId())
                || StringUtils.isBlank(jobOperateDTO.getCarCodeBack())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        PlatformDevice platformDevice = platformDeviceMapper.selectOne(new QueryWrapper<PlatformDevice>().lambda()
                        .eq(PlatformDevice::getIsdeleted,Constants.ZERO)
                .eq(PlatformDevice::getDeviceId,jobOperateDTO.getDeviceId())
                .last(" limit 1")
        );
        if(Objects.isNull(platformDevice)){
            return;
        }
        PlatformJob platformJob =  platformJobMapper.selectOne(new QueryWrapper<PlatformJob>().lambda()
                .eq(PlatformJob::getPlatformId,platformDevice.getPlatformId())
                .eq(PlatformJob::getCarCodeBack,jobOperateDTO.getCarCodeBack())
                .eq(PlatformJob::getStatus,Constants.PlatformJobStatus.CALLED.getKey())
                .last(" limit 1 ")
        );
        if(Objects.isNull(platformJob)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(!Constants.equalsInteger(platformJob.getStatus(),Constants.PlatformJobStatus.CALLED.getKey())){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,业务状态已流转!");
        }
 
        PlatformJob oldPlatformJob = new PlatformJob();
        BeanUtils.copyProperties(platformJob,oldPlatformJob);
        if(Objects.isNull(platformJob.getStartDate())){
            platformJob.setStartDate(new Date());
        }
        platformJob.setStatus(Constants.PlatformJobStatus.WORKING.getKey());
        platformJob.setEditDate(new Date());
        platformJobMapper.updateById(platformJob);
        //存储操作日志
        savePlatformLog(Constants.PlatformJobStatus.WORKING.getKey(),oldPlatformJob,platformJob);
    }
 
 
    /**
     * 存储操作日志
     * @param objType
     * @param platformJobBefor
     * @param platformJobAfter
     */
    public void savePlatformLog(Integer objType,PlatformJob platformJobBefor,PlatformJob platformJobAfter){
        PlatformLog platformLog = new PlatformLog();
        platformLog.setIsdeleted(Constants.ZERO);
        platformLog.setCreateDate(new Date());
        platformLog.setJobId(platformJobAfter.getId());
        platformLog.setObjType(objType);
        platformLog.setBeforeContent(JSONObject.toJSONString(platformJobBefor));
        platformLog.setAfterContent(JSONObject.toJSONString(platformJobAfter));
        platformLog.setObjId(platformJobAfter.getId().toString());
        //查询最后一次开始作业的日志
        PlatformLog lastBeginPlatform = platformLogMapper.selectOne(new QueryWrapper<PlatformLog>().lambda()
                .eq(PlatformLog::getIsdeleted,Constants.ZERO)
                .eq(PlatformLog::getObjType,Constants.PlatformJobStatus.WORKING.getKey())
                .eq(PlatformLog::getJobId,platformLog.getJobId())
                .orderByDesc(PlatformLog::getCreateDate)
                .last(" limit 1 ")
        );
        if(Objects.isNull(lastBeginPlatform)){
            return;
        }
        if(Constants.equalsInteger(objType,Constants.PlatformJobStatus.EXCEPTION.getKey())
            || Constants.equalsInteger(objType,Constants.PlatformJobStatus.TRANSFERING.getKey())){
            //异常挂起 与转移中 记录本次 作业时间
            platformLog.setParam1(DateUtil.getDate(lastBeginPlatform.getCreateDate(),"yyyy-MM-dd HH:mm:ss"));
            platformLog.setParam2(DateUtil.getDate(platformLog.getCreateDate(),"yyyy-MM-dd HH:mm:ss"));
            String v = Long.toString(platformJobAfter.getDoneDate().getTime() - platformJobBefor.getStartDate().getTime() / 1000) ;
            platformLog.setParam3(v);
        }else  if(Constants.equalsInteger(objType,Constants.PlatformJobStatus.DONE.getKey())){
            //查询最后一次日志是否为异常挂起,异常挂起后可进行转移 如果进行转移那么业务就需要重新进行开始就不需要特殊处理  ,如果不是异常挂起进行数据结束 则直接查询最后一次开始的时间
            PlatformLog lastPlatformLog = platformLogMapper.selectOne(new QueryWrapper<PlatformLog>().lambda()
                    .eq(PlatformLog::getIsdeleted,Constants.ZERO)
                    .eq(PlatformLog::getJobId,platformLog.getJobId())
                    .orderByDesc(PlatformLog::getCreateDate)
                    .last(" limit 1 ")
            );
            if(Objects.nonNull(lastPlatformLog) && Constants.equalsInteger(lastPlatformLog.getObjType(),Constants.PlatformJobStatus.EXCEPTION.getKey())){
                platformLog.setParam3("0");
            }else{
                //非异常挂起 则直接过去最后一次开始作业的日志的开始时间
                platformLog.setParam1(DateUtil.getDate(lastBeginPlatform.getCreateDate(),"yyyy-MM-dd HH:mm:ss"));
                platformLog.setParam2(DateUtil.getDate(platformLog.getCreateDate(),"yyyy-MM-dd HH:mm:ss"));
                String v = Long.toString(platformJobAfter.getDoneDate().getTime() - platformJobBefor.getStartDate().getTime() / 1000) ;
                platformLog.setParam3(v);
            }
        }else{
            platformLog.setParam3("0");
        }
        platformLogMapper.insert(platformLog);
    }
 
 
    @Override
    public  List<Platform> getPlatformList(Integer groupId, LoginUserInfo loginUserInfo){
        //查询月台组下所有月台
        List<Platform> allPlatformList = platformJoinMapper.selectJoinList(Platform.class,
                new MPJLambdaWrapper<Platform>()
                        .selectAll(Platform.class)
                        .select(" ( select count(1) from platform_job pj where t.id = pj.PLATFORM_ID and pj.STATUS = "+Constants.PlatformJobStatus.WORKING.getKey()+" ) as workStatus ")
                        .eq(Platform::getIsdeleted, Constants.ZERO)
                        .eq(Platform::getStatus,Constants.ZERO)
                        .eq(Platform::getGroupId,groupId)
        );
        this.getPlatformShow(allPlatformList,loginUserInfo);
        for (Platform platform:allPlatformList) {
            this.getJobByPlatform(platform,loginUserInfo);
        }
        return allPlatformList;
    }
 
    /**
     * 查询月台的开启情况
     * @param allPlatformList
     * @param loginUserInfo
     */
    public void getPlatformShow(List<Platform> allPlatformList , LoginUserInfo loginUserInfo){
        //处理我的月台信息 是否配置隐藏
        List<PlatformShowParam> platformShowParamList = platformShowParamMapper.selectList(new QueryWrapper<PlatformShowParam>()
                .lambda()
                .eq(PlatformShowParam::getIsdeleted, Constants.ZERO)
                .eq(PlatformShowParam::getMemberId,loginUserInfo.getMemberId())
        );
        //如果未配置该数据 则全部显示
        if(CollectionUtils.isEmpty(platformShowParamList)){
            for (Platform platform:allPlatformList) {
                platform.setShowConfig(true);
            }
        }else{
            //根据配置显示数据
            for (PlatformShowParam platformShowParam:platformShowParamList) {
                for (Platform platform:allPlatformList) {
                    if(Constants.equalsInteger(platform.getId(),platformShowParam.getPlatformId())){
                        platform.setShowConfig(true);
                        break;
                    }
                }
            }
        }
    }
 
 
    /**
     * 获取月台下的任务列表
     * @param platform
     * @param loginUserInfo
     */
    public void getJobByPlatform(Platform platform,LoginUserInfo loginUserInfo){
        List<PlatformJob> platformJobList =  platformJobJoinMapper.selectJoinList(PlatformJob.class,new MPJLambdaWrapper<PlatformJob>()
                .selectAll(PlatformJob.class)
                .selectAs(Platform::getName,PlatformJob::getPlatformName)
                .selectAs(Platform::getWorkRate,PlatformJob::getWorkRate)
                .leftJoin(Platform.class,Platform::getId,PlatformJob::getPlatformId)
                .eq(PlatformJob::getPlatformId,platform.getId())
                .eq(PlatformJob::getIsdeleted,Constants.ZERO)
                .eq(PlatformJob::getDrivierPhone,loginUserInfo.getMobile())
                .like(PlatformJob::getArriveDate, DateUtil.getCurrDate())
                .orderByDesc(PlatformJob::getId)
        );
        for (PlatformJob platformJob:platformJobList) {
            //处理WSM数量
            this.getWmsJobData(platformJob);
            //查询前方排队数量
            this.queryWaitNum(platformJob);
        }
        platform.setWorkJobList(platformJobList);
    }
 
}