public with sharing class calendarUtil {
    //日期中间取日历Map
    public static Map<string,OlympusCalendar__c> getCalendarMap(Date beginDate,Date endDate){
        Map<string,OlympusCalendar__c> calendarMap = new Map<string,OlympusCalendar__c>();
        List<OlympusCalendar__c> calendarList = [select id,Date__c,IsWorkDay__c from OlympusCalendar__c where Date__c >= :beginDate and Date__c <= :endDate];
        for(OlympusCalendar__c cal : calendarList){
            Date datetemp = cal.Date__c;
            calendarMap.put(datetemp.format(),cal);
        }
        return calendarMap;
    }
    //获取两个日期间的工资日
    public static Integer getNumfromCalendarMap (Map<string,OlympusCalendar__c> calendarMap,Date beginDate,Date endDate){
        Integer num = 0;
        List<OlympusCalendar__c> calendarList = new List<OlympusCalendar__c>();
        Set<String> keys = calendarMap.keySet();
        for(String key : keys){
            Date keyDate = Date.valueOf(key.replace('/', '-'));
            if(beginDate<=keyDate && keyDate <= endDate){
                calendarList.add(calendarMap.get(key));
            }
        }
        for(OlympusCalendar__c oly : calendarList){
            if(oly.IsWorkDay__c == 1){
                system.debug(oly.Date__c+'   '+oly.IsWorkDay__c);
                num++;
            }
        }

        return num;
    }
    //获取日期num个工作日之后\之前的日期
    public static Date getCorrespondingDate(Map<String,OlympusCalendar__c> calendarMap,Date beginDate,Integer num){
        Integer count = 0;
        Date temp ;
        if(num > 0){
            for(Integer i = 1;;i++){
                temp = beginDate.addDays(i);
                String str = temp.format();
                OlympusCalendar__c oly = calendarMap.get(str);
                if(oly.IsWorkDay__c == 1){
                    count++;
                }
                if(num == count){
                    break;
                }
            }
        }else{
            for(Integer i = -1;;i--){
                temp = beginDate.addDays(i);
                String str = temp.format();
                OlympusCalendar__c oly = calendarMap.get(str);
                if(oly.IsWorkDay__c == 1){
                    count--;
                }
                if(num == count){
                    break;
                }
            }
        }
        return temp;
    }


    //获取两个日期间的工资日
    public static string getWorkDayNum (String beginStr,String endStr){

        Date beginDate = Date.valueOf(beginStr);
        Date endDate = Date.valueOf(endStr); 

        Map<String,OlympusCalendar__c> olmap = calendarUtil.getCalendarMap(beginDate.addDays(-1),endDate.addDays(1));

        Integer count = calendarUtil.getNumfromCalendarMap(olmap,beginDate,endDate);

        return String.valueOf(count);

    }
    
    
    //public static string getWorkDayNum (String beginStr,String endStr){

    //  Date beginDate = Date.valueOf(beginStr.replace('/', '-'));
    //  Date endDate = Date.valueOf(endStr.replace('/', '-')); 

    //  Map<String,OlympusCalendar__c> olmap = calendarUtil.getCalendarMap(beginDate.addDays(-1),endDate.addDays(1));

    //  Integer count = calendarUtil.getNumfromCalendarMap(olmap,beginDate,endDate);

    //  return String.valueOf(count);

    //}

    //获取days个工作日之前或之后的日期
    public static String getWorkDay(String beginStr,String days){

        Date beginDate = Date.valueOf(beginStr.replace('/', '-'));
        Integer num = Integer.valueOf(days);
        Map<String,OlympusCalendar__c> olmap = new Map<String,OlympusCalendar__c>();
        if(num<0){
            olmap = calendarUtil.getCalendarMap(beginDate.addDays(num-20),beginDate.addDays(1));
        }else{
            olmap = calendarUtil.getCalendarMap(beginDate.addDays(-1),beginDate.addDays(num+20));
        }

        Date newDate = calendarUtil.getCorrespondingDate(olmap,beginDate,num);

        return String.valueOf(newDate);
    }


}