global class updateCheckPlanBatch implements Database.Batchable<sObject>, 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<Inspectup_Plan__c> IPlanList) {
        // 更新点检计划数据
        List<Inspectup_Plan__c> updateIPInfoList = new List<Inspectup_Plan__c>();
        // 存点检计划Id
        // List<String> IPIdList = new List<String>();
        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<Inspection_Report__c> PlanDownIRInfoList = [Select Id, Inspection_asset_number__c, Inspectup_Plan__c
        //         From Inspection_Report__c Where Inspectup_Plan__c in: IPIdList ];
        // // 存 每个点检计划下应该对应那些点检报告书
        // Map<String, List<Inspection_Report__c>> PlanToReportMap = new Map<String, List<Inspection_Report__c>>();
        // for (Inspection_Report__c IRInfo : PlanDownIRInfoList) {
        //     String IP = String.valueOf(IRInfo.Inspectup_Plan__c);
        //     if (PlanToReportMap.containsKey(IP)) {
        //         List<Inspection_Report__c> IRList = PlanToReportMap.get(IP);
        //         IRList.add(IRInfo);
        //         PlanToReportMap.put(IP, IRList);
        //     } else {
        //         List<Inspection_Report__c> IRnewList = new List<Inspection_Report__c>();
        //         IRnewList.add(IRInfo);
        //         PlanToReportMap.put(IP, IRnewList);
        //     }
        // }
        // System.debug('PlanToReportMap:'+PlanToReportMap);
        // // 报告书对应的点检计划
        // List<Inspectup_Plan__c> 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) {

    }
}