twysparks
2023-05-06 0994d62436004bd83059c51a48b7cd430feb9f43
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
public with sharing class lexPCLLostReportLwcController {
    @AuraEnabled
    public static Product2 pro2 { get; set; }
    @AuraEnabled
    public static string oppId { get; set; }
    @AuraEnabled
    public static string lostReportId { get; set; }
    // 当前页面状态 Create,View and Edit, MarketEdit
    @AuraEnabled
    public static string pageStatus { get; set; }
    // 失单,部分失单
    @AuraEnabled
    public static string lostType {get; set;}
    public  static Set<ID> deleteBrandIDSet = new Set<ID>();
    @AuraEnabled
    public static string submitFlag {get; set;}
    @AuraEnabled
    public static LostReport LostReport {get; set;}
    @AuraEnabled
    public static integer brandNo {get; set;}
    @AuraEnabled
    public static integer RemoveBrandNo {get; set;}
    @AuraEnabled
    public static integer setBrandNo {get; set;}
    @AuraEnabled
    public static integer brandCount {get; set;}
    // add tcm 20211122 start
    @AuraEnabled
    public static integer topNum {get; set;}
    @AuraEnabled
    public static integer secondNum {get; set;}
    // add tcm 20211122 end
    // 初始化
    @AuraEnabled
    public static void initForApex(string oppId1,string lostReportId1,string pageStatus1,string lostType1,string submitFlag1){
        oppId = oppId1;
        lostReportId = lostReportId;
        pageStatus = pageStatus1;
        lostType = lostType1;
        submitFlag = submitFlag1;
    }
    @AuraEnabled
    public static Map<string,object> init(){
        Map<string,object> maps = new Map<string,object>();
        if(string.isblank(pageStatus)) {
            // 这里需要报错没有传参数
            maps.put('message', '页面出错,请刷新后重试!');
            return maps;
        }
        else if(pageStatus.equals('Create')) {
            if(string.isBlank(oppId)) {
                // 这里报错创建时候必须有询价
                maps.put('message', '创建失单报告时,请重询价页面创建');
                return maps;
            }
            list<opportunity> opplist =
            [select id,Name,
             Sales_assistant_ID__c,
             Sales_manager_departmentID__c,Sales_owner_buchangID__c
             from opportunity
             where id =: oppId
                        limit 1];
            if( opplist.size() == 0) {
                pageStatus = null;
                maps.put('message', '您没有询价的查看权限,无法创建失单报告!');
                return maps;
            }
            Schema.DescribeSObjectResult s = Lost_cancel_report__c.sObjectType.getDescribe();
            if( !s.isCreateable()) {
                maps.put('message', '您不能创建失单/部分失单报告!');
                return maps;
            }
            Lost_cancel_report__c tempLostReport =
                new Lost_cancel_report__c(LostType__c= lostType,
                                          Opportunity__c = oppId,
                                          Sales_assistant__c = opplist[0].Sales_assistant_ID__c,
                                          Manager_sales__c = opplist[0].Sales_manager_departmentID__c,
                                          Buchang_sales__c = opplist[0].Sales_owner_buchangID__c,
                                          Submit_Day__c = date.today(),
                                          Report_Status__c = '草案');
            LostReport = new LostReport(tempLostReport);
            brandCount = 1;
        }
        else if(pageStatus.equals('View') ||  pageStatus.equals('Edit')) {
            system.debug('edit1');
            if(string.isBlank(lostReportId)) {
                pageStatus = null;
                maps.put('message', '您没有查看这个失单/部分失单报告的权限!');
                return maps;
            }
            system.debug('edit2');
            list<Lost_cancel_report__c> lostReportList =
            [select id,of_lost_system_processor__c,
             Lost_reason_main__c,
             Lost_Reason_Sub__c,Lost_By_Company__c,
             LostTotalAmount__c,LostType__c,
             Report_Status__c,Sales_assistant__c,
             Opportunity__c, Opportunity__r.Sales_assistant_ID__c,
             Opportunity__r.Sales_manager_departmentID__c,
             Manager_sales__c,recordtypeid,recordtype.DeveloperName
             from Lost_cancel_report__c
             where id =: lostReportId limit 1];
 
            if(lostReportList.size() == 0) {
                pageStatus = null;
                maps.put('message', '您没有查看这个失单/部分失单报告的权限!');
                return maps;
            }
            lostReportList[0].Sales_assistant__c =
                lostReportList[0].Opportunity__r.Sales_assistant_ID__c;
            lostReportList[0].Manager_sales__c =
                lostReportList[0].Opportunity__r.Sales_manager_departmentID__c;
            if(lostReportList[0].recordtype.DeveloperName.equals('PCL_Lost_report')) {
                lostReportList[0].LostType__c ='失单';
 
            }else if(lostReportList[0].recordtype.DeveloperName.equals('PCL_PartLost_report')) {
                lostReportList[0].LostType__c ='部分失单';
            }
            // 权限,如果当前状态不是草案中,那么需要跳转到只读页面
            if(!'草案'.equals(lostReportList[0].Report_Status__c) && userinfo.getProfileId() != (ID) '00e10000000Y3o5AAC') {
                pageStatus = 'View';
            }
            oppId = lostReportList[0].Opportunity__c;
            system.debug('edit3');
            list<PCLLostBrand__c> selectlostBrandList =
            [
                select id, LostPrice__c, Lost_By_Company__c,
                Lost_reason_main__c,Lost_Reason_Sub__c,Name,Agency__c, AgencyMannual__c,
                // ProductCategory__c,ProductClass__c,
                Lost_cancel_report__c,Lost_By_Company_Mannual__c,
                ( select id,PCLLostBrand__c,LostProduct__c,
                  LostProductMannual__c,            //SWAG-C6P9PX lt 20210913 add
                  ProductCategory__c,ProductClass__c, //add tcm 20211123
                  Quantity__c,Name, LostBrandName__c 
                  from PCLLostBrand__c.PCLLostBrandProduct__r)
                from PCLLostBrand__c
                where Lost_cancel_report__c =: lostReportId
            ];
            if(selectlostBrandList.size() == 0 ) {
                LostReport = new LostReport(lostReportList[0]);
                brandCount = 1;
            }else{
                system.debug('edit4:'+selectlostBrandList[0]);
                brandCount = 0;
                list<LostBrand> LostBrandList = new list<LostBrand>();
                for(PCLLostBrand__c templostBrand : selectlostBrandList) {
                    list<PCLLostProducts> tempProductList = new list<PCLLostProducts>();
                    integer productCount = 0;
                    system.debug('check products:'+ (templostBrand.PCLLostBrandProduct__r != null) + '|' + (templostBrand.PCLLostBrandProduct__r.size() > 0));
                    if (templostBrand.PCLLostBrandProduct__r != null && templostBrand.PCLLostBrandProduct__r.size() > 0) {
                        for( PCLLostProduct__c tempProduct: templostBrand.PCLLostBrandProduct__r) {
 
                            tempProductList.add(new PCLLostProducts(productCount,tempProduct));
                            productCount++;
                        }
                    } else {
                        PCLLostProducts newProduct = new PCLLostProducts();
                        if ('其他'.equals(templostBrand.Lost_By_Company__c) && 
                            templostBrand.Lost_By_Company_Mannual__c != null && 
                            !''.equals(templostBrand.Lost_By_Company_Mannual__c)) {
                                newProduct.LostProductss.LostBrandName__c = templostBrand.Lost_By_Company_Mannual__c;
                            } else {
                                newProduct.LostProductss.LostBrandName__c = templostBrand.Lost_By_Company__c;
                            }
                            system.debug('aaa4+++'+templostBrand.Lost_By_Company_Mannual__c);
                        tempProductList.add(newProduct);
                        productCount++;
                    }
                    LostBrandList.add( new LostBrand(brandCount,tempProductList, templostBrand));
                    brandCount++;
                }
 
                LostReport = new LostReport(lostReportList[0],LostBrandList);
                system.debug('LostReport.LostBrands:'+LostReport.LostBrands);
            }
        }
        brandNo = 0;
        // 读取竞争对手品牌
        // BrandmapSet();
        if(string.isNotEmpty(submitFlag)) {
            maps.put('message', '提交成功!');
            maps.put('LostReport', LostReport);
            return maps;
 
        }
        return null;
    }
    // 编辑 只有系统管理员或者草案中可以编辑
    @AuraEnabled
    public static String edit2(){
        if( userinfo.getProfileId() == (ID) '00e10000000Y3o5AAC' ||
            '草案'.equals(LostReport.LostReport.Report_Status__c)
            ) {
            pageStatus = 'Edit';
            init();
        }
        else{
            return '只有在草案中才能进行编辑!';
        }
        return null;
    }
    // 设置品牌
    @AuraEnabled
    public static void setBrand(){
        list<LostBrand> LostBrandlist =  LostReport.LostBrands;
        LostBrand tempBrand = LostBrandlist.get(setBrandNo);
        string brandName = tempBrand.lostBrand.Lost_By_Company__c;
        // fy SWAG-CCC6F6 start
        if(!'其他'.equals(tempBrand.lostBrand.Lost_By_Company_Mannual__c)){
            tempBrand.lostBrand.Lost_By_Company_Mannual__c = null;
        }
        system.debug('aaa5+++'+tempBrand.lostBrand.Lost_By_Company_Mannual__c);
        // fy SWAG-CCC6F6 end
        // ID compID = CompetitionMap.get(brandName);
        for(PCLLostProducts tempLostProduct : tempBrand.LostProducts ) {
            // test
            // tempLostProduct.LostProductss.Competitor__c = compID;
            tempLostProduct.LostProductss.LostBrandName__c = brandName;
            tempLostProduct.LostProductss.LostProduct__c = null;
            tempLostProduct.LostProductss.LostProductMannual__c = null;
            tempLostProduct.LostProductss.Quantity__c = null;
            tempLostProduct.LostProductss.ProductClass__c = null;
            tempLostProduct.LostProductss.ProductCategory__c = null;
            tempLostProduct.bool=false;
        }
    }
    // 设置品牌
    @AuraEnabled
    public static void setbrandmannual(){
        list<LostBrand> LostBrandlist =  LostReport.LostBrands;
        LostBrand tempBrand = LostBrandlist.get(setBrandNo);
        string brandName = tempBrand.lostBrand.Lost_By_Company__c;
        String brandNameMannual = tempBrand.lostBrand.Lost_By_Company_Mannual__c;
        // ID compID = CompetitionMap.get(brandName);
        for(PCLLostProducts tempLostProduct : tempBrand.LostProducts ) {
            // test
            // tempLostProduct.LostProductss.Competitor__c = compID;
            system.debug('aaaa3++'+brandNameMannual);
            if ('其他'.equals(brandName) && brandNameMannual != null && !''.equals(brandNameMannual)) {
                tempLostProduct.LostProductss.LostBrandName__c = brandNameMannual;
                // tempLostProduct.LostProductss.LostProduct__c = null;
                // tempLostProduct.LostProductss.LostProductMannual__c = null;
                // tempLostProduct.LostProductss.Quantity__c = null;
                // tempLostProduct.LostProductss.ProductClass__c = null;
                // tempLostProduct.LostProductss.ProductCategory__c = null;
                // tempLostProduct.bool=false;
            }
        }
    }
    // 保存
    @AuraEnabled
    public static String save(){
 
        if(!DataCheck()) {
            return null;
        }
        if(!dataEntry()) {
            return null;
        }
 
        brandCount = LostReport.LostBrands.size();
        pageStatus = 'View';
        return '保存成功!';
    }
    //读取并构建竞争对手品牌
    // public void BrandmapSet(){
    //     CompetitionMap = new map<string, id>();
    //     list <Competition_Company__c> competitionList
    //         =  [select id,name from Competition_Company__c];
    //     for(Competition_Company__c tempComp : competitionList ) {
    //         CompetitionMap.put(tempComp.name, tempComp.id);
    //     }
 
    // }
    // 数据检查
    @AuraEnabled
    public static boolean DataCheck(){
        boolean dataCheck = true;
        if(string.isBlank(LostReport.LostReport.LostType__c))
        {
            LostReport.LostReport.LostType__c.addError('必须填写失单类型!');
            dataCheck = false;
        }
        for(LostBrand tempLostBrand : LostReport.LostBrands ) {
            system.debug('aaaa1++'+tempLostBrand.lostBrand.Lost_By_Company_Mannual__c);
            if(string.isblank(tempLostBrand.lostBrand.Lost_By_Company__c)) {
                tempLostBrand.lostBrand.Lost_By_Company__c.addError('请填写失单品牌!');
                dataCheck = false;
            }// fy SWAG-CCC6F6 start
            else if('其他'.equals(tempLostBrand.lostBrand.Lost_By_Company__c)&&string.isblank(tempLostBrand.lostBrand.Lost_By_Company_Mannual__c)){
                tempLostBrand.lostBrand.Lost_By_Company_Mannual__c.addError('请填写失单品牌(手动)!');
                dataCheck = false;
            }// fy SWAG-CCC6F6 end
            system.debug('aaaa2++'+tempLostBrand.lostBrand.Lost_By_Company_Mannual__c);
            if(tempLostBrand.lostBrand.LostPrice__c == null) {
                tempLostBrand.lostBrand.LostPrice__c.addError('失单金额必填!');
                dataCheck = false;
            }
            if(string.isblank(tempLostBrand.lostBrand.Lost_reason_main__c )) {
                tempLostBrand.lostBrand.Lost_reason_main__c.addError('失单理由(主)必填!');
                dataCheck = false;
            }
            if(string.isblank(tempLostBrand.lostBrand.Agency__c )) {
                tempLostBrand.lostBrand.Agency__c.addError('中标经销商必填!');
                dataCheck = false;
            }
            // 检查是否有超过1个有数的产品
            integer productCount = 0;
            for( PCLLostProducts temlostProduct : tempLostBrand.LostProducts) {
                if (temlostProduct.LostProductss.LostProduct__c != null || temlostProduct.LostProductss.LostProductMannual__c != null) {
                    System.debug('失单型号' + temlostProduct.LostProductss.LostProduct__c);
                    System.debug('失单型号手动' + temlostProduct.LostProductss.LostProductMannual__c);
                    productCount ++;
                }
                // update tcm 20211123 start
                if((temlostProduct.LostProductss.LostProduct__c!=null || temlostProduct.LostProductss.LostProductMannual__c!=null)&&(temlostProduct.LostProductss.Quantity__c==null || temlostProduct.LostProductss.Quantity__c ==0)) {
                    temlostProduct.LostProductss.Quantity__c.addError('请填写失单数量!');
                    dataCheck = false;
                }
                if((temlostProduct.LostProductss.LostProduct__c!=null || temlostProduct.LostProductss.LostProductMannual__c!=null)&&temlostProduct.LostProductss.ProductCategory__c==null) {
                    if (temlostProduct.LostProductss.ProductClass__c==null) {
                        temlostProduct.LostProductss.ProductClass__c.addError('失单产品类别必填!');
                        temlostProduct.LostProductss.ProductCategory__c.addError('失单产品必填!');
                    }else {
                        temlostProduct.LostProductss.ProductCategory__c.addError('失单产品必填!');
                    }
                    dataCheck = false;
                }
                // 当失单品牌名为其他时,报错字段为失单对手型号(手动) thh 2022-01-17 start
                if ((temlostProduct.LostProductss.LostProduct__c==null && temlostProduct.LostProductss.LostProductMannual__c==null)&&(temlostProduct.LostProductss.ProductCategory__c!=null||temlostProduct.LostProductss.Quantity__c!=null)) {
                    if(tempLostBrand.lostBrand.Lost_By_Company__c != '其他'){
                        temlostProduct.LostProductss.LostProduct__c.addError('失单对手型号或失单对手型号(手动)必填!');
                    } else{
                        temlostProduct.LostProductss.LostProductMannual__c.addError('失单对手型号或失单对手型号(手动)必填!');
                    }
                    dataCheck = false;
                }
                // 当失单品牌名为其他时,报错字段为失单对手型号(手动) thh 2022-01-17 end
                // update tcm 20211123 end
            }
            // 当失单品牌名为其他时,报错字段为失单对手型号(手动) thh 2022-01-17 start
            if (productCount == 0 && tempLostBrand.LostProducts != null && tempLostBrand.LostProducts.size() > 0) {
                if(tempLostBrand.lostBrand.Lost_By_Company__c != '其他'){
                    tempLostBrand.LostProducts[0].LostProductss.LostProduct__c.addError('至少录入1条失单对手型号信息!');
                }else{
                    tempLostBrand.LostProducts[0].LostProductss.LostProductMannual__c.addError('至少录入1条失单对手型号信息!');
                }
                dataCheck = false;
            }
            // 当失单品牌名为其他时,报错字段为失单对手型号(手动) thh 2022-01-17 end
        }
 
        return dataCheck;
    }
    @AuraEnabled
    public static List<String> getValuesFromTable(){
        String ObjectApi_name = 'PCLLostBrand__c';
        String Field_name = 'Lost_By_Company__c';
        List<String> lstPickvals=new List<String>();
        //From the Object Api name retrieving the SObject
        Schema.SObjectType targetType = Schema.getGlobalDescribe().get(ObjectApi_name);
        Sobject Object_name = targetType.newSObject();
         //grab the sobject that was passed
        Schema.sObjectType sobject_type = Object_name.getSObjectType();
        //describe the sobject
        Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe(); 
         //get a map of fields for the passed sobject
        Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap();
        //grab the list of picklist values for the passed field on the sobject
        List<Schema.PicklistEntry> pick_list_values = field_map.get(Field_name).getDescribe().getPickListValues(); 
        
        for (Schema.PicklistEntry a : pick_list_values) 
        { //for all values in the picklist list
        
            lstPickvals.add(a.getValue());//add the value  to our final list
           }
 
          return lstPickvals;
    }
    @AuraEnabled
    public static list<LostBrand> brandcopy(){
        list<LostBrand> tempbrands = new list<LostBrand>();
        for(LostBrand tempbrand: LostReport.LostBrands) {
            LostBrand LostBrand =
                new LostBrand(tempbrand.lineNo,tempbrand.LostProducts );
            LostBrand.lostBrand = tempbrand.lostBrand.clone();
            LostBrand.lostBrand.id = tempbrand.lostBrand.id;
            tempbrands.add(LostBrand);
        }
        return tempbrands;
    }
    // 数据录入
    @AuraEnabled
    public static boolean dataEntry(){
        system.debug('save---start:');
        Savepoint sp = Database.setSavepoint();
        Lost_cancel_report__c templostReport = LostReport.lostReport.clone();
        templostReport.id = LostReport.lostReport.id;
        list<LostBrand> tempBrands = brandcopy();
        try{
            // 待完成,这里需要把第一个品牌的信息赋值到这个失单报告上去,还有需要把各个品牌金额汇总加起来;
            //system.debug('LostReport.LostBrands:'+LostReport.LostBrands);
            // 设置记录类型
            LostReport.lostReport.recordTypeID  =  LostReport.lostReport.LostType__c== '失单'?Schema.SObjectType.Lost_cancel_report__c.getRecordTypeInfosByDeveloperName().get('PCL_Lost_report').getRecordTypeId() : Schema.SObjectType.Lost_cancel_report__c.getRecordTypeInfosByDeveloperName().get('PCL_PartLost_report').getRecordTypeId();
            LostReport.lostReport.LostTotalAmount__c    = 0;
            LostReport.lostReport.of_lost_system_processor__c   = 0;
            LostReport.lostReport.Lost_reason_main__c = null;
            LostReport.lostReport.Lost_Reason_Sub__c  = null;
            LostReport.lostReport.Lost_By_Company__c = null;
            for(LostBrand tempLostBrand : LostReport.LostBrands) {
                if(string.isNotBlank(tempLostBrand.lostBrand.Lost_By_Company__c)) {
                    LostReport.lostReport.LostTotalAmount__c += tempLostBrand.lostBrand.LostPrice__c;
                    LostReport.lostReport.Lost_reason_main__c =
                        LostReport.lostReport.Lost_reason_main__c == null
                        ? tempLostBrand.lostBrand.Lost_reason_main__c
                        : LostReport.lostReport.Lost_reason_main__c;
                    LostReport.lostReport.Lost_Reason_Sub__c  =
                        LostReport.lostReport.Lost_Reason_Sub__c == null
                        ? tempLostBrand.lostBrand.Lost_Reason_Sub__c
                        : LostReport.lostReport.Lost_Reason_Sub__c;
                    LostReport.lostReport.Lost_By_Company__c =
                        LostReport.lostReport.Lost_By_Company__c == null
                        ? tempLostBrand.lostBrand.Lost_By_Company__c
                        : LostReport.lostReport.Lost_By_Company__c;
 
                    for( PCLLostProducts tempLostProduct : tempLostBrand.LostProducts) {
                        if(tempLostProduct.LostProductss.Quantity__c !=null && tempLostProduct.LostProductss.Quantity__c >0) {
                            LostReport.lostReport.of_lost_system_processor__c   += tempLostProduct.LostProductss.Quantity__c;
                        }
                    }
                }
            }
            upsert LostReport.lostReport;
            // 需要插入更新的失单品牌
            map<integer,PCLLostBrand__c> upsertLostBrandMap = new map<integer,PCLLostBrand__c>();
            // 需要删掉的失单品牌
            map<integer,PCLLostBrand__c> deleteLostBrandMap = new map<integer,PCLLostBrand__c>();
            // 需要插入更新的失单型号,key是品牌编号+型号编号,value是需要更新的失单型号
            map<string,PCLLostProduct__c> upsertLostProductMap = new map<string,PCLLostProduct__c>();
            // 需要删掉的失单品牌
            list<PCLLostProduct__c> deleteLostProductList = new list<PCLLostProduct__c>();
            for(LostBrand tempLostBrand : LostReport.LostBrands ) {
                // 如果有失单品牌,那么就要插入更新;
                system.debug('tempLostBrand.lostBrand.Lost_By_Company__c:'+tempLostBrand.lostBrand.Lost_By_Company__c);
                if(string.isNotBlank(tempLostBrand.lostBrand.Lost_By_Company__c)) {
                    upsertLostBrandMap.put(tempLostBrand.lineNo,tempLostBrand.lostBrand);
                    tempLostBrand.lostBrand.Lost_cancel_report__c = LostReport.lostReport.id;
                    tempLostBrand.lostBrand.Name = tempLostBrand.lostBrand.Lost_By_Company__c;
                    integer tempNo = 0;
                    for( PCLLostProducts tempLostProduct : tempLostBrand.LostProducts) {
                        system.debug('Name:'+ tempLostProduct.LostProductss.Name );
                        if(tempLostProduct.LostProductss.Quantity__c !=null && tempLostProduct.LostProductss.Quantity__c >0) {
                            tempLostProduct.LostProductss.Name = tempLostBrand.lostBrand.Name + tempLostProduct.LostProductss.Quantity__c;
                            upsertLostProductMap.put(tempLostBrand.lineNo +':'+ tempNo,tempLostProduct.LostProductss);
                        }else if(string.isNotBlank(tempLostProduct.LostProductss.id)) {
                            deleteLostProductList.add(tempLostProduct.LostProductss);
                        }
                        tempNo++;
                    }
                }
                // 如果没有品牌,但是有id, 这样数据需要删除
                else if(string.isNotBlank(tempLostBrand.lostBrand.id)) {
                    deleteLostBrandMap.put(tempLostBrand.lineNo,tempLostBrand.lostBrand);
                    // 这里可能需要也判断一下失单型号有没有需要删掉
                }
            }
            if(upsertLostBrandMap.size()>0) {
                upsert upsertLostBrandMap.values();
            }
            for( string productNo :upsertLostProductMap.keyset()) {
                PCLLostProduct__c tempLostProduct = upsertLostProductMap.get(productNo);
                integer brandNo = integer.valueof(productNo.split(':')[0]);
                tempLostProduct.PCLLostBrand__c = upsertLostBrandMap.get(brandNo).id;
            }
            if(upsertLostProductMap.size()>0) {
                upsert upsertLostProductMap.values();
            }
            if(deleteLostBrandMap.size()>0) {
                delete deleteLostBrandMap.values();
                for(PCLLostBrand__c pcl: deleteLostBrandMap.values()) {
                    pcl.id = null;
                }
            }
            if(deleteLostProductList.size()>0) {
                delete deleteLostProductList;
                for(PCLLostProduct__c pclp: deleteLostProductList) {
                    pclp.id = null;
                }
            }
            if(deleteBrandIDSet.size() > 0) {
                list<id> tempdeleteIDlist = new list<id>();
                tempdeleteIDlist.addall(deleteBrandIDSet);
                database.delete(tempdeleteIDlist);
 
            }
 
            for(integer brandNumber =0; brandNumber< LostReport.LostBrands.size(); brandNumber++) {
 
                LostBrand tempLostBrand = LostReport.LostBrands.get(brandNumber);
                if(tempLostBrand.lostBrand.Id == null) {
                    LostReport.LostBrands.remove(brandNumber);
                    brandNumber--;
                    continue;
                }
                for(integer productNumber = 0; productNumber< tempLostBrand.LostProducts.size(); productNumber++) {
                    PCLLostProducts tempLostProduct = tempLostBrand.LostProducts.get(productNumber);
                    if(tempLostProduct.LostProductss.Id == null) {
                        tempLostBrand.LostProducts.remove(productNumber);
                        productNumber--;
                        continue;
                    }
                }
            }
            lostReportId = LostReport.lostReport.id;
            system.debug('save---end:');
        }catch (Exception ex) {
            Database.rollback(sp);
            LostReport.lostReport = templostReport;
            LostReport.LostBrands = tempBrands;
            // 这里需要写一些报错信息
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,ex.getMessage()));
            return false;
        }
        return true;
    }
 
    
    // 提交
    @AuraEnabled
    public static Map<string,Object> submit(){
        Map<string,string> messages = new Map<string,string>();
        if(!'草案'.equals(LostReport.LostReport.Report_Status__c) ) {
            messages.put('error', '只有在草案中才能提交申请!');
            return messages;
        }
        Map<string,Object> objs = new Map<string,Object>();
        Savepoint sp = Database.setSavepoint();
        try{
            LostReport.lostReport.Report_Status__c = '提交';
            update LostReport.lostReport;
            Approval.ProcessSubmitRequest psr = new Approval.ProcessSubmitRequest();
            psr.setObjectId(lostReportId);
            Approval.ProcessResult submitResult = Approval.process(psr);
            objs.put('uri', '/apex/PCLLostReportPage');
            objs.put('Id', lostReportId);
            objs.put('pageStatus', 'View');
            objs.put('submitFlag', '1');
            return objs;
        }catch(exception ex) {
            Database.rollback(sp);
            messages.put('error', ex.getMessage());
            return messages;
        }
    }
    // 添加品牌
    @AuraEnabled
    public static String addBrand(){
        LostReport.LostBrands.add(new LostBrand(LostReport.LostBrands.size()));
        brandCount = LostReport.LostBrands.size();
        return null;
    }
    // 删除品牌 这个有参数brandNo,才知道是删除那个品牌
    @AuraEnabled
    public static String Remove(){
        system.debug('RemoveBrandNo:'+RemoveBrandNo);
        list<LostBrand> tempLostBrands = new List<lostBrand>();
        Integer i = 0;
        for(integer j = 0; j< LostReport.LostBrands.size(); j++ ) {
            LostBrand templostBrand  = LostReport.LostBrands.get(j);
            if(j != RemoveBrandNo) {
                templostBrand.lineNo = i;
                tempLostBrands.add(templostBrand);
                i++;
            }else if(!string.isBlank(templostBrand.lostBrand.id)) {
                deleteBrandIDSet.add(templostBrand.lostBrand.id);
            }
        }
 
        LostReport.LostBrands =  tempLostBrands;
 
        brandCount = LostReport.LostBrands.size();
        return null;
    }
    // 添加型号, 这个有参数brandNo,才知道是添加到那个品牌
    // update tcm 20211125 添加型号时自动带出品牌 start
    @AuraEnabled
    public static String addProduct(){
        system.debug('brandNo:'+brandNo);
        LostBrand tempLostBrand = LostReport.LostBrands.get(brandNo);
        // PCLLostProduct__c plp = new PCLLostProduct__c(Competitor__c=CompetitionMap.get(LostReport.LostBrands[brandNo].lostBrand.Lost_By_Company__c));
        string brandName = tempLostBrand.lostBrand.Lost_By_Company__c;
        PCLLostProduct__c plp = new PCLLostProduct__c();
        plp.LostBrandName__c = brandName;
        tempLostBrand.LostProducts.add(new PCLLostProducts(tempLostBrand.LostProducts.size(),plp));
        tempLostBrand.ProductSize = tempLostBrand.LostProducts.size();
        return null;
    }
    // update tcm 20211125 添加型号时自动带出品牌 end
    // 页面的数据结构
    public class LostReport {
        @AuraEnabled
        public Lost_cancel_report__c lostReport { get; set; }
        @AuraEnabled
        public list<LostBrand> LostBrands { get; set; }
        public LostReport(){
            lostReport = new Lost_cancel_report__c();
            LostBrands = new list<LostBrand> {new LostBrand(0)};
        }
        public LostReport(Lost_cancel_report__c lostReport){
            this.lostReport = lostReport;
            LostBrands = new list<LostBrand> {new LostBrand(0)};
        }
        public LostReport(Lost_cancel_report__c lostReport,  list<LostBrand> LostBrands){
            this.lostReport = lostReport;
            this.LostBrands = LostBrands;
        }
    }
 
    public class LostBrand {
        @AuraEnabled
        public PCLLostBrand__c lostBrand { get; set; }
        @AuraEnabled
        public Integer lineNo { get; set; }
        @AuraEnabled
        public list<PCLLostProducts> LostProducts { get; set; }
        @AuraEnabled
        public Integer ProductSize {get; set;}
        public LostBrand( integer lineNo ){
            lostBrand = new PCLLostBrand__c();
            this.lineNo = lineNo;
            LostProducts = new list<PCLLostProducts> {new PCLLostProducts()};
            ProductSize = 1;
            lostBrand = new PCLLostBrand__c();
        }
        public LostBrand( integer lineNo, list<PCLLostProducts> LostProducts ){
            lostBrand = new PCLLostBrand__c();
            this.lineNo = lineNo;
            this.LostProducts = LostProducts;
            ProductSize = LostProducts.size();
        }
        public LostBrand( integer lineNo, list<PCLLostProducts> LostProducts,  PCLLostBrand__c lostBrand){
            this.lostBrand = lostBrand;
            this.lineNo = lineNo;
            this.LostProducts = LostProducts;
            ProductSize = LostProducts.size();
        }
    }
    // add tcm 20211119 start
    public class PCLLostProducts {
        @AuraEnabled
        public Integer lineNo2 { get; set; }
        @AuraEnabled
        public PCLLostProduct__c LostProductss { get; set; }
        @AuraEnabled
        public Boolean bool { get; set; }
 
        public PCLLostProducts() {
            this.lineNo2 = 0;
            this.LostProductss=new PCLLostProduct__c();
        }
 
        public PCLLostProducts(Integer lineNo2) {
            this.lineNo2 = lineNo2;
            this.LostProductss=new PCLLostProduct__c();
        }
 
        public PCLLostProducts(Integer lineNo2,PCLLostProduct__c LostProductss) {
            this.lineNo2 = lineNo2;
            this.LostProductss=LostProductss;
            if (LostProductss.LostProduct__c != null) {
                this.bool = true;
            }
        }
    }
    // add tcm 20211119 end
 
    // add tcm 20211118 start
    public static void search() {
        if (LostReport.LostBrands[topNum].LostProducts[secondNum].LostProductss.LostProduct__c==null) {
            LostReport.LostBrands[topNum].LostProducts[secondNum].LostProductss.ProductClass__c=null;
            LostReport.LostBrands[topNum].LostProducts[secondNum].LostProductss.ProductCategory__c=null;
            LostReport.LostBrands[topNum].LostProducts[secondNum].bool=false;
 
        }else {
            Product2 prd = [select Id,ProductClass__c, ProductCategory__c from Product2 where Id =:LostReport.LostBrands[topNum].LostProducts[secondNum].LostProductss.LostProduct__c];
            if (prd.ProductCategory__c!=null) {
                LostReport.LostBrands[topNum].LostProducts[secondNum].LostProductss.ProductClass__c=prd.ProductClass__c;
                LostReport.LostBrands[topNum].LostProducts[secondNum].LostProductss.ProductCategory__c=prd.ProductCategory__c;
                LostReport.LostBrands[topNum].LostProducts[secondNum].bool=true;
            }else {
                LostReport.LostBrands[topNum].LostProducts[secondNum].LostProductss.ProductClass__c=null;
                LostReport.LostBrands[topNum].LostProducts[secondNum].LostProductss.ProductCategory__c=null;
                LostReport.LostBrands[topNum].LostProducts[secondNum].bool=false;
 
            }
        }
    }
    // add tcm 20211118 end
}