// 调货数据记录更新
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;

    }
}