public without sharing class BusinessActivityHandler {

    public static void ValidDaysBetweenTwoWorkingDays(List<BusinessActivity__c> newList, Map<Id, BusinessActivity__c> newMap, List<BusinessActivity__c> oldList, Map<Id, BusinessActivity__c> oldMap) {

        Map<String, Date> dateRangeMap = new Map<String, Date>();
        // 是否需要初始化一个日期如 1970-01-01
        Date bigDate = Date.today();//Date.newInstance(1970,1, 1);
        Date smaDate = Date.today();//Date.newInstance(4000, 12, 31);
        dateRangeMap.put('BigDate', bigDate);
        dateRangeMap.put('SmaDate', smaDate);
        Map<String, User_FaultInfo__c> updateUserFaultInfoMap = new Map<String, User_FaultInfo__c>();
        for (BusinessActivity__c ba : newList) {

            // 报告日期(后台用)
            if (ba.AgreeReportDate__c != null) {

                if (ba.AgreeReportDate__c > dateRangeMap.get('BigDate')) {
                    dateRangeMap.put('BigDate', ba.AgreeReportDate__c);
                }

                if (ba.AgreeReportDate__c  < dateRangeMap.get('SmaDate')) {
                    dateRangeMap.put('SmaDate', ba.AgreeReportDate__c );
                }
            }


            //  要求服务日期
            if (ba.BusinessADateRequest__c != null) {

                if (ba.BusinessADateRequest__c > dateRangeMap.get('BigDate')) {
                    dateRangeMap.put('BigDate', ba.BusinessADateRequest__c);
                }

                if (ba.BusinessADateRequest__c  < dateRangeMap.get('SmaDate')) {
                    dateRangeMap.put('SmaDate', ba.BusinessADateRequest__c );
                }

            }
            //  案件完成日
            if (ba.BusinessACompletionDate__c != null) {
                if (ba.BusinessACompletionDate__c > dateRangeMap.get('BigDate')) {
                    dateRangeMap.put('BigDate', ba.BusinessACompletionDate__c);
                }

                if (ba.BusinessACompletionDate__c  < dateRangeMap.get('SmaDate')) {
                    dateRangeMap.put('SmaDate', ba.BusinessACompletionDate__c );
                }
            }
            // 同意报价日期
            if (ba.BusinessADateApproval__c != null) {
                if (ba.BusinessADateApproval__c > dateRangeMap.get('BigDate')) {
                    dateRangeMap.put('BigDate', ba.BusinessADateApproval__c);
                }

                if (ba.BusinessADateApproval__c  < dateRangeMap.get('SmaDate')) {
                    dateRangeMap.put('SmaDate', ba.BusinessADateApproval__c );
                }
            }
            // 报价日期
            if (ba.BusinessADateQuotation__c != null) {
                if (ba.BusinessADateQuotation__c > dateRangeMap.get('BigDate')) {
                    dateRangeMap.put('BigDate', ba.BusinessADateQuotation__c);
                }

                if (ba.BusinessADateQuotation__c  < dateRangeMap.get('SmaDate')) {
                    dateRangeMap.put('SmaDate', ba.BusinessADateQuotation__c );
                }
            }
            // 报修日期
            if (ba.BusinessARepairDate__c != null) {
                if (ba.BusinessARepairDate__c > dateRangeMap.get('BigDate')) {
                    dateRangeMap.put('BigDate', ba.BusinessARepairDate__c);
                }

                if (ba.BusinessARepairDate__c  < dateRangeMap.get('SmaDate')) {
                    dateRangeMap.put('SmaDate', ba.BusinessARepairDate__c );
                }
            }

        }
        if (dateRangeMap.size() > 0) {
            if (dateRangeMap.size() > 0) {
                System.debug('SmaDate--->' + dateRangeMap.get('SmaDate'));
                System.debug('BigDate--->' + dateRangeMap.get('BigDate'));
                Map<String, OlympusCalendar__c> olyCalendarMap = calendarUtil.getCalendarMap(dateRangeMap.get('SmaDate').addDays(-40), dateRangeMap.get('BigDate').addDays(15));
                System.debug(olyCalendarMap);
                for (BusinessActivity__c ba : newList) {
                    // 派遣周期
                    if ( ba.BusinessASegment__c == 'IE' || ba.BusinessASegment__c == 'LS') {

                        if (ba.BusinessADateRequest__c == null || //[要求服务日期]=空
                                ba.BusinessAServicePlace__c == '站内' || //[服务场所]=”站内“
                                ba.BusinessAServiceCategory__c == 'Sevice' || //[服务范畴]="service"
                                ba.BusinessAServiceCategory__c == 'Association' ) {// [服务范畴]="Association"
                            // 派遣周期 置空
                            ba.BusinessADispatchTime__c = null;
                        } else {
                            if (ba.BusinessAServiceCategory__c == 'Repair' && //[服务范畴]="Repair"
                                    ba.BusinessAServicePlace__c == '现场') {//[服务场所]=“现场”
                                Date beginDate = null;
                                Date endDate = null ;
                                if (ba.AgreeReportDate__c >= ba.BusinessADateRequest__c) {
                                    beginDate = ba.BusinessADateRequest__c;
                                    endDate = ba.AgreeReportDate__c;
                                } else {
                                    beginDate = ba.AgreeReportDate__c;
                                    endDate = ba.BusinessADateRequest__c;
                                }
                                Integer count = calendarUtil.getNumfromCalendarMap(olyCalendarMap, beginDate, endDate);
                                if (count < 1) {
                                    count = 0 ;
                                } else if (count > 1) {
                                    count = count - 1;
                                }
                                ba.BusinessADispatchTime__c = count;
                                // [报告日期]- [要求服务日期] 判断哪个日期小
                            }

                        }
                    } else {
                        // RVI/NDT不需要计算派遣周期 因此直接置空
                        ba.BusinessADispatchTime__c = null;
                    }

                    // 维修周期
                    if (ba.BusinessACompletionDate__c == null || // [案件完成日]=空
                            ba.BusinessAServiceCategory__c == 'Sevice' || //[服务范畴]="service"
                            ba.BusinessAServiceCategory__c == 'Association') {//[服务范畴]="Association"
                        // 维修周期 = null
                        ba.BusinessAMaintainTime__c = null;
                    } else {
                        if (ba.BusinessAServiceCategory__c == 'Repair') {//[服务范畴]="Repair"
                            if (ba.BusinessANatureService__c == '有偿' ) {//[服务性质]="有偿"
                                if (ba.BusinessADateApproval__c == null) {//[同意报价日]=空
                                    // 维修周期 = null
                                    ba.BusinessAMaintainTime__c = null;
                                    continue;
                                } else  {
                                    Date beginDate = ba.BusinessADateApproval__c; //同意报价日期
                                    Date endDate = ba.BusinessACompletionDate__c ;//案件完成日
                                    Integer count = calendarUtil.getNumfromCalendarMap(olyCalendarMap, beginDate, endDate);
                                    if (count < 1) {
                                        count = 0 ;
                                    } else if (count > 1) {
                                        count = count - 1;
                                    }
                                    ba.BusinessAMaintainTime__c = count;
                                    // networkday [案件完成日](大)- [同意报价日期](小)
                                }

                            } else if (ba.BusinessANatureService__c == '无偿' ) {
                                Date beginDate = ba.AgreeReportDate__c;//报告日期
                                Date endDate = ba.BusinessACompletionDate__c ;//案件完成日
                                Integer count = calendarUtil.getNumfromCalendarMap(olyCalendarMap, beginDate, endDate);
                                if (count < 1) {
                                    count = 0 ;
                                } else if (count > 1) {
                                    count = count - 1;
                                }
                                ba.BusinessAMaintainTime__c = count;
                                // networkday [案件完成日]- [报告日期]
                            }
                        }
                    }
                    // 停机时间
                    if ( ba.BusinessACompletionDate__c == null || //[案件完成日]=空
                            ba.BusinessARepairDate__c == null || //[报修日期]=空
                            ba.BusinessAServiceCategory__c == 'Sevice'  || //[服务范畴]="Sevice"
                            ba.BusinessAServiceCategory__c == 'Association' ) {//[服务范畴]="Association"
                        // 停机时间 = null
                        ba.BusinessADowntime__c = null ;
                    } else {
                        Date beginDate1 = ba.BusinessARepairDate__c;//报修日期
                        Date endDate1 = ba.BusinessACompletionDate__c ;//案件完成日
                        Integer count1 = calendarUtil.getNumfromCalendarMap(olyCalendarMap, beginDate1, endDate1);
                        if (ba.BusinessAServiceCategory__c == 'Repair') {
                            if (ba.BusinessANatureService__c == '有偿' ) {
                                if (ba.BusinessADateQuotation__c == null || //报价日期
                                        ba.BusinessADateApproval__c == null) { //同意报价日期

                                    ba.BusinessADowntime__c = null;
                                    continue;
                                }
                                Date beginDate2 = ba.BusinessADateQuotation__c;//报价日期
                                Date endDate2 = ba.BusinessADateApproval__c ;//同意报价日期
                                Integer count2 = calendarUtil.getNumfromCalendarMap(olyCalendarMap, beginDate2, endDate2);
                                // networkday [案件完成日]- [报修日期]-networkday [同意报价日期]- [报价日期]
                                if (count1 - count2 < 1) {
                                    ba.BusinessADowntime__c = 0;
                                } else {
                                    ba.BusinessADowntime__c = count1 - count2 - 1;
                                }

                            } else if (ba.BusinessANatureService__c == '无偿' ) {
                                // networkday [案件完成日]- [报修日期]

                                if (count1 < 1) {
                                    count1 = 0 ;
                                } else {
                                    count1 = count1 - 1;
                                }
                                ba.BusinessADowntime__c = count1;
                            }
                        }
                    }
                    if (String.isNotBlank(ba.User_FaultInfo__c) && ba.BusinessAFirstSubmission__c != null) {
                        User_FaultInfo__c updateUserFaultInfo = new User_FaultInfo__c();
                        updateUserFaultInfo.Id = ba.User_FaultInfo__c;
                        updateUserFaultInfo.UserFaultInfoDispatchTime__c = ba.BusinessADispatchTime__c ; //派遣周期
                        updateUserFaultInfo.UserFaultInfoMaintainTime__c = ba.BusinessAMaintainTime__c;//维修周期
                        updateUserFaultInfo.UserFaultInfoDowntime__c = ba.BusinessADowntime__c;//停机时间
                        updateUserFaultInfoMap.put(ba.User_FaultInfo__c, updateUserFaultInfo);
                    }
                }

                if (updateUserFaultInfoMap != null && updateUserFaultInfoMap.size() > 0) {
                    update updateUserFaultInfoMap.values();
                }
            }
        }


    }
    // 汇总TS日报关联的业务活动的管理编码
    public static void TSRepairManagementCode(List<BusinessActivity__c> newList, Map<Id, BusinessActivity__c> newMap, List<BusinessActivity__c> oldList, Map<Id, BusinessActivity__c> oldMap) {
        Map<String, String> tSRepairIdMap = new Map<String, String>();
        
        if (Trigger.isDelete) {
            // 待删除的业务活动
            Map<String, String> pendingDeleteMap = new  Map<String, String>();

            for (BusinessActivity__c ba : oldList) {
                // 获取业务活动的TS日报
                if (String.isNotBlank(ba.BusinessATS__c)) {
                    tSRepairIdMap.put(ba.BusinessATS__c, ba.BusinessATS__c);
                    pendingDeleteMap.put(ba.Id, ba.FindManagementCode__c);
                }
            }

            if (tSRepairIdMap != null && tSRepairIdMap.size() > 0) {
                updateTSRepair(tSRepairIdMap,pendingDeleteMap);
            }
        } else if (Trigger.isInsert || Trigger.isUpdate) {
            // Trigger.isInsert 业务活动 选择 已有的用户/故障信息，将管理编码汇总
            for (BusinessActivity__c ba : newList) {
                // 获取业务活动的TS日报
                if (String.isNotBlank(ba.BusinessATS__c) && String.isNotBlank(ba.FindManagementCode__c)) {
                    tSRepairIdMap.put(ba.BusinessATS__c, ba.BusinessATS__c);
                }
            }

            if (tSRepairIdMap != null && tSRepairIdMap.size() > 0) {
                Map<String, String> pendingDeleteMap = new  Map<String, String>();
                updateTSRepair(tSRepairIdMap,pendingDeleteMap);
            }
        }
    }

    public static void updateTSRepair(Map<String, String> tSRepairIdMap,Map<String, String> pendingDeleteMap) {
        List<BusinessActivity__c> getBusinessActivity = new List<BusinessActivity__c>();
        // 查找业务活动
        getBusinessActivity = [select Id, Name, ManagementCode__c, FindManagementCode__c, BusinessATS__c,TSRepairManagementCode__c from BusinessActivity__c where BusinessATS__c In:tSRepairIdMap.keySet() order by Id];
        Map<String, String> updateTSRepairMap = new Map<String, String>();
        for (BusinessActivity__c ba : getBusinessActivity) {
            // 判断是否在待删除的Map中，不在 将 管理编码汇总
            if (!pendingDeleteMap.containsKey(ba.Id)) {
                if (String.isNotBlank(ba.FindManagementCode__c)) {
                    String managementCode = ba.FindManagementCode__c;
                    if (updateTSRepairMap.containsKey(ba.BusinessATS__c)) {
                        managementCode = updateTSRepairMap.get(ba.BusinessATS__c);
                        managementCode += ';' + ba.FindManagementCode__c;

                    }
                    updateTSRepairMap.put(ba.BusinessATS__c, managementCode);
                }
                
            } else {
                // 待删除的业务活动的管理编码与 TS日报 汇总的管理编码 相同 ，清空 TS日报的 管理编码
                if (ba.FindManagementCode__c == ba.TSRepairManagementCode__c) {
                    updateTSRepairMap.put(ba.BusinessATS__c, '');
                }
            }
        }

        if (updateTSRepairMap != null && updateTSRepairMap.size() > 0) {

            List<TS_Repair__c> updateTSRepairList = new List<TS_Repair__c>();
            for (String tSRepairId : updateTSRepairMap.keySet() ) {
                TS_Repair__c tSRepair = new TS_Repair__c();
                tSRepair.Id = tSRepairId;
                tSRepair.TSRepairManagementCode__c = updateTSRepairMap.get(tSRepairId);
                updateTSRepairList.add(tSRepair);
            }

            if (updateTSRepairList.size() > 0) {

                update updateTSRepairList;
            }

        }
    }
}