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) {

    }
}