global class MoreMaintenanceContractBatch implements Database.Batchable<sObject> {
    public String query;
    public String Id;
    global MoreMaintenanceContractBatch() {
        this.query = query;
    }
    global MoreMaintenanceContractBatch(String  Id) {
        Integer i=1;
        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++;
        this.Id = Id;
        this.query = query;
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        query = 'select id,Estimate_Target__c,RecordType_DeveloperName__c,Contract_Conclusion_Date__c from Maintenance_Contract__c where Estimate_Target__c=\'经销商\' and (RecordType_DeveloperName__c = \'NewMaintenance_Contract\' or RecordType_DeveloperName__c =\'VM_Contract\') ';
        if(String.isNotBlank(this.Id)){
            query += '   AND Id = \''+this.Id+'\'';
        }
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Maintenance_Contract__c> scope) {
        List<String> MCMC = new  List<String>();
        List<String> MCVM = new  List<String>();
        List<Repair__c> repairListMC = new List<Repair__c>();
        List<Repair__c> repairListVM = new List<Repair__c>();
        for(Maintenance_Contract__c mc:scope){
            if(mc.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')){
                MCMC.add(mc.Id);
            }
            if(mc.RecordType_DeveloperName__c.equals('VM_Contract')){
                MCVM.add(mc.Id);
            } 
        }
        if(MCMC.size()>0){
            repairListMC = [Select Id,Name,Maintenance_Contract__c,hospitalQuotationText__c from Repair__c where  Maintenance_Contract__c In:MCMC and Status1__c !='0.取消' and Status1__c != '0.删除' order by Maintenance_Contract__c];
            Map<String,Decimal> mapMC = new Map<String,Decimal>();
            Decimal hospitalQuotationText = 0.00;
            Decimal hospitalQuotationTextSum = 0.00;
            for(Repair__c obj : repairListMC){
                String MCId = (String)obj.Maintenance_Contract__c;
                if(mapMC.containsKey(MCId)){
                    hospitalQuotationText = mapMC.get(MCId);
                    if(obj.hospitalQuotationText__c !=null){
                        hospitalQuotationTextSum = hospitalQuotationText + obj.hospitalQuotationText__c;
                    }else{
                        hospitalQuotationTextSum = hospitalQuotationText;
                    }
                    mapMC.put(MCId, hospitalQuotationTextSum);
                }else{
                    mapMC.put(MCId, obj.hospitalQuotationText__c);
                }
            }
            List<Maintenance_Contract__c> upMC = new List<Maintenance_Contract__c>(); 
            for(String key : mapMC.keySet()){
                Maintenance_Contract__c updmc = new Maintenance_Contract__c();
                if(mapMC.get(key)>0.00){
                    updmc.Id = key;
                    updmc.hospitalQuotationTextSum__c = mapMC.get(key);
                    upMC.add(updmc); 
                }
                
            }
            update upMC;
        }
        if(MCVM.size()>0){
            repairListVM = [Select Id, Name,VM_Maintenance_Contract__c,hospitalQuotationText__c from Repair__c where  VM_Maintenance_Contract__c =:MCVM and Status1__c !='0.取消' and Status1__c != '0.删除' order by VM_Maintenance_Contract__c];
            Map<String, Decimal> mapVM = new Map<String, Decimal>();
            Decimal hospitalQuotationTextVM = 0.00;
            Decimal hospitalQuotationTextSumVM = 0.00;
            for(Repair__c obj : repairListVM){
                String VMId = (String)obj.VM_Maintenance_Contract__c;
                if(mapVM.containsKey(VMId)){
                    hospitalQuotationTextVM = mapVM.get(VMId);
                    if(obj.hospitalQuotationText__c !=null){
                        hospitalQuotationTextSumVM = hospitalQuotationTextVM + obj.hospitalQuotationText__c;
                    }else{
                        hospitalQuotationTextSumVM = hospitalQuotationTextVM;
                    }
                    mapVM.put(VMId, hospitalQuotationTextSumVM);
                }else{
                    mapVM.put(VMId, obj.hospitalQuotationText__c);
                }
            } 
            System.debug('test111:'+mapVM);
            List<Maintenance_Contract__c> upVM = new List<Maintenance_Contract__c>(); 
            for(String key : mapVM.keySet()){
                Maintenance_Contract__c updvm = new Maintenance_Contract__c();
                if(mapVM.get(key)>0.00){
                    updvm.Id = key;
                    updvm.hospitalQuotationTextSum__c = mapVM.get(key);
                    upVM.add(updvm);
                }
            }
            update upVM;
        }
    }

    global void finish(Database.BatchableContext BC) {

    }
}