global class MoreMaintenanceContractBatch implements Database.Batchable { 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 scope) { List MCMC = new List(); List MCVM = new List(); List repairListMC = new List(); List repairListVM = new List(); 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 mapMC = new Map(); 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 upMC = new List(); 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 mapVM = new Map(); 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 upVM = new List(); 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) { } }