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
/**************************************************************************************************
@Author: Denny陈帮才
@Name: SummaryThreeYearsContractBatch
@CreateDate: 24/08/2022
@Description: 汇总3年内合同历史 //每一年都要算(合同时间那一年维修金额 )//过去第三年、过去第二年、过去一年合同金额
@Version 1.0
*****************************************************************************************************/
global class SummaryThreeYearsContractBatch implements Database.Batchable<sObject>,Database.Stateful {
    public String query;
    public List < String > accountIdList;
    private BatchIF_Log__c iflog;
    public Date nowDt =Date.today();
    public String OCSM_Period_half;
    public String OCSM_Period;
    public Date sTime;
    public Date eTime;
    public Date firYEnd;
    public Date secYEnd;
 
    global SummaryThreeYearsContractBatch() {
        this.query = query;
    }
    global SummaryThreeYearsContractBatch(List < String > accountIdList) {
        this.accountIdList = accountIdList;
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        system.debug('执行start');
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'PushNotification';
        iflog.Log__c  = 'SummaryThreeYearsContractBatch start\n';
        iflog.ErrorLog__c = '';
        insert iflog;
 
        if(nowDt.month() >= 4){
            sTime = Date.newInstance(nowDt.year()-3,4,1);
            firYEnd = Date.newInstance(nowDt.year()-2,3,31);
            secYEnd = Date.newInstance(nowDt.year()-1,3,31);
            eTime = Date.newInstance(nowDt.year(),3,31);
            
        }else{
            sTime = Date.newInstance(nowDt.year()-4,4,1);
            firYEnd = Date.newInstance(nowDt.year()-3,3,31);
            secYEnd = Date.newInstance(nowDt.year()-2,3,31);
            eTime = Date.newInstance(nowDt.year()-1,3,31);
        }
        //查询三个财年内是否有维修合同的医院
        
        // query ='select Id, Hospital__c from Maintenance_Contract__c where ((Contract_End_Date__c>=:sTime and Contract_End_Date__c<=:eTime) or (Contract_Start_Date__c >=:sTime and Contract_Start_Date__c <=:eTime) or(Contract_Start_Date__c <:sTime and Contract_End_Date__c>:eTime)) and RecordType.Name!=\'多年保修合同\' ';
        query ='select Id from Account where Id IN( select  Hospital__c from Maintenance_Contract__c where ((Contract_End_Date__c>=:sTime and Contract_End_Date__c<=:eTime) or (Contract_Start_Date__c >=:sTime and Contract_Start_Date__c <=:eTime) or(Contract_Start_Date__c <:sTime and Contract_End_Date__c>:eTime)) and RecordType.Name!=\'多年保修合同\' and (Status__c=\'契約\' or Status__c=\'契約満了\'))';
        if (accountIdList != null && accountIdList.size() > 0) {
            query += ' AND Id IN :accountIdList ';
        }
        System.debug(LoggingLevel.INFO, '*** query: ' + query); 
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, list<Account> scope) {
        System.debug(LoggingLevel.INFO, '*** excute start: ' );
        if (nowDt.month() >= 4 && nowDt.month() <= 9) {
            OCSM_Period_half = '1H';
        }else{
            OCSM_Period_half = '2H';
        }
        if(nowDt.month() >= 1 && nowDt.month() <= 3){
            OCSM_Period = 'FY'+(nowDt.year());
        }else{
            OCSM_Period = 'FY'+(nowDt.year()+1);
        }
        System.debug(LoggingLevel.INFO, '*** the OCSM_Period_half__c: ' + OCSM_Period_half);
        System.debug(LoggingLevel.INFO, '*** the OCSM_Period: ' + OCSM_Period);
        //获取维修合同Id、Hospital
        List<Id> hosId = new List<Id>();
        List<Id> mainId = new List<Id>();
        // for (Maintenance_Contract__c mcc : scope) {
        //     hosId.add(mcc.Hospital__c);
        //     mainId.add(mcc.Id);
        // }
        for (Account acco : scope) {
            hosId.add(acco.Id);
        }
 
        for (Maintenance_Contract__c mcc :[select Id, Hospital__c from Maintenance_Contract__c 
                                            where ((Contract_End_Date__c>=:sTime and Contract_End_Date__c<=:eTime) 
                                            or (Contract_Start_Date__c >=:sTime and Contract_Start_Date__c <=:eTime) 
                                            or(Contract_Start_Date__c <:sTime and Contract_End_Date__c>:eTime)) 
                                            and RecordType.Name!='多年保修合同'
                                            and(Status__c='契約' or Status__c='契約満了')
                                            and Hospital__c IN:hosId] ) {
                mainId.add(mcc.Id);
        }
 
        //服务客户目标对象里 医院在scope里以及年份等于查询年份的
        List<Account_Service_Of_Target__c> asotList = [select Id,Account_HP__c 
                                                    from Account_Service_Of_Target__c 
                                                    where Account_HP__c in: hosId 
                                                    and OCSM_Period_half__c = :OCSM_Period_half
                                                    and OCSM_Period__c = :OCSM_Period];
        Map<Id,Account_Service_Of_Target__c> oldMap = new Map<Id,Account_Service_Of_Target__c>();
        for (Account_Service_Of_Target__c ast : asotList) {
            oldMap.put(ast.Account_HP__c,ast);
        }
        
        // //汇总医院维修合同数量
        AggregateResult[] aggResult = [SELECT Hospital__c,COUNT(Id)countalias
                                       FROM Maintenance_Contract__c WHERE Id IN: mainId 
                                       group by Hospital__c];   
        Map<Id,Decimal> countMap = new Map<Id,Decimal>();
 
        for (AggregateResult ass : aggResult ) {
            Id  assId= (Id)ass.get('Hospital__c');
            Decimal Defir = (Decimal)ass.get('countalias');
            countMap.put(assId, Defir);  
        }
 
        //过去1、2、3年维修金额
         Map<Id,Decimal> LastYearMap = new Map<Id,Decimal>();
         Map<Id,Decimal> Last2YearMap = new Map<Id,Decimal>();
         Map<Id,Decimal> Last3YearMap = new Map<Id,Decimal>();
 
         //过去1、2、3年签约合同数量
         Map<Id,Decimal> Last1YearCountMap = new Map<Id,Decimal>();
         Map<Id,Decimal> Last2YearCountMap = new Map<Id,Decimal>();
         Map<Id,Decimal> Last3YearCountMap = new Map<Id,Decimal>();
 
        //过去一年维修合同金额、签约合同数量(去年)
        AggregateResult[] LastYear = [SELECT Hospital__c,SUM(Contract_Amount__c)sumprices,COUNT(Id)countalias
                                       FROM Maintenance_Contract__c WHERE Id IN: mainId 
                                       AND Contract_Conclusion_Date__c>:secYEnd  //3.31
                                       AND Contract_Conclusion_Date__c<=:eTime   //3.31
                                       group by Hospital__c]; 
        for (AggregateResult ass : LastYear ) {
            Id  assId= (Id)ass.get('Hospital__c');
            Decimal Defir = (Decimal)ass.get('sumprices');
            Decimal MCount = (Decimal)ass.get('countalias');
            LastYearMap.put(assId, Defir); 
            Last1YearCountMap.put(assId,MCount); 
        }
 
        //过去两年维修合同金额、签约合同数量(前年)
        AggregateResult[] BeforeLastYear = [SELECT Hospital__c,SUM(Contract_Amount__c)sumprices,COUNT(Id)countalias
                                       FROM Maintenance_Contract__c WHERE Id IN: mainId 
                                       AND Contract_Conclusion_Date__c>:firYEnd   //3.31
                                       AND Contract_Conclusion_Date__c<=:secYEnd  //3.31
                                       group by Hospital__c]; 
        for (AggregateResult ass : BeforeLastYear ) {
            Id  assId= (Id)ass.get('Hospital__c');
            Decimal Defir = (Decimal)ass.get('sumprices');
            Decimal MCount = (Decimal)ass.get('countalias');
            Last2YearMap.put(assId, Defir);  
            Last2YearCountMap.put(assId,MCount); 
 
        }
 
        //过去三年年维修合同金额、签约合同数量(大前年)
        AggregateResult[] ThreeYearago = [SELECT Hospital__c,SUM(Contract_Amount__c)sumprices,COUNT(Id)countalias
                                       FROM Maintenance_Contract__c WHERE Id IN: mainId 
                                       AND Contract_Conclusion_Date__c>=:sTime  //4.1
                                       AND Contract_Conclusion_Date__c<=:firYEnd  //3.31
                                       group by Hospital__c];
        for (AggregateResult ass : ThreeYearago ) {
            Id  assId= (Id)ass.get('Hospital__c');
            Decimal Defir = (Decimal)ass.get('sumprices');
            Decimal MCount = (Decimal)ass.get('countalias');
            Last3YearMap.put(assId, Defir);  
            Last3YearCountMap.put(assId,MCount); 
 
        } 
 
        List<Account_Service_Of_Target__c> asList = new List<Account_Service_Of_Target__c>();
        for (Id mapId : countMap.keySet()) {
 
            Account_Service_Of_Target__c asItem = new Account_Service_Of_Target__c();
            asItem.Account_HP__c = mapId;
 
            //过去1、2、3年维修合同数量(按合同日计算)
            // asItem.Last_Years_Contract_Count__c = Last1YearCountMap.get(mapId);
            // asItem.Last_Two_Years_Contract_Count__c = Last2YearCountMap.get(mapId);
            // asItem.Last_Three_Year_Contract_Count__c = Last3YearCountMap.get(mapId);
 
            //过去第一年合同日是否有合同
            if(Last1YearCountMap.get(mapId)!=0 && Last1YearCountMap.get(mapId)!=null){
                asItem.IF_Last_Years_Contract__c = '1';
            }else{
                asItem.IF_Last_Years_Contract__c = '0';
            }
            //过去第二年合同日是否有合同
            if(Last2YearCountMap.get(mapId)!=0 && Last2YearCountMap.get(mapId)!=null){
                asItem.IF_Last_Two_Years_Contract__c = '1';
            }else{
                asItem.IF_Last_Two_Years_Contract__c = '0';
            }
            //过去第三年合同日是否有合同
            if(Last3YearCountMap.get(mapId)!=0 && Last3YearCountMap.get(mapId)!=null){
                asItem.IF_Last_Three_Years_Contract__c = '1';
            }else{
                asItem.IF_Last_Three_Years_Contract__c = '0';
            }
 
            asItem.OCSM_Period_half__c = OCSM_Period_half;
            asItem.OCSM_Period__c = OCSM_Period;
 
            //过去三年是否有签约合同
            asItem.Last_Three_Years_IF_Have_Contract__c = true;
            //过去三年维修合同数量(3个完整财年)
            asItem.Last_Three_Years_Contract_Count_del__c = countMap.get(mapId);
 
            //过去1、2、3年维修合同金额
            asItem.Last_Years_Contract_Amount__c = LastYearMap.get(mapId);
            asItem.Last_Two_Years_Contract_Amount__c = Last2YearMap.get(mapId);
            asItem.Last_Three_Years_Contract_Amount__c = Last3YearMap.get(mapId);
 
            if(oldMap.containsKey(asItem.Account_HP__c)){
                asItem.Id = oldMap.get(asItem.Account_HP__c).Id;
            }
            if(!asList.contains(asItem)){
                asList.add(asItem);
            }
 
        }
        System.debug(LoggingLevel.INFO, '*** asList: ' + asList);
        upsert asList;
 
 
        
    }
 
    global void finish(Database.BatchableContext BC) {
        iflog.Log__c += '\n SummaryThreeYearsContractBatch end';
        String tmp = iflog.ErrorLog__c;
        if (tmp.length() > 65000) {
            tmp = tmp.substring(0, 65000);
            tmp += ' ...have more lines...';
            iflog.ErrorLog__c = tmp;
        }
        update iflog;
    }
}