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

    public String logstr = '';
    
    public String errorstr = '';
    global OpportunityUpdateOldRecordBatch(String whereName) {
        // this.query = 'select id,Estimation_Id__c,Department_Class__c from Opportunity where Estimation_Id__c != null';
        // this.query = 'select id,Estimation_Id__c,Department_Class__c from Opportunity where id = \'0061000000gGcrA\' or id = \'00610000018ejcI\'';
        this.query = 'select id,Estimation_Id__c,Department_Class__c from Opportunity where ' + whereName;
        logstr = logstr + 'OpportunityUpdateOldRecordBatch' + '\n开始';
    }

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

    global void execute(Database.BatchableContext BC, list<Sobject> scope) {
        Set<String> quoteId = new Set<String>();
        Set<String> oppId = new Set<String>();
        Set<String> departId = new Set<String>();
        Map<Id,Date> oppQUoteCreateDateMap = new Map<Id,Date>();
        Map<Id,List<OpportunityLineItem>> oppLIteMap = new Map<Id,List<OpportunityLineItem>>();
        Map<Id,List<Rental_Apply_Equipment_Set_Detail__c>> rentMap = new Map<Id,List<Rental_Apply_Equipment_Set_Detail__c>>();
        List<Opportunity> oppList = new List<Opportunity>();
        oppList = scope;
        // List<Opportunity> oppList = [select id,Estimation_Id__c,Department_Class__c from Opportunity where id = '0061000000gGcrA' or id = '00610000018ejcI'];
        for(Opportunity opp : oppList){
            quoteId.add(opp.Estimation_Id__c);
            oppId.add(opp.Id);
            departId.add(opp.Department_Class__c);
        }
        List<Quote> quoteList = [select Id,OpportunityId,CreatedDate from Quote where id = :quoteId order by CreatedDate];
        for(Quote quo : quoteList){
            oppQUoteCreateDateMap.put(quo.OpportunityId, quo.CreatedDate.date());
        }
        List<OpportunityLineItem> oppLItemList = [select id,OpportunityId,id__c from OpportunityLineItem where OpportunityId = :oppId and Key_product_sql__c != null and Id__c != null];
        // List<>  = new List<>();
        for(Opportunity opp : oppList){
            List<OpportunityLineItem> oppLineList = new List<OpportunityLineItem>();
            for(OpportunityLineItem oppItem : oppLItemList){
                if(oppItem.OpportunityId == opp.Id){
                    oppLineList.add(oppItem);
                }
            }
            oppLIteMap.put(opp.Id, oppLineList);
        }
        // System.debug('oppLIteMap====ANY o:'+Integer.valueOf(oppLIteMap));
        if(quoteList.size() > 0 ){
            Date createdDateYear = quoteList[0].CreatedDate.addYears(-1).date();
            List<Rental_Apply_Equipment_Set_Detail__c> raesdList = [select id,product__c,Bollow_Date__c,Rental_Apply__r.Hospital__c,Rental_Apply__r.Strategic_dept__c from Rental_Apply_Equipment_Set_Detail__c where Rental_Apply__r.Strategic_dept__c = :departId and Key_product__c != null and Bollow_Date__c != null and Bollow_Date__c >= :createdDateYear order by Bollow_Date__c ];

            for(Opportunity opp : oppList){
                List<Rental_Apply_Equipment_Set_Detail__c> rentList = new List<Rental_Apply_Equipment_Set_Detail__c>();
                for(Rental_Apply_Equipment_Set_Detail__c rentaesds : raesdList){
                    if(rentaesds.Rental_Apply__r.Strategic_dept__c == opp.Department_Class__c){
                        rentList.add(rentaesds);
                    }
                }
                rentMap.put(opp.Id, rentList);
            }
            List<Opportunity> updateOppList = new List<Opportunity>();
            // if(oppLIteMap.size() > 0 && rentMap.size() > 0){
                for(Opportunity opp : oppList){
                    Opportunity o = new Opportunity();
                    o.Id = opp.Id;
                    o.WhetherTrySpareParts_3m__c = false;
                    o.WhetherTrySpareParts_6m__c = false;
                    o.WhetherTrySpareParts_1y__c = false;
                    for(OpportunityLineItem oppitem : oppLIteMap.get(opp.Id)){
                        for(Rental_Apply_Equipment_Set_Detail__c rapesd : rentMap.get(opp.Id)){

                            if(((String)oppitem.Id__c).substring(0,15) == rapesd.product__c){
                                // Date bollDate = rapesd.Bollow_Date__c;
                                
                                Date createdDateStr =  oppQUoteCreateDateMap.get(opp.Id);

                                if(rapesd.Bollow_Date__c != null && rapesd.Bollow_Date__c.addMonths(3) > createdDateStr && rapesd.Bollow_Date__c < createdDateStr){
                                    o.WhetherTrySpareParts_3m__c = true;
                                }
                                if(rapesd.Bollow_Date__c != null && rapesd.Bollow_Date__c.addMonths(6) > createdDateStr && rapesd.Bollow_Date__c.addMonths(3) < createdDateStr){
                                    o.WhetherTrySpareParts_6m__c = true;
                                }
                                if(rapesd.Bollow_Date__c != null && rapesd.Bollow_Date__c.addYears(1) > createdDateStr && rapesd.Bollow_Date__c.addMonths(6) < createdDateStr){
                                    o.WhetherTrySpareParts_1y__c = true;
                                }
                            }
                        }
                    }
                    updateOppList.add(o);
                }
            // }
            // if(updateOppList.size() > 0 ){
                Database.SaveResult[] saveResults = Database.update(updateOppList,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 = 'oppUpdateError';
        logstr += '\nOpportunityUpdateOldRecordBatchEnd';
        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;
    }
}