高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// 20211129 ljh  排队和现有出借数据的对应关系
global class RentalQueueShippmentDateBatch implements Database.Batchable<sObject> {
    public String query;
 
    global RentalQueueShippmentDateBatch() {
        this.query = query;
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        query = 'select id,name,NextShippmentDate__c ';
        // 型号   Fixture_Model_No_F__c  备品存放地(借出时)Internal_asset_location_before__c所在地区(本部) 借出时Salesdepartment_before__c  备品分类(借出时)Equipment_Type_text__c产品分类(GI/SP)(借出时)Product_category_text__c
        query += ' ,Asset_return_Day__c,Lost_item_check_OK__c,Inspection_result_after_OK__c,Fixture_Model_No_F__c,Internal_asset_location_before__c,Salesdepartment_before__c,Equipment_Type_text__c,Product_category_text__c ';
        query += ' from Rental_Apply_Equipment_Set_Detail__c where Is_Body__c = true and DeliverySlip__c != null and Arrival_in_wh__c = false ';
        query += ' and RA_RecordTypeId__c != \'01210000000NPGK\'';
        query += ' and Inspection_result_after_F_New__c != \'NG\'';
        query += ' and NextShippmentDate__c != null';
        query += ' and Rental_Apply_Equipment_Set__r.Received_Confirm__c != \'NG\' and Check_lost_Item_F__c != \'欠品\' and Check_lost_Item_F__c != \'消耗\' ';
        query += ' order by NextShippmentDate__c';
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, list<Rental_Apply_Equipment_Set_Detail__c> scope) {
        Map<String,List<Rental_Apply_Equipment_Set_Detail__c>> keyRaedMap = new Map<String,List<Rental_Apply_Equipment_Set_Detail__c>>();
        Map<String,List<Rental_Apply_Sequence__c>> keySequenceMap = new Map<String,List<Rental_Apply_Sequence__c>>();
        List<Rental_Apply_Equipment_Set__c> upRaeList = new List<Rental_Apply_Equipment_Set__c>();
        for(Rental_Apply_Equipment_Set_Detail__c raed:scope){
            String ukey = raed.Fixture_Model_No_F__c+raed.Internal_asset_location_before__c+raed.Salesdepartment_before__c+raed.Equipment_Type_text__c+raed.Product_category_text__c;
            if(!keyRaedMap.containsKey(ukey)){
                keyRaedMap.put(ukey,new List<Rental_Apply_Equipment_Set_Detail__c>());
            }
            keyRaedMap.get(ukey).add(raed); 
        }
        List<Rental_Apply_Sequence__c> applySeriesList = [SELECT Id,ExternalKey__c,Demo_Purpose2__c,
                                                        Apply_Set_Detail__c,Apply_Set_Detail_ExternalKey__c,
                                                        Series_No__c,Salesdepartment__c,Product_category__c,
                                                        Rental_Apply__c,Internal_asset_location__c,
                                                        Apply_Set_Detail__r.Queue_Number__c,
                                                        Apply_Set_Detail__r.Rental_Apply_Equipment_Set__c,
                                                        Series_Unequal_Queue_Flag__c,
                                                        Fixture_Model_No__c,Equipment_Type__c
                                                        FROM Rental_Apply_Sequence__c
                                                        WHERE Invalid_Flag__c = false 
                                                        AND ExternalKey__c IN:keyRaedMap.keySet() 
                                                        AND Apply_Set_Detail__r.Rental_Apply_Equipment_Set__r.QueueShippmentDate__c = null order by ExternalKey__c ASC NULLS LAST , Series_No__c ASC NULLS LAST];
        for(Rental_Apply_Sequence__c ras:applySeriesList){
            String ukey = ras.ExternalKey__c;
            if(!keySequenceMap.containsKey(ukey)){
                keySequenceMap.put(ukey,new List<Rental_Apply_Sequence__c>());
            }
            keySequenceMap.get(ukey).add(ras); 
        }
        Map<String,String> mapDId = new Map<String,String>();// 已经一对一的明细Id
        for(String key:keyRaedMap.keySet()){
            if(keySequenceMap.containsKey(key)){
                List<Rental_Apply_Sequence__c> rasList= keySequenceMap.get(key);// 关键字的list
                List<Rental_Apply_Equipment_Set_Detail__c> raedList = keyRaedMap.get(key);// 有预计的List
                Integer raedSize = raedList.size();
                Integer connectI = 0;
                if(raedSize > 0){
                    for(Rental_Apply_Sequence__c ras:rasList){
                        if(raedSize > connectI){
                            Rental_Apply_Equipment_Set__c temp = new Rental_Apply_Equipment_Set__c();
                            if(mapDId.containsKey(ras.Apply_Set_Detail__c)){
                                continue;
                            }else{
                                // 排队备品预计出库时间  QueueShippmentDate__c
                                // 排队备品回寄日 Asset_return_Day__c
                                // 排队备品欠品OK时间  Lost_item_check_OK__c
                                // 排队备品预计明细    QueueDetail__c
                                // 排队备品检测OK时间   Inspection_result_after_OK__c
                                mapDId.put(ras.Apply_Set_Detail__c,ras.Apply_Set_Detail__c);
                                temp.Id = ras.Apply_Set_Detail__r.Rental_Apply_Equipment_Set__c;
                                temp.QueueShippmentDate__c = raedList[connectI].NextShippmentDate__c;
                                temp.Asset_return_Day__c = raedList[connectI].Asset_return_Day__c;
                                temp.Lost_item_check_OK__c = raedList[connectI].Lost_item_check_OK__c;
                                temp.Inspection_result_after_OK__c = raedList[connectI].Inspection_result_after_OK__c;
                                temp.QueueDetail__c = raedList[connectI].Id;
                                connectI+=1;
                            }
                            upRaeList.add(temp);
                        }else{
                            break;
                        }
                    }
                }
            } 
        }
        if(upRaeList.size() > 0){
            System.debug('zheli~upRaeList:'+upRaeList);
            update upRaeList;
        }
        
    }
 
    global void finish(Database.BatchableContext BC) {
        if(System.Test.isRunningTest()){
           test(); 
        }
        Database.executeBatch(new RentalQueueShippmentDateSumBatch(),50);
    }
    @TestVisible private static void test() {
        Integer i = 0;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
    }
}