global class updateCheckPlanBatch implements Database.Batchable, Database.AllowsCallouts, Database.Stateful { // 如果批量执行失败,用于手动执行失败的数据 public String PlanId; global updateCheckPlanBatch(String PlanId) { this.PlanId = PlanId; } global updateCheckPlanBatch() { } global Database.QueryLocator start(Database.BatchableContext bc) { // 检索点检计划(将数据库中的数据检索出来) String query = ' select Id, Actual_Execution_Quantity__c,Check_Object_Quantity__c, ' + ' Timeliness_Rate__c,Actual_Execution_Quantity_Inplan__c, Implementation_Rate__c From Inspectup_Plan__c '; if (String.isNotBlank(PlanId)) { query += ' Where Id =: PlanId'; } return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, list IPlanList) { // 更新点检计划数据 List updateIPInfoList = new List(); // 存点检计划Id // List IPIdList = new List(); if (IPlanList.size() > 0) { for (Inspectup_Plan__c IPInfo : IPlanList) { Inspectup_Plan__c IPlanInfo = new Inspectup_Plan__c(); IPlanInfo.Id = IPInfo.Id; // 实施率 IPlanInfo.Implementation_Rate__c = (IPInfo.Actual_Execution_Quantity__c / IPInfo.Check_Object_Quantity__c) * 100; // 及时率 IPlanInfo.Timeliness_Rate__c = (IPInfo.Actual_Execution_Quantity_Inplan__c / IPInfo.Check_Object_Quantity__c) * 100; updateIPInfoList.add(IPlanInfo); // IPIdList.add(IPlanId.Id); } } // 检索所有点检计划下的所有点检报告书 // List PlanDownIRInfoList = [Select Id, Inspection_asset_number__c, Inspectup_Plan__c // From Inspection_Report__c Where Inspectup_Plan__c in: IPIdList ]; // // 存 每个点检计划下应该对应那些点检报告书 // Map> PlanToReportMap = new Map>(); // for (Inspection_Report__c IRInfo : PlanDownIRInfoList) { // String IP = String.valueOf(IRInfo.Inspectup_Plan__c); // if (PlanToReportMap.containsKey(IP)) { // List IRList = PlanToReportMap.get(IP); // IRList.add(IRInfo); // PlanToReportMap.put(IP, IRList); // } else { // List IRnewList = new List(); // IRnewList.add(IRInfo); // PlanToReportMap.put(IP, IRnewList); // } // } // System.debug('PlanToReportMap:'+PlanToReportMap); // // 报告书对应的点检计划 // List IPInfoList = [select Id, Check_Object_Quantity__c, Actual_Execution_Quantity__c // from Inspectup_Plan__c where Id in:PlanToReportMap.keySet()]; // if (IPInfoList.size() > 0) { // for (Inspectup_Plan__c IPInfo : IPInfoList) { // Inspectup_Plan__c IPlanInfo = new Inspectup_Plan__c(); // IPlanInfo.Id = IPInfo.Id; // // Decimal ints = 0; // // for (Inspection_Report__c IRFeilds : PlanToReportMap.get(IPInfo.Id)) { // // ints += IRFeilds.Inspection_asset_number__c; // // } // // IPlanInfo.Actual_Execution_Quantity__c = ints; // IPlanInfo.Implementation_Rate__c = (IPInfo.Actual_Execution_Quantity__c / IPInfo.Check_Object_Quantity__c)*100; // IPlanInfo.Timeliness_Rate__c = (IPInfo.Actual_Execution_Quantity__c / IPInfo.Check_Object_Quantity__c)*100; // updateIPInfoList.add(IPlanInfo); // } // } if (updateIPInfoList.size() > 0) { update updateIPInfoList; } } global void finish(Database.BatchableContext BC) { } }