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
//  预测消费率计算对应型号的过去3年期间有合同的的全国合同修理金额合计
global class ComputeLTYRepairBatch implements Database.Batchable<sObject>,Database.Stateful {
    public String query;
    public String year;
    public Date timeOne ;
    public Date timeTwo;
    public Date timeThr;
    public Date timeFour;
    private BatchIF_Log__c iflog;
    public List<String> targetList= new List<String>();
    public List<String> target2List= new List<String>();
    public List<String> asslist= new List<String>();
    global List<String> alist = new List<String>();
    global ComputeLTYRepairBatch(Date timeOne) {
 
        this.query = query;
        this.timeOne = timeOne;
        // 查询时间=当年-3的4月1日——今年的4月1日
        this.timeTwo = date.newInstance(
                                        this.timeOne.year(),
                                        4,
                                        1
                                        );
        this.timeThr = date.newInstance(
                                        this.timeOne.year()-3,
                                        4,
                                        1
                                        );
        this.timeFour = this.timeTwo.addDays(1); 
    }
    global ComputeLTYRepairBatch() {
    }
 
    global ComputeLTYRepairBatch(String year) {
 
        this.query = query;
        this.year = year;
        this.timeTwo = date.newInstance(
                                        2023,
                                        4,
                                        1
                                        );
        this.timeThr = date.newInstance(
                                        2020,
                                        4,
                                        1
                                        );
        this.timeFour = this.timeTwo.addDays(1);
    }
    global ComputeLTYRepairBatch(List<String> targetList,integer count) {
 
        this.query = query;
        if (count ==1) {
            this.targetList = targetList;
        }else if(count ==2){
            this.target2List = targetList;
        }
        
        this.timeTwo = date.newInstance(
                                        2023,
                                        4,
                                        1
                                        );
        this.timeThr = date.newInstance(
                                        2020,
                                        4,
                                        1
                                        );
        this.timeFour = this.timeTwo.addDays(1);
    }
    global Database.QueryLocator start(Database.BatchableContext bc) {
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'PushNotification';
        iflog.Log__c  = 'EquipmentRenewMoleculeBatch start\n';
        iflog.ErrorLog__c = '';
        insert iflog;
        if (targetList.size()>0 && targetList !=null) {
            List<Maintenance_Contract_Asset__c> asss = [Select id,name,asset__c  from Maintenance_Contract_Asset__c 
                                            where Maintenance_Contract__c  in :targetList];
                for(Maintenance_Contract_Asset__c a1:asss){
                    asslist.add(a1.asset__c);
                }
        }
        
        //查询符合条件的所有设备
        query = 'select id,product2.Asset_Model_No__c,Category4__c ';
        query +=' from asset where IsCompetitorProduct = false AND ( AssetMark__c != \'耗材\' OR Product2.Family != \'ET\') and RecordTypeId !=\'01210000000kOPRAA2\' and  Return_Flag__c = false   ';
        if (String.isNotBlank(year)) {
            query += ' and Shipping_Year__c= :year';
        }
        if (asslist.size()>0 && asslist !=null) {
            query += ' and id in :asslist';
        }
        if (target2List.size()>0 && target2List !=null) {
            query += ' and id in :target2List';
        }
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, list<asset> assetList) {
             for(Asset a1: assetList){
                this.alist.add(a1.id);
            }
            // 分类计算,分别赋值,第四分类和产品型号
           List<Asset> assList1 = ComputeLRepair(1,assetList);
           List<Asset> assList2 = ComputeLRepair(2,assetList);             
        try {
 
            if (assList1.size()>0) {
                update assList1;
            }
            if (assList2.size()>0) {
                update assList2;
            }
 
            
        } catch (Exception e) {
            system.debug('err:::::' + e.getMessage());
        }
    }
 
    global void finish(Database.BatchableContext BC) {
        iflog.Log__c += '\nalist== '+alist+'\n';
        update iflog;
       Database.executeBatch(new ComputeLTYRepairBatch2(alist),1);
    }
 
    // type==1为第四分类  type==2为产品型号
    private  List<Asset> ComputeLRepair(Integer type,list<asset> assetList) {
            List<String> uniqueAssetModelNo = new List<String>();
            List<String> uniqueCategories = new List<String>();
            for (Asset ass: assetList) {
                // 统计第四分类和 产品分类的集合
                if (!uniqueCategories.contains(ass.Category4__c) && ass.Category4__c !=null && type==1) {
                    uniqueCategories.add(ass.Category4__c);
                }
                if (!uniqueAssetModelNo.contains(ass.product2.Asset_Model_No__c) &&ass.product2.Asset_Model_No__c !=null && type==2) {
                    uniqueAssetModelNo.add(ass.product2.Asset_Model_No__c);
                }
            }
            // 产品型号
            if (type==2) {   
                    List<Repair__c> ThreeyearList = new List<Repair__c>();
                    ThreeyearList = [
                    select
                    Repair_List_Price_formula__c,
                    Delivered_Product__c,
                    Delivered_Product__r.id,
                    Delivered_Product__r.product2.Asset_Model_No__c,
                    Maintenance_Contract__r.MonthSize__c,
                    Maintenance_Contract__r.id
                    from
                    Repair__c
                    where
                    // 满足产品型号
                    Delivered_Product__r.product2.Asset_Model_No__c in:uniqueAssetModelNo
                    //  时间区间
                    and Agreed_Date__c != null
                    and Agreed_Date__c <= :this.timeTwo
                    and Agreed_Date__c >= :this.timeThr
                    and Status2__c !='00.删除' 
                    and Status2__c !='00.取消'
                    //  过去三年有合同的
                    and ISPassHaveMc__c = true
                    ];
 
                List<Id> AssList = new List<Id>();
                List<Asset>   updateAss = new List<Asset>();
                Map<String,Decimal> NotoAssSize = new Map<String,Decimal>();
                // 过去3年
                Map<String, Decimal> ThreeYearPriceSumMap = new Map<String, Decimal>();
                for (Repair__c  Rpc : ThreeyearList) {
                    // 修理原价
                    if (ThreeYearPriceSumMap.containsKey(Rpc.Delivered_Product__r.product2.Asset_Model_No__c)) {
                        Decimal priceAssAll = ThreeYearPriceSumMap.get(Rpc.Delivered_Product__r.product2.Asset_Model_No__c);
                        priceAssAll += Rpc.Repair_List_Price_formula__c;
                        ThreeYearPriceSumMap.put(Rpc.Delivered_Product__r.product2.Asset_Model_No__c,priceAssAll);
                    }else{
                        ThreeYearPriceSumMap.put(Rpc.Delivered_Product__r.product2.Asset_Model_No__c,Rpc.Repair_List_Price_formula__c);
                    }
                    // 换算月数
                    // String index = Rpc.Delivered_Product__r.id+':'+Rpc.Maintenance_Contract__r.id;
                    // String[] parts = index.split(':');
                    // String part1 = parts[0]; // 第一部分 设备id
                    // String part2 = parts[1]; // 第二部分 维修合同号
                    
                    // if (NotoAssSize.containsKey(Rpc.Delivered_Product__r.product2.Asset_Model_No__c)) {
                    //     AssMCData assmc = NotoAssSize.get(Rpc.Delivered_Product__r.product2.Asset_Model_No__c);
                    //     if (!assmc.asaMC.contains(index)) {
                    //         Double monthSizeAll = assmc.MonthSize;
                    //         if(Rpc.Maintenance_Contract__r.MonthSize__c != -1){
                    //             monthSizeAll += Rpc.Maintenance_Contract__r.MonthSize__c;
                    //             assmc.MonthSize = monthSizeAll;
                    //             assmc.asaMC.add(index);
                    //             NotoAssSize.put(Rpc.Delivered_Product__r.product2.Asset_Model_No__c,assmc);
                    //         }  
                    //     }
                    // }else{
                    //     NotoAssSize.put(Rpc.Delivered_Product__r.product2.Asset_Model_No__c,new AssMCData(index,Rpc.Maintenance_Contract__r.MonthSize__c));
                    // }
 
                }
               
                for (Asset ass : assetList) {
                    // 按照产品型号,更新对应的设备字段(过去3年有合同的修理实绩和,对应型号的设备总数换算为12个月的件数)
                        if (ThreeYearPriceSumMap.get(ass.product2.Asset_Model_No__c)!=null) {
                            Asset assOne = new Asset();
                            assOne.id = ass.id;
                            assOne.Three_Years_HasMcAssetModelNo__c = ThreeYearPriceSumMap.get(ass.product2.Asset_Model_No__c);
                            updateAss.add(assOne);
                        }
                }
 
                return updateAss;
                //第四分类
            }else if (type==1) {
                List<Id> AssList = new List<Id>();
                List<Asset>   updateAss = new List<Asset>();
                Map<String,Decimal> NotoAssSize = new Map<String,Decimal>();
                // 过去3年
                Map<String, Decimal> ThreeYearPriceSumMap = new Map<String, Decimal>();
 
 
                    List<Repair__c> ThreeyearList = new List<Repair__c>();
                    ThreeyearList = [
                    select
                    Repair_List_Price_formula__c,
                    Delivered_Product__c,
                    Delivered_Product__r.id,
                    Delivered_Product__r.Category4__c,
                    Maintenance_Contract__r.MonthSize__c,
                    Maintenance_Contract__r.id
                    from
                    Repair__c
                    where
                    // 满足产品型号
                    Delivered_Product__r.Category4__c in:uniqueCategories
                    //  时间区间
                    and Agreed_Date__c != null
                    and Agreed_Date__c <= :this.timeTwo
                    and Agreed_Date__c >= :this.timeThr
                    and Status2__c !='00.删除' 
                    and Status2__c !='00.取消'
                    //  过去三年有合同的
                    and ISPassHaveMc__c = true
                    ];
 
 
                for (Repair__c  Rpc : ThreeyearList) {
                    // 修理原价
                    if (ThreeYearPriceSumMap.containsKey(Rpc.Delivered_Product__r.Category4__c)) {
                        Decimal priceAssAll = ThreeYearPriceSumMap.get(Rpc.Delivered_Product__r.Category4__c);
                        priceAssAll += Rpc.Repair_List_Price_formula__c;
                        ThreeYearPriceSumMap.put(Rpc.Delivered_Product__r.Category4__c,priceAssAll);
                    }else{
                        ThreeYearPriceSumMap.put(Rpc.Delivered_Product__r.Category4__c,Rpc.Repair_List_Price_formula__c);
                    }
                    // 换算月数
                    // String index = Rpc.Delivered_Product__r.id+':'+Rpc.Maintenance_Contract__r.id;
                    // String[] parts = index.split(':');
                    // String part1 = parts[0]; // 第一部分 设备id
                    // String part2 = parts[1]; // 第二部分 维修合同号
                    // if (NotoAssSize.containsKey(Rpc.Delivered_Product__r.Category4__c)) {
                    //     AssMCData assmc = NotoAssSize.get(Rpc.Delivered_Product__r.Category4__c);
                    //     if (!assmc.asaMC.contains(index)) {
                    //         Double monthSizeAll = assmc.MonthSize;
                    //         if(Rpc.Maintenance_Contract__r.MonthSize__c != -1){
                    //             monthSizeAll += Rpc.Maintenance_Contract__r.MonthSize__c;
                    //             assmc.MonthSize = monthSizeAll;
                    //             assmc.asaMC.add(index);
                    //             NotoAssSize.put(Rpc.Delivered_Product__r.Category4__c,assmc);
                    //         }  
                    //     }
                    // }else{
                    //     NotoAssSize.put(Rpc.Delivered_Product__r.Category4__c,new AssMCData(index,Rpc.Maintenance_Contract__r.MonthSize__c));
                    // }   
                }
                    for (Asset ass : assetList) {
                    // 按照第四分类,更新对应的设备字段(过去3年有合同的修理实绩和,对应型号的设备总数换算为12个月的件数)
                        if (ThreeYearPriceSumMap.get(ass.Category4__c)!=null) {
                            Asset assOne = new Asset();
                            assOne.id = ass.id;
                            assOne.Three_Years_HasMcCategory4__c = ThreeYearPriceSumMap.get(ass.Category4__c);
                            updateAss.add(assOne);
                        }
                    }
                return updateAss;
            }else{
                return null;
            }
    }
 
}