liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
public with sharing class LexContractAuthorizeController {
    public LexContractAuthorizeController() {
    }
    @AuraEnabled
    public static Opportunity init(String recordId){
        try{
            Opportunity item=[select 
             id,Estimation_Decision__c,Contract_DB_complite_day__c,Contract_Authorize__c,Contract_Authorize_Lock__c,If_Need_Authorize__c,
             Authorized_DB_No__c,SPO_URL__c,Estimation_No__c,Closing_Bid_Date__c,Bid_Date__c,
             Cnt_Lost_cancel_Draft__c,Cnt_Lost_cancel_report__c,IF_Submit__c,Agency1__c,Trade__c,Sales_Root__c,Is_Corrosion__c,AccDealerBlacklist__c,
             Agency2__c,Hospital__c,OwnerId,Sales_assistant_name_text__c
            //zzm 贸易合规2期 20231120 add start 
            ,Hospital__r.TradeComplianceStatus__c
            ,Estimation_Id__c
            ,AccDealerBlacklist2__c
            //zzm 贸易合规2期 20231120 add end 
             from Opportunity
             where id = :recordId];
            return item;
 
        }catch(Exception e){
            System.debug('LexStockCancelController init error: '+e.getMessage());
        }
        return null;
    }
    //zzm 贸易合规2期 20231120 add start 
    //获取询价上最新报价的产品的贸易合规状态
    @AuraEnabled
    public static String getProTradeComplianceStatus(String eId) {
        List<QuoteLineItem> quoteLineItemList = [SELECT 
        Id
        ,QuoteId
        ,Product2Id
        ,PricebookEntry.Product2.USRatio_US__c
        ,PricebookEntry.Product2.USRatio_US_OUT10__c
        ,PricebookEntry.Product2.CountryOfOrigin__c
        ,PricebookEntry.Product2.ProTradeComplianceStatus__c
 
        from QuoteLineItem where QuoteId= :eId];
        for(QuoteLineItem qlt: quoteLineItemList) {
            // String flag = ControllerUtil.tradeComplianceStatusFlag(qlt.PricebookEntry.Product2.CountryOfOrigin__c,qlt.PricebookEntry.Product2.USRatio_US_OUT10__c);
            String flag = qlt.PricebookEntry.Product2.ProTradeComplianceStatus__c;
            if(flag == '0') {
                return '0';
            }
        }
        return '1';
        
    }
 
    //贸易合规 20230307 you start
    //营业拦截提醒
    @AuraEnabled
    public static Boolean accSendEmail(String  AccDealerBlacklist,String accname,String angency1,String angency2,String ownerids,String ddid,String oppid) {
      //发送邮件
        List<Messaging.SingleEmailMessage> sendMails = new List<Messaging.SingleEmailMessage>();
        List<String>  MailsList = new List<String>();
        List<String> userIdList = new List<String>();
        if(String.isNotBlank(ownerids)){
            userIdList.add(ownerids);
        }
        if(String.isNotBlank(ddid)){
            userIdList.add(ddid);
        }
        //询价所有人,产品担当,发邮件
        List<User> UsrList = [SELECT email FROM user WHERE id in :userIdList]; 
        //获取各个用户的email
        for (User u : UsrList){
            MailsList.add(u.email);
        }
        MailsList.add('olympus@prec-tech.com'); //zzm 20231218 合规2期
        MailsList.add(System.Label.LegalDepartmentEmail); //zzm 20231218 合规2期
        system.debug('111===:'+MailsList);
        if (MailsList!=null&&MailsList.size()>0){
            String title = '';
            String body = '';
            //zzm 20240111 合规2期 start
            String tmpMsg = '黑名单';
            title = ' 【贸易合规】询价医院/经销商涉及'; 
            if(AccDealerBlacklist=='123'){
                body += '黑名单的客户为:' + accname+ '<br/>';
                body += '黑名单的经销商1为:' + angency1+ '<br/>';
                body += '黑名单的经销商2为:' + angency2+ '<br/>';
            }else if(AccDealerBlacklist=='12'){
                body += '黑名单的客户为:' + accname+ '<br/>';
                body += '黑名单的经销商1为:' + angency1+ '<br/>';
            }else if(AccDealerBlacklist=='23'){
                body += '黑名单的经销商1为:' + angency1+ '<br/>';
                body += '黑名单的经销商2为:' + angency2+ '<br/>';
            }else if(AccDealerBlacklist=='13'){
                body += '黑名单的客户为:' + accname+ '<br/>';
                body += '黑名单的经销商2为:' + angency2+ '<br/>';
            }else if(AccDealerBlacklist=='1'){
                body += '黑名单的客户为:' + accname+ '<br/>';
            }else if(AccDealerBlacklist=='2'){
                body += '黑名单的经销商1为:' + angency1+ '<br/>';
            }else if(AccDealerBlacklist=='3'){
                body += '黑名单的经销商2为:' + angency2+ '<br/>';
            }
 
            if(AccDealerBlacklist=='890'){
                tmpMsg = '冻结审核中';
                body += '冻结审核中的客户为:' + accname+ '<br/>';
                body += '冻结审核中的经销商1为:' + angency1+ '<br/>';
                body += '冻结审核中的经销商2为:' + angency2+ '<br/>';
            }else if(AccDealerBlacklist=='89'){
                tmpMsg = '冻结审核中';
                body += '冻结审核中的客户为:' + accname+ '<br/>';
                body += '冻结审核中的经销商1为:' + angency1+ '<br/>';
            }else if(AccDealerBlacklist=='90'){
                tmpMsg = '冻结审核中';
                body += '冻结审核中的经销商1为:' + angency1+ '<br/>';
                body += '冻结审核中的经销商2为:' + angency2+ '<br/>';
            }else if(AccDealerBlacklist=='80'){
                tmpMsg = '冻结审核中';
                body += '冻结审核中的客户为:' + accname+ '<br/>';
                body += '冻结审核中的经销商2为:' + angency2+ '<br/>';
            }else if(AccDealerBlacklist=='8'){
                tmpMsg = '冻结审核中';
                body += '冻结审核中的客户为:' + accname+ '<br/>';
            }else if(AccDealerBlacklist=='9'){
                tmpMsg = '冻结审核中';
                body += '冻结审核中的经销商1为:' + angency1+ '<br/>';
            }else if(AccDealerBlacklist=='0'){
                tmpMsg = '冻结审核中';
                body += '冻结审核中的经销商2为:' + angency2+ '<br/>';
            }
           
            body += '<a href="' + System.Label.Environment_Url + oppid + '">'
                    + System.Label.Environment_Url+ oppid +'</a><br/>'; 
            body += '谢谢!';
  
            Messaging.SingleEmailMessage messageNEW = new Messaging.SingleEmailMessage();
            messageNEW.setSubject(title + tmpMsg); 
            //zzm 20240111 合规2期 end
            
            messageNEW.setHtmlBody(body);
            messageNEW.setCharset('UTF-8');
            messageNEW.setToAddresses(MailsList); 
 
            //List<String> toCcMailsList = new List<String>();
            //toCcMailsList.add('youchang@prec-tech.com');
            //messageNEW.setCcAddresses(toCcMailsList); 
            
            sendMails.add(messageNEW);
 
        }                      
            
        boolean rs = true;
        if (sendMails.size() > 0) {
            Messaging.Email[] allMails = new Messaging.Email[]{};
            for(Integer j = 0; j < sendMails.size(); j++) {
                allMails.add(sendMails.get(j));
            }
            system.debug('222===:'+allMails);
        
            Messaging.SendEmailResult[] results = Messaging.sendEmail(allMails);
            System.debug('results 结果 :'+results);
            for (Integer i = 0; i < results.size(); i++) {
                if (results[i].success == false) {
                    system.debug('=====send mail error:' + results[i].errors[0].message);
                    rs = false;
                }   
            }
        }
        return rs;
    }
    //合规2期警示名单发邮件
    @AuraEnabled
    public static Boolean proTradeComplianceSendEmail(String oppid,String btnType) {
        boolean rs = true;
        try{
            Opportunity item=[select 
             id,Estimation_Decision__c,Contract_DB_complite_day__c,Contract_Authorize__c,Contract_Authorize_Lock__c,If_Need_Authorize__c,
             Authorized_DB_No__c,SPO_URL__c,Estimation_No__c,Closing_Bid_Date__c,Bid_Date__c,
             Cnt_Lost_cancel_Draft__c,Cnt_Lost_cancel_report__c,IF_Submit__c,Agency1__c,Trade__c,Sales_Root__c,Is_Corrosion__c,AccDealerBlacklist__c,
             Agency2__c,Hospital__c,OwnerId,Sales_assistant_name_text__c
            ,Hospital__r.Name
            ,Hospital__r.TradeComplianceStatus__c
            ,Agency1__r.Name
            ,Agency1__r.TradeComplianceStatus__c
            ,Agency2__r.Name
            ,Agency2__r.TradeComplianceStatus__c
            ,Sales_assistant_name_text__r.Alias
            ,Estimation_Id__c
             from Opportunity
             where id = :oppid];
             //得到不合格的产品
            List<QuoteLineItem> ProTradeComplianceList = new List<QuoteLineItem>();
            List<QuoteLineItem> quoteLineItemList = [SELECT 
            Id
            ,QuoteId
            ,Product2Id
            ,PricebookEntry.Product2.Name
            ,PricebookEntry.Product2.ProTradeComplianceStatus__c
 
            from QuoteLineItem where QuoteId= :item.Estimation_Id__c];
            for(QuoteLineItem qlt: quoteLineItemList) {
                String flag = qlt.PricebookEntry.Product2.ProTradeComplianceStatus__c;
                if(flag == '0') {
                    ProTradeComplianceList.add(qlt);
                }
            }
 
            
 
            //发送邮件
            String AccDealerBlacklist = item.AccDealerBlacklist__c;
            List<Messaging.SingleEmailMessage> sendMails = new List<Messaging.SingleEmailMessage>();
            List<String>  MailsList = new List<String>();
            List<String>  toCcMailsList = new List<String>();
           
             //询价所有人,产品担当,发邮件
            User ownerUser = [SELECT email,Alias  FROM user WHERE id = :item.OwnerId]; 
            toCcMailsList.add('olympus@prec-tech.com'); 
            MailsList.add(System.Label.LegalDepartmentEmail); 
 
            //当前操作人发邮件
            // User currentUser = [SELECT email,Alias  FROM user WHERE id = :userinfo.getUserId()]; 
 
            User lidongFengIdEmail = [SELECT email,Alias  FROM user WHERE id = :System.Label.lidongFengId]; 
            if(btnType=='授权申请') {
                MailsList.add(lidongFengIdEmail.email);
            }
            // MailsList.add(currentUser.email); 
            system.debug('111===:'+MailsList);
            if (MailsList!=null&&MailsList.size()>0){
                String title = '';
                String body = '';
                title = ' 【贸易合规】--询价'+btnType+'涉及警示产品'; 
                if(AccDealerBlacklist=='567'){
                    body += '警示名单的客户为:' + item.Hospital__r.Name+' ,状态为:' +item.Hospital__r.TradeComplianceStatus__c + '<br/>';
                    body += '警示名单的经销商1为:' + item.Agency1__r.Name+' ,状态为:' +item.Agency1__r.TradeComplianceStatus__c+ '<br/>';
                    body += '警示名单的经销商2为:' + item.Agency2__r.Name+' ,状态为:' +item.Agency2__r.TradeComplianceStatus__c+ '<br/>';
                }else if(AccDealerBlacklist=='56'){
                    body += '警示名单的客户为:' + item.Hospital__r.Name+' ,状态为:' +item.Hospital__r.TradeComplianceStatus__c+ '<br/>';
                    body += '警示名单的经销商1为:' + item.Agency1__r.Name+' ,状态为:' +item.Agency1__r.TradeComplianceStatus__c+ '<br/>';
                }else if(AccDealerBlacklist=='67'){
                    body += '警示名单的经销商1为:' + item.Agency1__r.Name+' ,状态为:' +item.Agency1__r.TradeComplianceStatus__c+ '<br/>';
                    body += '警示名单的经销商2为:' + item.Agency2__r.Name+' ,状态为:' +item.Agency2__r.TradeComplianceStatus__c+ '<br/>';
                }else if(AccDealerBlacklist=='57'){
                    body += '警示名单的客户为:' + item.Hospital__r.Name+' ,状态为:' +item.Hospital__r.TradeComplianceStatus__c+ '<br/>';
                    body += '警示名单的经销商2为:' + item.Agency2__r.Name+' ,状态为:' +item.Agency2__r.TradeComplianceStatus__c+ '<br/>';
                }else if(AccDealerBlacklist=='5'){
                    body += '警示名单的客户为:' + item.Hospital__r.Name+' ,状态为:' +item.Hospital__r.TradeComplianceStatus__c+ '<br/>';
                }else if(AccDealerBlacklist=='6'){
                    body += '警示名单的经销商1为:' + item.Agency1__r.Name+' ,状态为:' +item.Agency1__r.TradeComplianceStatus__c+ '<br/>';
                }else if(AccDealerBlacklist=='7'){
                    body += '警示名单的经销商2为:' + item.Agency2__r.Name+' ,状态为:' +item.Agency2__r.TradeComplianceStatus__c+ '<br/>';
                }
                body += '询价所有人是:' +ownerUser.Alias +',询价助理是:' + item.Sales_assistant_name_text__r.Alias +'<br/>';
                body += '涉及的警示产品:' ;
                for(QuoteLineItem qlt: ProTradeComplianceList) {
                   body += '<a href="' + System.Label.Environment_Url + qlt.Product2Id + '">'
                        +  qlt.PricebookEntry.Product2.Name +'</a><br/>';  
                }
 
                body += '谢谢!';
      
                Messaging.SingleEmailMessage messageNEW = new Messaging.SingleEmailMessage();
                messageNEW.setSubject(title);
                messageNEW.setHtmlBody(body);
                messageNEW.setCharset('UTF-8');
                messageNEW.setToAddresses(MailsList); 
                messageNEW.setCcAddresses(toCcMailsList); 
                
                sendMails.add(messageNEW);
 
            }                      
                
            
            if (sendMails.size() > 0) {
                Messaging.Email[] allMails = new Messaging.Email[]{};
                for(Integer j = 0; j < sendMails.size(); j++) {
                    allMails.add(sendMails.get(j));
                }
                system.debug('222===:'+allMails);
            
                Messaging.SendEmailResult[] results = Messaging.sendEmail(allMails);
                System.debug('results 结果 :'+results);
                for (Integer i = 0; i < results.size(); i++) {
                    if (results[i].success == false) {
                        system.debug('=====send mail error:' + results[i].errors[0].message);
                        rs = false;
                    }   
                }
            }
        }catch(Exception e){
            System.debug('LexStockCancelController proTradeComplianceSendEmail error: '+e.getMessage());
            rs = false;
        }   
        return rs;     
    }
    //zzm 贸易合规2期 20231120 add end
    @AuraEnabled
    public static UpdateResult updateOppotunity(        
        String recordId
    ) {
        UpdateResult result = new UpdateResult();
        result.recordId = recordId;
        try{
            Opportunity opp=new Opportunity(id=recordId);
            opp.Contract_Authorize__c = true;
 
            opp.If_Interface_Lock__c=true;
            
            opp.IF_Submit__c = true;
            update opp;
            result.success = true;
            result.errors = new List<String>();
            return result;
        }catch(Exception e){
            result.success = false;
            result.errors = new List<String>();
            if(e.getMessage().contains(':')){
                String eMessage =e.getMessage();
                Integer left = eMessage.indexOf(',')+1;
                Integer right= eMessage.lastIndexOf('。 ')+1;
                if(right>eMessage.length()||right<=left){
                    right=eMessage.length();
                }
                String mes=eMessage.substring(left,right);
                result.errors.add(mes);
            }else{
                result.errors.add(e.getMessage());
            }
            System.debug(LoggingLevel.INFO,'updateOppotunity  Error : ' + e);
        }
        return result;
    }
    @AuraEnabled
    public static String submitApprovalRequest(String recordId) {
        try{
           Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
           req.setObjectId(recordId);
           Approval.ProcessResult result = Approval.process(req);
           if(result!=null&&result.getErrors()!=null&&result.getErrors().size()>0) return result.getErrors().get(0).getMessage();
        }catch(Exception e){
            System.debug('LexStockCancelController submitApprovalRequest  error: '+e.getMessage());
            return e.getMessage();
        }
           return null;
    }
 
    @AuraEnabled
    public static String getProfileId(){
        return UserInfo.getProfileId();
    }
    @AuraEnabled
    public static boolean queryProfileId(){
        List<Profile> res=[select id,name from profile where name in 
        ('2J3_特约店窗口','2M4_市场产品本部担当(询价)','系统管理员','2S6-2_营业助理','2S7_销售本部合同管理窗口','2S6-1_销售本部窗口')
        ];
        for(Profile p:res){
            if (UserInfo.getProfileId()==p.id) {
                return true;
            }
        }
        return false;
    }
    @AuraEnabled
    public static Boolean judgeIsAssistantAppliedOutTime(String  Quoteid) {
        List<PromotionSales__c> PromotionSalesList = [select id,IsAssistantAppliedOutTime__c from PromotionSales__c where Quote__r.Quote_No__c =:Quoteid];
        for(PromotionSales__c pro : PromotionSalesList){
            if(pro.IsAssistantAppliedOutTime__c){
                return false;
            }
        }
        return true;
    }
     @AuraEnabled
    public static Opportunity searchOpportunity(String oppid) {
        try{
           Opportunity res=[select Id,Contract_Authorize_Lock__c from Opportunity where Id =:oppid];
           return res;
        }catch(Exception e){
            System.debug('LexStockCancelController submitApprovalRequest  error: '+e.getMessage());
        }
           return null;
    }
    @AuraEnabled
    // LHJ 阿西赛多检查 Start
    public  static String checkDangerItem(String agency1) {
        //没有危化品证照的提示信息
        String licenseStr = '第一经销商没有有效的危险化学品经营许可证。';
        //没有阿西赛多经销商协议的提示信息
        String accStr = '第一经销商没有阿西赛多经销商协议。';
 
        //检查是否有证照
        List<License_Information__c> licenseList = [SELECT Id
                                                      FROM License_Information__c
                                                     WHERE LicenseAndAccount__c = :agency1
                                                       AND LicenseType__c = '危险化学品经营许可证'
                                                       AND Is_Active_Formula__c = true
                                                       
                                                    ];
        //检查是否有阿西赛多协议
        List<Account> accountList = [SELECT Id
                                                      FROM Account
                                                     WHERE Parent.id = :agency1
                                                       //阿西赛多 增加检索阿西赛多协议 以及 可以做报价(复选框) 精琢技术 wql 2021/01/04 start
                                                       AND Contract_Quote_Decide_Flag_checkbox__c =true
                                                       AND Assiesedo_Dealer__c  = true
                                                       //阿西赛多 增加检索阿西赛多协议 以及 可以做报价(复选框) 精琢技术 wql 2021/01/04 end
                                                    ];
 
        system.debug('licenseList:'+licenseList.size());
        system.debug('agency1:'+agency1);
        //①既没有危化品证也没有阿西赛多协议
        if(licenseList.size() == 0 && accountList.size() ==0){
            return licenseStr+accStr;
        }
        //②没有危化品证
        else if(licenseList.size() ==0){
            return licenseStr;
        }
        //②没有阿西赛多经销商协议
        else if(accountList.size() ==0){
            return accStr;
        }
        //③全都有
        else{
            return 'OK';
        }
    }
    @AuraEnabled
    public static String oppCheck(String oppid, String saveFlg) {
 
        Map<Id, String> proMap= new Map<Id, String>();
        List<OpportunityLineItem> OppItemList = [select PricebookEntry.Product2Id, PricebookEntry.Product2.Name
                                                   From OpportunityLineItem 
                                                  Where OpportunityId = :oppid];
 
        if (OppItemList.size() > 0) {
            for (OpportunityLineItem opl : OppItemList) {
                proMap.put(opl.PricebookEntry.Product2Id, opl.PricebookEntry.Product2.Name);
            }
        }
 
        List<Opportunity> oppList = [select Agency1__c from Opportunity where id = :oppid];
        String angencyId = oppList[0].Agency1__c;
        String strRet = '';
 
        if (saveFlg == '1') {
            strRet = checkProRegisterDecide(proMap, angencyId, oppid);
        } else {
            strRet = checkProRegisterDecide(proMap, angencyId, '');
        }
        return strRet;
    }
    public static String checkProRegisterDecide(Map<Id, String> proMap, String agency1, String OppId) {
    
        String errormessage = '';
        Boolean error = false;
 
        Map<String, String> CheckMap = new Map<String, String>();
        CheckMap = MapCheckProRegisterDecide(proMap, agency1, OppId);
        if (CheckMap.isEmpty()) {
            return 'OK';
        } else {
            return '有不可销售产品或超出经销商经营范围,请更新报价单' + CheckMap;
        }
 
    }
    @AuraEnabled
    public static String updReg(String  oppid) {
 
        List<OpportunityLineItem> updList = new List<OpportunityLineItem>();
        List<OpportunityLineItem> OppItemList = [select id, Id__c, Register_alleffective__c,
                                                        PricebookEntry.Product2.RegisterNo_ALL__c 
                                                           From OpportunityLineItem 
                                                          Where OpportunityId = :oppid];
        for (OpportunityLineItem ol : OppItemList) {
            ol.Register_alleffective__c = ol.PricebookEntry.Product2.RegisterNo_ALL__c;
            updList.add(ol);
        }
        if (updList.size() > 0)  update updList;
        return 'OK';
    }
    public static Map<String, String> MapCheckProRegisterDecide(Map<Id, String> proMap, String agency1, String OppId) {
 
        Map<String, String> retMap = new Map<String, String>();
        String errormessage = '';
        Boolean error = false;
        Map<String,List<String>> proAllBSstrMap;
        List<String> proNotNeedList = new List<String>();
 
        Map<String,List<String>> old_newMap = new Map<String,List<String>>();
        Map<String,List<String>> new_oldMap = new Map<String,List<String>>();
 
        // 取得 经营范围对应产品关系
        List<String> midStr = new List<String>();
        List<String> midStrO = new List<String>();
 
        // List<Product_Register_contrast__c> rrList = Product_Register_contrast__c.getall().values();
 
        // for (Product_Register_contrast__c rr : rrList) {
 
        //     midStr = new List<String>();
        //     midStrO = new List<String>();
 
        //     // 经营范围对应产品关系 Map作成 旧-新对照
        //     if (old_newMap.containsKey(rr.Register_old__c)) {
        //         old_newMap.get(rr.Register_old__c).add(rr.Register_new__c);
        //     } else {
        //         midStrO.add(rr.Register_new__c);
        //         old_newMap.put(rr.Register_old__c, midStrO.clone());
        //     }
 
        //     // 经营范围对应产品关系 Map作成 新-旧对照
        //     if (new_oldMap.containsKey(rr.Register_new__c)) {
        //         new_oldMap.get(rr.Register_new__c).add(rr.Register_old__c);
        //     } else {
        //         midStr.add(rr.Register_old__c);
        //         new_oldMap.put(rr.Register_new__c, midStr.clone());
        //     }
            
        // }
        // 取得经销商 医疗器械经营许可证
        List<License_Information__c> licenseList = [SELECT Id,
                                                           ScopeKey__c, 
                                                           LicenseType__c
                                                      FROM License_Information__c
                                                     WHERE LicenseAndAccount__c = :agency1
                                                    AND (LicenseType__c = '医疗器械经营许可证'
                                                        OR LicenseType__c = '第二类医疗器械经营备案凭证')
                                                      AND Is_Active_Formula__c = true
                                                    ];
        // 判断 医疗器械经营许可证 是否存在
        if (licenseList.size() == 0) {
            error = true;
            errormessage = '第一经销商没有有效的医疗器械经营许可证。';
            retMap.put('agency', '0');
 
        } else {
            //经销商经营范围
            List<String> agListA = new List<String>();
            List<String> agListTemp = new List<String>();
            List<String> agList = new List<String>();
 
            //取经销商下 所有医疗器械经营许可证、第二类医疗器械经营备案凭证
            for (License_Information__c li : licenseList) {
                if (li.ScopeKey__c != null) {
                    agListA.addAll(li.ScopeKey__c.split(';'));
                    agListTemp.addAll(li.ScopeKey__c.split(';'));
                }
            }
            for (String a : agListTemp) {
                if (a.length() >= 6) {
                    agList.add(a.substring(0, a.length() - 1));
                } else {
                    agList.add(a.trim());
                }                
            }
 
            //产品注册证
            Map<String,Map<String,String>> proBSMap = new Map<String,Map<String,String>>();
            
            Map<String,String> proMidMap = null;
            //产品所有有效注册证(可以和经销商匹配)
            proAllBSstrMap = new Map<String,List<String>>();
            set<Id> proIdList = new set<Id>();
 
            for (String qli : proMap.keySet()) {     
                    proIdList.add(qli);
                    proBSMap.put(qli, proMidMap);
                    List<String> empList = new List<String>();
                    proAllBSstrMap.put(qli, empList);
                
            }
 
            List<Product_Register_Link__c> prls = [
                                                 Select Product2__c,
                                                        Product2__r.Asset_Model_No__c,
                                                        Product2__r.SFDA_Approbated_Status__c,
                                                        Product2__r.SFDA_Status_New__c, // 产品的CFDA最终状态 20181225
                                                        Product_Register__r.Name,
                                                        Product_Register__r.BusinessScopeKey__c, 
                                                        Product_Register__r.RegisterNoStatus__c,
                                                        Product_Register__r.MedPrdClass__c 
                                                   From Product_Register_Link__c
                                                  where Product2__c in :proIdList
                                                  ];
 
            // LHJ 20190102 增加跳过逻辑 Start
            List<Product2> productList = [select ID,SFDA_Status__c from Product2 where id in :proIdList];
            for (Product2 pro : productList) {
                if (pro.SFDA_Status__c == '不要') {
                    proNotNeedList.add(pro.ID);
                }
            }
            // LHJ 20190102 增加跳过逻辑 End
 
            for (Product_Register_Link__c prl : prls) {
                
                // 设置产品类别MAP
                if (String.isNotBlank(prl.Product_Register__r.BusinessScopeKey__c)) {
                    system.debug('Product2 +++++++' + prl.Product2__c);
                    if (proBSMap.get(prl.Product2__c) != null) {
                        proBSMap.get(prl.Product2__c).put(prl.Product_Register__r.Name, prl.Product_Register__r.BusinessScopeKey__c);
                    
                    } else {
                        Map<String,String> toooMap = new Map<String,String>();
                        toooMap.put(prl.Product_Register__r.Name, prl.Product_Register__r.BusinessScopeKey__c);
                        proBSMap.put(prl.Product2__c, toooMap.clone());
                    }
                }
 
                // 产品的产品注册证是"不要"状态 不需要匹配注册证 20181225
                if (prl.Product2__r.SFDA_Approbated_Status__c == '不要' 
                        
                        || prl.Product_Register__r.MedPrdClass__c == '1') {
                     proNotNeedList.add(prl.Product2__c);
                }
            }
            system.debug('proBSMap +++++++' + proBSMap);
            for (String qli : proMap.keySet()) {
 
                String proId = qli;
                String proName = proMap.get(proId);
 
                if (proId != null) {
                    Boolean haveBS = false;
 
                    // 产品属于非医疗,不用判断经营范围
                    if (proNotNeedList.contains(proId)) {
                        haveBS = true;
                        continue;
                    }
 
                    if (proBSMap.get(proId) == null) {
                        error = true;
                        errormessage += '产品"' + proName + '"没有有效的产品注册证。\n';
                        retMap.put(proId, '1');
                        continue;
                    }
 
 
                    if (proBSMap.get(proId) != null) {
                        system.debug('111 +++++++' + proBSMap);
                        Map<String,String> midMap = new Map<String,String>();
                        midMap = proBSMap.get(proId);
 
                        for (String str : midMap.keySet()) {
                            // LHJ 20200711 Start
                            String strMidMap = midMap.get(str);
                            List<String> proRegList = new List<String>();
                            proRegList.addAll(strMidMap.split(';'));
                            // LHJ 20200711 End
                            for(String strReg:proRegList){
                                // 被定义为"6815A"的产品类别,经营范围中必须有6815或者6815A外才可以匹配。
                                if (strReg.length() == 6) {
                                
                                    for(String strAg:agListA){
                                        if(strAg.length() >= 5 && midMap.get(str.substring(0, str.length() - 1)) == strAg.substring(0,5)){
                                            proAllBSstrMap.get(proId).add(str.substring(0, str.length() - 1));
                                            haveBS = true;
                                            break;
                                        }
                                    }
 
                                    /*if (agListA.contains(midMap.get(str.substring(0, str.length() - 1)))) {
                                        proAllBSstrMap.get(proId).add(str.substring(0, str.length() - 1));
                                        haveBS = true;
                                    }*/
                                } else {
                                    system.debug('agList +++++++' + agList);
                                    system.debug('midMap +++++++' + strReg);
 
                                    if (agList.contains(strReg)) {
 
                                        //产品类别,匹配到经销商类别
                                        proAllBSstrMap.get(proId).add(str);
                                        haveBS = true;
                                        break;
                                    }
                                }  
                            }
                            
                                                      
                        }
                    }
 
                    if (!haveBS) {
                        error = true;
                        errormessage += '第一经销商的经营范围中不包含产品" ' + proName + '"。\n';
                        retMap.put(proId, '2');
                    }
                }
            }
        }
 
        if (retMap.isEmpty() == false) {
            //return errormessage;
            
        } else {
            if (OppId != '') {
                // 更新所有注册证信息
                String strRegister_alleffective;
                List<Product2> pro2List = new List<Product2>();
                Map<String, Product2> pro2Map = new Map<String, Product2>();
                List<OpportunityLineItem> updList = new List<OpportunityLineItem>();
                List<OpportunityLineItem> OppItemList = [select id, Id__c, Register_alleffective__c,PricebookEntry.Product2Id
                                                           From OpportunityLineItem 
                                                          Where OpportunityId = :OppId];
                // 非医疗或1类产品,取得
                if (proNotNeedList != null && proNotNeedList.size() > 0) {
                    pro2List = [select id, SFDA_Approbated_Status__c, RegisterNo_ALL__c from Product2 where Id in :proNotNeedList];
                }
                for (Product2 pro2 : pro2List) {
                    pro2Map.put(pro2.Id, pro2);
                }
 
                for (OpportunityLineItem ol : OppItemList) {
                    if (proAllBSstrMap.containsKey(ol.PricebookEntry.Product2Id)) {
                        ol.Register_alleffective__c = '';
                        for(String str: proAllBSstrMap.get(ol.PricebookEntry.Product2Id)) {
 
                            if (String.isNotBlank(ol.Register_alleffective__c)) {
                                ol.Register_alleffective__c += ';' + str;
                            } else {
                                ol.Register_alleffective__c = str;
                            }
                        }
                        // 非医疗或1类产品
                        if (pro2Map.containskey(ol.PricebookEntry.Product2Id)) {
                            if (pro2Map.get(ol.PricebookEntry.Product2Id).SFDA_Approbated_Status__c == '不要') {
                                ol.Register_alleffective__c = 'FYL';
                            } else {
                                ol.Register_alleffective__c = pro2Map.get(ol.PricebookEntry.Product2Id).RegisterNo_ALL__c;
                            }
                        }
                    }
                    updList.add(ol);
                }
                if (updList.size() > 0)  update updList;
            }
        }
        //如果不满足阿西赛多的条件,进入这个变量 20200821 ljh 
        return retMap;
    }
    public class UpdateResult {
        @AuraEnabled public String recordId {get;set;}
        @AuraEnabled public Boolean success {get;set;}
        @AuraEnabled public List<String> errors {get;set;}
    }
    public class InitData{
 
      
    }
    // @AuraEnabled
    // public static void test(){
    //     Integer i=0;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
        
    // }
}