buli
2022-05-14 ead4df22dca33a867279471821ca675f91dec760
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
354
355
356
357
358
359
360
361
362
363
364
365
366
global class SBG005WebService {
    public class SBG005WebServiceException extends Exception {}
    private final Id rowData_Id;
 
    global class GeDatas {
        webservice NFMUtil.Monitoring Monitoring;
        webservice SBG005WebService.GeData[] GeData;
    }
    global class GeData {
        webservice String  ProductCode;
        webservice String  Currency_x;
        webservice String  ProductSegment;
        webservice String  TradeType;
        webservice String  MachineParts;
        webservice String  SalesChannel;
        webservice Decimal SalesPrice;
        webservice String  DealerDistinguish;
        webservice String  EffectiveDateFrom;
        webservice String  EffectiveDateTo;
        webservice String  PurposeOfAdvice;
        webservice String  Other1;
        webservice String  Other2;
        webservice String  Other3;
    }
    webservice static void SBG005(GeDatas geDatas) {
        if (geDatas == null) {
            return;
        }
        NFMUtil.Monitoring Monitoring = GeDatas.Monitoring;
        if (Monitoring == null) {
            return;
        }
        BatchIF_Log__c rowData = NFMUtil.saveRowData(Monitoring, 'SBG005', GeDatas.GeData);
        if (GeDatas.GeData == null || GeDatas.GeData.size() == 0) {
            return;
        }
        rowData.retry_cnt__c = 1;
        update rowData;
        //SBG005WebService u = new SBG005WebService(rowData.Id, 0, GeDatas.GeData.size());
        //Database.executeBatch(u, 1);
        if (Test.isRunningTest()) {
            Id execBTId = Database.executeBatch(new LogAutoSendBatchProduct(), 4);
        }
        else{
            LogAutoSendScheduleProduct.assignOneMinute();
        }
    }
 
    /**
     * batchコンスタント、パラメータを受け取る
     */
 
 
    global static void execute(Database.BatchableContext BC, List<BatchIF_Log__c> scope) {
        Integer batch_retry_max_cnt = Integer.valueOf(System.Label.batch_retry_max_cnt);
        BatchIF_Log__c rowData = scope[0];
        //String logstr = rowData.MessageGroupNumber__c + ' ' + startIndex + '/' + dataLength + ' start\n';
        String logstr = '';
        BatchIF_Log__c iflog = new BatchIF_Log__c();
        iflog.Type__c = 'SBG005';
        iflog.MessageGroupNumber__c = rowData.MessageGroupNumber__c;
        //iflog.Log__c = logstr;
        iflog.ErrorLog__c = '';
        insert iflog;
        String rowDataStr = NFMUtil.getRowDataStr(rowData);
        List<GeData> purchasePriceList2 = (List<GeData>) JSON.deserialize(rowDataStr, List<GeData>.class);
        if (purchasePriceList2 == null || purchasePriceList2.size() == 0) {
            return;
        }
        List<GeData> purchasePriceList = new List<GeData>();
        List<String> productCodeList = new List<String>();
        for (Integer i = 0; i < purchasePriceList2.size(); i++) {
            purchasePriceList.add(purchasePriceList2.get(i));
            if (String.isBlank(purchasePriceList2.get(i).ProductCode) == false) {
                productCodeList.add(purchasePriceList2.get(i).ProductCode);
            }
        }
 
        Map<String, Product2> prdsMap = new Map<String, Product2>();
        for (Product2 prd : [select Id, ProductCode, Product_ECCode__c,
                                         EffectiveDateTo__c,
                                         ProductModels__c,
                                         ProductStatus__c
                                    from Product2
                                   where ProductModels__c = false and ProductCode in :productCodeList]
        ) {
            prdsMap.put(prd.ProductCode, prd);
        }
 
        Map <String,Pricebook2 > prebookmap = new Map<String,Pricebook2 >();
 
        for(Pricebook2 pre : [Select Id,Name,MachineParts__c,ProductSegment__c,SalesChannel__c,TradeType__c,isActive
                                 from Pricebook2 where IsStandard = false])
        {
            prebookmap.put(pre.ProductSegment__c + '_' + pre.TradeType__c + '_' + pre.SalesChannel__c + '_' + pre.MachineParts__c,pre);
        }
 
 
        List<String> prekeys = new List<String>();
        for(GeData data : purchasePriceList2)
        {
            String salesChannel = data.SalesChannel == '41' ? 'dealer' : 'direct';
            String key = data.ProductSegment + '_' + data.TradeType + '_' + salesChannel + '_' + data.MachineParts + '_' + data.ProductCode + '_' + data.Currency_x;
            prekeys.add(key);
        }
        system.debug('=====0:' + prekeys);
        Map <String,PricebookEntry > premap = new Map<String,PricebookEntry >();
        for(PricebookEntry pre : [Select Id,Name,KEY__c,EffectiveDateFrom1__c,EffectiveDateFrom2__c,EffectiveDateTo1__c,EffectiveDateTo2__c,
                                        EffectiveDateFrom1_sales__c,EffectiveDateFrom1_sales_A__c,EffectiveDateFrom1_sales_B__c,EffectiveDateFrom1_sales_C__c,
                                        EffectiveDateFrom2_sales__c,EffectiveDateFrom2_sales_A__c,EffectiveDateFrom2_sales_B__c,EffectiveDateFrom2_sales_C__c,
                                        EffectiveDateTo1_sales__c,EffectiveDateTo1_sales_A__c,EffectiveDateTo1_sales_B__c,EffectiveDateTo1_sales_C__c,
                                        EffectiveDateTo2_sales__c,EffectiveDateTo2_sales_A__c,EffectiveDateTo2_sales_B__c,EffectiveDateTo2_sales_C__c,
                                        CostPrice1__c,CostPrice2__c,
                                        SalesPrice1__c,SalesPrice1A__c,SalesPrice1B__c,SalesPrice1C__c,
                                        SalesPrice2__c,SalesPrice2A__c,SalesPrice2B__c,SalesPrice2C__c
                                 from PricebookEntry where KEY__c in:prekeys])
        {
            premap.put(pre.KEY__c,pre);
        }
 
        Savepoint sp = Database.setSavepoint();
    try {
        Map<String,PricebookEntry> updPresMap = new Map<String,PricebookEntry>();
        for (Integer i = 0; i < purchasePriceList2.size(); i++) 
        {
            //purchasePriceList.add(purchasePriceList2.get(i));
            GeData data = purchasePriceList2.get(i);
            if (data.PurposeOfAdvice == '1') {
                    data.EffectiveDateTo = NFMUtil.formatDate2Str(Date.today().addDays(-1));
                }
            if (String.isBlank(purchasePriceList2.get(i).ProductCode) == false) 
            {
                //productCodeList.add(purchasePriceList2.get(i).ProductCode);
                if (data.ProductCode == null || data.ProductCode == '') {
                    iflog.ErrorLog__c += 'ProductCode is required\n';
                    continue;
                }
                if (data.ProductSegment == null || data.ProductSegment == '') {
                    iflog.ErrorLog__c += '[' + data.ProductCode + ']PriceType is required\n';
                    continue;
                }
                if (data.Currency_x == null || data.Currency_x == '') {
                    iflog.ErrorLog__c += '[' + data.ProductCode + ']Currency is required\n';
                    continue;
                }
                if (data.EffectiveDateFrom == null || data.EffectiveDateFrom == '') {
                    iflog.ErrorLog__c += '[' + data.ProductCode + ']EffectiveDateFrom is required\n';
                    continue;
                }
                if (data.EffectiveDateTo == null || data.EffectiveDateTo == '') {
                    iflog.ErrorLog__c += '[' + data.ProductCode + ']EffectiveDateTo is required\n';
                    continue;
                }
                if(prdsMap.get(data.ProductCode) == null){
                    iflog.ErrorLog__c += 'Product[' + data.ProductCode + ']NotExist\n';
                    continue;
                }
 
                String salesChannel = data.SalesChannel == '41' ? 'dealer' : 'direct';
                String pbekey = data.ProductSegment + '_' + data.TradeType + '_' + salesChannel + '_' + data.MachineParts + '_' + data.ProductCode + '_' + data.Currency_x;
                String pbkey = data.ProductSegment + '_' + data.TradeType + '_' + salesChannel + '_' + data.MachineParts;
                System.debug(prdsMap.get(data.ProductCode));
                System.debug(prebookmap);
                if(!prebookmap.keyset().contains(pbkey)){
                     iflog.ErrorLog__c += 'Pricebook2[' + pbkey + ']NotExist\n';
                    continue;
                }
                if(String.isBlank(prebookmap.get(pbkey).Id))
                {
                    iflog.ErrorLog__c += 'Pricebook2[' + pbkey + ']NotExist\n';
                    continue;
                }
                System.debug('=====1:' + premap);
                PricebookEntry pre1 = setpricebookec(data,premap,prdsMap.get(data.ProductCode).Id,prebookmap.get(pbkey),pbekey,updPresMap);
                System.debug('=====2:' + pre1);
 
 
            }
        }
        logstr += 'Execute Record:' + updPresMap.keySet().size() + '\n';
        upsert(updPresMap.Values());
 
            logstr += '\nend';
            rowData.retry_cnt__c = 0;
        } catch(Exception ex) {
            // エラーが発生した場合
            System.debug(Logginglevel.ERROR, 'SBG005_' + rowData.MessageGroupNumber__c + ':' + ex.getMessage());
            System.debug(Logginglevel.ERROR, 'SBG005_' + rowData.MessageGroupNumber__c + ':' + ex.getStackTraceString());
            Database.rollback(sp);
            logstr += '\n' + ex.getMessage();
            iflog.ErrorLog__c = ex.getMessage() + '\n' + ex.getStackTraceString() + '\n' + iflog.ErrorLog__c;
            if (rowData.retry_cnt__c == null) rowData.retry_cnt__c = 0;
            if (rowData.retry_cnt__c < batch_retry_max_cnt){
                rowData.retry_cnt__c++;
                LogAutoSendScheduleProduct.assignOneMinute();
            }
        }
        update rowData;
        iflog.Log__c = logstr;
        if (iflog.Log__c.length() > 131072) {
            iflog.Log__c = iflog.Log__c.subString(0, 131065) + ' ...';
        }
        if (iflog.ErrorLog__c.length() > 32768) {
            iflog.ErrorLog__c = iflog.ErrorLog__c.subString(0, 32760) + ' ...';
        }
        update iflog;
    }
 
    global static PricebookEntry setpricebookec(GeData data,Map<String,PricebookEntry> premap,String productid,Pricebook2 pricebook2,String pbekey,Map<String,PricebookEntry> updPresMap)
    {   System.debug(productid);
        Date dateFrom = NFMUtil.parseStr2Date(data.EffectiveDateFrom, false);
        Date dateTo = NFMUtil.parseStr2Date(data.EffectiveDateTo, true);
        String curr = data.Currency_x == 'RMB' ? 'CNY' : data.Currency_x;
        
        PricebookEntry pre = premap.get(pbekey);
        if(updPresMap.containsKey(pbekey))
        {
           pre = updPresMap.get(pbekey);
        }
        System.debug('=====3' + pre);
        String dea = String.isBlank(data.DealerDistinguish) ? '' : '_' + data.DealerDistinguish;
            if (pre == null){
                pre = new PricebookEntry(KEY__c =  pbekey,
                                         //SalesPrice1__c = data.SalesPrice,
                                         DealerDistinguish_del__c = data.DealerDistinguish,
                                         CurrencyIsoCode = curr,
                                         //EffectiveDateFrom1_sales__c = dateFrom,
                                         //EffectiveDateTo1_sales__c = dateTo,
                                         UnitPrice = 0,
                                         Product2Id = productid,
                                         Pricebook2id = pricebook2.Id,
                                         isActive = pricebook2.isActive,
                                         UseStandardPrice = false);   
                if(String.isNotBlank(data.DealerDistinguish)){
                    pre.put('SalesPrice1' + data.DealerDistinguish + '__c',data.SalesPrice);
                    pre.put('EffectiveDateFrom1_sales_' + data.DealerDistinguish + '__c' , dateFrom);
                    pre.put('EffectiveDateTo1_sales_' + data.DealerDistinguish + '__c',dateTo);
                }
                else{
                    pre.put('SalesPrice1__c',data.SalesPrice);
                    pre.put('EffectiveDateFrom1_sales__c' , dateFrom);
                    pre.put('EffectiveDateTo1_sales__c',dateTo);
                }
 
            }
            else if((Date)pre.get('EffectiveDateFrom1_sales' + dea + '__c') == null || (Date)pre.get('EffectiveDateTo1_sales' + dea + '__c') < Date.today() || (Date)pre.get('EffectiveDateFrom1_sales'+ dea + '__c') == dateFrom){
                    //pre = new PricebookEntry(KEY__c =  pbekey,
                    //                         Id = pre.Id,
                    //                         //SalesPrice1__c = data.SalesPrice,
                    //                         DealerDistinguish_del__c = data.DealerDistinguish,
                    //                         CurrencyIsoCode = curr,
                    //                         //EffectiveDateFrom1_sales__c = dateFrom,
                    //                         //EffectiveDateTo1_sales__c = dateTo,
                    //                         UnitPrice = 0,
                    //                         Product2Id = productid,
                    //                         Pricebook2id = pricebook2.Id,
                    //                         isActive = pricebook2.isActive,
                    //                         UseStandardPrice = false);
                        pre.put('Id' , pre.Id);
                        pre.put('DealerDistinguish_del__c',data.DealerDistinguish);
                        pre.put('UnitPrice',0);
                        pre.put('isActive',pricebook2.isActive);
                        pre.put('UseStandardPrice',false); 
                    if(String.isNotBlank(data.DealerDistinguish)){
                        pre.put('SalesPrice1' + data.DealerDistinguish + '__c',data.SalesPrice);
                        pre.put('EffectiveDateFrom1_sales_' + data.DealerDistinguish + '__c' , dateFrom);
                        pre.put('EffectiveDateTo1_sales_' + data.DealerDistinguish + '__c',dateTo);
                    }
                    else{
                        pre.put('SalesPrice1__c',data.SalesPrice);
                        pre.put('EffectiveDateFrom1_sales__c' , dateFrom);
                        pre.put('EffectiveDateTo1_sales__c',dateTo);
                    }
            }
            else if((Date)pre.get('EffectiveDateFrom2_sales' + dea + '__c') == null || (Date)pre.get('EffectiveDateTo2_sales' + dea + '__c') < Date.today() || (Date)pre.get('EffectiveDateFrom2_sales' + dea + '__c') == dateFrom){
                    //pre = new PricebookEntry(KEY__c =  pbekey,
                    //                         Id = pre.Id,
                    //                         //SalesPrice2__c = data.SalesPrice,
                    //                         DealerDistinguish_del__c = data.DealerDistinguish,
                    //                         CurrencyIsoCode = curr,
                    //                         //EffectiveDateFrom2_sales__c = dateFrom,
                    //                         //EffectiveDateTo2_sales__c = dateTo,
                    //                         UnitPrice = 0,
                    //                         Product2Id = productid,
                    //                         Pricebook2id = pricebook2.Id,
                    //                         isActive = pricebook2.isActive,
                    //                         UseStandardPrice = false);   
                        pre.put('Id' , pre.Id);
                        pre.put('DealerDistinguish_del__c',data.DealerDistinguish);
                        pre.put('UnitPrice',0);
                        pre.put('isActive',pricebook2.isActive);
                        pre.put('UseStandardPrice',false);  
                    if(String.isNotBlank(data.DealerDistinguish)){
                        pre.put('SalesPrice2' + data.DealerDistinguish + '__c',data.SalesPrice);
                        pre.put('EffectiveDateFrom2_sales_' + data.DealerDistinguish + '__c' , dateFrom);
                        pre.put('EffectiveDateTo2_sales_' + data.DealerDistinguish + '__c',dateTo);
                    }
                    else{
                    pre.put('SalesPrice2__c',data.SalesPrice);
                    pre.put('EffectiveDateFrom2_sales__c' , dateFrom);
                    pre.put('EffectiveDateTo2_sales__c',dateTo);
                    }
            }
            else if((Date)pre.get('EffectiveDateFrom1_sales' + dea + '__c') < (Date)pre.get('EffectiveDateFrom2_sales' + dea + '__c')){
                    //pre = new PricebookEntry(KEY__c =  pbekey,
                    //                         Id = pre.Id,
                    //                         //SalesPrice1__c = data.SalesPrice,
                    //                         DealerDistinguish_del__c = data.DealerDistinguish,
                    //                         CurrencyIsoCode = curr,
                    //                         //EffectiveDateFrom1_sales__c = dateFrom,
                    //                         //EffectiveDateTo1_sales__c = dateTo,
                    //                         UnitPrice = 0,
                    //                         Product2Id = productid,
                    //                         Pricebook2id = pricebook2.Id,
                    //                         isActive = pricebook2.isActive,
                    //                         UseStandardPrice = false);  
                        pre.put('Id' , pre.Id);
                        pre.put('DealerDistinguish_del__c',data.DealerDistinguish);
                        pre.put('UnitPrice',0);
                        pre.put('isActive',pricebook2.isActive);
                        pre.put('UseStandardPrice',false); 
                    if(String.isNotBlank(data.DealerDistinguish)){
                        pre.put('SalesPrice1' + data.DealerDistinguish + '__c',data.SalesPrice);
                        pre.put('EffectiveDateFrom1_sales_' + data.DealerDistinguish + '__c' , dateFrom);
                        pre.put('EffectiveDateTo1_sales_' + data.DealerDistinguish + '__c',dateTo);
                    }
                    else{
                        pre.put('SalesPrice1__c',data.SalesPrice);
                        pre.put('EffectiveDateFrom1_sales__c' , dateFrom);
                        pre.put('EffectiveDateTo1_sales__c',dateTo);
                    }
            }
            else{
                    //pre = new PricebookEntry(KEY__c =  pbekey,
                    //                         Id = pre.Id,
                    //                         //SalesPrice2__c = data.SalesPrice,
                    //                         DealerDistinguish_del__c = data.DealerDistinguish,
                    //                         CurrencyIsoCode = curr,
                    //                         //EffectiveDateFrom2_sales__c = dateFrom,
                    //                         //EffectiveDateTo2_sales__c = dateTo,
                    //                         UnitPrice = 0,
                    //                         Product2Id = productid,
                    //                         Pricebook2id = pricebook2.Id,
                    //                         isActive = pricebook2.isActive,
                    //                         UseStandardPrice = false);  
                        pre.put('Id' , pre.Id);
                        pre.put('DealerDistinguish_del__c',data.DealerDistinguish);
                        pre.put('UnitPrice',0);
                        pre.put('isActive',pricebook2.isActive);
                        pre.put('UseStandardPrice',false); 
                    if(String.isNotBlank(data.DealerDistinguish)){
                        pre.put('SalesPrice2' + data.DealerDistinguish + '__c',data.SalesPrice);
                        pre.put('EffectiveDateFrom2_sales_' + data.DealerDistinguish + '__c' , dateFrom);
                        pre.put('EffectiveDateTo2_sales_' + data.DealerDistinguish + '__c',dateTo);
                    }
                    else{
                    pre.put('SalesPrice2__c',data.SalesPrice);
                    pre.put('EffectiveDateFrom2_sales__c' , dateFrom);
                    pre.put('EffectiveDateTo2_sales__c',dateTo);
                    }
                }
            updPresMap.put(pbekey,pre);
            return pre;
    }
 
}