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;

    }
    
}