binxie
2024-01-16 1b08402678deb31bba4a347bfd388eba8360cbc1
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
public without sharing class SSOpportunity2Batch implements Database.Batchable<SObject>, Database.Stateful {
    public static Integer FIELDMAX = 200;
    private final List<Id> TEST_ID = null;
    private BatchIF_Log__c iflog;
 
    //带参拍照,根据拍照
    public SSOpportunity2Batch(List<Id> testId) {
        TEST_ID = testId;
        System.debug('TEST_ID=' + TEST_ID);
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'SSOpportunity2Batch';
        iflog.Log__c  = 'SSOpportunity2Batch start\n';
        iflog.ErrorLog__c = '';
        insert iflog;
    }
    
    public static String makeSql(SS_Batch_Column_Mapping__c setting, Set<String> apiTempSet) {
        apiTempSet.add('Id');
        for (Integer i = 1; i <= FIELDMAX; i++) {
            String fromColumn = 'From_Column_' + ('00' + i).right(3) + '__c';
            String apiStr = String.valueOf(setting.get(fromColumn));
            if (String.isBlank(apiStr) == false && apiTempSet.contains(apiStr) == false) {
                apiTempSet.add(apiStr);
            }
        }
        String soql = 'Select ' + String.join(new List<String>(apiTempSet), ',') + ',Opportunity__c,Opportunity__r.Opportunity_No__c ' + ' from ' + setting.Name;
        return soql;
    }
 
    //搜索拍照表的Sql方法
    public static String makeSqlSS(SS_Batch_Column_Mapping__c setting, Set<String> apiTempSet) {
        apiTempSet.add('Id');
        for (Integer i = 1; i <= FIELDMAX; i++) {
            String fromColumn = 'SS_Column_' + ('00' + i).right(3) + '__c';
            String apiStr = String.valueOf(setting.get(fromColumn));
            if (String.isBlank(apiStr) == false && apiTempSet.contains(apiStr) == false) {
                //不搜索 “自定义设置”中 ,询价2自己本身的字段
                if (apiStr.startsWith('Opportunity2__r.')){
                    break;
                }
                apiTempSet.add(apiStr);
            }
        }
        String soql = 'Select ' + String.join(new List<String>(apiTempSet), ',')+ ',Opportunity_No__c ' + ' from ' + setting.SS_TableName__c;
        return soql;
    }
 
    public static Object getValue(SObject sobj, String field) {
        List<String> fieldPathList = field.split('\\.');
        Object rtn = null;
        for (Integer i = 0; i < fieldPathList.size(); i++) {
            String fieldPath = fieldPathList[i];
            if (i == fieldPathList.size() - 1) {
                rtn = sobj.get(fieldPath);
                System.debug(rtn + '= sobj.get(' + fieldPath + ')');
            } else {
                sobj = sobj.getSObject(fieldPath);
                if (sobj == null) {
                    break;
                }
                System.debug(sobj + '= sobj.getSObject(' + fieldPath + ')');
            }
        }
        return rtn;
    }
 
    //start方法里  查询<询价2>的数据(自定义设置左侧字段)
    public Database.QueryLocator start(Database.BatchableContext BC) {
        SS_Batch_Column_Mapping__c oppMapping = SS_Batch_Column_Mapping__c.getValues('Opportunity2__c');
        Set<String> apiTempSet = new Set<String>();
        apiTempSet.add('StageName__c');
        for (Integer i = 1; i <= FIELDMAX; i++) {
            String lpadI = ('00' + i).right(3);
            String fromColumn = 'From_Column_' + lpadI + '__c';
            String apiStr = String.valueOf(oppMapping.get(fromColumn));
            if (String.isBlank(apiStr) == false) {
                String ssColumn = 'SS_Column_' + lpadI + '__c';
                String ssApiStr = String.valueOf(oppMapping.get(ssColumn));
                if (ssApiStr.startsWith('Opportunity2__r.')) {
                    // 商談の項目を更新、変更があるかどうか判断するため、元データも取得
                    //为了判断商谈的项目是否有更新、变更,也要获取原始数据
                    System.debug('ssApiStr=' + ssApiStr + ', substring(16)=' + ssApiStr.substring(16));
                    apiTempSet.add(ssApiStr.substring(16));
                }
            }
        }
        String soql = makeSql(oppMapping, apiTempSet);
        if (TEST_ID <> null) {
            soql += ' where Id IN: TEST_ID';
        } else {
            // CHAN-AUE3TB Batch SSOpportunity的执行数据筛选
            // 20230414 DB202304001752 start
            Date nowDay = Date.today();
            nowDay = nowDay.addDays(-15);
            Integer iyear = nowDay.year()-1;
            if (nowDay.month() < 4) {
                iyear -= 1;
            }
            // Date thisYd = Date.valueOf(iyear + '-4-1');
            Date thisYd = Date.valueOf(iyear + '-10-1');
            // 20230414 DB202304001752 end 
            soql += ' where StageName__c = \'询价\' or StageName__c = \'注残\' or StageName__c = \'发货\' or ((StageName__c = \'完毕\' or StageName__c = \'取消\' or StageName__c = \'失单\') and Opportunity__r.SFDCLast_Process_Date__c >= :thisYd)';
            soql += '  or (StageName__c = \'完毕\' and Opportunity__r.Shipping_Finished_Day_Func__c >= :thisYd)';       
        }
        System.debug('soql=' + soql);
        return Database.getQueryLocator(soql);
    }
    
    public void execute(Database.BatchableContext BC, List<Opportunity2__c> oppList) {
        System.debug('start里初始List-oppList=' + oppList);
 
        //oppNoList 询价2 对应的询价的询价编码集合
        List<String> oppNoList = new List<String>();
        for (Opportunity2__c opp2 : oppList){
            oppNoList.add(opp2.Opportunity__r.Opportunity_No__c);
        }
        System.debug('询价编码List-oppNoList=' + oppNoList);
 
        //查询SS拍照表  start
        SS_Batch_Column_Mapping__c oppMapping1 = SS_Batch_Column_Mapping__c.getValues('Opportunity2__c');
        Set<String> apiTempSet = new Set<String>();
        apiTempSet.add('StageName__c');
        for (Integer i = 1; i <= FIELDMAX; i++) {
            String lpadI = ('00' + i).right(3);
            String fromColumn = 'From_Column_' + lpadI + '__c';
            String apiStr = String.valueOf(oppMapping1.get(fromColumn));
            if (String.isBlank(apiStr) == false) {
                String ssColumn = 'SS_Column_' + lpadI + '__c';
                String ssApiStr = String.valueOf(oppMapping1.get(ssColumn));
                if (ssApiStr.startsWith('Opportunity2__r.')) {
                    // 商談の項目を更新、変更があるかどうか判断するため、元データも取得
                    //为了判断商谈的项目是否有更新、变更,也要获取原始数据
                    System.debug('ssApiStr=' + ssApiStr + ', substring(16)=' + ssApiStr.substring(16));
                    apiTempSet.add(ssApiStr.substring(16));
                }
            }
        }
        
        String soql = makeSqlSS(oppMapping1, apiTempSet);
 
        //判断条件,SS拍照表的“考核月”字段在当月(thisto当月1日,thisto1下月1日)
        // 20240101 LHJ 修复年度切换 Start
        // Date nowDay = Date.today();
        // Integer toyear = nowDay.year();
        // Integer toMon = nowDay.month() - 1;
        // Integer toMon1 = nowDay.month();
        // Date thisto = Date.valueOf(toyear + '-' + toMon +'-1');
        // Date thisto1 = Date.valueOf(toyear + '-' + toMon1 +'-1');
        Date nowDay = Date.today();
        Integer toyear ;
        Integer toyear1 = nowDay.year();
        Integer toMon ;
        Integer toMon1 = nowDay.month();
 
        if(nowDay.month() == 1) {
            toyear = nowDay.year() - 1;
            toMon = 12;
        } else {
            toyear = nowDay.year();
            toMon = nowDay.month() - 1;
        }
        Date thisto = Date.valueOf(toyear + '-' + toMon +'-1');
        Date thisto1 = Date.valueOf(toyear1 + '-' + toMon1 +'-1');
 
        // 20240101 LHJ 修复年度切换 End
 
        soql += ' where Evaluation_date__c >= :thisto And Evaluation_date__c < :thisto1 and Opportunity_No__c in :oppNoList';
 
        System.debug('SS+soql=' + soql);
        List<SObject> insSSOppList1 = Database.query(soql);
        System.debug('SS+insSSOppList1=' + insSSOppList1);
        //查询SS拍照表  end
 
        //Map(询价编码,拍照表)  根据询价编码对应
        Map<String,SObject> SSnumMap = new Map<String,SObject>();
        for(SObject SS : insSSOppList1){
            SSnumMap.put(String.ValueOf(SS.get('Opportunity_No__c')),SS);
        }
        System.debug('SS+SSnumMap=' + SSnumMap);
 
        List<Opportunity2__c> updSelfList = new List<Opportunity2__c>();
        List<SObject> insSSOppList = new List<SObject>();
        
        //oppMapping(自定义设置的all),ssOppType(拍照表名),insSSOpp(Map拿到的SS拍照表数据)
        SS_Batch_Column_Mapping__c oppMapping = SS_Batch_Column_Mapping__c.getValues('Opportunity2__c');
        Schema.SObjectType ssOppType = Schema.getGlobalDescribe().get(String.valueOf(oppMapping.get('SS_TableName__c')));
        for (Opportunity2__c opp2 : oppList) {
            Boolean updSelfFlg = false;
            SObject insSSOpp = SSnumMap.get(opp2.Opportunity__r.Opportunity_No__c);
            for (Integer i = 1; i <= FIELDMAX; i++) {
                String lpadI = ('00' + i).right(3);
                String fromColumn = 'From_Column_' + lpadI + '__c';
                String apiStr = String.valueOf(oppMapping.get(fromColumn));
                if (String.isBlank(apiStr) == false) {
                    String ssColumn = 'SS_Column_' + lpadI + '__c';
                    String ssApiStr = String.valueOf(oppMapping.get(ssColumn));
                    if (ssApiStr.startsWith('Opportunity2__r.')) {
                        // 商談の項目を更新、変更があるかどうか判断する
                        if(apiStr.startsWith('Opportunity__r.')){
                            if(apiStr == 'Opportunity__r.ImportDemonstration_state__c'){
                                if (opp2.get(ssApiStr.substring(16)) != opp2.Opportunity__r.ImportDemonstration_state__c){
                                    opp2.put(ssApiStr.substring(16), opp2.Opportunity__r.ImportDemonstration_state__c);
                                    updSelfFlg = true;
                                }
                            }else 
                            if(apiStr == 'Opportunity__r.Amount_Without_Tax_Thousand_F__c'){
                                if (opp2.get(ssApiStr.substring(16)) != opp2.Opportunity__r.Amount_Without_Tax_Thousand_F__c){
                                    opp2.put(ssApiStr.substring(16), opp2.Opportunity__r.Amount_Without_Tax_Thousand_F__c);
                                    updSelfFlg = true;
                                }
                            }
                        } else
                        if (opp2.get(ssApiStr.substring(16)) != opp2.get(apiStr)) {
                            try {
                                opp2.put(ssApiStr.substring(16), opp2.get(apiStr));
                                updSelfFlg = true;
                            } catch (Exception e) {
                                iflog.ErrorLog__c = 'ERROR [' + apiStr + '] => [' + ssApiStr + ']' + e.getMessage() + '\n';
                            }
                        }
                    } else {
                        try {
                            insSSOpp.put(ssApiStr, getValue(opp2, apiStr));
                        } catch (Exception e) {
                            iflog.ErrorLog__c = 'ERROR1 ' + String.valueOf(oppMapping.get('SS_TableName__c')) + ' [' + apiStr + '] => [' + ssApiStr + ']' + e.getMessage() + '\n';
                        }
                    }
                    // try {
                    //     insSSOpp.put(ssApiStr, getValue(opp2, apiStr));
                    // } catch (Exception e) {
                    //     iflog.ErrorLog__c = 'ERROR2 ' + String.valueOf(oppMapping.get('SS_TableName__c')) + ' [' + apiStr + '] => [' + ssApiStr + ']' + e.getMessage() + '\n';
                    // }
                }
            }
            if (updSelfFlg) {
                updSelfList.add(opp2);
            }
 
            if (opp2.StageName__c == '询价' || opp2.StageName__c == '注残' || opp2.StageName__c == '发货' || opp2.StageName__c == '完毕' || opp2.StageName__c == '取消' || opp2.StageName__c == '失单') {
                if(insSSOpp != null){
                    insSSOppList.add(insSSOpp);
                }
            }
        }
 
        if (Oly_TriggerHandler.isBypassed('PowerBIBaseHandler') == false) {
            Oly_TriggerHandler.bypass('PowerBIBaseHandler');
        }
 
        // エラーをlogに書き出す
        if (updSelfList.size() > 0) {
            System.debug('updSelfList:' + updSelfList);
            // SWAG-CE55BX 预测优化 start
            StaticParameter.EscapeOppandStaTrigger = true;
            // SWAG-CE55BX 预测优化 end
            Database.SaveResult[] lsr = Database.update(updSelfList, false);
            List<Opportunity2__c> reuseUpdateList = new List<Opportunity2__c>();
            for (Integer tIdx = 0; tIdx < lsr.size(); tIdx++) {
                Database.SaveResult sr = lsr[tIdx];
                System.debug('sr.isSuccess:' + sr.isSuccess());
                if (!sr.isSuccess()) {
                    //wangweipeng   20210618   获取没有update的数据
                    reuseUpdateList.add(updSelfList[tIdx]);
                }
            }
 
            //wangweipeng  20210618  如果update失败,重复更新,最多3次,如果失败记录日志  start 
            if(reuseUpdateList != null && reuseUpdateList.size() > 0){
                reuseUpdate(reuseUpdateList,1);
            }
            //wangweipeng  20210618  如果update失败,重复更新,最多3次,如果失败记录日志  end 
        }
        System.debug('更新List==insSSOppList=' + insSSOppList);
        System.debug('更新List==insSSOppList.size()=' + insSSOppList.size());
        if(insSSOppList.size() > 0 ){
            Database.SaveResult[] lsr2 = Database.update(insSSOppList, false);
            for (Integer tIdx = 0; tIdx < lsr2.size(); tIdx++) {
                Database.SaveResult sr = lsr2[tIdx];
                if (!sr.isSuccess()) {
                    Database.Error emsg = sr.getErrors()[0];
                    //update by rentx 2020-10-27 start
                        if (!String.valueOf(emsg.getStatusCode()).contains('INACTIVE_OWNER_OR_USER')) {
                            iflog.ErrorLog__c += 'ERROR ' + insSSOppList[tIdx].get('Opportunity_No__c') + ' SS_opp:' + emsg + '\n';
                        }
                    //update by rentx 2020-10-27 end                 
                    // iflog.ErrorLog__c += 'ERROR ' + insSSOppList[tIdx].get('Opportunity_No__c') + ' SS_opp:' + emsg + '\n';
                }
            }
        }
        
    }
    
    public void finish(Database.BatchableContext BC) {
 
        // if (Test.isRunningTest() == false && TEST_ID == null) {
        //     Database.executeBatch(new SSBackorderBatch(null), 100);
        // }
 
        iflog.Log__c += '\nSSOpportunity2Batch 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;
    }
 
    //wangweipeng  20210618  如果update失败,重复更新,最多3次,如果失败记录日志  start 
    //参数:updSelfList   要重新插入的数据     indexUpdate   第几次插入
    public void reuseUpdate(List<Opportunity2__c> updSelfList,Integer indexUpdate){
        System.debug('这是此方法走的第:'+indexUpdate+' 次了。');
        // SWAG-CE55BX 预测优化 start
        StaticParameter.EscapeOppandStaTrigger = true;
        // SWAG-CE55BX 预测优化 end
        Database.SaveResult[] lsr = Database.update(updSelfList, false);
        List<Opportunity2__c> reuseUpdateList = new List<Opportunity2__c>();
        for (Integer tIdx = 0; tIdx < lsr.size(); tIdx++) {
            Database.SaveResult sr = lsr[tIdx];
            if (!sr.isSuccess()) {
                if(indexUpdate >= 3){
                    Database.Error emsg = sr.getErrors()[0];
                    //update by Gzw 2020-10-30 start
                    if (!String.valueOf(emsg.getStatusCode()).contains('INACTIVE_OWNER_OR_USER')) {
                        iflog.ErrorLog__c += 'ERROR ' + updSelfList[tIdx].Id + ' Opportunity2__c:' + emsg + '\n';
                    }
                        // iflog.ErrorLog__c += 'ERROR ' + updSelfList[tIdx].Opportunity_No__c + ' Opportunity2__c:' + emsg + '\n';
                    //update by Gzw 2020-10-30 end
                }else{
                    //获取没有update的数据
                    reuseUpdateList.add(updSelfList[tIdx]);
                }
            }
        }
        if(indexUpdate < 3){
            indexUpdate ++;
            if(reuseUpdateList != null && reuseUpdateList.size() > 0){
                reuseUpdate(reuseUpdateList,indexUpdate);
            }
        }
    }
    //wangweipeng  20210618  如果update失败,重复更新,最多3次,如果失败记录日志  end
}