buli
2023-05-23 07390e2fcb4adf27c928335bf27ae7939c5a80ad
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
// 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;
       
    }
}