GWY
2022-05-21 a3460549533111815e7f73d6cef601a58031525d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
@RestResource(urlMapping = '/SBG203/*')
global with sharing class SBG203Rest {
 
    global class GeDatas {
        public NFMUtil.Monitoring Monitoring;
        public GeData[] Partners;
    }
    //合作伙伴抬头层级
    global class GeData {
        public License_Info[] License_Info;
        public Consignee_Info[] Consignee_Info;
        public Contract_Info[] Contract_Info;
        public Authorized_Info[] Authorized_Info;
 
        public String AgentType; //经销商分类
        public String CompanyName; //企业名称
        public String CompanyEnglishName; //企业名称(英文)
        public String DealerTradeType; //代理商贸易类型
        public String Mobilephone; //合作伙伴手机
        public String ProductSegment; //产品分类
        public String KeyAccount; //关键客户
        public String ExportRegulatedCustomer; //出口限制用户
        public String MarketVerticals; //市场分类
        public String Industry; //行业
        public String Use; //用途
        public String SubUseEnterprise; //全国企业用户的SubUse
        public String BPCode; //合作伙伴代码(BS课税,其他分野公用)
        public String BPCodeforeign; //免税
        public String BPType; //合作伙伴类型
        public String RegisterAddress; //住所(等同注册地址)
        public String RegionName; //省别
        public String City; //市
        public String Phone; //合作伙伴电话
        public String Postal; //合作伙伴邮编
        public String Fax; //合作伙伴传真
        public String Region; //省别()
        public String ApplyType;
        // 1.客户终止(更新客户)。2.新增(创建-客户,联系人,代理商折扣,证照等)。3.变更( 同新增)。4.续约(同新增)。6.协议变更(客户,代理商折扣)。7.证照变更(客户,证照,联系人)。8.收货信息变更(客户,联系人)
        public String STR_SUPPL1; //开票地址(80)
 
        public String VTWEG; //分销渠道
        public String AccountSource; //客户来源
        public Boolean TargetCustomer; //客户类型
        public Boolean IsCOMPO; //COMPO客户
        public String UserType; //用户属性
        public String COMPOSubuse; //Subuse
 
        public String KATR2; //开票类型
        public String CompanyCode; //公司代码
        public String Incorporator; //法定代表人(医院)
        public String Invoice_phone; //开票联系电话
        public String AgentValidFrom;
        public String AgentValidTo;
        public String TaxNo;
        public String BankName;
        public String BankCode;
        public String Agreement1;
        public String Agreement2;
        public String Agreement3;
        public String Agreement4;
        public String Remark;
        public String Z3PLAuthorizedNo;
        public String AuthorizedScope;
        public String AuthorizedVF;
        public String AuthorizedVT;
        public String CNTag;
        public String Z3PLQualityAgrNo;
        public String QualityAgrNoVF;
        public String QualityAgrNoVT;
        public String Z3PLTransAgrNo;
        public String TransAgrVF;
        public String TransAgrVT;
        public String ServiceType;
        public String CustomerService;
        public String OperationProject;
        public String Approver;
        public String ApproveDate;
        public String Comment;
 
        public String other1;
        public String other2;
        public String other3;
    }
 
    // 证照层级
    global class License_Info {
        public Detail_Info[] Detail_Info;
 
        public String LicenseType; //证照类型
        public String BusLicenseNo; //证照号
        public String ValidFrom; //期限效期从
        public String ValidTo; //期限效期至
        public String WarehouseAddress; //库房地址
        public Boolean IfQuantityCtrl; //是否数量管控
        //以下字段暂时用不到
        public String ISSUEDATE1; //证照的发证日期
        public String DiagnosisScope; //诊疗科目
        public String BusinessType; //经营方式
        public String Principal; //企业负责人
        public String BusinessAddress; //经营场所(等同办公地址)
        public String BusinessScope; //经营二类范围
        public String BusinessScope1; //经营三类范围
        public string IssueAuthority; //发证机关
        public String Exclusive; //二类经营范围排除子分类
        public String Exclusive1; //三类经营范围排除子分类
        public String other4;
        public String other5;
        public String other6;
 
    }
 
    global class Detail_Info {
        public String ProSerial; //辐射产品系列
        public String Quantity; //数量
        public String ProName; //装置名称
        public String Category; //活动种类
    }
    // 收货人层级
    global class Consignee_Info {
        public String ContactId; //收货人ID(管理编码)
        public String ContactName; //有效收货人名称
        public String ContactPhone; //收货人电话
        public String PostalCode; //邮政编码
        public String ContactAddress; //有效收货人地址
        public String ContactMobilePhone; //收货人手机
        public String CITY1; //收货人城市
        public String ContactEnglishName; //英文名称
    }
 
    // 销售人层级
    global class Authorized_Info {
        public String SalesAuthorizedNo;
        public String SalesMan;
        public String SalesAuthorizedVF;
        public String SalesAuthorizedVT;
    }
 
    // 合同层级
    global class Contract_Info {
 
        public String ContractStartDate; //合同开始日
        public String ContractEndDate; //合同结束日
        public String DealerRank;
        public String SubUse;
        public String ContractRegion; //负责省
        public String DealerRate; //代理商折扣
 
        //以下字段暂时不会传输数据
        public String TermContractNo; //本期协议编号
        public String ContractOwner; //客户所有人
        public String ContractDecideStartDate; //允许报价期间(开始日)
        public String ContractDecideEndDate; //允许报价期间(结束日)
        public String BusinessAssistant; //营业助理
        public String SalesState; //申请省
        public String SalesShopClass; //经销商合同分类
        public String ContractDepartmentClass; //担当科室分类
        public String AgencyApplicationDay; //特约经销商申请日
        public String AgencyApplicationNo; //特约经销商申请编码
        public String SalesSection; //申请销售课
        public String SelableProduct; //担当产品
        public Double AimPrice; //目标金额
        public Double AimPriceAreaET; //ET目标金额
        public Double AimPriceAreaSP; //SP耗材目标金额
        public Double AimPriceAreaENG; //ENG目标金额
        public Double AimPriceAH; //奥辉目标金额
        public Double AimPriceSP; //SP目标金额
        public Double AimPriceGIR; //GIR目标金额
        public Double DealerdiscountET; //经销商折扣ET
        public Double DealerdiscountENG; //经销商折扣ENG
        public Double DealerdiscountSP; //经销商折扣SP
        public Double DealerdiscountAH; //经销商折扣奥辉
        public Double DealerdiscountBF; //经销商折扣BF
        public Double DealerdiscountENF; //经销商折扣ENF
        public Double DealerdiscountGI; //经销商折扣GI
        public Double DealerdiscountGS; //经销商折扣GS
        public Double DealerdiscountGYN; //经销商折扣GYN
        public Double DealerdiscountOTH; //经销商折扣OTH
        public Double DealerdiscountURO; //经销商折扣URO
        public Double DealerdiscountZF; //经销商折扣政府项目
        public String AimDivision; //目标区分
 
    }
 
    @HttpPost
    global static void execute() {
        // 取得接口传输内容
        String strData = RestContext.request.requestBody.toString();
        GeDatas ges = (GeDatas) JSON.deserializeStrict(strData, GeDatas.class);
 
        if (ges == null) {
            return;
        }
 
        NFMUtil.Monitoring Monitoring = ges.Monitoring;
        if (Monitoring == null) {
            return;
        }
 
        BatchIF_Log__c rowData = NFMUtil.saveRowData(Monitoring, 'SBG203', ges.Partners);
        if (String.isBlank(rowData.Log__c) == false) {
            executefuture(rowData.Id);
        }
        // JSONを戻す
        RestResponse res = RestContext.response;
        res.addHeader('Content-Type', 'application/json');
        res.statusCode = 200;
        String jsonResponse = '{"status": "0", "Message":""}';
        res.responseBody = blob.valueOf(jsonResponse);
        return;
    }
 
    @future
    global static void executefuture(String rowData_Id) {
        main(rowData_Id);
    }
 
    global static void main(String rowData_Id) {
        Integer batch_retry_max_cnt = Integer.valueOf(System.Label.batch_retry_max_cnt);
        BatchIF_Log__c rowData = [Select Id, Name, Log__c, ErrorLog__c, Log2__c, Log3__c, Log4__c, Log5__c, Log6__c, Log7__c, Log8__c, Log9__c, Log10__c, Log11__c, Log12__c, MessageGroupNumber__c, retry_cnt__c from BatchIF_Log__c where RowDataFlg__c = true and Id =: rowData_Id];
        String logstr = rowData.MessageGroupNumber__c + ' start\n';
        BatchIF_Log__c iflog = new BatchIF_Log__c();
        iflog.Type__c = 'SBG203';
        iflog.MessageGroupNumber__c = rowData.MessageGroupNumber__c;
        iflog.Log__c = logstr;
        iflog.ErrorLog__c = '';
        insert iflog;
 
        String rowDataStr = NFMUtil.getRowDataStr(rowData);
        List < GeData > geDataList = (List < GeData > ) JSON.deserialize(rowDataStr, List < GeData > .class);
        if (geDataList == null || geDataList.size() == 0) {
            return;
        }
        //客户转换表
        Map < String, String > transferMap = NFMUtil.BatchIF_Transfer('Account');
 
        //证照转换表
        Map < String, String > transferLicenseTypeMap = NFMUtil.BatchIF_Transfer('License_Information__c');
 
        //证照明细转换表
        Map < String, String > certificationDetailTransferMap = NFMUtil.BatchIF_Transfer('CertificationDetails__c');
 
        // 查找客户的全部记录类型
        Map < String, String > accountRecordTypeMap = getRecordTypeMap();
        Savepoint sp = Database.setSavepoint();
        try {
 
            //满足条件的GeData
            List < GeData > satisfyGeData = SatisfyGeData(geDataList, iflog);
 
            //根据bPCodeList查找存在的Account
            Map < String, Account > existAccountMap = ExistAccount(satisfyGeData, iflog);
 
            List < Account > upsertAccountList = new List < Account > ();
            Map < String, String > licenseChangeNotNullMap = new Map < String, String > ();
            if (satisfyGeData.size() > 0) {
 
                //考虑是否是BS经销商
                //存在的BS经销商
                Map < String, GeData > existBSAccountMap = new Map < String, GeData > ();
                //不存在的BS经销商
                Map < String, GeData > notExistBSAccountMap = new Map < String, GeData > ();
                //保存BS经销商
                Map < String, Account > insertBSAccountMap = new Map < String, Account > ();
                Boolean executeFlag = true;
                for (GeData gda: satisfyGeData) {
 
                    Account accountInfo = new Account();
 
                    String transformProductSegment = gda.ProductSegment == 'LS' ? 'BS' : gda.ProductSegment; //gda.ProductSegment.split(':')[0];//FieldTransformation(gda.BPCode,transferMap, 'ProductSegment__c', gda.ProductSegment, iflog,'ProductSegment');
 
                    if (existAccountMap.containsKey(gda.BPCode) && gda.BPType == '23' && transformProductSegment == 'BS') {
                        //BS代理商-BPCode(课税)存在
 
                        if (existAccountMap.containsKey(gda.BPCodeforeign)) { //变更BS代理商
                            //BS代理商-BPCode(免税)存在
 
                            //获取课税代理商Id
                            existBSAccountMap.put(existAccountMap.get(gda.BPCode).Id + ';' + gda.BPCode + ';' + existAccountMap.get(gda.BPCode).Name, gda);
                            //获取免税代理商Id
                            existBSAccountMap.put(existAccountMap.get(gda.BPCodeforeign).Id + ';' + gda.BPCodeforeign + ';' + existAccountMap.get(gda.BPCodeforeign).Name, gda);
                            //获取母公司Id
                            existBSAccountMap.put(existAccountMap.get(gda.BPCode).ParentId + ';' + gda.BPCode + ':' + gda.BPCodeforeign + ';' + existAccountMap.get(gda.BPCode).Parent.Name, gda);
                            //licenseChangeNotNullMap.put(gda.BPCodeforeign,gda.ApplyType);
                            licenseChangeNotNullMap.put(gda.BPCode + ':' + gda.BPCodeforeign, gda.ApplyType);
                        } else {
                            //BS代理商-BPCode(课税)存在,BS代理商-BPCodeforeign(免税)不存在
                            iflog.ErrorLog__c += 'BPCode[ ' + gda.BPCode + ' ] of BPCodeforeign is inexistence,This data is skipped.\n';
                            executeFlag = false;
                            continue;
                        }
                        logstr = GetLog(logstr, gda);
 
                    } else if (gda.BPType == '23' && transformProductSegment == 'BS') {
                        //新增BS代理商
                        accountInfo.BS_Children_Code__c = gda.BPCode + ':' + gda.BPCodeforeign;
                        accountInfo = AccountValueAssignment(gda, accountInfo, transferMap, accountRecordTypeMap, iflog);
                        String dealerType = FieldTransformation(gda.BPCode, transferMap, 'Dealer_Type__c', gda.AgentType, iflog, 'AgentType');
 
                        if (!dealerType.equals('Invalid')) {
                            accountInfo.Dealer_Type__c = dealerType;
                            //新增BS代理商中的母公司
                            insertBSAccountMap.put(accountInfo.BS_Children_Code__c, accountInfo);
                            //新增BS代理商中的子公司
                            notExistBSAccountMap.put(gda.BPCode, gda);
                            notExistBSAccountMap.put(gda.BPCodeforeign, gda);
                            notExistBSAccountMap.put(accountInfo.BS_Children_Code__c, gda);
                        } else {
                            executeFlag = false;
                            continue;
                        }
                        logstr = GetLog(logstr, gda);
                    }
                }
 
                //变更BS代理商
                if (existBSAccountMap.size() > 0) {
 
                    List < Account > updateBsChildrenAccountList = new List < Account > ();
                    for (String accountId: existBSAccountMap.keySet()) {
 
                        GeData gda = existBSAccountMap.get(accountId);
 
                        Account accountInfo = new Account();
                        accountInfo.Id = accountId.split(';')[0];
                        accountInfo = AccountValueAssignment(gda, accountInfo, transferMap, accountRecordTypeMap, iflog);
                        String dealerType = FieldTransformation(gda.BPCode, transferMap, 'Dealer_Type__c', gda.AgentType, iflog, 'AgentType');
 
                        if (dealerType.equals('Invalid')) {
                            executeFlag = false;
                            continue;
                        }
                        accountInfo.Dealer_Type__c = dealerType;
                        String bpCodeDutyFreeCode = gda.BPCode + ':' + gda.BPCodeforeign;
                        if (accountId.split(';')[1] == bpCodeDutyFreeCode) { //BS代理商母公司更名
                            accountInfo.Name = gda.CompanyName + '(P)';
                        } else { //BS代理商子公司更名
 
                            accountInfo.Name = gda.CompanyName;
                        }
                        updateBsChildrenAccountList.add(accountInfo);
 
                    }
 
                    //跳过SBG001接口
                    if (updateBsChildrenAccountList.size() > 0) {
                        NFMUtil.EscapeSBG001TriggerHandler = true;
                        update updateBsChildrenAccountList;
                    }
 
                }
                //新增BS代理商
                if (insertBSAccountMap.size() > 0) {
                    insert insertBSAccountMap.values();
 
                    List < Account > bsAccountPList = [Select Id, Name, BS_Children_Code__c from Account where BS_Children_Code__c In: insertBSAccountMap.keySet()];
 
                    if (bsAccountPList.size() > 0) {
 
                        List < Account > insertBsChildrenAccountList = new List < Account > ();
 
                        for (Account acc: bsAccountPList) {
 
                            String parentId = acc.Id;
                            String parentName = acc.Name;
                            String bsChildrenCode = acc.BS_Children_Code__c;
 
                            if (notExistBSAccountMap.containsKey(bsChildrenCode)) {
                                Integer no = 0;
                                for (String bpCode: bsChildrenCode.split(':')) {
 
                                    GeData gda = notExistBSAccountMap.get(bpCode);
                                    Account accountInfo = new Account();
 
                                    accountInfo.ManagementCode_Ext__c = bpCode;
                                    accountInfo.ParentId = parentId;
                                    accountInfo = AccountValueAssignment(gda, accountInfo, transferMap, accountRecordTypeMap, iflog);
                                    if (no == 0) {
                                        //课税代理商
                                        accountInfo.DealerTradeType__c = NFMUtil.getMapValue(transferMap, 'DealerTradeType__c', '11', iflog);
                                    } else {
                                        //免税代理商
                                        accountInfo.DealerTradeType__c = NFMUtil.getMapValue(transferMap, 'DealerTradeType__c', '12', iflog);
                                    }
 
                                    insertBsChildrenAccountList.add(accountInfo);
                                    no++;
 
                                }
                            }
                        }
 
                        if (insertBsChildrenAccountList.size() > 0) {
                            NFMUtil.EscapeSBG001TriggerHandler = true;
                            insert insertBsChildrenAccountList;
                        }
 
                    }
                }
 
                //新增或变更其它类型的客户
                for (GeData gda: satisfyGeData) {
                    String transformProductSegment = gda.ProductSegment == 'LS' ? 'BS' : gda.ProductSegment;
                    if (gda.BPType == '23' && transformProductSegment == 'BS') {
                        continue;
                    }
 
                    Account accountInfo = new Account();
 
                    if (existAccountMap.containsKey(gda.BPCode)) {
 
                        accountInfo = existAccountMap.get(gda.BPCode);
                        licenseChangeNotNullMap.put(gda.BPCode, gda.ApplyType);
                    } else {
 
                        accountInfo.ManagementCode_Ext__c = gda.BPCode;
                    }
 
                    if (gda.BPType == '23') {
                        String dealerType = FieldTransformation(gda.BPCode, transferMap, 'Dealer_Type__c', gda.AgentType, iflog, 'AgentType');
 
                        if (dealerType.equals('Invalid')) {
                            executeFlag = false;
                        }
                    }
 
                    if (gda.BPType == '22') {
                        accountInfo.RecordTypeId = '01228000000TF3Q'; //外贸公司
                    } else if (gda.BPType == '23') {
 
                        accountInfo.DealerTradeType__c = NFMUtil.getMapValue(transferMap, 'DealerTradeType__c', '13', iflog);
 
                    } else if (gda.BPType == '24') { //全国企业用户(大客户)
 
                        if ('42'.equals(gda.VTWEG)) {
                            Map < String, String > subuseMap = getSubuseMap();
 
                            if (!subuseMap.containsKey(gda.COMPOSubuse)) {
                                iflog.ErrorLog__c += 'BPCode[ ' + gda.BPCode + ' ] of  COMPOSubuse  [ ' + gda.COMPOSubuse + ' ]' + ' is  Invalid,This data is skipped.\n';
                                executeFlag = false;
                            }
 
                        } else {
                            String keyAccount = FieldTransformation(gda.BPCode, transferMap, 'KeyAccount__c', gda.KeyAccount, iflog, 'KeyAccount');
 
                            if (keyAccount.equals('Invalid')) {
                                executeFlag = false;
                            }
                            String marketVerticals = FieldTransformation(gda.BPCode, transferMap, 'MarketVerticals__c', gda.MarketVerticals, iflog, 'MarketVerticals');
 
                            if (marketVerticals.equals('Invalid')) {
                                executeFlag = false;
                            }
                            String use = FieldTransformation(gda.BPCode, transferMap, 'Use__c', gda.Use, iflog, 'Use');
                            if (use.equals('Invalid')) {
                                executeFlag = false;
                            }
 
                            String industry = FieldTransformation(gda.BPCode, transferMap, 'IndustryC__c', gda.Industry, iflog, 'Industry');
                            if (industry.equals('Invalid')) {
                                executeFlag = false;
                            }
                        }
 
 
                    }
 
                    if (!executeFlag) {
                        continue;
                    }
                    accountInfo = AccountValueAssignment(gda, accountInfo, transferMap, accountRecordTypeMap, iflog);
                    upsertAccountList.add(accountInfo);
                    logstr = GetLog(logstr, gda);
                }
 
                if (upsertAccountList.size() > 0) {
                    NFMUtil.EscapeSBG001TriggerHandler = true;
                    upsert upsertAccountList;
                    for (Account acc: upsertAccountList) {
                        if (acc.NationalEnterpriseUser__c) {
                            UpsertAccountTeamMember(acc.Id);
                        }
                    }
                }
                existAccountMap = ExistAccount(satisfyGeData, iflog); //new Map<String,Account>();
                Map < String, String > licenseChangeMap = LicenseChanges(existAccountMap); //new Map<String,String>();
 
                if (executeFlag) {
 
                    if (licenseChangeMap.size() > 0 && licenseChangeNotNullMap.size() > 0) {
                        //变更
                        LicenseChange(licenseChangeMap, licenseChangeNotNullMap);
                    }
 
                    //合同相关(代理商折扣)
                    SatisfyDealerDiscount(satisfyGeData, existAccountMap, iflog);
 
                    //证照相关
                    SatisfyLicenseInfo(satisfyGeData, existAccountMap, transferLicenseTypeMap, certificationDetailTransferMap, iflog);
 
                    //联系人相关
                    SatisfyConsigneeInfoList(satisfyGeData, existAccountMap, iflog);
 
                }
 
            }
 
            logstr += '\nend';
            rowData.retry_cnt__c = 0;
 
 
        } catch (Exception ex) {
            Database.rollback(sp);
            System.debug(Logginglevel.ERROR, 'SBG203_' + rowData.MessageGroupNumber__c + ':' + ex.getMessage());
            System.debug(Logginglevel.ERROR, 'SBG203_' + rowData.MessageGroupNumber__c + ':' + ex.getStackTraceString());
            logstr += '\n' + ex.getMessage();
            iflog.ErrorLog__c = ex.getMessage() + '\n' + ex.getStackTraceString() + '\n' + iflog.ErrorLog__c;
 
            if (rowData.retry_cnt__c == null) rowData.retry_cnt__c = 0;
            if (rowData.retry_cnt__c < batch_retry_max_cnt) {
                rowData.retry_cnt__c++;
                LogAutoSendSchedule.assignOneMinute();
            }
            if (rowData.retry_cnt__c >= batch_retry_max_cnt) {
                rowData.ErrorLog__c = ex.getMessage() + '\n' + ex.getStackTraceString() + '\n' + rowData.ErrorLog__c + '错误次数已经超过自动收信设定的最大次数,请手动收信';
            }
        }
        update rowData;
        iflog.Log__c = logstr;
        if (iflog.Log__c.length() > 131072) {
            iflog.Log__c = iflog.Log__c.subString(0, 131065) + ' ...';
        }
        if (iflog.ErrorLog__c.length() > 32768) {
            iflog.ErrorLog__c = iflog.ErrorLog__c.subString(0, 32760) + ' ...';
        }
        update iflog;
    }
    //证照变更
    public static void LicenseChange(Map < String, String > licenseChangeMap, Map < String, String > licenseChangeNotNullMap) {
 
        List < String > deleteExistDealerDiscountList = new List < String > ();
        List < String > deleteExistLicenseInformationList = new List < String > ();
        List < String > deleteExistContactList = new List < String > ();
        for (String managementCode: licenseChangeMap.keySet()) {
 
            if (licenseChangeNotNullMap.containsKey(managementCode)) {
 
                String accountId = licenseChangeMap.get(managementCode);
                //证照变更
                if (licenseChangeNotNullMap.get(managementCode) == '2' || licenseChangeNotNullMap.get(managementCode) == '3' ||
                    licenseChangeNotNullMap.get(managementCode) == '4' || licenseChangeNotNullMap.get(managementCode) == '7') {
 
                    deleteExistLicenseInformationList.add(accountId);
                }
            }
        }
 
        List < License_Information__c > deleteLicenseInformationList = null;
        if (deleteExistLicenseInformationList.size() > 0) {
 
            deleteLicenseInformationList = [select Id from License_Information__c
                where LicenseAndAccount__c In: deleteExistLicenseInformationList
            ];
            if (deleteLicenseInformationList.size() > 0) {
                delete deleteLicenseInformationList;
            }
        }
 
    }
    //代理商折扣
    public static void SatisfyDealerDiscount(List < GeData > satisfyGeData, Map < String, Account > existAccountMap, BatchIF_Log__c iflog) {
 
        //满足要求的代理商折扣
        Map < String, Contract_Info > satisfyContractInfoMap = new Map < String, Contract_Info > ();
        List < Dealer_Discount__c > dealerDiscountList = new List < Dealer_Discount__c > ();
        Map < String, Dealer_Discount__c > dealerDiscountMap = new Map < String, Dealer_Discount__c > ();
        //BS代理商免税代理商相关的代理商折扣
        Map < String, GeData > satisfy2GeData = new Map < String, GeData > ();
        List < String > bpCodeList = new List < String > ();
        Map < String, String > bpCodeMap = new Map < String, String > ();
        //验证代理商折扣必填字段
        for (GeData gda: satisfyGeData) {
 
            // if (gda.BPType != '23') {
            //     continue;
            // }
            if (!(gda.BPType == '23' || (gda.BPType == '24' && '42'.equals(gda.VTWEG)))) {
                continue;
            }
            if (gda.Contract_Info == null) {
                continue;
            }
            for (Contract_Info contractInfo: gda.Contract_Info) {
                if (!String.isBlank(gda.ApplyType) && gda.ApplyType != '2' && gda.ApplyType != '3' && gda.ApplyType != '4' && gda.ApplyType != '6') {
                    continue;
                }
                if (!existAccountMap.containsKey(gda.BPCode)) {
                    iflog.ErrorLog__c += ' This BPCode[' + gda.BPCode + '] is not exist,This data is skipped .\n';
                    continue;
                }
 
                String productSegment = existAccountMap.get(gda.BPCode).ProductSegment__c;
                if (productSegment == 'BS') {
 
                    if (String.isBlank(contractInfo.ContractRegion)) {
                        continue;
                    }
                    bpCodeMap.put(gda.BPCode, gda.BPCode);
                    bpCodeMap.put(gda.BPCodeforeign, gda.BPCodeforeign);
                    satisfyContractInfoMap.put(gda.BPCode + productSegment + contractInfo.ContractRegion, contractInfo);
                } else if (productSegment == 'IE') {
 
                    satisfyContractInfoMap.put(gda.BPCode + productSegment + contractInfo.DealerRank, contractInfo);
                    bpCodeMap.put(gda.BPCode, gda.BPCode);
                } else if (productSegment == 'RVI') {
 
                    // String processData = contractInfo.SubUse;
                    // // String latitudeValue = gda.BPCode + 'Other';
                    // if (String.isNotBlank(processData)) {
 
                    //     if ( processData.indexOf('Civil Aviation') != -1) {
                    //         // latitudeValue = gda.BPCode + 'Civil Aviation';
                    //         bpCodeMap.put(gda.BPCode, gda.BPCode);
                    //         if ( processData.indexOf(';') != -1 ) {
                    //             // latitudeValue = gda.BPCode + 'Other';
                    //             bpCodeMap.put(gda.BPCode, gda.BPCode);
                    //         }
                    //     } else {
                    //         bpCodeMap.put(gda.BPCode, gda.BPCode);
                    //     }
                    //     satisfyContractInfoMap.put(gda.BPCode + productSegment + contractInfo.SubUse, contractInfo);
                    // }
                    bpCodeMap.put(gda.BPCode, gda.BPCode);
                    satisfyContractInfoMap.put(gda.BPCode + productSegment + contractInfo.SubUse, contractInfo);
                } else if (productSegment == 'NDT' || productSegment == 'ANI') {
                    bpCodeMap.put(gda.BPCode, gda.BPCode);
                    satisfyContractInfoMap.put(gda.BPCode + productSegment, contractInfo);
                }
                satisfy2GeData.put(gda.BPCode, gda);
 
            }
        }
 
        Map < String, Dealer_Discount__c > existDealerDiscountMap = getExistDealerDiscountMap(bpCodeMap.values());
        Map < String, String > satisfyBsDealerDiscountMap = new Map < String, String > ();
 
        //Boolean bSforeignFlag = false;
        Boolean bSFlag = false;
        for (GeData gda: satisfy2GeData.values()) {
 
            //Bs免税代理商相关代理商折扣字段的赋值
            for (Contract_Info contractInfo: satisfyContractInfoMap.values()) {
 
                if (existAccountMap.containsKey(gda.BPCodeforeign)) {
                    String productSegment = existAccountMap.get(gda.BPCodeforeign).ProductSegment__c;
                    if (productSegment == 'BS') {
 
                        Map < String, Dealer_Discount__c > dealerDiscountBSMap = BS_DealerDiscount(existAccountMap, contractInfo.ContractRegion, existDealerDiscountMap, contractInfo, gda.BPCode, gda.BPCodeforeign, satisfyBsDealerDiscountMap);
                        dealerDiscountMap.putAll(dealerDiscountBSMap);
                    }
 
                }
 
            }
 
            //其它代理商相关代理商折扣字段的赋值
            for (Contract_Info contractInfo: satisfyContractInfoMap.values()) {
 
                String productSegment = existAccountMap.get(gda.BPCode).ProductSegment__c;
                Dealer_Discount__c dealerDiscount;
                if (productSegment == 'IE' || productSegment == 'NDT' || productSegment == 'ANI') {
                    String latitudeValue = gda.BPCode;
                    dealerDiscount = AcquireDealerDiscount(existDealerDiscountMap, latitudeValue, contractInfo, productSegment);
                    dealerDiscount = DealerDiscountFields(contractInfo, dealerDiscount, productSegment, existAccountMap, '', '', gda.BPCode);
                    dealerDiscountList.add(dealerDiscount);
                } else if (productSegment == 'BS') {
                    bSFlag = true;
                    Map < String, Dealer_Discount__c > dealerDiscountBSMap = BS_DealerDiscount(existAccountMap, contractInfo.ContractRegion, existDealerDiscountMap, contractInfo, gda.BPCode, '', satisfyBsDealerDiscountMap);
                    dealerDiscountMap.putAll(dealerDiscountBSMap);
                } else if (productSegment == 'RVI') {
 
                    String processData = contractInfo.SubUse;
                    String latitudeValue = gda.BPCode + 'Other';
                    contractInfo.SubUse = 'Other';
                    if (String.isNotBlank(processData)) {
                        // 包含 Civil Aviation;创建或新增代理商折扣
                        if (processData.indexOf('Civil Aviation') != -1) {
                            latitudeValue = gda.BPCode + 'Civil Aviation';
                            contractInfo.SubUse = 'Civil Aviation';
                            dealerDiscount = AcquireDealerDiscount(existDealerDiscountMap, latitudeValue, contractInfo, productSegment);
                            dealerDiscount = DealerDiscountFields(contractInfo, dealerDiscount, productSegment, existAccountMap, '', '', gda.BPCode);
                            dealerDiscountList.add(dealerDiscount);
                            // 包含';'说明包含 其它的分类;创建或新增代理商折扣
                            if (processData.indexOf(';') != -1) {
                                latitudeValue = gda.BPCode + 'Other';
                                contractInfo.SubUse = 'Other';
                                dealerDiscount = AcquireDealerDiscount(existDealerDiscountMap, latitudeValue, contractInfo, productSegment);
                                dealerDiscount = DealerDiscountFields(contractInfo, dealerDiscount, productSegment, existAccountMap, '', '', gda.BPCode);
                                dealerDiscountList.add(dealerDiscount);
                            }
                            continue;
                        }
                        // else {
                        // contractInfo.SubUse = 'Other';
                        // dealerDiscount = AcquireDealerDiscount(existDealerDiscountMap, latitudeValue, contractInfo, productSegment);
                        // dealerDiscount = DealerDiscountFields(contractInfo, dealerDiscount, productSegment, existAccountMap, '', '', gda.BPCode);
                        // dealerDiscountList.add(dealerDiscount);
                        // }
                    }
                    dealerDiscount = AcquireDealerDiscount(existDealerDiscountMap, latitudeValue, contractInfo, productSegment);
                    dealerDiscount = DealerDiscountFields(contractInfo, dealerDiscount, productSegment, existAccountMap, '', '', gda.BPCode);
                    dealerDiscountList.add(dealerDiscount);
 
                }
            }
        }
        if (dealerDiscountList.size() > 0) {
            upsert dealerDiscountList;
        }
        if (dealerDiscountMap.size() > 0) {
            upsert dealerDiscountMap.values();
        }
        if (bSFlag) {
            deleteDealerDiscount(satisfyBsDealerDiscountMap, existDealerDiscountMap);
        }
 
    }
    //转换前后的值的对比
    public static String FieldTransformation(String bpCode, Map < String, String > transformation, String api, String toConvertStr, BatchIF_Log__c iflog, String receivesField) {
        String afterTransformation = NFMUtil.getMapValue(transformation, api, toConvertStr, iflog);
        if (String.isNotBlank(afterTransformation) && !afterTransformation.equals(toConvertStr)) {
            return afterTransformation;
        } else if (String.isNotBlank(afterTransformation) && afterTransformation.equals(toConvertStr)) {
            return afterTransformation;
        } else {
            iflog.ErrorLog__c += 'BPCode[ ' + bpCode + ' ] of ' + receivesField + ' [ ' + toConvertStr + ' ]' + ' is  Invalid,This data is skipped.\n';
            return 'Invalid';
        }
    }
 
    //BS代理商折扣
    public static Map < String, Dealer_Discount__c > BS_DealerDiscount(Map < String, Account > existAccountMap,
        String contractRegion, Map < String, Dealer_Discount__c > existDealerDiscountMap,
        Contract_Info contractInfo, String bpCode, String bPCodeforeign,
        Map < String, String > satisfyBsDealerDiscountMap) {
        Map < String, Dealer_Discount__c > result = new Map < String, Dealer_Discount__c > ();
        String code = String.isBlank(bPCodeforeign) ? bpCode : bPCodeforeign;
 
        if (contractRegion.indexOf(';') != -1) {
            for (String region: contractRegion.split(';')) {
                if (String.isNotBlank(region)) {
                    Dealer_Discount__c dealerDiscount;
                    String latitudeValue = code + region;
                    dealerDiscount = AcquireDealerDiscount(existDealerDiscountMap, latitudeValue, contractInfo, 'BS');
                    dealerDiscount = DealerDiscountFields(contractInfo, dealerDiscount, 'BS', existAccountMap, bPCodeforeign, region, bpCode);
                    satisfyBsDealerDiscountMap.put(latitudeValue, latitudeValue);
                    result.put(latitudeValue, dealerDiscount);
                }
            }
        } else if (String.isNotBlank(contractRegion)) {
 
            Dealer_Discount__c dealerDiscount;
            String latitudeValue = code + contractRegion;
 
            dealerDiscount = AcquireDealerDiscount(existDealerDiscountMap, latitudeValue, contractInfo, 'BS');
            dealerDiscount = DealerDiscountFields(contractInfo, dealerDiscount, 'BS', existAccountMap, bPCodeforeign, contractRegion, bpCode);
            satisfyBsDealerDiscountMap.put(latitudeValue, latitudeValue);
            result.put(latitudeValue, dealerDiscount);
        }
        return result;
    }
 
    //代理商其它字段的赋值
    public static Dealer_Discount__c DealerDiscountFields(Contract_Info contractInfo, Dealer_Discount__c dealerDiscount, String productSegment,
        Map < String, Account > existAccountMap, String bPCodeforeign, String region, String bpCode) {
        String code = String.isNotBlank(bPCodeforeign) ? bPCodeforeign : bpCode;
        dealerDiscount.Product_Segment__c = productSegment;
        dealerDiscount.DimensionField1__c = 'Dealer_Code__c';
        dealerDiscount.DimensionValue1__c = code;
        dealerDiscount.DealerDiscountAccount__c = existAccountMap.get(code).Id;
        dealerDiscount.Name = productSegment + '-' + existAccountMap.get(code).Name;
        if (productSegment == 'BS') {
            dealerDiscount.DimensionField2__c = 'Province__c';
            dealerDiscount.DimensionValue2__c = region;
            if (String.isNotBlank(bPCodeforeign)) {
                dealerDiscount.DimensionValue1__c = code;
                dealerDiscount.DealerDiscountAccount__c = existAccountMap.get(code).Id;
                dealerDiscount.Name = productSegment + '-' + existAccountMap.get(code).Name;
            }
        } else if (productSegment == 'RVI') {
            dealerDiscount.DimensionField2__c = 'Subuse__c';
            dealerDiscount.DimensionValue2__c = contractInfo.SubUse == 'Civil Aviation' ? 'Civil Aviation' : 'Other';
 
        }
 
        return dealerDiscount;
    }
    //代理商折扣日期相关字段的赋值
    public static Dealer_Discount__c AcquireDealerDiscount(Map < String, Dealer_Discount__c > existDealerDiscountMap, String latitudeValue, Contract_Info contractInfo, String productSegment) {
        system.debug('AcquireDealerDiscount--->Start');
        Dealer_Discount__c dealerDiscount = new Dealer_Discount__c();
        Date dateFrom = String.isBlank(contractInfo.ContractStartDate) ? Date.today() : NFMUtil.parseStr2Date(contractInfo.ContractStartDate, false);
        Date dateTo = NFMUtil.parseStr2Date('20991231', true);
 
        Decimal dealerRate; //(off:直接存;ON:100-Decimal.valueOf(contractInfo.DealerRate))
        //if ( productSegment == 'NDT' || productSegment == 'ANI') {
        //  dealerRate = String.isBlank(contractInfo.DealerRate) ? 100 : Decimal.valueOf(contractInfo.DealerRate);
        //} else
        //2020-1-2 XHL Start
        if (productSegment == 'BS') {
            dealerRate = String.isBlank(contractInfo.DealerRate) ? 100 : 100 - Decimal.valueOf(contractInfo.DealerRate);
        } else {
            dealerRate = String.isBlank(contractInfo.DealerRate) ? 100 : Decimal.valueOf(contractInfo.DealerRate);
        }
        //2020-1-2 XHL End
        if (existDealerDiscountMap.containsKey(latitudeValue)) {
            dealerDiscount = existDealerDiscountMap.get(latitudeValue);
            Date statusOneStart = dealerDiscount.Valid_Date_From__c;
            Date statusOneEnd = dealerDiscount.Valid_Date_To__c;
            Date statusTwoStart = dealerDiscount.Valid_Date_From2__c;
            Date statusTwoEnd = dealerDiscount.Valid_Date_To2__c;
 
            if (statusOneStart == null || statusOneEnd < Date.today() || statusOneStart == dateFrom) {
                dealerDiscount.Valid_Date_To2__c = statusTwoEnd != null ? dateFrom.addDays(-1) : null;
                dealerDiscount.Valid_Date_From__c = dateFrom;
                dealerDiscount.Valid_Date_To__c = dateTo;
                dealerDiscount.Dealer_Rank__c = dealerRate;
            } else if (statusTwoStart == null || statusTwoEnd < Date.today() || statusTwoStart == dateFrom) {
                //20200108---Add---Start-折扣开始日2为空,折扣开始日1为未来日期,更新折扣1
                if (statusTwoStart == null && statusOneStart > Date.today()) {
                    dealerDiscount.Valid_Date_From__c = dateFrom;
                    dealerDiscount.Valid_Date_To__c = dateTo;
                    dealerDiscount.Dealer_Rank__c = dealerRate;
                    //20200108---Add---End
                } else {
                    dealerDiscount.Valid_Date_To__c = dateFrom.addDays(-1);
                    dealerDiscount.Valid_Date_From2__c = dateFrom;
                    dealerDiscount.Valid_Date_To2__c = dateTo;
                    dealerDiscount.Dealer_Rank2__c = dealerRate;
                }
            } else {
                if ((statusOneStart <= Date.today()) && (Date.today() <= statusOneEnd)) {
                    dealerDiscount.Valid_Date_To__c = dateFrom.addDays(-1);
                    dealerDiscount.Valid_Date_From2__c = dateFrom;
                    dealerDiscount.Valid_Date_To2__c = dateTo;
                    dealerDiscount.Dealer_Rank2__c = dealerRate;
                } else if ((statusTwoStart <= Date.today()) && (Date.today() <= statusTwoEnd)) {
                    dealerDiscount.Valid_Date_To2__c = dateFrom.addDays(-1);
                    dealerDiscount.Valid_Date_From__c = dateFrom;
                    dealerDiscount.Valid_Date_To__c = dateTo;
                    dealerDiscount.Dealer_Rank__c = dealerRate;
                }
            }
        } else {
            dealerDiscount.Valid_Date_From__c = dateFrom;
            dealerDiscount.Valid_Date_To__c = dateTo;
            dealerDiscount.Dealer_Rank__c = dealerRate;
        }
        dealerDiscount.IsFromSPO__c = true;
        return dealerDiscount;
    }
 
    public static void SatisfyLicenseInfo(List < GeData > satisfyGeData, Map < String, Account > existAccountMap, Map < String, String > transferLicenseTypeMap, Map < String, String > certificationDetailTransferMap, BatchIF_Log__c iflog) {
        system.debug('SatisfyLicenseInfo---Start');
        //证照层级
 
        List < License_Info > satisfyLicenseInfoList = new List < License_Info > ();
        List < License_Information__c > licenseInformationList = new List < License_Information__c > ();
        List < Account > upsertAccountBusinesslicense = new List < Account > ();
        List < CertificationDetails__c > certificationDetailsList = new List < CertificationDetails__c > ();
        for (GeData gda: satisfyGeData) {
            if (gda.License_Info == null) {
                continue;
            }
            satisfyLicenseInfoList = new List < License_Info > ();
            for (License_Info licenseInfo: gda.License_Info) {
                if (!String.isBlank(gda.ApplyType) && gda.ApplyType != '2' && gda.ApplyType != '3' && gda.ApplyType != '4' && gda.ApplyType != '7') {
                    continue;
                }
                if (String.isBlank(licenseInfo.LicenseType) || String.isBlank(licenseInfo.BusLicenseNo)) {
                    continue;
                }
                satisfyLicenseInfoList.add(licenseInfo);
            }
            if (satisfyLicenseInfoList.size() > 0) {
 
                for (License_Info licenseInfo: satisfyLicenseInfoList) {
                    License_Information__c licenseInformation = new License_Information__c();
                    String licenseType = FieldTransformation(gda.BPCode, transferLicenseTypeMap, 'LicenseType__c', licenseInfo.LicenseType, iflog, 'LicenseType');
                    if (licenseType.equals('Invalid')) {
                        continue;
                    }
 
                    licenseInformation.LicenseType__c = licenseType;
                    licenseInformation.BusinessLicense__c = licenseInfo.BusLicenseNo;
                    licenseInformation.ValidFrom__c = NFMUtil.parseStr2Date(licenseInfo.ValidFrom, false);
                    licenseInformation.ValidTo__c = NFMUtil.parseStr2Date(licenseInfo.ValidTo, false);
                    String warehouseAddress = licenseInfo.WarehouseAddress;
                    if (String.isNotBlank(warehouseAddress)) {
                        if (warehouseAddress.endsWith(';') || warehouseAddress.endsWith(';')) {
                            licenseInformation.StorageAddress__c = warehouseAddress.substring(0, warehouseAddress.length() - 1);
                        } else {
                            licenseInformation.StorageAddress__c = warehouseAddress;
                        }
                    } else {
                        licenseInformation.StorageAddress__c = warehouseAddress;
                    }
 
 
                    String productSegment = gda.ProductSegment == 'LS' ? 'BS' : gda.ProductSegment;
                    if (gda.BPType == '23' && productSegment == 'BS') {
                        licenseInformation.LicenseAndAccount__c = existAccountMap.get(gda.BPCode).ParentId;
                        licenseInformation.Name = existAccountMap.get(gda.BPCode).Parent.Name + licenseType;
                    } else {
                        licenseInformation.LicenseAndAccount__c = existAccountMap.get(gda.BPCode).Id;
                        licenseInformation.Name = existAccountMap.get(gda.BPCode).Name + licenseType;
                    }
                    //辐射安全许可证
                    if ('08'.equals(licenseInfo.LicenseType)) {
                        licenseInformation.IfQuantityCtrl__c = licenseInfo.IfQuantityCtrl;
                        if (licenseInfo.Detail_Info != null) {
 
                            certificationDetailsList = SatisfyLicenseDetails(gda.BPCode, licenseInfo.Detail_Info, certificationDetailTransferMap, iflog);
                        }
                    }
                    if ('04'.equals(licenseInfo.LicenseType)) {
 
                        Account acc = new Account();
                        acc.Id = licenseInformation.LicenseAndAccount__c;
                        acc.Business_license__c = licenseInfo.BusLicenseNo;
                        upsertAccountBusinesslicense.add(acc);
                    }
                    licenseInformationList.add(licenseInformation);
                }
            }
 
        }
 
        if (licenseInformationList.size() > 0) {
            upsert licenseInformationList;
            if (certificationDetailsList.size() > 0) {
                insertCertificationDetail(certificationDetailsList, licenseInformationList);
            }
        }
 
        if (upsertAccountBusinesslicense.size() > 0) {
            upsert upsertAccountBusinesslicense;
        }
    }
 
    //获取符合的证照明细
    public static List < CertificationDetails__c > SatisfyLicenseDetails(String bpCode, List < Detail_Info > LicenseDetailS, Map < String, String > certificationDetailTransferMap, BatchIF_Log__c iflog) {
        Map < String, CertificationDetails__c > certificationDetailMap = new Map < String, CertificationDetails__c > ();
        List < CertificationDetails__c > result = new List < CertificationDetails__c > ();
        for (Detail_Info detailInfo: LicenseDetailS) {
 
            String licenseType = FieldTransformation(bpCode, certificationDetailTransferMap, 'ProdustionType__c', detailInfo.ProSerial, iflog, 'ProSerial');
            Integer quantity = String.isBlank(detailInfo.Quantity) ? null : Integer.valueof(detailInfo.Quantity);
            String remarks = detailInfo.ProName + '(' + quantity + ')';
            String key = licenseType + '-' + detailInfo.Category;
            if (!certificationDetailMap.containsKey(key)) {
                CertificationDetails__c detail = new CertificationDetails__c();
                detail.ProdustionType__c = licenseType;
                detail.ProductModelNumber__c = quantity;
                detail.ActivitieTypes__c = detailInfo.Category;
                detail.DeviceName__c = detailInfo.ProName;
                detail.Remarks__c = remarks;
                certificationDetailMap.put(key, detail);
            } else {
                certificationDetailMap.get(key).ProductModelNumber__c += quantity;
                certificationDetailMap.get(key).DeviceName__c += '/' + detailInfo.ProName;
                certificationDetailMap.get(key).Remarks__c += '/' + remarks;
 
            }
        }
        if (certificationDetailMap.size() > 0) {
            for (CertificationDetails__c detail: certificationDetailMap.values()) {
                result.add(detail);
            }
        }
 
        return result;
    }
 
    public static void insertCertificationDetail(List < CertificationDetails__c > certificationDetailsList,
        List < License_Information__c > licenseInformationList) {
 
        String radiationCertificateId = '';
        for (License_Information__c licenseInformation: licenseInformationList) {
            if (licenseInformation.LicenseType__c == '辐射安全许可证') {
                radiationCertificateId = licenseInformation.Id;
            }
        }
        for (CertificationDetails__c certificationDetail: certificationDetailsList) {
            certificationDetail.LicenseInformation__c = radiationCertificateId;
        }
        upsert certificationDetailsList;
 
    }
    public static void SatisfyConsigneeInfoList(List < GeData > satisfyGeData, Map < String, Account > existAccountMap, BatchIF_Log__c iflog) {
        system.debug('SatisfyConsigneeInfoList---Start');
        //收货人层级相关(联系人)
        List < Contact > upsertContactList = new List < Contact > ();
        Map < String, Consignee_Info > satisfyConsigneeInfoMap = new Map < String, Consignee_Info > ();
        // 代理商禁用 将代理商用户设置为未启用
        List < String > disableContactIdList = new List < String > ();
        Boolean disableUserFlag = false;
        // 代理商禁用 将代理商用户设置为未启用
        // 
        List < Contact > existContact = getexistContactList(satisfyGeData,existAccountMap);
        
        for (GeData gda: satisfyGeData) {
            if (gda.Consignee_Info == null) {
                continue;
            }
            satisfyConsigneeInfoMap = new Map < String, Consignee_Info > ();
            for (Consignee_Info consigneeInfo: gda.Consignee_Info) {
                if (!String.isBlank(gda.ApplyType) && gda.ApplyType != '2' && gda.ApplyType != '3' &&
                    gda.ApplyType != '4' && gda.ApplyType != '7' && gda.ApplyType != '8') {
                    continue;
                }
                if (String.isBlank(consigneeInfo.ContactName) || String.isBlank(consigneeInfo.ContactId)) {
                    continue;
                }
                if (String.isBlank(consigneeInfo.ContactAddress) && gda.BPType != '22') {
                    continue;
                }
 
                String contactId = processContactId(consigneeInfo.ContactId);
                satisfyConsigneeInfoMap.put(contactId, consigneeInfo);
 
            }
            system.debug('<--satisfyConsigneeInfoMap-->' + satisfyConsigneeInfoMap);
            Map < String, Contact > existContactMap = new Map < String, Contact > ();
            if (satisfyConsigneeInfoMap.size() > 0) {
 
                // String accountId = existAccountMap.get(gda.BPCode).Id;
                // List < Contact > existContact = [select Id, ManagementCode_Ext__c, IsFromSPO__c, ManagementCode_F__c
                //     from Contact
                //     where AccountId =: accountId
                // ];
 
                if (existContact.size() > 0) {
                    for (Contact con: existContact) {
 
                        String contactId = processContactId(con.ManagementCode_F__c);
                        existContactMap.put(contactId, con);
                        if (!satisfyConsigneeInfoMap.containsKey(contactId)) {
                            Contact cont = new Contact();
                            cont.Id = con.Id;
                            cont.ContactStatus__c = 'Cancel';
                            cont.ContactStatusD__c = 'Cancel';
                            if (String.isNotBlank(con.ManagementCode_Ext__c)) {
                                upsertContactList.add(cont);
                            }
 
                        }
 
                        disableContactIdList.add(con.Id);
                    }
                    // 将代理商用户设置为未启用
                    // if (gda.ApplyType == '1' && disableContactIdList != null && disableContactIdList.size() > 0) {
                    //     disableUserFlag = true;
                    // }
                    // 将代理商用户设置为未启用
                }
                system.debug('existContactMap------>' + existContactMap);
                for (Consignee_Info consigneeInfo: satisfyConsigneeInfoMap.values()) {
 
                    String contactId = processContactId(consigneeInfo.ContactId);
                    Contact con = new Contact();
                    if (existContactMap.containsKey(contactId)) {
                        con = existContactMap.get(contactId);
                        if (String.isNotBlank(con.ManagementCode_Ext__c)) {
                            con.ManagementCode_Ext__c = consigneeInfo.ContactId;
                        }
                    } else {
                        con.ManagementCode_Ext__c = consigneeInfo.ContactId;
                        con.IsFromSPO__c = true;
                        con.Department = '无';
                    }
                    con.AccountId = existAccountMap.get(gda.BPCode).Id;
                    con.MobilePhone = consigneeInfo.ContactPhone; //SPO只有一个值consigneeInfo.ContactPhone故作此处理20191016 consigneeInfo.ContactMobilePhone;
                    con.MobilePhoneD__c = consigneeInfo.ContactPhone;
                    con.Phone = consigneeInfo.ContactPhone;
                    con.PhoneD__c = consigneeInfo.ContactPhone;
                    con.Postcode__c = consigneeInfo.PostalCode;
                    con.PostcodeD__c = consigneeInfo.PostalCode;
                    con.Address1__c = gda.BPType == '22' ? gda.RegisterAddress : consigneeInfo.ContactAddress;
                    con.Address1D__c = gda.BPType == '22' ? gda.RegisterAddress : consigneeInfo.ContactAddress;
                    con.EnglishAddress__c = gda.BPType == '22' ? gda.STR_SUPPL1 : '';
                    if (String.isNotBlank(consigneeInfo.ContactEnglishName) && gda.BPType == '22') {
                        con.LastName = consigneeInfo.ContactName + '(' + consigneeInfo.ContactEnglishName + ')';
                        con.FirstName = null;
                        con.ContactEnglishName__c = consigneeInfo.ContactEnglishName;
                    } else {
                        con.LastName = consigneeInfo.ContactName;
                        con.FirstName = null;
                    }
 
                    con.ContactStatus__c = 'Active';
                    con.ContactStatusD__c = 'Active';
                    con.StatusD__c = 'Pass';
                    con.ProductSegmentBS__c = false;
                    con.ProductSegmentRVI__c = false;
                    con.ProductSegmentIE__c = false;
                    con.ProductSegmentNDT__c = false;
                    con.ProductSegmentANI__c = false;
 
                    String productSegment = existAccountMap.get(gda.BPCode).ProductSegment__c;
                    if (productSegment == 'BS') {
                        con.ProductSegmentBS__c = true;
                    } else if (productSegment == 'RVI') {
                        con.ProductSegmentRVI__c = true;
                    } else if (productSegment == 'IE') {
                        con.ProductSegmentIE__c = true;
                    } else if (productSegment == 'NDT') {
                        con.ProductSegmentNDT__c = true;
                    } else if (productSegment == 'ANI') {
                        con.ProductSegmentANI__c = true;
                    }
 
                    upsertContactList.add(con);
                }
            }
 
        }
        // 将代理商用户设置为未启用 START
        // if (disableUserFlag) {
 
        //     List < User > disableUserList = [SELECT Id, Name, ContactId, IsActive FROM User WHERE ContactId In: disableContactIdList AND IsActive = true];
        //     if (disableUserList.size() > 0) {
        //         for (User user: disableUserList) {
        //             user.IsActive = false;
        //         }
        //         update disableUserList;
        //     }
        // }
        // 将代理商用户设置为未启用 END
        if (upsertContactList.size() > 0) {
            NFMUtil.EscapeSBG001TriggerHandler = true;
            system.debug('upsertContactList---->' + upsertContactList);
            upsert upsertContactList;
        }
        system.debug('SatisfyConsigneeInfoList---End');
 
    }
 
    public static List < Contact > getexistContactList(List < GeData > satisfyGeData, Map < String, Account > existAccountMap){
        List < Contact > existContact = new List < Contact >();
        String accountIdStr = null;
        Boolean  contactCancelFlg = false;
        for (GeData gda: satisfyGeData) {
            // 客户禁用,将客户下的联系人进行无效
            if (!String.isBlank(gda.ApplyType) && gda.ApplyType == '1' ) {
                contactCancelFlg = true;
            }
            accountIdStr = existAccountMap.get(gda.BPCode).Id;
        }
 
        if (String.isNotBlank(accountIdStr)) {
            existContact = [select Id, ManagementCode_Ext__c, IsFromSPO__c, ManagementCode_F__c
                from Contact
                where AccountId =: accountIdStr
            ];
 
            if (contactCancelFlg && existContact.size() > 0 ) {
                for (Contact con: existContact) {
                    con.ContactStatus__c = 'Cancel';
                    con.ContactStatusD__c = 'Cancel';
                }
 
                update existContact;
            }
        }
 
        return existContact;
    }
 
 
    public static String processContactId(String contactId) {
        String result = '';
        Pattern pattern = Pattern.compile('[0-9]*');
        Matcher isNum = pattern.matcher(contactId);
        String stringNum = '';
        if (isNum.matches()) {
            Integer integerNum = Integer.valueOf(contactId);
            stringNum = String.valueOf(integerNum);
        }
        result = String.isBlank(stringNum) ? contactId : stringNum;
        return result;
    }
 
    //验证字段是否有值
    public static Boolean FieldIsBlank(String bpCode, String verifyField, String receivesField, BatchIF_Log__c iflog) {
 
        if (String.isBlank(bpCode)) {
            iflog.ErrorLog__c += 'BPCode is required,This data is skipped.\n';
            return true;
        } else if (String.isBlank(verifyField)) {
            iflog.ErrorLog__c += 'BPCode[ ' + bpCode + ' ] of ' + receivesField + ' is required,This data is skipped.\n';
            return true;
        } else {
            return false;
        }
 
    }
    //将 吴 晓晓(00328000010GuUC)放到全国企业用户的客户团队里面
    public static void UpsertAccountTeamMember(String accountId) {
        AccountTeamMember atm = new AccountTeamMember();
        atm.accountId = accountId;
        atm.userId = '005280000071H0s';
        atm.teamMemberRole = 'Sales Manager';
        atm.ACCOUNTACCESSLEVEL = 'Edit';
        atm.CONTACTACCESSLEVEL = 'Edit';
        atm.OPPORTUNITYACCESSLEVEL = 'None';
        atm.CASEACCESSLEVEL = 'None';
        upsert atm;
    }
 
    public static List < GeData > SatisfyGeData(List < GeData > geDataList, BatchIF_Log__c iflog) {
 
        List < GeData > result = new List < GeData > ();
 
        for (GeData gda: geDataList) {
            Boolean ErrorFlg = false;
            if (FieldIsBlank(gda.BPCode, gda.BPCode, 'BPCode', iflog)) {
                ErrorFlg = true;
            }
 
            if (FieldIsBlank(gda.BPCode, gda.BPType, 'BPType', iflog)) {
                ErrorFlg = true;
            }
 
            if (FieldIsBlank(gda.BPCode, gda.ProductSegment, 'ProductSegment', iflog)) {
                ErrorFlg = true;
            }
 
            if (FieldIsBlank(gda.BPCode, gda.CompanyName, 'CompanyName', iflog)) {
                ErrorFlg = true;
            }
 
            // if (FieldIsBlank(gda.BPCode, gda.VTWEG, 'VTWEG', iflog)) {
            //     ErrorFlg = true;
            // }
 
            if (gda.BPType == '24') {
 
                // 分销渠道-直销
                if ('42'.equals(gda.VTWEG)) {
 
                    if (FieldIsBlank(gda.BPCode, gda.AccountSource, 'BPType is 24 of AccountSource', iflog)) {
                        ErrorFlg = true;
                    }
                    if (FieldIsBlank(gda.BPCode, gda.UserType, 'BPType is 24 of UserType', iflog)) {
                        ErrorFlg = true;
                    }
                    if (FieldIsBlank(gda.BPCode, gda.COMPOSubuse, 'BPType is 24 of COMPOSubuse', iflog)) {
                        ErrorFlg = true;
                    }
                    if (gda.TargetCustomer == null) {
                        iflog.ErrorLog__c += 'BPCode[ ' + gda.BPCode + ' ] of TargetCustomer is required,This data is skipped.\n'; //客户类型
                        ErrorFlg = true;
                    }
 
                    if (gda.IsCOMPO == null) {
                        iflog.ErrorLog__c += 'BPCode[ ' + gda.BPCode + ' ] of IsCOMPO is required,This data is skipped.\n'; //COMPO客户
                        ErrorFlg = true;
                    }
                } else {
                    if (FieldIsBlank(gda.BPCode, gda.KeyAccount, 'BPType is 24 of KeyAccount', iflog)) {
                        ErrorFlg = true;
                    }
 
                    if (FieldIsBlank(gda.BPCode, gda.ExportRegulatedCustomer, 'BPType is 24 of ExportRegulatedCustomer', iflog)) {
                        ErrorFlg = true;
                    }
 
                    if (FieldIsBlank(gda.BPCode, gda.MarketVerticals, 'BPType is 24 of MarketVerticals', iflog)) {
                        ErrorFlg = true;
                    }
 
                    if (FieldIsBlank(gda.BPCode, gda.Industry, 'BPType is 24 of Industry', iflog)) {
                        ErrorFlg = true;
                    }
 
                    if (FieldIsBlank(gda.BPCode, gda.Use, 'BPType is 24 of Use', iflog)) {
                        ErrorFlg = true;
                    }
                }
 
            }
            String transformProductSegment = gda.ProductSegment == 'LS' ? 'BS' : gda.ProductSegment;
 
            if (gda.BPType == '23') {
                //经销商分类
                if (FieldIsBlank(gda.BPCode, gda.AgentType, 'AgentType', iflog)) {
                    ErrorFlg = true;
                }
            }
            if (gda.BPType == '23' && transformProductSegment == 'BS') {
                //免税编码
                if (FieldIsBlank(gda.BPCode, gda.BPCodeforeign, 'BPType is 23 and ProductSegment is BS  BPCodeforeign', iflog)) {
                    ErrorFlg = true;
                } else {
                    String foreign = gda.BPCodeforeign.trim();
                    if (foreign.equals('3000000378')) {
                        gda.BPCodeforeign = foreign + '-1';
                    }
                }
            }
            if (ErrorFlg) {
                continue;
            }
            result.add(gda);
        }
 
        return result;
    }
    //存在的客户
    public static Map < String, Account > ExistAccount(List < GeData > satisfyGeData, BatchIF_Log__c iflog) {
 
        Map < String, Account > result = new Map < String, Account > ();
        List < String > bPCodeList = new List < String > ();
        for (GeData gda: satisfyGeData) {
            String transformProductSegment = gda.ProductSegment == 'LS' ? 'BS' : gda.ProductSegment;
            if (gda.BPType == '23' && transformProductSegment == 'BS') {
                bPCodeList.add(gda.BPCodeforeign);
            }
            bPCodeList.add(gda.BPCode);
        }
 
        if (bPCodeList.size() > 0) {
            List < Account > accountList = [select Id, Name, ManagementCode_Ext__c, ParentId, ProductSegment__c,
                Parent.Name, Parent.BS_Children_Code__c, Address1__c, RecordTypeId, IsPartner, ProhibitionAccount__c
                from Account
                where ManagementCode_Ext__c In: bPCodeList
            ];
 
            if (accountList.size() > 0) {
 
                for (Account acc: accountList) {
                    result.put(acc.ManagementCode_Ext__c, acc);
                }
            }
        }
 
        return result;
    }
 
    public static Map < String, String > LicenseChanges(Map < String, Account > existAccountMap) {
        Map < String, String > result = new Map < String, String > ();
 
        if (existAccountMap.size() > 0) {
 
            for (Account acc: existAccountMap.values()) {
 
                if (acc.RecordTypeId == '012280000005gnE' && acc.ProductSegment__c == 'BS') {
 
                    result.put(acc.Parent.BS_Children_Code__c, acc.ParentId);
                } else {
                    result.put(acc.ManagementCode_Ext__c, acc.Id);
                }
            }
        }
        return result;
 
    }
 
    public static Account AccountValueAssignment(GeData gda, Account accountInfo, Map < String, String > transferMap, Map < String, String > accountRecordTypeMap, BatchIF_Log__c iflog) {
        system.debug('AccountValueAssignment---Start');
        system.debug('accountRecordTypeMap---->' + accountRecordTypeMap);
        String productSegment = gda.ProductSegment == 'LS' ? 'BS' : gda.ProductSegment;
        if (gda.BPType == '23') {
            if (String.isNotBlank(accountInfo.BS_Children_Code__c)) {
                accountInfo.DealerTradeType__c = FieldTransformation(gda.BPCode, transferMap, 'DealerTradeType__c', '13', iflog, 'DealerTradeType');
                accountInfo.Name = gda.CompanyName + '(P)';
            } else {
                accountInfo.Name = gda.CompanyName;
            }
            accountInfo.RecordTypeId = '012280000005gnE';
            // accountInfo.DisableDate__c = gda.ApplyType == '1' ? Date.today() : null;
            // 将代理商的合作伙伴客户设置为假
            if (gda.ApplyType == '1') {
                accountInfo.DisableDate__c = Date.today();
                accountInfo.IsPartner = false; //将代理商的合作伙伴客户设置为False
                accountInfo.ProhibitionAccount__c = true;
            }
            // 将代理商的合作伙伴客户设置为假
            accountInfo.Dealer_Type__c = NFMUtil.getMapValue(transferMap, 'Dealer_Type__c', gda.AgentType, iflog);
            accountInfo.EnglishAddress__c = gda.RegisterAddress;
        } else if (gda.BPType == '22') {
            accountInfo.RecordTypeId = '01228000000TF3Q'; //外贸公司
            accountInfo.Name = gda.CompanyName;
            accountInfo.EnglishAddress__c = gda.STR_SUPPL1;
        } else if (gda.BPType == '24') { //全国企业用户(大客户)
            accountInfo.Name = gda.CompanyName;
            accountInfo.FacilityName__c = gda.CompanyName;
            accountInfo.DivisionName__c = '无';
            accountInfo.DivisionName_D__c = '无';
 
            if (String.isNotBlank(gda.Postal)) {
                accountInfo.PostCode__c = gda.Postal;
                accountInfo.PostCodeD__c = gda.Postal;
            }
 
 
 
            // 分销渠道-直销
            if ('42'.equals(gda.VTWEG)) {
                // IE直销
                accountInfo.CustomerSource__c = gda.AccountSource; //客户来源 【CustomerSource】添加选项值 直销、代理商
                accountInfo.TargetCustomer__c = gda.TargetCustomer ? '目标' : '非目标'; //客户类型
                accountInfo.compo_Acc__c = gda.IsCOMPO ? 'COMPO客户' : '非COMPO客户'; //COMPO客户
                accountInfo.UserType__c = gda.UserType; //用户属性
                accountInfo.RecordTypeId = accountRecordTypeMap.get('IE_Account'); //IE直销 //'0120T0000003Cxt';
                accountInfo.FacilityNameD__c = gda.CompanyName;
                Map < String, String > subuseMap = getSubuseMap();
                accountInfo.IsNew__c = false;
                if (subuseMap.containsKey(gda.COMPOSubuse)) {
                    String industryC_MarketVerticals_Use = subuseMap.get(gda.COMPOSubuse);
                    List < String > fieldList = industryC_MarketVerticals_Use.split(';');
                    accountInfo.Sub_Use__c = fieldList[0];
                    accountInfo.Sub_UseD__c = fieldList[0];
                    accountInfo.IndustryC__c = fieldList[1];
                    accountInfo.IndustryCD__c = fieldList[1];
                    accountInfo.MarketVerticals__c = fieldList[2];
                    accountInfo.MarketVerticalsD__c = fieldList[2];
                    accountInfo.Use__c = fieldList[3];
                    accountInfo.UseD__c = fieldList[3];
 
                }
 
                if (gda.ApplyType == '1') {
                    accountInfo.DisableDate__c = Date.today();
                    accountInfo.IsPartner = false;
                    accountInfo.AccountStatus__c = 'Cancel';
                    accountInfo.AccountStatusD__c = 'Cancel';
                }
            } else {
                String keyAccount = FieldTransformation(gda.BPCode, transferMap, 'KeyAccount__c', gda.KeyAccount, iflog, 'KeyAccount');
                String marketVerticals = FieldTransformation(gda.BPCode, transferMap, 'MarketVerticals__c', gda.MarketVerticals, iflog, 'MarketVerticals');
                String use = FieldTransformation(gda.BPCode, transferMap, 'Use__c', gda.Use, iflog, 'Use');
                String industry = FieldTransformation(gda.BPCode, transferMap, 'IndustryC__c', gda.Industry, iflog, 'Industry');
                accountInfo.KeyAccount__c = keyAccount;
                accountInfo.IndustryC__c = industry;
                accountInfo.IndustryCD__c = industry;
 
                accountInfo.MarketVerticals__c = marketVerticals;
                accountInfo.MarketVerticalsD__c = marketVerticals;
                accountInfo.Use__c = use;
                accountInfo.UseD__c = use;
                accountInfo.Sub_Use__c = gda.SubUseEnterprise;
                accountInfo.Sub_UseD__c = gda.SubUseEnterprise;
                accountInfo.NationalEnterpriseUser__c = true;
 
                accountInfo.ExportRegulatedCustomer__c = false;
                if (gda.ExportRegulatedCustomer == '1') {
                    accountInfo.ExportRegulatedCustomer__c = true;
                }
                if (productSegment == 'BS') {
                    accountInfo.RecordTypeId = '01228000000TdF1'; //客户BS
                } else if (productSegment == 'ANI') {
                    accountInfo.RecordTypeId = '01228000000TdFL'; //客户ANI
                } else if (productSegment == 'IE') {
                    accountInfo.RecordTypeId = '01228000000TdF6'; //客户IE
                } else if (productSegment == 'NDT') {
                    accountInfo.RecordTypeId = '01228000000TdFG'; //客户NDT
                } else if (productSegment == 'RVI') {
                    accountInfo.RecordTypeId = '01228000000TdFB'; //客户RVI
                }
            }
 
        }
 
        accountInfo.ProductSegment__c = productSegment;
        accountInfo.Province__c = gda.Region;
        accountInfo.MobilePhoneNumber__c = gda.Mobilephone;
        accountInfo.EnglishName__c = gda.CompanyEnglishName;
        accountInfo.EnglishNameD__c = gda.CompanyEnglishName;
        accountInfo.City__c = gda.City;
        accountInfo.CityD__c = gda.City;
        accountInfo.Address1__c = gda.RegisterAddress;
        accountInfo.Address1D__c = gda.RegisterAddress;
        accountInfo.Phone = gda.Phone;
        accountInfo.PhoneD__c = gda.Phone;
        accountInfo.Fax = gda.Fax;
        accountInfo.FaxD__c = gda.Fax;
        accountInfo.stautesD__c = 'Pass';
        system.debug('AccountValueAssignment---End');
        return accountInfo;
    }
 
    public static String GetLog(String logstr, GeData gda) {
        logstr += '管理编码:' + gda.BPCode;
        logstr += '\n变更类型:' + gda.ApplyType;
        logstr += '\n客户类型:' + gda.BPType;
        if (gda.BPType == '23') {
            logstr += '\n经销商分类:' + gda.AgentType;
        }
        return logstr;
    }
 
    public static Map < String, Dealer_Discount__c > getExistDealerDiscountMap(List < String > bpCodeList) {
        Map < String, Dealer_Discount__c > result = new Map < String, Dealer_Discount__c > ();
        //查找存在的代理商折扣
        List < Dealer_Discount__c > dealerDiscounts = [Select Id, Name, LatitudeValue__c, Valid_Date_From__c,
            Valid_Date_From2__c, Valid_Date_To__c, Valid_Date_To2__c
            from Dealer_Discount__c
            where DimensionValue1__c In: bpCodeList
        ];
 
 
        if (dealerDiscounts.size() > 0) {
 
            for (Dealer_Discount__c dealerDis: dealerDiscounts) {
 
                result.put(dealerDis.LatitudeValue__c, dealerDis);
            }
        }
        return result;
    }
 
 
    public static Map < String, String > getRecordTypeMap() {
        Map < String, String > result = new Map < String, String > ();
        List < RecordType > recordTypeList = [select Id, DeveloperName from RecordType where IsActive = true and SobjectType = 'Account'];
        if (recordTypeList.size() > 0) {
            for (RecordType recordType: recordTypeList) {
                result.put(recordType.DeveloperName, recordType.Id);
            }
        }
        return result;
    }
 
    public static void deleteDealerDiscount(Map < String, String > satisfyBsDealerDiscountMap, Map < String, Dealer_Discount__c > existDealerDiscountMap) {
        List < Dealer_Discount__c > dealerDiscountList = new List < Dealer_Discount__c > ();
        for (String key: existDealerDiscountMap.keySet()) {
 
            if (!satisfyBsDealerDiscountMap.containsKey(key)) {
                dealerDiscountList.add(existDealerDiscountMap.get(key));
            }
        }
        if (dealerDiscountList != null) {
            delete dealerDiscountList;
        }
 
    }
 
    /**
     * [getSubuseMap description]
     * @return [description]
     */
    public static Map < String, String > getSubuseMap() {
 
        // Sub_Use;IndustryC;MarketVerticals;Use
        Map < String, String > subuseMap = new Map < String, String > ();
        subuseMap.put('I045', '医疗器械;Medical Device/Equipment;Manufacturing;(Manufacturing) Other');
        subuseMap.put('I046', '激光加工;Other;Manufacturing;(Manufacturing) Other');
        subuseMap.put('I047', '制药;Pharmaceutical;Manufacturing;(Manufacturing) Other');
        subuseMap.put('I048', '动植物;Food/Feed/Agriculture;Other;(Other) Other');
        subuseMap.put('I049', '新能源汽车;Automotive;Manufacturing;Automotive Body/Engine');
        subuseMap.put('I050', '5G-PCB;Electronics;Manufacturing;Electronic Device');
        subuseMap.put('I051', '5G-半导体;Electronics;Manufacturing;Electronic Device');
        subuseMap.put('I052', '5G-其他;Electronics;Manufacturing;Electronic Device');
        subuseMap.put('I053', 'Compo-半导体-前道;Electronics;Manufacturing;(Manufacturing)OEM');
        subuseMap.put('I054', 'Compo-半导体-中期;Electronics;Manufacturing;(Manufacturing)OEM');
        subuseMap.put('I055', 'Compo-半导体-后道;Electronics;Manufacturing;(Manufacturing)OEM');
        subuseMap.put('I056', 'Compo-FPD;Electronics;Manufacturing;(Manufacturing)OEM');
        subuseMap.put('I057', 'Compo-3D 测量仪;Electronics;Manufacturing;(Manufacturing)OEM');
        subuseMap.put('I058', 'Compo-测试仪器;Electronics;Manufacturing;(Manufacturing)OEM');
        subuseMap.put('I059', 'Compo-拉曼光谱仪;Academic Research;Manufacturing;(Manufacturing)OEM');
        subuseMap.put('I060', 'Compo-血液;Medical Device/Equipment;Manufacturing;(Manufacturing)OEM');
        subuseMap.put('I061', 'Compo-尿液/粪便;Medical Device/Equipment;Manufacturing;(Manufacturing)OEM');
        subuseMap.put('I062', 'Compo-细胞学;Medical Device/Equipment;Manufacturing;(Manufacturing)OEM');
        subuseMap.put('I063', 'Compo-遗传学;Medical Device/Equipment;Manufacturing;(Manufacturing)OEM');
        subuseMap.put('I064', 'Compo-病理;Medical Device/Equipment;Manufacturing;(Manufacturing)OEM');
        subuseMap.put('I065', 'Compo-生殖;Medical Device/Equipment;Manufacturing;(Manufacturing)OEM');
        subuseMap.put('I066', 'Compo-高端显微镜;Academic Research;Manufacturing;(Manufacturing)OEM');
        subuseMap.put('I067', 'Compo-细胞观察;Academic Research;Manufacturing;(Manufacturing)OEM');
        subuseMap.put('I068', 'Compo-电子显微镜;Academic Research;Manufacturing;(Manufacturing)OEM');
        subuseMap.put('I069', 'Compo-高内涵;Academic Research;Manufacturing;(Manufacturing)OEM');
        subuseMap.put('I070', 'Compo-流式细胞仪;Academic Research;Manufacturing;(Manufacturing)OEM');
        subuseMap.put('I071', 'Compo-ODM;Other;Manufacturing;(Manufacturing)OEM');
        subuseMap.put('I072', 'Compo-精准医疗;Academic Research;Manufacturing;(Manufacturing)OEM');
        subuseMap.put('I073', 'Compo-其他;Other;Manufacturing;(Manufacturing)OEM');
        subuseMap.put('I038', '电子类_其他;Electronics;Manufacturing;Electronic Device');
        subuseMap.put('I036', '电子部品;Electronics;Manufacturing;Electronic Device');
        subuseMap.put('I035', '半导体;Electronics;Manufacturing;Semiconductor');
        subuseMap.put('I037', '太阳能;Electronics;Manufacturing;Electronic Device');
        subuseMap.put('I041', '石油地质;Mining/Geology;Natural Resources;Geology : Geological Surveys');
        subuseMap.put('I043', '重工设备;Fabricated Metal Manufacturing;Manufacturing;(Manufacturing) Other');
        subuseMap.put('I044', '材料类_其他;Academic Research;Manufacturing;Industrial Scientific research');//需特殊处理:材料类_其他
        subuseMap.put('I042', '五金模具;Metal Manufacturing;Manufacturing;Machined Parts');
        subuseMap.put('I039', '金属;Metal Manufacturing;Manufacturing;Casting');
        subuseMap.put('I033', 'LED;Electronics;Manufacturing;Electronic Device');
        subuseMap.put('I034', 'FPD;Electronics;Manufacturing;Electronic Device');
        subuseMap.put('I040', '汽车;Automotive;Manufacturing;Automotive Body/Engine');
 
        return subuseMap;
    }
    // public static Map < String, String > getSubuseMap() {
    //     Map < String, String > subuseMap = new Map < String, String > ();
    //     subuseMap.put('I045', '医疗器械_Medical Device/Equipment_Manufacturing_(Manufacturing) Other');
    //     subuseMap.put('I046', '激光加工_Other_Manufacturing_(Manufacturing) Other');
    //     subuseMap.put('I047', '制药_Pharmaceutical_Manufacturing_(Manufacturing) Other');
    //     subuseMap.put('I048', '动植物_Food/Feed/Agriculture_Other_(Other) Other');
    //     subuseMap.put('I049', '新能源汽车_Automotive_Manufacturing_Automotive Body/Engine');
    //     subuseMap.put('I050', '5G-PCB_Electronics_Manufacturing_Electronic Device');
    //     subuseMap.put('I051', '5G-半导体_Electronics_Manufacturing_Electronic Device');
    //     subuseMap.put('I052', '5G-其他_Electronics_Manufacturing_Electronic Device');
    //     subuseMap.put('I053', 'Compo-半导体-前道_Electronics_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('I054', 'Compo-半导体-中期_Electronics_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('I055', 'Compo-半导体-后道_Electronics_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('I056', 'Compo-FPD_Electronics_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('I057', 'Compo-3D 测量仪_Electronics_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('I058', 'Compo-测试仪器_Electronics_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('I059', 'Compo-拉曼光谱仪_Academic Research_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('I060', 'Compo-血液_Medical Device/Equipment_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('I061', 'Compo-尿液/粪便_Medical Device/Equipment_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('I062', 'Compo-细胞学_Medical Device/Equipment_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('I063', 'Compo-遗传学_Medical Device/Equipment_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('I064', 'Compo-病理_Medical Device/Equipment_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('I065', 'Compo-生殖_Medical Device/Equipment_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('I066', 'Compo-高端显微镜_Academic Research_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('I067', 'Compo-细胞观察_Academic Research_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('I068', 'Compo-电子显微镜_Academic Research_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('I069', 'Compo-高内涵_Academic Research_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('I070', 'Compo-流式细胞仪_Academic Research_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('I071', 'Compo-ODM_Other_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('I072', 'Compo-精准医疗_Academic Research_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('I073', 'Compo-其他_Other_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('I038', '电子类_其他_Electronics_Manufacturing_Electronic Device');
    //     subuseMap.put('I036', '电子部品_Electronics_Manufacturing_Electronic Device');
    //     subuseMap.put('I035', '半导体_Electronics_Manufacturing_Semiconductor');
    //     subuseMap.put('I037', '太阳能_Electronics_Manufacturing_Electronic Device');
    //     subuseMap.put('I041', '石油地质_Mining/Geology_Natural Resources_Geology : Geological Surveys');
    //     subuseMap.put('I043', '重工设备_Fabricated Metal Manufacturing_Manufacturing_(Manufacturing) Other');
    //     subuseMap.put('I044', '材料类_其他_Academic Research_Manufacturing_Industrial Scientific research');//需特殊处理:材料类_其他
    //     subuseMap.put('I042', '五金模具_Metal Manufacturing_Manufacturing_Machined Parts');
    //     subuseMap.put('I039', '金属_Metal Manufacturing_Manufacturing_Casting');
    //     subuseMap.put('I033', 'LED_Electronics_Manufacturing_Electronic Device');
    //     subuseMap.put('I034', 'FPD_Electronics_Manufacturing_Electronic Device');
    //     subuseMap.put('I040', '汽车_Automotive_Manufacturing_Automotive Body/Engine');
 
    //     return subuseMap;
    // }
 
    // public static Map < String, String > getSubuseMap() {
    //     Map < String, String > subuseMap = new Map < String, String > ();
    //     subuseMap.put('医疗器械', 'Medical Device/Equipment_Manufacturing_(Manufacturing) Other');
    //     subuseMap.put('激光加工', 'Other_Manufacturing_(Manufacturing) Other');
    //     subuseMap.put('制药', 'Pharmaceutical_Manufacturing_(Manufacturing) Other');
    //     subuseMap.put('动植物', 'Food/Feed/Agriculture_Other_(Other) Other');
    //     subuseMap.put('新能源汽车', 'Automotive_Manufacturing_Automotive Body/Engine');
    //     subuseMap.put('5G-PCB', 'Electronics_Manufacturing_Electronic Device');
    //     subuseMap.put('5G-半导体', 'Electronics_Manufacturing_Electronic Device');
    //     subuseMap.put('5G-其他', 'Electronics_Manufacturing_Electronic Device');
    //     subuseMap.put('Compo-半导体-前道', 'Electronics_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('Compo-半导体-中期', 'Electronics_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('Compo-半导体-后道', 'Electronics_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('Compo-FPD', 'Electronics_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('Compo-3D 测量仪', 'Electronics_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('Compo-测试仪器', 'Electronics_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('Compo-拉曼光谱仪', 'Academic Research_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('Compo-血液', 'Medical Device/Equipment_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('Compo-尿液/粪便', 'Medical Device/Equipment_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('Compo-细胞学', 'Medical Device/Equipment_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('Compo-遗传学', 'Medical Device/Equipment_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('Compo-病理', 'Medical Device/Equipment_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('Compo-生殖', 'Medical Device/Equipment_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('Compo-高端显微镜', 'Academic Research_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('Compo-细胞观察', 'Academic Research_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('Compo-电子显微镜', 'Academic Research_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('Compo-高内涵', 'Academic Research_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('Compo-流式细胞仪', 'Academic Research_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('Compo-ODM', 'Other_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('Compo-精准医疗', 'Academic Research_Manufacturing_(Manufacturing)OEM');
    //     subuseMap.put('Compo-其他', 'Other_Manufacturing_(Manufacturing)OEM');
 
    //     return subuseMap;
    // }
}