YYC
2023-10-29 e4a102acbd91e300497572404866f2a5f16571ab
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
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
package com.ld.igds.inout.manager;
 
import com.ld.igds.check.CheckStandardManager;
import com.ld.igds.check.dto.CheckItemData;
import com.ld.igds.check.dto.CheckUpdateResult;
import com.ld.igds.constant.RespCodeEnum;
import com.ld.igds.data.Page;
import com.ld.igds.data.PageResponse;
import com.ld.igds.file.CoreFileService;
import com.ld.igds.file.dto.FileData;
import com.ld.igds.inout.InoutConstant;
import com.ld.igds.inout.dto.InoutCheckData;
import com.ld.igds.inout.dto.InoutCheckParam;
import com.ld.igds.inout.dto.InoutData;
import com.ld.igds.inout.dto.InoutParam;
import com.ld.igds.inout.service.InoutService;
import com.ld.igds.m.service.InoutCommonService;
import com.ld.igds.models.InoutConf;
import com.ld.igds.models.InoutPrice;
import com.ld.igds.models.InoutSysConf;
import com.ld.igds.util.ContextUtil;
import com.ld.igds.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
 
/**
 * 出入库相关业务1
 *
 * @author
 */
@Component
public class InoutManager {
 
    @Resource
    private InoutService inoutService;
    @Resource
    private CoreFileService fileService;
    @Resource
    private InoutCommonService inoutCommonService;
    @Resource
    private CheckStandardManager checkStandardManager;
    @Resource
    private InoutEventControlManager inoutEventControl;
    @Resource
    private CoreFileService coreFileService;
 
    /**
     * 直接从数据库查询,不考虑信息状态
     *
     * @param param
     * @return
     */
    public PageResponse<InoutData> inoutQueryBack(InoutParam param) {
        if (StringUtils.isEmpty(param.getPlateNum())
                && StringUtils.isEmpty(param.getIntelCard())
                && StringUtils.isEmpty(param.getUserId())) {
 
            return new PageResponse<>(RespCodeEnum.CODE_1007.getCode(),
                    "查询参数不完整,查询失败!");
        }
 
        // 获取三天以内最后一天记录
        param.setStart(DateUtil.getNewByDay(new Date(), -3));
        param.setType(null);
        param.setProgress(null);
 
        InoutData result = inoutService.getLastRecord(param);
 
        if (null == result) {
            return new PageResponse<>(RespCodeEnum.CODE_1007.getCode(),
                    "根据当前条件未查询到车辆信息!");
        }
 
        if (InoutConstant.PROGRESS_RECORD.equals(result.getProgress())) {
            return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(),
                    "当前车辆流程已经结束", result);
        }
 
        if (!InoutConstant.PROGRESS_CARD_BACK.equals(result.getProgress())) {
            return new PageResponse<>(RespCodeEnum.CODE_1008.getCode(),
                    "当前车辆流程不在当前环节,无法执行出库", result);
        }
 
        return new PageResponse<>(RespCodeEnum.CODE_0000, result);
    }
 
    /**
     * 首先从缓存中获取,缓存中,从数据库中获取
     *
     * @param param
     * @return
     */
    public PageResponse<InoutData> inoutQuery(InoutParam param) {
        if (StringUtils.isEmpty(param.getPlateNum())
                && StringUtils.isEmpty(param.getIntelCard())
                && StringUtils.isEmpty(param.getUserId())) {
 
            return new PageResponse<>(RespCodeEnum.CODE_1007.getCode(),
                    "查询参数不完整,查询失败!");
        }
 
        InoutData result;
        String progress = param.getProgress();
        try {
            param.setProgress(null);
            result = inoutService.inoutProgressQuery(param);
            if (null == result) {
                return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(),
                        "系统为查询到执行中的车辆信息", null);
            }
 
            // 如果没有流程条件说明不需要做判断,直接返回
            if (StringUtils.isEmpty(progress)) {
                return new PageResponse<>(RespCodeEnum.CODE_0000, result);
            }
 
            // 流程类型验证
            if (!result.getType().equals(param.getType())) {
                String msg = "流程错误:当前车辆流程为【入库】";
                if (InoutConstant.TYPE_OUT.equals(result.getType()))
                    msg = "流程错误:当前车辆流程为【出库】";
                return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(),
                        msg, result);
            }
 
            // 流程环节验证
            if (progress.equals(InoutConstant.PROGRESS_WEIGHT_TAG)) {
                if (result.getProgress().equals(
                        InoutConstant.PROGRESS_WEIGHT_EMPTY)
                        || result.getProgress().equals(
                        InoutConstant.PROGRESS_WEIGHT_FULL)) {
                    // 表示当前流程正常
                } else {
                    return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(),
                            "提示:非当前环节,请到"
                                    + InoutConstant.getProcessName(
                                    param.getType(),
                                    result.getProgress()), result);
                }
            } else {
                if (!result.getProgress().equals(progress)) {
                    return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(),
                            "提示:非当前环节,请到"
                                    + InoutConstant.getProcessName(
                                    param.getType(),
                                    result.getProgress()), result);
                }
            }
 
            return new PageResponse<>(RespCodeEnum.CODE_0000, result);
 
        } catch (Exception e) {
            return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(),
                    e.getMessage());
        }
    }
 
 
    /**
     * 化验结果不通过时校验配置信息
     *
     * @param data
     * @return
     */
    private InoutData checkNoPass(InoutData data) {
        InoutSysConf inoutSysConf = inoutCommonService.getCacheInoutSysConf(
                data.getCompanyId(), data.getDeptId());
 
        if (inoutSysConf.getNoPassNext().equals(
                InoutConstant.CHECK_NOPASS_RECORD)) {
            // 流程结束
            data.setProgress(InoutConstant.PROGRESS_RECORD);
            data.setCompleteTime(new Date());
        }
        if (inoutSysConf.getNoPassNext()
                .equals(InoutConstant.CHECK_NOPASS_BACK)) {
            // 离库收卡
            data.setProgress(InoutConstant.PROGRESS_CARD_BACK);
        }
        return data;
    }
 
    /**
     * 更新质检信息
     *
     * @param data
     * @return
     */
    private InoutData updateCheckItems(InoutData data, boolean toSave) throws Exception {
 
        if (null == data.getCheckItems()) return data;
 
        //设置没有质检ID,取消执行持久化
        if (!toSave) data.setCheckId(null);
 
        // 更新检验项数据
        CheckUpdateResult checkUpdateResult = checkStandardManager.updateCheckItems(data.getCheckId(), data.getCompanyId(), data.getCheckItems());
 
        if (data.getDeCheck() == 0) {
            data.setDeCheck(checkUpdateResult.getDeSum());
        }
        if (data.getAddCheck() == 0) {
            data.setAddCheck(checkUpdateResult.getAddSum());
        }
 
        return data;
    }
 
    /**
     * 从缓存中获取下一流程
     *
     * @return
     */
    private String getNextProgress(String curProgress, String type,
                                   InoutSysConf sysConf) {
        String nextProgress = null;
        List<String> list = null;
        if (InoutConstant.TYPE_IN.equals(type)) {
            list = Arrays.asList(sysConf.getProgressIn().split("-"));
        }
        if (InoutConstant.TYPE_OUT.equals(type)) {
            list = Arrays.asList(sysConf.getProgressOut().split("-"));
        }
        if (list != null) {
            for (int i = 0; i < list.size() - 1; i++) {
                if (curProgress.equals(list.get(i))) {
                    nextProgress = list.get(i + 1);
                    break;
                }
            }
        }
        return nextProgress;
    }
 
    /**
     * 根据数据当前状态和下一个状态,更新基础信息
     *
     * @param data
     * @return
     */
    private InoutData updateBasicInfo(InoutData data, String curProgress,
                                      String nextProgress) {
 
        if (InoutConstant.PROGRESS_REGISTER.equals(curProgress)) {
            data.setRegisterTime(new Date());
            data.setRegisterUser(ContextUtil.getLoginUserCName());
            data.setProgress(nextProgress);
        }
 
        if (InoutConstant.PROGRESS_CHECK.equals(data.getProgress())) {
            if (StringUtils.isEmpty(data.getCheckUser())) {
                data.setCheckUser(ContextUtil.getLoginUserCName());
            }
            data.setProgress(nextProgress);
            return data;
        }
 
        if (InoutConstant.PROGRESS_WEIGHT_EMPTY.equals(curProgress)) {
            data.setEmptyWeightTime(new Date());
            data.setEmptyWeightUser(ContextUtil.getLoginUserCName());
            data.setProgress(nextProgress);
        }
 
        if (InoutConstant.PROGRESS_WEIGHT_FULL.equals(curProgress)) {
            data.setFullWeightTime(new Date());
            data.setFullWeightUser(ContextUtil.getLoginUserCName());
            data.setProgress(nextProgress);
        }
 
        if (InoutConstant.PROGRESS_HANDLE.equals(curProgress)) {
            if (null == data.getHandleUser()) {
                data.setHandleUser(ContextUtil.getLoginUserCName());
            }
            data.setProgress(nextProgress);
        }
 
        // 如果下一个数据状态=流程结束,直接补充完整信息
        if (InoutConstant.PROGRESS_RECORD.equals(nextProgress)) {
            data.setCompleteTime(DateUtils.addMinutes(new Date(), 2));
            data.setCompleteUser(ContextUtil.getLoginUserCName());
            data.setProgress(nextProgress);
        }
 
        return data;
    }
 
    /**
     * 卡回收逻辑处理
     *
     * @param data
     * @return
     */
    public PageResponse<InoutData> submitCardBack(InoutData data) {
        try {
            // 如果当前数据已经结束
            if (InoutConstant.PROGRESS_RECORD.equals(data.getProgress())) {
 
                // 删除缓存
                inoutService.delFromCache(data.getDeptId(), data.getType(),
                        data.getId());
 
                return new PageResponse<>(RespCodeEnum.CODE_0000.getCode(),
                        "当车辆流程已经完成,卡片已回收", data);
            }
 
            // 如果当前流程不是卡回收
            if (!InoutConstant.PROGRESS_CARD_BACK.equals(data.getProgress())) {
 
                return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(),
                        "当前流程环节不正确,不能执行卡回收", data);
            }
 
            // 如果当前流程状态是卡回收状态,就是完成流程。
            if (InoutConstant.PROGRESS_CARD_BACK.equals(data.getProgress())) {
                return submitComplete(data);
            }
 
        } catch (Exception e) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(),
                    "后端执行异常:" + e.getMessage());
        }
 
        return new PageResponse<>(RespCodeEnum.CODE_0000.getCode(), "执行成功",
                data);
    }
 
 
    public PageResponse<InoutData> submitComplete(InoutData data)
            throws Exception {
 
        if (StringUtils.isEmpty(data.getId())
                || StringUtils.isEmpty(data.getType())
                || StringUtils.isEmpty(data.getProgress())) {
 
            return new PageResponse<>(RespCodeEnum.CODE_1007.getCode(),
                    "没有获取到车辆信息。", data);
        }
 
        data.setUpdateTime(new Date());
        if (null == data.getCompleteTime()) {
            data.setCompleteTime(DateUtil.getNewByMinute(new Date(), 2));
        }
        if (null == data.getCompleteUser()) {
            data.setCompleteUser(ContextUtil.getLoginUserCName());
        }
 
        // 设置流程节点直接完成
        InoutParam param = new InoutParam();
        param.setCompanyId(data.getCompanyId());
        param.setId(data.getId());
        param.setType(data.getType());
        param.setDeptId(data.getDeptId());
        param.setIntelCard(data.getIntelCard());
        param.setUserId(data.getCompleteUser());
        param.setDepotId(data.getDepotId());
        param.setCompleteTime(new Date());
        param.setProgress(InoutConstant.PROGRESS_RECORD);
 
        String msg = inoutService.toComplete(param);
 
        if (null != msg) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(), msg);
        }
        data.setCompleteTime(param.getCompleteTime());
        data.setProgress(InoutConstant.PROGRESS_RECORD);
        inoutService.updateInoutCache(data);
        // 流程完成收事件
        inoutEventControl.onInoutComplete(data);
 
        return new PageResponse<>(RespCodeEnum.CODE_0000.getCode(), null, data);
    }
 
    /**
     * 补单操作 补单因为完成时间不确定,所以需要系统根据当前数据进行自动检测,调整原来流水信息和库存
     *
     * @param data
     * @return
     * @throws Exception
     */
    public String addInoutData(InoutData data) throws Exception {
 
        if (data.getSettleWeight() <= 0.0) {
            return "系统:补单数据要求结算重量必须大于0";
        }
 
        // 补单数据直接到结果状态
        data.setRecordStatus(InoutConstant.RECORD_STATUS_ADD);
        data.setProgress(InoutConstant.PROGRESS_RECORD);
        if (InoutConstant.STATUS_NONE.equals(data.getCheckStatus())) {
            data.setCheckStatus(InoutConstant.STATUS_PASS);
        }
 
        String loginUser = ContextUtil.getLoginUserCName();
        if (StringUtils.isEmpty(data.getRegisterUser())) {
            data.setRegisterUser(loginUser);
        }
        if (StringUtils.isEmpty(data.getFullWeightUser())) {
            data.setFullWeightUser(loginUser);
        }
        if (StringUtils.isEmpty(data.getEmptyWeightUser())) {
            data.setEmptyWeightUser(loginUser);
        }
        if (StringUtils.isEmpty(data.getHandleUser())) {
            data.setHandleUser(loginUser);
        }
        if (StringUtils.isEmpty(data.getCompleteUser())) {
            data.setCompleteUser(loginUser);
        }
 
        if (null == data.getCompleteTime()) {
            data.setCompleteTime(new Date());
        }
        // 注册时间比完成时间早一个小时
        data.setRegisterTime(DateUtils.addMinutes(new Date(), -80));
 
        if (StringUtils.isEmpty(data.getUserId())) {
            data.setUserId(InoutConstant.DEFAULT_ID_CARD);
        }
        if (null == data.getIntelCard()) {
            data.setIntelCard(data.getUserId());
        }
        if (null != data.getCheckItems() && StringUtils.isEmpty(data.getCheckUser())) {
            data.setCheckUser(loginUser);
        }
 
 
        if (InoutConstant.TYPE_IN.equals(data.getType())) {
            if (null == data.getFullWeightTime()) {
                data.setFullWeightTime(DateUtils.addMinutes(new Date(), -50));
            }
            if (null == data.getEmptyWeightTime()) {
                data.setEmptyWeightTime(DateUtils.addMinutes(new Date(), -10));
            }
 
            if (data.getCompleteTime().before(data.getEmptyWeightTime())) {
                data.setEmptyWeightTime(DateUtils.addMinutes(data.getCompleteTime(), -10));
            }
            if (data.getEmptyWeightTime().before(data.getFullWeightTime())) {
                data.setFullWeightTime(DateUtils.addMinutes(data.getEmptyWeightTime(), -40));
            }
            if (data.getFullWeightTime().before(data.getRegisterTime())) {
                data.setRegisterTime(DateUtils.addMinutes(data.getFullWeightTime(), -30));
            }
 
            data = this.updateCheckItems(data, false);
        } else {
            if (null == data.getEmptyWeightTime()) {
                data.setEmptyWeightTime(DateUtils.addMinutes(new Date(), -50));
            }
            if (null == data.getFullWeightTime()) {
                data.setFullWeightTime(DateUtils.addMinutes(new Date(), -10));
            }
            if (data.getCompleteTime().before(data.getFullWeightTime())) {
                data.setFullWeightTime(DateUtils.addMinutes(data.getCompleteTime(), -10));
            }
            if (data.getFullWeightTime().before(data.getEmptyWeightTime())) {
                data.setEmptyWeightTime(DateUtils.addMinutes(data.getEmptyWeightTime(), -40));
            }
            if (data.getEmptyWeightTime().before(data.getRegisterTime())) {
                data.setRegisterTime(DateUtils.addMinutes(data.getFullWeightTime(), -30));
            }
        }
 
 
        //先保存主表,后执行子表
        inoutService.insertData(data);
 
        if (InoutConstant.TYPE_IN.equals(data.getType())) {
            this.updateCheckItems(data, true);
        }
        fileService.saveInoutFiles(data.getFiles(), data.getCompanyId(), data.getId(), null);
        return null;
    }
 
    /**
     * 修改操作 修改逻辑说明:修改数据需要获取原数据库中的数据进行对比。
     *
     * @param data
     * @return
     * @throws Exception
     */
    @Transactional
    public String updateInoutData(InoutData data) throws Exception {
 
        if (InoutConstant.RECORD_STATUS_DEL.equals(data.getRecordStatus())) {
            return "系统:已经删除的数据不支持修改!";
        }
 
 
        if (InoutConstant.PROGRESS_RECORD.equals(data.getProgress())) {
            return "系统:流程已经结束的数据不支持修改!";
        }
 
        // 保存检测项目
        if (InoutConstant.TYPE_IN.equals(data.getType())) {
            data = updateCheckItems(data, true);
        }
 
        return inoutService.updateData(data);
    }
 
 
    /**
     * 异常终止操作 设置异常 为了避免页面数据缓存,需要重新获取最新数据判断
     *
     * @param data
     * @return
     * @throws Exception
     */
    public String errorInoutData(InoutData data) throws Exception {
        InoutParam param = new InoutParam();
        param.setCompanyId(data.getCompanyId());
        param.setId(data.getId());
 
        param.setMsg(" 于" + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm") + " 被 " + ContextUtil.getLoginUserCName() + " 执行终止,原因:" + data.getRemarks());
 
        data = inoutService.inoutQueryById(param);
 
//        if (InoutConstant.PROGRESS_RECORD.equals(data.getProgress())) {
//            return "流程结束的数据不支持异常处理!";
//        }
        if (InoutConstant.RECORD_STATUS_DEL.equals(data.getRecordStatus())) {
            return "当前信息已做异常处理,不支持重复执行!";
        }
 
 
        param.setDeptId(data.getDeptId());
        param.setType(data.getType());
        return inoutService.inoutStop(param);
    }
 
    /**
     * 快速结束操作 流程没有结束的单据进行结束
     *
     * @param data
     * @return
     */
    public String completeInoutData(InoutData data) throws Exception {
        // 从新查询数据
        InoutParam param = new InoutParam();
        param.setId(data.getId());
        param.setCompanyId(data.getCompanyId());
        param.setType(data.getType());
        InoutData cuData = inoutService.inoutQueryById(param);
        if (null == cuData) {
            return "当前车辆流程状态已经变化,请刷新页面重新操作!";
        }
 
        if (!cuData.getProgress().equals(data.getProgress())) {
            return "当前车辆流程状态已经变化,请刷新页面重新操作!";
        }
 
        if (InoutConstant.RECORD_STATUS_DEL.equals(cuData.getRecordStatus())) {
            return "当前信息已经被异常终止,不支持当前处理!";
        }
        if (InoutConstant.PROGRESS_RECORD.equals(cuData.getProgress())) {
            return "流程已完成的数据,不支持完成!";
        }
 
        if (null == data.getCompleteTime()) {
            return "请填写完成时间!!";
        }
        if (StringUtils.isEmpty(data.getDepotId())) {
            return "请填写仓库信息!";
        }
        if (StringUtils.isEmpty(data.getFoodYear())) {
            return "请填写粮食年份信息!";
        }
        if (StringUtils.isEmpty(data.getFoodVariety())) {
            return "请填写粮食品种信息!";
        }
 
        if (data.getSettleWeight() <= 0) {
            return "请填写结算净重信息!";
        }
 
        String msg = inoutService.quickComplete(data);
 
 
        return msg;
    }
 
    /**
     * 出入库登记初始化
     *
     * @param conf 车牌识别配置信息
     */
    public void initInoutRegister(InoutConf conf) {
        inoutEventControl.initInoutRegister(conf);
    }
 
    /**
     * 出入库注册数据提交,入库登记和出入库登记使用同一个方法
     *
     * @param data
     * @return
     * @throws Exception
     */
    public PageResponse<InoutData> submitRegisterInout(InoutData data) throws Exception {
 
        // 首先判断流程中是否有已经存在的车辆信息未执行完成,直接从数据库中查询
        InoutParam param = new InoutParam();
        param.setCompanyId(data.getCompanyId());
        param.setPlateNum(data.getPlateNum());
        param.setIntelCard(data.getIntelCard());
        param.setType(data.getType());
        param.setStart(DateUtil.getNewByDay(null, -10));
 
        int num = inoutService.checkExist(param);
        if (num > 0) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(),
                    "当前车牌或智慧卡有正在执行流程未结束,请联系管理员", data);
        }
 
        // 获取系统参数配置
        InoutSysConf inoutSysConf = inoutCommonService.getCacheInoutSysConf(
                data.getCompanyId(), data.getDeptId());
        if (null == inoutSysConf) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(),
                    "提示:当前库区未配置出入库流程信息,请联系管理员", data);
        }
        List<FileData> files = data.getFiles();
        // 获取下一个流程状态
        String curProgress = data.getProgress();
        String nextProgress = getNextProgress(curProgress, data.getType(),
                inoutSysConf);
 
        data = updateBasicInfo(data, curProgress, nextProgress);
 
        String msg = inoutService.insertData(data);
 
        if (null != msg) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(), msg, data);
        }
 
        if (null != files && files.size() > 0) {
            // 执行附件信息
            fileService.saveInoutFiles(files, data.getCompanyId(), data.getId(), curProgress);
        }
 
        //登记完成事件
        inoutEventControl.onInoutRegister(data);
 
        return new PageResponse<>(RespCodeEnum.CODE_0000.getCode(), "执行成功",
                data);
    }
 
    public PageResponse<InoutData> submitWeightIn(InoutData data) throws Exception {
        // 获取系统参数配置
        InoutSysConf inoutSysConf = inoutCommonService.getCacheInoutSysConf(
                data.getCompanyId(), data.getDeptId());
        if (null == inoutSysConf) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(),
                    "提示:当前系统中流程配置异常,请联系管理员", data);
        }
        List<FileData> files = data.getFiles();
        // 获取下一个流程状态
        String curProgress = data.getProgress();
        String nextProgress = getNextProgress(curProgress, data.getType(),
                inoutSysConf);
        data = updateBasicInfo(data, curProgress, nextProgress);
 
        // 入库称重可能调整质检单
        data = updateCheckItems(data, true);
 
        // 执行数据更新
        String msg = inoutService.updateData(data);
 
        if (null != msg) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(), msg,
                    data);
        }
 
        if (null != files && files.size() > 0) {
            // 执行附件信息
            fileService.saveInoutFiles(files, data.getCompanyId(), data.getId(), curProgress);
        }
 
        // 称重完成事件
        inoutEventControl.onInoutWeight(data, curProgress, nextProgress);
 
        return new PageResponse<>(RespCodeEnum.CODE_0000.getCode(), "执行成功", data);
    }
 
    public PageResponse<InoutData> submitWeightOut(InoutData data)
            throws Exception {
        // 获取系统参数配置
        InoutSysConf inoutSysConf = inoutCommonService.getCacheInoutSysConf(
                data.getCompanyId(), data.getDeptId());
        if (null == inoutSysConf) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(),
                    "提示:当前系统中流程配置异常,请联系管理员", data);
        }
 
        // 获取下一个流程状态
        String curProgress = data.getProgress();
        String nextProgress = getNextProgress(curProgress, data.getType(),
                inoutSysConf);
        data = updateBasicInfo(data, curProgress, nextProgress);
 
        // 执行数据更新
        String msg = inoutService.updateData(data);
 
        if (null != msg) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(), msg,
                    data);
        }
 
        // 执行附件信息
        fileService.saveInoutFiles(data.getFiles(), data.getCompanyId(),
                data.getId(), curProgress);
 
 
        // 称重完成事件
        inoutEventControl.onInoutWeight(data, curProgress, nextProgress);
 
        return new PageResponse<>(RespCodeEnum.CODE_0000.getCode(), "执行成功", data);
    }
 
    public PageResponse<InoutData> submitHandle(InoutData data)
            throws Exception {
 
        if (null == data.getHandleUser()) {
            data.setHandleUser(ContextUtil.getLoginUserCName());
        }
 
        // 获取系统参数配置
        InoutSysConf inoutSysConf = inoutCommonService.getCacheInoutSysConf(data.getCompanyId(), data.getDeptId());
        if (null == inoutSysConf) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(),
                    "提示:当前系统中流程配置异常,请联系管理员", data);
        }
 
        // 获取下一个流程状态
        String curProgress = data.getProgress();
        String nextProgress = getNextProgress(curProgress, data.getType(), inoutSysConf);
        data = updateBasicInfo(data, curProgress, nextProgress);
 
        // 执行数据更新
        String msg = inoutService.updateDataByHandle(data);
 
        if (null != msg) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(), msg, data);
        }
 
        // 执行附件信息
        fileService.saveInoutFiles(data.getFiles(), data.getCompanyId(), data.getId(), curProgress);
 
        // 值仓完成触发事件
        inoutEventControl.onInoutHandle(data, curProgress, nextProgress);
 
        return new PageResponse<>(RespCodeEnum.CODE_0000.getCode(), "执行成功", data);
    }
 
    /**
     * 根据条件获取扦样数据
     *
     * @param param
     * @return
     */
    public PageResponse<Page<InoutCheckData>> pageSampleData(
            InoutCheckParam param) {
 
        if (StringUtils.isEmpty(param.getCompanyId())) {
            param.setCompanyId(ContextUtil.getCompanyId());
        }
        if (StringUtils.isEmpty(param.getDeptId())) {
            param.setDeptId(ContextUtil.subDeptId(null));
        }
 
        // 若查询条件为已扦样,则流程置空;若未扦样,则流程不处理,仍为扦样化验流程(CHECK)
        if (!"NONE".equals(param.getSampleStatus())) {
            param.setProgress(null);
        }
 
        // 获取扦样数据
        Page<InoutCheckData> sampleList = inoutService.pageSampleData(param);
 
        if (null == sampleList.getRecords()
                || sampleList.getRecords().isEmpty()) {
            return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(),
                    "获取到数据信息为空");
        }
        return new PageResponse<>(RespCodeEnum.CODE_0000, sampleList);
    }
 
 
    public PageResponse<InoutCheckData> submitSample(InoutCheckData data) throws Exception {
 
        // 获取业务数据信息
        InoutParam param = new InoutParam();
        param.setCompanyId(data.getCompanyId());
        param.setId(data.getId());
        InoutData curData = inoutService.inoutProgressQuery(param);
 
 
        if (null == curData) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(),
                    "没有获取到入库业务数据信息,更新失败", data);
        }
        if (InoutConstant.PROGRESS_RECORD.equals(curData.getProgress())) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(),
                    "当前流程已经结束,不支持修改");
        }
 
        // 扦样时间和扦样人
        if (StringUtils.isEmpty(data.getSampleUser())) {
            data.setSampleUser(ContextUtil.getLoginUserCName());
        }
 
        if (null == data.getSampleTime()) {
            data.setSampleTime(new Date());
        }
 
        String msg = inoutService.updateSampleData(data);
 
        if (null != msg) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(), msg);
        }
 
        // 扦样完成触发事件
        inoutEventControl.onInSimple(data);
 
        return new PageResponse<>(RespCodeEnum.CODE_0000.getCode(), "执行成功");
    }
 
    public PageResponse<InoutCheckData> submitCheck(InoutCheckData data) throws Exception {
 
        // 获取系统参数配置
        InoutSysConf inoutSysConf = inoutCommonService.getCacheInoutSysConf(data.getCompanyId(), data.getDeptId());
        if (null == inoutSysConf) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(),
                    "提示:当前库区未配置出入库流程信息,请联系管理员", data);
        }
 
        // 获取业务数据信息
        InoutParam param = new InoutParam();
        param.setCompanyId(data.getCompanyId());
        param.setId(data.getId());
        InoutData curData = inoutService.inoutProgressQuery(param);
 
        if (null == curData) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(),
                    "没有获取到入库业务数据信息,更新失败", data);
        }
 
        if (InoutConstant.PROGRESS_RECORD.equals(curData.getProgress())) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(),
                    "当前流程已经结束,不支持修改");
        }
 
        // 更新化验项信息
        CheckUpdateResult checkResult = checkStandardManager.updateCheckItems(data.getCheckId(), data.getCompanyId(), data.getCheckItems());
        if (StringUtils.isNotEmpty(checkResult.getMsg())) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(),
                    "后台执行异常:" + checkResult.getMsg(), data);
        }
 
        // 设置化验信息
        if (checkResult.getDeSum() > 0) {
            curData.setDeCheck(checkResult.getDeSum());
            data.setDeCheck(checkResult.getDeSum());
        }
        if (checkResult.getAddSum() > 0) {
            curData.setAddCheck(checkResult.getAddSum());
            data.setAddCheck(checkResult.getAddSum());
        }
        if (null == data.getCheckTime()) {
            curData.setCheckTime(new Date());
            data.setCheckTime(new Date());
        }
        curData.setCheckUser(data.getCheckUser());
        curData.setCheckStatus(data.getCheckStatus());
        curData.setFoodYear(data.getFoodYear());
 
 
        // 获取下一个流程状态
        String curProgress = curData.getProgress();
        String nextProgress = getNextProgress(curProgress, curData.getType(), inoutSysConf);
        curData = updateBasicInfo(curData, curProgress, nextProgress);
 
        // 若化验结果不合格,判断配置后续流程
        if (InoutConstant.STATUS_UNPASS.equals(curData.getCheckStatus())) {
            curData = checkNoPass(curData);
        }
 
        curData.setProgress(curData.getProgress());
        curData.setType(curData.getType());
        curData.setFoodLevel(data.getFoodLevel());
        curData.setFoodVariety(data.getFoodVariety());
        curData.setPrice(data.getPrice());
        curData.setRemarks(data.getRemarks());
        curData.setCheckItems(data.getCheckItems());
        curData.setCheckTime(data.getCheckTime());
        curData.setDepotId(data.getDepotId());
 
        if (StringUtils.isEmpty(data.getCheckUser())) {
            curData.setCheckUser(ContextUtil.getLoginUserCName());
        }
        if (null == data.getCheckTime()) {
            curData.setCheckTime(new Date());
        }
 
        String msg = inoutService.updateCheckData(curData);
 
        if (null != msg) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(), msg);
        }
 
 
        // 质检完成触发事件
        inoutEventControl.onInCheck(data, curProgress, nextProgress);
 
        return new PageResponse<>(RespCodeEnum.CODE_0000.getCode(), "执行成功");
    }
 
    public PageResponse<InoutCheckData> submitCheckData(InoutCheckData data) throws Exception {
 
 
        // 获取业务数据信息
        InoutParam param = new InoutParam();
        param.setCompanyId(data.getCompanyId());
        param.setId(data.getId());
        InoutData curData = inoutService.inoutProgressQuery(param);
 
        if (null == curData) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(),
                    "没有获取到入库业务数据信息,更新失败", data);
        }
 
        if (InoutConstant.PROGRESS_RECORD.equals(curData.getProgress())) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(),
                    "当前流程已经结束,不支持修改");
        }
 
        // 更新化验项信息
        CheckUpdateResult checkResult = checkStandardManager.updateCheckItems(data.getCheckId(), data.getCompanyId(), data.getCheckItems());
        if (StringUtils.isNotEmpty(checkResult.getMsg())) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(),
                    "后台执行异常:" + checkResult.getMsg(), data);
        }
 
        curData.setType(curData.getType());
        curData.setFoodLevel(data.getFoodLevel());
        curData.setFoodVariety(data.getFoodVariety());
        curData.setPrice(data.getPrice());
        curData.setDepotId(data.getDepotId());
        curData.setCheckItems(data.getCheckItems());
 
        String msg = inoutService.updateCheckData(curData);
        if (null != msg) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(), msg);
        }
 
        return new PageResponse<>(RespCodeEnum.CODE_0000.getCode(), "保存成功");
    }
 
    /**
     * @param param
     * @return
     */
    public PageResponse<Page<InoutCheckData>> pageCheckData(
            InoutCheckParam param) {
 
        if (StringUtils.isEmpty(param.getCompanyId())) {
            param.setCompanyId(ContextUtil.getCompanyId());
        }
        if (StringUtils.isEmpty(param.getDeptId())) {
            param.setDeptId(ContextUtil.subDeptId(null));
        }
        // 如果是已化验,则将流程状态置空,如果是未化验,则流程状态不处理,仍是CHECK状态
        if (!"NONE".equals(param.getCheckStatus())) {
            param.setProgress(null);
        }
 
        Page<InoutCheckData> checkList = inoutService.pageCheckData(param);
 
        if (null == checkList.getRecords() || checkList.getRecords().isEmpty()) {
            return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(),
                    "获取到数据信息为空");
        }
        return new PageResponse<>(RespCodeEnum.CODE_0000, checkList);
    }
 
    /**
     * 获取化验项信息
     *
     * @param param
     * @return
     */
    public PageResponse<List<CheckItemData>> getCheckItemData(
            InoutCheckParam param) {
 
        try {
            List<CheckItemData> result = checkStandardManager.listCheckItem(
                    param.getCompanyId(), param.getCheckId(),
                    param.getFoodVariety());
 
            return new PageResponse<>(RespCodeEnum.CODE_0000, result);
        } catch (Exception e) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(), "后台异常:"
                    + e.getMessage(), null);
        }
    }
 
 
    /**
     * 获取单价信息
     *
     * @param param
     * @return
     */
    public PageResponse<InoutPrice> getPrice(InoutCheckParam param) {
        try {
 
            List<InoutPrice> price = inoutService.getPrice(param);
            if (null == price || price.isEmpty()) {
                return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(),
                        "未获取到粮食定价配置信息");
            }
            return new PageResponse<>(RespCodeEnum.CODE_0000, price.get(0));
 
        } catch (Exception e) {
            return new PageResponse<>(RespCodeEnum.CODE_1111.getCode(), "后台异常:"
                    + e.getMessage(), null);
        }
    }
 
    /**
     * @param param
     * @return
     */
    public PageResponse<InoutCheckData> inoutDataByCheckId(InoutCheckParam param) {
        if (StringUtils.isEmpty(param.getCheckId())) {
            return new PageResponse<>(RespCodeEnum.CODE_1007.getCode(),
                    "查询参数不完整,查询失败!");
        }
 
        InoutCheckData result;
        try {
            param.setProgress(InoutConstant.PROGRESS_CHECK);
            result = inoutService.inoutDataByCheckId(param);
            if (null == result) {
                return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(),
                        "系统未查询到执行中的车辆信息", null);
            }
 
            return new PageResponse<>(RespCodeEnum.CODE_0000, result);
 
        } catch (Exception e) {
            return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(),
                    e.getMessage());
        }
    }
 
    public void initInoutWeight(List<InoutConf> listInoutConf, String sort) {
        inoutEventControl.initInoutWeight(listInoutConf, sort);
    }
 
}