public without sharing class InspectionReportTriggerHandler extends Oly_TriggerHandler {
    private Map<Id, Inspection_Report__c> newMap;
    private Map<Id, Inspection_Report__c> oldMap;
    private List<Inspection_Report__c> newList;
    private List<Inspection_Report__c> oldList;
    public InspectionReportTriggerHandler() {
        this.newMap = (Map<Id, Inspection_Report__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Inspection_Report__c>) Trigger.oldMap;
        this.newList = (List<Inspection_Report__c>) Trigger.new;
        this.oldList = (List<Inspection_Report__c>) Trigger.old;
    }

    protected override void afterInsert() {
        updateAsset();
    }

    protected override void afterUpdate() {
        updateAsset();
        //fxk 2021/8/31 点检计划中相应字段的赋值逻辑 Star
        updateCheckPlan();
        // updateCheckPlanSubmit();
        // updateCheckPlanreject();
        //fxk 2021/8/31 点检计划中相应字段的赋值逻辑 end
        //add by rentx 20210823 start 在批准之后更新 使用ET 等信息到维修合同上
        updateVM();
        //add by rentx 20210823 end 在批准之后更新 使用ET 等信息到维修合同上
    }

    protected override void beforeDelete() {
        updateAsset_delete();
    }

    // 2021-04-16   mzy LJPH-BZPB7W SFDC点检报告更新逻辑修改 start
    //将更新保有设备的代码  移至Batch中  每天凌晨运行
    //Batch : UpdateInspectionReportAssetBatch
    private void updateAsset() {
        // 触发trigger的点检报告书
        List<String> irIds = new List<String>();
        for (Inspection_Report__c new_ir : newList) {
            irIds.add(new_ir.Id);
        }
        if (Trigger.isUpdate) {
            List<Inspection_Report__c> updateList = new List<Inspection_Report__c>();
            //2021-08-23 mzy  LJPH-C5Y5YD 【委托】合同前点检结果的实时带入 start
            //获取批准的报告书Id
            Set<String> irIdSet = new Set<String>();
            //2021-08-23   mzy  LJPH-C5Y5YD 【委托】合同前点检结果的实时带入 end
            for (Inspection_Report__c new_ir : newList) {

                //System.debug('新数据 :'+new_ir);
                //获取老数据
                Inspection_Report__c oldReport = oldMap.get(new_ir.Id) == null ? new Inspection_Report__c() : oldMap.get(new_ir.Id);
                //System.debug('老数据 :'+oldReport);
                //1.判断状态是否发生改变--为申请中
                if (oldReport != null
                        && !String.isEmpty(String.valueOf(oldReport.Status__c))
                        && new_ir.Status__c != null
                        && '申请中'.equals(new_ir.Status__c)
                        && oldReport.Status__c != new_ir.Status__c
                   ) {
                    irIdSet.add(new_ir.Id);
                }


                //2.判断状态是否发生改变--为批准
                if (oldReport != null
                        && !String.isEmpty(String.valueOf(oldReport.Status__c))
                        && new_ir.Status__c != null
                        && '批准'.equals(new_ir.Status__c)
                        && oldReport.Status__c != new_ir.Status__c
                   ) {

                    // 2021-08-23  mzy LJPH-C5Y5YD 【委托】合同前点检结果的实时带入 start
                    if (!new_ir.if_UpdateAsset__c) {
                        //需要更新
                        Inspection_Report__c tempReport = new Inspection_Report__c();
                        tempReport.Id = new_ir.id;
                        tempreport.if_UpdateAsset__c = true;
                        updateList.add(tempreport);
                    }

                    // 将批准的报告书 Id 保存到set集合中
                    irIdSet.add(new_ir.id);
                    // 2021-08-23 mzy LJPH-C5Y5YD 【委托】合同前点检结果的实时带入 end
                }
            }

            if (updateList.size() > 0) {
                System.debug('List集合 :' + updateList);
                update updateList;
            }

            //2021-09-01  mzy LJPH-C5Y5YD 【委托】合同前点检结果的实时带入 改为调用Batch来实现更新到保有设备 start
            if (irIdSet.size() > 0) {
                //如果有批准的点检报告书则将点检报告书的Id传入Batch进行更新保有设备
                List<String> ids = new List<String>();
                for (String i : irIdSet) {
                    ids.add(i);
                }
                UpdateInspectionReportAssetBatch.updateAssetFun(ids);
                
            }
            //2021-09-01  mzy LJPH-C5Y5YD 【委托】合同前点检结果的实时带入 改为调用Batch来实现更新到保有设备 end
        }


        /*
        System.debug('希望下面的代码不要运行.....');

        // 点检报告书中所有的保有设备
        List<String> assIds = new List<String>();
        Map<String, String> assMap = new Map<String, String>();
        List<Inspection_Item__c> iis = [select id, AssetId__c from Inspection_Item__c where Inspection_ReportId__c = :irIds];
        for (Inspection_Item__c ii : iis) {
            if (ii.AssetId__c != null && assMap.containsKey(ii.AssetId__c) == false) {
                assIds.add(ii.AssetId__c);
                assMap.put(ii.AssetId__c, ii.AssetId__c);
            }
        }
        // 保有设备对应的所有点检报告明细
        List<Inspection_Item__c> iiList = [select id, AssetId__c, Inspection_ReportId__c, Inspection_Result__c, Inspection_Comment__c, Inspection_ReportId__r.Name, Inspection_ReportId__r.Approved_date__c
                                             from Inspection_Item__c
                                            where AssetId__c = :assIds
                                            order by Inspection_ReportId__r.Approved_date__c desc];

        Map<String, Inspection_Item__c> iiMap = new Map<String, Inspection_Item__c>();
        for (Inspection_Item__c ii : iiList) {
            if (iiMap.containsKey(ii.AssetId__c) == false && ii.Inspection_ReportId__r.Approved_date__c != null) {
                iiMap.put(ii.AssetId__c, ii);
            }
        }

        List<Asset> updList = new List<Asset>();
        for (String assid : assIds) {
            Inspection_Item__c tmp = iiMap.get(assid) == null ? new Inspection_Item__c() : iiMap.get(assid);
            Asset ass = new Asset(
                Id = assid,
                Inspection_report_number__c = tmp.Inspection_ReportId__r.Name,
                Inspection_Report__c = tmp.Inspection_ReportId__c,
                Inspection_Comment__c = tmp.Inspection_Comment__c,
                Inspection_Result__c = tmp.Inspection_Result__c
            );
            updList.add(ass);
        }

        if (updList.size() > 0) update updList;

        */

    }
    // 2021-04-16   mzy LJPH-BZPB7W SFDC点检报告更新逻辑修改 end

    private void updateAsset_delete() {
        // 触发trigger的点检报告书
        List<String> irIds = new List<String>();
        for (Inspection_Report__c old_ir : oldList) {
            irIds.add(old_ir.Id);
        }
        // 点检报告书中所有的保有设备
        List<String> assIds = new List<String>();
        Map<String, String> assMap = new Map<String, String>();
        List<Inspection_Item__c> iis = [select id, AssetId__c from Inspection_Item__c where Inspection_ReportId__c = :irIds];
        for (Inspection_Item__c ii : iis) {
            if (assMap.containsKey(ii.AssetId__c) == false) {
                assIds.add(ii.AssetId__c);
                assMap.put(ii.AssetId__c, ii.AssetId__c);
            }
        }
        // 保有设备对应的所有点检报告明细
        List<Inspection_Item__c> iiList = [select id, AssetId__c, Inspection_ReportId__c, Inspection_Result__c, Inspection_Comment__c, Inspection_ReportId__r.Name, Inspection_ReportId__r.Approved_date__c
                                           from Inspection_Item__c
                                           where AssetId__c = :assIds
                                                   and Inspection_ReportId__c <> :irIds
                                                   order by Inspection_ReportId__r.Approved_date__c desc];

        Map<String, Inspection_Item__c> iiMap = new Map<String, Inspection_Item__c>();
        for (Inspection_Item__c ii : iiList) {
            if (iiMap.containsKey(ii.AssetId__c) == false && ii.Inspection_ReportId__r.Approved_date__c != null) {
                iiMap.put(ii.AssetId__c, ii);
            }
        }

        List<Asset> updList = new List<Asset>();
        for (String assid : assIds) {
            Inspection_Item__c tmp = iiMap.get(assid) == null ? new Inspection_Item__c() : iiMap.get(assid);
            Asset ass = new Asset(
                Id = assid,
                Inspection_report_number__c = tmp.Inspection_ReportId__r.Name,
                Inspection_Report__c = tmp.Inspection_ReportId__c,
                Inspection_Comment__c = tmp.Inspection_Comment__c,
                Inspection_Result__c = tmp.Inspection_Result__c
            );
            updList.add(ass);
        }

        if (updList.size() > 0) update updList;
    }
    //fxk 2021/8/31 点检计划中相应字段的赋值逻辑 star
    private void updateCheckPlan() {
        System.debug('Start');
        List<Inspectup_Plan__c> updateIPInfoList = new List<Inspectup_Plan__c>();
        Set<String> IPIdSet = new Set<String>();
        for (Inspection_Report__c new_IRInfo : newList) {
            if ((new_IRInfo.Status__c != oldMap.get(new_IRInfo.Id).Status__c) && new_IRInfo.Status__c == '批准') {
                if (new_IRInfo.Inspectup_Plan__c != null) {
                    IPIdSet.add(new_IRInfo.Inspectup_Plan__c);
                }
            }
        }
        Map<Id, Map<String, Decimal>> iplanMap = new Map<Id, Map<String, Decimal>>();
        List<String > sumNumsList = new List<String > {'aEQs', 'aEQInPlans', 'failnum', 'abandonmentnum', 'assetNum', 'reports', 'iDate', 'aEQsKeyRepeat'};
        Map<String, Set<Id>> PlanToAssetMap = new Map<String, Set<Id>>();
        List<Inspection_Item__c> ItemInfoList = new List<Inspection_Item__c>();
        if (IPIdSet.size() > 0) {
            ItemInfoList = [select Id, name, AssetId__c, Failourassetnumber__c, Inspection_ReportId__c, Inspection_ReportId__r.Inspectup_Plan__c,
                            Inspection_ReportId__r.Inspection_Date__c, Inspection_ReportId__r.Failourassetnumber__c,Inspection_ReportId__r.Inspectup_Plan__r.overstep_check_interval__c,
                            Inspection_ReportId__r.Inspectup_Plan__r.Planned_Start_Date__c, Inspection_ReportId__r.Inspectup_Plan__r.Planned_End_Date__c,
                            Inspection_ReportId__r.Inspectup_Plan__r.Check_Object_Quantity__c, Inspection_ReportId__r.Start_Date__c,
                            Inspection_ReportId__r.Inspection_asset_number__c, ItemStatus__c,Inspection_ReportId__r.Inspectup_Plan__r.Execution_End_Date__c, 
                            Inspection_ReportId__r.Abandonment_Num__c, Inspection_ReportId__r.Status__c from Inspection_Item__c
                            where Inspection_ReportId__r.Inspectup_Plan__c in: IPIdSet AND Inspection_ReportId__r.Status__c = '批准'];
        }
        System.debug('-------3------' + ItemInfoList);
        Map<String, Date> IDateMap = new Map<String, Date>();
        Map<String, Decimal> SumNumMap = new Map<String, Decimal>();
        // add 如果 超出点检区间=true 则个点检计划数据不计算 fxk 2021/10/20 Star
        Map<String, Boolean> overstepMap = new Map<String, Boolean>();
        // add 如果 超出点检区间=true 则个点检计划数据不计算 fxk 2021/10/20 End
        if (ItemInfoList.size() > 0) {
            for (Inspection_Item__c items : ItemInfoList) {
                Decimal aEQs = 0;//实际执行数(季度)：没去重   计划内外
                Decimal aEQInPlans = 0;//计划内实际执行数
                Decimal failnum = 0;//点检计划，设备异常数
                Decimal abandonmentnum = 0;//点检计划 放弃设备树
                // Decimal Reportnum = 0;//本次点检次数（审批过后的点检报告书）
                String planId = items.Inspection_ReportId__r.Inspectup_Plan__c;


                if (iplanMap.containsKey(planId)) {
                    SumNumMap = iplanMap.get(planId);
                }
                // iplanMap.put(ANY key, ANY value)
                // SumNumMap.put(planId , SumNumMap.get(planId))
                if (items.Inspection_ReportId__r.Inspection_Date__c >= items.Inspection_ReportId__r.Inspectup_Plan__r.Planned_Start_Date__c) {
                    //items.Inspection_ReportId__r.Inspection_asset_number__c;//计划内和外（实际执行数）
                    // System.debug('-------6------'+ a );
                    // 实际执行数(季度)：没去重   计划内外
                    String aEQsKeyRepeat = planId + sumNumsList[7];
                    if (SumNumMap.containsKey(aEQsKeyRepeat)) {
                        aEQs = SumNumMap.get(aEQsKeyRepeat);
                    }
                    aEQs++;
                    SumNumMap.put(aEQsKeyRepeat, aEQs);
                    System.debug('-------7------' + SumNumMap );
                    // 实际点检设备数   去重
                    if (items.AssetId__c != null) {
                        String aEQsKey = planId + sumNumsList[0];
                        Set<Id> AssetSet = new Set<Id>();
                        if (PlanToAssetMap.containsKey(aEQsKey)) {
                            AssetSet = PlanToAssetMap.get(aEQsKey);
                        }
                        AssetSet.add(items.AssetId__c);
                        PlanToAssetMap.put(aEQsKey , AssetSet);
                        System.debug('-------8------' + PlanToAssetMap );
                    }
                }
                if (items.Inspection_ReportId__r.Inspection_Date__c <= items.Inspection_ReportId__r.Inspectup_Plan__r.Planned_End_Date__c
                        && items.Inspection_ReportId__r.Inspection_Date__c >= items.Inspection_ReportId__r.Inspectup_Plan__r.Planned_Start_Date__c) {
                    // AEQInPlans++; //items.Inspection_ReportId__r.Inspection_asset_number__c;
                    // 计划期内完成设备数
                    String aEQInPlansKey = planId + sumNumsList[1];
                    if (SumNumMap.containsKey(aEQInPlansKey)) {
                        AEQInPlans = SumNumMap.get(aEQInPlansKey);
                    }
                    AEQInPlans++;
                    SumNumMap.put(aEQInPlansKey, AEQInPlans);
                    System.debug('-------9------' + PlanToAssetMap );
                    // 计划内点检设备数
                    if (items.AssetId__c != null) {
                        String assetNumKey = planId + sumNumsList[4];
                        Set<Id> AssetSet = new Set<Id>();
                        if (PlanToAssetMap.containsKey(assetNumKey)) {
                            AssetSet = PlanToAssetMap.get(assetNumKey);
                        }
                        AssetSet.add(items.AssetId__c);
                        PlanToAssetMap.put(assetNumKey , AssetSet);
                        System.debug('-------8------' + PlanToAssetMap );
                    }
                }
                //点检计划，设备异常数
                if (items.ItemStatus__c == 'NG' && items.AssetId__c != null) {
                    String failnumKey = planId + sumNumsList[2];
                    Set<Id> AssetSet = new Set<Id>();
                    if (PlanToAssetMap.containsKey(failnumKey)) {
                        AssetSet = PlanToAssetMap.get(failnumKey);
                    }
                    AssetSet.add(items.AssetId__c);
                    PlanToAssetMap.put(failnumKey, AssetSet);
                }
                //点检计划 放弃设备数
                if (items.ItemStatus__c == '医院放弃' && items.AssetId__c != null ) {
                    String abandonmentnumKey = planId + sumNumsList[3];
                    Set<Id> AssetSet = new Set<Id>();
                    if (PlanToAssetMap.containsKey(abandonmentnumKey)) {
                        AssetSet = PlanToAssetMap.get(abandonmentnumKey);
                    }
                    AssetSet.add(items.AssetId__c);
                    PlanToAssetMap.put(abandonmentnumKey, AssetSet);
                }
                // 计划点检对象数
                String reportKey = planId + sumNumsList[5];
                SumNumMap.put(reportKey, items.Inspection_ReportId__r.Inspectup_Plan__r.Check_Object_Quantity__c);
                //点检报告书 检测日期
                // String IDateKey = planId + sumNumsList[6];
                IDateMap.put(planId, items.Inspection_ReportId__r.Inspection_Date__c);
                // 报告书 执行日
                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);
                // add 如果 超出点检区间=true 则个点检计划数据不计算 fxk 2021/10/20 Star
                // 点检计划 超出点检区间
                if(items.Inspection_ReportId__r.Inspectup_Plan__r.overstep_check_interval__c){
                    overstepMap.put(items.Inspection_ReportId__r.Inspectup_Plan__c,items.Inspection_ReportId__r.Inspectup_Plan__r.overstep_check_interval__c);
                }
                // add 如果 超出点检区间=true 则个点检计划数据不计算 fxk 2021/10/20 End
                iplanMap.put(planId, SumNumMap);
            }
        }
        if (IPIdSet.size() > 0) {
            // 点检计划赋值
            for (String iPId : IPIdSet) {
                // add 如果 超出点检区间=true 则个点检计划数据不计算 fxk 2021/10/20 Star
                System.debug('超出'+overstepMap.get(iPId+'overstep'));
                if(!overstepMap.containskey(iPId)){
                // add 如果 超出点检区间=true 则个点检计划数据不计算 fxk 2021/10/20 End
                Inspectup_Plan__c IPlanInfo =  new Inspectup_Plan__c();
                IPlanInfo.Id = iPId;
                SumNumMap = iplanMap.get(iPId);
                System.debug('-------4------' + iplanMap);
                System.debug('-------2------' + SumNumMap);
                // 20210911 gzw bug fix start
                // if (SumNumMap.size() > 0) {
                    if (SumNumMap != null && SumNumMap.size() > 0 ) {
                        // 20210911 gzw bug fix end
                        if (PlanToAssetMap.containsKey(iPId + sumNumsList[0])) {
                            IPlanInfo.Actual_Execution_Quantity__c = PlanToAssetMap.get(iPId + sumNumsList[0]).size();//实际点检设备数   去重
                        }
                        IPlanInfo.Actual_Execution_Quantity_Inplan__c = SumNumMap.get(iPId + sumNumsList[1]);// 计划期内完成设备数
                        System.debug('-------11------' + SumNumMap.get(iPId + sumNumsList[1]));
                        if (PlanToAssetMap.containsKey(iPId + sumNumsList[4])) {
                            System.debug('-------10------' + PlanToAssetMap.get(iPId + sumNumsList[4]).size());
                            IPlanInfo.Planned_check_equipment_Num__c = PlanToAssetMap.get(iPId + sumNumsList[4]).size();// 计划内点检设备数
                        }
                        if (PlanToAssetMap.containsKey(iPId + sumNumsList[2])) {
                            IPlanInfo.Outlier_Quantity__c = PlanToAssetMap.get(iPId + sumNumsList[2]).size();//点检计划，设备异常数
                        }
                        if (PlanToAssetMap.containsKey(iPId + sumNumsList[3])) {
                            IPlanInfo.Abandonment_Number__c = PlanToAssetMap.get(iPId + sumNumsList[3]).size();//点检计划 放弃设备数
                        }
                        System.debug('-------131------' + IPlanInfo.Planned_check_equipment_Num__c);
                        System.debug('-------121------' + SumNumMap.get(iPId + sumNumsList[5]));
                        if (IPlanInfo.Actual_Execution_Quantity__c == SumNumMap.get(iPId + sumNumsList[5])) {
                            System.debug('-------13------' + IPlanInfo.Actual_Execution_Quantity__c);
                            System.debug('-------12------' + SumNumMap.get(iPId + sumNumsList[5]));
                            IPlanInfo.Ben_Actual_Times__c = 1;//本次点检次数（审批过后的点检报告书）
                            if (IDateMap.get(iPId + 'End') == null) {
                                IPlanInfo.Execution_End_Date__c = IDateMap.get(iPId);//点检计划 完成日
                            }
                        }
                        if (IDateMap.get(iPId + 'Start') == null) {
                            System.debug('--------123------' + IDateMap.get(iPId + 'Start'));
                            IPlanInfo.Execution_Start_Date__c = IDateMap.get(iPId);//执行开始日--检测日期
                        }
                        IPlanInfo.Actual_Execute_Num__c = SumNumMap.get(iPId + sumNumsList[7]);
                    }
                    System.debug('=====1===='+IPlanInfo.overstep_check_interval__c);
                    updateIPInfoList.add(IPlanInfo);
                    System.debug('-------1------' + updateIPInfoList);
                }
            }
        }
        if (updateIPInfoList.size() > 0) {
            update updateIPInfoList;
        }
        // // System.debug('IPIdMap.values():' + IPIdMap.values());
        // // 报告书批准后，点检计划
        // List<Inspection_Report__c> IRInfoList = [select Id, Failourassetnumber__c, Inspection_asset_number__c, Inspectup_Plan__c, Inspection_Date__c,
        //                             Submit_date__c, Abandonment_Num__c
        //                             from Inspection_Report__c where Inspectup_Plan__c in:IPIdSet and Status__c = '批准'];
        // // System.debug('IRInfoList:' + IRInfoList);
        // Map<String, List<Inspection_Report__c>> IPToIRsMap = new Map<String, List<Inspection_Report__c>>();
        // List<String> InsReportIdList = new List<String>();
        // for (Inspection_Report__c IRInfo : IRInfoList) {
        //     String IPkey = String.valueOf(IRInfo.Inspectup_Plan__c);
        //     // if (IPToIRsMap.containsKey(IPkey)) {
        //     //     List<Inspection_Report__c> IRValue = IPToIRsMap.get(IPkey);
        //     //     IRValue.add(IRInfo);
        //     //     IPToIRsMap.put(IPkey, IRValue);
        //     // } else {
        //     //     List<Inspection_Report__c> IRValue1 = new List<Inspection_Report__c>();
        //     //     IRValue1.add(IRInfo);
        //     //     IPToIRsMap.put(IPkey, IRValue1);
        //     // }
        //     List<Inspection_Report__c> IRValue = new List<Inspection_Report__c>();
        //     if (IPToIRsMap.containsKey(IPkey)) {
        //         IRValue = IPToIRsMap.get(IPkey);
        //     }
        //     IRValue.add(IRInfo);
        //     IPToIRsMap.put(IPkey, IRValue);
        //     InsReportIdList.add(IRInfo.Id);
        // }
        // List<Inspectup_Plan__c> IPInfoList = [select Id, Check_Object_Quantity__c, Actual_Execution_Quantity__c,
        //                                       Planned_Start_Date__c, Planned_End_Date__c
        //                                       from Inspectup_Plan__c where Id in:IPToIRsMap.keySet()];
        // Map<Id, set<String>> PlanToItemMap = new Map<Id, set<String>>();
        // List<Inspection_Item__c> ItemInfoList = [select Id, name, AssetId__c, Inspection_ReportId__r.Inspectup_Plan__c from Inspection_Item__c
        //                                         where Inspection_ReportId__c in: InsReportIdList AND Inspection_ReportId__r.Status__c = '批准'];
        // for (Inspection_Item__c Item : ItemInfoList) {
        //     set<String> AssetSet = new set<String>();
        //     if (PlanToItemMap.containsKey(Item.Inspection_ReportId__r.Inspectup_Plan__c)) {
        //         AssetSet = PlanToItemMap.get(Item.Inspection_ReportId__r.Inspectup_Plan__c);
        //     }
        //     AssetSet.add(Item.AssetId__c);
        //     PlanToItemMap.put(Item.Inspection_ReportId__r.Inspectup_Plan__c, AssetSet);
        // }
        // if (IPInfoList.size() > 0) {
        //     for (Inspectup_Plan__c IPInfokeys : IPInfoList) {
        //         Inspectup_Plan__c IPInfo = new Inspectup_Plan__c();
        //         IPInfo.Id = IPInfokeys.Id;
        //         Decimal AEQs = 0;//计划内和外（实际执行数）
        //         Decimal AEQInPlans = 0;//计划内实际执行数
        //         Decimal Failnum = 0;//点检计划，设备异常数
        //         Decimal Abandonmentnum = 0;//点检计划 放弃设备树
        //         for (Inspection_Report__c IRFeilds : IPToIRsMap.get(IPInfokeys.Id)) {
        //             if (IRFeilds.Inspection_Date__c >= IPInfokeys.Planned_Start_Date__c) {
        //                 AEQs += IRFeilds.Inspection_asset_number__c;//计划内和外（实际执行数）
        //             }
        //             if (IRFeilds.Inspection_Date__c < IPInfokeys.Planned_End_Date__c
        //                     && IRFeilds.Inspection_Date__c > IPInfokeys.Planned_Start_Date__c) {
        //                 AEQInPlans += IRFeilds.Inspection_asset_number__c;//计划内实际执行数
        //             }
        //             Failnum += IRFeilds.Failourassetnumber__c;//点检计划，设备异常数
        //             Abandonmentnum += IRFeilds.Abandonment_Num__c;//点检计划 放弃设备数
        //             if (IPInfokeys.Check_Object_Quantity__c == PlanToItemMap.get(IPInfokeys.Id).size()) {
        //                 IPInfo.Execution_End_Date__c = IRFeilds.Inspection_Date__c;//点检计划 完成日
        //             }
        //         }
        //         IPInfo.Planned_check_equipment_Num__c = PlanToItemMap.get(IPInfokeys.Id).size();
        //         IPInfo.Actual_Execution_Quantity_Inplan__c = AEQInPlans;//计划内实际执行数
        //         IPInfo.Actual_Execution_Quantity__c = AEQs;//计划内和外（实际执行数）
        //         IPInfo.Outlier_Quantity__c = Failnum;//点检计划，设备异常数
        //         IPInfo.Abandonment_Number__c = Abandonmentnum;//点检计划 放弃设备数
        //         IPInfo.Ben_Actual_Times__c = IPToIRsMap.get(IPInfokeys.Id).size();//本次点检次数（审批过后的点检报告书）

        //         updateIPInfoList.add(IPInfo);
        //     }
        //     // update updateIPInfoList;
        // }

        // // System.debug('updateIPInfoList:' + updateIPInfoList);
        // if (updateIPInfoList.size() > 0) {
        //     update updateIPInfoList;
        // }
    }
    // private void updateCheckPlanSubmit() {
    //     System.debug('--------123123123123------');
    //     List<Inspectup_Plan__c> updateIPInfoList = new List<Inspectup_Plan__c>();
    //     Set<String> IPIdSet = new Set<String>();
    //     for (Inspection_Report__c new_IRInfo : newList) {
    //         if ((new_IRInfo.Status__c != oldMap.get(new_IRInfo.Id).Status__c) && new_IRInfo.Status__c == '申请中') {
    //             if (new_IRInfo.Inspectup_Plan__c != null) {
    //                 IPIdSet.add(new_IRInfo.Inspectup_Plan__c);
    //                 // Inspectup_Plan__c IPInfo = new Inspectup_Plan__c();
    //                 // IPInfo.Id = new_IRInfo.Inspectup_Plan__c;
    //                 // System.debug('--------123123------' + new_IRInfo.Start_Date__c);
    //                 // if (new_IRInfo.Start_Date__c == null) {
    //                 //     System.debug('--------123------' + new_IRInfo.Start_Date__c);
    //                 //     IPInfo.Execution_Start_Date__c = new_IRInfo.Inspection_Date__c;//执行开始日--申请日
    //                 // }
    //                 // updateIPInfoList.add(IPInfo);
    //             }
    //         }
    //     }
    //     // ItemSql(IPIdSet, '申请中', '批准');
    //     List<Inspection_Item__c> ItemInfoList = new List<Inspection_Item__c>();
    //     if (IPIdSet.size() > 0) {
    //         ItemInfoList = [select Id, name, AssetId__c, Failourassetnumber__c, Inspection_ReportId__c, Inspection_ReportId__r.Inspectup_Plan__c,
    //                         Inspection_ReportId__r.Inspection_Date__c, Inspection_ReportId__r.Failourassetnumber__c,
    //                         Inspection_ReportId__r.Inspectup_Plan__r.Planned_Start_Date__c, Inspection_ReportId__r.Inspectup_Plan__r.Planned_End_Date__c,
    //                         Inspection_ReportId__r.Inspectup_Plan__r.Check_Object_Quantity__c,
    //                         Inspection_ReportId__r.Inspection_asset_number__c, ItemStatus__c, Inspection_ReportId__r.Start_Date__c,
    //                         Inspection_ReportId__r.Abandonment_Num__c, Inspection_ReportId__r.Status__c from Inspection_Item__c
    //                         where Inspection_ReportId__r.Inspectup_Plan__c in: IPIdSet AND (Inspection_ReportId__r.Status__c = '申请中' OR Inspection_ReportId__r.Status__c = '批准')];
    //     }
    //     // 获取日期
    //     Map<String, Date> IDateMap = new Map<String, Date>();
    //     // 去重设备数
    //     Map<String, Set<Id>> NumsMap = new Map<String, Set<Id>>();
    //     // 获取设备数量
    //     Map<String, Decimal> SumNumMap = new Map<String, Decimal>();
    //     if (ItemInfoList.size() > 0) {
    //         for (Inspection_Item__c items : ItemInfoList) {
    //             // 提交或申请设备数
    //             if (items.AssetId__c != null) {
    //                 Set<Id> AssetSet = new Set<Id>();
    //                 if (NumsMap.containsKey(items.Inspection_ReportId__r.Inspectup_Plan__c)) {
    //                     AssetSet = NumsMap.get(items.Inspection_ReportId__r.Inspectup_Plan__c);
    //                 }
    //                 AssetSet.add(items.AssetId__c);
    //                 NumsMap.put(items.Inspection_ReportId__r.Inspectup_Plan__c, AssetSet);
    //             }
    //             // 报告书 检测日期（为申请日和完成日赋值）
    //             IDateMap.put(items.Inspection_ReportId__r.Inspectup_Plan__c, items.Inspection_ReportId__r.Inspection_Date__c);
    //             // 报告书 执行日
    //             IDateMap.put(items.Inspection_ReportId__r.Inspectup_Plan__c + 'Start', items.Inspection_ReportId__r.Start_Date__c);
    //             // 计划点检对象数
    //             SumNumMap.put(items.Inspection_ReportId__r.Inspectup_Plan__c + 'PlanNum', items.Inspection_ReportId__r.Inspectup_Plan__r.Check_Object_Quantity__c);

    //         }
    //     }
    //     if (IPIdSet.size() > 0) {
    //         for (String iPId : IPIdSet) {
    //             Inspectup_Plan__c IPlanInfo =  new Inspectup_Plan__c();
    //             IPlanInfo.Id = iPId;
    //             System.debug('--------123321------' + IDateMap.get(iPId + 'Start'));
    //             if (IDateMap.get(iPId + 'Start') == null) {
    //                 System.debug('--------123------' + IDateMap.get(iPId + 'Start'));
    //                 IPlanInfo.Execution_Start_Date__c = IDateMap.get(iPId);//执行开始日--检测日期
    //             }
    //             if (NumsMap.containsKey(iPId)) {
    //                 IPlanInfo.Submit_Approval_Num__c = NumsMap.get(iPId).size();
    //             }
    //             if (IPlanInfo.Submit_Approval_Num__c == SumNumMap.get(iPId + 'PlanNum')) {
    //                 IPlanInfo.Execution_End_Date__c = IDateMap.get(iPId);//完成日--检测日期
    //             }
    //             updateIPInfoList.add(IPlanInfo);
    //         }
    //     }
    //     if (updateIPInfoList.size() > 0) {
    //         update updateIPInfoList;
    //     }
    // }
    // private void updateCheckPlanreject() {
    //     List<Inspectup_Plan__c> updateIPInfoList = new List<Inspectup_Plan__c>();
    //     Set<String> IRIdSet = new Set<String>();
    //     Set<String> IPIdSet = new Set<String>();
    //     for (Inspection_Report__c new_IRInfo : newList) {
    //         if ((new_IRInfo.Status__c != oldMap.get(new_IRInfo.Id).Status__c)
    //                 && new_IRInfo.Status__c == '草案中'
    //                 && oldMap.get(new_IRInfo.Id).Status__c == '申请中') {
    //             if (new_IRInfo.Inspectup_Plan__c != null ) {
    //                 IRIdSet.add(new_IRInfo.Id);
    //                 IPIdSet.add(new_IRInfo.Inspectup_Plan__c);
    //             }
    //         }
    //     }
    //     List<Inspection_Item__c> ItemInfoList = new List<Inspection_Item__c>();
    //     if (IPIdSet.size() > 0 && IRIdSet.size() > 0) {
    //         ItemInfoList = [select Id, name, AssetId__c, Failourassetnumber__c, Inspection_ReportId__c, Inspection_ReportId__r.Inspectup_Plan__c,
    //                         Inspection_ReportId__r.Inspection_Date__c, Inspection_ReportId__r.Failourassetnumber__c,
    //                         Inspection_ReportId__r.Inspectup_Plan__r.Planned_Start_Date__c, Inspection_ReportId__r.Inspectup_Plan__r.Planned_End_Date__c,
    //                         Inspection_ReportId__r.Inspectup_Plan__r.Check_Object_Quantity__c,
    //                         Inspection_ReportId__r.Inspection_asset_number__c, ItemStatus__c, Inspection_ReportId__r.Inspectup_Plan__r.Submit_Approval_Num__c,
    //                         Inspection_ReportId__r.Abandonment_Num__c, Inspection_ReportId__r.Status__c from Inspection_Item__c
    //                         where Inspection_ReportId__r.Inspectup_Plan__c in: IPIdSet and Inspection_ReportId__c in: IRIdSet];
    //     }
    //     // 驳回（去重设备数）
    //     Map<String, Set<Id>> NumsMap = new Map<String, Set<Id>>();
    //     // 获取设备数量
    //     Map<String, Decimal> SumNumMap = new Map<String, Decimal>();
    //     if (ItemInfoList.size() > 0) {
    //         for (Inspection_Item__c items : ItemInfoList) {
    //             // 提交或申请设备数（驳回设备数）
    //             if (items.AssetId__c != null) {
    //                 Set<Id> AssetSet = new Set<Id>();
    //                 if (NumsMap.containsKey(items.Inspection_ReportId__r.Inspectup_Plan__c)) {
    //                     AssetSet = NumsMap.get(items.Inspection_ReportId__r.Inspectup_Plan__c);
    //                 }
    //                 AssetSet.add(items.AssetId__c);
    //                 NumsMap.put(items.Inspection_ReportId__r.Inspectup_Plan__c, AssetSet);
    //             }
    //             // 提交或申请设备数
    //             SumNumMap.put(items.Inspection_ReportId__r.Inspectup_Plan__c, items.Inspection_ReportId__r.Inspectup_Plan__r.Submit_Approval_Num__c);
    //         }
    //     }
    //     if (IPIdSet.size() > 0) {
    //         for (String iPId : IPIdSet) {
    //             Inspectup_Plan__c IPlanInfo =  new Inspectup_Plan__c();
    //             IPlanInfo.Id = iPId;
    //             if (NumsMap.containsKey(iPId)) {
    //                 IPlanInfo.Submit_Approval_Num__c = SumNumMap.get(iPId) - NumsMap.get(iPId).size();
    //             }
    //             IPlanInfo.Execution_End_Date__c = null;
    //             updateIPInfoList.add(IPlanInfo);
    //         }
    //     }
    //     if (updateIPInfoList.size() > 0) {
    //         update updateIPInfoList;
    //     }
    // }
    // private List<Inspection_Item__c> ItemSql(Set<String> IPIdsSet, String Status1, String Status2) {
    //     List<Inspection_Item__c> ItemInfoList = [select Id, name, AssetId__c, Failourassetnumber__c, Inspection_ReportId__c, Inspection_ReportId__r.Inspectup_Plan__c,
    //                              Inspection_ReportId__r.Inspection_Date__c, Inspection_ReportId__r.Failourassetnumber__c,
    //                              Inspection_ReportId__r.Inspectup_Plan__r.Planned_Start_Date__c, Inspection_ReportId__r.Inspectup_Plan__r.Planned_End_Date__c,
    //                              Inspection_ReportId__r.Inspectup_Plan__r.Check_Object_Quantity__c,
    //                              Inspection_ReportId__r.Inspection_asset_number__c, ItemStatus__c,
    //                              Inspection_ReportId__r.Abandonment_Num__c, Inspection_ReportId__r.Status__c from Inspection_Item__c
    //                              where Inspection_ReportId__r.Inspectup_Plan__c in: IPIdsSet AND Inspection_ReportId__r.Status__c = : Status1 AND Inspection_ReportId__r.Status__c = :Status2];
    //     // return ItemInfoList;
    //     Map<Id, Set<Id>> NumsMap = new Map<Id, Set<Id>>();
    //     for (Inspection_Item__c items : ItemInfoList) {
    //         Set<Id> AssetSet = new Set<Id>();
    //         if (NumsMap.containsKey(items.Inspection_ReportId__r.Inspectup_Plan__c)) {
    //             AssetSet = NumsMap.get(items.Inspection_ReportId__r.Inspectup_Plan__c);
    //         }
    //         AssetSet.add(items.AssetId__c);
    //         NumsMap.put(items.Inspection_ReportId__r.Inspectup_Plan__c, AssetSet);
    //     }
    // }
    //fxk 2021/8/31 点检计划中相应字段的赋值逻辑 end

    //add by rentx 20210823 start 在批准之后更新 使用ET 等信息到维修合同上
    private void updateVM() {
        // 批准后的点检报告书上传至AWS(关联的维修合同对应的修理来源是报修子单)
        Map<Id,Id>  idMap = new Map<Id,Id>();

        List<Maintenance_Contract__c> updatevmList = new List<Maintenance_Contract__c>();
        //1. 当前操作是否变为 批准
        for (Inspection_Report__c rep : newList) {
            if (rep.Status__c == '批准' && oldMap.get(rep.Id).Status__c != '批准' ) {
                //2. 判断当前报告书是否关联维修合同
                if (rep.Contract__c != null) {
                    Boolean flag = false;
                    Maintenance_Contract__c vm = new Maintenance_Contract__c();
                    vm.Id = rep.Contract__c;
                    if (rep.Used_ET__c != null && rep.Used_ET__c != '') {
                        vm.Used_ET__c = rep.Used_ET__c;
                        flag = true;
                    }
                    if (rep.UsedMachine__c != null && rep.UsedMachine__c != '') {
                        vm.UsedMachine__c = rep.UsedMachine__c;
                        flag = true;
                    }
                    if (rep.Disinfectant__c != null && rep.Disinfectant__c != '') {
                        vm.Disinfectant__c = rep.Disinfectant__c;
                        flag = true;
                    }
                    if (rep.CleaningFluid__c != null && rep.CleaningFluid__c != '') {
                        vm.CleaningFluid__c = rep.CleaningFluid__c;
                        flag = true;
                    }
                    if (rep.Remarks__c != null && rep.Remarks__c != '') {
                        vm.Remarks__c = rep.Remarks__c;
                        flag = true;
                    }
                    if (rep.Mode__c != null && rep.Mode__c != '') {
                        vm.Mode__c = rep.Mode__c;
                        flag = true;
                    }
                    if (rep.SterilizationMethod__c != null && rep.SterilizationMethod__c != '') {
                        vm.SterilizationMethod__c = rep.SterilizationMethod__c;
                        flag = true;
                    }
                    if (flag) {
                        updatevmList.add(vm);
                    }
                    // 记录类型是 合同点检 时，发送至ASW 
                    if(rep.RecordTypeId.equals(System.Label.InspectionReportRecordTypeId)){
                        idMap.put(rep.Id, rep.Contract__c);
                    }
                    
                }
            }
        }

        if (updatevmList.size() > 0) {
            update updatevmList;
        }

        if (idMap.size() > 0) {
            NFM602Controller.sendToAWS(idMap);
        }

    }
    //add by rentx 20210823 end 在批准之后更新 使用ET 等信息到维修合同上
}