李彤
2022-03-18 2d04395e1642eb444322b45fec9c53174b1c8c00
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
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
// 点检对象:今天所在月加上之前2个月
// 访问,OPD,NTC对象:今天所在月加上之前11个月
// 修理,重点产品OPD对象:昨天所在年度一年的。如果到了4月没有数据,结果是0
// FIXME goto 访问,OPD,NTC现在一年有些长
global class RollupToHPBatch implements Database.Batchable<sObject> {
  String Salesdepartment_HP1;
  String Salesdepartment_HP2;
  String hpId;
  // SWAG-BQ75WE update by vivek 2020/06/11 start
  Map<String,String> filedNameMap;
  // SWAG-BQ75WE update by vivek 2020/06/11 end
 
  // 点检月3个月
  Integer intMonth = -4;          // 要注意:initAccountCount() も修正が必要
  // 年度切り替えたけど、前の年度のデータを見たい期間、4日の猶予期間
  static Integer intYearDelayDay = Integer.valueOf(System.Label.ShowLastYear_DefermentDay);
 
  /**
   * コンスタント
   */
  global RollupToHPBatch(String Salesdepartment_HP1, String Salesdepartment_HP2) {
    this.Salesdepartment_HP1 = Salesdepartment_HP1;
    this.Salesdepartment_HP2 = Salesdepartment_HP2;
  }
  global RollupToHPBatch(String hpId) {
    this.hpId = hpId;
  }
 
  /**
   * startには、queryを実行、病院を検索
   */
  global Database.QueryLocator start(Database.BatchableContext BC) {
    if (String.isBlank(this.Salesdepartment_HP1)) {
      // UT 用
      if (String.isBlank(this.hpId)) {
        return Database.getQueryLocator(
                 [select Id
                  from Account
                  where RecordType.DeveloperName = 'HP'
                      and Is_Active__c <> '無効'
                      order by Id]
               );
      } else {
        return Database.getQueryLocator(
                 [select Id
                  from Account
                  where RecordType.DeveloperName = 'HP'
                      and Is_Active__c <> '無効'
                      and Id = :this.HpId
                               order by Id]
               );
      }
    } else {
      return Database.getQueryLocator(
               [select Id
                from Account
                where RecordType.DeveloperName = 'HP'
                    and Is_Active__c <> '無効'
                    and (Salesdepartment_HP__c = :this.Salesdepartment_HP1 or Salesdepartment_HP__c = :this.Salesdepartment_HP2)
                    order by Id]
             );
    }
  }
 
  global void execute(Database.BatchableContext BC, List<SObject> hpList) {
 
 
    Date targetDay2 = getTargetDay();
    Integer targetmonth = targetDay2.month();           // 点検3ヶ月を消すため
    Date nextMonth = targetDay2.addMonths(1);
 
    Date sDate = Date.valueOf(nextMonth.year() - 1 + '-' + nextMonth.month() + '-1');
    Date eDate = Date.valueOf(nextMonth.year() + '-' + nextMonth.month() + '-1');
    // 点检月份,3个月前的1日到下个月1日
    Date sDay = Date.valueOf(targetDay2.addMonths(intMonth).year() + '-' + targetDay2.addMonths(intMonth).month() + '-1');
    Date eDay = Date.valueOf(nextMonth.year() + '-' + nextMonth.month() + '-1');
 
    // 修理TAT(到客户返送日),修理TAT(到RC返送日),修理报价,发票金额 Hospital__c应与Delivered_Product__r.Hospital__c一致
    Integer iyear = targetDay2.year();
    if (targetDay2.month() < 4) {
      iyear -= 1;
    }
    Date lastYd = Date.valueOf(iyear - 1 + '-4-1');
    Date thisYd = Date.valueOf(iyear   + '-4-1');
    Date nextYd = Date.valueOf(iyear + 1 + '-4-1');
    List<AggregateResult> repairList = [
                                         select AVG(Elapsed_day_from_Repair__c) avg_Repair,
                                         AVG(Elapsed_day_from_Repair2__c) avg_Repair2,
                                         SUM(Repair_List_Price__c) sum_Price,
                                         SUM(Billing_Amount__c) sum_Billing,
                                         Delivered_Product__r.Hospital__c Hospital
                                         from Repair__c
                                         where Delivered_Product__r.Hospital__c in : hpList
                                         and Hospital__c != null
                                         and Failure_Occurrence_Date__c >= : thisYd
                                         and Failure_Occurrence_Date__c < : nextYd
                                         group by Delivered_Product__r.Hospital__c
                                       ];
    Map<String, AggregateResult> repairMap = new Map<String, AggregateResult>();
    for (AggregateResult repair : repairList) {
      repairMap.put(String.valueOf(repair.get('Hospital')), repair);
    }
    List<AggregateResult> repair_LY_List = [
        select AVG(Elapsed_day_from_Repair__c) avg_Repair,
        AVG(Elapsed_day_from_Repair2__c) avg_Repair2,
        SUM(Repair_List_Price__c) sum_Price,
        SUM(Billing_Amount__c) sum_Billing,
        Delivered_Product__r.Hospital__c Hospital
        from Repair__c
        where Delivered_Product__r.Hospital__c in : hpList
        and Hospital__c != null
        and Failure_Occurrence_Date__c >= : lastYd
        and Failure_Occurrence_Date__c < : thisYd
        group by Delivered_Product__r.Hospital__c
                                           ];
    Map<String, AggregateResult> repair_LY_Map = new Map<String, AggregateResult>();
    for (AggregateResult repair : repair_LY_List) {
      repair_LY_Map.put(String.valueOf(repair.get('Hospital')), repair);
    }
 
    // 未回款金额 Hospital__c应与Delivered_Product__r.Hospital__c一致
    List<AggregateResult> unpaidList = [
                                         select SUM(Unpaid_Amount__c) sum_Unpaid,
                                         Delivered_Product__r.Hospital__c Hospital
                                         from Repair__c
                                         where Delivered_Product__r.Hospital__c in : hpList
                                         and Hospital__c != null
                                         /* CHAN-B7J5EK 2018/12/18 未回款金额为0 则不检索 start */
                                         and Unpaid_Amount__c != null
                                         and Unpaid_Amount__c != 0
                                         /* CHAN-B7J5EK 2018/12/18 未回款金额为0 则不检索 end */
                                         group by Delivered_Product__r.Hospital__c
                                       ];
    Map<String, AggregateResult> unpaidMap = new Map<String, AggregateResult>();
    for (AggregateResult unpaid : unpaidList) {
      unpaidMap.put(String.valueOf(unpaid.get('Hospital')), unpaid);
    }
 
    // 维修合同 按 状态=契約
    List<AggregateResult> mContractList = [
                                            select Count(Id) Cnt_Id, Hospital__c
                                            from Maintenance_Contract__c
                                            where Status__c = '契約'
                                                and Hospital__c in : hplist
                                      // KPI 报表修改 , 刨除多年保修合同 start 
                                      and (recordType.developerName = 'NewMaintenance_Contract' or recordType.developerName = 'Maintenance_Contract')
                            //  KPI 报表修改 , 刨除多年保修合同 end
                                                group by Hospital__c
                                          ];
    Map<String, AggregateResult> mContractMap = new Map<String, AggregateResult>();
    for (AggregateResult mContract : mContractList) {
      mContractMap.put(String.valueOf(mContract.get('Hospital__c')), mContract);
    }
 
 
    // 提案书 FIXME goto 時間の範囲?
    Map<Id, AggregateResult> proposalDocumentMap = new Map<Id, AggregateResult>([
          select Hospital__c Id,
//                       Max(Proposal_count__c) Submit_Status
          Max(Proposal_count__c) Proposal_Submit_Status,
          Max(Quarterly_count__c) Quarterly_Submit_Status
          from TemporaryFileBox__c
          where Hospital__c in : hpList
          and Approval_day__c >= :thisYd
          and Approval_day__c < :nextYd
          group by Hospital__c
        ]);
 
    Map<Id, AggregateResult> proposalDocumentMap2 = new Map<Id, AggregateResult>([
          select Hospital__c Id,
          Max(Customer_submit_day__c) Submit_day
          from TemporaryFileBox__c
          where Hospital__c in : hpList
          and Attached_category__c = '季报'
                                     and Approval_day__c >= :thisYd
                                     and Approval_day__c < :nextYd
                                     group by Hospital__c
        ]);
 
 
    Map<Id, Account2__c> updateAccount = new Map<Id, Account2__c>();
    List<Account2__c> acc2List =
      [select Id, Account__c, Account_Org__c from Account2__c
       where Account_Org__c in :hpList];
 
    for (Account2__c acc2 : acc2List) {
      String hpid = acc2.Account_Org__c;
      Account2__c acc = new Account2__c(Id = acc2.Id);
      initAccountCount(acc, targetmonth);
      // 提案书, 季报
      if (proposalDocumentMap.containsKey(hpid)) {
        acc.Proposal_Submit_Status__c =
          (proposalDocumentMap.get(hpid).get('Proposal_Submit_Status') == null ? 0 : (Decimal) proposalDocumentMap.get(hpid).get('Proposal_Submit_Status'))
          + (proposalDocumentMap.get(hpid).get('Quarterly_Submit_Status') == null ? 0 : (Decimal) proposalDocumentMap.get(hpid).get('Quarterly_Submit_Status'));
      }
      // 季报
      if (proposalDocumentMap2.containsKey(hpid)) {
        acc.Latest_Proposal_document_Submit_day__c =
          proposalDocumentMap2.get(hpid).get('Submit_day') == null ? null : (Date) proposalDocumentMap2.get(hpid).get('Submit_day');
      }
      if (repairMap.containsKey(hpid)) {
        acc.AVG_Elapsed_day_from_Repair__c += repairMap.get(hpid).get('avg_Repair') == null ? 0 : Decimal.valueOf(String.valueOf(repairMap.get(hpid).get('avg_Repair')));
        acc.AVG_Elapsed_day_from_Repair2__c += repairMap.get(hpid).get('avg_Repair2') == null ? 0 : Decimal.valueOf(String.valueOf(repairMap.get(hpid).get('avg_Repair2')));
        acc.SUM_Repair_List_Price__c += repairMap.get(hpid).get('sum_Price') == null ? 0 : Decimal.valueOf(String.valueOf(repairMap.get(hpid).get('sum_Price')));
        acc.SUM_Repair_Billing_Amount__c += repairMap.get(hpid).get('sum_Billing') == null ? 0 : Decimal.valueOf(String.valueOf(repairMap.get(hpid).get('sum_Billing')));
      }
      if (repair_LY_Map.containsKey(hpid)) {
        acc.AVG_Elapsed_day_from_Repair_Lastyear__c += repair_LY_Map.get(hpid).get('avg_Repair') == null ? 0 : Decimal.valueOf(String.valueOf(repair_LY_Map.get(hpid).get('avg_Repair')));
        acc.AVG_Elapsed_day_from_Repair2_Lastyear__c += repair_LY_Map.get(hpid).get('avg_Repair2') == null ? 0 : Decimal.valueOf(String.valueOf(repair_LY_Map.get(hpid).get('avg_Repair2')));
        acc.SUM_Repair_List_Price_Lastyear__c += repair_LY_Map.get(hpid).get('sum_Price') == null ? 0 : Decimal.valueOf(String.valueOf(repair_LY_Map.get(hpid).get('sum_Price')));
        acc.SUM_Repair_Billing_Amount_Lastyear__c += repair_LY_Map.get(hpid).get('sum_Billing') == null ? 0 : Decimal.valueOf(String.valueOf(repair_LY_Map.get(hpid).get('sum_Billing')));
      }
      if (unpaidMap.containsKey(hpid)) {
        acc.SUM_Unpaid_Amount__c += unpaidMap.get(hpid).get('sum_Unpaid') == null ? 0 : Decimal.valueOf(String.valueOf(unpaidMap.get(hpid).get('sum_Unpaid')));
      }
      if (mContractMap.containsKey(hpid)) {
        acc.Service_contract_active_number_HP__c += Integer.valueOf(mContractMap.get(hpid).get('Cnt_Id'));
      }
      updateAccount.put(hpid, acc);
      
    }
    
    
    // 戦略科室を検索
    Map<Id, Account> dcListMap = new Map<Id, Account>(
      [select Id, Department_Class_Label__c
       from Account
       where Parent.RecordType.DeveloperName = 'HP'
           and Is_Active__c <> '無効'
           and ParentId in : hpList]
    );
 
    // 2018-09-14 CHAN-B4J9TC start 额外获取归属于哪个战略科室 :Account_Org__r.RecordType_DeveloperName__c
    Map<Id, Account2__c> OldAccount2 =
      new Map<Id, Account2__c> (
      [select Id, Account__c, Account_Org__r.RecordType_DeveloperName__c
       , Account_Org__r.parentID //  SWAG-BD24SU
       from Account2__c
       where Account_Org__c in :dcListMap.keySet()]);
    // 2018-09-14 CHAN-B4J9TC end 额外获取归属于哪个战略科室
 
 
    // 修理TAT(到客户返送日),修理TAT(到RC返送日),修理报价,发票金额 Department_Class__c应与Delivered_Product__r.Department_Class__c一致
    List<AggregateResult> dc_repairList = [
                                            select AVG(Elapsed_day_from_Repair__c) avg_Repair,
                                            AVG(Elapsed_day_from_Repair2__c) avg_Repair2,
                                            SUM(Repair_List_Price__c) sum_Price,
                                            SUM(Billing_Amount__c) sum_Billing,
                                            Delivered_Product__r.Department_Class__c Department_Class
                                            from Repair__c
                                            where Delivered_Product__r.Department_Class__c in : dcListMap.keySet()
                                            and Department_Class__c != null
                                            and Failure_Occurrence_Date__c >= : thisYd
                                            and Failure_Occurrence_Date__c < : nextYd
                                            group by Delivered_Product__r.Department_Class__c
                                          ];
    Map<String, AggregateResult> dc_repairMap = new Map<String, AggregateResult>();
    for (AggregateResult dc_repair : dc_repairList) {
      dc_repairMap.put(String.valueOf(dc_repair.get('Department_Class')), dc_repair);
    }
    List<AggregateResult> dc_repair_LY_List = [
          select AVG(Elapsed_day_from_Repair__c) avg_Repair,
          AVG(Elapsed_day_from_Repair2__c) avg_Repair2,
          SUM(Repair_List_Price__c) sum_Price,
          SUM(Billing_Amount__c) sum_Billing,
          Delivered_Product__r.Department_Class__c Department_Class
          from Repair__c
          where Delivered_Product__r.Department_Class__c in : dcListMap.keySet()
          and Department_Class__c != null
          and Failure_Occurrence_Date__c >= : lastYd
          and Failure_Occurrence_Date__c < : thisYd
          group by Delivered_Product__r.Department_Class__c
        ];
    Map<String, AggregateResult> dc_repair_LY_Map = new Map<String, AggregateResult>();
    for (AggregateResult dc_repair : dc_repair_LY_List) {
      dc_repair_LY_Map.put(String.valueOf(dc_repair.get('Department_Class')), dc_repair);
    }
 
    // 未回款金额 Department_Class__c应与Delivered_Product__r.Department_Class__c一致
    List<AggregateResult> dc_unpaidList = [
                                            select SUM(Unpaid_Amount__c) sum_Unpaid,
                                            Delivered_Product__r.Department_Class__c Department_Class
                                            from Repair__c
                                            where Delivered_Product__r.Department_Class__c in : dcListMap.keySet()
                                            and Department_Class__c != null
                                            /* CHAN-B7J5EK 2018/12/18 未回款金额为0 则不检索 start */
                                            and Unpaid_Amount__c != null
                                            and Unpaid_Amount__c != 0
                                            /* CHAN-B7J5EK 2018/12/18 未回款金额为0 则不检索 end */
                                            group by Delivered_Product__r.Department_Class__c
                                          ];
    Map<String, AggregateResult> dc_unpaidMap = new Map<String, AggregateResult>();
    for (AggregateResult dc_unpaid : dc_unpaidList) {
      dc_unpaidMap.put(String.valueOf(dc_unpaid.get('Department_Class')), dc_unpaid);
    }
 
    // 维修合同 按 状态=契約
    List<AggregateResult> dc_mContractList = [
          select Count(Id) Cnt_Id, Department_Class__c
          from Maintenance_Contract__c
          where Status__c = '契約'
          // KPI 报表修改 , 刨除多年保修合同 start 
          and (recordType.developerName = 'NewMaintenance_Contract' or recordType.developerName = 'Maintenance_Contract')
          //  KPI 报表修改 , 刨除多年保修合同 end
                            and Department_Class__c in : dcListMap.keySet()
                            group by Department_Class__c
        ];
    Map<String, AggregateResult> dc_mContractMap = new Map<String, AggregateResult>();
    for (AggregateResult dc_mContract : dc_mContractList) {
      dc_mContractMap.put(String.valueOf(dc_mContract.get('Department_Class__c')), dc_mContract);
    }
 
    // 戦略科室更新
    // 2018/08/24 SWAG-B3U67F 获取战略科室的提案书情况 存至客户2中 start
 
    Map<Id, AggregateResult> proposalDocumentMap_dept = new Map<Id, AggregateResult>([
          select Account_dept__c Id,
          Max(Proposal_count__c) Proposal_Submit_Status,
          Max(Quarterly_count__c) Quarterly_Submit_Status
          from TemporaryFileBox__c
          where Account_dept__r.Parent.RecordType.DeveloperName = 'HP'
              and Account_dept__r.Is_Active__c <> '無効'
              and Account_dept__r.parentID in : hpList
              and Approval_day__c >= :thisYd
              and Approval_day__c < :nextYd
              group by Account_dept__c
        ]);
 
    // 2018/08/24 SWAG-B3U67F 获取战略科室的提案书情况 存至客户2中 end
 
    List<Account2__c> acc2List2 =
      [select Id, Account__c, Account_Org__c
       // SWAG-BD24SU 额外检索一些新东西 start
       , Account_Org__r.ParentId, Account_Org__r.RecordType_DeveloperName__c
       // SWAG-BD24SU 额外检索一些新东西 end
       from Account2__c where Account_Org__c in :dcListMap.keySet()];
    // SWAG-BD24SU 构建医院为key, 消化科战略科室客户2ID为Value 的map start
    Map<id, id> HPtoGIAcc2Map = new Map<id, Id>();
    Map<String, id> depToOtherDepAcc2Map = new Map<String,id>();
    for (Account2__c tempAcc2 : acc2List2) {
      // SWAG-BQ75WE update by vivek 2020/26/11 start
      // if (tempAcc2.Account_Org__c != null
          // && tempAcc2.Account_Org__r.RecordType_DeveloperName__c.equals('Department_Class_GI') ) {
          // HPtoGIAcc2Map.put(tempAcc2.Account_Org__r.ParentId, tempAcc2.Account_Org__c)
      // }
      if (tempAcc2.Account_Org__c != null) {
          depToOtherDepAcc2Map.put(tempAcc2.Account_Org__r.ParentId+tempAcc2.Account_Org__r.RecordType_DeveloperName__c, tempAcc2.Account_Org__c); 
      }
      // SWAG-BQ75WE update by vivek 2020/26/11  end
    }
    // SWAG-BD24SU 额外检索一些新东西 end
    for (Account2__c acc2 : acc2List2) {
      String dcid = acc2.Account_Org__c;
      Account2__c acc = new Account2__c(Id = acc2.Id);
      initAccountCount(acc, targetmonth);
      // 2018/08/24 SWAG-B3U67F 获取战略科室的提案书情况 存至客户2中 start
 
      if (proposalDocumentMap_dept.containsKey(dcid)) {
        acc.Proposal_Submit_Status_Dept__c =
          (proposalDocumentMap_dept.get(dcid).get('Proposal_Submit_Status') == null ? 0 : (Decimal) proposalDocumentMap_dept.get(dcid).get('Proposal_Submit_Status'))
          + (proposalDocumentMap_dept.get(dcid).get('Quarterly_Submit_Status') == null ? 0 : (Decimal) proposalDocumentMap_dept.get(dcid).get('Quarterly_Submit_Status'));
      }
 
      // 2018/08/24 SWAG-B3U67F 获取战略科室的提案书情况 存至客户2中 end
      if (dc_repairMap.containsKey(dcid)) {
        acc.AVG_Elapsed_day_from_Repair__c += dc_repairMap.get(dcid).get('avg_Repair') == null ? 0 : Decimal.valueOf(String.valueOf(dc_repairMap.get(dcid).get('avg_Repair')));
        acc.AVG_Elapsed_day_from_Repair2__c += dc_repairMap.get(dcid).get('avg_Repair2') == null ? 0 : Decimal.valueOf(String.valueOf(dc_repairMap.get(dcid).get('avg_Repair2')));
        acc.SUM_Repair_List_Price__c += dc_repairMap.get(dcid).get('sum_Price') == null ? 0 : Decimal.valueOf(String.valueOf(dc_repairMap.get(dcid).get('sum_Price')));
        acc.SUM_Repair_Billing_Amount__c += dc_repairMap.get(dcid).get('sum_Billing') == null ? 0 : Decimal.valueOf(String.valueOf(dc_repairMap.get(dcid).get('sum_Billing')));
      }
      if (dc_repair_LY_Map.containsKey(dcid)) {
        acc.AVG_Elapsed_day_from_Repair_Lastyear__c += dc_repair_LY_Map.get(dcid).get('avg_Repair') == null ? 0 : Decimal.valueOf(String.valueOf(dc_repair_LY_Map.get(dcid).get('avg_Repair')));
        acc.AVG_Elapsed_day_from_Repair2_Lastyear__c += dc_repair_LY_Map.get(dcid).get('avg_Repair2') == null ? 0 : Decimal.valueOf(String.valueOf(dc_repair_LY_Map.get(dcid).get('avg_Repair2')));
        acc.SUM_Repair_List_Price_Lastyear__c += dc_repair_LY_Map.get(dcid).get('sum_Price') == null ? 0 : Decimal.valueOf(String.valueOf(dc_repair_LY_Map.get(dcid).get('sum_Price')));
        acc.SUM_Repair_Billing_Amount_Lastyear__c += dc_repair_LY_Map.get(dcid).get('sum_Billing') == null ? 0 : Decimal.valueOf(String.valueOf(dc_repair_LY_Map.get(dcid).get('sum_Billing')));
      }
      if (dc_unpaidMap.containsKey(dcid)) {
        acc.SUM_Unpaid_Amount__c += dc_unpaidMap.get(dcid).get('sum_Unpaid') == null ? 0 : Decimal.valueOf(String.valueOf(dc_unpaidMap.get(dcid).get('sum_Unpaid')));
      }
      if (dc_mContractMap.containsKey(dcid)) {
        acc.Service_contract_active_number_HP__c += Integer.valueOf(dc_mContractMap.get(dcid).get('Cnt_Id'));
      }
      updateAccount.put(dcid, acc);
    }
   // 战略科室下的保有设备(按照重点产品区分) SWAG-BSC5WP you-20201021 start
    for (Asset aggAst : [
           select Department_Class__c, Product2.Key_product_147P__c,Product2Id
           from Asset
           where Status <> '廃棄' and Asset_Owner__c ='病院資産'
                          and Department_Class__c in : dcListMap.keySet() and Department_Class__c !=null 
                          and Product2.Key_product_147P__c <> ''
         ]) {
      Id dcid = (Id) aggAst.Department_Class__c; //战略科室id
      Account2__c acc = updateAccount.get(dcid);  //战略科室对应得 客户2
       String tmpKey = '';
        String tempKeyProduct = String.valueOf(aggAst.Product2.Key_product_147P__c);//重点产品区分
        if(tempKeyProduct != null&&String.isNotBlank(tempKeyProduct)){
          tmpKey = tempKeyProduct.substring(0,2);
        }
        system.debug('战略科室id:' + dcid + '==产品id=' + aggAst.Product2Id+'===重点产品区分 =='+tempKeyProduct+'==acc=='+acc);
        if(acc!=null){
            setAssetCount(acc, tmpKey, 1, OldAccount2, depToOtherDepAcc2Map , updateAccount);
 
        }
        
      }
    //SWAG-BSC5WP you-20201021 end
    // 资产 按 分类
    for (AggregateResult aggAst : [
           select Count(Id) Cnt_Id, Hospital__c, Department_Class__r.Department_Class_Label__c, Product2.Category3__c, Product2.Category4__c , Product2.Category5__c
           from Asset
           where Status = '使用中'
                          and Department_Class__c != null
                          and Hospital__c in : hpList
                          group by Hospital__c, Department_Class__r.Department_Class_Label__c, Product2.Category3__c, Product2.Category4__c , Product2.Category5__c
         ]) {
      Id hpid = (Id) aggAst.get('Hospital__c');
      Account2__c acc = updateAccount.get(hpid);
      String strDCL = aggAst.get('Department_Class_Label__c') == null ? '' : String.valueOf(aggAst.get('Department_Class_Label__c'));
      String strC3 = aggAst.get('Category3__c') == null ? '' : String.valueOf(aggAst.get('Category3__c'));
      String strC4 = aggAst.get('Category4__c') == null ? '' : String.valueOf(aggAst.get('Category4__c'));
      String strC5 = aggAst.get('Category5__c') == null ? '' : String.valueOf(aggAst.get('Category5__c'));
      Integer cntId = Integer.valueOf(aggAst.get('Cnt_Id'));
      setAssetCategoryCount(acc, strDCL, strC3, strC4, strC5, cntId);
    }
 
    // 资产 按 ProductCode
    for (AggregateResult aggPcode : [
           select Count(Id) Cnt_Id, Hospital__c, Product2.ProductCode
           from Asset
           where Status = '使用中'
                          and Product2.ProductCode in ('N4491360', 'WB91051C')
                          and Hospital__c in : hpList
                          group by Hospital__c, Product2.ProductCode
         ]) {
      Id hpid = (Id) aggPcode.get('Hospital__c');
      Account2__c acc = updateAccount.get(hpid);
      String strPcode = aggPcode.get('ProductCode') == null ? '' : String.valueOf(aggPcode.get('ProductCode'));
      Integer cntId = Integer.valueOf(aggPcode.get('Cnt_Id'));
      setAssetPcodeCount(acc, strPcode, cntId);
    }
 
    //报告一览 按 月份 按照医院
    //SWAG-BBBAKG之前的逻辑为 各个本部(包括能量)的数据都按照月累计,只区分销售推广和 销售服务(FSE)
    /*for (AggregateResult aggEvent : [
           select Count(Id) Cnt_Id, Account_ID__r.Parent.ParentId HPID, ActivityDate__c, Reporter_Job_Category__c
           from Event__c
           where Daily_Report__r.Status_With_Check__c <> '草案中'
           and Daily_Report__r.Status_With_Check__c <> ''
           and Account_ID__r.Parent.ParentId in : hplist
           and ActivityDate__c >= : sDate
           and ActivityDate__c < : eDate
           and Reporter_Job_Category__c in ('销售推广', '销售服务')
           group by Account_ID__r.Parent.ParentId, ActivityDate__c, Reporter_Job_Category__c
         ]) {
      Id hpid = (Id) aggEvent.get('HPID');
      Account2__c acc = updateAccount.get(hpid);
      Integer iMonth = aggEvent.get('ActivityDate__c') == null ? 0 : Date.valueOf(aggEvent.get('ActivityDate__c')).month();
      Integer cntId = Integer.valueOf(aggEvent.get('Cnt_Id'));
      String job_Category = (String) aggEvent.get('Reporter_Job_Category__c');
      setEventCount(acc, iMonth, cntId, job_Category);
    }*/
 
    /* SWAG-BBBAKG新逻辑为 销售服务(FSE)保持不变,
    * 原销售推广按照六个本部(华东、华北之类)按月累计(排除能量本部);
    * 对于能量本部,销售推广和销售市场统合起来按照月统计
    */
    //销售服务(FSE)
    for (AggregateResult aggEvent : [
           select Count(Id) Cnt_Id, Account_ID__r.Parent.ParentId HPID, ActivityDate__c, Reporter_Job_Category__c
           from Event__c
           where Daily_Report__r.Status_With_Check__c <> '草案中'
           and Daily_Report__r.Status_With_Check__c <> ''
           and Account_ID__r.Parent.ParentId in : hplist
           and ActivityDate__c >= : sDate
           and ActivityDate__c < : eDate
           and Reporter_Job_Category__c in ('销售服务')
           and Daily_Report__r.Submit_OnTime_Count__c = 1
           group by Account_ID__r.Parent.ParentId, ActivityDate__c, Reporter_Job_Category__c
         ]) {
      Id hpid = (Id) aggEvent.get('HPID');
      Account2__c acc = updateAccount.get(hpid);
      Integer iMonth = aggEvent.get('ActivityDate__c') == null ? 0 : Date.valueOf(aggEvent.get('ActivityDate__c')).month();
      Integer cntId = Integer.valueOf(aggEvent.get('Cnt_Id'));
      String job_Category = (String) aggEvent.get('Reporter_Job_Category__c');
      setEventCount(acc, iMonth, cntId, job_Category);
    }
    // 六个本部 销售推广
    for (AggregateResult aggEvent : [
           select Count(Id) Cnt_Id, Account_ID__r.Parent.ParentId HPID, ActivityDate__c, Reporter_Job_Category__c
           from Event__c
           where Daily_Report__r.Status_With_Check__c <> '草案中'
           and Daily_Report__r.Status_With_Check__c <> ''
           and Account_ID__r.Parent.ParentId in : hplist
           and ActivityDate__c >= : sDate
           and ActivityDate__c < : eDate
           and Reporter_Job_Category__c in ('销售推广')
           and Reporter_Dept__c in('医疗东北营业本部', '医疗华北营业本部',
                                   '医疗华东营业本部', '医疗华南营业本部', '医疗西北营业本部', '医疗西南营业本部')
           and Daily_Report__r.Submit_OnTime_Count__c = 1
           group by Account_ID__r.Parent.ParentId, ActivityDate__c, Reporter_Job_Category__c
         ]) {
      Id hpid = (Id) aggEvent.get('HPID');
      Account2__c acc = updateAccount.get(hpid);
      Integer iMonth = aggEvent.get('ActivityDate__c') == null ? 0 : Date.valueOf(aggEvent.get('ActivityDate__c')).month();
      Integer cntId = Integer.valueOf(aggEvent.get('Cnt_Id'));
      String job_Category = (String) aggEvent.get('Reporter_Job_Category__c');
      setEventCount(acc, iMonth, cntId, job_Category);
    }
    // 能量本部 销售推广 销售市场
    for (AggregateResult aggEvent : [
           select Count(Id) Cnt_Id, Account_ID__r.Parent.ParentId HPID, ActivityDate__c, Reporter_Job_Category__c
           from Event__c
           where Daily_Report__r.Status_With_Check__c <> '草案中'
           and Daily_Report__r.Status_With_Check__c <> ''
           and Account_ID__r.Parent.ParentId in : hplist
           and ActivityDate__c >= : sDate
           and ActivityDate__c < : eDate
           and Reporter_Job_Category__c in ('销售推广', '销售市场')
           // LHJ 202006 恢复 Start
           and ((Reporter_Product_specialist_incharge_pro__c = 'ENG'
           or Reporter_Product_specialist_incharge_pro__c = 'SP(兼ENG)'
           or Reporter_Product_specialist_incharge_pro__c = 'ALL(兼ENG/ET)'
           or (Reporter_Product_specialist_incharge_pro__c = 'SP'
           and Reporter_Post__c in ('经理','副经理')))
           //2021.6.29新增负责产品(兼)逻辑
           or 
           (Responsible_for_Products_Concurrently__c  = 'ENG'
           or Responsible_for_Products_Concurrently__c = 'ENG/ET'
           or (Responsible_for_Products_Concurrently__c = 'SP'
           and Reporter_Post__c in ('经理','副经理')))
           )
           //and Reporter_Dept__c = '能量事业本部'
           // LHJ 202006 恢复 End
           and Daily_Report__r.Submit_OnTime_Count__c = 1
                                  group by Account_ID__r.Parent.ParentId, ActivityDate__c, Reporter_Job_Category__c
         ]) {
      Id hpid = (Id) aggEvent.get('HPID');
      Account2__c acc = updateAccount.get(hpid);
      Integer iMonth = aggEvent.get('ActivityDate__c') == null ? 0 : Date.valueOf(aggEvent.get('ActivityDate__c')).month();
      Integer cntId = Integer.valueOf(aggEvent.get('Cnt_Id'));
      setEventCount_Eng(acc, iMonth, cntId);
    }
    // SWAG-BBBAKG end
 
    // 资产 按 分类
    for (AggregateResult aggAst : [
           select Count(Id) Cnt_Id, Department_Class__c, Product2.Category3__c, Product2.Category4__c , Product2.Category5__c
           from Asset
           where Status = '使用中'
                          and Department_Class__c in : dcListMap.keySet()
                          group by Department_Class__c, Product2.Category3__c, Product2.Category4__c , Product2.Category5__c
         ]) {
      Id dcid = (Id) aggAst.get('Department_Class__c');
      Account2__c acc = updateAccount.get(dcid);
      String strDCL = dcListMap.get(dcid).Department_Class_Label__c;
      String strC3 = aggAst.get('Category3__c') == null ? '' : String.valueOf(aggAst.get('Category3__c'));
      String strC4 = aggAst.get('Category4__c') == null ? '' : String.valueOf(aggAst.get('Category4__c'));
      String strC5 = aggAst.get('Category5__c') == null ? '' : String.valueOf(aggAst.get('Category5__c'));
      Integer cntId = Integer.valueOf(aggAst.get('Cnt_Id'));
      setAssetCategoryCount(acc, strDCL, strC3, strC4, strC5, cntId);
    }
 
    // 资产 按 ProductCode
    for (AggregateResult aggPcode : [
           select Count(Id) Cnt_Id, Department_Class__c, Product2.ProductCode
           from Asset
           where Status = '使用中'
                          and Product2.ProductCode in ('N4491360', 'WB91051C')
                          and Department_Class__c in : dcListMap.keySet()
                          group by Department_Class__c, Product2.ProductCode
         ]) {
      Id dcid = (Id) aggPcode.get('Department_Class__c');
      Account2__c acc = updateAccount.get(dcid);
      String strPcode = aggPcode.get('ProductCode') == null ? '' : String.valueOf(aggPcode.get('ProductCode'));
      Integer cntId = Integer.valueOf(aggPcode.get('Cnt_Id'));
      setAssetPcodeCount(acc, strPcode, cntId);
    }
    // 报告一览 按 月份 按照战略科室
    //SWAG-BBBAKG之前的逻辑为 各个本部(包括能量)的数据都按照月累计,只区分销售推广和 销售服务(FSE)
    /*
    for (AggregateResult aggEvent : [
           select Count(Id) Cnt_Id, Account_ID__r.ParentId DCID, ActivityDate__c, Reporter_Job_Category__c
           from Event__c
           where Daily_Report__r.Status_With_Check__c <> '草案中'
           and Daily_Report__r.Status_With_Check__c <> ''
           and Account_ID__r.ParentId in : dcListMap.keySet()
           and ActivityDate__c >= : sDate
           and ActivityDate__c < : eDate
           and Reporter_Job_Category__c in ('销售推广', '销售服务')
           group by Account_ID__r.ParentId, ActivityDate__c, Reporter_Job_Category__c
         ]) {
      Id dcid = (Id) aggEvent.get('DCID');
      Account2__c acc = updateAccount.get(dcid);
      Integer iMonth = aggEvent.get('ActivityDate__c') == null ? 0 : Date.valueOf(aggEvent.get('ActivityDate__c')).month();
      Integer cntId = Integer.valueOf(aggEvent.get('Cnt_Id'));
      String job_Category = (String) aggEvent.get('Reporter_Job_Category__c');
      setEventCount(acc, iMonth, cntId, job_Category);
    }
    */
 
    /* SWAG-BBBAKG新逻辑为 销售服务(FSE)保持不变,
    * 原销售推广按照六个本部(华东、华北之类)按月累计(排除能量本部);
    * 对于能量本部,销售推广和销售市场统合起来按照月统计
    */
    
     //销售服务(FSE)
     for (AggregateResult aggEvent : [
            select Count(Id) Cnt_Id, Account_ID__r.ParentId DCID, ActivityDate__c, Reporter_Job_Category__c
            from Event__c
            where Daily_Report__r.Status_With_Check__c <> '草案中'
            and Daily_Report__r.Status_With_Check__c <> ''
            and Account_ID__r.ParentId in : dcListMap.keySet()
            and ActivityDate__c >= : sDate
            and ActivityDate__c < : eDate
            and Reporter_Job_Category__c = '销售服务'
            and Daily_Report__r.Submit_OnTime_Count__c = 1
                                           group by Account_ID__r.ParentId, ActivityDate__c, Reporter_Job_Category__c
          ]) {
       Id dcid = (Id) aggEvent.get('DCID');
       Account2__c acc = updateAccount.get(dcid);
       Integer iMonth = aggEvent.get('ActivityDate__c') == null ? 0 : Date.valueOf(aggEvent.get('ActivityDate__c')).month();
       Integer cntId = Integer.valueOf(aggEvent.get('Cnt_Id'));
       String job_Category = (String) aggEvent.get('Reporter_Job_Category__c');
       setEventCount(acc, iMonth, cntId, job_Category);
     }
     // 六个本部 销售推广
     for (AggregateResult aggEvent : [
            select Count(Id) Cnt_Id, Account_ID__r.ParentId DCID, ActivityDate__c, Reporter_Job_Category__c
            from Event__c
            where Daily_Report__r.Status_With_Check__c <> '草案中'
            and Daily_Report__r.Status_With_Check__c <> ''
            and Account_ID__r.ParentId in : dcListMap.keySet()
            and ActivityDate__c >= : sDate
            and ActivityDate__c < : eDate
            and Reporter_Job_Category__c in ('销售推广')
            and Reporter_Dept__c in('医疗东北营业本部', '医疗华北营业本部',
                                    '医疗华东营业本部', '医疗华南营业本部', '医疗西北营业本部', '医疗西南营业本部')
            and Daily_Report__r.Submit_OnTime_Count__c = 1
            group by Account_ID__r.ParentId, ActivityDate__c, Reporter_Job_Category__c
          ]) {
       Id dcid = (Id) aggEvent.get('DCID');
       Account2__c acc = updateAccount.get(dcid);
       Integer iMonth = aggEvent.get('ActivityDate__c') == null ? 0 : Date.valueOf(aggEvent.get('ActivityDate__c')).month();
       Integer cntId = Integer.valueOf(aggEvent.get('Cnt_Id'));
       String job_Category = (String) aggEvent.get('Reporter_Job_Category__c');
       setEventCount(acc, iMonth, cntId, job_Category);
     }
     // 能量本部 销售推广 销售市场
     for (AggregateResult aggEvent : [
            select Count(Id) Cnt_Id, Account_ID__r.ParentId DCID, ActivityDate__c, Reporter_Job_Category__c
            from Event__c
            where Daily_Report__r.Status_With_Check__c <> '草案中'
            and Daily_Report__r.Status_With_Check__c <> ''
            and Account_ID__r.ParentId in : dcListMap.keySet()
            and ActivityDate__c >= : sDate
            and ActivityDate__c < : eDate
            and Reporter_Job_Category__c in ('销售推广', '销售市场')
            // LHJ 20200624 恢复 Start
            and ((Reporter_Product_specialist_incharge_pro__c = 'ENG'
            or Reporter_Product_specialist_incharge_pro__c = 'SP(兼ENG)'
            or Reporter_Product_specialist_incharge_pro__c = 'ALL(兼ENG/ET)'
            or(Reporter_Product_specialist_incharge_pro__c = 'SP'
            and Reporter_Post__c in ('经理','副经理')))
            or
            //2021.6.29 新增的逻辑  报告者・推进员工负责产品(兼)
            (Responsible_for_Products_Concurrently__c = 'ENG'
            or Responsible_for_Products_Concurrently__c = 'ENG/ET'
            or (Responsible_for_Products_Concurrently__c = 'SP'
            and Reporter_Post__c in ('经理','副经理'))
            ))
            // and Reporter_Dept__c = '能量事业本部'
            // LHJ 20200624 End
            and Daily_Report__r.Submit_OnTime_Count__c = 1
                                   group by Account_ID__r.ParentId, ActivityDate__c, Reporter_Job_Category__c
          ]) {
       Id dcid = (Id) aggEvent.get('DCID');
       Account2__c acc = updateAccount.get(dcid);
       Integer iMonth = aggEvent.get('ActivityDate__c') == null ? 0 : Date.valueOf(aggEvent.get('ActivityDate__c')).month();
       Integer cntId = Integer.valueOf(aggEvent.get('Cnt_Id'));
       String job_Category = (String) aggEvent.get('Reporter_Job_Category__c');
       setEventCount_Eng(acc, iMonth, cntId);
     }
     
    // SWAG-BBBAKG end
 
 
    // 点检回数(按单据),点检回数(按设备)
    // なぜか Inspection_Times__c 設定されない
    List<AssetHistory__c> ahListFlag = new List<AssetHistory__c>();
    List<AssetHistory__c> ahList0Flag = new List<AssetHistory__c>();
    String breakInspection = '';
    Boolean isUpdateFlag = true;
    List<AssetHistory__c> ahs = [
                                  select Inspection_report_number__c,
                                  Inspection_Times__c,
                                  Inspection_Count__c,
                                  Inspection_Report__c,
                                  Inspection_Report__r.Status__c,
                                  AssetId__r.Hospital__c,
                                  AssetId__r.Department_Class__c,
                                  Examination_Date__c
                                  from AssetHistory__c
                                  where AssetId__r.Hospital__c in : hpList
                                  and Field_Api_Name__c = 'Inspection_report_number__c'
                                      and NewValue__c != null
                                      and AssetId__r.Department_Class__c in : dcListMap.keySet()
                                      and Examination_Date__c >= :sDay
                                      and Examination_Date__c < :eDay
                                      order by NewValue__c, Inspection_Times__c desc];
    for (AssetHistory__c ah : ahs) {
      System.debug('ah.Inspection_report_number__c=' + ah.Inspection_report_number__c);
      if (ah.Inspection_Report__c != null && ah.Inspection_Report__r.Status__c == '草案中') {
        if (ah.Inspection_Times__c == 1) {
          ah.Inspection_Times__c = 0;
          ahList0Flag.add(ah);
          continue;
        }
      } else if (breakInspection != ah.Inspection_report_number__c) {
        System.debug('breakInspection != ah.Inspection_report_number__c');
        isUpdateFlag = true;
        breakInspection = ah.Inspection_report_number__c;
        if (ah.Inspection_Times__c != 1) {
          ahListFlag.add(ah); // まずは処理対象
        } else {
          isUpdateFlag = false;
        }
      }
      if (isUpdateFlag) {
        if (ah.Inspection_Times__c == 1) {
          isUpdateFlag = false;
          System.debug('ah.Inspection_Times__c != 0');
          ahListFlag.remove(ahListFlag.size() - 1);
        }
      }
    }
    if (ahListFlag.size() > 0 || ahList0Flag.size() > 0) {
      for (AssetHistory__c ah : ahListFlag) {
        ah.Inspection_Times__c = 1;
      }
      ahListFlag.addAll(ahList0Flag);
      System.debug('ahListFlag.size()=' + ahListFlag.size());
      update ahListFlag;
    }
    for (AssetHistory__c ah : ahs) {
      if (ah.Inspection_Report__c != null && ah.Inspection_Report__r.Status__c == '草案中') {
        continue;
      }
      Id hpid = ah.AssetId__r.Hospital__c;
      Account2__c hpacc = updateAccount.get(hpid);
      Id dcid = ah.AssetId__r.Department_Class__c;
      Account2__c dcacc = updateAccount.get(dcid);
      Decimal times = ah.Inspection_Times__c == null ? 0 : ah.Inspection_Times__c;
      Decimal counts = ah.Inspection_Count__c == null ? 0 : ah.Inspection_Count__c;
      Integer month = ah.Examination_Date__c.month();
      setTimesAndCounts(hpacc, month, times, counts);
      setTimesAndCounts(dcacc, month, times, counts);
    }
 
    // opp RankTax
    // convertCurrencyにて、SFのレート使う(カスタム項目のレートを使うべき?)
 
    for (Opportunity opp : [
           select convertCurrency(RANK_A_inc_tax__c), convertCurrency(RANK_B_inc_tax__c), convertCurrency(RANK_C_inc_tax__c),
           convertCurrency(RANK_D_inc_tax__c), convertCurrency(RANK_E_inc_tax__c),
           Today_Rate__c, Trade__c, convertCurrency(Forecast_Amount_All_F__c), convertCurrency(ShippedAmount__c),
           Opp_Actual_ThousandY__c, BO_Forecast_ThousandY__c,
           OP_ThousandY__c, Opp_Forecast_ThousandY__c,
           //Opportunity_ThousandY__c,  //lt 20220318 注释
           Amount_Without_Tax_Thousand_F__c,
           Hospital__c, Department_Class__c
           , OCSM_RMB_Without_Tax_1000TR__c//20220111 SWAG-C8MBB6 加7.询价 lt
           from Opportunity
           where Hospital__c in :hpList
         ]) {
 
      Id hpid = opp.Hospital__c;
      Account2__c hpacc = updateAccount.get(hpid);
      Id dcid = opp.Department_Class__c;
      Account2__c dcacc = updateAccount.get(dcid);
 
      Decimal a1 = opp.RANK_A_inc_tax__c == null ? 0 : opp.RANK_A_inc_tax__c;
      Decimal a2 = opp.RANK_B_inc_tax__c == null ? 0 : opp.RANK_B_inc_tax__c;
      Decimal a3 = opp.RANK_C_inc_tax__c == null ? 0 : opp.RANK_C_inc_tax__c;
      Decimal a4 = opp.RANK_D_inc_tax__c == null ? 0 : opp.RANK_D_inc_tax__c;
      Decimal a5 = opp.RANK_E_inc_tax__c == null ? 0 : opp.RANK_E_inc_tax__c;
      Decimal fa = opp.Forecast_Amount_All_F__c == null ? 0 : opp.Forecast_Amount_All_F__c;
      Decimal sa = opp.ShippedAmount__c == null ? 0 : opp.ShippedAmount__c;
      setRankTaxCount(hpacc, a1, a2, a3, a4, a5, fa, sa);
      setRankTaxCount(dcacc, a1, a2, a3, a4, a5, fa, sa);
 
      Decimal o1 = opp.Opp_Actual_ThousandY__c == null ? 0 : opp.Opp_Actual_ThousandY__c;
      Decimal o2 = opp.BO_Forecast_ThousandY__c == null ? 0 : opp.BO_Forecast_ThousandY__c;
      Decimal o3 = opp.OP_ThousandY__c == null ? 0 : opp.OP_ThousandY__c;
      Decimal o4 = opp.Opp_Forecast_ThousandY__c == null ? 0 : opp.Opp_Forecast_ThousandY__c;
      //Decimal o5 = opp.Opportunity_ThousandY__c == null ? 0 : opp.Opportunity_ThousandY__c;//lt 20220318 注释
      //lt 20220318 start
      Decimal o5 = 0 ;
      //lt 20220318 start
      Decimal o6 = opp.Amount_Without_Tax_Thousand_F__c == null ? 0 : opp.Amount_Without_Tax_Thousand_F__c;
      //20220111 SWAG-C8MBB6 加7.询价 lt  start
      Decimal o7 = opp.OCSM_RMB_Without_Tax_1000TR__c == null ? 0 : opp.OCSM_RMB_Without_Tax_1000TR__c;
      //20220111 SWAG-C8MBB6 加7.询价 lt  end
      setOppAmount(hpacc, o1, o2, o3, o4, o5, o6, o7);
      setOppAmount(dcacc, o1, o2, o3, o4, o5, o6, o7);
    }
 
    // 报告书
    for (Report__c report : [
           select Id, Hospital_Reference__c, Department_Class_Ref__c, Date__c, RecordType.DeveloperName,
           CDS_training_report_check__c, Reporter_Job_Category__c,
           Important_product1__c, Important_product2__c, Important_product3__c, Important_product4__c, Important_product5__c,
           Important_product6__c, Important_product7__c, Important_product8__c, Important_product9__c, Important_product10__c,
           Important_product11__c, Important_product12__c, Important_product13__c, Important_product14__c, Important_product15__c,
           Important_product16__c, Important_product17__c, Important_product18__c, Important_product19__c, Important_product20__c
           from Report__c
           where RecordType.DeveloperName in ('OPD', 'NTC')
           and Status__c <> '作成中' and Status__c <> '取消し' and Status__c <> ''
           and Date__c >= : sDate                       //                and Date__c >= : thisYd
           and Date__c < : nextYd
           and Hospital_Reference__c in : hpList
           and Department_Class_Ref__c in : dcListMap.values()
         ]) {
      Id hpid = report.Hospital_Reference__c;
      Account2__c hpacc = updateAccount.get(hpid);
      Id dcid = report.Department_Class_Ref__c;
      Account2__c dcacc = updateAccount.get(dcid);
      String developerName = report.RecordType.DeveloperName;
      // 重点产品OPD次数、年度単位
      if (report.Date__c >= thisYd && developerName == 'OPD') {
        Decimal decIP1 = report.Important_product1__c;
        Decimal decIP2 = report.Important_product2__c;
        Decimal decIP3 = report.Important_product3__c;
        Decimal decIP4 = report.Important_product4__c;
        Decimal decIP5 = report.Important_product5__c;
        Decimal decIP6 = report.Important_product6__c;
        Decimal decIP7 = report.Important_product7__c;
        Decimal decIP8 = report.Important_product8__c;
        Decimal decIP9 = report.Important_product9__c;
        Decimal decIP10 = report.Important_product10__c;
        Decimal decIP11 = report.Important_product11__c;
        Decimal decIP12 = report.Important_product12__c;
        Decimal decIP13 = report.Important_product13__c;
        Decimal decIP14 = report.Important_product14__c;
        Decimal decIP15 = report.Important_product15__c;
        Decimal decIP16 = report.Important_product16__c;
        Decimal decIP17 = report.Important_product17__c;
        Decimal decIP18 = report.Important_product18__c;
        Decimal decIP19 = report.Important_product19__c;
        Decimal decIP20 = report.Important_product20__c;
        setOpdIproductCount(hpacc, decIP1, decIP2, decIP3, decIP4, decIP5, decIP6, decIP7, decIP8, decIP9, decIP10,
                            decIP11, decIP12, decIP13, decIP14, decIP15, decIP16, decIP17, decIP18, decIP19, decIP20);
        setOpdIproductCount(dcacc, decIP1, decIP2, decIP3, decIP4, decIP5, decIP6, decIP7, decIP8, decIP9, decIP10,
                            decIP11, decIP12, decIP13, decIP14, decIP15, decIP16, decIP17, decIP18, decIP19, decIP20);
      }
      // 十二肠镜CDS培训次数、年度単位
      if (report.Date__c >= thisYd && developerName == 'NTC') {
        Decimal cds = report.CDS_training_report_check__c;
        setNtcCdsCount(hpacc, cds);
        setNtcCdsCount(dcacc, cds);
      }
      // OPD,NTC回数、12月、回すタイプの集計
      if (report.Date__c < eDate) {
        Integer iMonth = report.Date__c.month();
        String job_Category = report.Reporter_Job_Category__c;
        setDoCount(hpacc, iMonth, developerName, job_Category);
        setDoCount(dcacc, iMonth, developerName, job_Category);
      }
    }
 
    // SWAG-BQ75WE 20200620 LHJ Start
    // for (Report__c report : [
    //        select Id, Hospital_Reference__c, Department_Class_Ref__c, RecordType.DeveloperName,
    //        OPD_ProductCategory1__c, OPD_ProductCategory2__c, Date__c
    //        from Report__c
    //        where RecordType.DeveloperName in ('OPD', 'SIS')
    //        and Status__c in ('批准', '提交')
    //        and Date__c >= : lastYd
    //        and Date__c < : nextYd
    //        //and Hospital_Reference__c in : hpList
    //        and Department_Class_Ref__c in : dcListMap.values()
    //      ]) {
      // Id dcid = report.Department_Class_Ref__c;
      //Account2__c acc = updateAccount.get(dcid);
      //Date date_c = report.Date__c;
      //String productCategory1 = report.OPD_ProductCategory1__c;
      //String productCategory2 = report.OPD_ProductCategory2__c;
      // if (date_c >= lastYd && date_c < thisYd) {
      //   if (productCategory1 == '3D System' || productCategory2 == '3D System') {
      //     acc.OPD_3D_pre__c += 1;
      //   }
      //   // SWAG-BC832V  保存至客户2 不使用:OPD数(上期):CYF start
      //   if (productCategory1 == 'CYF' || productCategory2 == 'CYF') {
      //     acc.OPD_CYF_pre__c += 1;
      //   }
      //   // SWAG-BC832V  保存至客户2 不使用:OPD数(上期):CYF end
      //   if (productCategory1 == 'NBI-170' || productCategory2 == 'NBI-170') {
      //     acc.OPD_CV_170_pre__c += 1;
      //   }
      //   if (productCategory1 == 'OR Imaging Products' || productCategory2 == 'OR Imaging Products') {
      //     acc.OPD_CV_190_pre__c += 1;
      //   }
      //   if (productCategory1 == 'NBI-290' || productCategory2 == 'NBI-290') {
      //     acc.OPD_CV_290_pre__c += 1;
      //   }
      //   if (productCategory1 == 'EUS' || productCategory2 == 'EUS' || productCategory1 == 'EBUS' || productCategory2 == 'EBUS') {
      //     acc.OPD_EU_ME2_pre__c += 1;
      //   }
      //   if (productCategory1 == 'OTV-S190' || productCategory2 == 'OTV-S190') {
      //     acc.OPD_OTV_S190_pre__c += 1;
      //   }
      //   if (productCategory1 == 'STMS' || productCategory2 == 'STMS') {
      //     acc.OPD_STMS_pre__c += 1;
      //   }
      //   if (productCategory1 == 'ERCP(含导丝)' || productCategory2 == 'ERCP(含导丝)') {
      //     acc.OPD_GW_pre__c += 1;
      //   }
      // } else if (date_c >= thisYd && date_c < nextYd) {
      //   if (productCategory1 == '3D System' || productCategory2 == '3D System') {
      //     acc.OPD_3D__c += 1;
      //   }
      //   // SWAG-BC832V  保存至客户2 不使用:OPD数(本期):CYF start
      //   if (productCategory1 == 'CYF' || productCategory2 == 'CYF') {
      //     acc.OPD_CYF__c += 1;
      //   }
      //   // SWAG-BC832V  保存至客户2 不使用:OPD数(本期):CYF end
      //   if (productCategory1 == 'NBI-170' || productCategory2 == 'NBI-170') {
      //     acc.OPD_CV_170__c += 1;
      //   }
      //   if (productCategory1 == 'OR Imaging Products' || productCategory2 == 'OR Imaging Products') {
      //     acc.OPD_CV_190__c += 1;
      //   }
      //   if (productCategory1 == 'NBI-290' || productCategory2 == 'NBI-290') {
      //     acc.OPD_CV_290__c += 1;
      //   }
      //   if (productCategory1 == 'EUS' || productCategory2 == 'EUS' || productCategory1 == 'EBUS' || productCategory2 == 'EBUS') {
      //     acc.OPD_EU_ME2__c += 1;
      //   }
      //   if (productCategory1 == 'OTV-S190' || productCategory2 == 'OTV-S190') {
      //     acc.OPD_OTV_S190__c += 1;
      //   }
      //   if (productCategory1 == 'STMS' || productCategory2 == 'STMS') {
      //     acc.OPD_STMS__c += 1;
      //   }
      //   if (productCategory1 == 'ERCP(含导丝)' || productCategory2 == 'ERCP(含导丝)') {
      //     acc.OPD_GW__c += 1;
      //   }
      //   if (productCategory1 == 'OER-AW' || productCategory2 == 'OER-AW') {
      //     acc.OPD_OER_AW__c += 1;
      //   }
      //   if (productCategory1 == 'OTV-S400' || productCategory2 == 'OTV-S400') {
      //     acc.OPD_OTV_S400__c += 1;
      //   }
      //   if (productCategory1 == 'CH-S400-XZ' || productCategory2 == 'CH-S400-XZ') {
      //     acc.OPD_CH_S400_XZ__c += 1;
      //   }
      //   if (productCategory1 == 'UCES-3' || productCategory2 == 'UCES-3') {
      //     acc.OPD_UCES_3__c += 1;
      //   }
      // }
      //}
    for (Report__c report : [
              select Id, Hospital_Reference__c, Department_Class_Ref__c, RecordType.DeveloperName,
              OPD_ProductCategory1__c, OPD_ProductCategory2__c, Date__c,
              Important_product1__c, Important_product2__c, Important_product3__c, Important_product4__c, Important_product5__c,
              Important_product6__c, Important_product7__c, Important_product8__c, Important_product9__c, Important_product10__c,
              Important_product11__c, Important_product12__c, Important_product13__c, Important_product14__c, Important_product15__c,
              Important_product16__c, Important_product17__c, Important_product18__c, Important_product19__c, Important_product20__c,
              Important_product21__c, Important_product22__c, Important_product23__c, Important_product24__c, Important_product25__c,
              Important_product26__c, Important_product27__c, Important_product28__c, Important_product29__c, Important_product30__c
              from Report__c
              where RecordType.DeveloperName in ('OPD', 'SIS')
              and Status__c in ('批准', '提交')
              and Inportant_Product_total__c = 1
              and Date__c >= : thisYd
              and Date__c < : nextYd
              and Department_Class_Ref__c in : dcListMap.values()
      ]) {
          Id dcid = report.Department_Class_Ref__c;
          Account2__c dcacc = updateAccount.get(dcid);
 
          // String tmpKey = '';
          List<String> tempKeyList =new List<String>();
          for (Integer i = 1; i <= 30; i++) {
              if (report.get('Important_product' + i + '__c') == 1) {
                  tempKeyList.add(String.ValueOf(i));
              }
          }
          for (Integer i = 0; i < tempKeyList.size(); i++) {
              System.debug('======0623======dcid='+ dcid);
              System.debug('======0623======i='+ i);
              setOpdCount(dcacc, tempKeyList[i], 1, OldAccount2, depToOtherDepAcc2Map , updateAccount);
          }
    }
    // LHJ SWAG-BQ75WE 20200620 End
 
    // 医療従事者 TTC参加次数,HCP
    for (Contact cont : [
           select Id, Number_of_participant_for_TTC__c, HCP__c, Strategic_dept_Class__r.ParentId, Strategic_dept_Class__c
           from Contact
           where Strategic_dept_Class__r.ParentId in : hpList
           and Strategic_dept_Class__c in : dcListMap.values()
         ]) {
      Id hpid = cont.Strategic_dept_Class__r.ParentId;
      Account2__c hpacc = updateAccount.get(hpid);
      Id dcid = cont.Strategic_dept_Class__c;
      Account2__c dcacc = updateAccount.get(dcid);
      Decimal decTTc = cont.Number_of_participant_for_TTC__c == null ? 0 : cont.Number_of_participant_for_TTC__c;
      String strHCP = cont.HCP__c == null ? '' : cont.HCP__c;
      setContactCount(hpacc, decTTc, strHCP);
      setContactCount(dcacc, decTTc, strHCP);
    }
    
    //  SWAG-BD24SU 检索对应的保有设备,赋值到战略科室的客户2对应的发货数 start
    for (Asset Ast : [
           select Id, Department_Class__c,
           Important_product1__c, Important_product2__c,
           Important_product3__c, Important_product4__c,
           Important_product5__c, Important_product6__c,
           Important_product7__c, Important_product8__c,
           Important_product9__c, Important_product10__c,
           Important_product11__c, Important_product12__c,
           Important_product13__c, Important_product14__c,
           Important_product15__c, Important_product16__c,
           Important_product17__c, Important_product18__c,
           Important_product19__c, Important_product20__c,
           Important_product21__c, Important_product22__c,
           Important_product23__c, Important_product24__c,
           Important_product25__c, Important_product26__c,
           Important_product27__c, Important_product28__c,
           Important_product29__c, Important_product30__c
           // WLIG-BTNDLX start
           , AssetPacking_list__c
           //WLIG-BTNDLX end
           //WLIG-BXT9DQ 【委托】【重要】目标客户统计中的“发货数”需求 精琢技术 2021/02/26 start
           ,Shipping_Finished_Day_Func__c 
           //WLIG-BXT9DQ 【委托】【重要】目标客户统计中的“发货数”需求 精琢技术 2021/02/26 end
           from Asset
           //注释原逻辑 替换为询价上的20.最新发货日 start
           //WLIG-BXT9DQ 【委托】【重要】目标客户统计中的“发货数”需求 精琢技术 2021/02/26 start
           //SWAG-C66D25 恢复为发货日,另外增加手工除外的条件 Start
           where Product2.Important_product__c = true
               and Posting_Date__c
               >= :thisYd
               and Posting_Date__c   < :nextYd
               and Department_Class__c in : dcListMap.values()
               and RecordTypeID__c   = '01210000000kOPMAA2'
               and Account.parent.parent.Management_Code__c != '9999999'
               and Information_From__c != '手动输入'
            // where Product2.Important_product__c = true
            //    and Shipping_Finished_Day_Func__c
            //    >= :thisYd
            //    and Shipping_Finished_Day_Func__c   < :nextYd
            //    and Department_Class__c in : dcListMap.values()
            //    and RecordTypeID__c   = '01210000000kOPMAA2'
            //    and Account.parent.parent.Management_Code__c != '9999999'
            //注释原逻辑 替换为询价上的20.最新发货日 end   
            ////SWAG-C66D25 恢复为发货日,另外增加手工除外的条件 End
            //WLIG-BXT9DQ 【委托】【重要】目标客户统计中的“发货数”需求 精琢技术 2021/02/26 end
 
         ]) {
      Id dcid = Ast.Department_Class__c;
      Account2__c dcacc = updateAccount.get(dcid);
 
      String tmpKey = '';
      // WLIG-BTNDLX start
      Decimal tmpCnt = Ast.AssetPacking_list__c == null ? 1: Ast.AssetPacking_list__c ;
      // 原逻辑
      //Decimal tmpCnt = 1;
      // WLIG-BTNDLX end
      for (Integer i = 1; i <= 30; i++) {
        if (Ast.get('Important_product' + i + '__c') == true) {
          tmpKey = '' + i;
          break;
        }
      }
      // SWAG-BQ75WE update by vivek 2020/06/11 start
      // setDeliveryCount(dcacc, tmpKey, tmpCnt, OldAccount2, HPtoGIAcc2Map , updateAccount);
      setDeliveryCount(dcacc, tmpKey, tmpCnt, OldAccount2, depToOtherDepAcc2Map , updateAccount);
      // SWAG-BQ75WE update by vivek 2020/06/11 end
    }
    //  SWAG-BD24SU 检索对应的保有设备,赋值到战略科室的客户2对应的发货数 end
    System.debug('======thisYd'+thisYd);
    System.debug('======nextYd'+nextYd);
    //Map<String, String> keyMap = new Map<String, String>();
    for (OpportunityLineItem oli : [
           select Id, Quantity, OpportunityId, Opportunity.Department_Class__c, Opportunity.Contract_recognized_day_1__c, Opportunity.Close_Forecasted_Date__c, Opportunity.Competitor__c,
           OCM_Sales_Forecast_Product1_Number__c, OCM_Sales_Forecast_Product2_Number__c, OCM_Sales_Forecast_Product3_Number__c,
           OCM_Sales_Forecast_Product4_Number__c, OCM_Sales_Forecast_Product5_Number__c, OCM_Sales_Forecast_Product6_Number__c,
           OCM_Sales_Forecast_Product7_Number__c, OCM_Sales_Forecast_Product8_Number__c, OCM_Sales_Forecast_Product9_Number__c,
           OCM_Sales_Forecast_Product10_Number__c, OCM_Sales_Forecast_Product11_Number__c, OCM_Sales_Forecast_Product12_Number__c,
           OCM_Sales_Forecast_Product13_Number__c, OCM_Sales_Forecast_Product14_Number__c, OCM_Sales_Forecast_Product15_Number__c,
           OCM_Sales_Forecast_Product16_Number__c, OCM_Sales_Forecast_Product17_Number__c, OCM_Sales_Forecast_Product18_Number__c,
           OCM_Sales_Forecast_Product19_Number__c, OCM_Sales_Forecast_Product20_Number__c, OCM_Sales_Forecast_Product21_Number__c,
           OCM_Sales_Forecast_Product22_Number__c, OCM_Sales_Forecast_Product23_Number__c, OCM_Sales_Forecast_Product24_Number__c,
           OCM_Sales_Forecast_Product25_Number__c, OCM_Sales_Forecast_Product26_Number__c, OCM_Sales_Forecast_Product27_Number__c,
           OCM_Sales_Forecast_Product28_Number__c, OCM_Sales_Forecast_Product29_Number__c, OCM_Sales_Forecast_Product30_Number__c
           from OpportunityLineItem
           where PricebookEntry.Product2.Important_product__c = true
               and Opportunity.order_Date_For_Report__c
               >= :thisYd
               and Opportunity.order_Date_For_Report__c < :nextYd
               and Opportunity.StageName__c in ('注残', '发货', '完毕')
               and Opportunity.Department_Class__c in : dcListMap.values()
         ]) {
      Id dcid = oli.Opportunity.Department_Class__c;
      Account2__c dcacc = updateAccount.get(dcid);
 
      String tmpKey = '';
      Decimal tmpCnt = oli.Quantity == null ? 0 : oli.Quantity;
      for (Integer i = 1; i <= 30; i++) {
        if (oli.get('OCM_Sales_Forecast_Product' + i + '_Number__c') == 'true') {
          tmpKey = '' + i;
          break;
        }
      }
      //String cntKey = oli.OpportunityId + '_' + tmpKey;
      //if (keyMap.containsKey(cntKey) == false) {
      //keyMap.put(cntKey, cntKey);
      // 2020-06-12 SWAG-BQ75WE 额外传递depToOtherDepAcc2Map和updateAccount start
      // setOrderCount(dcacc, tmpKey, tmpCnt, OldAccount2);
      setOrderCount(dcacc, tmpKey, tmpCnt, OldAccount2, depToOtherDepAcc2Map , updateAccount);
      // 2020-06-12 SWAG-BQ75WE 额外传递depToOtherDepAcc2Map和updateAccount end
      //}
    }
 
    //Map<String, String> keyMap2 = new Map<String, String>();
    for (OpportunityLineItem oli : [
           select Id, Quantity, OpportunityId, Opportunity.Department_Class__c, Opportunity.Contract_recognized_day_1__c, Opportunity.Close_Forecasted_Date__c, Opportunity.Competitor__c,
           OCM_Sales_Forecast_Product1_Number__c, OCM_Sales_Forecast_Product2_Number__c, OCM_Sales_Forecast_Product3_Number__c,
           OCM_Sales_Forecast_Product4_Number__c, OCM_Sales_Forecast_Product5_Number__c, OCM_Sales_Forecast_Product6_Number__c,
           OCM_Sales_Forecast_Product7_Number__c, OCM_Sales_Forecast_Product8_Number__c, OCM_Sales_Forecast_Product9_Number__c,
           OCM_Sales_Forecast_Product10_Number__c, OCM_Sales_Forecast_Product11_Number__c, OCM_Sales_Forecast_Product12_Number__c,
           OCM_Sales_Forecast_Product13_Number__c, OCM_Sales_Forecast_Product14_Number__c, OCM_Sales_Forecast_Product15_Number__c,
           OCM_Sales_Forecast_Product16_Number__c, OCM_Sales_Forecast_Product17_Number__c, OCM_Sales_Forecast_Product18_Number__c,
           OCM_Sales_Forecast_Product19_Number__c, OCM_Sales_Forecast_Product20_Number__c, OCM_Sales_Forecast_Product21_Number__c,
           OCM_Sales_Forecast_Product22_Number__c, OCM_Sales_Forecast_Product23_Number__c, OCM_Sales_Forecast_Product24_Number__c,
           OCM_Sales_Forecast_Product25_Number__c, OCM_Sales_Forecast_Product26_Number__c, OCM_Sales_Forecast_Product27_Number__c,
           OCM_Sales_Forecast_Product28_Number__c, OCM_Sales_Forecast_Product29_Number__c, OCM_Sales_Forecast_Product30_Number__c
           from OpportunityLineItem
           where PricebookEntry.Product2.Important_product__c = true
               and Opportunity.Shipping_Date_For_Report__c   >= :thisYd
               and Opportunity.StageName__c = '询价'
                   and Opportunity.Shipping_Date_For_Report__c   < :nextYd
                   and Opportunity.Competitor__c in ('A1', 'A', 'B', 'C')
                   and Opportunity.Department_Class__c in : dcListMap.values()
         ]) {
      Id dcid = oli.Opportunity.Department_Class__c;
      Account2__c dcacc = updateAccount.get(dcid);
 
      String tmpKey = '';
      Decimal tmpCnt = oli.Quantity == null ? 0 : oli.Quantity;
      for (Integer i = 1; i <= 30; i++) {
        if (oli.get('OCM_Sales_Forecast_Product' + i + '_Number__c') == 'true') {
          tmpKey = '' + i;
          break;
        }
      }
      //String cntKey = oli.OpportunityId + '_' + tmpKey;
      //if (keyMap2.containsKey(cntKey) == false) {
      //keyMap2.put(cntKey, cntKey);
      // 2020-06-12 SWAG-BQ75WE 额外传递depToOtherDepAcc2Map和updateAccount start
      // setOppCount(dcacc, tmpKey, tmpCnt, OldAccount2);
      setOppCount(dcacc, tmpKey, tmpCnt, OldAccount2, depToOtherDepAcc2Map , updateAccount);
      // 2020-06-12 SWAG-BQ75WE 额外传递depToOtherDepAcc2Map和updateAccount end
      
      //}
    }
    //20200825 ljh SWAG-BRY6PF  add start
    for (AggregateResult oli:[select  Rental_Apply__c,Product2__c,Rental_Apply__r.Strategic_dept__c  
          from Rental_Apply_Equipment_Set__c where 
          //IsOPD_Account__c = 1 AND 
          Rental_Apply__r.Strategic_dept__c in : dcListMap.values() 
          AND Demo_purpose1__c = '产品试用'
          and Bollow_Date__c   >= :thisYd
          and Bollow_Date__c   < :nextYd
          group by Rental_Apply__c,Product2__c,Rental_Apply__r.Strategic_dept__c
          ]) {
        Id dcid = ID.valueOf(String.valueOf(oli.get('Strategic_dept__c')));
        Account2__c dcacc = updateAccount.get(dcid);
        String tmpKey = '';
        String tempProduct2 = String.valueOf(oli.get('Product2__c'));
        if(tempProduct2 != null&&String.isNotBlank(tempProduct2)){
          tmpKey = tempProduct2.substring(0,2);
        }
        setRentalCount(dcacc, tmpKey, 1, OldAccount2, depToOtherDepAcc2Map , updateAccount);
    } 
    //20200825 ljh SWAG-BRY6PF  add end
 
 
    //SWAG-C9WCE5 战略科室和目标客户设定页面增加字段  lt 20211228 start
 
    //Map<战略科室,数量> (CountHostsNumber)
    Map<String,Integer> CountHostsMap = new Map<String,Integer>(); //所有主机
    Map<String,Integer> CountRivalHostsMap = new Map<String,Integer>(); //对手主机 
    
    for(Asset chn : [
            Select Id, Department_Class__c, Product2.ProductClass__c, Product2.Brand_Name__c, Product2.ProductCategory__c
            From Asset
            Where ((Product2.ProductClass__c = '主机' )
                Or (Product2.ProductClass__c = '能量'
                    And (Product2.ProductCategory__c = 'STMS' 
                      Or Product2.ProductCategory__c = 'ESG-400' 
                      Or Product2.ProductCategory__c = '主机')))
                And Department_Class__c in : dcListMap.values()
    ]){
      //所有主机数量
      if(CountHostsMap.containsKey(chn.Department_Class__c)){
        CountHostsMap.put(chn.Department_Class__c, CountHostsMap.get(chn.Department_Class__c)+1);
      }else{
        CountHostsMap.put(chn.Department_Class__c, 1);
      }
 
      //对手主机数量
      System.debug('主机厂商:' + chn.Product2.Brand_Name__c);
      System.debug('判断是否奥林巴斯主机:' + (!'奥林巴斯'.equals(chn.Product2.Brand_Name__c)) + '|' + (chn.Product2.Brand_Name__c != '奥林巴斯'));
      if(!'奥林巴斯'.equals(chn.Product2.Brand_Name__c)){
        if(CountRivalHostsMap.containsKey(chn.Department_Class__c)){
          CountRivalHostsMap.put(chn.Department_Class__c, CountRivalHostsMap.get(chn.Department_Class__c)+1);
        }else{
          CountRivalHostsMap.put(chn.Department_Class__c, 1);
        }
      }
 
      Id dcid = chn.Department_Class__c; //战略科室id
      Account2__c acc = updateAccount.get(dcid); //战略科室对应得 客户2
 
      if(acc != null){
        Integer CountHostsNumber = CountHostsMap.containsKey(dcid)?CountHostsMap.get(dcid):0;
        Integer CountRivalHostsNumber = CountRivalHostsMap.containsKey(dcid)?CountRivalHostsMap.get(dcid):0;
 
        acc.AllHostsNumber__c = CountHostsNumber;       //所有主机数
        acc.AllHostsNumber_first__c = CountHostsNumber;       //所有主机数(期初)
        acc.RivalHostsNumber__c = CountRivalHostsNumber;  //对手主机数
        acc.RivalHostsNumber_first__c = CountRivalHostsNumber;  //对手主机数(期初)
 
      }
    }
 
    //SWAG-C9WCE5 战略科室和目标客户设定页面增加字段  lt 20211228 end
    
    StaticParameter.EscapeNFM001Trigger = true;
    StaticParameter.EscapeNFM001AgencyContractTrigger = true;
    // for (Account2__c acc : updateAccount.values()) {
    //   System.debug(acc);
    // }
    update updateAccount.values();
 
  }
 
  global void finish(Database.BatchableContext BC) {
    // 今回はやることないです
  }
 
  // batch集計の基準日
  public static Date getTargetDay() {
    Date targetDay2 = null;
    {
      Date targetDay = Date.today();
      if (targetDay.month() == 4 && targetDay.day() <= intYearDelayDay) {
        targetDay2 = Date.valueOf(targetDay.year() + '-4-1').addDays(-1);
      } else {
        targetDay2 = targetDay.addDays(-1);
      }
    }
    return targetDay2;
  }
 
  private void initAccountCount(Account2__c acc, Integer targetmonth) {
    //        if (acc.Id == '0011000000V9OU4') {
    //            BatchIF_Log__c iflog = new BatchIF_Log__c();
    //            iflog.Type__c = 'RollupToHPBatch';
    //            iflog.Log__c  = '0011000000V9OU4 initAccountCount(' + targetmonth + ')\n';
    //            insert iflog;
    //        }
    acc.Latest_Proposal_document_Submit_day__c = null;
    acc.Proposal_Submit_Status__c = null;
    acc.SpecialAsset_CV_Count__c = 0;
    acc.SpecialAsset_CV_Other_Count__c = 0;
    acc.SpecialAsset_GIF_Count__c = 0;
    acc.SpecialAsset_OTV_Count__c = 0;
    acc.JF_TJF_scope_count__c = 0;
    acc.USG_400_count__c = 0;
    acc.SpecialAsset_C3_Electronic_Mirror__c = 0;
    acc.SpecialAsset_C3_Fiber_Mirror__c = 0;
    acc.CV_290_Count__c = 0;
    acc.CV_260SL_Count__c = 0;
    acc.CV_260_Count__c = 0;
    acc.CV_240_Other_Count__c = 0;
    acc.CV_Other_Count__c = 0;
    acc.D3_System_Count__c = 0;
    acc.CV_190_Count__c = 0;
    acc.OTV_S190_Count__c = 0;
    acc.OTV_S7Pro_Count__c = 0;
    acc.CV_180_Count__c = 0;
    acc.OTV_S7_Count__c = 0;
    acc.OTV_S6_Count__c = 0;
    acc.OTV_Other_Count__c = 0;
    acc.SSG2_Count__c = 0;
    acc.ESG_400_Count__c = 0;
    acc.G400_Count__c = 0;
    acc.HD_EndoEYE_Count__c = 0;
    acc.SD_EndoEYE_Count__c = 0;
    acc.Video_URF_Count__c = 0;
    acc.Video_CYF_Count__c = 0;
    acc.Video_ENF_Count__c = 0;
    acc.Competitor_Count__c = 0;
    acc.Competitor_Count_SP__c = 0;
    acc.Energy_Processor_Count__c = 0;
    acc.Energy_Processor_Count_SP__c = 0;
    acc.Other_Competitor_Product_Count__c = 0;
    //20200825 ljh SWAG-BRY6PF  add start
    acc.Rental_Cnt_CV290__c = 0;
    acc.Rental_Cnt_290MiroGI__c = 0;     
    acc.Rental_Cnt_290MiroGI_Plus__c = 0;
    acc.Rental_Cnt_290MiroGI_CF__c = 0;
    acc.Rental_Cnt_GFUCT260__c = 0;
    acc.Rental_Cnt_EUME2__c = 0;
    acc.Rental_Cnt_290MiroBF__c = 0;
    acc.Rental_Cnt_OTVS400__c = 0;
    acc.Rental_Cnt_OTVS300__c = 0;
    acc.Rental_Cnt_CV170__c = 0;
    acc.Rental_Cnt_CV190__c = 0;
    acc.Rental_Cnt_OTVS190__c = 0;
    acc.Rental_Cnt_ESG400__c = 0;
    acc.Rental_Cnt_USG400__c = 0;
    acc.Rental_Cnt_TB__c = 0;
    //SWAG-BSC5WP you-20201021 start
    acc.Asset_Cnt_290MiroBF__c = 0;
    acc.Asset_Cnt_290MiroGI__c = 0;
    acc.Asset_Cnt_290MiroGI_CF__c = 0;
    acc.Asset_Cnt_290MiroGI_Plus__c = 0;
    acc.Asset_Cnt_CV170__c = 0;
    acc.Asset_Cnt_CV190__c = 0;
    acc.Asset_Cnt_CV290__c = 0;
    acc.Asset_Cnt_ESG400__c = 0;
    acc.Asset_Cnt_EUME2__c = 0;
    acc.Asset_Cnt_GFUCT260__c = 0;
    acc.Asset_Cnt_OTVS190__c = 0;
    acc.Asset_Cnt_OTVS300__c = 0;
    acc.Asset_Cnt_OTVS400__c = 0;
    acc.Asset_Cnt_TB__c = 0;
    acc.Asset_Cnt_USG400__c = 0;
    //SWAG-BSC5WP you-20201021 end
 
    //20200825 ljh SWAG-BRY6PF  add start
    for (Integer columnNameInt = targetmonth + intMonth; columnNameInt <= targetmonth; columnNameInt++) {
      String columnNameStr = columnNameInt + '__c';
      if (columnNameInt <= 0) {
        columnNameStr = (columnNameInt + 12) + '__c';
      }
      acc.put('This_year_inspection_times' + columnNameStr, 0);
      acc.put('This_year_inspection_count' + columnNameStr, 0);
    }
 
    acc.AVG_Elapsed_day_from_Repair__c = 0;
    acc.AVG_Elapsed_day_from_Repair2__c = 0;
    acc.SUM_Repair_List_Price__c = 0;
    acc.SUM_Repair_Billing_Amount__c = 0;
    acc.AVG_Elapsed_day_from_Repair_Lastyear__c = 0;
    acc.AVG_Elapsed_day_from_Repair2_Lastyear__c = 0;
    acc.SUM_Repair_List_Price_Lastyear__c = 0;
    acc.SUM_Repair_Billing_Amount_Lastyear__c = 0;
    acc.SUM_Unpaid_Amount__c = 0;
 
    acc.RANK_A_tax_HP__c = 0;
    acc.RANK_B_tax_HP__c = 0;
    acc.RANK_C_tax_HP__c = 0;
    acc.RANK_D_tax_HP__c = 0;
    acc.RANK_E_tax_HP__c = 0;
    acc.Forecast_Amount_HP__c = 0;
    acc.ShippedAmount_HP__c = 0;
 
    acc.Opp_Actual_ThousandY__c = 0;
    acc.BO_Forecast_ThousandY__c = 0;
    acc.OP_ThousandY__c = 0;
    
    acc.Opp_Forecast_ThousandY__c = 0;
    //acc.Opportunity_ThousandY__c = 0;//lt 20220318 注释
    acc.Amount_Without_Tax_Thousand__c = 0;
 
    //20220111 SWAG-C8MBB6 加7.询价 lt  start
    acc.AllOP_ThousandY__c = 0;
    //20220111 SWAG-C8MBB6 加7.询价 lt  end
 
    acc.Important_product1__c = 0;
    acc.Important_product2__c = 0;
    acc.Important_product3__c = 0;
    acc.Important_product4__c = 0;
    acc.Important_product5__c = 0;
    acc.Important_product6__c = 0;
    acc.Important_product7__c = 0;
    acc.Important_product8__c = 0;
    acc.Important_product9__c = 0;
    acc.Important_product10__c = 0;
    acc.Important_product11__c = 0;
    acc.Important_product12__c = 0;
    acc.Important_product13__c = 0;
    acc.Important_product14__c = 0;
    acc.Important_product15__c = 0;
    acc.Important_product16__c = 0;
    acc.Important_product17__c = 0;
    acc.Important_product18__c = 0;
    acc.Important_product19__c = 0;
    acc.Important_product20__c = 0;
 
    acc.CDS_training_number_this_year__c = 0;
 
    acc.ThisYear1__c = 0;
    acc.ThisYear2__c = 0;
    acc.ThisYear3__c = 0;
    acc.ThisYear4__c = 0;
    acc.ThisYear5__c = 0;
    acc.ThisYear6__c = 0;
    acc.ThisYear7__c = 0;
    acc.ThisYear8__c = 0;
    acc.ThisYear9__c = 0;
    acc.ThisYear10__c = 0;
    acc.ThisYear11__c = 0;
    acc.ThisYear12__c = 0;
    acc.ThisYear1_FSE__c = 0;
    acc.ThisYear2_FSE__c = 0;
    acc.ThisYear3_FSE__c = 0;
    acc.ThisYear4_FSE__c = 0;
    acc.ThisYear5_FSE__c = 0;
    acc.ThisYear6_FSE__c = 0;
    acc.ThisYear7_FSE__c = 0;
    acc.ThisYear8_FSE__c = 0;
    acc.ThisYear9_FSE__c = 0;
    acc.ThisYear10_FSE__c = 0;
    acc.ThisYear11_FSE__c = 0;
    acc.ThisYear12_FSE__c = 0;
 
    // SWAG-BBBAKG start 能量拜访数初始化 start
    acc.ThisYear1_Eng__c = 0;
    acc.ThisYear2_Eng__c = 0;
    acc.ThisYear3_Eng__c = 0;
    acc.ThisYear4_Eng__c = 0;
    acc.ThisYear5_Eng__c = 0;
    acc.ThisYear6_Eng__c = 0;
    acc.ThisYear7_Eng__c = 0;
    acc.ThisYear8_Eng__c = 0;
    acc.ThisYear9_Eng__c = 0;
    acc.ThisYear10_Eng__c = 0;
    acc.ThisYear11_Eng__c = 0;
    acc.ThisYear12_Eng__c = 0;
    // SWAG-BBBAKG start 能量拜访数初始化 end
 
 
    acc.DoNTCHospital_ThisYear1__c = 0;
    acc.DoNTCHospital_ThisYear2__c = 0;
    acc.DoNTCHospital_ThisYear3__c = 0;
    acc.DoNTCHospital_ThisYear4__c = 0;
    acc.DoNTCHospital_ThisYear5__c = 0;
    acc.DoNTCHospital_ThisYear6__c = 0;
    acc.DoNTCHospital_ThisYear7__c = 0;
    acc.DoNTCHospital_ThisYear8__c = 0;
    acc.DoNTCHospital_ThisYear9__c = 0;
    acc.DoNTCHospital_ThisYear10__c = 0;
    acc.DoNTCHospital_ThisYear11__c = 0;
    acc.DoNTCHospital_ThisYear12__c = 0;
    acc.DoOPDHospital_ThisYear1__c = 0;
    acc.DoOPDHospital_ThisYear2__c = 0;
    acc.DoOPDHospital_ThisYear3__c = 0;
    acc.DoOPDHospital_ThisYear4__c = 0;
    acc.DoOPDHospital_ThisYear5__c = 0;
    acc.DoOPDHospital_ThisYear6__c = 0;
    acc.DoOPDHospital_ThisYear7__c = 0;
    acc.DoOPDHospital_ThisYear8__c = 0;
    acc.DoOPDHospital_ThisYear9__c = 0;
    acc.DoOPDHospital_ThisYear10__c = 0;
    acc.DoOPDHospital_ThisYear11__c = 0;
    acc.DoOPDHospital_ThisYear12__c = 0;
    acc.DoOPDHospital_ThisYear1_FSE__c = 0;
    acc.DoOPDHospital_ThisYear2_FSE__c = 0;
    acc.DoOPDHospital_ThisYear3_FSE__c = 0;
    acc.DoOPDHospital_ThisYear4_FSE__c = 0;
    acc.DoOPDHospital_ThisYear5_FSE__c = 0;
    acc.DoOPDHospital_ThisYear6_FSE__c = 0;
    acc.DoOPDHospital_ThisYear7_FSE__c = 0;
    acc.DoOPDHospital_ThisYear8_FSE__c = 0;
    acc.DoOPDHospital_ThisYear9_FSE__c = 0;
    acc.DoOPDHospital_ThisYear10_FSE__c = 0;
    acc.DoOPDHospital_ThisYear11_FSE__c = 0;
    acc.DoOPDHospital_ThisYear12_FSE__c = 0;
 
    acc.Service_contract_active_number_HP__c = 0;
    acc.Sum_Number_of_participant_for_TTC__c = 0;
    acc.Academic_HCP_Count__c = 0;
    acc.General_HCP_Count__c = 0;
    acc.Product_HCP_Count__c = 0;
 
    acc.OPD_3D_pre__c = 0;
    acc.OPD_CV_170_pre__c = 0;
    acc.OPD_CV_190_pre__c = 0;
    acc.OPD_CV_290_pre__c = 0;
    acc.OPD_EU_ME2_pre__c = 0;
    acc.OPD_OTV_S190_pre__c = 0;
    acc.OPD_STMS_pre__c = 0;
    acc.OPD_GW_pre__c = 0;
 
    // SWAG-BC832V 初始化  CYF 为 0 start
    acc.OPD_CYF_pre__c = 0;
    acc.OPD_CYF__c = 0;
    // SWAG-BC832V 初始化 CYF 为 0 end
 
    acc.OPD_3D__c = 0;
    acc.OPD_CV_170__c = 0;
    acc.OPD_CV_190__c = 0;
    acc.OPD_CV_290__c = 0;
    acc.OPD_EU_ME2__c = 0;
    acc.OPD_OTV_S190__c = 0;
    acc.OPD_STMS__c = 0;
    acc.OPD_GW__c = 0;
    acc.OPD_OER_AW__c = 0;
    acc.OPD_OTV_S400__c = 0;
    acc.OPD_CH_S400_XZ__c = 0;
    acc.OPD_UCES_3__c = 0;
 
    acc.Order_cnt_290Miro__c = 0;
    acc.Order_cnt_3D__c = 0;
    acc.Order_cnt_CHS400XZ__c = 0;
    acc.Order_cnt_CV170__c = 0;
    acc.Order_cnt_CV190__c = 0;
    acc.Order_cnt_CV290__c = 0;
    acc.Order_cnt_EUME2__c = 0;
    acc.Order_cnt_OERAW__c = 0;
    acc.Order_cnt_OTVS190__c = 0;
    acc.Order_cnt_OTVS400__c = 0;
    acc.Order_cnt_UCES3__c = 0;
    acc.Order_cnt_USG400__c = 0;
    acc.Order_cnt_GW__c = 0;
    //  SWAG-BD24SU 初始化发货数 start
    acc.Delivery_cnt_3D__c = 0;
    acc.Delivery_cnt_CV290__c = 0;
    acc.Delivery_cnt_CYF__c = 0;
    acc.Delivery_cnt_EUME2__c = 0;
    acc.Delivery_cnt_OERAW__c = 0;
    acc.Delivery_cnt_OTVS400__c = 0;
    acc.Delivery_cnt_USG400__c = 0;
    acc.Delivery_cnt_CV170__c = 0;
    acc.Delivery_cnt_CV190__c = 0;
    acc.Delivery_cnt_OTVS190__c = 0;
    //  SWAG-BD24SU 初始化发货数 end
 
    // SWAG-BC832V 初始化  CYF 为 0 start
    acc.Order_cnt_CYF__c     = 0;
    acc.Opp_cnt_CYF__c       = 0;
    // SWAG-BC832V 初始化 CYF 为 0 end
 
    acc.Opp_cnt_290Miro__c = 0;
    acc.Opp_cnt_3D__c = 0;
    acc.Opp_cnt_CHS400XZ__c = 0;
    acc.Opp_cnt_CV170__c = 0;
    acc.Opp_cnt_CV190__c = 0;
    acc.Opp_cnt_CV290__c = 0;
    acc.Opp_cnt_EUME2__c = 0;
    acc.Opp_cnt_OERAW__c = 0;
    acc.Opp_cnt_OTVS190__c = 0;
    acc.Opp_cnt_OTVS400__c = 0;
    acc.Opp_cnt_UCES3__c = 0;
    acc.Opp_cnt_USG400__c = 0;
    acc.Opp_cnt_GW__c = 0;
    // SWAG-BQ75WE 20200609 Start
    acc.Opp_cnt_290MiroBF__c = 0;
    acc.Opp_cnt_290MiroGI__c = 0;
    acc.Opp_cnt_290MiroGI_Plus__c = 0;
    acc.Opp_cnt_290MiroGI_CF__c = 0;
    acc.Opp_cnt_GFUCT260__c = 0;
    acc.Opp_cnt_OTVS300__c = 0;
    acc.Opp_cnt_ESG400__c = 0;
    acc.Opp_cnt_TB__c = 0;
    
    acc.Delivery_cnt_290MiroBF__c = 0;
    acc.Delivery_cnt_290MiroGI__c = 0;
    acc.Delivery_cnt_290MiroGI_Plus__c = 0;
    acc.Delivery_cnt_290MiroGI_CF__c = 0;
    acc.Delivery_cnt_GFUCT260__c = 0;
    acc.Delivery_cnt_OTVS300__c = 0;
    acc.Delivery_cnt_ESG400__c = 0;
    acc.Delivery_cnt_TB__c = 0;
    
    acc.Order_cnt_290MiroBF__c = 0;
    acc.Order_cnt_290MiroGI__c = 0;
    acc.Order_cnt_290MiroGI_Plus__c = 0;
    acc.Order_cnt_290MiroGI_CF__c = 0;
    acc.Order_cnt_GFUCT260__c = 0;
    acc.Order_cnt_OTVS300__c = 0;
    acc.Order_cnt_ESG400__c = 0;
    acc.Order_cnt_TB__c = 0;
 
    acc.OPD_USG400__c = 0;
    acc.OPD_OTV_S400__c = 0;
    acc.OPD_290Miro_BF__c = 0;
    acc.OPD_290Miro_GI__c = 0;
    acc.OPD_290Miro_GICF__c = 0;
    acc.OPD_290Miro_GIPlus__c = 0;
    acc.OPD_GFUCT260__c = 0;
    acc.OPD_OTV_S300__c = 0;
    acc.OPD_ESG400__c = 0;
    acc.OPD_TB__c = 0;
    // SWAG-BQ75WE 20200609 End
    
  }
 
  private void setTimesAndCounts(Account2__c acc, Integer iMonth, Decimal times, Decimal counts) {
    acc.put('This_year_inspection_times' + iMonth + '__c', (Decimal) acc.get('This_year_inspection_times' + iMonth + '__c') + times);
    acc.put('This_year_inspection_count' + iMonth + '__c', (Decimal) acc.get('This_year_inspection_count' + iMonth + '__c') + counts);
  }
 
  private void setAssetCategoryCount(Account2__c acc, String strDCL, String strC3, String strC4, String strC5, Integer cntId) {
//        if (acc.Id == '0011000000V9OU4') {
//            BatchIF_Log__c iflog = new BatchIF_Log__c();
//            iflog.Type__c = 'RollupToHPBatch';
//            iflog.Log__c  = '0011000000V9OU4 ' + strDCL + ' start\n';
//            iflog.Log__c += 'strC3=' + strC3 + '\n';
//            iflog.Log__c += 'strC4=' + strC4 + '\n';
//            iflog.Log__c += 'strC5=' + strC5 + '\n';
//            iflog.Log__c += 'cntId=' + cntId + '\n';
//            insert iflog;
//        }
    if ((strDCL == '消化科' || strDCL == '呼吸科') && strC4 == 'CV') {
      acc.SpecialAsset_CV_Count__c += cntId;
    }
    if (!(strDCL == '消化科' || strDCL == '呼吸科') && strC4 == 'CV') {
      acc.SpecialAsset_CV_Other_Count__c += cntId;
    }
    if (strC3 == '电子镜' && strC4 == 'GIF') {
      acc.SpecialAsset_GIF_Count__c += cntId;
    }
    if (strC3 == '电子镜') {
      acc.SpecialAsset_C3_Electronic_Mirror__c += cntId;
    }
    if (strC3 == '纤维镜') {
      acc.SpecialAsset_C3_Fiber_Mirror__c += cntId;
    }
    if (strC4 == 'OTV') {
      acc.SpecialAsset_OTV_Count__c += cntId;
    }
    if (strC4 == 'JF') {
      acc.JF_TJF_scope_count__c += cntId;
    }
    if (strC5 == 'USG-400') {
      acc.USG_400_count__c += cntId;
    }
    if (strC4 == 'CV' && strC5 == '290系列') {
      acc.CV_290_Count__c += cntId;
    }
    if (strC4 == 'CV' && strC5 == '260SL系列') {
      acc.CV_260SL_Count__c += cntId;
    }
    if (strC4 == 'CV' && strC5 == '260系列') {
      acc.CV_260_Count__c += cntId;
    }
    if (strC4 == 'CV' && (strC5 == '240系列' || strC5 == '200系列其他')) {
      acc.CV_240_Other_Count__c += cntId;
    }
    if (strC4 == 'CV' && !(strC5 == '290系列' || strC5 == '260SL系列' || strC5 == '260系列' || strC5 == '240系列' || strC5 == '200系列其他')) {
      acc.CV_Other_Count__c += cntId;
    }
    if (strC4 == 'CV' && strC5 == '190系列') {
      acc.CV_190_Count__c += cntId;
    }
    if (strC4 == 'OTV' && strC5 == 'OTV-S190系列') {
      acc.OTV_S190_Count__c += cntId;
    }
    if (strC4 == 'OTV' && strC5 == 'OTV-S7Pro系列') {
      acc.OTV_S7Pro_Count__c += cntId;
    }
    if (strC4 == 'CV' && strC5 == '180系列') {
      acc.CV_180_Count__c += cntId;
    }
    if (strC4 == 'OTV' && strC5 == 'OTV-S7系列') {
      acc.OTV_S7_Count__c += cntId;
    }
    if (strC4 == 'OTV' && strC5 == 'OTV-S6系列') {
      acc.OTV_S6_Count__c += cntId;
    }
    if (strC4 == 'OTV' && !(strC5 == 'OTV-S190系列' || strC5 == 'OTV-S7Pro系列' || strC5 == 'OTV-S7系列' || strC5 == 'OTV-S6系列')) {
      acc.OTV_Other_Count__c += cntId;
    }
    if (strC3 == '能量主机' && strC5 == 'G2') {
      acc.SSG2_Count__c += cntId;
    }
    if (strC3 == '能量主机' && strC5 == 'G400') {
      acc.G400_Count__c += cntId;
    }
    if (strC3 == '电子腹腔镜' && strC5 == 'HD EndoEYE') {
      acc.HD_EndoEYE_Count__c += cntId;
    }
    if (strC3 == '电子腹腔镜' && strC5 == 'SD EndoEYE') {
      acc.SD_EndoEYE_Count__c += cntId;
    }
    if (strC3 == '电子镜' && strC4 == 'URF') {
      acc.Video_URF_Count__c += cntId;
    }
    if (strC3 == '电子镜' && strC4 == 'CYF') {
      acc.Video_CYF_Count__c += cntId;
    }
    if (strC3 == '电子镜' && strC4 == 'ENF') {
      acc.Video_ENF_Count__c += cntId;
    }
    if ((strDCL == '消化科' || strDCL == '呼吸科') && strC3 == '主机' && strC5 == '竞争对手') {
      acc.Competitor_Count__c += cntId;
    }
    if (!(strDCL == '消化科' || strDCL == '呼吸科') && strC3 == '主机' && strC5 == '竞争对手') {
      acc.Competitor_Count_SP__c += cntId;
    }
    if ((strDCL == '消化科' || strDCL == '呼吸科') && strC3 == '能量主机' && strC5 == '竞争对手') {
      acc.Energy_Processor_Count__c += cntId;
    }
    if (!(strDCL == '消化科' || strDCL == '呼吸科') && strC3 == '能量主机' && strC5 == '竞争对手') {
      acc.Energy_Processor_Count_SP__c += cntId;
    }
    if (strC5 == '竞争对手' && !(strC3 == '主机' || strC3 == '能量主机')) {
      acc.Other_Competitor_Product_Count__c += cntId;
    }
//        if (acc.Id == '0011000000V9OU4') {
//            BatchIF_Log__c iflog = new BatchIF_Log__c();
//            iflog.Type__c = 'RollupToHPBatch';
//            iflog.Log__c  = '0011000000V9OU4 ' + strDCL + ' end\n';
//            iflog.Log__c += 'Competitor_Count__c=' + acc.Competitor_Count__c + '\n';
//            iflog.Log__c += 'Competitor_Count_SP__c=' + acc.Competitor_Count_SP__c + '\n';
//            iflog.Log__c += 'Energy_Processor_Count__c=' + acc.Energy_Processor_Count__c + '\n';
//            iflog.Log__c += 'Energy_Processor_Count_SP__c=' + acc.Energy_Processor_Count_SP__c + '\n';
//            iflog.Log__c += 'Other_Competitor_Product_Count__c=' + acc.Other_Competitor_Product_Count__c + '\n';
//            insert iflog;
//        }
  }
 
  private void setAssetPcodeCount(Account2__c acc, String strPcode, Integer cntId) {
    if (strPcode == 'N4491360') {
      acc.D3_System_Count__c += cntId;
    }
    if (strPcode == 'WB91051C') {
      acc.ESG_400_Count__c += cntId;
    }
  }
  // SWAG-BBBAKG start  保存能量的拜访情况
  private void setEventCount_Eng(Account2__c acc, Integer iMonth, Integer cntId) {
    acc.put('ThisYear' + iMonth + '_Eng__c', (Decimal) acc.get('ThisYear' + iMonth + '_Eng__c') + cntId);
  }
  // SWAG-BBBAKG end
  private void setEventCount(Account2__c acc, Integer iMonth, Integer cntId, String jobCategory) {
    if (jobCategory == '销售推广') {
      acc.put('ThisYear' + iMonth + '__c', (Decimal) acc.get('ThisYear' + iMonth + '__c') + cntId);
    } else if (jobCategory == '销售服务') {
      acc.put('ThisYear' + iMonth + '_FSE__c', (Decimal) acc.get('ThisYear' + iMonth + '_FSE__c') + cntId);
    }
  }
 
  private void setRankTaxCount(Account2__c acc, Decimal a1, Decimal a2, Decimal a3, Decimal a4, Decimal a5, Decimal fa, Decimal sa) {
    // 正式环境空指针报错处理 2021-11-15
    if (acc == null) {
      return;
    }
    acc.RANK_A_tax_HP__c += a1;
    acc.RANK_B_tax_HP__c += a2;
    acc.RANK_C_tax_HP__c += a3;
    acc.RANK_D_tax_HP__c += a4;
    acc.RANK_E_tax_HP__c += a5;
    acc.Forecast_Amount_HP__c += fa;
    acc.ShippedAmount_HP__c += sa;
  }
 
  private void setOppAmount(Account2__c acc, Decimal o1, Decimal o2, Decimal o3, Decimal o4, Decimal o5, Decimal o6, Decimal o7) {
    acc.Opp_Actual_ThousandY__c += o1;
    acc.BO_Forecast_ThousandY__c += o2;
    acc.OP_ThousandY__c += o3;
    acc.Opp_Forecast_ThousandY__c += o4;
    //acc.Opportunity_ThousandY__c += o5;//lt 20220318 注释
    acc.Amount_Without_Tax_Thousand__c += o6;
    //20220111 SWAG-C8MBB6 加7.询价 lt  start
    acc.AllOP_ThousandY__c += o7;
    //20220111 SWAG-C8MBB6 加7.询价 lt  end
  }
 
  private void setOpdIproductCount(Account2__c acc, Decimal decIP1, Decimal decIP2, Decimal decIP3, Decimal decIP4, Decimal decIP5, Decimal decIP6, Decimal decIP7, Decimal decIP8, Decimal decIP9, Decimal decIP10,
                                   Decimal decIP11, Decimal decIP12, Decimal decIP13, Decimal decIP14, Decimal decIP15, Decimal decIP16, Decimal decIP17, Decimal decIP18, Decimal decIP19, Decimal decIP20) {
    acc.Important_product1__c += decIP1;
    acc.Important_product2__c += decIP2;
    acc.Important_product3__c += decIP3;
    acc.Important_product4__c += decIP4;
    acc.Important_product5__c += decIP5;
    acc.Important_product6__c += decIP6;
    acc.Important_product7__c += decIP7;
    acc.Important_product8__c += decIP8;
    acc.Important_product9__c += decIP9;
    acc.Important_product10__c += decIP10;
    acc.Important_product11__c += decIP11;
    acc.Important_product12__c += decIP12;
    acc.Important_product13__c += decIP13;
    acc.Important_product14__c += decIP14;
    acc.Important_product15__c += decIP15;
    acc.Important_product16__c += decIP16;
    acc.Important_product17__c += decIP17;
    acc.Important_product18__c += decIP18;
    acc.Important_product19__c += decIP19;
    acc.Important_product20__c += decIP20;
  }
 
  private void setNtcCdsCount(Account2__c acc, Decimal cds) {
    acc.CDS_training_number_this_year__c += cds;
  }
 
  private void setDoCount(Account2__c acc, Integer iMonth, String dName, String jobCategory) {
    if (dName == 'OPD') {
      if (jobCategory == '销售推广') {
        acc.put('DoOPDHospital_ThisYear' + iMonth + '__c', (Decimal) acc.get('DoOPDHospital_ThisYear' + iMonth + '__c') + 1);
      } else if (jobCategory == '销售服务') {
        acc.put('DoOPDHospital_ThisYear' + iMonth + '_FSE__c', (Decimal) acc.get('DoOPDHospital_ThisYear' + iMonth + '_FSE__c') + 1);
      }
    } else if (dName == 'NTC') {
      acc.put('DoNTCHospital_ThisYear' + iMonth + '__c', (Decimal) acc.get('DoNTCHospital_ThisYear' + iMonth + '__c') + 1);
    }
  }
 
  private void setContactCount(Account2__c acc, Decimal decTTC, String strHCP) {
    acc.Sum_Number_of_participant_for_TTC__c += decTTC;
    if (strHCP == '学術HCP') {
      acc.Academic_HCP_Count__c += 1;
    } else if (strHCP == '製品HCP') {
      acc.Product_HCP_Count__c += 1;
    } else if (strHCP == '一般HCP') {
      acc.General_HCP_Count__c += 1;
    }
  }
 
  //  SWAG-BD24SU 累加发货数的方法 start
  // update by vivek SWAG-BQ75WE 2020/06/11 修改参数 start
  private void setDeliveryCount(Account2__c acc, String tmpKey,
                                Decimal tmpCnt , Map<Id, Account2__c> OldAccount2 ,
                                // Map<id, id> HPtoGIAcc2Map,
                                Map<String,id> depToOtherDepAcc2Map,
                                Map<Id, Account2__c> updateAccount) {
  // update by vivek SWAG-BQ75WE 2020/06/11 修改参数 end
    // switch on tmpKey {
    // when '8' {acc.Delivery_cnt_3D__c += tmpCnt;}
    // when '1' {
    //   Account2__c acc2 = OldAccount2.get(acc.Id);
    //     if (acc2 != null &&
    //     !acc2.Account_Org__r.RecordType_DeveloperName__c.equals('Department_Class_BF')) {
    //       ID depGIId = HPtoGIAcc2Map.get(acc2.Account_Org__r.parentID);
    //       Account2__c acc2GI = updateAccount.get(depGIId);
    //       acc2GI.Delivery_cnt_CV290__c += tmpCnt;
 
    //     } else{
    //       acc.Delivery_cnt_CV290__c += tmpCnt;
    //     }
    //   }
    //   when '18' {acc.Delivery_cnt_CYF__c += tmpCnt;}
    //   when '4' {
    //     Account2__c acc2 = OldAccount2.get(acc.Id);
    //     if (acc2 != null &&
    //     !acc2.Account_Org__r.RecordType_DeveloperName__c.equals('Department_Class_BF')) {
    //       ID depGIId = HPtoGIAcc2Map.get(acc2.Account_Org__r.parentID);
    //       Account2__c acc2GI = updateAccount.get(depGIId);
    //       acc2GI.Delivery_cnt_EUME2__c += tmpCnt;
 
    //     } else{
    //       acc.Delivery_cnt_EUME2__c += tmpCnt;
    //     }
 
 
    //   }
    //   when '27' {
    //     Account2__c acc2 = OldAccount2.get(acc.Id);
    //     if (acc2 != null &&
    //     !acc2.Account_Org__r.RecordType_DeveloperName__c.equals('Department_Class_BF')) {
    //       ID depGIId = HPtoGIAcc2Map.get(acc2.Account_Org__r.parentID);
    //       Account2__c acc2GI = updateAccount.get(depGIId);
    //       acc2GI.Delivery_cnt_OERAW__c += tmpCnt;
 
    //     } else{
    //       acc.Delivery_cnt_OERAW__c += tmpCnt;
    //     }
    //   }
    //   when '29' {acc.Delivery_cnt_OTVS400__c += tmpCnt;}
    //   when '25' {acc.Delivery_cnt_USG400__c += tmpCnt;}
    //   when '3' {acc.Delivery_cnt_CV170__c += tmpCnt;}
    //   when '9' {acc.Delivery_cnt_CV190__c += tmpCnt;}
    //   when '10' {acc.Delivery_cnt_OTVS190__c += tmpCnt;}
 
    //   when else {
    //     system.debug('=====setDeliveryCount else:' + tmpKey + '_x_' + tmpCnt);
    //   }
 
  // update by vivek SWAG-BQ75WE 2020/06/12 通用方法修改 start
  List<List<String>> allList = new List<List<String>>();
  allList = getImportantProductModel(tmpKey, '2');
  Account2__c acc2 = OldAccount2.get(acc.Id);
  String nameType = 'Delivery_cnt_';
  String departmentE = acc2.Account_Org__r.RecordType_DeveloperName__c.substring(17);
  countImportantProduct(acc,nameType,tmpCnt,tmpKey,allList,departmentE,depToOtherDepAcc2Map, updateAccount, OldAccount2);
 
  }
 
  //SWAG-BD24SU 累加发货数的方法 end
 
  // 2020-06-12 SWAG-BQ75WE 额外传递depToOtherDepAcc2Map和updateAccount start
  // 2018-09-14 CHAN-B4J9TC start 额外传递下客户2对象map,用来判断战略科室
  // private void setOrderCount(Account2__c acc, String tmpKey, Decimal tmpCnt , Map<Id, Account2__c> OldAccount2) {
    private void setOrderCount(Account2__c acc, String tmpKey, Decimal tmpCnt , Map<Id, Account2__c> OldAccount2,Map<String,id> depToOtherDepAcc2Map,
                                Map<Id, Account2__c> updateAccount) {
    // 2018-09-14 CHAN-B4J9TC end 额外传递下客户2对象map,用来判断战略科室
    // 2020-06-12 SWAG-BQ75WE 额外传递depToOtherDepAcc2Map和updateAccount end
    // switch on tmpKey {
    // 2018-09-14 CHAN-B4J9TC start 消化科统计GI 290镜子,呼吸科统计 BF-290 镜子
    // when '2' {
    //   Account2__c acc2 = OldAccount2.get(acc.Id);
    //     if (acc2 != null && acc2.Account_Org__r.RecordType_DeveloperName__c.equals('Department_Class_GI')) {
    //       acc.Order_cnt_290Miro__c += tmpCnt;
    //     }
    //   }
    //   when '5' {
    //     Account2__c acc2 = OldAccount2.get(acc.Id);
    //     if (acc2 != null && acc2.Account_Org__r.RecordType_DeveloperName__c.equals('Department_Class_BF')) {
    //       acc.Order_cnt_290Miro__c += tmpCnt;
    //     }
    //   }
    //   // 2018-09-14 CHAN-B4J9TC end  消化科统计GI 290镜子,呼吸科统计 BF-290 镜子
 
    //   // CHAN-B496KE 2018/09/04 订货数3D所指的重点产品,由重点製品11 LTF-3D 改为 重点产品08 3DV-190 start
    //   when '8' {acc.Order_cnt_3D__c += tmpCnt;}
    //   // CHAN-B496KE 2018/09/04 订货数3D所指的重点产品,由重点製品11 LTF-3D 改为 重点产品08 3DV-190 end
    //   when '30' {acc.Order_cnt_CHS400XZ__c += tmpCnt;}
    //   when '3' {acc.Order_cnt_CV170__c += tmpCnt;}
    //   when '9' {acc.Order_cnt_CV190__c += tmpCnt;}
    //   when '1' {acc.Order_cnt_CV290__c += tmpCnt;}
    //   when '4' {acc.Order_cnt_EUME2__c += tmpCnt;}
    //   when '27' {acc.Order_cnt_OERAW__c += tmpCnt;}
    //   when '10' {acc.Order_cnt_OTVS190__c += tmpCnt;}
    //   when '29' {acc.Order_cnt_OTVS400__c += tmpCnt;}
    //   when '28' {acc.Order_cnt_UCES3__c += tmpCnt;}
    //   when '25' {acc.Order_cnt_USG400__c += tmpCnt;}
    //   when '6' {acc.Order_cnt_GW__c += tmpCnt;}
    //   // SWAG-BC832V 订货数:CYF 累加
    //   when '18' {acc.Order_cnt_CYF__c += tmpCnt;}
    //   // SWAG-BC832V
    //   when else {
    //     system.debug('=====setOrderCount else:' + tmpKey + '_x_' + tmpCnt);
    //   }
 
    // SWAG-BQ75WE  2020-06-12 start
      List<List<String>> allList = new List<List<String>>();
      allList = getImportantProductModel(tmpKey, '2');
      Account2__c acc2 = OldAccount2.get(acc.Id);
      String nameType = 'Order_cnt_';
      String departmentE = acc2.Account_Org__r.RecordType_DeveloperName__c.substring(17);
      countImportantProduct(acc,nameType,tmpCnt,tmpKey,allList,departmentE,depToOtherDepAcc2Map, updateAccount, OldAccount2);
    // SWAG-BQ75WE  2020-06-12 start
  }
  
  // 2020-06-12 SWAG-BQ75WE 额外传递depToOtherDepAcc2Map和updateAccount start
  // 2018-09-14 CHAN-B4J9TC start 额外传递下客户2对象map,用来判断战略科室
  private void setOppCount(Account2__c acc, String tmpKey, Decimal tmpCnt , Map<Id, Account2__c> OldAccount2,Map<String,id> depToOtherDepAcc2Map,
                                Map<Id, Account2__c> updateAccount) {
    // 2018-09-14 CHAN-B4J9TC end 额外传递下客户2对象map,用来判断战略科室
    // 2020-06-12 SWAG-BQ75WE 额外传递depToOtherDepAcc2Map和updateAccount end
    // switch on tmpKey {
    // 2018-09-14 CHAN-B4J9TC start 消化科统计GI 290镜子,呼吸科统计 BF-290 镜子
      // when '2' {
      //   Account2__c acc2 = OldAccount2.get(acc.Id);
      //   if (acc2 != null && acc2.Account_Org__r.RecordType_DeveloperName__c.equals('Department_Class_GI')) {
      //     acc.Opp_cnt_290Miro__c += tmpCnt;
      //   }
      // }
      // when '5' {
      //   Account2__c acc2 = OldAccount2.get(acc.Id);
      //   if (acc2 != null && acc2.Account_Org__r.RecordType_DeveloperName__c.equals('Department_Class_BF')) {
      //     acc.Opp_cnt_290Miro__c += tmpCnt;
      //   }
      // }
      // // 2018-09-14 CHAN-B4J9TC end  消化科统计GI 290镜子,呼吸科统计 BF-290 镜子
 
 
      // // CHAN-B496KE 2018/09/04 询价数3D所指的重点产品,由重点製品11 LTF-3D 改为 重点产品08 3DV-190 start
      // when '8' {acc.Opp_cnt_3D__c += tmpCnt;}
      // // CHAN-B496KE 2018/09/04 询价数3D所指的重点产品,由重点製品11 LTF-3D 改为 重点产品08 3DV-190 end
      // when '30' {acc.Opp_cnt_CHS400XZ__c += tmpCnt;}
      // when '3' {acc.Opp_cnt_CV170__c += tmpCnt;}
      // when '9' {acc.Opp_cnt_CV190__c += tmpCnt;}
      // when '1' {acc.Opp_cnt_CV290__c += tmpCnt;}
      // when '4' {acc.Opp_cnt_EUME2__c += tmpCnt;}
      // when '27' {acc.Opp_cnt_OERAW__c += tmpCnt;}
      // when '10' {acc.Opp_cnt_OTVS190__c += tmpCnt;}
      // when '29' {acc.Opp_cnt_OTVS400__c += tmpCnt;}
      // when '28' {acc.Opp_cnt_UCES3__c += tmpCnt;}
      // when '25' {acc.Opp_cnt_USG400__c += tmpCnt;}
      // when '6' {acc.Opp_cnt_GW__c += tmpCnt;}
      // // SWAG-BC832V 订货数:CYF 累加
      // when '18' {
      //   acc.Opp_cnt_CYF__c += tmpCnt;
      //   system.debug('=====setOppCount else:' + tmpKey + '_x_' + tmpCnt);
      // }
      // // SWAG-BC832V
      // when else {
      //   system.debug('=====setOppCount else:' + tmpKey + '_x_' + tmpCnt);
      // }
 
     // SWAG-BQ75WE  2020-06-12 start
      List<List<String>> allList = new List<List<String>>();
      allList = getImportantProductModel(tmpKey, '2');
      Account2__c acc2 = OldAccount2.get(acc.Id);
      String nameType = 'Opp_cnt_';
      String departmentE = acc2.Account_Org__r.RecordType_DeveloperName__c.substring(17);
      countImportantProduct(acc,nameType,tmpCnt,tmpKey,allList,departmentE,depToOtherDepAcc2Map, updateAccount, OldAccount2);
   
    // SWAG-BQ75WE  2020-06-12 end
  }
  //20200825 ljh SWAG-BRY6PF  add start
  private void setRentalCount(Account2__c acc, String tmpKey, Integer tmpCnt , Map<Id, Account2__c> OldAccount2,Map<String,id> depToOtherDepAcc2Map,
                                Map<Id, Account2__c> updateAccount) {
    switch on tmpKey {
      when '01' {acc.Rental_Cnt_CV290__c += tmpCnt;}
      when '02' {acc.Rental_Cnt_290MiroGI__c += tmpCnt;}     
      when '03' {acc.Rental_Cnt_290MiroGI_Plus__c += tmpCnt;}
      when '04' {acc.Rental_Cnt_290MiroGI_CF__c += tmpCnt;}
      when '07' {acc.Rental_Cnt_GFUCT260__c += tmpCnt;}
      when '05' {acc.Rental_Cnt_EUME2__c += tmpCnt;}
      when '09' {acc.Rental_Cnt_290MiroBF__c += tmpCnt;}
      when '14' {acc.Rental_Cnt_OTVS400__c += tmpCnt;}
      when '15' {acc.Rental_Cnt_OTVS300__c += tmpCnt;}
      when '16' {acc.Rental_Cnt_CV170__c += tmpCnt;}
      when '17' {acc.Rental_Cnt_CV190__c += tmpCnt;}
      when '18' {acc.Rental_Cnt_OTVS190__c += tmpCnt;}
      when '28' {acc.Rental_Cnt_ESG400__c += tmpCnt;}
      when '29' {acc.Rental_Cnt_USG400__c += tmpCnt;}
      when '30' {acc.Rental_Cnt_TB__c += tmpCnt;}
      when else {
        system.debug('=====setRentalCount else:' + tmpKey + '_x_' + tmpCnt);
      }
    }
  }
  //20200825 ljh SWAG-BRY6PF  add end
  //SWAG-BSC5WP you-20201021 start
  private void setAssetCount(Account2__c acc, String tmpKey, Integer tmpCnt , Map<Id, Account2__c> OldAccount2,Map<String,id> depToOtherDepAcc2Map,
                                Map<Id, Account2__c> updateAccount) {
    switch on tmpKey {
      when '01' {acc.Asset_Cnt_CV290__c += tmpCnt;}
      when '02' {acc.Asset_Cnt_290MiroGI__c += tmpCnt;}     
      when '03' {acc.Asset_Cnt_290MiroGI_Plus__c += tmpCnt;}
      when '04' {acc.Asset_Cnt_290MiroGI_CF__c += tmpCnt;}
      when '07' {acc.Asset_Cnt_GFUCT260__c += tmpCnt;}
      when '05' {acc.Asset_Cnt_EUME2__c += tmpCnt;}
      when '09' {acc.Asset_Cnt_290MiroBF__c += tmpCnt;}
      when '14' {acc.Asset_Cnt_OTVS400__c += tmpCnt;}
      when '15' {acc.Asset_Cnt_OTVS300__c += tmpCnt;}
      when '16' {acc.Asset_Cnt_CV170__c += tmpCnt;}
      when '17' {acc.Asset_Cnt_CV190__c += tmpCnt;}
      when '18' {acc.Asset_Cnt_OTVS190__c += tmpCnt;}
      when '28' {acc.Asset_Cnt_ESG400__c += tmpCnt;}
      when '29' {acc.Asset_Cnt_USG400__c += tmpCnt;}
      when '30' {acc.Asset_Cnt_TB__c += tmpCnt;}
      when else {
        system.debug('=====setAssetCount else:' + tmpKey + '_x_' + tmpCnt);
      }
    }
  }
  //SWAG-BSC5WP you-20201021 end
 
  // update by vivek SWAG-BQ75WE 2020/06/11 start
  // 计算重点产品区分
  private void countImportantProduct(Account2__c acc,String nameType,Decimal tmpCnt,String tmpKey, List<List<String>> modelList,String departmentE,Map<String,id> depToOtherDepAcc2Map,Map<Id, Account2__c> updateAccount,Map<Id, Account2__c> OldAccount2){
    System.debug('======acc'+acc);
    System.debug('======nameType'+nameType);
    System.debug('======tmpCnt'+tmpCnt);
    System.debug('======tmpKey'+tmpKey);
    System.debug('======modelList'+modelList);
    System.debug('======departmentE'+departmentE);
    System.debug('======depToOtherDepAcc2Map'+depToOtherDepAcc2Map);
    System.debug('======updateAccount'+updateAccount);
    System.debug('======OldAccount2'+OldAccount2);
 
      // 全部计算的科室
      List<String> allCountDepList = new List<String>();
      // 不计算的科室
      List<String> noCountDepList = new List<String>();
      // 只计算的科室
      List<String> onlyCountDepList = new List<String>();
      // 通过编号获取需要更新的字段名称
      String filedName = filedNameMap.get(tmpKey);
      System.debug('======filedName'+filedName);
      if(filedName != null){
      
        if(modelList.size() == 3){
            allCountDepList = modelList[0];
            noCountDepList = modelList[1];
            onlyCountDepList = modelList[2];
        }
        if(allCountDepList.size() == 0 && noCountDepList.size() == 0 && onlyCountDepList.size() == 0){
            // 都是单独计算的,都计算自己对应的战略科室
            // Decimal tmpCntOld = (Decimal)acc.get('Delivery_cnt_'+filedName);
            Decimal tmpCntOld = (Decimal)acc.get(nameType+filedName);
            if(tmpCntOld == null){
              // acc.put('Delivery_cnt_'+filedName,tmpCnt);
              acc.put(nameType+filedName,tmpCnt);
            }else{
              // acc.put('Delivery_cnt_'+filedName,tmpCntOld+tmpCnt);
              acc.put(nameType+filedName,tmpCntOld+tmpCnt);
            }
        }else{
            // 需要判断具体计算哪个战略科室
            // 如果当前科室在全部计算科室中,计算全部科室中第一个科室。
            if(allCountDepList.size() > 0 && allCountDepList.contains(departmentE)){
                Account2__c acc2 = OldAccount2.get(acc.Id);
                ID depGIId = depToOtherDepAcc2Map.get(acc2.Account_Org__r.parentID+'Department_Class_'+allCountDepList[0]);
                System.debug('depGIId======'+depGIId);
                System.debug('======depart:'+'Department_Class_'+allCountDepList[0]);
                Account2__c acc2Oth = updateAccount.get(depGIId);
                // Decimal tmpCntOld = (Decimal)acc2Oth.get('Delivery_cnt_'+filedName);
                Decimal tmpCntOld = (Decimal)acc2Oth.get(nameType+filedName);
                if(tmpCntOld == null){
                  // acc2Oth.put('Delivery_cnt_'+filedName,tmpCnt);
                  acc2Oth.put(nameType+filedName,tmpCnt);
                }else{
                  // acc2Oth.put('Delivery_cnt_'+filedName,tmpCntOld+tmpCnt);
                  acc2Oth.put(nameType+filedName,tmpCntOld+tmpCnt);
                }
                System.debug(acc2Oth);
                
            }
            // 如果当前科室在不计算的科室中,不需要任何操作。
 
            // 如果当前科室在只计算的科室中,直接计算自己对应的战略科室。
            else if(onlyCountDepList.size() > 0 && onlyCountDepList.contains(departmentE)){
                // acc.put('Delivery_cnt_'+filedName, acc.get('Delivery_cnt_'+filedName)+tmpCnt);
                Decimal tmpCntOld = (Decimal)acc.get(nameType+filedName);
                if(tmpCntOld == null){
                  // acc.put('Delivery_cnt_'+filedName,tmpCnt);
                  acc.put(nameType+filedName,tmpCnt);
                }else{
                  // acc.put('Delivery_cnt_'+filedName,tmpCntOld+tmpCnt);
                  acc.put(nameType+filedName,tmpCntOld+tmpCnt);
                }
            }
        }
      }
  }
 
  // 建立重点产品区分计算模型
  public List<List<String>> getImportantProductModel(String i, String StrType){
      // filedNameMap 赋值,名称匹配
      // if(filedNameMap == null){
        // filedNameMap.clear();
          if (StrType == '1') {
              filedNameMap = new Map<String,String>();
              filedNameMap.put('1', 'CV_290__c');
              filedNameMap.put('2', '290Miro_GI__c');
              filedNameMap.put('3', '290Miro_GIPlus__c');
              filedNameMap.put('4', '290Miro_GICF__c');
              filedNameMap.put('7', 'GFUCT260__c');
              // 20210602 SWAG-C3K6L7 Start
              //filedNameMap.put('5', 'EU_ME2__c');
              // 20210602 SWAG-C3K6L7 End
              filedNameMap.put('9', '290Miro_BF__c');
              filedNameMap.put('14', 'OTV_S400__c');
              filedNameMap.put('15', 'OTV_S300__c');
              filedNameMap.put('16', 'CV_170__c');
              filedNameMap.put('17', 'CV_190__c');
              // 20210602 SWAG-C3K6L7 Start
              //filedNameMap.put('18', 'OTV_S190__c');
              // 20210602 SWAG-C3K6L7 End
              filedNameMap.put('28', 'ESG400__c');
              filedNameMap.put('29', 'USG400__c');
              filedNameMap.put('30', 'TB__c');
          } else {
              filedNameMap = new Map<String,String>();
              filedNameMap.put('1', 'CV290__c');
              filedNameMap.put('2', '290MiroGI__c');
              filedNameMap.put('3', '290MiroGI_Plus__c');
              filedNameMap.put('4', '290MiroGI_CF__c');
              filedNameMap.put('7', 'GFUCT260__c');
              // 20210602 SWAG-C3K6L7 Start
              //filedNameMap.put('5', 'EUME2__c');
              // 20210602 SWAG-C3K6L7 End
              filedNameMap.put('9', '290MiroBF__c');
              filedNameMap.put('14', 'OTVS400__c');
              filedNameMap.put('15', 'OTVS300__c');
              filedNameMap.put('16', 'CV170__c');
              filedNameMap.put('17', 'CV190__c');
              // 20210602 SWAG-C3K6L7 Start
              //filedNameMap.put('18', 'OTVS190__c');
              // 20210602 SWAG-C3K6L7 End
              filedNameMap.put('28', 'ESG400__c');
              filedNameMap.put('29', 'USG400__c');
              filedNameMap.put('30', 'TB__c');
          // }
        
      }
      
 
      // 全部科室简称
      String departmentBF = 'BF';  // 呼吸科
      String departmentENT = 'ENT';  // 耳鼻喉科
      // String departmentET = 'ET';  // ET耗材
      String departmentGI = 'GI';  // 消化科
      String departmentGS = 'GS';  // 普外科
      String departmentGYN = 'GYN';  // 妇科
      String departmentOTH = 'OTH';  // 其他
      String departmentURO = 'URO';  // 泌尿科
      // 全部科室的list
      List<String> allDepList = new List<String>();
      allDepList.add(departmentBF);
      allDepList.add(departmentENT);
      allDepList.add(departmentGI);
      allDepList.add(departmentGS);
      allDepList.add(departmentGYN);
      allDepList.add(departmentURO);
      allDepList.add(departmentOTH);
      List<List<String>> allList = new List<List<String>>();
      // 全部计算的科室
      List<String> allCountDepList = new List<String>();
      // 不计算的科室
      List<String> noCountDepList = new List<String>();
      // 只计算的科室
      List<String> onlyCountDepList = new List<String>();
      // 20210602 SWAG-C3K6L7 Start
      //if(i == '1' || i == '5'){
      if(i == '1'){
      // 20210602 SWAG-C3K6L7 End
        // 这里是需要修改的 start
        allCountDepList.add(departmentGI);
        // noCountDepList.add() 这里没有不需要计算的科室
        onlyCountDepList.add(departmentBF);
        // 这里是需要修改的 end
        for(String str :allDepList){
          if(!allCountDepList.contains(str) && !noCountDepList.contains(str) && !onlyCountDepList.contains(str)){
            allCountDepList.add(str);
          }
        }
        allList.add(allCountDepList);
        allList.add(noCountDepList);
        allList.add(onlyCountDepList);
      }else if(i == '2' || i == '3' || i == '4' || i == '7'){
        // 这里是需要修改的 start
        allCountDepList.add(departmentGI);
        // noCountDepList.add() 这里没有不需要计算的科室
        // onlyCountDepList.add(departmentBF); 这里也没有直接算的科室,都算在消化科里
        // 这里是需要修改的 end
        for(String str :allDepList){
          if(!allCountDepList.contains(str) && !noCountDepList.contains(str) && !onlyCountDepList.contains(str)){
            allCountDepList.add(str);
          }
        }
        allList.add(allCountDepList);
        allList.add(noCountDepList);
        allList.add(onlyCountDepList);
      }else if(i == '9'){
        // 这里是需要修改的 start
        allCountDepList.add(departmentBF); // 都计算在呼吸科里
        // noCountDepList.add() 这里没有不需要计算的科室
        // onlyCountDepList.add(departmentBF); 这里也没有直接算的科室,都算在呼吸科里
        // 这里是需要修改的 end
        for(String str :allDepList){
          if(!allCountDepList.contains(str) && !noCountDepList.contains(str) && !onlyCountDepList.contains(str)){
            allCountDepList.add(str);
          }
        }
        allList.add(allCountDepList);
        allList.add(noCountDepList);
        allList.add(onlyCountDepList);
      // 20210602 SWAG-C3K6L7 Start
      //}else if(i == '14' || i == '15' || i == '17' || i == '18' || i == '29' || i == '28' || i == '30'){
      } else if(i == '14' || i == '15' || i == '17' || i == '29' || i == '28' || i == '30'){
      // 20210602 SWAG-C3K6L7 End
        // 这里是需要修改的 start
        allCountDepList.add(departmentGS); // 都计算在普外科里
        // noCountDepList.add() 这里没有不需要计算的科室
        onlyCountDepList.add(departmentGYN); //这里也没有直接算的科室,都算在呼吸科里
        onlyCountDepList.add(departmentENT);
        onlyCountDepList.add(departmentURO);
        // 这里是需要修改的 end
        for(String str :allDepList){
          if(!allCountDepList.contains(str) && !noCountDepList.contains(str) && !onlyCountDepList.contains(str)){
            allCountDepList.add(str);
          }
        }
        allList.add(allCountDepList);
        allList.add(noCountDepList);
        allList.add(onlyCountDepList);
      }else if(i == '16'){
        // 这里是需要修改的 start
        allCountDepList.add(departmentGS); // 都计算在普外科里
        noCountDepList.add(departmentBF); // 这里不包含呼吸科
        noCountDepList.add(departmentGI); // 这里不包含消化科
        // onlyCountDepList.add(departmentBF); // 这里没有只计算的内容
        onlyCountDepList.add(departmentGYN); //这里也没有直接算的科室,都算在呼吸科里
        onlyCountDepList.add(departmentENT);
        onlyCountDepList.add(departmentURO);
        // 这里是需要修改的 end
        for(String str :allDepList){
          if(!allCountDepList.contains(str) && !noCountDepList.contains(str) && !onlyCountDepList.contains(str)){
            allCountDepList.add(str);
          }
        }
        allList.add(allCountDepList);
        allList.add(noCountDepList);
        allList.add(onlyCountDepList);
      }else{
        // 都是直接计算自己对应的战略科室下
        allList.add(allCountDepList);
        allList.add(noCountDepList);
        allList.add(onlyCountDepList);
      }
      return allList;
  }
  // update by vivek SWAG-BQ75WE 2020/06/11 end
 
  private void setOpdCount(Account2__c acc, String tmpKey, Decimal tmpCnt , Map<Id, Account2__c> OldAccount2,Map<String,id> depToOtherDepAcc2Map,
                                Map<Id, Account2__c> updateAccount) {
  
      List<List<String>> allList = new List<List<String>>();
      allList = getImportantProductModel(tmpKey, '1');
      Account2__c acc2 = OldAccount2.get(acc.Id);
      String nameType = 'Opd_';
      String departmentE = acc2.Account_Org__r.RecordType_DeveloperName__c.substring(17);
      countImportantProduct(acc,nameType,tmpCnt,tmpKey,allList,departmentE,depToOtherDepAcc2Map, updateAccount, OldAccount2);
    
  }
}