高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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) {
 
    }
}