liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
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
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
/*
 TestClass
 RentalApplyTriggerHandlerTest
 RentalFixtureManage11Test
 RentalFixtureManage14Test
*/
public without sharing class RentalApplyTriggerHandler extends Oly_TriggerHandler {
    private Map<Id, Rental_Apply__c> newMap;
    private Map<Id, Rental_Apply__c> oldMap;
    private List<Rental_Apply__c> newList;
    private List<Rental_Apply__c> oldList;
    private static Date td = Date.today();
    private static Map<Id, Rental_Apply__c> oldRaMap = new Map<Id, Rental_Apply__c>();
    public static Boolean isFirst = true;
 
    //update      wangweipeng                             2021/11/25                   start
    private static Map<String, String> approver_of_Service_DepartmentMap = new Map<String, String>();
    /*private static Map<String, String> approver_of_Service_DepartmentMap = new Map<String, String>{
        '共通办事处' => System.Label.Extension_to_Beijing_common_approver,
        '北京办事处' => System.Label.Extension_to_Beijing_common_approver,
        '沈阳办事处' => System.Label.Extension_to_Shenyang_approver,
        '上海办事处' => System.Label.Extension_to_Shanghai_approver,
        '广东办事处' => System.Label.Extension_to_Guangdong_approver
    };*/
    //update      wangweipeng                             2021/11/25                   end
 
    private static boolean hasInsert;
    // static initialization
    static {
        hasInsert = false;
    }
    public static Integer FIELDMAX = 200; // 202100823 ljh SFDC-C448KZ add
    private static Map<String, String> rental_Apply_App_CCEmailMap = FixtureUtil.initRental_Apply_App_CCEmailMap();
 
    public RentalApplyTriggerHandler() {
        System.debug('进入RentalApplyTriggerHandler');
        Integer i = 0;
        i ++;
        this.newMap = (Map<Id, Rental_Apply__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Rental_Apply__c>) Trigger.oldMap;
        this.newList = (List<Rental_Apply__c>) Trigger.new;
        this.oldList = (List<Rental_Apply__c>) Trigger.old;
 
        // 借用机会可视化 SQL101优化 zyh 20231223 start
        // approver_of_Service_DepartmentMap = customPostponeWorkLocation();
        // 借用机会可视化 SQL101优化 zyh 20231223 start
    }
 
    protected override void beforeInsert() {
        System.debug('进入rentalapply beforeInsert');
        setManager();
        beforeSetValue();
        setOffice_Assistant();
 
        //DB202401538028 备品智能化-申请单推送处理时间 20240201 by lc Start
        setAssginPushTime();
        //DB202401538028 备品智能化-申请单推送处理时间 20240201 by lc End
    }
    protected override void afterInsert() {
        System.debug('进入rentalapply afterInsert');
        // Check本部是否可以选择
        checkbenbu();// 20220909 ljh 恢复代码
        // 共享设定
        setShare();
        
        //decryptInsert(newList);  //deloitte-zhj 20231116 PIPL还原
    }
    protected override void beforeUpdate() {
        setManager();
        beforeSetValue();
        approvalCheck();
        setOffice_Assistant();
 
        checkExtensionDeadline();
        //DB202401538028 备品智能化-申请单推送处理时间-追加批准后修改 20240301 by zyh Start
        updateAssginPushTime();
        //DB202401538028 备品智能化-申请单推送处理时间-追加批准后修改 20240301 by zyh End
    }
 
    protected override void afterUpdate() {
        // Check本部是否可以选择
        checkbenbu();// 20220909 ljh 恢复代码
        cancelRa();
        // before では数式項目がnullの場合があります
        formulaToTextCheck();
        //医院确认相关的字段更新的时候要更新一览
        reReceivedConfirmStatus();
        //医院确认相关的字段更新的时候要更新一览
        reApprovalStatus();
        // 取消申请单的审批
        removedProcessRequest();
 
        System.debug('---------------newList--------------' + newList);
 
        // add by lc 2022/11/15 DB202211029119 start
        if (isFirst) {
            // 主从申请单,只延主单的情况,走单独的处理逻辑,并且只执行一次
            synchRentalApplyDataMaster();
        }
        // add by lc 2022/11/15 DB202211029119 end
 
        synchRentalApplyData2();
        // 延期审批后需要更新一览
        setAppExtensionRaes();
        // 共享设定
        setShare();
        // 办事处分单的装机确认
        setAgencyHPReceived();
        //批量审批时,需要把主单和从单的延期字段信息同步
        synchRentalApplyData();
 
        // 备品智能化项目对应 20231122 by lc Start
        // 跨区域分配,清空分配和排队相关的信息
        clearAssignAndQueueByCrossRegionAssign();
        // 备品智能化项目对应 20231122 by lc End
    }
    //DB202401538028 备品智能化-申请单推送处理时间-追加批准后修改 20240301 by zyh Start
    // 如果OPD追加审批,审批通过后更新推送处理时间
    private void updateAssginPushTime() {
        // String profileName = [SELECT Id,Name From Profile WHERE Name = '系统管理员'].Id;
        List<String> uIdList = new List<String>();
        Map<String,String> uMap = new Map<String,String>();
        for (Rental_Apply__c nObj : newList) {
            uIdList.add(nObj.CreatedById);
        }
        List<User> uList = [SELECT Id,Name,Profile.Name FROM User WHERE Id IN :uIdList];
        for (User u : uList) {
            uMap.put(u.Id, u.Profile.Name);
        }
        for (Rental_Apply__c nObj : newList) {
            Rental_Apply__c oObj = oldMap.get(nObj.Id);
            if (nObj.Demo_purpose1__c == '产品试用' 
                && nObj.demo_purpose2__c != '已购待货' 
                && String.isNotBlank(nObj.Split_Apply_Reason__c) 
                && nObj.Request_approval_time__c != oObj.Request_approval_time__c
                && uMap.get(nObj.CreatedById) != '系统管理员') {
                    nObj.Rental_Fixture_Push_Time__c = nObj.Request_approval_time__c;
            }
        }
    }
    //DB202401538028 备品智能化-申请单推送处理时间-追加批准后修改 20240301 by zyh End
    //DB202401538028 备品智能化-申请单推送处理时间 20240201 by lc Start
    //  设置申请单推送处理时间(sys):给非实时单做分单处理后,分出去的申请单需要自动设置申请单推送处理时间(sys)
    //    人工分单:使用批准时间
    //    系统分单:使用系统处理时间
    private void setAssginPushTime() {
        for (Rental_Apply__c nObj : newList) {
            if (nObj.Demo_purpose1__c == '产品试用' && nObj.demo_purpose2__c != '已购待货' && String.isNotBlank(nObj.Split_Apply_Reason__c)) {
                if (UserInfo.getName() == '精琢技术' || UserInfo.getName() == 'Batch') {
                    nObj.Rental_Fixture_Push_Time__c = System.now();
                } else {
                    nObj.Rental_Fixture_Push_Time__c = nObj.Request_approval_time__c;
                }
            }
        }
    }
    //DB202401538028 备品智能化-申请单推送处理时间 20240201 by lc End
 
    // add by lc 2022/11/15 DB202211029119 start
    // 主从申请单,只延主单的情况,走单独的处理逻辑
    private void synchRentalApplyDataMaster() {
        List<Rental_Apply_Equipment_Set__c> raesList = new List<Rental_Apply_Equipment_Set__c>();
        // 批准只能一条一条的批准
        for (Rental_Apply__c nObj : newList) {
            Rental_Apply__c oObj = oldMap.get(nObj.Id);
 
            if (oObj.ExtensionApprovalTime_Initial__c != nObj.ExtensionApprovalTime_Initial__c 
                && nObj.ExtensionApprovalTime_Initial__c != null && oObj.ExtensionApprovalTime_Initial__c == null
                && String.isNotBlank(nObj.Extension_Type__c) && nObj.Extension_Type__c == '批量延期'
                && (nObj.demo_purpose2__c == '试用(无询价)' || nObj.demo_purpose2__c == '试用(有询价)')
                && String.isBlank(oObj.Extension_Much_ID__c)
                && String.isBlank(oObj.Root_Rental_Apply__c)) {
                isFirst = false;
                List<Rental_Apply__c> checkRentalApply = new List<Rental_Apply__c>();
                checkRentalApply.add(oObj);
                try {
                    System.debug('========================checkRentalApply=========================' + checkRentalApply);
                    for (Rental_Apply_Equipment_Set__c raes : getCan_Extend_RequestList(checkRentalApply)) {
                        System.debug('raes.Id=========================' + raes.Id);
                        //判断是此申请单是否存在 ok并且回寄时间不为空的一览,
                        if ((raes.Received_Confirm__c == 'OK' || raes.Received_Confirm__c == '默认签收-OK') && raes.Asset_return_time__c != null) {
 
                        }else{
                            raes.RcUnexpectExpiryDelay__c = raes.Rental_Apply__r.RcUnexpectExpiryDelay__c;
                            raesList.add(raes);
                        }
                    }
                }
                catch (Exception e) {
                    nObj.addError(e.getMessage() + ',请操作驳回。');
                }
            }
        }
        if (0 < raesList.size()) {
            update raesList;
        }
    }
    // add by lc 2022/11/15 DB202211029119 end
 
    // 备品智能化项目对应 20231122 by lc Start
    private void clearAssignAndQueueByCrossRegionAssign() {
        List<String> raIds = new List<String>();
        for (Rental_Apply__c nObj : newList) {
            Rental_Apply__c oObj = oldMap.get(nObj.Id);
 
            // 跨区域分配时,清空分配和排队相关的信息
            if (nObj.Cross_Region_Assign__c != oObj.Cross_Region_Assign__c) {
                raIds.add(nObj.Id);
            }
        }
 
        // 为了之后的OPD延期使用共同的方法,另作单独的方法来实现清除的逻辑
        clearAssignAndQueue(raIds);
    }
 
    public static void clearAssignAndQueue(List<String> raIds) {
 
        if (raIds.size() > 0) { // 2023-12-30 借用机会可视化 zyh 判断空
            
            List<Rental_Apply_Equipment_Set_Detail__c> raesdList = [
                SELECT Id,QuenType__c,Queue_Day__c,Queue_Day_Text__c,Queue_Number__c,Queue_Time__c,Queue_Time_Text__c,Queue_User__c,Asset__c, Is_Body__c, SerialNumber_text__c,
                        Rental_Apply__c,Select_Time__c,Equipment_Type_text__c,ExternalKey__c,Fixture_Model_No_text__c,Fixture_Name_text__c,Internal_asset_location_before__c,
                        Intervention_Reason__c,Product_category_text__c,Salesdepartment_before__c,SalesProvince_before__c,UniqueKey_Queue__c,Zhu_Ti_Fen_Pei_Jia__c,
                        Fu_Shu_Pin_Fen_Pei_Jia__c,Jie_Chu_Fen_Pei_Jia__c,Shipment_request_time2__c,Shipment_request__c,FSD_Fixture_Model_No__c,Asset_cost_del_before__c,
                        FSD_Name_CHN__c,EquipmentSet_Managment_Code_text__c,Queue_Conment__c,FSD_OneToOneAccessory_Cnt__c,Fixture_OneToOne_Link_Id__c
                FROM Rental_Apply_Equipment_Set_Detail__c 
                WHERE Rental_Apply__c IN: raIds and Cancel_Select__c = false];
 
            Set<Id> ids = new Set<Id>();
            Set<Id> assIds = new Set<Id>();
            for (Rental_Apply_Equipment_Set_Detail__c raesd : raesdList) {
                if (raesd.Is_Body__c) {
                    ids.add(raesd.Id);
                    assIds.add(raesd.Asset__c);
                }
 
                raesd.QuenType__c = null;
                raesd.Queue_Day__c = null;
                raesd.Queue_Day_Text__c = null;
                raesd.Queue_Number__c = null;
                raesd.Queue_Time__c = null;
                raesd.Queue_Time_Text__c = null;
                raesd.Queue_User__c = null;
                raesd.Select_Time__c = null;
                raesd.Equipment_Type_text__c = null;
                raesd.ExternalKey__c = null;
                raesd.Fixture_Model_No_text__c = raesd.FSD_Fixture_Model_No__c;
                raesd.Fixture_Name_text__c = raesd.FSD_Name_CHN__c;
                raesd.Internal_asset_location_before__c = null;
                raesd.Intervention_Reason__c = null;
                raesd.Product_category_text__c = null;
                raesd.Salesdepartment_before__c = null;
                raesd.SalesProvince_before__c = null;
                raesd.UniqueKey_Queue__c = null;
                raesd.Zhu_Ti_Fen_Pei_Jia__c = null;
                raesd.Asset__c = null;
                raesd.SerialNumber_text__c = null;
                raesd.Fu_Shu_Pin_Fen_Pei_Jia__c = null;
                raesd.Jie_Chu_Fen_Pei_Jia__c = null;
                raesd.Shipment_request_time2__c = null;
                raesd.Shipment_request__c = false;
                raesd.Asset_cost_del_before__c = null;
                raesd.EquipmentSet_Managment_Code_text__c = null;
                raesd.Queue_Conment__c = null;
                raesd.Fixture_OneToOne_Link_Id__c = null;
            }
 
            if (ids.size() > 0) { // 2023-12-30 借用机会可视化 zyh 判断空
                List<Rental_Apply_Sequence__c> rasList = [SELECT Id from Rental_Apply_Sequence__c where Apply_Set_Detail__c IN: ids];
                if (!rasList.isEmpty()) {
                    delete rasList;
                }
            }
    
            update raesdList; 
            if (assIds.size() > 0) { // 2023-12-30 借用机会可视化 zyh 判断空
                List<Fixture_OneToOne_Link__c> fo2oList = [
                    SELECT Id, Main_Asset__c,
                        Accessory_Asset__c,
                        Accessory_Asset__r.Fixture_Model_No_F__c,
                        Quantity__c,
                        Select_Accessory_Asset_Cnt__c
                    FROM Fixture_OneToOne_Link__c
                    WHERE Accessory_Asset__c != null                   // 念のため
                    AND Main_Asset__c IN :assIds];
                for (Fixture_OneToOne_Link__c foto : fo2oList) {
                    foto.Select_Accessory_Asset_Cnt__c = 0;
                }
    
                if (!fo2oList.isEmpty()) {
                    update fo2oList;
                }
            }
        }
    }
    // 备品智能化项目对应 20231122 by lc End
 
    private void setAgencyHPReceived() {
        Set<Id> raIdSet = new Set<Id>();
        for (Rental_Apply__c nObj : newList) {
            Rental_Apply__c oObj;
            if(Trigger.isUpdate) {
                oObj = oldMap.get(nObj.Id);
                if(oObj.HP_received_sign_day__c != nObj.HP_received_sign_day__c
                    || oObj.HP_received_sign_rich__c != nObj.HP_received_sign_rich__c
                    || oObj.HP_received_sign_NG__c != nObj.HP_received_sign_NG__c
                    || oObj.HP_received_sign_NG_Reason__c != nObj.HP_received_sign_NG_Reason__c
                    || oObj.AssetManageConfirm__c != nObj.AssetManageConfirm__c
                    ) {
                    raIdSet.add(nObj.Id);
                }
            }
        }
        // 20220123 ljh update start
        if(raIdSet.size() > 0){
            List<Rental_Apply__c> childRaList = [
                SELECT Id
                     , Old_Rental_Apply__c,root_Rental_Apply__c 
                  FROM Rental_Apply__c
                  // WHERE Old_Rental_Apply__c IN:raIdSet
                   WHERE root_Rental_Apply__c  IN:raIdSet //20210611 ljh update 1732
                   AND RecordType.DeveloperName = 'AgencyRequest'
                   AND Split_Apply_Reason__c = '现地管理分单'
                   AND RA_Status__c <>'取消'// 20210719 SFDC-C539AF you
            ];
            for(Rental_Apply__c childRa: childRaList) {
                // Rental_Apply__c parentRa = newMap.get(childRa.Old_Rental_Apply__c);
                Rental_Apply__c parentRa = newMap.get(childRa.root_Rental_Apply__c); //20210611 ljh update 1732
                childRa.HP_received_sign_day__c = parentRa.HP_received_sign_day__c;
                childRa.HP_received_sign_rich__c = parentRa.HP_received_sign_rich__c;
                childRa.HP_received_sign_NG__c = parentRa.HP_received_sign_NG__c;
                childRa.HP_received_sign_NG_Reason__c = parentRa.HP_received_sign_NG_Reason__c;
                childRa.AssetManageConfirm__c = parentRa.AssetManageConfirm__c;
            }
            if(!childRaList.isEmpty()) {
                update childRaList;
                // 主单里附件
                //20231027  ymh添加注释   修改附件上传  start
                List<ContentDocumentLink> attList = [SELECT Id, LinkedEntityId, ContentDocumentId, IsDeleted, 
                                                    ContentDocument.Title
                                                    FROM ContentDocumentLink 
                                                    WHERE  LinkedEntityId IN: raIdSet];
                // 从单里附件,放到一起查会报limit错
                List<Id> racIdList = new List<Id>();
                for (Rental_Apply__c rac : childRaList) {
                    racIdList.add(rac.Id);
                }
                List<ContentDocumentLink> attList1 =[SELECT Id, LinkedEntityId, ContentDocumentId, IsDeleted, 
                                                    ContentDocument.Title
                                                    FROM ContentDocumentLink 
                                                    WHERE LinkedEntityId IN: racIdList];
                attList.addAll(attList1);
                if(attList.isEmpty()) {
                    return;
                }
                Map<Id, List<ContentDocumentLink>> parentFiles = new Map<Id, List<ContentDocumentLink>>();
                // 待删除附件
                List<ContentDocumentLink> deleteFiles = new List<ContentDocumentLink>();
                for(ContentDocumentLink att: attList) {
                    if(att.ContentDocument.Title.startsWith('QRCode-') || att.ContentDocument.Title.startsWith('BRCode-')) {
                        continue;
                    }
                    if(raIdSet.contains(att.LinkedEntityId)) {
                        List<ContentDocumentLink> tempList = null;
                        if(parentFiles.containsKey(att.LinkedEntityId)) {
                            tempList = parentFiles.get(att.LinkedEntityId);
                        }
                        else {
                            tempList = new List<ContentDocumentLink>();
                        }
                        tempList.add(att);
                        parentFiles.put(att.LinkedEntityId, tempList);
                    }
                    else {
                        deleteFiles.add(att);
                    }
                }
                // 待插入的附件
                List<ContentDocumentLink> newFiles = new List<ContentDocumentLink>();
                for(Rental_Apply__c childRa: childRaList) {
                    if(parentFiles.containsKey(childRa.Old_Rental_Apply__c)) {
                        for(ContentDocumentLink att : parentFiles.get(childRa.Old_Rental_Apply__c)){
                            newFiles.add(new ContentDocumentLink(ContentDocumentId = att.ContentDocumentId,LinkedEntityId = childRa.Id,ShareType = 'I',Visibility = 'AllUsers'));
                        }
                    }
                }
                // chenjingwu 20240229 start
                StaticParameter.ContentDocumentLink = false;
                // chenjingwu 20240229 end
                if(!deleteFiles.isEmpty()) {
                    delete deleteFiles;
                }
                if(!newFiles.isEmpty()) {
                    insert newFiles;
                }
                //20231027  ymh添加注释   修改附件上传  end
                
                // // 主单里附件
                // List<Attachment> attList = [SELECT Id, Body, Name, ParentId
                //                             FROM Attachment
                //                             WHERE ParentId IN: raIdSet
                //                             ];
                // // 从单里附件,放到一起查会报limit错
                // attList.addAll([SELECT Id, Name, ParentId FROM Attachment WHERE ParentId IN:childRaList]);
                // if(attList.isEmpty()) {
                //     return;
                // }
                // Map<Id, List<Attachment>> parentFiles = new Map<Id, List<Attachment>>();
                // // 待删除附件
                // List<Attachment> deleteFiles = new List<Attachment>();
                // for(Attachment att: attList) {
                //     if(att.Name.startsWith('QRCode-')) {
                //         continue;
                //     }
                //     if(raIdSet.contains(att.ParentId)) {
                //         List<Attachment> tempList = null;
                //         if(parentFiles.containsKey(att.ParentId)) {
                //             tempList = parentFiles.get(att.ParentId);
                //         }
                //         else {
                //             tempList = new List<Attachment>();
                //         }
                //         tempList.add(att);
                //         parentFiles.put(att.ParentId, tempList);
                //     }
                //     else {
                //         deleteFiles.add(att);
                //     }
                // }
                // // 待插入的附件
                // List<Attachment> newFiles = new List<Attachment>();
                // for(Rental_Apply__c childRa: childRaList) {
                //     if(parentFiles.containsKey(childRa.Old_Rental_Apply__c)) {
                //         for(Attachment att : parentFiles.get(childRa.Old_Rental_Apply__c)){
                //             newFiles.add(new Attachment(Body = att.Body,Name = att.Name, ParentId = childRa.Id));
                //         }
                //     }
                // }
                // if(!deleteFiles.isEmpty()) {
                //     delete deleteFiles;
                // }
                // if(!newFiles.isEmpty()) {
                //     insert newFiles;
                // }
            }
        }
    }
 
    // 前提: before 的时候 一定要运行 setOffice_Assistant() 设定 Office_Assistant1__c 和 Office_Assistant2__c
    // 20210727 ljh SFDC-C54C33 前提: before 的时候 一定要运行 setManager() 设定新的经理 部长 总监
    // after insert, after update
    private void setShare() {
        try{
        List<Rental_Apply__Share> rasList = new List<Rental_Apply__Share>();
        List<Id> deleteOfficeAssistantShare_nObjId_List = new List<Id>();       // 共享删除用
        List<Id> deleteApplyUserShare_nObjId_List = new List<Id>();// 20210727 ljh SFDC-C54C33 共享删除用
        Set<Id> shareSet = new Set<Id>(); // 20230301 ljh DB202302444522 add
        for (Rental_Apply__c nObj : newList) {
            Rental_Apply__c oObj = (null == this.oldMap) ? null : this.oldMap.get(nObj.Id);
            // 服务部审批人
            if (nObj.Approver_of_Service_Department__c != null
                && (Trigger.isInsert
                    || (Trigger.isUpdate
                            && oObj.Approver_of_Service_Department__c != nObj.Approver_of_Service_Department__c
                        )
                    )
            ) {
                Rental_Apply__Share ras = new Rental_Apply__Share(
                            RowCause = 'ApplyUserShare__c',
                            ParentId = nObj.Id,
                            UserOrGroupId = nObj.Approver_of_Service_Department__c,
                            AccessLevel = 'Edit'
                        );
                rasList.add(ras);
            }
            // 办事处助理
            if (Trigger.isInsert
                || (oObj.Office_Assistant1__c != nObj.Office_Assistant1__c
                    || oObj.Office_Assistant2__c != nObj.Office_Assistant2__c
                )
            ) {
                deleteOfficeAssistantShare_nObjId_List.add(nObj.Id);
                //String theId=UserInfo.getUserId();
                //User theUser=[select IsActive from user where id=:theId];
                //if(theUser.IsActive==true){
                    if (nObj.Office_Assistant1__c != null) {
                    rasList.add(new Rental_Apply__Share(
                        RowCause = 'Office_Assistant__c',
                        ParentId = nObj.Id,
                        UserOrGroupId = nObj.Office_Assistant1__c,
                        AccessLevel = 'Edit'
                    ));
                //}
                if (nObj.Office_Assistant2__c != null) {
                    rasList.add(new Rental_Apply__Share(
                        RowCause = 'Office_Assistant__c',
                        ParentId = nObj.Id,
                        UserOrGroupId = nObj.Office_Assistant2__c,
                        AccessLevel = 'Edit'
                    ));
                }
                
                }
            }
            //20210727 ljh SFDC-C54C33  add start
            //审批 共享 经理、部长、总监(若有审批要求共享参考上面  办事处助理 最终定位到人员上变化)// 20240108 ljh 智能化优化 add OPDApprovalStatus__c
            if ((Trigger.isUpdate && ((oObj.Status__c != nObj.Status__c && nObj.Status__c == '填写完毕') || (oObj.OPDApprovalStatus__c != nObj.OPDApprovalStatus__c && nObj.OPDApprovalStatus__c == '提交完毕'))) 
                || (Trigger.isUpdate && oObj.ExtensionStatus__c != nObj.ExtensionStatus__c && nObj.ExtensionStatus__c == '填写完毕')
                || (Trigger.isUpdate && oObj.Add_Approval_Status__c != nObj.Add_Approval_Status__c && nObj.Add_Approval_Status__c == '填写完毕')
                ) {
                deleteApplyUserShare_nObjId_List.add(nObj.Id);
                if(nObj.SalesManager__c != null){
                    shareSet.add(nObj.SalesManager__c);// 20230301 ljh DB202302444522 add
                    Rental_Apply__Share rasSalesManager = new Rental_Apply__Share(
                            RowCause = 'ApplyUserShare__c',
                            ParentId = nObj.Id,
                            UserOrGroupId = nObj.SalesManager__c,
                            AccessLevel = 'Edit'
                        );
                    rasList.add(rasSalesManager);
                }
                if(nObj.BuchangApprovalManagerSales__c != null){
                    shareSet.add(nObj.BuchangApprovalManagerSales__c);// 20230301 ljh DB202302444522 add
                    Rental_Apply__Share rasBz = new Rental_Apply__Share(
                            RowCause = 'ApplyUserShare__c',
                            ParentId = nObj.Id,
                            UserOrGroupId = nObj.BuchangApprovalManagerSales__c,
                            AccessLevel = 'Edit'
                        );
                    rasList.add(rasBz);
                }
                if(nObj.ZongjianApprovalManager__c != null){
                    shareSet.add(nObj.ZongjianApprovalManager__c);// 20230301 ljh DB202302444522 add
                    Rental_Apply__Share rasZj = new Rental_Apply__Share(
                            RowCause = 'ApplyUserShare__c',
                            ParentId = nObj.Id,
                            UserOrGroupId = nObj.ZongjianApprovalManager__c,
                            AccessLevel = 'Edit'
                        );
                    rasList.add(rasZj);
                }  
            }                
            //20210727 ljh SFDC-C54C33  add end
        }
        // 先 Delete, 后 Insert
        //20210727 ljh SFDC-C54C33  update start
        /*List<Rental_Apply__Share> deleteShareList = [SELECT Id, UserOrGroupId, ParentId, UserOrGroup.Name
                 FROM Rental_Apply__Share
                WHERE RowCause = 'Office_Assistant__c'
                  AND ParentId =: deleteOfficeAssistantShare_nObjId_List];*/
        String soql = 'SELECT Id, UserOrGroupId, ParentId, UserOrGroup.Name FROM Rental_Apply__Share ';
        soql += ' WHERE  Id != null ';
        if(deleteOfficeAssistantShare_nObjId_List.size() > 0){
            soql += ' AND (RowCause = \'Office_Assistant__c\' AND ParentId =: deleteOfficeAssistantShare_nObjId_List) ';
            if(deleteApplyUserShare_nObjId_List.size() > 0){
                // 20230301 ljh DB202302444522 update start
                // soql += ' OR (RowCause = \'ApplyUserShare__c\'AND ParentId =: deleteApplyUserShare_nObjId_List)';
                soql += ' OR (RowCause = \'ApplyUserShare__c\'AND ParentId =: deleteApplyUserShare_nObjId_List';
                soql += ' and UserOrGroupId IN :shareSet)';
                // 20230301 ljh DB202302444522 update end
            }
        }else if(deleteApplyUserShare_nObjId_List.size() > 0){
            // 20230301 ljh DB202302444522 update start
            // soql += ' AND (RowCause = \'ApplyUserShare__c\'AND ParentId =: deleteApplyUserShare_nObjId_List)';
            soql += ' AND (RowCause = \'ApplyUserShare__c\'AND ParentId =: deleteApplyUserShare_nObjId_List';
            soql += ' and UserOrGroupId IN :shareSet)';
            // 20230301 ljh DB202302444522 update end
        }
        List<Rental_Apply__Share> deleteShareList = new List<Rental_Apply__Share>();
        if(deleteOfficeAssistantShare_nObjId_List.size() > 0 || deleteApplyUserShare_nObjId_List.size() > 0){
            deleteShareList = Database.query(soql);
        }
        //20210727 ljh SFDC-C54C33  update end
        if (deleteShareList.size() > 0) { delete deleteShareList; }
        if (rasList.size() > 0) { insert rasList; }
        }
         catch(Exception e){
           String msg=e.getMessage();
           if(msg!=null && msg.containsIgnoreCase('INACTIVE_OWNER_OR_USER')){
           Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'未激活的审批人账号:该服务部审批人已离职,不能分单'));
           }else{
              Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,msg));
           }
     }
 
 
 
    }
 
    // 延期审批逻辑修改前需要Check批准后是否需要Check的条件
    private void setAppExtensionRaes() {
        List<Rental_Apply_Equipment_Set__c> raesList = new List<Rental_Apply_Equipment_Set__c>();
        // 批准只能一条一条的批准
        for (Rental_Apply__c nObj : newList) {
            Rental_Apply__c oObj = oldMap.get(nObj.Id);
            if (oldRaMap.containsKey(nObj.Id)) {
                oObj = oldRaMap.get(nObj.Id);
            }
            oldRaMap.put(nObj.Id, nObj);
            //update          wangweipeng                  2021/12/07                start
            //如果为批量延期,并且延期的是主单下的从单,那么会走主单的审批流,而在审批过程中,会判断当前审批的单子是否满足延期条件
            //所有我们需要判断是延期的这几个从单是否满足条件
            //如果主单的延期信息设为空,那么不需要走这里的逻辑,因为可能会出现以下情况:
            //批量延期,入口为主单,主单没延期,但是从单延期,那么走完流程以后会清空主单的延期信息
            if ((oObj.ExtensionApprovalTime_Initial__c != nObj.ExtensionApprovalTime_Initial__c 
                    && nObj.ExtensionApprovalTime_Initial__c != null && oObj.ExtensionApprovalTime_Initial__c == null) 
                || (oObj.ExtensionApprovalTime_Final__c != nObj.ExtensionApprovalTime_Final__c && nObj.demo_purpose1__c == '协议借用') // 只有第二次需要审批流的才需要做check,不需要的时候设值之前已经check了
                || (oObj.Extension_NewStep_AppTime__c != nObj.Extension_NewStep_AppTime__c && nObj.Extension_NewStep_AppTime__c != null)) 
            {
                try {
                    //存放需要 验证是否可以延期的申请单
                    //如果为批量延期,那么这个集合里面会存放 原单+原单下所有的从单
                    //如果为从单,并且目的2为询价,那么会存放 当前从单的原单+从单原单下所有的从单(包括当前从单)
                    List<Rental_Apply__c> checkRentalApply = new List<Rental_Apply__c>();
                    // 只有产品试用会存在批量延期
                    if(String.isNotBlank(nObj.Extension_Type__c) && nObj.Extension_Type__c == '批量延期'){
                        System.debug('========================1=========================');
                        if(String.isNotBlank(nObj.Extension_Much_ID__c)){
                            System.debug('========================2=========================');
                            String parentId = nObj.Id;
                            parentId = parentId.substring(0,15);
                            String likeParentId = parentId+'%';
 
                            checkRentalApply = [SELECT id,
                                                    Name,
                                                    RA_Status__c,
                                                    Request_return_day__c,
                                                    demo_purpose1__c,demo_purpose2__c,
                                                    ExtensionApprovalTime_Final__c,
                                                    RC_Ordered_Date__c,
                                                    Bollow_Date_Add_10_WD__c,
                                                    Loaner_received_ng_num__c,
                                                    ExtensionApprovalTime_Initial__c,
                                                    next_action__c,
                                                    NewRepair__c,
                                                    NewRepair__r.Agreed_Date__c,
                                                    NewRepair__r.Status__c,
                                                    NewRepair__r.ReRepairObject_F__c,
                                                    NewRepair__r.Repair_Shipped_Date__c,
                                                    AgreementBorrowingExtensionDate__c,
                                                    Return_dadeline_final__c,
                                                    ExtensionApplicationTime_Initial__c,
                                                    Root_Rental_Apply__c,
                                                    ExtensionStatus__c
                                                FROM Rental_Apply__c WHERE id = :parentId OR Root_Rental_Apply__c like :likeParentId 
                                            order by CreatedDate asc];
                        }
                    }else{
                        System.debug('========================3=========================');
                        //如果延期的是从单,那么需要特殊处理
                        if(String.isNotBlank(nObj.Root_Rental_Apply__c) && (nObj.demo_purpose2__c == '试用(无询价)' || nObj.demo_purpose2__c == '试用(有询价)')){
                            String likeParentId = nObj.Root_Rental_Apply__c+'%';
                            checkRentalApply = [SELECT id,
                                                    Name,
                                                    RA_Status__c,
                                                    Request_return_day__c,
                                                    demo_purpose1__c,demo_purpose2__c,
                                                    ExtensionApprovalTime_Final__c,
                                                    RC_Ordered_Date__c,
                                                    Bollow_Date_Add_10_WD__c,
                                                    Loaner_received_ng_num__c,
                                                    ExtensionApprovalTime_Initial__c,
                                                    next_action__c,
                                                    NewRepair__c,
                                                    NewRepair__r.Agreed_Date__c,
                                                    NewRepair__r.Status__c,
                                                    NewRepair__r.ReRepairObject_F__c,
                                                    NewRepair__r.Repair_Shipped_Date__c,
                                                    AgreementBorrowingExtensionDate__c,
                                                    Return_dadeline_final__c,
                                                    ExtensionApplicationTime_Initial__c,
                                                    Root_Rental_Apply__c,
                                                    ExtensionStatus__c
                                                FROM Rental_Apply__c 
                                                WHERE id != :nObj.Id 
                                                and (Root_Rental_Apply__c like :likeParentId OR id = :nObj.Root_Rental_Apply__c)
                                            order by CreatedDate asc];
 
                        }
                        checkRentalApply.add(oObj);
                    }
 
                    System.debug('========================checkRentalApply=========================' + checkRentalApply);
                    for (Rental_Apply_Equipment_Set__c raes : getCan_Extend_RequestList(checkRentalApply)) {
                        //延期批准时间(最初)或延期批准时间(最终) 值都有变动,那么证明此次延期已经批准了,那么需要给申请单的一览赋值
                        if (oObj.ExtensionApprovalTime_Initial__c != nObj.ExtensionApprovalTime_Initial__c
                            || oObj.ExtensionApprovalTime_Final__c != nObj.ExtensionApprovalTime_Final__c
                        ) {
                            //如果目的2为以下,那么证明需要做特殊处理
                            if(nObj.demo_purpose2__c == '试用(无询价)' || nObj.demo_purpose2__c == '试用(有询价)'){
                                //判断是此申请单是否存在 ok并且回寄时间不为空的一览,
                                if ((raes.Received_Confirm__c == 'OK' || raes.Received_Confirm__c == '默认签收-OK') && raes.Asset_return_time__c != null) {
 
                                }else{
                                    //如果是批量延期,要把此次延期的所有从单的配套都赋值
                                    if(String.isNotBlank(nObj.Extension_Type__c) && nObj.Extension_Type__c == '批量延期'){
                                        String emicc = raes.Rental_Apply__c;
                                        emicc = emicc.substring(0,15);
                                        if(String.isNotBlank(nObj.Extension_Much_ID__c)){
                                            for(String emic : nObj.Extension_Much_ID__c.split(',')){
                                                if(String.isNotBlank(emic)){
                                                    emic = emic.substring(0,15);
                                                    if(emic == emicc){
                                                        raes.RcUnexpectExpiryDelay__c = raes.Rental_Apply__r.RcUnexpectExpiryDelay__c;
                                                    }
                                                }
                                            }
                                        }
                                        //查看此次是否延期原单了,如果延了,那么把主单的配套也赋值
                                        if(!nObj.Is_Delete_Extension__c){
                                            String emiccc = nObj.Id;
                                            emiccc = emiccc.substring(0,15);
                                            if(emiccc == emicc){
                                                raes.RcUnexpectExpiryDelay__c = raes.Rental_Apply__r.RcUnexpectExpiryDelay__c;
                                            }
                                        }
                                    }else{
                                        //raes.RcUnexpectExpiryDelay__c = raes.RcUnexpectExpiryDelay__c;
                                        //判断是否是从单,如果是从单,那么只给从单一览赋值
                                        if(String.isNotBlank(nObj.Root_Rental_Apply__c)){
                                            if(raes.Rental_Apply__c == nObj.Id){
                                                raes.RcUnexpectExpiryDelay__c = raes.Rental_Apply__r.RcUnexpectExpiryDelay__c;
                                            }
                                        }else{//如果不为从单,那么证明此次延期为原单,他没有分割单,所有只把他自己的配套赋值就行
                                            raes.RcUnexpectExpiryDelay__c = raes.Rental_Apply__r.RcUnexpectExpiryDelay__c;
                                        }
                                    }
                                    //update          wangweipeng                  2021/12/07                end
                                    raesList.add(raes);
                                }
                            }else{//其他延期的配套赋值
                                raes.RcUnexpectExpiryDelay__c = raes.Rental_Apply__r.RcUnexpectExpiryDelay__c;
                                raesList.add(raes);
                            }
                        }
                    }
                }
                catch (Exception e) {
                    nObj.addError(e.getMessage() + ',请操作驳回。');
                }
            }
        }
        if (0 < raesList.size()) {
            update raesList;
        }
    }
    // 20220909 ljh 恢复代码
    private void checkbenbu() {
        for (Rental_Apply__c nObj : newList) {
            if (nObj.DataMigration_Flag__c == false) {
                Rental_Apply__c oObj;
                if (Trigger.isUpdate) {
                    oObj = oldMap.get(nObj.Id);
                }
                if ((Trigger.isInsert
                        || oObj.Demo_purpose2__c != nObj.Demo_purpose2__c
                        || oObj.Salesdept__c != nObj.Salesdept__c)
                        // 日报画面新建的情况,可以不填使用目的
                        && !(nObj.Demo_purpose2__c == null && nObj.Event_Id__c != null)) {
                    if (!FixtureUtil.departmentMap.containsKey(nObj.Demo_purpose2__c)) {
                        nObj.Demo_purpose2__c.addError('没有定义目的2 ' + nObj.Demo_purpose2__c + '可以选择的本部');
                    }
                    else {
                        Set<String> benbuSet = new Set<String>();
                        benbuSet.addAll(FixtureUtil.departmentMap.get(nObj.Demo_purpose2__c));
                        if (!benbuSet.contains(nObj.Salesdept__c)) {
                            nObj.Person_In_Charge__c.addError('此用户无该使用目的的申请权限');
                        }
                    }
                }
            }
        }
    }
 
    // 申请书部长经理等设置
    private void setManager() {
        // 申請中かどうかのチェック
        List<Id> copyUserIds = new List<Id>();                       // 件数は Trigger.New と同じ
        List<Rental_Apply__c> newList1 = new List<Rental_Apply__c>(); // 件数は Trigger.New と同じ
        for (Rental_Apply__c nObj : newList) {
            Rental_Apply__c oObj;
            if (Trigger.isUpdate) {
                oObj = oldMap.get(nObj.Id);
            }
            if (nObj.Person_In_Charge__c != null) nObj.OwnerId = nObj.Person_In_Charge__c;
            System.debug(nObj.Person_In_Charge__c);
            System.debug(nObj.OwnerId);
            if (Trigger.isInsert
                        || (Trigger.isUpdate && oObj.Status__c != nObj.Status__c && nObj.Status__c == '填写完毕')
                        || (Trigger.isUpdate && oObj.OPDApprovalStatus__c != nObj.OPDApprovalStatus__c && nObj.OPDApprovalStatus__c == '提交完毕') // 20240108 ljh 智能化优化 add OPDApprovalStatus__c
                        || (Trigger.isUpdate && oObj.ExtensionStatus__c != nObj.ExtensionStatus__c && nObj.ExtensionStatus__c == '填写完毕')
                        || (Trigger.isUpdate && oObj.Add_Approval_Status__c != nObj.Add_Approval_Status__c && nObj.Add_Approval_Status__c == '填写完毕')//20210727 ljh SFDC-C54C33  add start
                        || (Trigger.isUpdate && oObj.OwnerId != nObj.OwnerId)
                ) {
                    newList1.add(nObj);
                    copyUserIds.add(nObj.OwnerId);
                }
        }
        System.debug(copyUserIds);
        if (copyUserIds.size() > 0) {
            Map<Id, User> copyUserMap = new Map<Id, User>([
                SELECT Id, Name, Buzhang_Equipment_Manager__c, JingliEquipmentManager__c, SalesManager__c, BuchangApprovalManagerSales__c, JingliApprovalManager__c, BuchangApprovalManager__c, ZongjianApprovalManager__c, TongkuoZongjian__c FROM User WHERE Id IN :copyUserIds
            ]);
 
            for (Integer i = 0; i < copyUserIds.size(); i++) {
                Rental_Apply__c nObj = newList1[i];
                User loginUser = copyUserMap.get(copyUserIds[i]);
                nObj.SalesManager__c = loginUser.JingliEquipmentManager__c;
                nObj.BuchangApprovalManagerSales__c = loginUser.Buzhang_Equipment_Manager__c;
                nObj.JingliApprovalManager__c = loginUser.JingliApprovalManager__c;
                nObj.BuchangApprovalManager__c = loginUser.BuchangApprovalManager__c;
                nObj.ZongjianApprovalManager__c = loginUser.ZongjianApprovalManager__c;
                nObj.TongkuoZongjian__c = loginUser.TongkuoZongjian__c;
                 System.debug(loginUser);
            }
        }
    }
 
    private void beforeSetValue() {
        List<Rental_Apply__c> ApprovalApply = new List<Rental_Apply__c>();
        List<Rental_Apply__c> addApprovalApply = new List<Rental_Apply__c>();
        Set<Id> hpIdSet = new Set<Id>();
        Map<Rental_Apply__c, Id> eramap = new Map<Rental_Apply__c, Id>();
        Map<Rental_Apply__c, Id> newRepairMap = new Map<Rental_Apply__c, Id>();
        Map<Id, Id> needRaMap = new Map<Id, Id>();
        for (Rental_Apply__c nObj : newList) {
            nObj.HP_received_Confirmed__c  = nObj.HP_received_Confirmed_F__c;
            nObj.HP_received_Confirmed__c = nObj.HP_received_Confirmed_F__c;
            if (nObj.Hospital__c != null ) hpIdSet.add(nObj.Hospital__c);
            Rental_Apply__c oObj;
            if (Trigger.isUpdate) {
                oObj = oldMap.get(nObj.Id);
 
                if (oObj.ExtensionApprovalTime_Final__c != nObj.ExtensionApprovalTime_Final__c
                ) {
                    Map<String, Object> bkMap = new Map<String, Object>();
                    if (String.isNotBlank(nObj.Apply_Backup__c)) {
                        bkMap = (Map<String, Object>) JSON.deserializeUntyped(nObj.Apply_Backup__c);
                    }
                    bkMap.put('ExtensionApplicationTime_Final__c', nObj.ExtensionApplicationTime_Final__c.formatGmt('yyyy-MM-dd HH:mm:ss'));
                    nObj.Apply_Backup__c = JSON.serialize(bkMap);
                }
 
                // 备品智能化项目对应 20231122 by lc Start
                // 跨区域分配时,清空分配和排队相关的信息
                if (nObj.Cross_Region_Assign__c != oObj.Cross_Region_Assign__c) {
                    nObj.Assign_Person__c = null;
                    nObj.Assigned_Count__c = null;
                    nObj.Assigned_Hash__c = null;
                    nObj.Request_answer_time__c = null;
                }
                // 备品智能化项目对应 20231122 by lc End
 
                if (oObj.ExtensionStatus__c != nObj.ExtensionStatus__c
                    && nObj.ExtensionStatus__c == '已批准'
                    && oObj.ExtensionApprovalTime_Final__c == nObj.ExtensionApprovalTime_Final__c
                    && oObj.ExtensionApprovalTime_Initial__c == nObj.ExtensionApprovalTime_Initial__c
                    && String.isNotBlank(nObj.Apply_Backup__c)
                    ) {
                    System.debug(nObj.Apply_Backup__c);
                    Map<String, Object> bkMap = (Map<String, Object>) JSON.deserializeUntyped(nObj.Apply_Backup__c);
                    System.debug(Datetime.valueOfGmt((String)bkMap.get('ExtensionApplicationTime_Final__c')));
                    nObj.ExtensionApplicationTime_Final__c = Datetime.valueOfGmt((String)bkMap.get('ExtensionApplicationTime_Final__c'));
                }
 
                if (oObj.Status__c != FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Qu_Xiao.ordinal())
                    && nObj.Status__c == FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Qu_Xiao.ordinal())) {
                    nObj.Cancel_time__c = Datetime.now();
                    // nObj.Cancel_Mem__c = UserInfo.getUserId();
                }
                if (oObj.NewRepair__c != nObj.NewRepair__c
                    && String.isNotBlank(nObj.NewRepair__c)
                ) {
                    newRepairMap.put(nObj, nObj.NewRepair__c);
                }
                if (nObj.demo_purpose2__c == '索赔QIS') {
                    needRaMap.put(nObj.Id, nObj.QISRepair__c);
                }
                else {
                    needRaMap.put(nObj.Id, nObj.Repair__c);
                }
                // 20240108 ljh 智能化优化 add OPDApprovalStatus__c
                if ((oObj.Status__c != '填写完毕' && nObj.Status__c == '填写完毕') || (oObj.OPDApprovalStatus__c != '填写完毕' && nObj.OPDApprovalStatus__c == '提交完毕')) {
                    ApprovalApply.add(nObj);
                    if (rental_Apply_App_CCEmailMap.containsKey(nObj.Salesdept__c)) {
                        String ccUser = nObj.get(rental_Apply_App_CCEmailMap.get(nObj.Salesdept__c)) == null ? null : String.valueOf(nObj.get(rental_Apply_App_CCEmailMap.get(nObj.Salesdept__c)));
                        nObj.CC_EmailUser__c =  ccUser;
                    }
                }
 
                if (oObj.Add_Approval_Status__c != '填写完毕' && nObj.Add_Approval_Status__c == '填写完毕') {
                    addApprovalApply.add(nObj);
                }
 
                //批准之前就有批准时间的话需要清空
                if ((nObj.Status__c == '草案中'
                      || nObj.Status__c == '填写完毕'
                      || nObj.Status__c == '申请中'
                      || nObj.Status__c == '申请中(OPD未通过)')       ////20231224 sx add 备品智能化添加状态申请中(OPD未通过) 优化
                    && nObj.Request_approval_time__c != null) {
                    nObj.Request_approval_time__c = null;
                }
 
                // 提交申请的时候设置跟进询价状态(申请时)
                if (oObj.Status__c != FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Tian_Xie_Wan_Bi.ordinal())
                    && nObj.Status__c == FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Tian_Xie_Wan_Bi.ordinal())) {
                        nObj.Follow_pcl_status2_Text__c = nObj.Follow_pcl_status2__c;
                }
 
                Rental_Apply__c oObj1 = oObj;
                if (oldRaMap.containsKey(nObj.Id)) {
                    oObj1 = oldRaMap.get(nObj.Id);
                }
                if (oObj.ExtensionApprovalTime_Initial__c == null || nObj.demo_purpose1__c == '协议借用') {
                    if (oObj.ExtensionStatus__c != nObj.ExtensionStatus__c
                        && nObj.ExtensionStatus__c == '填写完毕'
                        && nObj.Approver_of_Service_Department__c == null
                        && String.isNotBlank(nObj.NewRepair__c)
                    ) {
                        eramap.put(nObj, nObj.NewRepair__c);
                    }
                    else if (oObj1.ExtensionApprovalTime_Initial__c != nObj.ExtensionApprovalTime_Initial__c
                        || oObj1.ExtensionApprovalTime_Final__c != nObj.ExtensionApprovalTime_Final__c
                    ) {
                        if (nObj.ExtensionSuccessTimes__c == null) {
                            nObj.ExtensionSuccessTimes__c = 0;
                        }
                        nObj.ExtensionSuccessTimes__c += 1;
                        nObj.RcUnexpectExpiryDelay__c = oObj.RcUnexpectExpiryDelay_Mail__c;
                        nObj.ExtensionContent__c = '申请延期从' + oObj.Return_dadeline_final__c + '延期到' + nObj.RcUnexpectExpiryDelay__c;
                    }
                }
            }
            if (nObj.demo_purpose2__c == '试用(无询价)'
                    || nObj.demo_purpose2__c == '试用(有询价)'
                    || nObj.demo_purpose2__c == '新产品评价'
                    || nObj.demo_purpose2__c == '其他'
                    || nObj.demo_purpose2__c == '协议借用') {
                if (trigger.isInsert
                        || (oObj.Request_shipping_day__c != nObj.Request_shipping_day__c)
                        || oObj.Hope_Lonaer_date_Num__c != nObj.Hope_Lonaer_date_Num__c) {
                    if (nObj.Hope_Lonaer_date_Num__c != null && nObj.Request_shipping_day__c != null) {
                        nObj.Request_return_day__c = (nObj.Request_shipping_day__c + Integer.valueOf(nObj.Hope_Lonaer_date_Num__c));
                    }
                    else {
                        nObj.Request_return_day__c = null;
                    }
                }
            }
            else if (nObj.demo_purpose2__c == '一般用户'
                    || nObj.demo_purpose2__c == '保修用户'
                    || nObj.demo_purpose2__c == '市场多年保修'
                    || nObj.demo_purpose2__c == '再修理'
                    || nObj.demo_purpose2__c == '索赔QIS'
                    || nObj.demo_purpose2__c == '已购待货') {
                // 不需要设置预计归还日
            }
            else if (nObj.demo_purpose2__c == '学会展会') {
                // 不需要设置预计归还日
            }
            // 必ず最後で置く
            nObj.Status_Text__c = nObj.Status__c;
            nObj.RA_Status_Text__c = nObj.RA_Status__c;
            //20230911  sx add 新加字段 start
            nObj.Rental_Status__c = nObj.RA_Status__c;
            //20230911  sx add 新加字段 end
            nObj.NotWatch_RA_Status__c = nObj.NotWatch_RA_Status_F__c;
            nObj.Notice_of_Delivery_Hash__c = getHash('SHA-256', nObj.Notice_of_Delivery_Text__c);
            nObj.Assigned_Hash__c = getHash('SHA-256', nObj.Assigned_Text__c);
            // OLY_OCM-621 From WF 设定-申请者相关字段文本化
            if (String.isBlank(nObj.Work_Location_text__c)
                    || String.isBlank(nObj.Owner_province_text__c)
                    || String.isBlank(nObj.Onwer_job_category_text__c)
                    || String.isBlank(nObj.Salesdepartment_text__c)
                    || String.isBlank(nObj.Branch_text__c)
                    || String.isBlank(nObj.Salesdept_text__c)
                    || (Trigger.isUpdate
                            && (oObj.OwnerId != nObj.OwnerId || hasInsert))) {
                // 设定-借出申请人-工作地(文本)
                nObj.Work_Location_text__c = nObj.Work_Location__c;
                // 设定-借出申请人-省(文本)
                nObj.Owner_province_text__c = nObj.Owner_province__c;
                // 设定-借出申请人-职种(文本)
                nObj.Onwer_job_category_text__c = nObj.Onwer_job_category__c;
                // 设定-借出申请人-销售本部(文本)
                nObj.Salesdepartment_text__c = nObj.Salesdepartment__c;
                // 设定-借出申请人-分公司(文本)
                nObj.Branch_text__c = nObj.Branch__c;
                // 设定-申请者销售本部(文本)
                nObj.Salesdept_text__c = nObj.Salesdept__c;
 
                // OLY_OCM-666 第二次trigger更新正确数据, 新建数据时第二次更新OwnerId无变化, 需要强制更新
                if (Trigger.isInsert) {
                    RentalApplyTriggerHandler.hasInsert = true;
                }
            }
        }
        //拷贝医院的市字段
        Map<Id, Account> accMap = new Map<Id, Account>();
        // Set<Id> hpIdSetCopy = new Set<Id>();
        // Integer count = 0;
        if (hpIdSet.size() > 0) {
        //    for (Id a : hpIdSet) {
        //        hpIdSetCopy.add(a);
        //        count++;
        //        if (count>=99) {
        //           break; 
        //        }
        //    }
            accMap.putAll([SELECT Id, City_Master__r.Name, State_Text__c FROM Account WHERE Id IN: hpIdSet]);
            for (Rental_Apply__c nObj : newList) {
                if (accMap.containsKey(nObj.Hospital__c)) {
                    nObj.HP_City__c = accMap.get(nObj.Hospital__c).City_Master__r.Name;
                }
            }
        }
 
        if (!ApprovalApply.isEmpty()) {
            List<Rental_Apply_Equipment_Set__c> raess = [Select Id, Loaner_name_F__c, Rental_Apply__c, Loaner_code_F__c,
                    First_RAESD_Model_No_F__c
                    From Rental_Apply_Equipment_Set__c
                    Where Rental_Apply__c =: ApprovalApply
                    AND Cancel_Select__c = false
                    order by Rental_Apply__c];
            Map<Id, String> raMap = new Map<Id, String>();
            String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
            for (Integer i = 0; i < raess.size(); i ++) {
                Rental_Apply_Equipment_Set__c raes = raess[i];
                if(!raMap.containsKey(raes.Rental_Apply__c)) {
                    raMap.put(raes.Rental_Apply__c, '');
                }
                String str = raMap.get(raes.Rental_Apply__c);
                raMap.put(raes.Rental_Apply__c,str + '备品配套'
                        + (i + 1)
                        + ':<BR>'
                        // + '<a href="'
                        // + baseUrl + '/' + raes.Id
                        // +'">'
                        + '' + raes.First_RAESD_Model_No_F__c
                        // + '  '
                        // + ' 主体明细型号:' + raes.First_RAESD__r.Fixture_Model_No_F__c
                        // + + '</a>'
                        + '<BR>');
            }
            for (Rental_Apply__c nObj : newList) {
                if (raMap.containsKey(nObj.Id)) {
                    nObj.Email_Rental_Apply_Equipment_Set__c = raMap.get(nObj.id);
                }
            }
        }
 
        if (!addApprovalApply.isEmpty()) {
            List<Rental_Apply_Equipment_Set_Detail__c> raesds = [SELECT Id
                        , Fixture_Model_No_F__c
                        , Rental_Apply_Equipment_Set__r.Rental_Apply__c
                     FROM Rental_Apply_Equipment_Set_Detail__c
                    WHERE Rental_Apply_Equipment_Set__r.Rental_Apply__c = :addApprovalApply
                      AND Cancel_Select__c = false
                      AND ApplyPersonAppended_F__c = true
                      AND Add_Request_approval_time__c = null
                      AND Add_Request_demo_time__c = null
                    ORDER BY Rental_Apply_Equipment_Set__r.Rental_Apply__c];
            String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
            Map<Id, String> raMap = new Map<Id, String>();
            for (Integer i = 0; i < raesds.size(); i ++) {
                Rental_Apply_Equipment_Set_Detail__c raes = raesds[i];
                if(!raMap.containsKey(raes.Rental_Apply_Equipment_Set__r.Rental_Apply__c)) {
                    raMap.put(raes.Rental_Apply_Equipment_Set__r.Rental_Apply__c, '');
                }
                String str = raMap.get(raes.Rental_Apply_Equipment_Set__r.Rental_Apply__c);
                raMap.put(raes.Rental_Apply_Equipment_Set__r.Rental_Apply__c,str + '备品明细'
                        + (i + 1)
                        + ':<BR>'
                        // + '<a href="'
                        // + baseUrl + '/' + raes.Id
                        // +'">'
                        + '型号:' + raes.Fixture_Model_No_F__c
                        // + '</a>'
                        + '<BR>');
            }
            for (Rental_Apply__c nObj : newList) {
                if (raMap.containsKey(nObj.Id)) {
                    nObj.Email_Add_Detail__c = raMap.get(nObj.id);
                }
            }
        }
 
        if (eramap.isEmpty() == false) {
            Map<Id, Repair__c> rsMap = new Map<Id, Repair__c>();
            // 借用机会可视化 SQL101优化 zyh 20231223 start
            approver_of_Service_DepartmentMap = customPostponeWorkLocation();
            // 借用机会可视化 SQL101优化 zyh 20231223 end
            
            for (Repair__c re : [SELECT ID
                                        , work_location_select__c
                                        , Delivered_Product__c
                                    FROM Repair__c
                                    WHERE Id =:eramap.values()
                                       OR Id = :needRaMap.values()]) {
                if (approver_of_Service_DepartmentMap.containsKey(re.work_location_select__c)) {
                    rsMap.put(re.Id, re);
                }
            }
            for (Rental_Apply__c nObj : eramap.keySet()) {
                if (rsMap.get(needRaMap.get(nObj.Id)).Delivered_Product__c != rsMap.get(nObj.NewRepair__c).Delivered_Product__c) {
                    nObj.NewRepair__c.addError('新修理必须和原修理是同一设备');
                }
                if (rsMap.containsKey(nObj.NewRepair__c)) {
                    nObj.Approver_of_Service_Department__c = approver_of_Service_DepartmentMap.get(rsMap.get(nObj.NewRepair__c).work_location_select__c);
                }
            }
        }
        if (newRepairMap.isEmpty() == false) {
            Map<Id, Repair__c> rsMap = new Map<Id, Repair__c>();
            for (Repair__c re : [SELECT ID
                                        , work_location_select__c
                                        , Delivered_Product__c
                                    FROM Repair__c
                                    WHERE Id =:newRepairMap.values()
                                       OR Id = :needRaMap.values()
            ]) {
                rsMap.put(re.Id, re);
            }
            for (Rental_Apply__c nObj : newRepairMap.keySet()) {
                if (rsMap.get(needRaMap.get(nObj.Id)).Delivered_Product__c != rsMap.get(nObj.NewRepair__c).Delivered_Product__c) {
                    nObj.NewRepair__c.addError('新修理必须和原修理是同一设备');
                }
            }
        }
 
    }
 
    // before insert, before update
    private void setOffice_Assistant() {
 
        Set<String> locSet = new Set<String>();
        List<Rental_Apply__c> nObjList = new List<Rental_Apply__c>();
        for (Rental_Apply__c nObj : newList) {
            Rental_Apply__c oObj = (null == this.oldMap) ? null : this.oldMap.get(nObj.Id);
            if (Trigger.isInsert
                || oObj.ToAgency__c != nObj.ToAgency__c
            ) {
                nObj.Office_Assistant1__c = null;
                nObj.Office_Assistant2__c = null;
                if (String.isNotBlank(nObj.ToAgency__c)) {
                    locSet.add(nObj.ToAgency__c);
                    nObjList.add(nObj);
                }
            }
        }
 
        if (locSet.size() > 0) {
            Map<String, OCM_Management_Province__c> ocpMap = new Map<String, OCM_Management_Province__c>();
            for (OCM_Management_Province__c ocp : [SELECT Id
                                                        , Name
                                                        , Agency_assistant1__c
                                                        , Agency_assistant2__c
                                                     FROM OCM_Management_Province__c
                                                    WHERE Name = :locSet]
            ) {
                ocpMap.put(ocp.Name, ocp);
            }
            for (Rental_Apply__c nObj : nObjList) {
                if (ocpMap.containsKey(nObj.ToAgency__c)) {
                    nObj.Office_Assistant1__c = ocpMap.get(nObj.ToAgency__c).Agency_assistant1__c;
                    nObj.Office_Assistant2__c = ocpMap.get(nObj.ToAgency__c).Agency_assistant2__c;
                }
            }
        }
    }
 
    private void cancelRa() {
        Set<Id> raIdSet = new Set<Id>();
        List<Rental_Apply__c> raList = new List<Rental_Apply__c>(); //20210823 ljh 
        for (Rental_Apply__c nObj : newList) {
            Rental_Apply__c oObj = oldMap.get(nObj.Id);
            System.debug(FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Qu_Xiao.ordinal()));
            System.debug(oObj.Status__c);
            System.debug(nObj.Status__c);
            if (oObj.Status__c != FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Qu_Xiao.ordinal())
                && nObj.Status__c == FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Qu_Xiao.ordinal())) {
                raIdSet.add(nObj.Id);
            }
            // 20210823 ljh SFDC-C448KZ add start
            if(oObj.Cancel_Reason__c == null && nObj.Cancel_Reason__c != null 
                && oObj.Loaner_cancel_reason__c == null && nObj.Loaner_cancel_reason__c != null 
                && oObj.Loaner_cancel_request__c == null && nObj.Loaner_cancel_request__c != null){
                raList.add(nObj);
            }
            // 20210823 ljh SFDC-C448KZ add end
        }
        //20210823 ljh SFDC-C448KZ add start 
        if(raList.size() >0 ){
            Map<String,String> cancleMap = new Map<String,String>();
            List<CancelPostponePlan__c> cppList  = new List<CancelPostponePlan__c>();
            SS_Batch_Column_Mapping__c mpdMapping = SS_Batch_Column_Mapping__c.getValues('Rental_Apply_OPD_Cancle');         
            Map<String,String> opdMap = new Map<String,String>();
            for (Integer i = 101; i <= FIELDMAX; i++) {
                String lpadI = ('00' + i).right(3);
                String fromColumn = 'From_Column_' + lpadI + '__c';
                String apiStr = String.valueOf(mpdMapping.get(fromColumn));
                if (String.isBlank(apiStr) == false) {
                    String ssColumn = 'SS_Column_' + lpadI + '__c';
                    String ssApiStr = String.valueOf(mpdMapping.get(ssColumn));
                    if(apiStr.split(';').size()>=2){
                        if(apiStr.split(';')[1] != null && (apiStr.split(';')[1] == '主动取消'||apiStr.split(';')[1] == '被动取消')){
                            cancleMap.put(apiStr.split(';')[0],ssApiStr);
                        }                   
                    }          
                }
            }
            for(Rental_Apply__c ra:raList){
                if(ra.OPDPlan__c != null && !ra.if_HaveOPD_Apply__c){
                    CancelPostponePlan__c cpp = new CancelPostponePlan__c();
                    Boolean  flag = true;
                    cpp.CancelOPDPlan__c = ra.OPDPlan__c;//opdList[0].id;
                    //deloitte-kaiyu 20231212 本地化去hardCode
                    cpp.RecordTypeId = System.label.CancelPostponePlanRecordTypeCancelType;
                    cpp.Status__c='取消成功';
                    if (ra.OPDType__c == '学会') {
                        flag = false;
                    }else{  
                        cpp.cancelReasonCombobox__c = cancleMap.get(ra.Loaner_cancel_reason__c);
                    }
                    cpp.if_HaveRental_Apply__c=true;//打标机是防止循环更新 opd计划
                    if (flag) {
                        cppList.add(cpp);
                    }
                }
            }
            if(cppList.size() > 0 ){
                insert cppList;
            }
        }
        //20210823 ljh SFDC-C448KZ add end
        if (raIdSet.isEmpty()) {
            return;
        }
        List<Rental_Apply_Equipment_Set__c> raess = [Select id, Rental_Apply__r.Cancel_Reason__c,
                Rental_Apply__r.Loaner_cancel_request__c,
                Rental_Apply__r.Loaner_cancel_reason__c
                FROM Rental_Apply_Equipment_Set__c
                WHERE Rental_Apply__c = :raIdSet
                  AND Cancel_Select__c = false // OLY_OCM-609 已经取消的备品借出一览不再修改取消理由等字段
                ];
        if (raess.size() > 0) {
            for (Rental_Apply_Equipment_Set__c raes : raess) {
                raes.Cancel_Select__c = true;
                raes.Cancel_Reason__c = raes.Rental_Apply__r.Cancel_Reason__c;
                raes.Loaner_cancel_Remarks__c = raes.Rental_Apply__r.Loaner_cancel_request__c;
                //20210706 SFDC-C448KZ you
                raes.Loaner_cancel_reason__c = raes.Rental_Apply__r.Loaner_cancel_reason__c;
                raes.Cancel_Mem__c = UserInfo.getUserId();
                raes.Cancel_Date__c = Date.today();
                raes.Cancel_Time__c = MainFixtureSelectController.getCurrentTime();
            }
            update raess;
        }
    }
 
    // From RentalApplyApprovalProcess.trigger TODO test
    // beforeUpdate
    private void approvalCheck() {
        List<Id> raIdList = new List<Id> ();
        for (Rental_Apply__c nObj : newList) {
            Rental_Apply__c oObj = null;
            if (Trigger.isUpdate) {
                oObj = oldMap.get(nObj.Id);
            }
            if (oObj.Status__c == '申请中' && nObj.Status__c == '已批准'
                    && nObj.Rental_Apply_Equipment_Set_Cnt__c != 0) {
//bp2               // 自动引当
                // 借出时间check
                String rs1 = RentalApplyWebService.approvalCheck(nObj.Id);
                if (rs1 != '1') {
                    nObj.addError(rs1);
                }
//bp2               else {
//                  // 正常终了
//                  raesNew.Status__c = '引当完了';
//              }
            }
            if (nObj.ExtensionStatus__c == '申请中' && oObj != null && nObj.Extension_NewStep_AppTime__c != null && oObj.Extension_NewStep_AppTime__c != nObj.Extension_NewStep_AppTime__c
            ) {
                raIdList.add(nObj.Id);
            }
        }
        if (raIdList.size() > 0) {
            RentalApplyTriggerHandler.doUnlockByFuture(raIdList);
        }
    }
    
    public static void decryptInsert(List<Rental_Apply__c> newList){
        System.debug('enter RentalApply decryptInsert');
        // 借用机会可视化-Batch执行不走这个逻辑 添加|| !System.isBatch()  2023-12-06 zyh start
        // if(!system.isFuture()){
        if(!system.isFuture() && !System.isBatch()){
        // 借用机会可视化-Batch执行不走这个逻辑 || !System.isBatch() 2023-12-06 zyh end
            List<Rental_Apply__c> fendanList = new List<Rental_Apply__c>();
            for(Rental_Apply__c ra : newList){
                System.debug('zyhtest=====gebaofendan'+ra.Old_Rental_Apply__c);
                if(ra.Old_Rental_Apply__c != null){
                    System.debug('zyhtest=====gebaofendan'+ra.Old_Rental_Apply__c);
                    fendanList.add(ra);
                }
            }
            
            if(fendanList.size() == 0){
                system.debug('no need split');
                return;
            }
            //zhj MEBG新方案改造 2022-12-01 start
           //decryptInsertFuture(JSON.serialize(fendanList));
           Map<String,PIHelper.PIIntegration> staticResource = new Map<String,PIHelper.PIIntegration>();
           staticResource.put('Rental_Apply__c',PIHelper.getPIIntegrationInfo('Rental_Apply__c'));
           Map<String, Map<String, PI_Field_Policy_Detail__c>> mmsp = new Map<String, Map<String,PI_Field_Policy_Detail__c>>();
           for (String key : staticResource.keySet()) {
               mmsp.put(key, new Map<String,PI_Field_Policy_Detail__c>());
               for (PI_Field_Policy_Detail__c detail : staticResource.get(key).PIDetails) {
                   mmsp.get(key).put(detail.SF_Field_API_Name__c, detail);
               }
           }
           System.debug('mmsp = ' + mmsp);
 
 
           List<AWSServiceTool2V2.EncryptPushRequestBody> EncryptPushList = new List<AWSServiceTool2V2.EncryptPushRequestBody>();
            for(Rental_Apply__c ac : fendanList){
                Rental_Apply__c oldAc = [select id,AWS_Data_Id__c from Rental_Apply__c where id=:ac.Old_Rental_Apply__c];
                System.debug('oldAc = ' + oldAc);
                AWSServiceTool2V2.EncryptPushRequestBody EncryptPush = new AWSServiceTool2V2.EncryptPushRequestBody();
                EncryptPush.dataId = ac.AWS_Data_Id__c != null ?ac.AWS_Data_Id__c:'';
                EncryptPush.sfRecordId = ac.Id;
                EncryptPush.fieldsMapping = new Map<String, List<AWSServiceTool2V2.EncryptPushRes>>();
                List<AWSServiceTool2V2.EncryptPushRes> resList = new List<AWSServiceTool2V2.EncryptPushRes>();
                AWSServiceTool2V2.EncryptPushRes res= new AWSServiceTool2V2.EncryptPushRes();
                res.isQueryDb = true;
                res.value = '';
                res.table = staticResource.get('Rental_Apply__c').awsTableName;
                //res.dataId = ac.Old_Rental_Apply__r.AWS_Data_Id__c;
                res.dataId = oldAc.AWS_Data_Id__c;
                res.field = mmsp.get('Rental_Apply__c').get('direct_shippment_address__c').AWS_Field_API__c;
                resList.add(res);
 
                List<AWSServiceTool2V2.EncryptPushRes> resList2 = new List<AWSServiceTool2V2.EncryptPushRes>();
                AWSServiceTool2V2.EncryptPushRes res2= new AWSServiceTool2V2.EncryptPushRes();
                res2.isQueryDb = true;
                res2.value = '';
                res2.table = staticResource.get('Rental_Apply__c').awsTableName;
                //res2.dataId = ac.Old_Rental_Apply__r.AWS_Data_Id__c;
                res2.dataId = oldAc.AWS_Data_Id__c;
                res2.field = mmsp.get('Rental_Apply__c').get('Phone_number__c').AWS_Field_API__c;
                resList2.add(res2);
 
                EncryptPush.fieldsMapping.put(mmsp.get('Rental_Apply__c').get('direct_shippment_address__c').AWS_Field_API__c, resList);
                EncryptPush.fieldsMapping.put(mmsp.get('Rental_Apply__c').get('Phone_number__c').AWS_Field_API__c, resList2);
                EncryptPushList.add(EncryptPush);
            }
            System.debug('EncryptPushListdataId = ' + JSON.serialize(EncryptPushList[0].dataId));
            System.debug('EncryptPushListsfRecordId = ' + JSON.serialize(EncryptPushList[0].sfRecordId));
            System.debug('EncryptPushListfieldsMapping = ' + JSON.serialize(EncryptPushList[0].fieldsMapping));
            System.debug('EncryptPushList = ' + JSON.serialize(EncryptPushList));
            AwsServiceTool2V2.EncryptPushFutureV2(Json.serialize(EncryptPushList),Json.serialize(fendanList), 'Rental_Apply__c');
           //zhj MEBG新方案改造 2022-12-01 end
 
           //decryptInsertFuture(JSON.serialize(fendanList)); 
        }
    }
    
    @future(callout=true)
    public static void decryptInsertFuture(string json_list){
        decryptInsertCore(json_list);
    }
    
    // List<Rental_Apply__c> temps = [select id,AWS_Data_Id__c,name, direct_shippment_address__c,  Direct_Shippment_Address_Encrypt__c, Phone_number__c,  Phone_Number_Encrypt__c,CreatedDate   from Rental_Apply__c where AWS_Data_Id__c != null order by CreatedDate desc limit 2];
    public static void decryptInsertCore(string json_list){
        system.debug('enter decryptInsertCore');
        //调用滨璜接口更新
        PIHelper.PIIntegration staticResource =  PIHelper.getPIIntegrationInfo('Rental_Apply__c');
        system.debug('staticResource.token='+staticResource.token);
        if(String.isBlank(staticResource.token)){
            System.debug('获取aws token 失败');
            return;
        }
        List<Rental_Apply__c> newList = (List<Rental_Apply__c>)Json.deserialize(json_list, List<Rental_Apply__c>.class);
        Map<Id,Rental_Apply__c> newMap = new Map<Id,Rental_Apply__c>(newList);
        List<Map<string,object>> lmso = new List<Map<string,object>>();
        for(Rental_Apply__c ra : newList){
            Map<string,object> mso = new Map<string,object>();
            
            /*if(!string.isBlank(ra.AWS_Data_Id__c)){
                continue;
            }*/
            for(PI_Field_Policy_Detail__c detail : staticResource.PIDetails){
                if(ra.isSet(detail.SF_Field_API_Name__c)){
                    mso.put(detail.AWS_Field_API__c,ra.get(detail.SF_Field_API_Name__c));
                    mso.put(detail.AWS_Encrypted_Field_API__c,ra.get(detail.SF_Field_Encrypted_API__c));
                }
            }
            mso.put('sfRecordId',ra.Id);
            lmso.add(mso);
        }
        
        if(lmso.size()==0){
            system.debug('lmso.size()='+lmso.size());
            return;
        }
        string payload = Json.serialize(lmso);
        system.debug('payload='+payload);
        String awsApi = staticResource.viewUnifiedContactUrl;
        NFMUtil.response response = NFMUtil.sendToPiAWS(payload, awsApi,staticResource.token);
        system.debug(response);
        Map<string,object> res_obj = (Map<string,object>)Json.deserializeUntyped(response.responseBody);
        if(res_obj == null || !res_obj.containsKey('object') ){
            System.debug('res_obj == null || !res_obj.containsKey(\'object\')');
            return;
        }
        
        List<object> objList = (List<object>)res_obj.get('object');
        if(objList == null){
            System.debug('objList == null');
            return;
        }
        
        List<Rental_Apply__c> updateList = new List<Rental_Apply__c>();
        for(object obj : objList){
            Map<string,object> obj_map = (Map<string,object>)obj;
            string sfRecordId = null;
            string dataId = null;
            if(obj_map.containsKey('sfRecordId')){
                sfRecordId = string.valueOf(obj_map.get('sfRecordId'));
            }else{
                system.debug('obj_map.containsKey(\'sfRecordId\')='+obj_map.containsKey('sfRecordId'));
                continue;
            }
            
            if(obj_map.containsKey('dataId')){
                dataId = string.valueOf(obj_map.get('dataId'));
            }else{
                system.debug('obj_map.containsKey(\'dataId\')='+obj_map.containsKey('dataId'));
                continue;
            }
            
            
            if(newMap.containsKey(sfRecordId)){
                Rental_Apply__c ra = newMap.get(sfRecordId);
                ra.AWS_Data_Id__c = dataId;
                updateList.add(ra);
            }else{
                system.debug('newMap.containsKey('+sfRecordId+')='+newMap.containsKey(sfRecordId));
                continue;
            }
        }
        
        system.debug('updateList.size='+updateList.size());
        if(updateList.size()>0){
            update updateList;
        }
        
    }
 
    @future
    public static void doUnlockByFuture(List<ID> idList) {
        // Unlock操作
        List<Rental_Apply__c> raList = [SELECT Id FROM Rental_Apply__c WHERE ID IN: idList];
        Approval.UnLockResult[] results = Approval.unlock(raList, false);
        System.debug('非同期処理によるロック解除操作の対象件数 = ' + results.size()+' 日志:'+results);
    }
 
    //before 数式の値がnullになる可能性がありますのでここでも一回チェックします
    private void formulaToTextCheck() {
        List<Rental_Apply__c> ras = new List<Rental_Apply__c>();
        List<Id> raIds = new List<Id>();
        for (Rental_Apply__c nObj : newList) {
            if (nObj.RA_Status_Text__c != nObj.RA_Status__c
                || nObj.Status_Text__c != nObj.Status__c
                || nObj.Rental_Status__c != nObj.RA_Status__c // 20231027 ljh add
                || nObj.NotWatch_RA_Status__c != nObj.NotWatch_RA_Status_F__c) {
                Rental_Apply__c ra = new Rental_Apply__c(Id = nObj.Id);
                ra.RA_Status_Text__c = nObj.RA_Status__c;
                //20230911  sx add 新加字段 start
                ra.Rental_Status__c = nObj.RA_Status__c;
                 //20230911  sx add 新加字段 end
                ra.Status_Text__c = nObj.Status__c;
                ra.NotWatch_RA_Status__c = nObj.NotWatch_RA_Status_F__c;
                ras.add(ra);
                // raIds.add(ra.Id);
            }
        }
        if (!ras.isEmpty()) {
            update ras;
        }
 
        // if (!raIds.isEmpty()) {
        //     RentalApplyTriggerHandler.someFutureMethod(raIds);
        // }
    }
 
    // @future
    // public static void someFutureMethod(List<Id> recordIds) {
    //     List<Rental_Apply__c> ras = [Select Id from Rental_Apply__c Where Id IN :recordIds];
    //     update ras;
    //     // process account records to do awesome stuff
    // }
 
    // afterUpdate 医院确认相关的字段更新的时候要更新一览
    private void reReceivedConfirmStatus() {
        Set<Id> raIdSet = new Set<Id>();
        for (Rental_Apply__c nObj : newList) {
            Rental_Apply__c oObj = oldMap.get(nObj.Id);
 
            if (nObj.AssetManageConfirm__c != oObj.AssetManageConfirm__c
                    || nObj.HP_received_sign_NG__c != oObj.HP_received_sign_NG__c
                    || nObj.HP_received_sign_day__c != oObj.HP_received_sign_day__c) {
                raIdSet.add(nObj.Id);
            }
        }
         System.debug(raIdSet);
        if (raIdSet.isEmpty()) {
            return;
        }
 
        List<Rental_Apply_Equipment_Set__c> raess = [Select Id
                From Rental_Apply_Equipment_Set__c
                Where Rental_Apply__c = :raIdSet];
        System.debug(raess.size());
        update raess;
    }
 
    //
    private void reApprovalStatus() {
        Map<Id, Rental_Apply__c> raIdMap = new Map<Id, Rental_Apply__c>();
        for (Rental_Apply__c nObj : newList) {
            Rental_Apply__c oObj = oldMap.get(nObj.Id);
            if (oObj.Add_Approval_Status__c != nObj.Add_Approval_Status__c
                && nObj.Request_approval_day__c != null
                && oObj.Request_approval_day__c == nObj.Request_approval_day__c
                && nObj.Add_Approval_Status__c != '填写完毕') {
                raIdMap.put(nObj.Id, nObj);
            }
        }
 
        if (raIdMap.isEmpty()) {
            return;
        }
 
        List<Rental_Apply_Equipment_Set_Detail__c> raesds = [Select Id, Rental_Apply__c
                From Rental_Apply_Equipment_Set_Detail__c
                Where Rental_Apply__c = :raIdMap.keySet()
                AND Select_Time__c = null
                AND ApplyPersonAppended_F__c = true
                AND Add_Request_approval_time__c = null];
        for (Rental_Apply_Equipment_Set_Detail__c raesd : raesds) {
            Rental_Apply__c ra = raIdMap.get(raesd.Rental_Apply__c);
            if (ra.Add_Approval_Status__c == '申请中') {
                raesd.Add_Request_demo_time__c = ra.Add_Request_demo_time__c;
            } else if (ra.Add_Approval_Status__c == '已批准') {
                raesd.Add_Request_approval_time__c = ra.Add_Request_approval_time__c;
            } else if (ra.Add_Approval_Status__c == '草案中') {
                raesd.Add_Request_demo_time__c = null;
                raesd.Add_Request_approval_time__c = null;
            }
        }
        update raesds;
    }
 
    // 申请中的申请书取消时,取消审批流
    private void removedProcessRequest() {
        Set<String> cancelIdSet = new Set<String>();
        for (Rental_Apply__c nObj : newList) {
            Rental_Apply__c oObj = oldMap.get(nObj.Id);
            //20231224 sx add 备品智能化添加状态申请中(OPD未通过) 优化
            if ((oObj.Status__c == '申请中' || oObj.Status__c == '申请中(OPD未通过)' )
                    && nObj.Status__c == '取消'
                    //SWAG-BUF6J5  20201117 you打标记为了能正确更新备品出借申请得状态  start
                    && nObj.if_HaveOPDPlanCan__c==false
                    //SWAG-BUF6J5  20201117 you打标记为了能正确更新备品出借申请得状态 end
                    ) {
                cancelIdSet.add(nObj.Id);
            }
        }
 
        if (cancelIdSet.size() > 0) {
            List<Approval.ProcessWorkitemRequest> requests = new List<Approval.ProcessWorkitemRequest> ();
            Map<ID,ProcessInstance> piMap = New Map<ID,ProcessInstance>([Select Id from ProcessInstance where TargetObjectId IN :cancelIdSet]);
            for(ProcessInstanceWorkItem wi : [Select Id from ProcessInstanceWorkItem where ProcessInstanceId IN :piMap.keySet()]){
                Approval.ProcessWorkitemRequest req2 = new Approval.ProcessWorkitemRequest();
                req2.setAction('Removed');
                req2.setWorkitemId(wi.Id);
                requests.add(req2);
            }
            if (requests.size() > 0) {
                Approval.ProcessResult[] processResults = null;
                processResults = Approval.process(requests, true);
            }
        }
    }
 
    // 字符串转Hash
    public Static String getHash(String digest, String message) {
        if (String.isBlank(message)) {
            message = '';
        }
        return EncodingUtil.convertToHex(Crypto.generateDigest(digest, Blob.valueOf(message)));
    }
 
    /**
     * [getCan_Extend_RequestList 验证申请单是否可以延期]
     * @param  raL [需要验证的数据]
     * @return     [description]
     *
     * 延期分两种:
     *     单独延期
     *     批量延期
     */
    public static List<Rental_Apply_Equipment_Set__c> getCan_Extend_RequestList(List<Rental_Apply__c> raL) {
        List<Rental_Apply_Equipment_Set__c> raesList = new List<Rental_Apply_Equipment_Set__c>();
        if(raL != null && raL.size() > 0){
            List<String> racIdList = new List<String>();
            for(Rental_Apply__c ra : raL){
                if (ra.demo_purpose2__c == '学会展会'
                    || ra.demo_purpose2__c == '新产品评价'
                    || ra.demo_purpose2__c == '已购待货'
                    || ra.demo_purpose2__c == '其他'
                ) {
                    throw new ControllerUtil.myException('使用目的' + ra.demo_purpose2__c + '的申请不能做延期申请');
                }
                if (ra.demo_purpose1__c == '维修代用') {
                    if (ra.ExtensionApprovalTime_Final__c != null) {
                        throw new ControllerUtil.myException('维修代用的申请不能提交两次以上延期申请');
                    }
                    if (ra.demo_purpose2__c == '故障排查'){
                        if(ra.RC_Ordered_Date__c == null){
                            throw new ControllerUtil.myException('[4.修理品RC受理日]为空,不可延期');
                        }
                        if(ra.Bollow_Date_Add_10_WD__c == null) {
                            throw new ControllerUtil.myException('此单不满足延期条件');
                        }
                        if(ra.RC_Ordered_Date__c > ra.Bollow_Date_Add_10_WD__c) {
                            throw new ControllerUtil.myException('[4.修理品RC受理日]超过出库后10个工作日,不可延期');
                        }
                    }
                    if (String.isBlank(ra.NewRepair__c)) {
                        throw new ControllerUtil.myException('提交维修代用的延期申请,必须填写新修理单号');
                    } else if (
                        ra.ExtensionApprovalTime_Initial__c != null// 第二次延期审批
                        && (
                            ra.NewRepair__r.Agreed_Date__c != null // 7.用户同意日≠空
                            && ra.NewRepair__r.Status__c != '0.取消' // 修理状态≠取消、删除
                            && ra.NewRepair__r.Status__c != '0.删除' // 修理状态≠取消、删除
                            && ra.NewRepair__r.ReRepairObject_F__c == true // 再受理对象品参考=真
                            && ra.NewRepair__r.Repair_Shipped_Date__c == null) == false// 修理品返送日=空
                    ) {
                        throw new ControllerUtil.myException('此单不满足第二次延期条件');
                    }
 
                }
                else if (ra.demo_purpose1__c == '产品试用') {
                    
                    //可能会出现这样的场景:有一个主单A,连个从单 A1 A2,第一次延期A A1,第二次延期入口为从单A2,那么就不需要走else判断
                    //或反过来,第一次延期一个从单,第二次准备延期主单A和A2,那么也不需要走else判断
                    if(raL.size() > 1){
                        
                    }else{
                        //批量延期时,跳过这个验证
                        if (ra.Loaner_received_ng_num__c > 0) {
                            throw new ControllerUtil.myException('未完成到货确认的操作不能做延期申请');
                            //throw new ControllerUtil.myException('存在没有做现场收到确认结果的一览不能做延期申请1111');
                        }
                        else if (ra.ExtensionApprovalTime_Initial__c != null ) {
                            throw new ControllerUtil.myException('产品试用的申请不能提交第二次延期申请');
                        }
                    }
                }else if (ra.demo_purpose1__c == '协议借用' && ra.AgreementBorrowingExtensionDate__c == null) {
                    throw new ControllerUtil.myException('协议借用的延期申请,必须填写协议借用延期日期');
                }
 
                if (ra.demo_purpose2__c == '索赔QIS'
                        && ra.next_action__c != '无偿维修'
                        && ra.next_action__c != '有偿维修'
                        && ra.next_action__c != '有偿维修+无偿维修'
                ) {
                    throw new ControllerUtil.myException('此单不满足延期条件');
                }
 
                //收集 申请单满足条件的id
                racIdList.add(ra.Id);
            }
            if(racIdList != null && racIdList.size() > 0){
                Boolean haveNotOk = false;
                for (Rental_Apply_Equipment_Set__c raes : [SELECT Id
                                                                , Rental_Apply__c
                                                                , Rental_Apply__r.Repair__r.Agreed_Date__c
                                                                , Rental_Apply__r.Repair__r.Repair_Estimated_date_formula__c
                                                                , Rental_Apply__r.NewRepair__c
                                                                , Rental_Apply__r.NewRepair__r.Agreed_Date__c
                                                                , Rental_Apply__r.NewRepair__r.Status__c
                                                                , Rental_Apply__r.NewRepair__r.ReRepairObject_F__c
                                                                , Rental_Apply__r.NewRepair__r.Repair_Shipped_Date__c
                                                                , Rental_Apply__r.QISRepair__r.Repair_Shipped_Date__c
                                                                , Rental_Apply__r.RC_return_to_office__c
                                                                , Rental_Apply__r.AgreementBorrowingExtensionDate__c
                                                                , Rental_Apply__r.ExtensionApprovalTime_Initial__c
                                                                , Rental_Apply__r.ExtensionApplicationTime_Final__c
                                                                , Rental_Apply__r.RcUnexpectExpiryDelay__c
                                                                , Final_reply_day__c
                                                                , Asset_return_time__c
                                                                , Bollow_Date__c
                                                                , demo_purpose2__c
                                                                , demo_purpose1__c
                                                                , Request_demo_time__c
                                                                , Loaner_received_time__c
                                                                , Received_Confirm__c
                                                                , Loaner_received_day2__c
                                                                , RcUnexpectExpiryDelay__c
                                                             FROM Rental_Apply_Equipment_Set__c
                                                            WHERE Rental_Apply__c in :racIdList 
                                                              AND Cancel_Reason__c = null // 取消重新分配的话需要做为NG重新分配的情况所以不能用Cancel_Select__c
                                                             ]) {
                    if (raes.demo_purpose1__c == '产品试用') {
                        if (raes.Received_Confirm__c != 'OK' && raes.Received_Confirm__c != '默认签收-OK' && raes.Received_Confirm__c != null) {
                            haveNotOk = true;
                        }
                        if ((raes.Received_Confirm__c == 'OK' || raes.Received_Confirm__c == '默认签收-OK')
                            && raes.Asset_return_time__c != null
                            && raL.size() == 1
                        ) {
                            throw new ControllerUtil.myException('此单不满足延期条件');
                        }
                        if (raes.Received_Confirm__c == 'NG'
                            && raes.Asset_return_time__c == null
                        ) {
                            throw new ControllerUtil.myException('存在NG未回寄的一览不能做延期申请');
                        }
                        if (raes.Received_Confirm__c == 'NG' && raes.Asset_return_time__c != null && raes.Loaner_received_day2__c != null
                        ) {
                            Date d2 = Date.valueOf(raes.Asset_return_time__c);
                            if (raes.Loaner_received_day2__c.daysBetween(d2) > 7) {
                                throw new ControllerUtil.myException('此单不满足延期条件');
                            }
                        }
                    }
                    System.debug('raes==============' + raes);
                    System.debug('raes1==============' + checkCan_Extend_Request(raes, false));
                    if (checkCan_Extend_Request(raes, false)) {
                        raesList.add(raes);
                    }
                }
                System.debug(raesList+'---------------提示5---'+haveNotOk+'------------'+raL[0].demo_purpose1__c);
                if (raesList.size() == 0 || (haveNotOk == false && raL[0].demo_purpose1__c == '产品试用')) {
                    throw new ControllerUtil.myException('此单不满足延期条件');
                }
            }
        }
        return raesList;
    }
 
    // check一览是否可以做延期申请
    public static Boolean checkCan_Extend_Request(Rental_Apply_Equipment_Set__c raes, Boolean flg) {
        if (raes.demo_purpose1__c == '维修代用')  {
            // 第一次延期审批
            if (raes.Rental_Apply__r.ExtensionApprovalTime_Initial__c == null) {
                Date agreed_Date = raes.Rental_Apply__r.Repair__r.Agreed_Date__c;
                Date repair_Estimated_date_formula = raes.Rental_Apply__r.Repair__r.Repair_Estimated_date_formula__c;
                Boolean canExtend = (false == flg || raes.Rental_Apply__r.NewRepair__c != null) // 新修理单号≠空
                                    && (raes.Rental_Apply__r.RC_return_to_office__c != null || raes.Rental_Apply__r.QISRepair__r.Repair_Shipped_Date__c != null) // 旧修理.有修理品返送日≠空
                                    && raes.Final_reply_day__c >= td // 最新预定归还日 ≥ 今天
                                    && raes.Asset_return_time__c == null // 回寄时间=空
                                    && raes.Bollow_Date__c != null; // 备品中心出库≠空
                if (canExtend) {
                    if (raes.demo_purpose2__c == '一般用户') {
                        return (agreed_Date != null && agreed_Date <= raes.Request_demo_time__c) // 7.用户同意日≠空 &&7.用户同意日≤申请时间
                                || (agreed_Date != null
                                    && repair_Estimated_date_formula != null
                                    && raes.Request_demo_time__c < agreed_Date
                                    && repair_Estimated_date_formula.daysBetween(agreed_Date) <= 21);
                    }
                    else if (raes.demo_purpose2__c == '故障排查') {
                        return agreed_Date != null // 同意日!=空
                               && repair_Estimated_date_formula != null //报价日!=空
                               && repair_Estimated_date_formula.daysBetween(agreed_Date) <= 21; // 同意日-报价日<=21
                    }
                }
                return canExtend;
            }
            else {  // 第二次延期审批
                return raes.Bollow_Date__c != null // 备品中心出库≠空
                    && raes.Asset_return_time__c == null // 回寄时间=空
                    && raes.Final_reply_day__c >= td // 最新预定归还日 ≥ 今天
                    && (flg == false
                        || (raes.Rental_Apply__r.NewRepair__c != null // 新修理单号≠空
                            && raes.Rental_Apply__r.NewRepair__r.Agreed_Date__c != null // 7.用户同意日≠空
                            && raes.Rental_Apply__r.NewRepair__r.Status__c != '0.取消' // 修理状态≠取消、删除
                            && raes.Rental_Apply__r.NewRepair__r.Status__c != '0.删除' // 修理状态≠取消、删除
                            && raes.Rental_Apply__r.NewRepair__r.ReRepairObject_F__c == true // 再受理对象品参考=真
                            && raes.Rental_Apply__r.NewRepair__r.Repair_Shipped_Date__c == null // 修理品返送日=空
                        )
                    );
            }
        }
        else if (raes.demo_purpose1__c == '产品试用'
            && raes.Bollow_Date__c != null
            && raes.Asset_return_time__c == null
        ) {
            Date bollow_Date14 = raes.Bollow_Date__c.addDays(14);
            Date d1 = bollow_Date14  > raes.Final_reply_day__c ? raes.Final_reply_day__c : bollow_Date14;
            Date d2 = Date.valueOf(raes.Asset_return_time__c);
            return raes.Bollow_Date__c != null // 备品中心出库≠空
                && raes.Asset_return_time__c == null // 回寄时间=空
                //&& raes.Loaner_received_time__c != null // 申请者收到确认未完了数=0
                && d1 >= td
                && raes.Received_Confirm__c != 'NG';
        }
        else if (raes.demo_purpose1__c == '协议借用') {
            return raes.Bollow_Date__c != null // 备品中心出库≠空
                && raes.Asset_return_time__c == null // 回寄时间=空
                && raes.Final_reply_day__c >= td
                && (flg == false || raes.Rental_Apply__r.AgreementBorrowingExtensionDate__c != null)
            ;
        }
        return false;
    }
 
    //update      wangweipeng                             2021/11/25                   start
    //获取 自定义元数据 的数据
    public Map<String,String> customPostponeWorkLocation(){
        Map<String,String> customPostponeWorkLocationMap = new Map<String,String>();
        List<RentalApply_Postpone__mdt> usrList = [select id,MasterLabel,Approver__c from RentalApply_Postpone__mdt];
        if(usrList != null && usrList.size() > 0){
            for(RentalApply_Postpone__mdt rpm : usrList){
                if(String.isNotBlank(rpm.MasterLabel) && String.isNotBlank(rpm.Approver__c)){
                    customPostponeWorkLocationMap.put(rpm.MasterLabel,rpm.Approver__c);
                }
            }
        }
        return customPostponeWorkLocationMap;
    }
 
    /**add         wangweipeng                       2021/12/02                        start
     * [synchRentalApplyData 同步延期字段信息]
     * @param ra [description]
     * 批量延期时:
     *     1:主单和从单都延期了,那么需要主单和从单的延期信息同步
     *     2:如果延期了从单,但是走的是主单的审批流,那么在审批完成以后,您需要把主单的延期信息清空
     */
    public void synchRentalApplyData() {
        //获取主单延期信息有变化的id
        List<String> raIDList = new List<String>();
        for(Rental_Apply__c ra : newList){
            //是批量审批,并且延期状态发生变化,那么就需要同步延期信息
            if(ra.Extension_Type__c == '批量延期' && ra.ExtensionStatus__c != oldMap.get(ra.Id).ExtensionStatus__c){
                //只判断为主单时,并且 批量延期申请单id 字段不为空,那么就需要把延期数据同步到从单上
                if(String.isBlank(ra.Root_Rental_Apply__c) && String.isNotBlank(ra.Extension_Much_ID__c)){
                    //延期状态为 已批准、驳回或为空时,才同步
                    if('已批准'.equals(ra.ExtensionStatus__c) || '驳回'.equals(ra.ExtensionStatus__c) || String.isBlank(ra.ExtensionStatus__c)){
                        //获取此次批量延期的所有从单单子
                        for(String emic : ra.Extension_Much_ID__c.split(',')){
                            if(String.isNotBlank(emic)){
                                raIDList.add(emic);
                            }
                        }
                    }
                }
            }
        }
        if(raIDList != null && raIDList.size() > 0){
            List<Rental_Apply__c> racExtensionData = [SELECT ID
                                                            ,NAME
                                                            ,Is_Delete_Extension__c
                                                            ,ExtensionStatus__c
                                                            ,Extension_Type__c
                                                            ,Extension_Parent_Entrance__c 
                                                            ,ExtensionApplicationTime_Initial__c
                                                            ,ExtensionApprovalTime_Initial__c
                                                            ,ExtensionSuccessTimes__c
                                                            ,RcUnexpectExpiryDelay__c
                                                            ,ExtensionContent__c
                                                            ,RcUnexpectExpiryDelay_Mail__c
                                                            ,ExtensionDays__c
                                                        FROM Rental_Apply__c 
                                                        WHERE ID IN :raIDList 
                                                            AND Extension_Type__c = '批量延期'
                                                            AND ExtensionApplicationTime_Initial__c != NULL];
            if(racExtensionData != null && racExtensionData.size() > 0){
                List<Rental_Apply__c> updateRACE = new List<Rental_Apply__c>();
                for(Rental_Apply__c ra : newList){
                    //是批量审批,并且延期状态发生变化,那么就需要同步延期信息
                    if('批量延期'.equals(ra.Extension_Type__c) && ra.ExtensionStatus__c != oldMap.get(ra.Id).ExtensionStatus__c){
                        //只判断为主单时,并且 批量延期申请单id 字段不为空,那么就需要把延期数据同步到从单上
                        if(String.isBlank(ra.Root_Rental_Apply__c) 
                            && String.isNotBlank(ra.Extension_Much_ID__c)
                            && ('已批准'.equals(ra.ExtensionStatus__c) || '驳回'.equals(ra.ExtensionStatus__c) || String.isBlank(ra.ExtensionStatus__c)))
                        {
                            if('已批准'.equals(ra.ExtensionStatus__c)){
                                //updateRACE = setUpdateRACE(ra.Extension_Much_ID__c,ra,racExtensionData,'');
                                //存放当前主单数据,用于情况延期信息
                                Rental_Apply__c racc = new Rental_Apply__c();
                                racc.id = ra.id;
                                //批量延期申请单  赋值  已批量延期申请单
                                if(ra.Extension_List_RentalApply__c != null){
                                    if(ra.History_Extension_Much_ID__c != null){
                                        racc.History_Extension_List_RentalApply__c += ra.Extension_List_RentalApply__c;
                                    }else{
                                        racc.History_Extension_List_RentalApply__c = ra.Extension_List_RentalApply__c;
                                    }
                                }
                                //批量延期申请单id 赋值  已批量延期申请单id
                                if(ra.Extension_Much_ID__c != null){
                                    if(ra.History_Extension_Much_ID__c != null){
                                        racc.History_Extension_Much_ID__c += ',' +ra.Extension_Much_ID__c;
                                    }else{
                                        racc.History_Extension_Much_ID__c = ra.Extension_Much_ID__c;
                                    }
                                }
                                //如果批量延期的时候,主单没有延期,从单延期了,那么也是走主单的审批流程,但是审批完成以后,
                                //需要把主单的延期信息字段置空,不能影响主单他自己的延期
                                if(ra.Is_Delete_Extension__c){
                                    racc.ExtensionApprovalTime_Initial__c = null;//延期批准时间(最初)
                                    racc.ExtensionSuccessTimes__c = null;//延期成功次数
                                    racc.RcUnexpectExpiryDelay__c = null;//RC未定到期延时
                                    racc.RcUnexpectExpiryDelay_Mail__c = null;//RC未定到期延时(邮件用)
                                    racc.ExtensionContent__c = null;//延期内容
                                    racc.ExtensionStatus__c = null;//延期状态
                                    racc.ExtensionApplicationTime_Initial__c = null;//延期申请时间(最初)
                                    racc.Is_Delete_Extension__c = false;
                                    racc.Extension_Type__c = '';
                                    racc.Extension_Much_ID__c = null;//批量延期申请单id
                                    racc.Extension_NewStep_AppTime__c = null;
                                    racc.ExtensionDays__c = null;//延期天数
                                    //racc.Extension_List_RentalApply__c = null;//批量延期申请单
                                }
                                updateRACE.add(racc);
                            }else if('驳回'.equals(ra.ExtensionStatus__c)){
                                //updateRACE = setUpdateRACE(ra.Extension_Much_ID__c,ra,racExtensionData,'1');
                                Rental_Apply__c racc = new Rental_Apply__c();
                                racc.id = ra.id;
                                racc.Is_Delete_Extension__c = false;
                                racc.Extension_Type__c = '';
                                racc.Extension_Much_ID__c = null;//批量延期申请单id
                                racc.Extension_List_RentalApply__c = null;//批量延期申请单
                                updateRACE.add(racc);
                            }else if(String.isBlank(ra.ExtensionStatus__c)){
                                //updateRACE = setUpdateRACE(ra.Extension_Much_ID__c,ra,racExtensionData,'1');
                                Rental_Apply__c racc = new Rental_Apply__c();
                                racc.id = ra.id;
                                racc.Is_Delete_Extension__c = false;
                                racc.Extension_Type__c = '';
                                racc.Extension_Much_ID__c = null;
                                racc.Extension_List_RentalApply__c = null;
                                updateRACE.add(racc);
                            }
                        }
                    }
                }
                if(updateRACE != null && updateRACE.size() > 0){
                    update updateRACE;
                }
            }
        }
    }
 
    /**
     * [synchRentalApplyData2 批量延期时同步从单]
     *
     * 批量延期时,需要主单和从单的延期信息同步
     */
    public void synchRentalApplyData2() {
        //获取主单延期信息有变化的id
        List<String> raIDList = new List<String>();
        for(Rental_Apply__c ra : newList){
            //是批量审批,并且延期状态发生变化,那么就需要同步延期信息
            if(ra.Extension_Type__c == '批量延期' && ra.ExtensionStatus__c != oldMap.get(ra.Id).ExtensionStatus__c){
                //只判断为主单时,并且 批量延期申请单id 字段不为空,那么就需要把延期数据同步到从单上
                if(String.isBlank(ra.Root_Rental_Apply__c) && String.isNotBlank(ra.Extension_Much_ID__c)){
                    //延期状态为 已批准、驳回或为空时,才同步
                    if('已批准'.equals(ra.ExtensionStatus__c) || '驳回'.equals(ra.ExtensionStatus__c) || String.isBlank(ra.ExtensionStatus__c)){
                        //获取此次批量延期的所有从单单子
                        for(String emic : ra.Extension_Much_ID__c.split(',')){
                            if(String.isNotBlank(emic)){
                                raIDList.add(emic);
                            }
                        }
                    }
                }
            }
        }
        if(raIDList != null && raIDList.size() > 0){
            List<Rental_Apply__c> racExtensionData = [SELECT ID
                                                            ,NAME
                                                            ,Is_Delete_Extension__c
                                                            ,ExtensionStatus__c
                                                            ,Extension_Type__c
                                                            ,Extension_Parent_Entrance__c 
                                                            ,ExtensionApplicationTime_Initial__c
                                                            ,ExtensionApprovalTime_Initial__c
                                                            ,ExtensionSuccessTimes__c
                                                            ,RcUnexpectExpiryDelay__c
                                                            ,ExtensionContent__c
                                                            ,RcUnexpectExpiryDelay_Mail__c
                                                            ,ExtensionDays__c
                                                        FROM Rental_Apply__c 
                                                        WHERE ID IN :raIDList 
                                                            AND Extension_Type__c = '批量延期'
                                                            AND ExtensionApplicationTime_Initial__c != NULL];
            if(racExtensionData != null && racExtensionData.size() > 0){
                List<Rental_Apply__c> updateRACE = new List<Rental_Apply__c>();
                for(Rental_Apply__c ra : newList){
                    //是批量审批,并且延期状态发生变化,那么就需要同步延期信息
                    if('批量延期'.equals(ra.Extension_Type__c) && ra.ExtensionStatus__c != oldMap.get(ra.Id).ExtensionStatus__c){
                        //只判断为主单时,并且 批量延期申请单id 字段不为空,那么就需要把延期数据同步到从单上
                        if(String.isBlank(ra.Root_Rental_Apply__c) 
                            && String.isNotBlank(ra.Extension_Much_ID__c)
                            && ('已批准'.equals(ra.ExtensionStatus__c) || '驳回'.equals(ra.ExtensionStatus__c) || String.isBlank(ra.ExtensionStatus__c)))
                        {
                            if('已批准'.equals(ra.ExtensionStatus__c)){
                                updateRACE = setUpdateRACE(ra.Extension_Much_ID__c,ra,racExtensionData,'');
                            }else if('驳回'.equals(ra.ExtensionStatus__c)){
                                updateRACE = setUpdateRACE(ra.Extension_Much_ID__c,ra,racExtensionData,'1');
                            }else if(String.isBlank(ra.ExtensionStatus__c)){
                                updateRACE = setUpdateRACE(ra.Extension_Much_ID__c,ra,racExtensionData,'1');
                            }
                        }
                    }
                }
                if(updateRACE != null && updateRACE.size() > 0){
                    update updateRACE;
                }
            }
        }
    }
    /**
     * [setUpdateRACE 更新从单的延期数据]
     * @param  emicS    [主单存放的此次延期的从单id]
     * @param  ra       [主单数据]
     * @param  raIDData [所有从单数据]
     * @param  rcType   [是否为 驳回或调回]
     * @return          [description]
     *
     * 注意:驳回和调回时,需要清空延期类型,而审批完成不需要
     */
    public List<Rental_Apply__c> setUpdateRACE(String emicS,Rental_Apply__c ra,List<Rental_Apply__c> raIDData,String rcType){
        List<Rental_Apply__c> updateRACE = new List<Rental_Apply__c>();
        if(raIDData != null && raIDData.size() > 0 && String.isNotBlank(emicS)){
            for(String emic : emicS.split(',')){
                if(String.isNotBlank(emic)){
                    emic = emic.substring(0,15);
                    for(Rental_Apply__c eRac : raIDData){
                        String eRacId = eRac.Id;
                        eRacId = eRacId.substring(0,15);
                        if(emic == eRacId){
                            eRac.ExtensionApprovalTime_Initial__c = ra.ExtensionApprovalTime_Initial__c;//延期批准时间(最初)
                            //eRac.RcUnexpectExpiryDelay__c = ra.RcUnexpectExpiryDelay__c;//RC未定到期延时
                            //eRac.ExtensionContent__c = ra.ExtensionContent__c;//延期内容
                            eRac.ExtensionStatus__c = ra.ExtensionStatus__c;//延期状态
                            eRac.ExtensionApplicationTime_Initial__c = ra.ExtensionApplicationTime_Initial__c;//延期申请时间(最初)
                            //由于如果是撤回时,那么需要把延期类型设为空
                            if(String.isNotBlank(rcType) && rcType == '1'){
                                eRac.Extension_Type__c = '';
                            }else{
                                //eRac.ExtensionSuccessTimes__c = ra.ExtensionSuccessTimes__c;//延期成功次数,从单会自动判断不需要同步
                                //只有审批完成以后才会赋值给从单
                                eRac.Extension_NewStep_AppTime__c = ra.Extension_NewStep_AppTime__c;//延期最新步骤批准时间
                            }
                            updateRACE.add(eRac);
                        }
                    }
                }
            }
        }
        return updateRACE;
    }
 
    /**
     * [checkExtensionDeadline 延期是否还可以审批]
     *
     * 判断延期审批的时间是否超过延期截止日期
     * 如果超过了,那么就不能审批了,只能驳回会撤回
     * 如果没有超过,那么可以正常审批
     * 
     */
    public void checkExtensionDeadline() {
        for(Rental_Apply__c ra : newList){
            try{
                if(ra.demo_purpose2__c == '试用(无询价)' || ra.demo_purpose2__c == '试用(有询价)'){
                    Rental_Apply__c ora = oldMap.get(ra.Id);
                    //是批量审批,并且延期状态发生变化,那么就需要同步延期信息
                    if(ra.Extension_Type__c == '批量延期'){
                        //只判断为主单时,并且 批量延期申请单id 字段不为空,那么就需要把延期数据同步到从单上
                        if(String.isBlank(ra.Root_Rental_Apply__c)){
                            //延期状态为 已批准、驳回或为空时,才同步
                            if(('申请中'.equals(ra.ExtensionStatus__c) || ('已批准'.equals(ra.ExtensionStatus__c) && ora.ExtensionStatus__c == '申请中')) 
                                && ra.Extension_NewStep_AppTime__c != ora.Extension_NewStep_AppTime__c){
                                if(ra.Extension_Deadline__c != null){
                                    Date nDa = Date.today();
                                    if(nDa > ra.Extension_Deadline__c){
                                        throw new ControllerUtil.myException('延期截止日期小于当前时间,不能延期');
                                    }
                                }
                                //判断此次延期的申请单是否存在 ok并且回寄时间不为空的一览
                                List<String> racLi = new List<String>();
                                if(String.isNotBlank(ra.Extension_Much_ID__c)){
                                    //获取此次批量延期的所有从单单子
                                    for(String emic : ra.Extension_Much_ID__c.split(',')){
                                        if(String.isNotBlank(emic)){
                                            racLi.add(emic);
                                        }
                                    }
                                }
                                //判断批量延期的时候,主单是否延期了
                                if(!ra.Is_Delete_Extension__c){
                                    racLi.add(ra.Id);
                                }
                                if(racLi.size() > 0){
                                    getAssetReturnTime(racLi);
                                }
                            }
                        }
                    }else if(String.isNotBlank(ra.Root_Rental_Apply__c)){
                        //延期状态为 已批准、驳回或为空时,才同步
                        if(('申请中'.equals(ra.ExtensionStatus__c) || ('已批准'.equals(ra.ExtensionStatus__c) && ora.ExtensionStatus__c == '申请中')) 
                            && ra.Extension_NewStep_AppTime__c != ora.Extension_NewStep_AppTime__c){
                            if(ra.Extension_Deadline__c != null){
                                Date nDa = Date.today();
                                if(nDa > ra.Extension_Deadline__c){
                                    throw new ControllerUtil.myException('延期截止日期小于当前时间,不能延期');
                                }
                            }
                            getAssetReturnTime(new List<String>{ra.Id});
                        }
                    }
                }
            }catch (Exception e) {
                ra.addError(e.getMessage() + ',请操作驳回。');
            }
        }
    }
 
    /**
     * [getAssetReturnTime description]
     *
     * 判断申请单是否存在 ok并且回寄时间不为空的一览
     */
    public void getAssetReturnTime(List<String> racLi){
        if(racLi != null && racLi.size() > 0){
            List<Rental_Apply_Equipment_Set__c> raescL = [select id,name 
                                                                from Rental_Apply_Equipment_Set__c 
                                                                where Rental_Apply__c in :racLi 
                                                                and (Received_Confirm__c = 'OK' OR Received_Confirm__c = '默认签收-OK' )
                                                                and Asset_return_time__c != null];
            if(raescL != null && raescL.size() > 0){
                throw new ControllerUtil.myException('此单不满足延期条件');
            }
        }
    }
    //add         wangweipeng                       2021/12/02                        end
 
    // @testVisible
    // private void testI() {
       
    // }
    @testVisible
    private void testI() {
        Integer i = 0;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
                i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
    }
    
}