高章伟
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
global class InsReToMaintenanceBatch implements Database.Batchable<sObject> {
    
    String query;
    public List<Maintenance_Contract__c> MtcUpdate{get;set;}
    public List<Inspection_Report__c> SelallInsReport {get;set;}
    //维修合同和所属检点书Map
    public Map<String,List<Integer>> InsMtcMap {get;set;}
    global InsReToMaintenanceBatch() {
        SelallInsReport = new List<Inspection_Report__c>();
        InsMtcMap = new Map<String,List<Integer>>();
        MtcUpdate = new List<Maintenance_Contract__c>();
    }
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        query = 'select id,name,PA_Contrant_Cnt__c,PB_Contrant_Cnt__c from Maintenance_Contract__c';
        //query = '';
        return Database.getQueryLocator(query);
    }
 
       global void execute(Database.BatchableContext BC, List<sObject> scope) {
           Date TodayYMD =  Date.today();
           MtcUpdate = scope;
           SelallInsReport = [select id,name,Inspection_Date__c,Contract__c from Inspection_Report__c where Contract__c !=null];
        //数据清零
        for(Maintenance_Contract__c Mc : MtcUpdate){
            Mc.PA_Contrant_Cnt__c = null;
            Mc.PB_Contrant_Cnt__c = null;
        }
        for(Inspection_Report__c iRc : SelallInsReport){
            if(InsMtcMap.containsKey(iRc.Contract__c)){
                continue;
            }else{
                List<Integer> NullList = New List<Integer>();
                InsMtcMap.put(iRc.Contract__c, NullList);
            }
        }
        system.debug('InsMtcMap::::::'+InsMtcMap);
        for(Inspection_Report__c iR : SelallInsReport){
            if(InsMtcMap.containsKey(iR.Contract__c)){
                //区分是不是今年
                if(TodayYMD.month()>3&&((iR.Inspection_Date__c.year()==TodayYMD.year()&&iR.Inspection_Date__c.month()>3)||
                   (iR.Inspection_Date__c.year()==(TodayYMD.year()+1)&&iR.Inspection_Date__c.month()<4))||
                   (TodayYMD.month()<3&&((iR.Inspection_Date__c.year()==TodayYMD.year()&&iR.Inspection_Date__c.month()<4)||
                   (iR.Inspection_Date__c.year()==(TodayYMD.year()-1)&&iR.Inspection_Date__c.month()>3)))
                ){
                    //区分上半年下半年
                    if(iR.Inspection_Date__c.month()>3&&iR.Inspection_Date__c.month()<10){
                        List<Integer> AddListInt = new List<Integer>();
                        AddListInt = InsMtcMap.get(iR.Contract__c);
                        AddListInt.add(0);
                        InsMtcMap.put(iR.Contract__c,AddListInt);
                    }else{
                        List<Integer> AddListInt = new List<Integer>();
                        AddListInt = InsMtcMap.get(iR.Contract__c);
                        AddListInt.add(1);
                        InsMtcMap.put(iR.Contract__c,AddListInt);
                    }
                }else{
                    //不是今年的忽略不计
                    continue;
                }
            }
        }
        system.debug('InsMtcMap::::::'+InsMtcMap);
        List<Maintenance_Contract__c> UpdateList = new List<Maintenance_Contract__c>();
        Map<String,String> AddOrNot = new Map<String,String>();
        for(Maintenance_Contract__c Mc : MtcUpdate){
                Integer PA = 0; //上半年计数
                Integer PB = 0; //下半年计数
                if(AddOrNot.containsKey(Mc.id)){
                    continue;
                }else{
                    if(InsMtcMap.containsKey(Mc.id)){
                    List<Integer> ResultCnt = InsMtcMap.get(Mc.id);
                        for(Integer i : ResultCnt){
                            if(i == 0){
                                PA=PA+1;
                            }else{
                                PB=PB+1;
                            }
                        }
                    }
                    Mc.PA_Contrant_Cnt__c = PA;
                    Mc.PB_Contrant_Cnt__c = PB;
                    //UpdateList.add(Mc);
                    AddOrNot.put(Mc.id, Mc.id);
                }
        }
        
        system.debug('MtcUpdate::::::'+MtcUpdate);
        if(MtcUpdate.size()>0){
            update MtcUpdate;
        }
    }
    
    global void finish(Database.BatchableContext BC) {
        
    }
    
}