/**
     * @Author    lijinhuan
     * @DateTime  2022-12-09T14:22:31+0800
     * 测试类：MergeObjectBatchTest
     */
global class MergeObjectBatch implements Database.Batchable<sObject> {
    public String query;
    public Set<Id> errorIdSet;
    public String logstr = '';
    public String errorstr = '';
    public String sObjectName;
    public String selectStr;
    global MergeObjectBatch() {
        this.query = query;
    }
    global MergeObjectBatch(String selectStr,String sObjectName){
        this.query = query;
        this.sObjectName = sObjectName;
        this.selectStr = selectStr;
        logstr = logstr + sObjectName + '\n开始';
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator([
            SELECT Id,master__c 
              FROM MergeObject__c
             WHERE mergeFinish__c = false
              ]);
    }

    global void execute(Database.BatchableContext BC, list<MergeObject__c> MergeObject) {
        logstr += 'zheli:';
        //默认只执行一个
        if(MergeObject.size() > 0 && String.isNotBlank(selectStr) && String.isNotBlank(sObjectName)){
            SObject master;
            List<SObject> duplicates;
            if(sObjectName.equalsIgnoreCase('Contact')){
                master = new Contact();
                duplicates = new List<Contact>();
            }
            String masterStr = MergeObject[0].master__c;
            List<String> duplicatesList = MergeObject[0].duplicates__c.split(',');

            String masterSoql = selectStr+sObjectName+' where id = :masterStr';
            master = Database.query(masterSoql);
            String dupSoql = selectStr+sObjectName+' where id in :duplicatesList';
            duplicates = Database.query(dupSoql);
            // Merge accounts into master
            Database.MergeResult[] results = Database.merge(master, duplicates, false);

            Boolean okFlag = true;
            for(Database.MergeResult res : results) {
                if (res.isSuccess()) {
                    // Get the master ID from the result and validate it
                    System.debug('Master record ID: ' + res.getId());
                    System.assertEquals(master.Id, res.getId());              
                    logstr += ';master.Id'+res.getId();
                    // Get the IDs of the merged records and display them
                    List<Id> mergedIds = res.getMergedRecordIds();
                    System.debug('IDs of merged records: ' + mergedIds);                
                    logstr += ';IDs of merged records:'+mergedIds;
                    // Get the ID of the reparented record and 
                    // validate that this the contact ID.
                    System.debug('Reparented record ID: ' + res.getUpdatedRelatedIds());
                    logstr += ';Reparented record ID:'+res.getUpdatedRelatedIds();            
                }
                else {
                    okFlag = false;
                    for(Database.Error err : res.getErrors()) {
                        // Write each error to the debug output
                        System.debug('zheli:'+err.getMessage());
                        logstr += ';errorMessages:'+err.getMessage();
                        errorstr += ';errorMessages:'+err.getMessage();
                    }
                }
            }
            if(okFlag){
                MergeObject[0].mergeFinish__c = true;
                update MergeObject;
            }
        }
        
    }

    global void finish(Database.BatchableContext BC) {
        BatchIF_Log__c batchIfLog = new BatchIF_Log__c();
        batchIfLog.Type__c = 'mergeError';
        logstr += '\nMergeObjectBatchEnd';
        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;
    }
}