buli
2023-06-05 3962c2bb0435484b60a3e408e4738d792e249a53
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
public without sharing class LexRemoveBoxController {
    //经销商产品分类
    public static String agencyProType {get;set;}
    public static String userPro_Typestr = null;
    public static String barcode { get; set; }
    /*****************画面表示Bean******************/
    public static List<ConsumableorderdetailsInfo> consumableInventory { get; set; }
    //错误明细
    public static List<ConsumableorderdetailsInfo> ConsumableorderdetailsRecordserror { get; set; }
    //拆盒明细
    public static List<ConsumableorderdetailsInfo> ConsumableorderdetailsRecordsdummy { get; set; }
    public static boolean saveFLGbln {get;set;}
    //错误信息
    public static String alertMessage {set;get;}
    //拆盒明细件数
    public static integer getinventorysize(){
        return ConsumableorderdetailsRecordsdummy.size();
    }
    public static Map<String, String> ExistIdMap = new Map<String, String>();
    public static Map<String, String> ErrorIdMap = new Map<String, String>();
    public static Map<String, String> AllMap = new Map<String, String>();
    public static List<String> BarCodeListP = new List<String>();
    //不符合的barcode和型号
    public static list<String> notInlist = new list<String>();
    public static String baseUrl {get;private set;}
    private static String accountid = null;
    private static String accountName = null;
    //public String[] proidList =new String[]{};
    // 登录者工作地
    private static String userWorkLocation;
 
    public LexRemoveBoxController(){
        baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        ConsumableorderdetailsRecordserror = new List<ConsumableorderdetailsInfo>();
        ConsumableorderdetailsRecordsdummy = new List<ConsumableorderdetailsInfo>();
        consumableInventory = new List<ConsumableorderdetailsInfo>();
        saveFLGbln = FALSE;
    }
 
    @AuraEnabled
    public static ResponseBodyLWC init() {
        ResponseBodyLWC res = new ResponseBodyLWC();
        Map<String,object> data = new Map<String,object>();
        res.entity = data;
 
 
        String userId = UserInfo.getUserId();
        List<user> Useracc = New List<user>();
        Useracc = [SELECT accountid, Work_Location__c,UserPro_Type__c FROM user WHERE id =:userId];
        accountid = Useracc[0].accountid;
        userWorkLocation = Useracc[0].Work_Location__c;
        agencyProType = Useracc[0].UserPro_Type__c;
        if(String.isBlank(Useracc[0].UserPro_Type__c)){
            agencyProType = 'ET';
        }
        userPro_Typestr = '%' + agencyProType + '%';
        List<account> accountInfo = [SELECT Name FROM account WHERE id =:accountid];
        //String view_product = accountInfo[0].view_product__c;
        accountName = accountInfo[0].Name;
        //proidList = view_product.split(',');
 
        data.put('ConsumableorderdetailsRecordsdummy',JSON.serialize(ConsumableorderdetailsRecordsdummy));
        data.put('accountName',accountName);
        data.put('userWorkLocation',userWorkLocation);
        data.put('agencyProType',agencyProType);
        res.status = 'Success';
        res.code = 200;
        System.debug('res = ' + res);
        return res;
    }
 
    // 将页面或取得BarCode去掉重复的,转换成List
    public static List<String> ParseBarCode(String Code){
        String[] Cache = new String[]{};
        Cache = Code.split('\n');
        Set <String> Buff = new Set<String>();
        for(String A : Cache){
            Buff.add(A);
        }
        List<String> outPut = new List<String>();
        for(String B :Buff){
            B = B.trim();
            outPut.add(B);
        }
        return outPut;
    }
 
    //获取明细
    @AuraEnabled
    public static ResponseBodyLWC searchConsumableorderdetails(String barcode,String accountName,String userWorkLocation,String agencyProType) {
 
        ResponseBodyLWC res = new ResponseBodyLWC();
        Map<String,object> data = new Map<String,object>();
        res.entity = data;
        userPro_Typestr = '%' + agencyProType + '%';
        barcode = barcode;
        accountName = accountName;
        userWorkLocation = userWorkLocation;
        ConsumableorderdetailsRecordserror = new List<ConsumableorderdetailsInfo>();
        ConsumableorderdetailsRecordsdummy = new List<ConsumableorderdetailsInfo>();
 
 
 
        notInlist = new list<String>();
        BarCodeListP = ParseBarCode(barcode);
        Consumable_order_details2__c p = new Consumable_order_details2__c();
        List<Consumable_order_details2__c> Ins = New List<Consumable_order_details2__c>();
        List<Consumable_order_details2__c> reSet = new List<Consumable_order_details2__c>();
        List<Consumable_order_details2__c> reSet1 = new List<Consumable_order_details2__c>();
 
        saveFLGbln = false;
        alertMessage='';
        
        //判断barcode是否为空
        if(barcode == null || barcode ==''){
            return new ResponseBodyLWC('Error',500, '请输入BarCode号', '');
            // ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'请输入BarCode号。'));
            // return;
        }
 
        // ExistIdMap.clear();
        // ErrorIdMap.clear();
        // notInlist.clear();
        // ConsumableorderdetailsRecordserror.clear();
        // ConsumableorderdetailsRecordsdummy.clear();
 
        // 产品类型不同
        reSet1 = [SELECT Id,  Name,Consumable_Product__r.Name__c,
                        Consumable_Product__c,Consumable_Product__r.Name, Sterilization_limit__c,
                        Deliver_date__c,Bar_Code__c,Product_Type__c,
                        Arrive_date__c,Send_Date__c,Consumable_order_minor__r.Name,Consumable_order_minor__c,
                        recordtypeid,Consumable_order_minor__r.Arrive_Order__c, Arrive_Owner_Work_Location__c
                    FROM Consumable_order_details2__c
                    WHERE Dealer_Arrive__c = true
                    AND   Bar_Code__c in :BarCodeListP
                    AND   Dealer_Info_text__c = :accountName
                    AND   Arrive_Owner_Work_Location__c = : userWorkLocation
                    AND (not Product_Type__c like : userPro_Typestr)
                    ORDER BY Name ];
        if(reSet1.size() > 0){
            for (Integer i = 0; i < reSet1.size(); i++) {
                if(ErrorIdMap.containsKey(reSet1[i].Bar_Code__c)){
                    // 跳过已经处理的消耗品明细
                    continue;
                }else{
                    String str = '该商品类型'+ reSet1[i].Product_Type__c +'与用户类型'+ agencyProType +'不符,不能拆盒';
                    ConsumableorderdetailsRecordserror.add(new ConsumableorderdetailsInfo(reSet1[i],str));
                    ErrorIdMap.put(reSet1[i].Bar_Code__c,reSet1[i].Bar_Code__c);
                }
            }
        }
        //未到货
        reSet1 = [SELECT Id,  Name,Consumable_Product__r.Name__c,
                        Consumable_Product__c,Consumable_Product__r.Name, Sterilization_limit__c,
                        Deliver_date__c,Bar_Code__c,
                        Arrive_date__c,Send_Date__c,Consumable_order_minor__r.Name,Consumable_order_minor__c,
                        recordtypeid,Consumable_order_minor__r.Arrive_Order__c
                    FROM Consumable_order_details2__c
                    WHERE Dealer_Arrive__c = FALSE
                    AND  Dealer_Shipment__c= FALSE
                    AND  Dealer_Saled__c = FALSE
                    AND  Dealer_Returned__c = false
                    AND  Lose_Flag__c = false
                    AND  Box_Piece__c = '盒'
                    AND  Bar_Code__c in :BarCodeListP
                    AND  Dealer_Info_text__c = :accountName
                    ORDER BY Name ];  //已发货,未到货
        if(reSet1.size() > 0){
            for (Integer i = 0; i < reSet1.size(); i++) {
                if(ErrorIdMap.containsKey(reSet1[i].Bar_Code__c)){
                    // 跳过已经处理的消耗品明细
                    continue;
                }else{
                    String str = '该商品未到货,不能拆盒';
                    ConsumableorderdetailsRecordserror.add(new ConsumableorderdetailsInfo(reSet1[i],str));
                    ErrorIdMap.put(reSet1[i].Bar_Code__c,reSet1[i].Bar_Code__c);
                }
            }
        }
 
        //已规格=1
        reSet1 = [SELECT Id,  Name,Consumable_Product__r.Name__c,
                        Consumable_Product__c,Consumable_Product__r.Name, Sterilization_limit__c,
                        Deliver_date__c,Bar_Code__c,
                        Arrive_date__c,Send_Date__c,Consumable_order_minor__r.Name,Consumable_order_minor__c,
                        recordtypeid,Consumable_order_minor__r.Arrive_Order__c
                    FROM Consumable_order_details2__c
                    WHERE Dealer_Arrive__c = true
                    AND  Dealer_Shipment__c= FALSE
                    AND  Dealer_Saled__c = FALSE
                    AND  Dealer_Returned__c = false
                    AND  Lose_Flag__c = false
                    AND  Box_Piece__c = '盒'
                    AND  ProductPacking_list_manual__c = 1
                    AND  Bar_Code__c in :BarCodeListP
                    AND  Dealer_Info_text__c = :accountName
                    ORDER BY Name ];  //没出库,没销售,就是在库,没拆盒,规格=1
        if(reSet1.size() > 0){
            for (Integer i = 0; i < reSet1.size(); i++) {
                if(ErrorIdMap.containsKey(reSet1[i].Bar_Code__c)){
                    // 跳过已经处理的消耗品明细
                    continue;
                }else{
                    String str = '该商品规格为1,不能拆盒';
                    ConsumableorderdetailsRecordserror.add(new ConsumableorderdetailsInfo(reSet1[i],str));
                    ErrorIdMap.put(reSet1[i].Bar_Code__c,reSet1[i].Bar_Code__c);
                }
            }
        }
        //已出货
        reSet1 = [SELECT Id,  Name,Consumable_Product__r.Name__c,
                        Consumable_Product__c,Consumable_Product__r.Name, Sterilization_limit__c,
                        Deliver_date__c,Bar_Code__c,
                        Arrive_date__c,Send_Date__c,Consumable_order_minor__r.Name,Consumable_order_minor__c,
                        recordtypeid,Consumable_order_minor__r.Arrive_Order__c
                    FROM Consumable_order_details2__c
                    WHERE Dealer_Shipment__c = true
                    AND   Dealer_Arrive__c = true
                    AND   Dealer_Returned__c = false
                    AND   Lose_Flag__c = false
                    AND   Bar_Code__c in :BarCodeListP
                    AND   Dealer_Info_text__c = :accountName
                    ORDER BY Name ];
        if(reSet1.size() > 0){
            for (Integer i = 0; i < reSet1.size(); i++) {
                if(ErrorIdMap.containsKey(reSet1[i].Bar_Code__c)){
                    // 跳过已经处理的消耗品明细
                    continue;
                }else{
                    String str = '该商品已出货';
                    ConsumableorderdetailsRecordserror.add(new ConsumableorderdetailsInfo(reSet1[i],str));
                    ErrorIdMap.put(reSet1[i].Bar_Code__c,reSet1[i].Bar_Code__c);
                }
            }
        }
 
        //已销售
        reSet1 = [SELECT Id,  Name,Consumable_Product__r.Name__c,
                        Consumable_Product__c,Consumable_Product__r.Name, Sterilization_limit__c,
                        Deliver_date__c,Bar_Code__c,
                        Arrive_date__c,Send_Date__c,Consumable_order_minor__r.Name,Consumable_order_minor__c,
                        recordtypeid,Consumable_order_minor__r.Arrive_Order__c
                    FROM Consumable_order_details2__c
                    WHERE Dealer_Saled__c = true
                    AND   Dealer_Returned__c = false
                    AND   Dealer_Arrive__c = true
                    AND   Bar_Code__c in :BarCodeListP
                    AND   Dealer_Info_text__c = :accountName
                    ORDER BY Name ];
        if(reSet1.size() > 0){
            for (Integer i = 0; i < reSet1.size(); i++) {
                if(ErrorIdMap.containsKey(reSet1[i].Bar_Code__c)){
                    // 跳过已经处理的消耗品明细
                    continue;
                }else{
                    String str = '该商品已销售';
                    ConsumableorderdetailsRecordserror.add(new ConsumableorderdetailsInfo(reSet1[i],str));
                    ErrorIdMap.put(reSet1[i].Bar_Code__c,reSet1[i].Bar_Code__c);
                }
            }
        }
 
        //已拆盒
        reSet1 = [SELECT Id,  Name,Consumable_Product__r.Name__c,
                        Consumable_Product__c,Consumable_Product__r.Name, Sterilization_limit__c,
                        Deliver_date__c,Bar_Code__c,
                        Arrive_date__c,Send_Date__c,Consumable_order_minor__r.Name,Consumable_order_minor__c,
                        recordtypeid,Consumable_order_minor__r.Arrive_Order__c
                    FROM Consumable_order_details2__c
                    WHERE Dealer_Arrive__c = true
                    AND  Dealer_Shipment__c= false
                    AND  Dealer_Saled__c = false
                    AND  Dealer_Returned__c = false
                    AND  Box_Piece__c = '个'
                    AND  ProductPacking_list_manual__c > 1
                    AND  Bar_Code__c in :BarCodeListP
                    AND  Dealer_Info_text__c = :accountName
                    ORDER BY Name ];  //没出库,没销售,就是在库,已拆盒
        if(reSet1.size() > 0){
            for (Integer i = 0; i < reSet1.size(); i++) {
                if(ErrorIdMap.containsKey(reSet1[i].Bar_Code__c)){
                    // 跳过已经处理的消耗品明细
                    continue;
                }else{
                    String str = '该商品已拆盒';
                    ConsumableorderdetailsRecordserror.add(new ConsumableorderdetailsInfo(reSet1[i],str));
                    ErrorIdMap.put(reSet1[i].Bar_Code__c,reSet1[i].Bar_Code__c);
                }
            }
        }
 
        // 已经取消
        reSet1 = [SELECT Id,  Name,Consumable_Product__r.Name__c,
                        Consumable_Product__c,Consumable_Product__r.Name, Sterilization_limit__c,
                        Deliver_date__c,Bar_Code__c,
                        Arrive_date__c,Send_Date__c,Consumable_order_minor__r.Name,Consumable_order_minor__c,
                        recordtypeid,Consumable_order_minor__r.Arrive_Order__c, Arrive_Owner_Work_Location__c
                    FROM Consumable_order_details2__c
                    WHERE Dealer_Arrive__c = true
                    AND   Cancellation_Flag__c = true
                    AND   Bar_Code__c in :BarCodeListP
                    AND   Dealer_Info_text__c = :accountName
                    AND   Arrive_Owner_Work_Location__c = : userWorkLocation
                    ORDER BY Name ];
        if(reSet1.size() > 0){
            for (Integer i = 0; i < reSet1.size(); i++) {
                if(ErrorIdMap.containsKey(reSet1[i].Bar_Code__c)){
                    // 跳过已经处理的消耗品明细
                    continue;
                }else{
                    String str = '该商品已经取消,不能拆盒';
                    ConsumableorderdetailsRecordserror.add(new ConsumableorderdetailsInfo(reSet1[i],str));
                    ErrorIdMap.put(reSet1[i].Bar_Code__c,reSet1[i].Bar_Code__c);
                }
            }
        }
 
        // 工作地不同
        reSet1 = [SELECT Id,  Name,Consumable_Product__r.Name__c,
                        Consumable_Product__c,Consumable_Product__r.Name, Sterilization_limit__c,
                        Deliver_date__c,Bar_Code__c,
                        Arrive_date__c,Send_Date__c,Consumable_order_minor__r.Name,Consumable_order_minor__c,
                        recordtypeid,Consumable_order_minor__r.Arrive_Order__c, Arrive_Owner_Work_Location__c
                    FROM Consumable_order_details2__c
                    WHERE Dealer_Arrive__c = true
                    // gzw 无法拆盒,去掉销售明细 20210802
                    AND  Dealer_Shipment__c= false
                    AND  Dealer_Saled__c = false
                    AND  Dealer_Returned__c = false
                    // gzw 无法拆盒,去掉销售明细 20210802
                    AND   Bar_Code__c in :BarCodeListP
                    AND   Dealer_Info_text__c = :accountName
                    AND   Arrive_Owner_Work_Location__c != : userWorkLocation
                    ORDER BY Name ];
        if(reSet1.size() > 0){
            for (Integer i = 0; i < reSet1.size(); i++) {
                if(ErrorIdMap.containsKey(reSet1[i].Bar_Code__c)){
                    // 跳过已经处理的消耗品明细
                    continue;
                }else{
                    String str = '该商品属于' + reSet1[i].Arrive_Owner_Work_Location__c + ',不能拆盒';
                    ConsumableorderdetailsRecordserror.add(new ConsumableorderdetailsInfo(reSet1[i],str));
                    ErrorIdMap.put(reSet1[i].Bar_Code__c,reSet1[i].Bar_Code__c);
                }
            }
        }
 
        
 
        reSet = [SELECT Id,
                        Name,
                        Intra_Trade_List_RMB__c,
                        Asset_Model_No__c,
                        Consumable_Product__c,
                        Consumable_Product__r.Name,
                        Consumable_Product__r.Name__c,
                        Sterilization_limit__c,
                        Deliver_date__c,
                        Bar_Code__c,
                        Arrive_date__c,
                        Send_Date__c,
                        Consumable_order_minor__r.Name,
                        Consumable_order_minor__c,
                        Dealer_Arrive__c,
                        Guarantee_period_for_products__c,
                        CFDA_Status__c,
                        ProductPacking_list_manual__c,
                        Report_Product_Approbation__c,
                        Report_Product_Expiration__c,
                        Agencyinfo_fromSAP__c,
                        Frist_Storage_Agency__c,
                        Dealer_Info_text__c
                    FROM Consumable_order_details2__c
                    WHERE Dealer_Arrive__c = true
                    AND Dealer_Shipment__c = false
                    AND Dealer_Saled__c = false
                    AND Dealer_Returned__c = false
                    AND Lose_Flag__c = false
                    AND Bar_Code__c in :BarCodeListP
                    AND Dealer_Info_text__c = :accountName
                    AND Arrive_Owner_Work_Location__c = : userWorkLocation
                    AND Product_Type__c like : userPro_Typestr
                ];
        if(reSet.size()>0){
            for (Integer i = 0; i < reSet.size(); i++) {
                if(ErrorIdMap.containsKey(reSet[i].Bar_Code__c) ){
                    // 跳过错误的消耗品明细
                    continue;
                }
                else if(ExistIdMap.containsKey(reSet[i].Bar_Code__c) ){
                    // 跳过已经处理的消耗品明细
                    continue;
                }
                else{
                    ConsumableorderdetailsRecordsdummy.add(new ConsumableorderdetailsInfo(reSet[i]));
                    ExistIdMap.put(reSet[i].Bar_Code__c,reSet[i].Bar_Code__c);
                }
            }
        }
 
        AllMap.putAll(ExistIdMap);
        //得到不存在的BarCode
        for(Integer i = 0; i < BarCodeListP.size(); i++){
            if(ExistIdMap.containsKey(BarCodeListP[i])){
                continue;
            }else if(ErrorIdMap.containsKey(BarCodeListP[i])){
                continue;
            }else{
                notInlist.add(BarCodeListP[i]);
            }
        }
        if(notInlist.size()>0){
                // ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'BarCode'+ notInlist + '不存在。'));
                return new ResponseBodyLWC('Error',500, 'BarCode'+ notInlist + '不存在', '');
        }
 
 
        data.put('ConsumableorderdetailsRecordsdummy',JSON.serialize(ConsumableorderdetailsRecordsdummy));
        data.put('ConsumableorderdetailsRecordserror',JSON.serialize(ConsumableorderdetailsRecordserror));
        data.put('dataSize',ConsumableorderdetailsRecordsdummy.size());
        res.status = 'Success';
        res.code = 200;
        System.debug('res = ' + res);
        return res;
 
    }
 
    //拆盒确认
    @AuraEnabled
    public static ResponseBodyLWC removeBoxConfirm(String saveConsumableorderdetailsRecordsdummy,String accountName,String userWorkLocation,String agencyProType) {
        ResponseBodyLWC res = new ResponseBodyLWC();
        Map<String,object> data = new Map<String,object>();
        res.entity = data;
 
        userPro_Typestr = '%' + agencyProType + '%';
        accountName = accountName;
        userWorkLocation =userWorkLocation;
        List<ConsumableorderdetailsInfo> ConsumableorderdetailsRecordsdummy = (List<ConsumableorderdetailsInfo>)JSON.deserialize(saveConsumableorderdetailsRecordsdummy,List<ConsumableorderdetailsInfo>.class);
      
 
 
 
        List<Consumable_order_details2__c> ins = New List<Consumable_order_details2__c>();
 
        Savepoint sp = Database.setSavepoint();
        if(ConsumableorderdetailsRecordsdummy.size()<1){
            return new ResponseBodyLWC('Error',500, '不存在拆盒明细', '');
            // ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'不存在拆盒明细'));
            // return null;
        }
 
        List<String> BarCodeList = new List<String>();
        for (ConsumableorderdetailsInfo tmp : ConsumableorderdetailsRecordsdummy)  {
            BarCodeList.add(tmp.esd.Bar_Code__c);
        }
 
        try{
            //取得在库信息
            List<Consumable_order_details2__c> orderDetails2 =  [SELECT 
                        Id,
                        Name,
                        Bar_Code__c,
                        TracingCode__c,
                        Intra_Trade_List_RMB__c,
                        Purchase_Unitprice_From__c,
                        Guarantee_period_for_products__c,
                        Used_account__c,
                        Consumable_ZS_order__c,
                        Cancellation_Date__c,
                        Consumable_product__c,
                        Consumable_order_minor__c,
                        Direct_Arrive_Product__c,
                        Arrive_date__c,
                        Consumable_Arrived_order__c,
                        Unitprice_To_agency__c,
                        SerialLotNo__c,
                        Sterilization_limit__c,
                        Invoice_Date__c,
                        Lose_reason__c,
                        Asset_Model_No__c,
                        Deliver_date__c,
                        Inventory_date__c,
                        Consumable_Inventory_order__c,
                        Dealer_Arrive__c,
                        ProductPacking_list_manual__c,
                        Agencyinfo_fromSAP__c,
                        Frist_Storage_Agency__c,
                        Dealer_Info_text__c,
                        //add by rentx 2020-10-13 start
                        ContractNo_text__c
                        /*,ContractNo__c*/
                        //add by rentx 2020-10-13 end
                         // tcm start
                        ,Consumable_order_minor__r.ContractNo__c
                        // tcm end
                    FROM Consumable_order_details2__c
                    WHERE Dealer_Arrive__c = true
                    AND Dealer_Shipment__c = false
                    AND Dealer_Saled__c = false
                    AND Dealer_Returned__c = false
                    AND Lose_Flag__c = false
                    AND Box_Piece__c = '盒'
                    AND ProductPacking_list_manual__c > 1
                    AND Bar_Code__c in :BarCodeList
                    AND Dealer_Info_text__c = :accountName 
                    AND Arrive_Owner_Work_Location__c =: userWorkLocation
                    AND Product_Type__c like : userPro_Typestr
                ];
 
            List<Consumable_order_details2__c> orderDetails2del = new List<Consumable_order_details2__c>();
            List<Consumable_order_details2__c> orderDetails2ins = new List<Consumable_order_details2__c>();
            for (Consumable_order_details2__c detail2tmp : orderDetails2) {
                Decimal num                              = detail2tmp.ProductPacking_list_manual__c;
                Consumable_order_details2__c delDetails2 = new Consumable_order_details2__c();
                delDetails2.Id                           = detail2tmp.Id;
 
                //删除拆盒前在库
                //delete delDetails2;
                orderDetails2del.add(delDetails2);
 
                for(integer i=0; i < num; i++){
                    Consumable_order_details2__c insertDetails2 = new Consumable_order_details2__c();
                    insertDetails2 = detail2tmp.clone();
 
                    insertDetails2.Id                = null;
                    //单位(盒/个)
                    insertDetails2.Box_Piece__c      = '个';
                    //拆盒番号
                    insertDetails2.RemoveBox_No__c   = i+1;
                    //拆盒日期
                    insertDetails2.RemoveBox_date__c =  Date.today();
                    //add by rentx 2020-10-13 start 拆盒/取消拆盒时,记录SAP合同号
                    // insertDetails2.ContractNo_text__c= detail2tmp.ContractNo__c;
                    // tcm start
                    if (String.isBlank(detail2tmp.Consumable_order_minor__r.ContractNo__c)) {
                        insertDetails2.ContractNo_text__c= detail2tmp.ContractNo_text__c;
                    }else {
                        insertDetails2.ContractNo_text__c= detail2tmp.Consumable_order_minor__r.ContractNo__c;
                    }
                    // tcm end
                    //登录拆盒后在库
                    //insert insertDetails2;
                    orderDetails2ins.add(insertDetails2);
                }
            }
 
            if (orderDetails2del.size()>0){
                delete orderDetails2del;
            }
 
            if(orderDetails2ins.size()>0){
                insert orderDetails2ins;
 
            }
 
            saveFLGbln = true;
            alertMessage = '拆盒完成';
            data.put('saveFLGbln',saveFLGbln);
            res.status = 'Success';
            res.code = 200;
            res.msg = alertMessage;
            System.debug('res = ' + res);
            return res;
            // return null;
        }catch(Exception e){
            Database.rollback(sp);
            ApexPages.addMessages(e);
            return null;
        }
    }
 
 
    class ConsumableorderdetailsInfo implements Comparable {
        @AuraEnabled
        public Consumable_order_details2__c esd { get; set; }
        @AuraEnabled
        public Product2__c Prod { get; set; }
        //public String ProductName { get; set; }
        //public Boolean canSELECT { get; set; }
        //public String barCodeNo { get; set; }
        //public Date sterilizationlimitDate { get; set; }
        //public String serialNoorLotNo { get; set; }
        //public String tracingCodeNo { get; set; }
        @AuraEnabled
        public String oldConsumableCount { get; set; }
        @AuraEnabled
        public String ErrorReason { get; set; }
        public ConsumableorderdetailsInfo(Consumable_order_details2__c e,string str) {
            esd                = e;
            Prod               = e.Consumable_Product__r;
            oldConsumableCount = e.name;
            ErrorReason        = str;
        }
        public ConsumableorderdetailsInfo(Consumable_order_details2__c e) {
            esd                = e;
            Prod               = e.Consumable_Product__r;
            oldConsumableCount = e.name;
        }
 /*
        public ConsumableorderdetailsInfo(Product2__c e,String barCode,Date sterilizationlimit,String serialNoorLot,String tracingCode) {
            esd = new Consumable_order_details2__c();
            ProductName = e.Name__c;
            Prod = e;
            barCodeNo = barCode;
            sterilizationlimitDate = sterilizationlimit;
            serialNoorLotNo = serialNoorLot;
            tracingCodeNo = tracingCode;
        }
 
        public ConsumableorderdetailsInfo(Consumable_order_details2__c e,String barCode,Date sterilizationlimit,String serialNoorLot,String tracingCode) {
            //esd = new Consumable_order_details2__c();
            Prod               = e.Consumable_Product__r;
            //ProductName = productName;
            barCodeNo              = barCode;
            sterilizationlimitDate = sterilizationlimit;
            serialNoorLotNo        = serialNoorLot;
            tracingCodeNo          = tracingCode;
        }
*/
        // 排序
        public Integer compareTo(Object compareTo) {
            return null;
        }
    }
}