高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
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
85
86
87
global class CheckPlanHistory implements Database.Batchable<sObject>, Database.AllowsCallouts, Database.Stateful {
    public List<String> IPIdList;
 
    global CheckPlanHistory(List<String> IPIdList) {
        this.IPIdList = IPIdList;
    }
    global CheckPlanHistory() {
 
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
 
        String  query = ' select Id, name, AssetId__c, Failourassetnumber__c, Inspection_ReportId__c, Inspection_ReportId__r.Inspectup_Plan__c, ';
        query += ' Inspection_ReportId__r.Inspection_Date__c, Inspection_ReportId__r.Failourassetnumber__c, ';
        query += ' Inspection_ReportId__r.Inspectup_Plan__r.Planned_Start_Date__c, Inspection_ReportId__r.Inspectup_Plan__r.Planned_End_Date__c, ';
        query += ' Inspection_ReportId__r.Inspectup_Plan__r.Check_Object_Quantity__c, Inspection_ReportId__r.Start_Date__c, ';
        query += ' Inspection_ReportId__r.Inspection_asset_number__c, ItemStatus__c,Inspection_ReportId__r.Inspectup_Plan__r.Execution_End_Date__c, ';
        query += ' Inspection_ReportId__r.Abandonment_Num__c, Inspection_ReportId__r.Status__c from Inspection_Item__c ';
        query += ' where Inspection_ReportId__r.Inspectup_Plan__c in: IPIdList AND Inspection_ReportId__r.Status__c = \'批准\' ';
        System.debug('1234567890'+query);
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, list<Inspection_Item__c> ItemInfoList) {
        System.debug('1234567890');
        BatchIF_Log__c iflog = new BatchIF_Log__c();
        iflog.Type__c = 'checkMaplog';
        iflog.RowDataFlg__c = false;
        iflog.Log__c = '1234567890';
        iflog.ErrorLog__c = '';
        insert iflog;
        Map<Id, Map<String, Decimal>> iplanMap = new Map<Id, Map<String, Decimal>>();
        Map<String, Decimal> SumNumMap = new Map<String, Decimal>();
        Map<String, Date> IDateMap = new Map<String, Date>();
        if (ItemInfoList.size() > 0) {
            for (Inspection_Item__c items : ItemInfoList) {
                Decimal aEQs = 0;//实际执行数(季度):没去重   计划内外
 
                String planId = items.Inspection_ReportId__r.Inspectup_Plan__c;
                if (iplanMap.containsKey(planId)) {
                    SumNumMap = iplanMap.get(planId);
                }
 
                if (items.Inspection_ReportId__r.Inspection_Date__c >= items.Inspection_ReportId__r.Inspectup_Plan__r.Planned_Start_Date__c) {
                    // 实际执行数(季度):没去重   计划内外
                    String aEQsKeyRepeat = planId + 'num';
                    if (SumNumMap.containsKey(aEQsKeyRepeat)) {
                        aEQs = SumNumMap.get(aEQsKeyRepeat);
                    }
                    aEQs++;
                    SumNumMap.put(aEQsKeyRepeat, aEQs);
                    System.debug('-------7------' + SumNumMap );
                }
                // 报告书 执行日
                IDateMap.put(items.Inspection_ReportId__r.Inspectup_Plan__c + 'Start', items.Inspection_ReportId__r.Start_Date__c);
                // 点检计划 完成日
                IDateMap.put(items.Inspection_ReportId__r.Inspectup_Plan__c + 'End', items.Inspection_ReportId__r.Inspectup_Plan__r.Execution_End_Date__c);
            }
        }
        List<Inspectup_Plan__c> updateIPInfoList = new List<Inspectup_Plan__c>();
        if (IPIdList.size() > 0) {
            // 点检计划赋值
            for (String iPId : IPIdList) {
                Inspectup_Plan__c IPlanInfo =  new Inspectup_Plan__c();
                IPlanInfo.Id = iPId;
                SumNumMap = iplanMap.get(iPId);
                System.debug('-------4------' + iplanMap);
                System.debug('-------2------' + SumNumMap);
                // BatchIF_Log__c checkMaplog = new BatchIF_Log__c();
                // 20210911 gzw bug fix start
                // if (SumNumMap.size() > 0) {
                if (SumNumMap != null && SumNumMap.size() > 0 ) {
 
                    IPlanInfo.Actual_Execute_Num__c = SumNumMap.get(iPId + 'num');
                }
                updateIPInfoList.add(IPlanInfo);
            }
        }
        if (updateIPInfoList.size() > 0) {
            update updateIPInfoList;
        }
    }
 
    global void finish(Database.BatchableContext BC) {
 
    }
}