高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
global class UpdateGastroscopyBatch implements Database.Batchable<sObject> {
    public String query;
    public String conId;
    public List<String> conIds;
 
    global UpdateGastroscopyBatch(){
        this.query = query;
    }
 
    global UpdateGastroscopyBatch(List<String> conIds){
        this.query = query;
        //this.conId = conId;
        this.conIds = conIds;
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        // query = 'select Id,Maintenance_Contract__c,IS_GASTROSCOPY__c,IF_URF__c,IF_BRONCHOSCOPE__c,IF_PERIPHERAL__c,IF_OTHERTUBE__c,First_Month_RepairAmount__c,Second_Month_RepairAmount__c,Third_Month_RepairAmount__c,Maintenance_Contract__r.RecordType_DeveloperName__c,Asset__r.Maintenance_Price_Month__c from Maintenance_Contract_Asset__c  ';
        query = 'select Id,Estimate_Asset_Cnt__c,Contract_mirror_type__c,GastroscopyRepairCost__c,';
        query += ' BronchoscopeRepairCost__c,PreipheralRepairCost__c,keepRepairInContractMoney__c,keepRepairInContract__c,Contract_Conclusion_Date__c from Maintenance_Contract__c  ';
        query += ' where (Status__c = \'契約\' or Status__c = \'契約満了\') and Contract_mirror_type__c = null';
 
        // if (conId != null && conId != '') {
        //     query += ' where  Maintenance_Contract__c = :conId ';
        // }
        if(conIds != null && conIds.size() != 0) {
            query += ' and id in :conIds';
        }
 
        return Database.getQueryLocator(query);
    }
 
 
    global void execute(Database.BatchableContext BC, list<Maintenance_Contract__c> scope) {
        //上传对象
        Map<String,Maintenance_Contract__c> MCMap = new Map<String,Maintenance_Contract__c>();
 
        // Set<String> contactidList = new Set<String>();
 
        for (Maintenance_Contract__c mc : scope) {
            MCMap.put(mc.Id,mc);
        }
        List<Maintenance_Contract_Asset__c> McaList = [select Id,Maintenance_Contract__c,Asset_Type__c,
                                                       Maintenance_Contract__r.Estimate_Asset_Cnt__c,IF_OTHERTUBE__c,
                                                       First_Month_RepairAmount__c,Second_Month_RepairAmount__c,Third_Month_RepairAmount__c,
                                                       Maintenance_Contract__r.RecordType_DeveloperName__c,Asset__r.Maintenance_Price_Month__c
                                                       from Maintenance_Contract_Asset__c
                                                       where (Maintenance_Contract__r.Status__c = '契約' or Maintenance_Contract__r.Status__c = '契約満了')
                                                       and Maintenance_Contract__c in :MCMap.keyset()];
 
        //统计肠胃镜个数
        Map<String,Integer> gastroscopyMap = new Map<String,Integer>();
 
        //胃肠镜定价金额
        Map<String,Double> gastroscopyDepositMap = new Map<String,Double>();
 
        //统计URF个数
        Map<String,Integer> URFMap = new Map<String,Integer>();
 
        //统计支气管镜个数
        Map<String,Integer> bronchoscopeMap = new Map<String,Integer>();
 
        //支气管镜定价金额
        Map<String,Double> bronchoscopeDepositMap = new Map<String,Double>();
 
        // gzw 20210926 追加'电子腹腔镜、附属品、光学视管、气管镜 合同类型 start
        //统计电子腹腔镜个数
        Map<String,Integer> dzcountMap = new Map<String,Integer>();
        //统计附属品个数
        Map<String,Integer> fscountMap = new Map<String,Integer>();
        //统计光学视管个数
        Map<String,Integer> gxcountMap = new Map<String,Integer>();
        //统计气管镜个数
        Map<String,Integer> qgcountMap = new Map<String,Integer>();
        // gzw 20210926 追加'电子腹腔镜、附属品、光学视管、气管镜 合同类型 end
 
        //统计周边设备个数
        Map<String,Integer> preipheralMap = new Map<String,Integer>();
 
        //统计其他镜种个数
        Map<String,Integer> othertubeMap = new Map<String,Integer>();
 
        //计算合同报价资产件数
        Map<String,Integer> totalMap = new Map<String,Integer>();
 
        //肠胃镜第4分类的几种情况
        List<String> gastroscopyForthType = new List<String>();
        gastroscopyForthType.add('GF');
        gastroscopyForthType.add('GIF');
        gastroscopyForthType.add('CF');
        gastroscopyForthType.add('JF');
        gastroscopyForthType.add('SIF');
 
        //支气管镜第四分类几种情况
        List<String> bronchoscopeForthType = new List<String>();
        bronchoscopeForthType.add('BF');
 
        //周边设备第四分类几种情况
        List<String> preipheralForthType = new List<String>();
        preipheralForthType.add('CV');
        preipheralForthType.add('CLV');
        preipheralForthType.add('双极电切镜工作把手');
        preipheralForthType.add('其他');
 
        //修理报价等级
        List<String> repairRank = new List<String>();
        repairRank.add('A');
        repairRank.add('AW');
        repairRank.add('B');
        repairRank.add('BW');
        repairRank.add('C');
        repairRank.add('CW');
 
 
        for (Maintenance_Contract_Asset__c mca : McaList) {
            if (mca.Asset_Type__c == '胃肠镜') {
                //存在了就累加
                if(gastroscopyMap.containsKey(mca.Maintenance_Contract__c)) {
                    gastroscopyMap.put(mca.Maintenance_Contract__c,gastroscopyMap.get(mca.Maintenance_Contract__c)+1);
                    if(mca.Asset__r.Maintenance_Price_Month__c!=null) {
                        gastroscopyDepositMap.put(mca.Maintenance_Contract__c,gastroscopyDepositMap.get(mca.Maintenance_Contract__c) + mca.Asset__r.Maintenance_Price_Month__c);
                    }
                }else{
                    //如果没有就初始化一个
                    gastroscopyMap.put(mca.Maintenance_Contract__c,1);
                    if(mca.Asset__r.Maintenance_Price_Month__c!=null) {
                        gastroscopyDepositMap.put(mca.Maintenance_Contract__c,mca.Asset__r.Maintenance_Price_Month__c);
                    }else{
                        gastroscopyDepositMap.put(mca.Maintenance_Contract__c,0.00);
                    }
                }
            }
            if(mca.Asset_Type__c == 'URF') {
                if(URFMap.containsKey(mca.Maintenance_Contract__c)) {
                    URFMap.put(mca.Maintenance_Contract__c,URFMap.get(mca.Maintenance_Contract__c)+1);
                }else{
                    URFMap.put(mca.Maintenance_Contract__c,1);
                }
            }
 
            // gzw 20210926 追加镜种 start
 
            if(mca.Asset_Type__c == '电子腹腔镜') {
                if(dzcountMap.containsKey(mca.Maintenance_Contract__c)) {
                    dzcountMap.put(mca.Maintenance_Contract__c,dzcountMap.get(mca.Maintenance_Contract__c)+1);
                }else{
                    dzcountMap.put(mca.Maintenance_Contract__c,1);
                }
            }
 
            if(mca.Asset_Type__c == '附属品') {
                if(fscountMap.containsKey(mca.Maintenance_Contract__c)) {
                    fscountMap.put(mca.Maintenance_Contract__c,fscountMap.get(mca.Maintenance_Contract__c)+1);
                }else{
                    fscountMap.put(mca.Maintenance_Contract__c,1);
                }
            }
 
            if(mca.Asset_Type__c == '光学视管') {
                if(gxcountMap.containsKey(mca.Maintenance_Contract__c)) {
                    gxcountMap.put(mca.Maintenance_Contract__c,gxcountMap.get(mca.Maintenance_Contract__c)+1);
                }else{
                    gxcountMap.put(mca.Maintenance_Contract__c,1);
                }
            }
 
            if(mca.Asset_Type__c == '气管镜') {
                if(qgcountMap.containsKey(mca.Maintenance_Contract__c)) {
                    qgcountMap.put(mca.Maintenance_Contract__c,qgcountMap.get(mca.Maintenance_Contract__c)+1);
                }else{
                    qgcountMap.put(mca.Maintenance_Contract__c,1);
                }
            }
 
            // gzw 20210926 追加镜种 end
 
            if(mca.Asset_Type__c == '支气管镜') {
                if(bronchoscopeMap.containsKey(mca.Maintenance_Contract__c)) {
                    bronchoscopeMap.put(mca.Maintenance_Contract__c,bronchoscopeMap.get(mca.Maintenance_Contract__c)+1);
                    if(mca.Asset__r.Maintenance_Price_Month__c!=null) {
                        bronchoscopeDepositMap.put(mca.Maintenance_Contract__c,bronchoscopeDepositMap.get(mca.Maintenance_Contract__c) + mca.Asset__r.Maintenance_Price_Month__c);
                    }
 
                }else{
                    bronchoscopeMap.put(mca.Maintenance_Contract__c,1);
                    if(mca.Asset__r.Maintenance_Price_Month__c != null) {
                        bronchoscopeDepositMap.put(mca.Maintenance_Contract__c,mca.Asset__r.Maintenance_Price_Month__c);
                    }else{
                        bronchoscopeDepositMap.put(mca.Maintenance_Contract__c,0.00);
                    }
 
                }
            }
            if(mca.Asset_Type__c == '周边设备') {
                if(preipheralMap.containsKey(mca.Maintenance_Contract__c)) {
                    preipheralMap.put(mca.Maintenance_Contract__c,preipheralMap.get(mca.Maintenance_Contract__c)+1);
                }else{
                    preipheralMap.put(mca.Maintenance_Contract__c,1);
                }
            }
            if(mca.IF_OTHERTUBE__c == true) {
                if(othertubeMap.containsKey(mca.Maintenance_Contract__c)) {
                    othertubeMap.put(mca.Maintenance_Contract__c,othertubeMap.get(mca.Maintenance_Contract__c));
                }else{
                    othertubeMap.put(mca.Maintenance_Contract__c,1);
                }
            }
        }
 
        //
        for (String key : MCMap.keyset()) {
            Maintenance_Contract__c mc = MCMap.get(key);
            // Maintenance_Contract__c mc : MCMap) {
            // 1.胃肠镜
            if (gastroscopyMap.containsKey(mc.Id)) {
                mc.GastroscopySize__c = gastroscopyMap.get(mc.Id);
            }
            // 肠胃镜金额
            if (gastroscopyDepositMap.containsKey(mc.Id)) {
                mc.GastroscopyCoast__c = gastroscopyDepositMap.get(mc.Id);
            }
            // 2.支气管镜
            if (bronchoscopeMap.containsKey(mc.Id)) {
                mc.BronchoscopeSize__c = bronchoscopeMap.get(mc.Id);
            }
            // 支气管镜金额
            if (bronchoscopeDepositMap.containsKey(mc.Id)) {
                mc.BronchoscopeCost__c = bronchoscopeDepositMap.get(mc.Id);
            }
            // 3.URF
            if (URFMap.containsKey(mc.Id)) {
                mc.URFSize__c = URFMap.get(mc.Id);
            }
            // 4.电子腹腔镜
 
            // 5.附属品
 
            // 6.光学视镜
 
            // 7.气管镜
 
            // 周边设备
            if (preipheralMap.containsKey(mc.Id)) {
                mc.PreipheralSize__c = preipheralMap.get(mc.Id);
            }
 
 
            // 合同镜种类型计算
            if( gastroscopyMap.containsKey(mc.Id) && gastroscopyMap.get(mc.Id) > mc.Estimate_Asset_Cnt__c/2 ) {
                mc.Contract_mirror_type__c = '肠胃镜为主合同';
            }
            if( bronchoscopeMap.containsKey(mc.Id) && bronchoscopeMap.get(mc.Id) > mc.Estimate_Asset_Cnt__c/2) {
                mc.Contract_mirror_type__c = '支气管镜为主合同';
            }
            if( URFMap.containsKey(mc.Id) && URFMap.get(mc.Id) > mc.Estimate_Asset_Cnt__c/2 ) {
                mc.Contract_mirror_type__c = 'URF为主合同';
            }
            if( URFMap.containsKey(mc.Id) && URFMap.get(mc.Id) == mc.Estimate_Asset_Cnt__c ) {
                mc.Contract_mirror_type__c = 'URF单镜种合同';
            }
            if( dzcountMap.containsKey(mc.Id) && dzcountMap.get(mc.Id) > mc.Estimate_Asset_Cnt__c/2 ) {
                mc.Contract_mirror_type__c = '电子腹腔镜为主合同';
            }
            if( fscountMap.containsKey(mc.Id) && fscountMap.get(mc.Id) > mc.Estimate_Asset_Cnt__c/2 ) {
                mc.Contract_mirror_type__c = '附属品为主合同';
            }
            if( gxcountMap.containsKey(mc.Id) && gxcountMap.get(mc.Id) > mc.Estimate_Asset_Cnt__c/2 ) {
                mc.Contract_mirror_type__c = '光学视镜为主合同';
            }
            if( qgcountMap.containsKey(mc.Id) && qgcountMap.get(mc.Id) > mc.Estimate_Asset_Cnt__c/2 ) {
                mc.Contract_mirror_type__c = '气管镜为主合同';
            }
            if(String.isBlank(mc.Contract_mirror_type__c)) {
                mc.Contract_mirror_type__c = '其他';
            }
        }
        //
 
        // //将各个镜种存到对应的Map当中
        // for(String key : gastroscopyMap.keySet()){
        //     Maintenance_Contract__c mc = new Maintenance_Contract__c();
        //     mc.Id = key;
        //     mc.GastroscopySize__c = gastroscopyMap.get(key);
        //     MCMap.put(key,mc);
        // }
 
 
        // //存入肠胃镜定价金额
        // if(gastroscopyDepositMap.size()>0){
        //     for(String gac : gastroscopyDepositMap.keySet()){
        //         if(MCMap.containsKey(gac)){
        //             MCMap.get(gac).GastroscopyCoast__c = gastroscopyDepositMap.get(gac);
        //         }
        //     }
        // }
 
 
        // for(String key : URFMap.keySet()){
        //     if(MCMap.containsKey(key)){
        //         MCMap.get(key).URFSize__c = URFMap.get(key);
        //     }else{
        //         Maintenance_Contract__c mc = new Maintenance_Contract__c();
        //         mc.Id = key;
        //         mc.URFSize__c = URFMap.get(key);
        //         MCMap.put(key,mc);
        //     }
        // }
 
        // for(String key : bronchoscopeMap.keySet()){
        //     if(MCMap.containsKey(key)){
        //         MCMap.get(key).BronchoscopeSize__c = bronchoscopeMap.get(key);
        //     }else{
        //         Maintenance_Contract__c mc = new Maintenance_Contract__c();
        //         mc.Id = key;
        //         mc.BronchoscopeSize__c = bronchoscopeMap.get(key);
        //         MCMap.put(key,mc);
        //     }
        // }
 
 
 
        // //存入支气管的定价金额
        // if(bronchoscopeDepositMap.size()>0){
        //     for(String bcc : bronchoscopeDepositMap.keySet()){
        //         if(MCMap.containsKey(bcc)){
        //             MCMap.get(bcc).BronchoscopeCost__c = bronchoscopeDepositMap.get(bcc);
        //         }
        //     }
        // }
 
 
        // for(String key : preipheralMap.keySet()){
        //     if(MCMap.containsKey(key)){
        //         MCMap.get(key).PreipheralSize__c = preipheralMap.get(key);
        //     }else{
        //         Maintenance_Contract__c mc = new Maintenance_Contract__c();
        //         mc.Id = key;
        //         mc.PreipheralSize__c = preipheralMap.get(key);
        //         MCMap.put(key,mc);
        //     }
        // }
 
 
 
        // for(String key : othertubeMap.keySet()){
        //     if(MCMap.containsKey(key)){
        //         MCMap.get(key).OthertubeSize__c = othertubeMap.get(key);
        //     }else{
        //         Maintenance_Contract__c mc = new Maintenance_Contract__c();
        //         mc.Id = key;
        //         mc.OthertubeSize__c = othertubeMap.get(key);
        //         MCMap.put(key,mc);
        //     }
        // }
 
        // //统计总和
        // for(String key : MCMap.keySet()){
        //     if(totalMap.containsKey(key)){
        //         if(MCMap.get(key).GastroscopySize__c != null){
        //             totalMap.put(key,totalMap.get(key)+ MCMap.get(key).GastroscopySize__c.intValue());
        //         }else if(MCMap.get(key).URFSize__c != null){
        //             totalMap.put(key,totalMap.get(key)+ MCMap.get(key).URFSize__c.intValue());
        //         }else if(MCMap.get(key).BronchoscopeSize__c != null){
        //             totalMap.put(key,totalMap.get(key)+ MCMap.get(key).BronchoscopeSize__c.intValue());
        //         }else if(MCMap.get(key).PreipheralSize__c != null){
        //             totalMap.put(key,totalMap.get(key)+ MCMap.get(key).PreipheralSize__c.intValue());
        //         }else if(MCMap.get(key).OthertubeSize__c != null){
        //             totalMap.put(key,totalMap.get(key)+ MCMap.get(key).OthertubeSize__c.intValue());
        //         }
        //     }else{
        //         if(MCMap.get(key).GastroscopySize__c != null){
        //             totalMap.put(key,MCMap.get(key).GastroscopySize__c.intValue());
        //         }else if(MCMap.get(key).URFSize__c != null){
        //             totalMap.put(key,MCMap.get(key).URFSize__c.intValue());
        //         }else if(MCMap.get(key).BronchoscopeSize__c != null){
        //             totalMap.put(key,MCMap.get(key).BronchoscopeSize__c.intValue());
        //         }else if(MCMap.get(key).PreipheralSize__c != null){
        //             totalMap.put(key,MCMap.get(key).PreipheralSize__c.intValue());
        //         }else if(MCMap.get(key).OthertubeSize__c != null){
        //             totalMap.put(key,MCMap.get(key).OthertubeSize__c.intValue());
        //         }
        //     }
        // }
 
 
 
 
        //对比规则
        /**
         *
         * 1.当该合同内肠胃镜条数多于合同报价资产件数的一半------【肠胃镜为主合同】
         * 2.当该合同内支气管镜条数多于合同报价资产件数的一半------【支气管镜为主合同】
         * 3.当该合同内URF条数多于合同报价资产件数的一半------【URF为主合同】
         * 4.当该合同内URF条数等于合同报价资产件数-----【URF单镜种合同】
         * 5.当该合同内的CHF、CYF、ENF、HD EndoEYE、HYF、LTF、泌尿科用硬性镜、腹腔镜/胸腔镜多于报价资产件数的一半时,取最多的镜种作为【该镜种为主的合同】
         * 6.以上情况均不符合-----其他
         *
         */
        // for(String key : MCMap.keySet()){
        //     if( MCMap.get(key).GastroscopySize__c > totalMap.get(key)/2 ){
        //         MCMap.get(key).Contract_mirror_type__c = '肠胃镜为主合同';
        //     }else if( MCMap.get(key).BronchoscopeSize__c > totalMap.get(key)/2){
        //         MCMap.get(key).Contract_mirror_type__c = '支气管镜为主合同';
        //     }else if( MCMap.get(key).URFSize__c > totalMap.get(key)/2 ){
        //         MCMap.get(key).Contract_mirror_type__c = 'URF为主合同';
        //     }else if( MCMap.get(key).URFSize__c == totalMap.get(key)/2 ){
        //         MCMap.get(key).Contract_mirror_type__c = 'URF单镜种合同';
        //     }
        //     // else if( MCMap.get(key).OthertubeSize__c > totalMap.get(key)/2){
        //     //     MCMap.get(key).Contract_mirror_type__c = '其他镜种合同';
        //     // }
        //     else{
        //         MCMap.get(key).Contract_mirror_type__c = '其他';
        //     }
        // }
 
 
        //计算设备生效后的第1.2.3个月修理金额
 
        // //拿出所有的维修合同
        // Set<String> weixiuContractId = new Set<String>();
        // for(Maintenance_Contract_Asset__c mca :scope){
        //     weixiuContractId.add(mca.Maintenance_Contract__c);
        // }
 
        //检索所有合同保有设备的修理
        list<Repair__c> newRepiarList = new list<Repair__c>();
        // if(weixiuContractId.size()>0){
        newRepiarList =
        [select Id, Usage_Ratio_Price_Contract__c,
         Usage_Ratio_Price_Guarantee__c,Usage_Ratio_Price_Service__c,
         Delivered_Product__c,
         Repair_cost__c,
         Maintenance_Contract__c,
         Maintenance_Contract__r.Contract_Start_Date__c,
         Maintenance_Contract__r.Contract_End_Date__c,
         // tcm 20211011 生效后第N个月的区间都改为 合同日+N个月 end
         Maintenance_Contract__r.Contract_Conclusion_Date__c,
         // tcm 20211011 生效后第N个月的区间都改为 合同日+N个月 end
         Failure_Occurrence_Date__c,
         Repair_Quotation_Id__c,
         VM_Maintenance_Contract__c,
         Delivered_Product__r.Category4__c,
         Repair_Rank__c
         from Repair__c
         where (Maintenance_Contract__c != null or VM_Maintenance_Contract__c != null)
         and (Maintenance_Contract__c in: MCMap.keyset()
              or VM_Maintenance_Contract__c in: MCMap.keyset())
         and Status2__c != '00.删除'
         and Status2__c != '00.取消'
         order by Maintenance_Contract__c
        ];
        // }
 
 
 
        //找到对应维修合同/保有设备底下的合同
        if(newRepiarList != null && newRepiarList.size()>0) {
            //初始化清空维修合同/保有设备的第一二三月的维修金额
            for(Maintenance_Contract_Asset__c mca : McaList) {
                mca.First_Month_RepairAmount__c = 0.00;
                mca.Second_Month_RepairAmount__c = 0.00;
                mca.Third_Month_RepairAmount__c = 0.00;
            }
 
        }
 
        //置空肠胃镜 支气管镜 周边设备的修理金额
        for(String key : MCMap.keySet()) {
            MCMap.get(key).GastroscopyRepairCost__c = 0.00;
            MCMap.get(key).BronchoscopeRepairCost__c = 0.00;
            MCMap.get(key).PreipheralRepairCost__c = 0.00;
        }
        // tcm 20211011 修复大修次数和大修修理金额的数字对不上 start
        // 初始化大修次数和大修金额
        for (Maintenance_Contract_Asset__c mca : McaList) {
            MCMap.get(mca.Maintenance_Contract__c).keepRepairInContract__c = 0;
            MCMap.get(mca.Maintenance_Contract__c).keepRepairInContractMoney__c = 0.00;
        }
        // tcm 20211011 修复大修次数和大修修理金额的数字对不上 end
 
        //各种镜种的修理金额
        for (Maintenance_Contract_Asset__c mca : McaList) {
            for(Repair__c rc : newRepiarList) {
                if(mca.Maintenance_Contract__c == rc.Maintenance_Contract__c && mca.Asset_Type__c == '胃肠镜' && gastroscopyForthType.contains(rc.Delivered_Product__r.Category4__c) && mca.Asset__c == rc.Delivered_Product__c ) {
                    //肠胃镜修理金额
                    if(MCMap.containsKey(mca.Maintenance_Contract__c)) {
 
                        if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('Maintenance_Contract')) {
                            MCMap.get(mca.Maintenance_Contract__c).GastroscopyRepairCost__c += rc.Usage_Ratio_Price_Contract__c;
 
                        }else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')) {
                            MCMap.get(mca.Maintenance_Contract__c).GastroscopyRepairCost__c += rc.Usage_Ratio_Price_Service__c;
 
                        }else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('VM_Contract')) {
                            MCMap.get(mca.Maintenance_Contract__c).GastroscopyRepairCost__c += rc.Usage_Ratio_Price_Guarantee__c;
 
                        }
                    }
                    // tcm 20211011     start
                    // else{
                    //     Maintenance_Contract__c mc = new Maintenance_Contract__c();
                    //     mc.Id = mca.Maintenance_Contract__c;
                    //     if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('Maintenance_Contract')) {
                    //         mc.GastroscopyRepairCost__c = rc.Usage_Ratio_Price_Contract__c;
                    //     }else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')) {
                    //         mc.GastroscopyRepairCost__c = rc.Usage_Ratio_Price_Service__c;
                    //     }else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('VM_Contract')) {
                    //         mc.GastroscopyRepairCost__c = rc.Usage_Ratio_Price_Guarantee__c;
                    //     }
                    //     MCMap.put(mca.Maintenance_Contract__c,mc);
                    // }
                    // tcm 20211011     end
                    //支气管镜维修金额
                }else if(mca.Maintenance_Contract__c == rc.Maintenance_Contract__c && mca.Asset_Type__c == '支气管镜' && bronchoscopeForthType.contains(rc.Delivered_Product__r.Category4__c) && mca.Asset__c == rc.Delivered_Product__c) {
                    if(MCMap.containsKey(mca.Maintenance_Contract__c)) {
                        if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('Maintenance_Contract')) {
                            MCMap.get(mca.Maintenance_Contract__c).BronchoscopeRepairCost__c += rc.Usage_Ratio_Price_Contract__c;
                        }else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')) {
                            MCMap.get(mca.Maintenance_Contract__c).BronchoscopeRepairCost__c += rc.Usage_Ratio_Price_Service__c;
                        }else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('VM_Contract')) {
                            MCMap.get(mca.Maintenance_Contract__c).BronchoscopeRepairCost__c += rc.Usage_Ratio_Price_Guarantee__c;
                        }
                    }
                    // tcm 20211011     start
                    // else{
                    //     Maintenance_Contract__c mc = new Maintenance_Contract__c();
                    //     mc.Id = mca.Maintenance_Contract__c;
                    //     if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('Maintenance_Contract')) {
                    //         mc.BronchoscopeRepairCost__c = rc.Usage_Ratio_Price_Contract__c;
                    //     }else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')) {
                    //         mc.BronchoscopeRepairCost__c = rc.Usage_Ratio_Price_Service__c;
                    //     }else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('VM_Contract')) {
                    //         mc.BronchoscopeRepairCost__c = rc.Usage_Ratio_Price_Guarantee__c;
                    //     }
                    //     MCMap.put(mca.Maintenance_Contract__c,mc);
                    // }
                    // tcm 20211011     end
                }
                //周边设备的修理金额
                else if(mca.Maintenance_Contract__c == rc.Maintenance_Contract__c && mca.Asset_Type__c == '周边设备' && preipheralForthType.contains(rc.Delivered_Product__r.Category4__c) && mca.Asset__c == rc.Delivered_Product__c) {
                    if(MCMap.containsKey(mca.Maintenance_Contract__c)) {
                        if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('Maintenance_Contract')) {
                            MCMap.get(mca.Maintenance_Contract__c).PreipheralRepairCost__c += rc.Usage_Ratio_Price_Contract__c;
 
                            System.debug('IF_PERIPHERAL__c === Maintenance_Contract'+rc.Usage_Ratio_Price_Contract__c+'==='+mca.Maintenance_Contract__c);
                        }else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')) {
                            MCMap.get(mca.Maintenance_Contract__c).PreipheralRepairCost__c += rc.Usage_Ratio_Price_Service__c;
 
                            System.debug('IF_PERIPHERAL__c === Maintenance_Contract'+rc.Usage_Ratio_Price_Service__c+'==='+mca.Maintenance_Contract__c);
                        }else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('VM_Contract')) {
                            MCMap.get(mca.Maintenance_Contract__c).PreipheralRepairCost__c += rc.Usage_Ratio_Price_Guarantee__c;
 
                            System.debug('IF_PERIPHERAL__c === Maintenance_Contract'+rc.Usage_Ratio_Price_Guarantee__c+'==='+mca.Maintenance_Contract__c);
                        }
                    }
                    // tcm 20211011     start
                    // else{
                    //     Maintenance_Contract__c mc = new Maintenance_Contract__c();
                    //     mc.Id = mca.Maintenance_Contract__c;
                    //     if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('Maintenance_Contract')) {
                    //         System.debug('IF_PERIPHERAL__c === Maintenance_Contract'+rc.Usage_Ratio_Price_Contract__c+'==='+mca.Maintenance_Contract__c);
                    //         mc.PreipheralRepairCost__c = rc.Usage_Ratio_Price_Contract__c;
                    //     }else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')) {
                    //         System.debug('IF_PERIPHERAL__c === Maintenance_Contract'+rc.Usage_Ratio_Price_Service__c+'==='+mca.Maintenance_Contract__c);
                    //         mc.PreipheralRepairCost__c = rc.Usage_Ratio_Price_Service__c;
                    //     }else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('VM_Contract')) {
                    //         System.debug('IF_PERIPHERAL__c === Maintenance_Contract'+rc.Usage_Ratio_Price_Guarantee__c+'==='+mca.Maintenance_Contract__c);
                    //         mc.PreipheralRepairCost__c = rc.Usage_Ratio_Price_Guarantee__c;
                    //     }
                    //     MCMap.put(mca.Maintenance_Contract__c,mc);
                    // }
                    // tcm 20211011     end
                }
 
                //存在修理 且修理等级为A/AW/B/BW/C/CW
                if(mca.Maintenance_Contract__c == rc.Maintenance_Contract__c && repairRank.contains(rc.Repair_Rank__c) && mca.Asset__c == rc.Delivered_Product__c) {
                    //查找是否在MCMap里
                    if(MCMap.containsKey(mca.Maintenance_Contract__c)) {
                        // tcm 20211011 修复大修次数和大修修理金额的数字对不上 start
                        // if(MCMap.get(mca.Maintenance_Contract__c).keepRepairInContract__c == null) {
                        //     //计算修理次数
                        //     MCMap.get(mca.Maintenance_Contract__c).keepRepairInContract__c = 1;
                        //     MCMap.get(mca.Maintenance_Contract__c).keepRepairInContractMoney__c = 0.00;
                        //     //计算修理金额
                        //     if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('Maintenance_Contract')) {
                        //         MCMap.get(mca.Maintenance_Contract__c).keepRepairInContractMoney__c += rc.Usage_Ratio_Price_Contract__c;
                        //     }else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')) {
                        //         MCMap.get(mca.Maintenance_Contract__c).keepRepairInContractMoney__c += rc.Usage_Ratio_Price_Service__c;
                        //     }else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('VM_Contract')) {
                        //         MCMap.get(mca.Maintenance_Contract__c).keepRepairInContractMoney__c += rc.Usage_Ratio_Price_Guarantee__c;
                        //     }
                        // }else{
                        // tcm 20211011 修复大修次数和大修修理金额的数字对不上 end
                        //已经存在的值
                        MCMap.get(mca.Maintenance_Contract__c).keepRepairInContract__c = MCMap.get(mca.Maintenance_Contract__c).keepRepairInContract__c + 1;
                        if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('Maintenance_Contract')) {
                            MCMap.get(mca.Maintenance_Contract__c).keepRepairInContractMoney__c += rc.Usage_Ratio_Price_Contract__c;
                        }else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')) {
                            MCMap.get(mca.Maintenance_Contract__c).keepRepairInContractMoney__c += rc.Usage_Ratio_Price_Service__c;
                        }else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('VM_Contract')) {
                            MCMap.get(mca.Maintenance_Contract__c).keepRepairInContractMoney__c += rc.Usage_Ratio_Price_Guarantee__c;
                        }
                        // tcm 20211011 修复大修次数和大修修理金额的数字对不上 start
                        // }
                    }
 
                    // else{
                    //     //不在就新建一个对象
                    //     Maintenance_Contract__c mc = new Maintenance_Contract__c();
                    //     mc.Id = mca.Maintenance_Contract__c;
                    //     mc.keepRepairInContract__c = 1;
                    //     if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('Maintenance_Contract')) {
                    //         mc.keepRepairInContractMoney__c = rc.Usage_Ratio_Price_Contract__c;
                    //     }else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')) {
                    //         mc.keepRepairInContractMoney__c = rc.Usage_Ratio_Price_Service__c;
                    //     }else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('VM_Contract')) {
                    //         mc.keepRepairInContractMoney__c = rc.Usage_Ratio_Price_Guarantee__c;
                    //     }
 
                    //     MCMap.put(mca.Maintenance_Contract__c,mc);
                    // }
                    // tcm 20211011 修复大修次数和大修修理金额的数字对不上 end
                }
 
                //第一二三月修理金额
                if(mca.Maintenance_Contract__c == rc.Maintenance_Contract__c) {
                    // tcm 20211011 生效后第N个月的区间都改为 合同日+N个月 start
                    //判断 故障发生日 Failure_Occurrence_Date__c 是否在第一个月内
                    if(rc.Failure_Occurrence_Date__c <= rc.Maintenance_Contract__r.Contract_Conclusion_Date__c.addMonths(1)) {
                        // tcm 20211011 生效后第N个月的区间都改为 合同日+N个月 end
                        //按照维修合同的RecordType_DeveloperName__c 计算维修金额
                        //不同种类维修金额不同
                        if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('Maintenance_Contract')) {
                            mca.First_Month_RepairAmount__c += rc.Usage_Ratio_Price_Contract__c;
                        }else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')) {
                            mca.First_Month_RepairAmount__c += rc.Usage_Ratio_Price_Service__c;
                        }else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('VM_Contract')) {
                            mca.First_Month_RepairAmount__c += rc.Usage_Ratio_Price_Guarantee__c;
                        }
                    }
                    // tcm 20211011 生效后第N个月的区间都改为 合同日+N个月 start
                    //判断是否在第二个月内
                    if(rc.Failure_Occurrence_Date__c <= rc.Maintenance_Contract__r.Contract_Conclusion_Date__c.addMonths(2)) {
                        system.debug('进入第二个月判断,记录类型名等于'+mca.Maintenance_Contract__r.RecordType_DeveloperName__c);
                        // tcm 20211011 生效后第N个月的区间都改为 合同日+N个月 end
                        if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('Maintenance_Contract')) {
                            mca.Second_Month_RepairAmount__c += rc.Usage_Ratio_Price_Contract__c;
                        }else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')) {
                            mca.Second_Month_RepairAmount__c += rc.Usage_Ratio_Price_Service__c;
                        }else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('VM_Contract')) {
                            mca.Second_Month_RepairAmount__c += rc.Usage_Ratio_Price_Guarantee__c;
                        }
                        system.debug('第二个月维修价格等于'+mca.First_Month_RepairAmount__c);
                    }
                    // tcm 20211011 生效后第N个月的区间都改为 合同日+N个月 start
                    //判断是否在第三个月内
                    if(rc.Failure_Occurrence_Date__c <= rc.Maintenance_Contract__r.Contract_Conclusion_Date__c.addMonths(3)) {
                        system.debug('进入第三个月判断,记录类型名等于'+mca.Maintenance_Contract__r.RecordType_DeveloperName__c);
                        // tcm 20211011 生效后第N个月的区间都改为 合同日+N个月 end
                        if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('Maintenance_Contract')) {
                            mca.Third_Month_RepairAmount__c += rc.Usage_Ratio_Price_Contract__c;
                        }else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')) {
                            mca.Third_Month_RepairAmount__c += rc.Usage_Ratio_Price_Service__c;
                        }else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('VM_Contract')) {
                            mca.Third_Month_RepairAmount__c += rc.Usage_Ratio_Price_Guarantee__c;
                        }
                        system.debug('第三个月维修价格等于'+mca.First_Month_RepairAmount__c);
                    }
                }
            }
        }
        update McaList;
        update MCMap.values();
    }
 
    global void finish(Database.BatchableContext BC) {
    }
 
    global void calculateSum (Maintenance_Contract_Asset__c mca, Repair__c rc ){
 
    }
 
    // @TestVisible private void test() {
    //     Integer i = 0;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    //     i++;
    // }
}