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
global class ConUpdateLastContractbatch implements Database.Batchable<sObject> {
    // 设定运行的维修合同的list<ID>
    public list<string> TestIDList;
 
    // 报错信息
    public String errorstr = '';
 
    global ConUpdateLastContractbatch() {
    }
    global ConUpdateLastContractbatch(List<string> TestIDList) {
        this.TestIDList = TestIDList;
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        string query =
            ' select id , previousTotalRepairAmount__c , currentTotalRepairAmount__c ,' +
            ' previousTotalContractAmount__c , currentTotalContractAmountVM__c , currentTotalGuaranteePrice__c '+
            ' , RecordType_DeveloperName__c ,Contract_Start_Date__c ' +  //tcm Contract_Start_Date__c
            '  from Maintenance_Contract__c '
            + ' where ';
        if (TestIDList != null && TestIDList.size() > 0) {
            query += ' id in: TestIDList';
        }else {
            query += ' Status__c in (\'契約\',\'契約満了\') ';
        }
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, list<Maintenance_Contract__c> scope) {
        List<String> mCidList = new List<String>();
        Map<String,String> IdMap=new Map<String,String>();
        for (Maintenance_Contract__c MC : scope) {
            mCidList.add(MC.Id);
            IdMap.put(MC.Id, MC.Id);
        }
        system.debug('scope等于'+scope);
 
        // 先取维修合同ID
        Map<String,Map<String,lastMContract> > lastMContractMap = new Map<String,Map<String,lastMContract> >();
        Map<String,Map<String,lastMContract> > lastMContractMap1 = new Map<String,Map<String,lastMContract> >();
 
        // 再取有上期合同的合同保有设备信息
        //tcm 20211130 start 更换Maintenance_Contract_No_F__c为Management_Code__c
        List<Maintenance_Contract_Asset__c> mCAList =
        [select Id,Maintenance_Contract__c,LastMContract__c,LastMContract__r.Contract_End_Date__c,
         LastMContract__r.Management_Code__c,/*LastMContract__r.Maintenance_Contract_No_F__c,*/
         Maintenance_Contract__r.Contract_Start_Date__c,LastMContract__r.RecordType_DeveloperName__c
         from Maintenance_Contract_Asset__c
         where Maintenance_Contract__r.Status__c in ('契約','契約満了')
         and Maintenance_Contract__c in : mCidList
         and LastMContract__c != null
         order by Asset__c,Maintenance_Contract__r.Contract_End_Date__c];
 
        for (Maintenance_Contract_Asset__c mca : mCAList) {
            //判断 如果上一期维修合同为 多年保修合同 的话 则不放入map进行更新 LJPH-BUU3E3 update by rentx 2020-11-03 start
            // if (String.isNotBlank(mca.LastMContract__c) && mca.LastMContract__r.RecordType_DeveloperName__c!= 'VM_Contract'&&mca.LastMContract__r.Contract_End_Date__c!=null) {
            if (String.isNotBlank(mca.LastMContract__c) &&mca.LastMContract__r.Contract_End_Date__c!=null) {
                lastMContract lM = new lastMContract(mca.LastMContract__c,mca.LastMContract__r.Management_Code__c/*Maintenance_Contract_No_F__c*/,mca.LastMContract__r.Contract_End_Date__c,1);
                // 判断空白期
                system.debug('合同开始日等于:'+mca.Maintenance_Contract__r.Contract_Start_Date__c+';合同结束日等于:'+mca.LastMContract__r.Contract_End_Date__c);
                if (mca.Maintenance_Contract__r.Contract_Start_Date__c.daysbetween(mca.LastMContract__r.Contract_End_Date__c) > -366 &&mca.Maintenance_Contract__r.Contract_Start_Date__c.daysbetween(mca.LastMContract__r.Contract_End_Date__c) < 366) {
                    // 数量
                    if (lastMContractMap.containsKey(mca.Maintenance_Contract__c)) {
                        if (lastMContractMap.get(mca.Maintenance_Contract__c).containsKey(mca.LastMContract__c)) {
                            lastMContractMap.get(mca.Maintenance_Contract__c).get(mca.LastMContract__c).count++;
                        }else {
                            lastMContractMap.get(mca.Maintenance_Contract__c).put(mca.LastMContract__c, lM);
                        }
                    }else{
                        Map<String,lastMContract> lastMContracttemp = new Map<String,lastMContract>();
                        lastMContracttemp.put(mca.LastMContract__c, lM);
                        lastMContractMap.put(mca.Maintenance_Contract__c, lastMContracttemp);
                    }
                } else {
                    // 数量
                    if (lastMContractMap1.containsKey(mca.Maintenance_Contract__c)) {
                        if (lastMContractMap1.get(mca.Maintenance_Contract__c).containsKey(mca.LastMContract__c)) {
                            lastMContractMap1.get(mca.Maintenance_Contract__c).get(mca.LastMContract__c).count++;
                        }else {
                            lastMContractMap1.get(mca.Maintenance_Contract__c).put(mca.LastMContract__c, lM);
                        }
                    }else{
                        Map<String,lastMContract> lastMContracttemp1 = new Map<String,lastMContract>();
                        lastMContracttemp1.put(mca.LastMContract__c, lM);
                        lastMContractMap1.put(mca.Maintenance_Contract__c, lastMContracttemp1);
                    }
                }
            }else {
                Map<String,lastMContract> lastMContracttemp2 = new Map<String,lastMContract>();
                lastMContractMap.put(mca.Maintenance_Contract__c, lastMContracttemp2);
            }
        }
        system.debug('lastMContractMap等于+'+lastMContractMap);
        system.debug('lastMContractMap1等于+'+lastMContractMap1);
 
        Map<String,List<lastMContract> > mapResult = new Map<String,List<lastMContract> >();
 
        for (String str : mCidList) {
            List<lastMContract> listResulttemp = new List<lastMContract>();
            List<lastMContract> listResulttemp1 = new List<lastMContract>();
            List<lastMContract> listResult = new List<lastMContract>();
            List<lastMContract> listResult1 = new List<lastMContract>();
 
            if (lastMContractMap.containsKey(str)) {
                // 空白期
                listResulttemp = lastMContractMap.get(str).values();
                listResulttemp.sort();
                for (Integer i = 0; i < listResulttemp.size(); i++) {
                    if (listResulttemp.size() >= i+1) {
                        listResult.add(listResulttemp[i]);
                    }
                }
            }
            if (lastMContractMap1.containsKey(str)&&listResult.size()<=5) {
                // 非空白期
                listResulttemp1 = lastMContractMap1.get(str).values();
                listResulttemp1.sort();
                System.debug('listResulttemp1size等于'+listResulttemp1.size());
                for (Integer i = 0; i < listResulttemp1.size(); i++) {
                    // if (listResulttemp1.size() >= i+1) {
                        listResult1.add(listResulttemp1[i]);
                    //     system.debug('iiiiiiiiiiii'+i);
                    // }else {
                    //     listResult1.add(new lastMContract(null, null, null, null));
                    // }
                }
            }
 
            listResult.addAll(listResult1);
            system.debug('listResultsize1等于'+listResult.size());
            system.debug('listResultsList等于'+listResult);
 
            // 如果不足5个合同,自动添加剩余的合同为null
            if (listResult.size()<5) {
                Integer num=5-listResult.size();
                for (Integer i = 0; i < num; i++) {
                    listResult.add(new lastMContract(null, null, null, null));
                }
                mapResult.put(str, listResult);
            }else {
                mapResult.put(str, listResult);
            }
            system.debug('listResultsizeend等于'+listResult.size());
        }
        system.debug('mapResult等于+'+mapResult);
 
        List<Maintenance_Contract__c> upMCList = new List<Maintenance_Contract__c>();
        // 五个合同赋值
        for (String str : mCidList) {
            if (mapResult.containsKey(str)) {
                Maintenance_Contract__c upMC = new Maintenance_Contract__c();
                upMC.Id = str;
                upMC.LastMContract1__c = mapResult.get(str)[0].contractId;
                upMC.LastMContract1_NO__c = mapResult.get(str)[0].contractNo;
                upMC.LastMContract1_ConCount__c = mapResult.get(str)[0].count;
 
                upMC.LastMContract2__c = mapResult.get(str)[1].contractId;
                upMC.LastMContract2_NO__c = mapResult.get(str)[1].contractNo;
                upMC.LastMContract2_ConCount__c = mapResult.get(str)[1].count;
 
                upMC.LastMContract3__c = mapResult.get(str)[2].contractId;
                upMC.LastMContract3_NO__c = mapResult.get(str)[2].contractNo;
                upMC.LastMContract3_ConCount__c = mapResult.get(str)[2].count;
 
                upMC.LastMContract4__c = mapResult.get(str)[3].contractId;
                upMC.LastMContract4_NO__c = mapResult.get(str)[3].contractNo;
                upMC.LastMContract4_ConCount__c = mapResult.get(str)[3].count;
 
                upMC.LastMContract5__c = mapResult.get(str)[4].contractId;
                upMC.LastMContract5_NO__c = mapResult.get(str)[4].contractNo;
                upMC.LastMContract5_ConCount__c = mapResult.get(str)[4].count;
                upMCList.add(upMC);
            }
            // else {
            //     Maintenance_Contract__c upMC = new Maintenance_Contract__c();
            //     upMC.Id = str;
            //     upMC.LastMContract1__c = null;
            //     upMC.LastMContract1_NO__c = null;
            //     upMC.LastMContract1_ConCount__c =  null;
 
            //     upMC.LastMContract2__c = null;
            //     upMC.LastMContract2_NO__c = null;
            //     upMC.LastMContract2_ConCount__c =  null;
 
            //     upMC.LastMContract3__c = null;
            //     upMC.LastMContract3_NO__c = null;
            //     upMC.LastMContract3_ConCount__c =  null;
 
            //     upMC.LastMContract4__c = null;
            //     upMC.LastMContract4_NO__c = null;
            //     upMC.LastMContract4_ConCount__c = null;
 
            //     upMC.LastMContract5__c = null;
            //     upMC.LastMContract5_NO__c = null;
            //     upMC.LastMContract5_ConCount__c =  null;
            //     upMCList.add(upMC);
            // }
        }
        system.debug('upMCList等于+'+upMCList);
 
        Database.SaveResult[] lsr = Database.update(upMCList, false);
        String errorMsg = '';
        for (Integer sIdx = 0; sIdx < lsr.size(); sIdx++) {
            Database.SaveResult sr = lsr[sIdx];
            if (!sr.isSuccess()) {
                Database.Error emsg = sr.getErrors()[0];
                errorstr += 'ERROR ' + upMCList[sIdx].get('Id') + ' 维修合同:' + emsg + '\n';
            }
        }
    }
 
 
    global void finish(Database.BatchableContext BC) {
 
        BatchIF_Log__c batchIfLog = new BatchIF_Log__c();
        batchIfLog.Type__c = 'MCAssetUpdate';
        batchIfLog.Log__c = '\nConUpdateLastContractbatchend';
        if (errorstr.length() > 60000) {
            batchIfLog.ErrorLog__c = errorstr.substring(0, 60000);
        } else {
            batchIfLog.ErrorLog__c = errorstr.substring(0, errorstr.length());
        }
        insert batchIfLog;
 
    }
 
    // 续签合同信息 class
    public class lastMContract implements Comparable {
        public Date endDate;
        public Integer count;
        public String contractId;
        public String contractNo;
        // tcm start
        public Boolean ayearcontract;
        // tcm end
        public lastMContract(String contractId,String contractNo,Date endDate, Integer count) {
            this.endDate = endDate;
            this.count = count;
            this.contractId = contractId;
            this.contractNo = contractNo;
        }
        // 排序
        // tcm start
        public Integer compareTo(Object compareTo) {
            lastMContract compareToesd = (lastMContract)compareTo;
            Integer returnValue = 0;
            // 判断数量
            if (count  > compareToesd.count) {
                returnValue = -1;
            } else if (count  < compareToesd.count) {
                returnValue = 1;
            } else {
                if (endDate  > compareToesd.endDate ) {
                    returnValue = 1;
                } else {
                    returnValue = -1;
                }
            }
            return returnValue;
        }
        // tcm end
    }
 
    
}