高章伟
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
global class Consumabledet2up implements Database.Batchable<sObject>{
    // global Consumable_order_details2__c lastcod;
    private final List<String> TEST_ID = null;
    private BatchIF_Log__c iflog;
 
    public Consumabledet2up(List<String> testId) {
        TEST_ID = testId;
        System.debug('TEST_ID=' + TEST_ID);
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'Consumabledet2up';
        iflog.Log__c  = 'Consumabledet2up start\n';
        iflog.ErrorLog__c = '';
        insert iflog;
    }
 
    global Database.Querylocator start(Database.BatchableContext BC) {
        if (TEST_ID == null) {
            return Database.getQuerylocator(
            [select Id,Bar_Code__c,Consumable_order_minor__c,Dealer_Info_text__c,Dealer_Info_IF__c,
            Dealer_Info_IF__r.Name,Consumable_Arrived_order__c,Frist_Storage_Agency_F__c,
            TracingCode__c,Arrive_date__c,Frist_Storage_Agency__c, Agencyinfo_fromSAP__c
            from Consumable_order_details2__c 
            where TracingCode__c != null
            //where (Name like '%-150P-%' or Name like '%-151P-%')
            order by TracingCode__c,Arrive_date__c
            ]);
        }else{
            return Database.getQuerylocator(
            [select Id,Bar_Code__c,Consumable_order_minor__c,Dealer_Info_text__c,Dealer_Info_IF__c,
            Dealer_Info_IF__r.Name,Consumable_Arrived_order__c,Frist_Storage_Agency_F__c,
            TracingCode__c,Arrive_date__c,Frist_Storage_Agency__c, Agencyinfo_fromSAP__c 
            from Consumable_order_details2__c 
            where TracingCode__c in :TEST_ID
            and TracingCode__c != null
            order by TracingCode__c,Arrive_date__c
            ]);
        }
        
 
    }
 
    global void execute(Database.BatchableContext BC, List<Consumable_order_details2__c> scope) {
        List<Consumable_order_details2__c> uptcods = new List<Consumable_order_details2__c>();
        for (Integer i = 0; i < scope.size(); i++) {
            if (i == 0) {
                // if (null != lastcod && lastcod.TracingCode__c == scope[i].TracingCode__c) {
                //     scope[i].Agencyinfo_fromSAP__c = lastcod.Agencyinfo_fromSAP__c;
                //     scope[i].Frist_Storage_Agency__c = lastcod.Frist_Storage_Agency__c;
                    
                // }else{
                    if (!String.isBlank(scope[i].Consumable_order_minor__c)) {
                        scope[i].Agencyinfo_fromSAP__c = scope[i].Dealer_Info_text__c;
                    }else if (!String.isBlank(scope[i].Dealer_Info_IF__c) ) {
                        scope[i].Agencyinfo_fromSAP__c = scope[i].Dealer_Info_IF__r.Name;
                    }
                    scope[i].Frist_Storage_Agency__c = scope[i].Frist_Storage_Agency_F__c;
                // }
            }else{
                if (scope[i].TracingCode__c == scope[i-1].TracingCode__c) {
                    scope[i].Agencyinfo_fromSAP__c = scope[i-1].Agencyinfo_fromSAP__c;
                    scope[i].Frist_Storage_Agency__c = scope[i-1].Frist_Storage_Agency__c;
                }else{
                    if (!String.isBlank(scope[i].Consumable_order_minor__c)) {
                        scope[i].Agencyinfo_fromSAP__c = scope[i].Dealer_Info_text__c;
                    }else if (!String.isBlank(scope[i].Dealer_Info_IF__c) ) {
                        scope[i].Agencyinfo_fromSAP__c = scope[i].Dealer_Info_IF__r.Name;
                    }
                    scope[i].Frist_Storage_Agency__c = scope[i].Frist_Storage_Agency_F__c;
                }
 
            }
            uptcods.add(scope[i]);
            // lastcod = scope[i];
        }
 
        // try {
        //     update uptcods;
 
        // }catch (Exception ex) {
        //     BatchIF_Log__c iflog = new BatchIF_Log__c();
        //     iflog.Type__c = 'Consumabledet2up';
        //     iflog.Log__c = ex.getMessage() + '\n';
        //     iflog.ErrorLog__c = ex.getStackTraceString();
        //     insert iflog;
        // }
 
       Database.SaveResult[] lsr = Database.update(uptcods, false);
       String errorMsg = '';
       for (Integer sIdx = 0; sIdx < lsr.size(); sIdx++) {
           Database.SaveResult sr = lsr[sIdx];
           if (!sr.isSuccess()) {
               Database.Error emsg = sr.getErrors()[0];
               iflog.ErrorLog__c += 'ERROR ' + uptcods[sIdx].get('TracingCode__c') + ' Con_det2:' + emsg + '\n';
           }
       }
    }
 
    global void finish(Database.BatchableContext BC){
        iflog.Log__c += '\nConsumabledet2up end';
        String tmp = iflog.ErrorLog__c;
        if (tmp.length() > 65000) {
            tmp = tmp.substring(0, 65000);
            tmp += ' ...have more lines...';
            iflog.ErrorLog__c = tmp;
        }
        //insert iflog;
        update iflog;
 
    }
    
}