hjg
2023-11-17 3780c5e65b05bf23020810798babc6d20311fa79
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
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
package com.mandi.webservice.service.impl;
 
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.mandi.basicconfig.mapper.FdRepertoryNexusMapper;
import com.mandi.basicconfig.mapper.NczsjMapper;
import com.mandi.basicconfig.persist.*;
import com.mandi.common.JSONobj;
import com.mandi.common.Jacksonmethod;
import com.mandi.common.ParamFileter.BigDecimalfiter;
import com.mandi.common.SpringContext;
import com.mandi.common.Wxhtmethod;
import com.mandi.dao.common.ObjectResult;
import com.mandi.dao.common.PageResult;
import com.mandi.fendan.mapper.*;
import com.mandi.fendan.persist.*;
import com.mandi.fendan.util.Contants;
import com.mandi.fendan.util.ConvertTool;
import com.mandi.fendan.util.ThreadPoolUtil;
import com.mandi.fendan.util.persist.Serviceenum;
import com.mandi.fendan.util.persist.WaitXSTDMX;
import com.mandi.fendan.util.persist.Yktbsitem;
import com.mandi.servlet.file.impl.FileLoadImpl;
import com.mandi.webservice.service.IFdXSTDService;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.*;
 
@Service
public class FdXSTDService implements IFdXSTDService{
 
    @Resource
    private Fd_XSTDMapper tdper;
    @Resource
    private Fd_XSTDMXMapper tdmxper;
    @Resource
    private Fd_YKTMapper yktper;
    @Resource
    private VehicleXSTDMapper vhxper;
    @Resource
    private Fd_VehicleMapper  vhper;
    @Resource
    private NczsjMapper zsjper;
    @Resource
    private Fd_BsheetMapper fbper;
    @Resource
    private Fd_CompanyMapper cmper;
    @Resource
    private Fd_GbxstdMapper gbper;
    @Resource
    private Fd_GbxstdmxMapper gbmxper;
    @Resource
    private BusinessMapper bsper;
    @Resource
    private BsheetEcgbMapper beper;
    @Resource
    private FdRepertoryNexusMapper reNxPer;
 
    private Logger log= Logger.getLogger(FileLoadImpl.class);
 
    public static Boolean refreshMsLedLock = false;
    public static Boolean refreshYrLedLock = false;
 
    @Override
    @Transactional(rollbackFor=Exception.class)
    public ObjectResult<Boolean> saveFd_xstd(String dataArr) throws Exception {
        ObjectResult<Boolean> orr=new ObjectResult<Boolean>();
        JsonArray ja=null;
        try {
            JsonParser jp=new JsonParser();
             ja=jp.parse(dataArr).getAsJsonArray();
        } catch (Exception e) {
            throw new Exception("数据解析错误,参数不是json数组!");
        }
        if(ja!=null){
            for (JsonElement je : ja) {
                JsonObject jo=je.getAsJsonObject();
                JsonObject objJo=null;
                JsonArray tdmxArr=null;
                try {
                     objJo=jo.getAsJsonObject("head");
                     tdmxArr=jo.getAsJsonArray("body");
                } catch (Exception e) {
                    throw new Exception("数据解析错误,数据格式不正确!");
                }
                String ztbz=JSONobj.getstring(objJo, "td_ztbz");
                Fd_XSTD xstd=new Fd_XSTD();
                xstd=JSONobj.getobj(objJo, xstd);
                if("01".equals(ztbz)){
                    xstd.setTd_ztbz(2);
                }else if("02".equals(ztbz)){
                    xstd.setTd_ztbz(1);
                }else if("03".equals(ztbz)){
                    xstd.setTd_ztbz(3);
                }else{
                    xstd.setTd_ztbz(1);
                }
                //明细业务单元
                String mxywyd="";
                //根据仓库查找对应的厂区编号
                String ckNo="";//明细仓库编号
 
                Ncywdy cn=this.zsjper.getNcywdybyCode(xstd.getTd_ywdybh());
                //系统部门找不到不做处理
                if(cn==null||cn.getYktCode()==null||StringUtils.isEmpty(cn.getYktCode())){
                    continue;
                }
                Ncworker ncw=this.zsjper.getNcworkerbyCode(xstd.getTd_zdr());
                Ncdepart ncd=this.zsjper.getNcdepartbyCode(xstd.getTd_bmbh());
                if(ncw!=null)
                    xstd.setTd_zdrmc(ncw.getName());
                if(ncd!=null)
                    xstd.setTd_bmmc(ncd.getName());
                int td_wfdsl=0;//未分单数量
                double td_wfdzl=0;//未分单重量
                String td_tdls=xstd.getTd_tdls();//提单流水号
                for (int i = 0; i < tdmxArr.size(); i++) {
                    objJo=tdmxArr.get(i).getAsJsonObject();
                    String h=JSONobj.getstring(objJo, "h");
                    String k=JSONobj.getstring(objJo, "k");
                    String c=JSONobj.getstring(objJo, "c");
                    Fd_XSTDMX xstdmx=new Fd_XSTDMX();
                    xstdmx=JSONobj.getobj(objJo,xstdmx);
                    double mz=xstdmx.getTdmx_mz();
 
                    xstdmx.setTdmx_ggxh((h==null?"C":h)+"*"+(k==null?"C":k)+"*"+(c==null?"C":c));
                    if(StringUtils.isEmpty(td_tdls)){
                        td_tdls=xstdmx.getTdmx_tdls();
                    }
                    if(StringUtils.isBlank(mxywyd)){
                        cn=this.zsjper.getNcywdybyCode(xstdmx.getTdmx_fhzz());
                        //系统部门找不到不做处理
                        if(cn!=null && StringUtils.isNotBlank(cn.getYktCode())){
                            mxywyd=xstdmx.getTdmx_fhzz();
                        }else{
                            continue;
                        }
                        if(StringUtils.isBlank(ckNo)) {
                            Ncck ck=this.zsjper.getNcckbyPkid(xstdmx.getTdmx_ck());
                            if(ck!=null)
                                ckNo=ck.getCode();
 
                        }
                    }
                    //有上次已装车记录
                    Fd_XSTDMX ccr=this.tdmxper.get(xstdmx.getTdmx_tdfl());
                    if(ccr!=null&&ccr.getTdmx_zcbz()>0){
                        continue;
                    }else{
                        if(ccr!=null){
                            this.tdmxper.del(xstdmx.getTdmx_tdfl());
                        }
                    }
 
 
                    if(Contants.NcNosend.equals(mxywyd)){
                        this.tdmxper.delbyTDLS(xstd.getTd_tdls());
                        this.tdper.del(xstd.getTd_tdls());
                        break;
                    }
                    this.tdmxper.insert(xstdmx);
                    //如果这次推过来的是关闭的 明细保存  表头不增加未分单数量
                    if(xstdmx.getTdmx_gbbz()==0){
                        td_wfdsl++;
                        td_wfdzl+=mz;
                    }
 
                }
 
                if(!Contants.NcNosend.equals(mxywyd)){
                    //获取生成厂区编号
                    FdRepertoryNexus renx=this.reNxPer.getbyckNoAndYwdyNo(ckNo,mxywyd);
                    if(renx!=null)
                        xstd.setTd_cqNo(renx.getSdepartNo());
                    xstd.setTd_wfdsl(td_wfdsl);
                    xstd.setTd_wfdzl(td_wfdzl);
                    xstd.setTd_ywdybh(mxywyd);
                    this.tdper.insert(xstd);
                }
            }
        }
        orr.setCode(0);
        orr.setErrmsg("新增成功!");
        return orr;
    }
 
    @Override
    @Transactional(rollbackFor=Exception.class)
    public ObjectResult<Boolean> delFd_xstd(String dataArr) throws Exception {
        ObjectResult<Boolean> orr=new ObjectResult<Boolean>();
        JsonArray ja=null;
        try {
            JsonParser jp=new JsonParser();
            ja=jp.parse(dataArr).getAsJsonArray();
        } catch (Exception e) {
            throw new Exception("数据解析错误,参数不是json数组!");
        }
        if(ja!=null){
            for (JsonElement je : ja) {
                JsonObject jo=je.getAsJsonObject();
                JsonObject objJo=null;
                try {
                     objJo=jo.getAsJsonObject("head");
                } catch (Exception e) {
                    throw new Exception("数据解析错误,数据格式不正确!");
                }
                String td_tdls=JSONobj.getstring(objJo, "td_tdls");
                if(StringUtils.isBlank(td_tdls)){
                    throw new Exception("数据解析错误,提单流水号不能为空!");
                }
 
                Fd_XSTD td_xstd=this.tdper.get(td_tdls);
                if(td_xstd!=null){
                    int c=this.tdmxper.getbytdlswithZCorFD(td_tdls);
                    if(c>0){
                        throw new Exception("发货单:"+td_xstd.getTd_tdbh()+" 已经分单或装车,不能删除!");
                    }
//                    //只删除标志是未装车的
                    this.tdmxper.delbyTDLS(td_tdls);
                    this.tdper.del(td_tdls);
                    this.gbper.del(td_xstd.getTd_tdbh());
                    this.gbmxper.del(td_xstd.getTd_tdbh());
                }
            }
        }
        orr.setCode(0);
        orr.setErrmsg("删除成功!");
        return orr;
    }
 
    @Override
    @Transactional(rollbackFor=Exception.class)
    public ObjectResult<Boolean> closeFd_xstd(String dataArr) throws Exception {
        ObjectResult<Boolean> orr=new ObjectResult<Boolean>();
        JsonArray ja=null;
        try {
            JsonParser jp=new JsonParser();
            ja=jp.parse(dataArr).getAsJsonArray();
        } catch (Exception e) {
            throw new Exception("数据解析错误,参数不是json数组!");
        }
        if(ja!=null){
            for (JsonElement je : ja) {
                JsonObject jo=je.getAsJsonObject();
                JsonObject objJo=null;
                JsonArray tdmxArr=null;
                try {
                     objJo=jo.getAsJsonObject("head");
                     tdmxArr=jo.getAsJsonArray("body");
                } catch (Exception e) {
                    throw new Exception("数据解析错误,数据格式不正确!");
                }
 
                //提单流水号
                String td_tdls=objJo.get("td_tdls")==null?"":objJo.get("td_tdls").getAsString();
                Fd_XSTD xstd=this.tdper.get(td_tdls);
                if(xstd!=null){
                    for (int i = 0; i < tdmxArr.size(); i++) {
                        objJo=tdmxArr.get(i).getAsJsonObject();
                        String tdmx_tdfl=objJo.get("tdmx_tdfl")==null?"":objJo.get("tdmx_tdfl").getAsString();
                        String sfckgb=objJo.get("sfckgb")==null?"":objJo.get("sfckgb").getAsString();
                        Fd_XSTDMX tdmx=this.tdmxper.get(tdmx_tdfl);
                        if(tdmx!=null&&tdmx.getTdmx_gbbz()==0){
                            tdmx.setTdmx_gbbz(1);
                            this.tdmxper.updateGB(tdmx);
//                            if(tdmx.getTdmx_zcbz()!=0){
//                                continue;
//                            }
                            if(tdmx.getTdmx_fdbz()!=0){
                                //已分单未装车
                                if("N".equals(sfckgb)){
                                    if(tdmx.getTdmx_zcbz()==0){
                                        throw new Exception("提单明细:批次号:"+tdmx.getTdmx_pch()+" 已分单未装车,请联系物流公司取消分单!");
                                    }
                                }
                            }
//                            if(tdmx.getTdmx_zcbz()!=0){
//                                throw new Exception("提单流水号:"+xstd.getTd_tdls()+" 已扫码装车,不能关闭!");
//                            }
 
                            if(tdmx.getTdmx_fdbz()==0){
                                xstd.setTd_wfdsl(xstd.getTd_wfdsl()-1);
                                xstd.setTd_wfdzl(xstd.getTd_wfdzl()-tdmx.getTdmx_mz());
                                this.tdper.updateFD(xstd);
                            }else{
                                if(tdmx.getTdmx_zcbz()==0){
                                    tdmx.setTdmx_fdywbh(null);
                                    tdmx.setTdmx_fdwlgsbh(null);
                                    tdmx.setTdmx_fdbz(0);
                                    tdmx.setTdmx_fdcph(null);
                                    this.tdmxper.updateFD(tdmx);
                                }
                            }
 
                            //这个关闭记录表示的是一卡通里的关闭操作
                            Fd_Gbxstdmx gbmx=this.gbmxper.get(tdmx_tdfl);
                            if(gbmx!=null){
                                Fd_Gbxstd gb=this.gbper.get(xstd.getTd_tdbh());
                                if(gb!=null){
                                    gb.setTd_gbsl(gb.getTd_gbsl()-1);
                                    gb.setTd_gbzl(BigDecimalfiter.sub(gb.getTd_gbzl(),tdmx.getTdmx_mz()));
                                    this.gbper.update(gb);
                                }
                                this.gbmxper.delbyId(tdmx_tdfl);
                            }
                        }
                    }
                }
            }
        }
        orr.setCode(0);
        orr.setErrmsg("关闭成功!");
        return orr;
    }
 
    @Override
    @Transactional(rollbackFor=Exception.class)
    public ObjectResult<Boolean> openFd_xstd(String dataArr) throws Exception {
        ObjectResult<Boolean> orr=new ObjectResult<Boolean>();
        JsonArray ja=null;
        try {
            JsonParser jp=new JsonParser();
             ja=jp.parse(dataArr).getAsJsonArray();
        } catch (Exception e) {
            throw new Exception("数据解析错误,参数不是json数组!");
        }
        if(ja!=null){
            for (JsonElement je : ja) {
                JsonObject jo=je.getAsJsonObject();
                JsonObject objJo=null;
                JsonArray tdmxArr=null;
                try {
                     objJo=jo.getAsJsonObject("head");
                     tdmxArr=jo.getAsJsonArray("body");
                } catch (Exception e) {
                    throw new Exception("数据解析错误,数据格式不正确!");
                }
 
                //提单流水号
                String td_tdls=objJo.get("td_tdls")==null?"":objJo.get("td_tdls").getAsString();
                Fd_XSTD xstd=this.tdper.get(td_tdls);
                if(xstd!=null){
                    for (int i = 0; i < tdmxArr.size(); i++) {
                        objJo=tdmxArr.get(i).getAsJsonObject();
                        String tdmx_tdfl=objJo.get("tdmx_tdfl")==null?"":objJo.get("tdmx_tdfl").getAsString();
                        Fd_XSTDMX tdmx=this.tdmxper.get(tdmx_tdfl);
                        if(tdmx!=null&&tdmx.getTdmx_gbbz()==1){
                            tdmx.setTdmx_gbbz(0);
                            this.tdmxper.updateGB(tdmx);
                            if(tdmx.getTdmx_fdbz()==0){
                                xstd.setTd_wfdsl(xstd.getTd_wfdsl()+1);
                                xstd.setTd_wfdzl(xstd.getTd_wfdzl()+tdmx.getTdmx_mz());
                                this.tdper.updateFD(xstd);
                            }
                        }
                    }
                }
            }
        }
        orr.setCode(0);
        orr.setErrmsg("打开成功!");
        return orr;
    }
 
    @Override
    @Transactional(rollbackFor=Exception.class)
    public PageResult<Map<String, Object>> addZCMX(String cardID,String yktywdh, JsonArray dataArr) throws Exception {
        PageResult<Map<String, Object>> prr=new PageResult<Map<String,Object>>();
        List<Map<String, Object>> relist=new ArrayList<Map<String,Object>>();
        if(StringUtils.isEmpty(yktywdh)){
            prr.setCode(1);
            prr.setErrmsg("参数:一卡通业务单号为空!");
            return prr;
        }
        //根据卡号判断分单信息
        //循环批次号 如果没有分单 修改为分单和装车
        Fd_Bsheet fdb=this.fbper.getbybsNo(StringUtils.isNotBlank(yktywdh)?yktywdh:"");
        String vehicleId="";
        if(fdb==null){
            prr.setCode(1);
            prr.setErrmsg("一卡通业务单未找到,请确认单号是否正确!");
            return prr;
        }
 
        if (fdb.getStatus() != Serviceenum.在办) {
            prr.setCode(2);
            prr.setErrmsg("一卡通业务单不是在办状态,系统不做处理!");
            return prr;
        }
        if (StringUtils.isBlank(cardID)) {
            prr.setCode(1);
            prr.setErrmsg("参数:一卡通卡号为空!");
            return prr;
        }
        if (dataArr == null) {
            prr.setCode(1);
            prr.setErrmsg("参数:批次号为空!");
            return prr;
        }
 
        Map<String, Object> mp = this.yktper.getcardinuse(cardID);
        if (mp == null) {
            throw new Exception("卡号不正确未找到在用卡!");
        }
        vehicleId = mp.get("numberplates") == null ? "" : mp.get("numberplates").toString();
        if (!vehicleId.equals(fdb.getVehicleId())) {
            prr.setCode(1);
            prr.setErrmsg("参数:卡号关联的车牌号和业务单关联的车牌号不一致");
            return prr;
        }
 
        // yktywdh = fdb.getBsNo();
 
        int wcnum=this.bsper.getweightDoc(vehicleId, fdb.getBsNo());
        if(wcnum<=0){
            prr.setCode(1);
            prr.setErrmsg("车辆:"+vehicleId+" 未找到过磅单,请先过磅!");
            return prr;
        }
        yktywdh=fdb.getBsNo();
        //是否郑州厂区
        boolean iszz=false;
 
        //检查是否装车完成
        Map<String, Object> mmr=this.bsper.selectTaskTime(fdb.getBsNo());
        try {
            if(mmr!=null&&mmr.get("zcWc")!=null&&Boolean.valueOf(mmr.get("zcWc").toString())) {
                prr.setCode(1);
                prr.setErrmsg("联系成品库办公室取消装车完成!");
                return prr;
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
 
        String currPch="";// 当前批次号
        //第一次添加装卸记录发送装车信息
        int sfzc=this.fbper.getbsitembyNo(fdb.getBsNo());
        if(sfzc<=0) {
            //发送微信公众号提醒信息
            String url=Contants.yktLocalAdd+"/routine/sendZcMsg.htm";
            Map<String, String> paramMp=new HashMap<String, String>();
            mp.put("sdepartNo", fdb.getSdepartNo());
            mp.put("vehicleId", vehicleId);
            mp.put("businessNo", fdb.getBsNo());
            Wxhtmethod.postJson(url, paramMp);
        }
        // 现场过完毛重后出现扫码装车的,必须重新过磅
        int grossNum=this.bsper.getgrosstDoc(vehicleId, fdb.getBsNo());
        List tdmxPchList = new ArrayList();
        for (JsonElement je : dataArr) {
            JsonObject jo=je.getAsJsonObject();
            String tdmx_pch = jo.get("tdmx_pch").getAsString();
            tdmxPchList.add(tdmx_pch);
        }
        if("08".equals(fdb.getSdepartNo())) { // 只是义瑞厂区有效
            // 检查是否当前区域已经排队
            Map paramMap = new HashMap();
            paramMap.put("vehicleId", vehicleId);
            paramMap.put("tdmxPchList", tdmxPchList);
            String jsonStr = Jacksonmethod.tojson(paramMap, false);
            JsonObject jsonObject = Wxhtmethod.postJsonStr(Contants.yr_canDoScanUrl, jsonStr);
            if (jsonObject.get("code").getAsInt() != 0) {
                prr.setCode(1);
                prr.setErrmsg(jsonObject.get("errmsg").getAsString());
                return prr;
            }
        }
        for (JsonElement je : dataArr) {
            JsonObject jo=je.getAsJsonObject();
            String tdmx_pch = jo.get("tdmx_pch").getAsString();
//            Fd_XSTDMX tdmx=this.tdmxper.getbyPCH(tdmx_pch);
            Fd_XSTDMX tdmx=this.tdmxper.getbyPCHONE(tdmx_pch,vehicleId);
            if(tdmx==null){
                prr.setCode(1);
                prr.setErrmsg("车辆:"+vehicleId+"没有该明细的分单记录或已关闭,不能添加装车!");
                return prr;
            }
            // 下面部分注释掉,改为sql语句中判断是否为未关闭
            /*if(tdmx.getTdmx_gbbz()!=0){
                prr.setCode(1);
                prr.setErrmsg("明细:"+tdmx.getTdmx_pch()+" 在NC是已关闭状态,不能添加装车!");
                return prr;
            }*/
 
 
//            boolean booff=false;
//            if(tdmx.getTdmx_zcbz()!=0){
//                booff=true;
//                throw new Exception("批次号:"+tdmx_pch+" 已经提交装车,不能重复提交装车!");
//            }
            Fd_XSTD xstd=this.tdper.get(tdmx.getTdmx_tdls());
            if(Contants.nc_ywdy_ZZ.equals(xstd.getTd_cqNo())&&!iszz){
                iszz=true;
            }
            Vehicle_XSTD vxs=this.vhxper.getVhicleByIDs(vehicleId, xstd.getTd_tdbh());
            if(vxs==null) {
                prr.setCode(1);
                prr.setErrmsg("未找到配车关联记录,不能添加装车!");
                return prr;
            }
            //修改提单的未分单数量和重量
            //修改提单vehicle_XSTD表
            Fd_Company fdc=null;
            //是否分单
            if(tdmx.getTdmx_fdbz()==0){
                fdc=this.cmper.getByCompanyNo(vxs.getCompanyNo());
                tdmx.setTdmx_fdbz(1);
                tdmx.setTdmx_fdcph(vehicleId);
                tdmx.setTdmx_fdywbh(yktywdh);
                tdmx.setTdmx_fdwlgsbh(vxs.getCompanyNo());
                xstd.setTd_wfdsl(xstd.getTd_wfdsl()-1);
                xstd.setTd_wfdzl(xstd.getTd_wfdzl()-tdmx.getTdmx_mz());
                this.tdper.updateFD(xstd);
                vxs.setFd_num(vxs.getFd_num()+1);
                vxs.setFd_weight(vxs.getFd_weight()+tdmx.getTdmx_mz());
                this.vhxper.updatewithIDs(vxs);
            }else {
                fdc=this.cmper.getByCompanyNo(tdmx.getTdmx_fdwlgsbh());
            }
            tdmx.setTdmx_zcbz(1);
            tdmx.setTdmx_zccph(vehicleId);
            tdmx.setTdmx_zcwlgsName(fdc.getName());
            tdmx.setTdmx_zcdriver(fdb.getDriver());
            tdmx.setTdmx_zcphone(fdb.getPhone());
            this.tdmxper.updateZCnoFD(tdmx);
 
            WaitXSTDMX wx=this.fbper.getWaittdmx(tdmx.getTdmx_tdls(), tdmx.getTdmx_tdfl());
            this.fbper.delbsitem(tdmx.getTdmx_pch(), fdb.getBsNo());
            Yktbsitem bsi=new Yktbsitem();
            bsi.setId(UUID.randomUUID()+"");
            bsi.setBussinessNo(fdb.getBsNo());
            bsi.setName(wx.getTdmx_wlmc());
            bsi.setMaterielNo(tdmx.getTdmx_wlbh());
            bsi.setNorms(wx.getTdmx_ggxh());
            bsi.setUnitname("Kg");
            bsi.setNum(1);
            bsi.setWeight(BigDecimalfiter.mulcon(tdmx.getTdmx_jz(), 1000, Contants.scale));
            bsi.setPrice(0);
            bsi.setPreweight(BigDecimalfiter.mulcon(tdmx.getTdmx_jz(), 1000, Contants.scale));
            bsi.setPici(tdmx.getTdmx_pch());
            bsi.setHejin(wx.getTdmx_hjzt());
            bsi.setGross(BigDecimalfiter.mulcon(tdmx.getTdmx_mz(), 1000, Contants.scale));
            bsi.setTaotongWeight(0);
            bsi.setMoney(0);
            bsi.setCdepart("");
            bsi.setCdepartid(0);
            bsi.setCname("");
            bsi.setCreateid("");
            bsi.setCtime(new Date());
            bsi.setLoadbuild(false);
            bsi.setFachang(false);
            bsi.setFachang(true);
            bsi.setFcnum(0);
            if(StringUtils.isBlank(xstd.getTd_fpbh())){
                bsi.setRecompany(wx.getTdmx_shdw());
            }else{
                bsi.setRecompany(
                          ConvertTool.covertToCustoms(xstd.getTd_ywdybh())
                            );
            }
            bsi.setReID("");
            bsi.setDecompany(ConvertTool.ncCovertToFactoryName(xstd.getTd_ywdybh()));
            bsi.setDeID("");
            bsi.setFrombatch("ERP系统");
            bsi.setForshowindx(1);
            bsi.setReserved11(0);
            bsi.setReserved12(0);
            bsi.setReserved13(0);
            bsi.setJsnum(0);
            this.fbper.insertbsitem(bsi);
            Map<String, Object> remp=new HashMap<String, Object>();
            remp.put("tdmx_pch",tdmx.getTdmx_pch());
            remp.put("td_tdls",tdmx.getTdmx_tdls());
            remp.put("tdmx_tdfl",tdmx.getTdmx_tdfl());
            relist.add(remp);
            currPch  = tdmx.getTdmx_pch();
        }
 
        if(grossNum>0) {
            Map<String, Object> xstm=new HashMap<String, Object>();
            xstm.put("canCm",false);
            xstm.put("businessNo",fdb.getBsNo());
            this.bsper.updateCanCm(xstm);
            this.bsper.updatecanFb(vehicleId, fdb.getBsNo());
        }
 
        //如果是郑州的销售业务单删除排队信息
        if(iszz){
            int ca=this.bsper.getvehicleQ(vehicleId);
            if(ca>0){
//                this.bsper.delvehicleQ(vehicleId);
                //删除车辆排队信息
                String url=Contants.yktLocalAdd+"/search/vehicleQueue/delItem.htm";
                Map<String, String> paramMp=new HashMap<String, String>();
                mp.put("vehicleID", vehicleId);
                Wxhtmethod.postJson(url, paramMp);
            }
        }
 
         // 此处异步触发一下明晟发车
        if("06".equals(fdb.getSdepartNo())) {
            final String cph = currPch;
            final String finalVehicleId = vehicleId;
            ThreadPoolUtil.getInstance().execute(()->{
                synchronized (FdXSTDService.refreshMsLedLock) {
                    if (FdXSTDService.refreshMsLedLock) { // 系统繁忙
                        log.info(finalVehicleId + "" + cph + "系统繁忙!");
                        return;
                    }
                }
                try {
                    synchronized (FdXSTDService.refreshMsLedLock) {
                        FdXSTDService.refreshMsLedLock = true;
                    }
                    sendToMsLed(cph);
                } catch (Exception e) {
                    log.error("出现异常:"+ e.getMessage(),e);
                } finally {
                    synchronized (FdXSTDService.refreshMsLedLock) {
                        FdXSTDService.refreshMsLedLock = false;
                    }
                }
            });
        }
        if("08".equals(fdb.getSdepartNo())) {
            final String cph = currPch;
            ThreadPoolUtil.getInstance().execute(()->{
 
                Map scanCodeParamMap = new HashMap();
                scanCodeParamMap.put("pch",cph);
                Wxhtmethod.post(Contants.yr_scanCodeNoticeUrl, scanCodeParamMap);
 
                try {
                    synchronized (FdXSTDService.refreshYrLedLock) {
                        if (FdXSTDService.refreshYrLedLock) { // 系统繁忙
                            return  ;
                        }
                        FdXSTDService.refreshYrLedLock = true;
                    }
                    sendToYrLed(cph);
                } catch (Exception e) {
                    log.error(e);
                } finally {
                    synchronized (FdXSTDService.refreshYrLedLock) {
                        FdXSTDService.refreshYrLedLock = false;
                    }
                }
            });
        }
        prr.setCode(0);
        prr.setErrmsg("装车操作成功!");
        prr.setList(relist);
        return prr;
    }
 
    /**
     *
     * @param currPch 当前批次号
     */
    public void sendToMsLed(String currPch){
        //A区  76-86 +  99-1
        //B区  14-33 +  99-2
        //C区  53-67 +  99-3
        log.info("LED-ABC当前扫码批次号:" + currPch);
        List<Map> totalList =  tdmxper.getHwList("06"); // 获取明晟所有在办业务单的信息
        log.info("LED-ABC获取信息条数:" + totalList.size());
        Map<String,List<Map>> ywbhListMap = new HashMap<>();
        for (Map map : totalList) {
            String ywdw = (String) map.get("tdmx_fdywbh");
            if(ywdw == null) {
                log.info("出现业务单号为null的情况:" + Jacksonmethod.tojson(map,false));
                continue;
            }
            if(ywbhListMap.get(ywdw) == null) {
                ywbhListMap.put(ywdw, new ArrayList<>());
            }
            ywbhListMap.get(ywdw).add(map);
        }
        String str1 = " 车号  A区  B区  C区" + "\r\n", str2="";
        //"(车牌号)\\-(A区已装车)/(A区总计)\\-(B区已装车)/(B区总计)\\-(C区已装车)/(C区总计)\\r\\n";
        String formatStr = "%s %s/%s %s/%s %s/%s\r\n";
        // 只有1个区
 
        Map speakParam = new HashMap(1);
        for (Map.Entry<String, List<Map>> entry : ywbhListMap.entrySet()) {
            // 遍历每个业务单的
            // 当前装车A、B、C区
            boolean currA = false,currB = false, currC = false;
            boolean hasZcwc = false; // 保证已经完成了
            String ywhb = entry.getKey();
            List<Map> vehicleIdTotalList = entry.getValue();
            Map<String, Integer> totalAbcMap = new HashMap(3); // 总计信息
            totalAbcMap.put("A", 0);
            totalAbcMap.put("B", 0);
            totalAbcMap.put("C", 0);
            Map<String, Integer> abcMap = new HashMap(3); // 已装信息
            abcMap.put("A", 0);
            abcMap.put("B", 0);
            abcMap.put("C", 0);
            String cph = ""; // 车牌号
            for (Map map : vehicleIdTotalList) {
                String hw = (String) map.get("tdmx_hwmc");
                String pch = (String) map.get("tdmx_pch");
                if(StringUtils.isBlank(cph))  cph = (String) map.get("tdmx_fdcph");
                Integer zcbz = (Integer) map.get("tdmx_zcbz");
                String hh = hw.replaceAll("\\-.*","");
                hh = hh.substring(hh.length()-2); // 取最后两位
                int num = Integer.parseInt(hh);
                if (num >= 76 && num <= 86 || hw.lastIndexOf("99-1") >= 0) { // A区
                    totalAbcMap.put("A",totalAbcMap.get("A") + 1);
                    if (zcbz != null && zcbz == 1) { // 装车完成
                        abcMap.put("A", abcMap.get("A") + 1);
                        hasZcwc = true;
                    }
                    if(pch.equals(currPch)) {
                        currA = true; currB = false;currC =false;
                    }
                } else if (num >= 14 && num <= 33 || hw.lastIndexOf("99-2") >= 0) { // B区
                    totalAbcMap.put("B", totalAbcMap.get("B") + 1);
                    if (zcbz != null && zcbz == 1) {
                        abcMap.put("B", abcMap.get("B") + 1);
                        hasZcwc = true;
                    }
                    if(pch.equals(currPch)) {
                        currA = false; currB = true;currC =false;
                    }
                } else if (num >= 53 && num <= 67 || hw.lastIndexOf("99-3") >= 0) { // C区
                    totalAbcMap.put("C", totalAbcMap.get("C") + 1);
                    if (zcbz != null && zcbz == 1) {
                        abcMap.put("C", abcMap.get("C") + 1);
                        hasZcwc = true;
                    }
                    if(pch.equals(currPch)) {
                        currA = false; currB = false; currC =true;
                    }
                }
            }
            // if (!hasZcwc) continue; //没有装车完成的也跳过
            /*if (!(currA || currB || currC) && abcMap.get("A") + abcMap.get("B") + abcMap.get("C") ==
                    totalAbcMap.get("A") + totalAbcMap.get("B") + totalAbcMap.get("C")) { // 非当前车牌号,已经装车完成但是未出厂
                continue;
            }*/
            if(totalAbcMap.get("B")==0 && totalAbcMap.get("C")==0 && totalAbcMap.get("A")==0) {
                continue;
            }
            str2 += String.format(formatStr, cph, fillWithZero(abcMap.get("A")), fillWithZero(totalAbcMap.get("A")),
                    fillWithZero(abcMap.get("B")), fillWithZero(totalAbcMap.get("B")),
                    fillWithZero(abcMap.get("C")), fillWithZero(totalAbcMap.get("C")));
            log.info(ywhb +  "投屏准备完成,开始准备语音。");
            // 开始准备语音
            int total = 0;
            if(currA && abcMap.get("A") == totalAbcMap.get("A") && totalAbcMap.get("A")>0) { // 当前车在A区装车完成
                total = totalAbcMap.get("A");
            }
            if(currB && abcMap.get("B") == totalAbcMap.get("B") && totalAbcMap.get("B")>0) {// 当前车在B区装车完成
                total = totalAbcMap.get("B");
            }
            if(currC && abcMap.get("C") == totalAbcMap.get("C") && totalAbcMap.get("C")>0) {// 当前车在C区装车完成
                total = totalAbcMap.get("C");
            }
            try {
                String speakTxt = startToSpeak(cph,total,abcMap,totalAbcMap);
                if(StringUtils.isNotBlank(speakTxt)) {
                    log.info("SpeakTxt:"+ speakTxt);
                    speakParam.put("SpeakTxt", speakTxt);
                }
            }catch (Exception e){
                log.error("准备语音出错!" + e.getMessage(),e);
            }
 
            // 准备语音结束
            // 单辆车的单个业务单的信息结束
        }
        if(StringUtils.isBlank(str2)) {
            str2 = "无车辆";
        }
        Map paramMap = new HashMap();
        paramMap.put("strTitle",str1);
        paramMap.put("strContent",str2);
        try {
            log.info("LED-ABC webServiceAddrss:"+Contants.msLedServiceAddr+"st1::" +str1+";;;;;str2::"+str2);
            String re = Wxhtmethod.post(Contants.msLedServiceAddr + "/SendLED",paramMap);
            log.info("LED-ABC result:"+re);
            // 语音只播一条
            String re1 = Wxhtmethod.post(Contants.msLedServiceAddr + "/Speak",speakParam);
        }catch (Exception e){}
 
 
    }
 
    /**
     * 准备语音
     * @param cph 车牌号
     * @param total 当前区总数
     * @param abcMap A、B、C为key的区已装数目
     * @param totalAbcMap A、B、C为key的区总数
     * @return
     */
    String startToSpeak(String cph, int total,Map<String,Integer> abcMap,Map<String,Integer> totalAbcMap) {
        String speakTxt=null ;
        if(total > 0) { // 播放语音,请离开区域
            String speakTxtFormatStr1 = "车号%s ,装货%d件已完成,请司机清点货物后尽快驶离装货区。";
            // 只有2个区
            String speakTxtFormatStr2 = "车号%s,%s区装货%d件已全部扫描完成,请司机清点货物后去%s区装货。";
            // 只有3个区
            String speakTxtFormatStr3 = "车号%s,%s区装货%d件已全部扫描完成,请司机清点货物后去%s区%s区装货。";
 
            if( (abcMap.get("A")>0  && totalAbcMap.get("B")==0 && totalAbcMap.get("C")==0) ||
                    (abcMap.get("B")>0  && totalAbcMap.get("A")==0 && totalAbcMap.get("C")==0)  ||
                    (abcMap.get("C")>0  && totalAbcMap.get("B")==0 && totalAbcMap.get("D")==0)
            ) { // 只有一个区域有货物
                speakTxt= String.format(speakTxtFormatStr1, cph, total);
            }else  if(totalAbcMap.get("A")>0 && totalAbcMap.get("B")>0 &&
                    (totalAbcMap.get("C")==0 || totalAbcMap.get("C") == abcMap.get("C"))){ // AB都有货 C区已完或无货
                if(totalAbcMap.get("C")>0 && abcMap.get("A")< totalAbcMap.get("A") && abcMap.get("B")< totalAbcMap.get("B")) { // C装完AB未装完
                    speakTxt= String.format(speakTxtFormatStr3, cph,"C", total,"A","B");
                } else if(abcMap.get("A")== totalAbcMap.get("A") && abcMap.get("B")< totalAbcMap.get("B")) { //A完B未完
                    speakTxt = String.format(speakTxtFormatStr2, cph, "A", total, "B");
                }else if(abcMap.get("B")== totalAbcMap.get("B") && abcMap.get("A")< totalAbcMap.get("A")) {//B完A未完
                    speakTxt = String.format(speakTxtFormatStr2, cph, "B", total, "A");
                }else { // 全部装完
                    speakTxt= String.format(speakTxtFormatStr1, cph, total);
                }
            }else if(totalAbcMap.get("C")>0 && totalAbcMap.get("B")>0 &&
                    (totalAbcMap.get("A")==0 || totalAbcMap.get("A") == abcMap.get("A")) ){ //BC有货 A区已完或无货
                if(totalAbcMap.get("A")>0 && abcMap.get("C")< totalAbcMap.get("C") && abcMap.get("B")< totalAbcMap.get("B")) {  // A装完BC未装完
                    speakTxt= String.format(speakTxtFormatStr3, cph,"A", total,"B","C");
                } else if(abcMap.get("C")== totalAbcMap.get("C") && abcMap.get("B")< totalAbcMap.get("B")) { // C完B未装完
                    speakTxt = String.format(speakTxtFormatStr2, cph, "C", total, "B");
                }else if(abcMap.get("B")== totalAbcMap.get("B") && abcMap.get("C")< totalAbcMap.get("C")) {// B完C未装完
                    speakTxt = String.format(speakTxtFormatStr2, cph, "B", total, "C");
                }else { // 全部装完
                    speakTxt= String.format(speakTxtFormatStr1, cph, total);
                }
            }else  if (totalAbcMap.get("A")>0 && totalAbcMap.get("C")>0 &&
                    (totalAbcMap.get("B")==0 || totalAbcMap.get("B") == abcMap.get("B")) ) { //AC有货
                if(totalAbcMap.get("B")>0 && abcMap.get("A")< totalAbcMap.get("A") && abcMap.get("C")< totalAbcMap.get("C")) {
                    speakTxt= String.format(speakTxtFormatStr3, cph,"B", total,"A","C");
                } else if(abcMap.get("A")== totalAbcMap.get("A") && abcMap.get("C")< totalAbcMap.get("C")) { // A装完C未装完
                    speakTxt = String.format(speakTxtFormatStr2, cph, "A", total, "C");
                }else if(abcMap.get("C")== totalAbcMap.get("C") && abcMap.get("A")< totalAbcMap.get("A")) {// C装完A未装完
                    speakTxt = String.format(speakTxtFormatStr2, cph, "C", total, "A");
                }else { // 全部装完
                    speakTxt= String.format(speakTxtFormatStr1, cph, total);
                }
            }else if(totalAbcMap.get("A")>0 && totalAbcMap.get("B")>0 && totalAbcMap.get("C")>0) { // 3区都有货物,且只有一个取装完了
                if(abcMap.get("A")>0 && abcMap.get("B")<totalAbcMap.get("B") && abcMap.get("C")<totalAbcMap.get("C")) {
                    speakTxt= String.format(speakTxtFormatStr3, cph,"A", total,"B","C");
                } else if(abcMap.get("B")>0 && abcMap.get("C")==0 && abcMap.get("A")==0) {
                    speakTxt= String.format(speakTxtFormatStr3, cph,"B", total,"A","C");
                } else if(abcMap.get("C")>0 && abcMap.get("A")==0 && abcMap.get("B")==0) {
                    speakTxt= String.format(speakTxtFormatStr3, cph,"C", total,"A","B");
                } else { // 全部装完
                    speakTxt= String.format(speakTxtFormatStr1, cph, total);
                }
            } else {
                speakTxt= String.format(speakTxtFormatStr1, cph, total);
            }
        }
        return speakTxt;
    }
 
    @Override
    public void sendToYrLed(String currPch) {
            // 状态 一磅后,待装;PDA扫码 在装; 二次过磅后消失
            // 装车区 两个汉字加两个英文或数字
            String strTitle="序号  车号   装车区  状态\r\n";
            String strContent = "";
            String contentFormatStr = "%s   %s  %s   %s\r\n";
            List<Map> list = tdmxper.yRZcList("08");
            int index = 1;
            for (Map map : list) {
                String numberplates = (String) map.get("numberplates"); // 车牌号
                String zcWz = (String) map.get("zcWz"); // 装车位置
                if(StringUtils.isNotBlank(zcWz)){
                    zcWz = zcWz.substring(0,4);
                } else { // 空是6个位置
                    zcWz = "      ";
                }
                String zcStatus = (String) map.get("zcStatus"); // 装车位置 在办/待办
                strContent += String.format(contentFormatStr, fillWithZero(index++), numberplates, zcWz, zcStatus);
            }
            Map paramMap = new HashMap();
            paramMap.put("strTitle",strTitle);
            paramMap.put("strContent",strContent);
            Wxhtmethod.post(Contants.yr_zcLedUrl + "/SendLED",paramMap);
            log.info("yr_zcLedUrl: " + Contants.yr_zcLedUrl + ";title:" + strTitle + ",content:" + strContent);
    }
 
    String fillWithZero(int str){
        String s;
        if(str<10 && str>=0) s= "0" + str;
        else s=String.valueOf(str);
        return s;
    }
 
    @Override
    @Transactional(rollbackFor=Exception.class)
    public ObjectResult<Boolean> delZCMX(String cardID, String yktywdh,JsonArray dataArr) throws Exception {
        ObjectResult<Boolean> orr=new ObjectResult<Boolean>();
        //根据卡号判断分单信息
        //循环批次号 如果没有分单 修改为分单和装车
        Fd_Bsheet fdb=this.fbper.getbybsNo(StringUtils.isNotBlank(yktywdh)?yktywdh:"");
        String vehicleId="";
        if(fdb==null){
            orr.setCode(1);
            orr.setErrmsg("一卡通业务单未找到,请确认单号是否正确!");
            return orr;
        }else{
            if(fdb.getStatus()!=Serviceenum.在办){
                orr.setCode(1);
                orr.setErrmsg("一卡通业务单不是在办状态,系统不做处理!");
                return orr;
            }
            if(StringUtils.isBlank(cardID)){
                orr.setCode(1);
                orr.setErrmsg("参数:一卡通卡号为空!");
                return orr;
            }
            if(dataArr==null){
                orr.setCode(1);
                orr.setErrmsg("参数:批次号为空!");
                return orr;
            }
 
            Map<String, Object> mp=this.yktper.getcardinuse(cardID);
            if(mp==null){
                throw new Exception("卡号不正确未找到在用卡!");
            }
            vehicleId=mp.get("numberplates")==null?"":mp.get("numberplates").toString();
            if(!vehicleId.equals(fdb.getVehicleId())){
                orr.setCode(1);
                orr.setErrmsg("参数:卡号关联的车牌号和业务单关联的车牌号不一致");
                return orr;
            }
            yktywdh=fdb.getBsNo();
        }
        //检查是否装车完成
        Map<String, Object> mmr=this.bsper.selectTaskTime(fdb.getBsNo());
        try {
            if(mmr!=null&&mmr.get("zcWc")!=null&&Boolean.valueOf(mmr.get("zcWc").toString())) {
                orr.setCode(1);
                orr.setErrmsg("联系成品库办公室取消装车完成!");
                return orr;
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
//        Fd_Bsheet nr=this.fbper.getbyVehicleId(vehicleId, Serviceenum.在办);
//        if(nr==null){
//            orr.setCode(1);
//            orr.setErrmsg("卡号:"+cardID+" 未找到在办销售发货单!");
//            return orr;
//        }
 
//        Fd_Vehicle fdv=this.vhper.getByVehicleId(vehicleId);
//        if(fdv==null){
//            orr.setCode(1);
//            orr.setErrmsg("车牌号:"+cardID+" 未录入车辆信息!");
//            return orr;
//        }
        //现场过完毛重后出现扫码装车的,必须重新过磅
        int grossNum=this.bsper.getgrosstDoc(vehicleId, fdb.getBsNo());
        if(grossNum>0) {
            Map<String, Object> xstm=new HashMap<String, Object>();
            xstm.put("canCm",false);
            xstm.put("businessNo",fdb.getBsNo());
            this.bsper.updateCanCm(xstm);
            this.bsper.updatecanFb(vehicleId, fdb.getBsNo());
        }
        for (JsonElement je : dataArr) {
            JsonObject jo=je.getAsJsonObject();
            String tdmx_pch=jo.get("tdmx_pch").getAsString();
//            Fd_XSTDMX tdmx=this.tdmxper.getbyPCH(tdmx_pch);
            Fd_XSTDMX tdmx=this.tdmxper.getbyPCHONE(tdmx_pch,vehicleId);
            if(tdmx == null) {
                String errMsg = "一卡通卸车失败,请查看【" + tdmx_pch +"】出库关闭标志!";
                throw  new Exception(errMsg);
            }
//            if(tdmx.getTdmx_zcbz()!=1){
////                throw new Exception("批次号:"+tdmx_pch+" 已经提交装车,不能重复提交装车!");
//                continue;
//            }
//            Fd_XSTD xstd=this.tdper.get(tdmx.getTdmx_tdls());
//            if(xstd==null){
//                continue;
//            }
            //修改Fd_Bsheetitem表  这张表去掉了
            this.fbper.delbsitem(tdmx.getTdmx_pch(), yktywdh);
//            //修改一卡通明细表bsitem
//            if(tdmx.getTdmx_zcbz()==0){
//                continue;
//            }
            tdmx.setTdmx_zcbz(0);
            tdmx.setTdmx_zccph(null);
            tdmx.setTdmx_zcdriver(null);
            tdmx.setTdmx_zcphone(null);
            tdmx.setTdmx_zcwlgsName(null);
            this.tdmxper.updateZCnoFD(tdmx);
        }
 
        orr.setCode(0);
        orr.setErrmsg("取消装车操作成功!");
        return orr;
    }
 
    @Override
    public ObjectResult<Map<String, Object>> getcInfo(String cardID) {
        ObjectResult<Map<String, Object>> orr=new ObjectResult<Map<String,Object>>();
        Map<String, Object> rem=new HashMap<String, Object>();
        Map<String, Object> mp=this.yktper.getcardinuse(cardID);
        if(mp==null){
            orr.setCode(1);
            orr.setErrmsg("卡号不正确未找到在用卡!");
            return orr;
        }
        String vehicleId=mp.get("numberplates")==null?"":mp.get("numberplates").toString();
 
        String type = (String)mp.get("type");
        String sdepartNo = null;
        if(type.equals("外来车辆卡")) {
            sdepartNo = (String) mp.get("sdepartNo");
        }
 
        List<Fd_Bsheet> fd_bList = this.fbper.getListbyVehicleId(vehicleId, Serviceenum.在办);
        if (fd_bList == null||fd_bList.isEmpty()) {
            orr.setCode(1);
            orr.setErrmsg("车辆未找到在办分单!");
            return orr;
        }
        Fd_Bsheet fd_b=null;
        if(fd_bList.size()>1) {
            for (Fd_Bsheet fd_Bsheet : fd_bList) {
                int wd=this.bsper.getweightDoc(vehicleId, fd_Bsheet.getBsNo());
                if(wd>0) {
                    fd_b=fd_Bsheet;
                    break;
                }
            }
            if(fd_b==null) {
                orr.setCode(1);
                orr.setErrmsg("车辆:"+vehicleId+" 当前业务未找到绑定的过磅单,请先过磅或者重绑磅单!");
                return orr;
            }
        }else {
            fd_b=fd_bList.get(0);
            int wcnum=this.bsper.getweightDoc(vehicleId, fd_b.getBsNo());
            if(wcnum<=0){
                orr.setCode(1);
                orr.setErrmsg("车辆:"+vehicleId+" 当前业务未找到绑定的过磅单,请先过磅或者重绑磅单!");
                return orr;
            }
        }
        // 改成使用在用卡的卡号、车牌号码、厂区来确认是否有销售的业务单 btid=3 businesstype00002
        String businessNo = bsper.getBusinessNoByCardNo(cardID,vehicleId,sdepartNo);
        if(StringUtils.isBlank(businessNo)) {
            orr.setCode(1);
            orr.setErrmsg("未找到该卡的在办业务单!");
            return orr;
        }
//        List<Vehicle_XSTD> vxl=this.vhxper.getlistbyVehicleId(vehicleId);
        List<Vehicle_XSTD> vxl= this.vhxper.getlistbyVehicleIdandbsNoGp(vehicleId, businessNo);
        List<Map<String, Object>> datamp=new ArrayList<Map<String, Object>>();
        Map<String,Fd_XSTD> xstdMap = new HashMap(); // 缓存Fd_XSTD,避免多查
        for (Vehicle_XSTD vx : vxl) {
            String tdbh=vx.getXstdNo();
            Fd_XSTD td=this.tdper.getbyTDBH(tdbh);
            String wlgsbh=vx.getCompanyNo();
            Fd_Company fd_c=this.cmper.getByCompanyNo(wlgsbh);
            Map<String, Object> tdm=new HashMap<String, Object>();
            if(fd_c!=null){
                tdm.put("td_wlgs", fd_c.getName());//物流公司
            }else{
                tdm.put("td_wlgs", "");//物流公司
            }
            if(td==null){
                continue;
            }
            // List<Fd_XSTDMX> tdmxl=this.tdmxper.getbytdls(td.getTd_tdls());
            List<Fd_XSTDMX> tdmxl= tdmxper.getListByTdlsAndCph(td.getTd_tdls(),vehicleId);
            if(tdmxl==null||tdmxl.size()<=0){
                continue;
            }
            Map<String, Object> mm=new HashMap<String, Object>();
            tdm.put("td_tdbh", td.getTd_tdbh());
            tdm.put("td_tdls", td.getTd_tdls());
            tdm.put("td_jylx", td.getTd_jylx());
            Fd_XSTD fd_xstd = xstdMap.get(td.getTd_tdbh());
            if(xstdMap.get(td.getTd_tdbh())==null) { // 保证同一个提单只查一遍
                fd_xstd = tdper.getbyttdbh(td.getTd_tdbh());
            }
            //增加客户名称 备注
            if(StringUtils.isBlank(fd_xstd.getTd_fpbh())){
                Nckh nckh = zsjper.getNckhbyCode(fd_xstd.getTd_khbh());// 客户不存在的特殊情况是不应该存在的,以防万一,加判空。
                tdm.put("td_khmc",nckh!=null?nckh.getName():"");
            }else {
                String name = ConvertTool.covertToCustoms(fd_xstd.getTd_ywdybh());
                tdm.put("td_khmc", name);
            }
            tdm.put("td_bz",fd_xstd.getTd_bz());
            mm.put("td", tdm);
            mm.put("tdmx", tdmxl);
            datamp.add(mm);
        }
 
 
        rem.put("cph", vehicleId);
        rem.put("sjmc", fd_b.getDriver());
        rem.put("dh", fd_b.getPhone());
//        rem.put("sjmc", fdv.getDriver());
//        rem.put("dh", fdv.getPhone());
        rem.put("ywdh", fd_b.getBsNo());
        rem.put("respcode", 111);
        rem.put("data", datamp);
 
        //修改装车记录
        Map<String, Object> mr=this.bsper.selectTaskTime(fd_b.getBsNo());
        if(mr!=null) {
            if(mr.get("firstzxhtime")!=null) {
                mr.put("lastzxhtime", new Date());
            }else {
                mr.put("firstzxhtime", new Date());
            }
            this.bsper.updateTaskTime(mr);
        }else {
            orr.setCode(1);
            orr.setErrmsg("未找到装车记录");
            return orr;
        }
 
        orr.setCode(0);
        orr.setItem(rem);
        return orr;
    }
 
    @Override
    public ObjectResult<Map<String, Object>> getxstdInfo(String xstdNo) {
        ObjectResult<Map<String, Object>> orr=new ObjectResult<Map<String,Object>>();
        Fd_XSTD xstd=this.tdper.getbyTDBH(xstdNo);
        if(xstd==null){
            orr.setCode(1);
            orr.setErrmsg("提单未找到!");
            return orr;
        }
        Map<String, Object> rm=new HashMap<String, Object>();
        List<Fd_XSTDMX> tdmxlist=this.tdmxper.getListbytdls(xstd.getTd_tdls());
        rm.put("code", 0);
        rm.put("item", xstd);
        rm.put("itemList", tdmxlist);
        orr.setCode(0);
        orr.setItem(rm);
        return orr;
    }
 
    @Override
    @Transactional(rollbackFor=Exception.class)
    public ObjectResult<Boolean> savewdControl(JsonArray dataArr)throws Exception {
        ObjectResult<Boolean> orr=new ObjectResult<Boolean>();
        if(dataArr==null){
            orr.setCode(0);
            orr.setErrmsg("添加过磅控制成功!");
            return orr;
        }
        for (JsonElement je : dataArr) {
            JsonObject jo=je.getAsJsonObject();
            String bsNo=jo.get("yktywdh").getAsString();
            BsheetEcgb be=this.beper.getbyNo(bsNo);
            if(be==null){
                be=new BsheetEcgb();
                be.setBsNo(bsNo);
                be.setCtime(new Date());
                be.setCname("生产系统添加");
                be.setSate(1);
                this.beper.insert(be);
            }
        }
        orr.setCode(0);
        orr.setErrmsg("添加过磅控制成功!");
        return orr;
    }
 
 
 
 
}