高章伟
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
// 调货数据记录更新
global class Consumabledet2upjuBatch implements Database.Batchable<sObject> {
    private final List<String> TEST_ID = null;
    private BatchIF_Log__c iflog;
 
    public Consumabledet2upjuBatch(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) {
        // 所有调货产品的管理编码
        List<String> trcodeList = new List<String>();
 
        List<Consumable_order_details2__c> jiudeList = [select id,name,bar_code__c,TracingCode__c,
                                    Transfer_Time__c,Agency_Transfer__c,Frist_Transfer_Agency__c 
        from Consumable_order_details2__c where  SummonsForDirction_det__c = '互相调货'];
        for (Consumable_order_details2__c cod2 : jiudeList) {
            trcodeList.add(cod2.TracingCode__c);
        }
        if (TEST_ID == null) {
            return Database.getQueryLocator([select id,name,bar_code__c,TracingCode__c,Transfer_Time__c,
                        Agency_Transfer__c,Frist_Transfer_Agency__c,SummonsForDirction_det__c,
                        Dealer_Info_text__c
                        from Consumable_order_details2__c 
                        where  TracingCode__c in : trcodeList
                        and TracingCode__c != null
                        order by TracingCode__c,Arrive_date__c]);
        }else{
            return Database.getQueryLocator([select id,name,bar_code__c,TracingCode__c,Transfer_Time__c,
                        Agency_Transfer__c,Frist_Transfer_Agency__c,SummonsForDirction_det__c,
                        Dealer_Info_text__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 (scope[i].SummonsForDirction_det__c == '互相调货') {
                    scope[i].Agency_Transfer__c = true;
                    scope[i].Transfer_Time__c = 1;
                    scope[i].Frist_Transfer_Agency__c = scope[i].Dealer_Info_text__c;
                }
            }else{
                if (scope[i].TracingCode__c == scope[i-1].TracingCode__c) {
                    if (scope[i].SummonsForDirction_det__c == '互相调货') {
                        scope[i].Agency_Transfer__c = true;
                        scope[i].Transfer_Time__c = scope[i-1].Transfer_Time__c == null ? 1 : scope[i-1].Transfer_Time__c + 1; 
                        scope[i].Frist_Transfer_Agency__c = scope[i-1].Frist_Transfer_Agency__c == null ? scope[i].Dealer_Info_text__c : scope[i-1].Frist_Transfer_Agency__c;
                    }else if (scope[i-1].Agency_Transfer__c) {
                        scope[i].Agency_Transfer__c = true;
                        scope[i].Transfer_Time__c = scope[i-1].Transfer_Time__c == null ? 1 : scope[i-1].Transfer_Time__c; 
                        scope[i].Frist_Transfer_Agency__c = scope[i-1].Frist_Transfer_Agency__c == null ? scope[i].Dealer_Info_text__c : scope[i-1].Frist_Transfer_Agency__c;
                    }
                }else{
                    if (scope[i].SummonsForDirction_det__c == '互相调货') {
                        scope[i].Agency_Transfer__c = true;
                        scope[i].Transfer_Time__c = 1;
                        scope[i].Frist_Transfer_Agency__c = scope[i].Dealer_Info_text__c;
                    }
                }
                    
            }
            uptcods.add(scope[i]);
        }
 
        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;
 
    }
}