李彤
2023-03-16 cb4bae31a02af72fea30e1a57c1d498eabef5d01
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
// create by tcm 20211220 处理失单报告历史数据
// 2022-01-18 修改 从失单报告处理历史数据
global class LostCancelReportOppBatch implements Database.Batchable<sObject> {
 
    public List<String> IdList=new List<String> ();
 
    public Date start_date;
 
    public Date end_date;
 
    public Boolean update_all = true;
 
    global LostCancelReportOppBatch() {
    }
 
    // 通过询价Id查找
    global LostCancelReportOppBatch(List<string> IdList) {
        this.IdList = IdList;
    }
 
    // 通过日期查找
    global LostCancelReportOppBatch(String str_start, String str_end, Boolean if_upd_all) {
        this.start_date = String.isNotBlank(str_start) ? Date.parse(str_start) : null;
        this.end_date = String.isNotBlank(str_end) ? Date.parse(str_end) : null;
        this.update_all = if_upd_all != null ? if_upd_all : true;
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        // string query ='select id from Opportunity ';
        // if (IdList.size() > 0) {
        //     query += ' where Id in :IdList';
        // }
        // 2022-01-18 修改 从失单报告取值
        String query = 'SELECT Id, Opportunity__c FROM Lost_cancel_report__c WHERE Report_Status__c = \'批准\' AND LostType__c in (\'失单\', \'部分失单\') ';
        //20230215 lt DB202302247719 add , Opportunity__c
        if (start_date != null) {
            query += ' AND Submit_Day__c >= :start_date';
        }
        if (end_date != null) {
            query += ' AND Submit_Day__c >= :start_date';
        }
        if (IdList != null && IdList.size() > 0) {
            query += ' AND Opportunity__c in :IdList';
        }
        if (!update_all) {
            query += ' AND Opportunity__r.PCLLostBrands__c != null';
        }
        return Database.getQueryLocator(query);
    }
 
    // global void execute(Database.BatchableContext BC, List<Opportunity> scope) {
 
    //     List<String> oppIdList=new List<String>();
    //     for (Opportunity opp : scope) {
    //         oppIdList.add(opp.Id);
    //     }
 
    //     List<PCLLostProduct__c> pclLpList=[select id,Quantity__c, LostProduct__r.Name, LostProductMannual__c, PCLLostBrand__r.Lost_By_Company__c, PCLLostBrand__r.Lost_reason_main__c, PCLLostBrand__r.Lost_Reason_Sub__c, PCLLostBrand__r.Lost_By_Company_Mannual__c, PCLLostBrand__r.Agency__r.Name, PCLLostBrand__r.AgencyMannual__c, PCLLostBrand__r.Lost_cancel_report__r.LostType__c, PCLLostBrand__r.Lost_cancel_report__r.LostTotalAmount__c, PCLLostBrand__r.Lost_cancel_report__r.of_lost_system_processor__c,PCLLostBrand__r.Lost_cancel_report__r.Opportunity__c from PCLLostProduct__c where PCLLostBrand__r.Lost_cancel_report__r.Report_Status__c = '批准' and PCLLostBrand__r.Lost_cancel_report__r.Opportunity__c in : oppIdList and LostProduct__c!=null];
 
    //     List<Opportunity> oppList=new List<Opportunity>();
    //     for (String oppId : oppIdList) {
 
    //         Opportunity opp=new Opportunity();
    //         opp.Id=Id.valueOf(oppId); opp.PCLLostBrands__c=''; opp.Agencies__c=''; opp.Lost_Reason_Sub__c='';opp.of_lost_system_processor__c=0;    //Id ,品牌,经销商,原因次
    //         Integer i=0;
    //         List<String> ppList=new List<String>(); //品牌List
    //         List<String> jxsList=new List<String>(); //经销商List
 
 
    //         for (PCLLostProduct__c lp : pclLpList) {
    //             if (oppId==lp.PCLLostBrand__r.Lost_cancel_report__r.Opportunity__c) {
    //                 if (i==0) {
    //                     opp.LostTypeText__c=lp.PCLLostBrand__r.Lost_cancel_report__r.LostType__c;  //失单类型
    //                     opp.LostPrices__c=lp.PCLLostBrand__r.Lost_cancel_report__r.LostTotalAmount__c; //失单金额
    //                     // opp.of_lost_system_processor__c=lp.PCLLostBrand__r.Lost_cancel_report__r.of_lost_system_processor__c; //失单主机台数(总)
    //                     opp.Lost_reason_main__c=lp.PCLLostBrand__r.Lost_reason_main__c; //失单理由(主)
    //                 }
    //                 opp.of_lost_system_processor__c+=lp.Quantity__c;     //失单主机台数(累加)
    //                 if (lp.PCLLostBrand__r.Lost_Reason_Sub__c!=null) {
    //                     opp.Lost_Reason_Sub__c=lp.PCLLostBrand__r.Lost_Reason_Sub__c;   //失单理由(次)
    //                 }
 
    //                 // 失单产品123 start
    //                 if (opp.CompetitorProduct1__c==null) {
    //                     opp.CompetitorProduct1__c=lp.LostProduct__r.Name;
    //                 }else if (opp.CompetitorProduct2__c==null) {
    //                     opp.CompetitorProduct2__c=lp.LostProduct__r.Name;
    //                 }else if (opp.CompetitorProduct3__c==null) {
    //                     opp.CompetitorProduct3__c=lp.LostProduct__r.Name;
    //                 }
    //                 // 失单产品123 end
 
    //                 //失单品牌 start
    //                 if (!ppList.contains(lp.PCLLostBrand__r.Lost_By_Company__c)) {
    //                     ppList.add(lp.PCLLostBrand__r.Lost_By_Company__c);
    //                     if (ppList.size()==1) {
    //                         if (lp.PCLLostBrand__r.Lost_By_Company__c=='其他') {
    //                             opp.PCLLostBrands__c+=lp.PCLLostBrand__r.Lost_By_Company_Mannual__c;
    //                         }else {
    //                             opp.PCLLostBrands__c+=lp.PCLLostBrand__r.Lost_By_Company__c;
    //                         }
    //                     }else {
    //                         if (lp.PCLLostBrand__r.Lost_By_Company__c=='其他') {
    //                             opp.PCLLostBrands__c+=','+lp.PCLLostBrand__r.Lost_By_Company_Mannual__c;
    //                         }else {
    //                             opp.PCLLostBrands__c+=','+lp.PCLLostBrand__r.Lost_By_Company__c;
    //                         }
    //                     }
    //                 }
    //                 //失单品牌 end
 
    //                 //经销商 start
    //                 if (!jxsList.contains(lp.PCLLostBrand__r.Agency__r.Name)) {
    //                     jxsList.add(lp.PCLLostBrand__r.Agency__r.Name);
    //                     if (jxsList.size()==1) {
    //                         if (lp.PCLLostBrand__r.AgencyMannual__c==null) {
    //                             opp.Agencies__c+=lp.PCLLostBrand__r.Agency__r.Name;
    //                         }else {
    //                             opp.Agencies__c+=lp.PCLLostBrand__r.AgencyMannual__c;
    //                         }
    //                     }else {
    //                         if (lp.PCLLostBrand__r.AgencyMannual__c==null) {
    //                             opp.Agencies__c+=','+lp.PCLLostBrand__r.Agency__r.Name;
    //                         }else {
    //                             opp.Agencies__c+=','+lp.PCLLostBrand__r.AgencyMannual__c;
    //                         }
    //                     }
    //                 }
    //                 //经销商 end
    //                 i++;
    //             }
    //         }
    //         oppList.add(opp);
    //         system.debug('opp等于'+opp);
    //     }
    //     system.debug('oppList等于'+oppList);
    //     update oppList;
    // }
 
    // 2022-01-18 修改 从失单报告取值
    global void execute(Database.BatchableContext BC, List<Lost_cancel_report__c> scope) {
        
        // List<String> reportIdList=new List<String>();//20230215 lt DB202302247719 注释
        Set<String> oppIdList = new Set<String>();//20230215 lt DB202302247719 
 
        for (Lost_cancel_report__c report : scope) {
            // reportIdList.add(report.Id);   //20230215 lt DB202302247719 注释
            oppIdList.add(report.Opportunity__c);      //20230215 lt DB202302247719
        }
 
        //DB202302247719 注释  lt  注释原因:DB202302247719课题刷失单报告数据只刷失单金额
        // List<PCLLostProduct__c> pclLpList=[select 
        //                                     id,                                                                  // 失单型号id
        //                                     Quantity__c,                                                         // 失单数量
        //                                     LostProductName__c,                                                  // 失单对手型号
        //                                     LostProductMannual__c,                                               // 失单对手型号(手动)
        //                                     PCLLostBrand__r.Lost_By_Company__c,                                  // 失单品牌
        //                                     PCLLostBrand__r.Lost_reason_main__c,                                 // 失单原因(主)
        //                                     PCLLostBrand__r.Lost_Reason_Sub__c,                                  // 失单原因(次)
        //                                     PCLLostBrand__r.Lost_By_Company_Mannual__c,                          // 失单品牌(手动)
        //                                     PCLLostBrand__r.Agency__r.Name,                                      // 中标经销商
        //                                     PCLLostBrand__r.AgencyMannual__c,                                    // 中标经销商(手动)
        //                                     PCLLostBrand__r.Lost_cancel_report__r.LostType__c,                   // 失单类型
        //                                     PCLLostBrand__r.Lost_cancel_report__r.LostTotalAmount__c,            // 失单总金额
        //                                     PCLLostBrand__r.Lost_cancel_report__r.TotalAmountLost__c,              // 失单总金额(不含税)  //20230215 lt DB202302247719
        //                                     PCLLostBrand__r.Lost_cancel_report__r.of_lost_system_processor__c,   // 失单主机台数
        //                                     PCLLostBrand__r.Lost_cancel_report__r.Opportunity__c 
        //                                 from 
        //                                     PCLLostProduct__c 
        //                                 where 
        //                                 PCLLostBrand__r.Lost_cancel_report__r.Opportunity__c in :reportIdList];
                                        //20230215 lt DB202302247719   PCLLostBrand__r.Lost_cancel_report__c  ---update---  PCLLostBrand__r.Lost_cancel_report__r.Opportunity__c
 
        List<Lost_cancel_report__c> lcrList = [SELECT Id, Opportunity__c, TotalAmountLost__c
                                               FROM Lost_cancel_report__c
                                               WHERE Opportunity__c in :oppIdList];
        //DB202302247719 注释  lt  注释原因:DB202302247719课题刷失单报告数据只刷失单金额
 
        //DB202302247719 lt start
        Map<String, Opportunity> opp_map = new Map<String, Opportunity>();
 
        for (Lost_cancel_report__c lcr : lcrList){
            // 询价id
            String opp_id = lcr.Opportunity__c;
 
            // 询价
            Opportunity opp = opp_map.get(opp_id);
            if (opp == null) {
                opp = new Opportunity();
                opp.Id = opp_id;
                opp_map.put(opp_id, opp);
            }
 
            System.debug('---lt123---询价失单金额---'+opp.LostPrices__c);
            System.debug('---lt123---失单总金额,千元不含税---'+lcr.TotalAmountLost__c);
            if(lcr.TotalAmountLost__c != null){
                if(opp.LostPrices__c == null){
                    opp.LostPrices__c = 0;
                }
                opp.LostPrices__c += lcr.TotalAmountLost__c;  // 失单金额  
                System.debug('---lt123---累加后询价失单金额---'+opp.LostPrices__c);
            }
 
        }
        //DB202302247719 lt end
        
        //DB202302247719 注释  lt  注释原因:DB202302247719课题刷失单报告数据只刷失单金额
        // // 初始化保存参数
        // Map<String, Opportunity> opp_map = new Map<String, Opportunity>();
        // Map<String, String> prod_map = new Map<String, String>();
        // Map<String, String> agency_map = new Map<String, String>();
 
        // // 循环整理数据
        // for (PCLLostProduct__c lost : pclLpList) {
        //     // 判断是否可用数据
        //     // if (String.isBlank(lost.LostProductName__c) && String.isBlank(lost.LostProductMannual__c)) {
        //     //     continue;
        //     // }
        //     // 询价id
        //     String opp_id = lost.PCLLostBrand__r.Lost_cancel_report__r.Opportunity__c;
        //     // 询价
        //     Opportunity opp = opp_map.get(opp_id);
        //     if (opp == null) {
        //         opp = new Opportunity();
        //         opp.Id = opp_id;
        //         opp.of_lost_system_processor__c = 0;
        //         opp_map.put(opp_id, opp);
        //     }
 
        //     //20230215 lt DB202302247719 start
        //     System.debug('---lt123---询价失单金额---'+opp.LostPrices__c);
        //     System.debug('---lt123---失单总金额,千元不含税---'+lost.PCLLostBrand__r.Lost_cancel_report__r.TotalAmountLost__c);
 
        //     if(lost.PCLLostBrand__r.Lost_cancel_report__r.TotalAmountLost__c != null){
        //         if(opp.LostPrices__c == null){
        //             opp.LostPrices__c = 0;
        //         }
        //         opp.LostPrices__c += lost.PCLLostBrand__r.Lost_cancel_report__r.TotalAmountLost__c;  // 失单金额  
        //     }
        
        //     //DB202302247719 注释  lt  注释原因:DB202302247719课题刷失单报告数据只刷失单金额
        //     // 赋值
        //     // opp.LostTypeText__c = lost.PCLLostBrand__r.Lost_cancel_report__r.LostType__c;       // 失单类型
        //     // //opp.LostPrices__c = lost.PCLLostBrand__r.Lost_cancel_report__r.LostTotalAmount__c;  // 失单金额 
        //     // opp.Lost_reason_main__c = lost.PCLLostBrand__r.Lost_reason_main__c;                 // 失单原因(主)
        //     // opp.Lost_Reason_Sub__c = lost.PCLLostBrand__r.Lost_Reason_Sub__c;                   // 失单理由(次)
        //     // opp.of_lost_system_processor__c += lost.Quantity__c;                                 // 失单主机台数(累加)
            
 
        //     // 失单产品123 start
        //     // if (opp.CompetitorProduct1__c == null) {
        //     //     opp.CompetitorProduct1__c = lost_product_name(lost.LostProductName__c, lost.LostProductMannual__c);
        //     // }else if (opp.CompetitorProduct2__c == null) {
        //     //     opp.CompetitorProduct2__c = lost_product_name(lost.LostProductName__c, lost.LostProductMannual__c);
        //     // }else if (opp.CompetitorProduct3__c == null) {
        //     //     opp.CompetitorProduct3__c = lost_product_name(lost.LostProductName__c, lost.LostProductMannual__c);
        //     // }
        //     // 失单产品123 end
 
        //     // 失单品牌
        //     // opp.PCLLostBrands__c = lost_brand_name(opp.PCLLostBrands__c, lost.PCLLostBrand__r.Lost_By_Company__c, lost.PCLLostBrand__r.Lost_By_Company_Mannual__c);
 
        //     // 中标经销商
        //     // opp.Agencies__c = lost_agency_name(opp.Agencies__c, lost.PCLLostBrand__r.Agency__r.Name, lost.PCLLostBrand__r.AgencyMannual__c);
        //     //DB202302247719 注释  lt
        //     //20230215 lt DB202302247719 end
 
        // }
        //DB202302247719 注释  lt  注释原因:DB202302247719课题刷失单报告数据只刷失单金额
        System.debug(opp_map);
        StaticParameter.EscapeOppandStaTrigger = true; //20230215 lt DB202302247719
        update opp_map.values();
        StaticParameter.EscapeOppandStaTrigger = true; //20230215 lt DB202302247719
    }
 
    //DB202302247719 注释  lt  注释原因:DB202302247719课题刷失单报告数据只刷失单金额
    // 失单对手型号
    // private String lost_product_name(String lost_product, String lost_product_mannual) {
    //     if (String.isBlank(lost_product) && String.isNotBlank(lost_product_mannual)) {
    //         return lost_product_mannual;
    //     }
    //     return lost_product;
    // }
 
    // // 失单品牌
    // private String lost_brand_name(String lost_brand_in_opp, String lost_brand, String lost_brand_mannual) {
    //     // 初始化参数
    //     String plus = '';
    //     // 判断    
    //     // SWAG-CCC6F6 2022-04-22 ssm start
    //     // 询价上都只显示选项列表的值,不需要手动的值
    //     // if ('其他'.equals(lost_brand) && String.isNotBlank(lost_brand_mannual)) {
    //     //     plus = lost_brand_mannual;
    //     // } else {
    //     //     plus = lost_brand;
    //     // }
    //     plus = lost_brand;
    //     // SWAG-CCC6F6 2022-04-22 ssm end
    //     // 初始化返回值
    //     if (String.isBlank(lost_brand_in_opp)) {
    //         lost_brand_in_opp = '';
    //     }
    //     // 拼接
    //     if (String.isNotBlank(plus) && !lost_brand_in_opp.contains(plus)) {
    //         // 增加分隔标识
    //         if (String.isNotBlank(lost_brand_in_opp)) {
    //             lost_brand_in_opp += ',';
    //         }
    //         lost_brand_in_opp += plus;
    //     }
    //     return lost_brand_in_opp;
    // }
 
    // // 中标经销商
    // private String lost_agency_name(String lost_agency_in_opp, String lost_agency, String lost_agency_mannual) {
    //     // 初始化参数
    //     String plus = '';
    //     // 判断    
    //     // SWAG-CCC6F6 2022-04-22 ssm start
    //     // 询价上都只显示选项列表的值,不需要手动的值
    //     // if ('对手经销商'.equals(lost_agency) && String.isNotBlank(lost_agency_mannual)) {
    //     //     plus = lost_agency_mannual;
    //     // } else {
    //     //     plus = lost_agency;
    //     // }
    //     plus = lost_agency;
    //     // SWAG-CCC6F6 2022-04-22 ssm end
    //     // 初始化返回值
    //     if (String.isBlank(lost_agency_in_opp)) {
    //         lost_agency_in_opp = '';
    //     }
    //     // 拼接
    //     if (String.isNotBlank(plus) && !lost_agency_in_opp.contains(plus)) {
    //         // 增加分隔标识
    //         if (String.isNotBlank(lost_agency_in_opp)) {
    //             lost_agency_in_opp += ',';
    //         }
    //         lost_agency_in_opp += plus;
    //     }
    //     return lost_agency_in_opp;
    // }
    //DB202302247719 注释  lt  注释原因:DB202302247719课题刷失单报告数据只刷失单金额
 
    global void finish(Database.BatchableContext BC) {
 
    }
}
 
 
// List<String> IdList=new List<String>{'0061m000003fUz6'};
// Database.executeBatch(new LostCancelReportOppBatch(IdList));
 
// Database.executeBatch(new LostCancelReportOppBatch());