/*
*Author：vivek
*Date：2019-06-17
*/
global class BathUpdateSobject implements Database.Batchable<sObject>, Database.Stateful {
    public String query;

    public Set<Id> errorIdSet;

    public String logstr = '';
    
    public String errorstr = '';

    global BathUpdateSobject(String sObjectName){
        this.query = 'select Id from '+sObjectName;
        logstr = logstr + sObjectName + '\n开始';
    }

    global BathUpdateSobject(String sObjectName,String whereName){
        this.query = 'select Id from '+sObjectName+' where '+whereName;
        logstr = logstr + sObjectName + '\n开始';
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        system.debug('zheli'+query);
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, list<Sobject> scope) {
        Database.SaveResult[] saveResults = Database.update(scope,false);
        for(Integer i=0;i<saveResults.size();i++){
            if(!saveResults.get(i).isSuccess()){
                logstr += scope.get(i).Id+',';
                errorstr += saveResults.get(i).getErrors();
            }
        }  
    }

    global void finish(Database.BatchableContext BC) {
        BatchIF_Log__c batchIfLog = new BatchIF_Log__c();
        batchIfLog.Type__c = 'updateError';
        logstr += '\nBathUpdateSobjectEnd';
        if(errorstr.length() > 60000){
            batchIfLog.ErrorLog__c = errorstr.substring(0,60000);
        }else{
            batchIfLog.ErrorLog__c = errorstr.substring(0,errorstr.length());
        }
        
        if(logstr.length() < 131072){
            batchIfLog.Log__c = logstr.substring(0, logstr.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                logstr += '11111111111111111111111111111111';
            }
        }
        if(logstr.length() > 131072 && logstr.length() < 131072*2){
            batchIfLog.Log__c = logstr.substring(0, 131072);
            batchIfLog.Log2__c = logstr.substring(131072, logstr.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                logstr += '11111111111111111111111111111111';
            }
        }
        if(logstr.length() > 131072*2 && logstr.length() < 131072*3){
            batchIfLog.Log__c = logstr.substring(0, 131072);
            batchIfLog.Log2__c = logstr.substring(131072, 131072*2);
            batchIfLog.Log3__c = logstr.substring(131072*2, logstr.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                logstr += '11111111111111111111111111111111';
            }
        }
        if(logstr.length() > 131072*3 && logstr.length() < 131072*4){
            batchIfLog.Log__c = logstr.substring(0, 131072);
            batchIfLog.Log2__c = logstr.substring(131072, 131072*2);
            batchIfLog.Log3__c = logstr.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = logstr.substring(131072*3, logstr.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                logstr += '11111111111111111111111111111111';
            }
        }
        if(logstr.length() > 131072*4 && logstr.length() < 131072*5){
            batchIfLog.Log__c = logstr.substring(0, 131072);
            batchIfLog.Log2__c = logstr.substring(131072, 131072*2);
            batchIfLog.Log3__c = logstr.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = logstr.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = logstr.substring(131072*4, logstr.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                logstr += '11111111111111111111111111111111';
            }
        }
        if(logstr.length() > 131072*5 && logstr.length() < 131072*6){
            batchIfLog.Log__c = logstr.substring(0, 131072);
            batchIfLog.Log2__c = logstr.substring(131072, 131072*2);
            batchIfLog.Log3__c = logstr.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = logstr.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = logstr.substring(131072*4, 131072*5);
            batchIfLog.Log6__c = logstr.substring(131072*5, logstr.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                logstr += '11111111111111111111111111111111';
            }
        }
        if(logstr.length() > 131072*6 && logstr.length() < 131072*7){
            batchIfLog.Log__c = logstr.substring(0, 131072);
            batchIfLog.Log2__c = logstr.substring(131072, 131072*2);
            batchIfLog.Log3__c = logstr.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = logstr.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = logstr.substring(131072*4, 131072*5);
            batchIfLog.Log6__c = logstr.substring(131072*5, 131072*6);
            batchIfLog.Log7__c = logstr.substring(131072*6, logstr.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                logstr += '11111111111111111111111111111111';
            }
        }
        if(logstr.length() > 131072*7 && logstr.length() < 131072*8){
            batchIfLog.Log__c = logstr.substring(0, 131072);
            batchIfLog.Log2__c = logstr.substring(131072, 131072*2);
            batchIfLog.Log3__c = logstr.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = logstr.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = logstr.substring(131072*4, 131072*5);
            batchIfLog.Log6__c = logstr.substring(131072*5, 131072*6);
            batchIfLog.Log7__c = logstr.substring(131072*6, 131072*7);
            batchIfLog.Log8__c = logstr.substring(131072*7, logstr.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                logstr += '11111111111111111111111111111111';
            }
        }
        if(logstr.length() > 131072*8 && logstr.length() < 131072*9){
            batchIfLog.Log__c = logstr.substring(0, 131072);
            batchIfLog.Log2__c = logstr.substring(131072, 131072*2);
            batchIfLog.Log3__c = logstr.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = logstr.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = logstr.substring(131072*4, 131072*5);
            batchIfLog.Log6__c = logstr.substring(131072*5, 131072*6);
            batchIfLog.Log7__c = logstr.substring(131072*6, 131072*7);
            batchIfLog.Log8__c = logstr.substring(131072*7, 131072*8);
            batchIfLog.Log9__c = logstr.substring(131072*8, logstr.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                logstr += '11111111111111111111111111111111';
            }
        }
        if(logstr.length() > 131072*9 && logstr.length() < 131072*10){
            batchIfLog.Log__c = logstr.substring(0, 131072);
            batchIfLog.Log2__c = logstr.substring(131072, 131072*2);
            batchIfLog.Log3__c = logstr.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = logstr.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = logstr.substring(131072*4, 131072*5);
            batchIfLog.Log6__c = logstr.substring(131072*5, 131072*6);
            batchIfLog.Log7__c = logstr.substring(131072*6, 131072*7);
            batchIfLog.Log8__c = logstr.substring(131072*7, 131072*8);
            batchIfLog.Log9__c = logstr.substring(131072*8, 131072*9);
            batchIfLog.Log10__c = logstr.substring(131072*9, logstr.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                logstr += '11111111111111111111111111111111';
            }
        }
        if(logstr.length() > 131072*10 && logstr.length() < 131072*11){
            batchIfLog.Log__c = logstr.substring(0, 131072);
            batchIfLog.Log2__c = logstr.substring(131072, 131072*2);
            batchIfLog.Log3__c = logstr.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = logstr.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = logstr.substring(131072*4, 131072*5);
            batchIfLog.Log6__c = logstr.substring(131072*5, 131072*6);
            batchIfLog.Log7__c = logstr.substring(131072*6, 131072*7);
            batchIfLog.Log8__c = logstr.substring(131072*7, 131072*8);
            batchIfLog.Log9__c = logstr.substring(131072*8, 131072*9);
            batchIfLog.Log10__c = logstr.substring(131072*9, 131072*10);
            batchIfLog.Log11__c = logstr.substring(131072*10, logstr.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                logstr += '11111111111111111111111111111111';
            }
        }
        if(logstr.length() > 131072*11 && logstr.length() < 131072*12-5){
            batchIfLog.Log__c = logstr.substring(0, 131072);
            batchIfLog.Log2__c = logstr.substring(131072, 131072*2);
            batchIfLog.Log3__c = logstr.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = logstr.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = logstr.substring(131072*4, 131072*5);
            batchIfLog.Log6__c = logstr.substring(131072*5, 131072*6);
            batchIfLog.Log7__c = logstr.substring(131072*6, 131072*7);
            batchIfLog.Log8__c = logstr.substring(131072*7, 131072*8);
            batchIfLog.Log9__c = logstr.substring(131072*8, 131072*9);
            batchIfLog.Log10__c = logstr.substring(131072*9, 131072*10);
            batchIfLog.Log11__c = logstr.substring(131072*10, 131072*11);
            batchIfLog.Log12__c = logstr.substring(131072*11, logstr.length());
        }
        if(logstr.length() > 131072*12-5){
            logstr += '...';
            batchIfLog.Log__c = logstr.substring(0, 131072);
            batchIfLog.Log2__c = logstr.substring(131072, 131072*2);
            batchIfLog.Log3__c = logstr.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = logstr.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = logstr.substring(131072*4, 131072*5);
            batchIfLog.Log6__c = logstr.substring(131072*5, 131072*6);
            batchIfLog.Log7__c = logstr.substring(131072*6, 131072*7);
            batchIfLog.Log8__c = logstr.substring(131072*7, 131072*8);
            batchIfLog.Log9__c = logstr.substring(131072*8, 131072*9);
            batchIfLog.Log10__c = logstr.substring(131072*9, 131072*10);
            batchIfLog.Log11__c = logstr.substring(131072*10, 131072*11);
            batchIfLog.Log12__c = logstr.substring(131072*11, logstr.length());
        }
        insert batchIfLog;
    }
}