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.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);
	}

	// 在自定义标签中获取老的简档ID  calendarUtil.getMemberProfileID
	public static String getMemberProfileID(String memberID){

		if(String.isBlank(memberID) || memberID.length()<15) return '0000000000000000';

		memberID = memberID.substring(0, 15);
		List<String> allProfile = Profile();
		for(String profile:allProfile){
			if(String.isNotBlank(profile) && profile.contains(':')){
				List<String> profileList = profile.split(':');
				if(profileList.size() > 1 && profileList[1].contains(memberID)){
					return profileList[0];
				}
			}
		}
		return '0000000000000000';
	}

	private static List<String> Profile(){
		List<String> profilList = new List<String>();
		profilList.add(System.label.SSBG_ANI);
		// profilList.add(System.label.SSBG_ANI_after);
		profilList.add(System.label.SSBG_ANI_manager);
		profilList.add(System.label.SSBG_IE);
		profilList.add(System.label.SSBG_IE_Manager_Above);
		// profilList.add(System.label.SSBG_IE_after);
		profilList.add(System.label.SSBG_IE_One);
		profilList.add(System.label.SSBG_IE_manager);
		profilList.add(System.label.SSBG_LS);
		profilList.add(System.label.SSBG_LS_Manager_Above);
		profilList.add(System.label.SSBG_LS_OnlyLoaner);
		profilList.add(System.label.SSBG_LS_notone);
		profilList.add(System.label.SSBG_LS_prototype_manager);
		profilList.add(System.label.SSBG_NDT);
		profilList.add(System.label.SSBG_NDT_Manager_Above);
		profilList.add(System.label.SSBG_NDT_prototype_manager);
		profilList.add(System.label.SSBG_Read_Only);
		profilList.add(System.label.SSBG_Read_Only_OT);
		profilList.add(System.label.SSBG_RVI);
		// profilList.add(System.label.SSBG_RVI_one);
		profilList.add(System.label.SSBG_RVI_after_one);
		profilList.add(System.label.SSBG_RVI_2);
		profilList.add(System.label.SSBG_Scientific_management_headquarters);
		profilList.add(System.label.SSBG_Service_Manager);
		profilList.add(System.label.SSBG_service_department);
		profilList.add(System.label.SSBG_Compliance_department);
		profilList.add(System.label.SSBG_Market_promotion_department);
		profilList.add(System.label.SSBG_Scientific_management_headquarters2);
		profilList.add(System.label.SSBG_Business_management_department);
		profilList.add(System.label.SSBG_Strategic_planning_department);
		profilList.add(System.label.SSBG_Business_management_department2);
		profilList.add(System.label.SSBG_Business_management_department_Have_price);
		profilList.add(System.label.Trial_Customer_Portal_User);
		profilList.add(System.label.system_administrator);
		profilList.add(System.label.system_administrator2);
		profilList.add(System.label.SystemAdmin_GPI_New);
		return profilList;
	}
}