global class NextMContractHistoricalBatch implements Database.Batchable<sObject> {
    public String query;

    global NextMContractHistoricalBatch() {
        this.query = query;
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        query = 'select Id,Maintenance_Contract__c,Maintenance_Contract__r.id,Maintenance_Contract__r.RecordType_Name__c,Asset__r.name,Asset__r.id,asset__r.First_Service_number__c,Maintenance_Contract__r.Maintenance_Contract_No_F__c,Maintenance_Contract__r.CreatedDate'
                        +' from Maintenance_Contract_Asset__c  where (Maintenance_Contract__r.Status__c = \'契約\' or Maintenance_Contract__r.Status__c = \'契約満了\') and Maintenance_Contract__r.RecordType_Name__c = \'多年保修合同\' order by Asset__c,Maintenance_Contract__r.CreatedDate desc';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, list<Maintenance_Contract_Asset__c> mcaList) {
        Map<String,Maintenance_Contract_Asset__c> mcMap =  new Map<String,Maintenance_Contract_Asset__c>();
        for(Maintenance_Contract_Asset__c mca:mcaList){
            if (mca.Asset__r.First_Service_number__c!=null) {
                if (!mcMap.containsKey(mca.Asset__r.id)) {
                mcMap.put(mca.Asset__r.id,mca);
            }
                if(mcMap.containsKey(mca.Asset__r.id)&&mca.Maintenance_Contract__r.CreatedDate>mcMap.get(mca.Asset__r.id).Maintenance_Contract__r.CreatedDate){
                    mcMap.remove(mca.Asset__r.id);
                    mcMap.put(mca.Asset__r.id,mca);
                }
            }
        }
        List<String> mcids = new  List<String>();
        for ( Maintenance_Contract_Asset__c mca:mcMap.values()) {
            if (!mcids.contains(mca.Maintenance_Contract__r.id)) {
                mcids.add(mca.Maintenance_Contract__r.id);
            }
        }
        List<Maintenance_Contract_Asset__c> mcas =  [select Id,Maintenance_Contract__r.RecordType_Name__c,Next_Maintenance_Contract__c,Asset__r.name,Asset__r.id,asset__r.First_Service_number__c,Maintenance_Contract__r.CreatedDate 
                                                        from Maintenance_Contract_Asset__c 
                                                        where Maintenance_Contract__c in :mcids];
                                                        
        List<Maintenance_Contract_Asset__c>   updateList = new List<Maintenance_Contract_Asset__c>();
        List<String> mcsql = new List<String>();
        for (Maintenance_Contract_Asset__c mca:mcas) {
            mcsql.add(mca.Asset__r.First_Service_number__c);
        }   
        List<Maintenance_Contract__c> mcs = [select Id,name,Maintenance_Contract_No__c from Maintenance_Contract__c where Maintenance_Contract_No__c in :mcsql ];
        Map<String,Maintenance_Contract__c> mcMap2 = new  Map<String,Maintenance_Contract__c>();
            for (Maintenance_Contract__c mc :mcs) {
                    if (!mcMap2.containsKey(mc.Maintenance_Contract_No__c)) {
                        mcMap2.put(mc.Maintenance_Contract_No__c,mc);
                    }
            }      
        for (Maintenance_Contract_Asset__c mca:mcas) {
            Maintenance_Contract_Asset__c mcaNew = new Maintenance_Contract_Asset__c();
            mcaNew.id = mca.id;
            mcaNew.Next_Maintenance_Contract__c = mcMap2.get(mca.Asset__r.First_Service_number__c).id;
            updateList.add(mcaNew);
        }
        if (updateList.size()>0) {
            update updateList;
        }
    }

    global void finish(Database.BatchableContext BC) {
        integer i=0;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
    }
}