global class GetMaintenanceContractAssetBatch implements Database.Batchable<sObject>, Database.Stateful{
    public String query;

    public Map<Id,Maintenance_Contract_Asset__c> MainConAssetMap = new Map<Id,Maintenance_Contract_Asset__c>();

    global GetMaintenanceContractAssetBatch() {
        this.query = 'select id,Maintenance_Contract__r.Contract_End_Date__c,Asset__c,Estimate_List_Price_All__c from Maintenance_Contract_Asset__c where ( Maintenance_Contract__r.recordtype.name = \'服务合同\' or Maintenance_Contract__r.recordtype.name = \'维修合同\') and (Maintenance_Contract__r.Status__c = \'契約\' or Maintenance_Contract__r.Status__c = \'契約満了\')';
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, list<Sobject> scope) {
        // List<Maintenance_Contract_Asset__c>  = (List<Maintenance_Contract_Asset__c>)scope;
        for(Maintenance_Contract_Asset__c mca : (List<Maintenance_Contract_Asset__c>)scope){
            // if(MainConAssetMap == null){
            //     MainConAssetMap.put(mca.Asset__c, mca);
            // }else{
                if(MainConAssetMap.containsKey(mca.Asset__c)){
                    if(mca.Maintenance_Contract__r.Contract_End_Date__c > MainConAssetMap.get(mca.Asset__c).Maintenance_Contract__r.Contract_End_Date__c){
                        MainConAssetMap.put(mca.Asset__c, mca);
                    }
                }else{
                    MainConAssetMap.put(mca.Asset__c, mca);
                }
            // }
            
        }
    }

    global void finish(Database.BatchableContext BC) {
        BatchIF_Log__c batchIfLog = new BatchIF_Log__c();
        batchIfLog.Type__c = 'updateError';
        String mcaId = '';
        for(Maintenance_Contract_Asset__c mca : (List<Maintenance_Contract_Asset__c>)MainConAssetMap.values()){
            if(mca.Estimate_List_Price_All__c == 0 || mca.Estimate_List_Price_All__c == null){
                mcaId += mca.Id+',';
            }
            
        }
        if(mcaId.length() <  131072){
            batchIfLog.Log__c = mcaId.substring(0, mcaId.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                mcaId += '11111111111111111111111111111111';
            }
        }
        if(mcaId.length() > 131072 && mcaId.length() < 131072*2){
            batchIfLog.Log__c = mcaId.substring(0, 131072);
            batchIfLog.Log2__c = mcaId.substring(131072, mcaId.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                mcaId += '11111111111111111111111111111111';
            }
        }
        if(mcaId.length() > 131072*2 && mcaId.length() < 131072*3){
            batchIfLog.Log__c = mcaId.substring(0, 131072);
            batchIfLog.Log2__c = mcaId.substring(131072, 131072*2);
            batchIfLog.Log3__c = mcaId.substring(131072*2, mcaId.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                mcaId += '11111111111111111111111111111111';
            }
        }
        if(mcaId.length() > 131072*3 && mcaId.length() < 131072*4){
            batchIfLog.Log__c = mcaId.substring(0, 131072);
            batchIfLog.Log2__c = mcaId.substring(131072, 131072*2);
            batchIfLog.Log3__c = mcaId.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = mcaId.substring(131072*3, mcaId.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                mcaId += '11111111111111111111111111111111';
            }
        }
        if(mcaId.length() > 131072*4 && mcaId.length() < 131072*5){
            batchIfLog.Log__c = mcaId.substring(0, 131072);
            batchIfLog.Log2__c = mcaId.substring(131072, 131072*2);
            batchIfLog.Log3__c = mcaId.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = mcaId.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = mcaId.substring(131072*4, mcaId.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                mcaId += '11111111111111111111111111111111';
            }
        }
        if(mcaId.length() > 131072*5 && mcaId.length() < 131072*6){
            batchIfLog.Log__c = mcaId.substring(0, 131072);
            batchIfLog.Log2__c = mcaId.substring(131072, 131072*2);
            batchIfLog.Log3__c = mcaId.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = mcaId.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = mcaId.substring(131072*4, 131072*5);
            batchIfLog.Log6__c = mcaId.substring(131072*5, mcaId.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                mcaId += '11111111111111111111111111111111';
            }
        }
        if(mcaId.length() > 131072*6 && mcaId.length() < 131072*7){
            batchIfLog.Log__c = mcaId.substring(0, 131072);
            batchIfLog.Log2__c = mcaId.substring(131072, 131072*2);
            batchIfLog.Log3__c = mcaId.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = mcaId.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = mcaId.substring(131072*4, 131072*5);
            batchIfLog.Log6__c = mcaId.substring(131072*5, 131072*6);
            batchIfLog.Log7__c = mcaId.substring(131072*6, mcaId.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                mcaId += '11111111111111111111111111111111';
            }
        }
        if(mcaId.length() > 131072*7 && mcaId.length() < 131072*8){
            batchIfLog.Log__c = mcaId.substring(0, 131072);
            batchIfLog.Log2__c = mcaId.substring(131072, 131072*2);
            batchIfLog.Log3__c = mcaId.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = mcaId.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = mcaId.substring(131072*4, 131072*5);
            batchIfLog.Log6__c = mcaId.substring(131072*5, 131072*6);
            batchIfLog.Log7__c = mcaId.substring(131072*6, 131072*7);
            batchIfLog.Log8__c = mcaId.substring(131072*7, mcaId.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                mcaId += '11111111111111111111111111111111';
            }
        }
        if(mcaId.length() > 131072*8 && mcaId.length() < 131072*9){
            batchIfLog.Log__c = mcaId.substring(0, 131072);
            batchIfLog.Log2__c = mcaId.substring(131072, 131072*2);
            batchIfLog.Log3__c = mcaId.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = mcaId.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = mcaId.substring(131072*4, 131072*5);
            batchIfLog.Log6__c = mcaId.substring(131072*5, 131072*6);
            batchIfLog.Log7__c = mcaId.substring(131072*6, 131072*7);
            batchIfLog.Log8__c = mcaId.substring(131072*7, 131072*8);
            batchIfLog.Log9__c = mcaId.substring(131072*8, mcaId.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                mcaId += '11111111111111111111111111111111';
            }
        }
        if(mcaId.length() > 131072*9 && mcaId.length() < 131072*10){
            batchIfLog.Log__c = mcaId.substring(0, 131072);
            batchIfLog.Log2__c = mcaId.substring(131072, 131072*2);
            batchIfLog.Log3__c = mcaId.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = mcaId.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = mcaId.substring(131072*4, 131072*5);
            batchIfLog.Log6__c = mcaId.substring(131072*5, 131072*6);
            batchIfLog.Log7__c = mcaId.substring(131072*6, 131072*7);
            batchIfLog.Log8__c = mcaId.substring(131072*7, 131072*8);
            batchIfLog.Log9__c = mcaId.substring(131072*8, 131072*9);
            batchIfLog.Log10__c = mcaId.substring(131072*9, mcaId.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                mcaId += '11111111111111111111111111111111';
            }
        }
        if(mcaId.length() > 131072*10 && mcaId.length() < 131072*11){
            batchIfLog.Log__c = mcaId.substring(0, 131072);
            batchIfLog.Log2__c = mcaId.substring(131072, 131072*2);
            batchIfLog.Log3__c = mcaId.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = mcaId.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = mcaId.substring(131072*4, 131072*5);
            batchIfLog.Log6__c = mcaId.substring(131072*5, 131072*6);
            batchIfLog.Log7__c = mcaId.substring(131072*6, 131072*7);
            batchIfLog.Log8__c = mcaId.substring(131072*7, 131072*8);
            batchIfLog.Log9__c = mcaId.substring(131072*8, 131072*9);
            batchIfLog.Log10__c = mcaId.substring(131072*9, 131072*10);
            batchIfLog.Log11__c = mcaId.substring(131072*10, mcaId.length());
        }
        if(Test.isRunningTest()){
            for(Integer i = 0 ; i < 4096 ; i++){
                mcaId += '11111111111111111111111111111111';
            }
        }
        if(mcaId.length() > 131072*11 && mcaId.length() < 131072*12-5){
            batchIfLog.Log__c = mcaId.substring(0, 131072);
            batchIfLog.Log2__c = mcaId.substring(131072, 131072*2);
            batchIfLog.Log3__c = mcaId.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = mcaId.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = mcaId.substring(131072*4, 131072*5);
            batchIfLog.Log6__c = mcaId.substring(131072*5, 131072*6);
            batchIfLog.Log7__c = mcaId.substring(131072*6, 131072*7);
            batchIfLog.Log8__c = mcaId.substring(131072*7, 131072*8);
            batchIfLog.Log9__c = mcaId.substring(131072*8, 131072*9);
            batchIfLog.Log10__c = mcaId.substring(131072*9, 131072*10);
            batchIfLog.Log11__c = mcaId.substring(131072*10, 131072*11);
            batchIfLog.Log12__c = mcaId.substring(131072*11, mcaId.length());
        }
        if(mcaId.length() > 131072*12-5){
            mcaId += '...';
            batchIfLog.Log__c = mcaId.substring(0, 131072);
            batchIfLog.Log2__c = mcaId.substring(131072, 131072*2);
            batchIfLog.Log3__c = mcaId.substring(131072*2, 131072*3);
            batchIfLog.Log4__c = mcaId.substring(131072*3, 131072*4);
            batchIfLog.Log5__c = mcaId.substring(131072*4, 131072*5);
            batchIfLog.Log6__c = mcaId.substring(131072*5, 131072*6);
            batchIfLog.Log7__c = mcaId.substring(131072*6, 131072*7);
            batchIfLog.Log8__c = mcaId.substring(131072*7, 131072*8);
            batchIfLog.Log9__c = mcaId.substring(131072*8, 131072*9);
            batchIfLog.Log10__c = mcaId.substring(131072*9, 131072*10);
            batchIfLog.Log11__c = mcaId.substring(131072*10, 131072*11);
            batchIfLog.Log12__c = mcaId.substring(131072*11, mcaId.length());
        }
        insert batchIfLog;
    }
}