package com.persagy.calendar.handle;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.persagy.calendar.constant.WorkCalendarConstant;
import com.persagy.calendar.pojo.dto.WorkCalendar;
import com.persagy.calendar.pojo.dto.WorkCalendarDate;
import com.persagy.calendar.pojo.dto.WorkCalendarDict;
import com.persagy.calendar.pojo.dto.WorkCalendarLabel;
import com.persagy.calendar.pojo.dto.WorkCalendarObject;
import com.persagy.calendar.pojo.dto.WorkCalendarRule;
import com.persagy.calendar.pojo.old.OldCalendarIntervalCreateVO;
import com.persagy.calendar.pojo.old.OldCalendarQueryVO;
import com.persagy.calendar.pojo.old.OldCalendarResponseVO;
import com.persagy.calendar.pojo.vo.BaseUpdateVO;
import com.persagy.calendar.service.IWorkCalendarDateService;
import com.persagy.calendar.service.IWorkCalendarDictService;
import com.persagy.calendar.service.IWorkCalendarLabelService;
import com.persagy.calendar.service.IWorkCalendarObjectService;
import com.persagy.calendar.service.IWorkCalendarRuleService;
import com.persagy.calendar.service.IWorkCalendarService;
import com.persagy.calendar.utils.ResponseOldResult;
import com.persagy.calendar.utils.ResponseOldResultUtil;
import com.persagy.common.enums.ResponseCode;
import com.persagy.common.exception.BusinessException;
import com.persagy.common.utils.DateUtil;
import com.persagy.common.utils.IdGenerator;
import com.persagy.common.utils.ResponseResult;
import com.persagy.common.utils.ResponseUtil;
import com.persagy.common.utils.StringUtil;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUnit;

/**
 * @version 
 * @description 
 * @company persagy
 * @author zhangqiankun
 * @since 2020年10月3日:	下午1:03:19
 */
@Component
public class WorkCalendarHandler {
	
	@Autowired
    private IWorkCalendarService workCalendarService;
    
    @Autowired
    private IWorkCalendarRuleService workCalendarRuleService;
    
	@Autowired
	private IWorkCalendarDictService workCalendarDictService;
	
    @Autowired
    private IWorkCalendarDateService workCalendarDateService;

	@Autowired
	private IWorkCalendarLabelService workCalendarLabelService;
	
	@Autowired
	private IWorkCalendarObjectService workCalendarObjectService;
	
	/**
	 * 根据原有的工作历ID创建新的工作历信息和规则信息
	 * 
	 * @param workCalendar 其中的项目ID可能与原项目ID不一致
	 * @param fromId 原工作历类型ID
	 * @param oldProjectId 原项目ID
	 * @return
	 */
    @Transactional
	public ResponseResult createWorkCalendarFromId(WorkCalendar workCalendar, String fromId, String oldProjectId) {
    	boolean isSameProjectId = oldProjectId.equals(workCalendar.getProjectId());
    	
		// 1.添加工作历和对象信息
    	boolean result = this.workCalendarService.save(workCalendar);
    	if (!result) {
    		return ResponseUtil.errorResult("新增失败");
    	}
    	// 2.创建对象关联信息
    	if (CollectionUtil.isNotEmpty(workCalendar.getObjIds())) {
    		// 插入对象与工作历的关联表中
			WorkCalendarObject object = new WorkCalendarObject();
			object.setGroupCode(workCalendar.getGroupCode());
			object.setCalendarId(workCalendar.getId());
			object.setCreateTime(workCalendar.getCreateTime());
			object.setUpdateUser(workCalendar.getUpdateUser());
			object.setProjectId(workCalendar.getProjectId());
			result = this.workCalendarObjectService.batchInsertObjects(object, workCalendar.getObjIds());
			if (!result) {
				throw new BusinessException(ResponseCode.B0001.getCode(), "物理世界对象关联工作历失败");
	    	}
		}
    	// 3.复制规则信息
    	QueryWrapper<WorkCalendarRule> queryWrapper = new WorkCalendarRule.Builder().createQueryWrapper().groupCodeEq(workCalendar.getGroupCode())
    			.calendarIdOrZore(fromId).projectIdEq(oldProjectId, null)
    			.delFlagEq(WorkCalendarConstant.WORK_CALENDAR_DEL_FLAG_NOR).builderQueryWrapper();
    	List<WorkCalendarRule> ruleList = this.workCalendarRuleService.list(queryWrapper);
		if (result && CollectionUtil.isNotEmpty(ruleList)) {
			for (WorkCalendarRule workCalendarRule : ruleList) {
				if (isSameProjectId && "0".equals(workCalendarRule.getCalendarId())) {
					continue;	// 属于项目或者集团级别的，不在进行复制
				}
				workCalendarRule.setId(null);
				workCalendarRule.setGroupCode(workCalendar.getGroupCode());
				workCalendarRule.setCalendarId(workCalendar.getId());
				workCalendarRule.setUpdateUser(workCalendar.getUpdateUser());
				workCalendarRule.setProjectId(workCalendar.getProjectId());
				workCalendarRule.setCreateTime(null);
				workCalendarRule.setUpdateTime(null);
				result = workCalendarRuleService.save(workCalendarRule);
				if (!result) {
					throw new BusinessException(ResponseCode.B0001.getCode(), "复制原有规则信息失败");
				}
			}
		}
		// 4.复制自定义作息时间信息，自定义的作息时间，一定是属于工作历级别的，这里不需要在判断
		this.copyCustomCalendarDate(workCalendar, fromId, oldProjectId);
		// 5.复制自定义标签信息
		this.copyCustomCalendarLabel(workCalendar, fromId, oldProjectId);
		// 6.复制属于工作历类型级别的字典值
		this.copyCustomCalendarDict(workCalendar, fromId, oldProjectId);
		
		return ResponseUtil.successResult("新增成功", workCalendar.getId());
    }
    
    /**
	 * 复制自定义的作息时间信息，
	 * 后续若是要连历史的一起复制的话，需更新RULE_ID、WORK_DATE_START、WORK_DATE_END，但是要兼容字典类型级别这一字段
	 * 
	 * @param workCalendar 其中的项目ID可能与原项目ID不一致
	 * @param fromId 原工作历类型ID
	 * @param oldProjectId 原项目ID
	 * @return
	 */
    private void copyCustomCalendarDate(WorkCalendar workCalendar, String fromId, String oldProjectId) {
    	// 先获取原项目、原工作历下的自定义作息时间
    	QueryWrapper<WorkCalendarDate> queryWrapper = new WorkCalendarDate.Builder().createQueryWrapper().calendarIdOrZore(fromId)
    			.projectIdEq(oldProjectId, null).customFlagEq(WorkCalendarConstant.CUSTOM_CALENDAR_DATE_YES).builderQueryWrapper();
    	List<WorkCalendarDate> calendarDates = this.workCalendarDateService.list(queryWrapper);
    	if (CollectionUtil.isNotEmpty(calendarDates)) {
    		calendarDates.forEach(calendarDate -> {
    			calendarDate.setId(IdGenerator.getSnowIdStr(WorkCalendarConstant.WORK_CALENDAR_DATE_ID_PREFIX));
    			calendarDate.setCalendarId(workCalendar.getId());
    			calendarDate.setProjectId(workCalendar.getProjectId());
    			calendarDate.setCalendarName(workCalendar.getCalendarName());
    			calendarDate.setCalendarDesc(workCalendar.getCalendarDesc());
    			calendarDate.setCreateTime(workCalendar.getCreateTime());
    			calendarDate.setUpdateTime(workCalendar.getUpdateTime());
    			calendarDate.setUpdateUser(workCalendar.getUpdateUser());
    		});
			this.workCalendarDateService.batchCreateCalendarDate(calendarDates);
		}
    }
    
    /**
     * 复制标签信息，仅复制属于工作历的标签，复制属于项目的标签的前提是跨项目复制了
     * 
     * @param fromId
     * @param workCalendar
     */
    private void copyCustomCalendarLabel(WorkCalendar workCalendar, String fromId, String oldProjectId) {
    	boolean isSameProjectId = oldProjectId.equals(workCalendar.getProjectId());
    	
    	// 这里的项目ID必须，一直作为查询条件，工作历ID，根据是否是同一项目来，集团级的永远不会被复制
    	QueryWrapper<WorkCalendarLabel> queryWrapper = new WorkCalendarLabel.Builder().createQueryWrapper().groupCodeEq(workCalendar.getGroupCode())
    			.calendarIdEq(fromId, isSameProjectId ? null : 1).projectIdEq(oldProjectId, null).builderQueryWrapper();
    	List<WorkCalendarLabel> calendarLabels = this.workCalendarLabelService.list(queryWrapper);
    	
    	if (CollectionUtil.isNotEmpty(calendarLabels)) {
    		calendarLabels.forEach(calendarLabel -> {
    			calendarLabel.setId(IdGenerator.getSnowIdStr(WorkCalendarConstant.WORK_CALENDAR_LABEL_ID_PREFIX));
    			calendarLabel.setCalendarId("0".equals(calendarLabel.getCalendarId()) ? "0" : workCalendar.getId());
    			calendarLabel.setProjectId("0".equals(calendarLabel.getProjectId()) ? "0" : workCalendar.getProjectId());
    			calendarLabel.setDiyValueStr(CollectionUtil.isEmpty(calendarLabel.getDiyValue()) ? null : JSON.toJSONString(calendarLabel.getDiyValue()));
    			calendarLabel.setCreateTime(workCalendar.getCreateTime());
    			calendarLabel.setUpdateTime(workCalendar.getUpdateTime());
    			calendarLabel.setUpdateUser(workCalendar.getUpdateUser());
    		});
			this.workCalendarLabelService.batchCreateCalendarLabel(calendarLabels);
		}
    }
    
    /**
     * 复制字典数据信息，仅复制属于工作历的标签，复制属于项目的标签的前提是跨项目复制了
     * 
     * @param fromId
     * @param workCalendar
     */
    private void copyCustomCalendarDict(WorkCalendar workCalendar, String fromId, String oldProjectId) {
    	boolean isSameProjectId = oldProjectId.equals(workCalendar.getProjectId());
    	
    	// 获取原有的字典数据
    	LambdaQueryWrapper<WorkCalendarDict> queryWrapper = new WorkCalendarDict.BuilderQueryWrapper().groupCodeEq(workCalendar.getGroupCode())
    			.projectIdEq(oldProjectId, null).calendarIdEq(fromId, isSameProjectId ? null : 1).builder();
    	List<WorkCalendarDict> calendarDicts = this.workCalendarDictService.list(queryWrapper);
    	if (CollectionUtil.isNotEmpty(calendarDicts)) {
    		calendarDicts.forEach(calendarDict -> {
    			calendarDict.setId(IdGenerator.getUUID());
    			calendarDict.setCalendarId("0".equals(calendarDict.getCalendarId()) ? "0" : workCalendar.getId());
    			calendarDict.setProjectId("0".equals(calendarDict.getProjectId()) ? "0" : workCalendar.getProjectId());
    		});
			this.workCalendarDictService.batchCreateCalendarDict(calendarDicts);
		}
    }
    
    /**
     * 逻辑删除工作历类型数据，且级联删除规则、对象、标签、自定义作息时间
     * 
     * @param baseUpdateVO
     * @param onlyCalendarByGroup true-该集团下最后一个工作历类型
     * @param onlyCalendarByProject true-该项目下最后一个工作历类型
     * @return
     */
    @Transactional
	public ResponseResult deleteWorkCalendar(BaseUpdateVO baseUpdateVO, boolean onlyCalendarByGroup, boolean onlyCalendarByProject) {
    	String id = baseUpdateVO.getId();
    	String projectId = baseUpdateVO.getProjectId();
    	String updateUser = baseUpdateVO.getUserId();
		// 1.逻辑删除工作历信息
    	boolean result = this.workCalendarService.deleteWorkCalendar(baseUpdateVO.getId(), baseUpdateVO.getUserId());
    	if (!result) {
			return ResponseUtil.errorResult("删除工作历类型失败");
		}
    	// 2.取消关联的对象数据信息
    	QueryWrapper<WorkCalendarObject> objectWrapper = new WorkCalendarObject.Builder().createQueryWrapper().groupCodeEq(baseUpdateVO.getGroupCode())
    			.projectIdEq(projectId).calendarIdEq(baseUpdateVO.getId()).builderQueryWrapper();
    	this.workCalendarObjectService.remove(objectWrapper);
    	// 3.逻辑删除规则信息
    	WorkCalendarRule workCalendarRule = new WorkCalendarRule();
    	if (!onlyCalendarByProject) {
    		workCalendarRule.setCalendarId(id);
		} else if (!onlyCalendarByGroup) {
			workCalendarRule.setProjectId(baseUpdateVO.getProjectId());
		}
    	workCalendarRule.setGroupCode(baseUpdateVO.getGroupCode());
    	workCalendarRule.setCalendarId(id);
    	workCalendarRule.setUpdateUser(updateUser);
    	this.workCalendarRuleService.deleteWorkCalendarRule(workCalendarRule, null);
    	// 4.物理删除作息时间信息，包含自定义的
    	QueryWrapper<WorkCalendarDate> dateWrapper = new WorkCalendarDate.Builder().createQueryWrapper().groupCodeEq(baseUpdateVO.getGroupCode())
    			.calendarIdEq(id, onlyCalendarByProject ? 1 : null).projectIdEq(projectId, onlyCalendarByGroup ? 0 : null).builderQueryWrapper();
    	this.workCalendarDateService.remove(dateWrapper);
		// 5.物理删除标签信息，剩余仅此一个工作历类型时，工作历ID，不在作为查询条件，该项目下全部删除
    	QueryWrapper<WorkCalendarLabel> labelWrapper = new WorkCalendarLabel.Builder().createQueryWrapper().groupCodeEq(baseUpdateVO.getGroupCode())
    			.calendarIdEq(id, onlyCalendarByProject ? 1 : null).projectIdEq(projectId, onlyCalendarByGroup ? 0 : null).builderQueryWrapper();
    	this.workCalendarLabelService.remove(labelWrapper);
		// 6.物理删除属于工作历类型级别的字典值
    	LambdaQueryWrapper<WorkCalendarDict> dictWrapper = new WorkCalendarDict.BuilderQueryWrapper().groupCodeEq(baseUpdateVO.getGroupCode())
    			.calendarIdEq(id, onlyCalendarByProject ? 1 : null).projectIdEq(projectId, onlyCalendarByGroup ? 0 : null).builder();
    	this.workCalendarDictService.remove(dictWrapper);
    	
		return ResponseUtil.successResult("删除工作历类型成功");
    }
    
    
    /**
     * 模板创建工作历信息--老版服务
     * 
     * @param from_id 来源ID
     * @param workCalendar
     * @return
     */
    @Transactional
	public boolean createOldWorkCalendarFromId(String from_id, String projectId, WorkCalendar workCalendar) {
    	// 工作历信息插入
    	boolean result = this.workCalendarService.save(workCalendar);;
    	if (!result) {
			throw new BusinessException(ResponseCode.B0001.getCode(), "创建工作历信息失败");
		}
    	// 新工作历ID
    	String calendarId = workCalendar.getId();
    	
    	// 获取规则信息
    	QueryWrapper<WorkCalendarRule> queryRule = new WorkCalendarRule.Builder().createQueryWrapper()
    			.calendarIdEq(from_id, null).projectIdEq(workCalendar.getProjectId(), null)
    			.delFlagEq(WorkCalendarConstant.WORK_CALENDAR_DEL_FLAG_NOR).builderQueryWrapper();
    	List<WorkCalendarRule> ruleList = this.workCalendarRuleService.list(queryRule);
    	
    	WorkCalendarRule calendarRule = null;
    	if (CollectionUtil.isNotEmpty(ruleList)) {
			for (WorkCalendarRule rule : ruleList) {
				calendarRule = new WorkCalendarRule();
				calendarRule.setCalendarId(calendarId);
				calendarRule.setDictType(rule.getDictType());
				calendarRule.setDictCode(rule.getDictCode());
				calendarRule.setValue(rule.getValue());
				calendarRule.setProjectId(projectId);
				calendarRule.setRuleFlag(rule.getRuleFlag());
				result = this.workCalendarRuleService.save(calendarRule);
				if (!result) {
					throw new BusinessException(ResponseCode.B0001.getCode(), "级联创建规则信息失败");
				}
			}
		}
    	// 获取作息时间信息
    	QueryWrapper<WorkCalendarDate> queryDate = new WorkCalendarDate.Builder().createQueryWrapper()
    			.calendarIdEq(from_id, null).projectIdEq(workCalendar.getProjectId(), null).builderQueryWrapper();
    	List<WorkCalendarDate> dateList = this.workCalendarDateService.list(queryDate);
    	
    	WorkCalendarDate calendarDate = null;
    	if (CollectionUtil.isNotEmpty(dateList)) {
    		for (WorkCalendarDate date : dateList) {
    			calendarDate = new WorkCalendarDate();
    			calendarDate.setCalendarId(calendarId);
    			calendarDate.setProjectId(projectId);
    			calendarDate.setDictType(date.getDictType());
    			calendarDate.setDictCode(date.getDictCode());
    			calendarDate.setWorkTime(date.getWorkTime());
    			calendarDate.setCustomFlag(WorkCalendarConstant.CUSTOM_CALENDAR_DATE_NO);
    			calendarDate.setWorkDateStart(date.getWorkDateStart());
    			calendarDate.setWorkDateEnd(date.getWorkDateEnd());
    			result = this.workCalendarDateService.save(calendarDate);
				if (!result) {
					throw new BusinessException(ResponseCode.B0001.getCode(), "级联创建作息时间信息失败");
				}
    		}
    	}
    	
    	return result;
	}
    
    /**
     * 老版服务删除工作历，级联删除，工作历和规则
     * @param id
     * @return
     */
    @Transactional
	public ResponseOldResult deleteWorkCalendar(String id) {
		boolean result = workCalendarService.deleteWorkCalendar(id, null);
		if (result) {
			WorkCalendarRule workCalendarRule = new WorkCalendarRule();
			workCalendarRule.setCalendarId(id);
			result = workCalendarRuleService.deleteWorkCalendarRule(workCalendarRule, null);
			if (!result) {
				return ResponseOldResultUtil.errorResultMsg("工作历删除成功，对应规则删除失败");
			}
		}
		return ResponseOldResultUtil.successResult();
	}
    
    /**
     * workdateStart[start~end] OR workDateEnd[start-end] 是否如此待定
     * 解析老版工作历规则，组装响应数据
     * @param requestData
     */
	public List<OldCalendarResponseVO> queryOldCalendarDetails(OldCalendarQueryVO requestData) {
		// 获取数据,tag由于需要like in 不在sql语句中处理，在此对返回结果做处理
		QueryWrapper<WorkCalendarDate> queryWrapper = new WorkCalendarDate.Builder().createQueryWrapper()
				.calendarIdEq(requestData.getId(), null).dictCodeIn(requestData.getIntervals())
				.workDateStartGe(requestData.getDates().getFrom()).workDateEndLe(requestData.getDates().getTo())
				.builderQueryWrapper();
		// 其中的数据包括作息日期 作息时间段
		List<WorkCalendarDate> dateList = this.workCalendarDateService.list(queryWrapper);
		if (CollectionUtil.isEmpty(dateList)) {
			dateList = new ArrayList<WorkCalendarDate>();
		}
		
		// 再获取规则数据
		QueryWrapper<WorkCalendarRule> queryRule = new WorkCalendarRule.Builder()
				.createQueryWrapper().calendarIdEq(requestData.getId(), null)
				.delFlagEq(WorkCalendarConstant.WORK_CALENDAR_DEL_FLAG_NOR)
				.ruleFlagEq(WorkCalendarConstant.WORK_CALENDAR_RULE_FLAG_OLD).builderQueryWrapper();
		List<WorkCalendarRule> ruleList = this.workCalendarRuleService.list(queryRule);
		if (CollectionUtil.isEmpty(ruleList)) {
			ruleList = new ArrayList<WorkCalendarRule>();
		}
		
		return this.handleDataList(dateList, ruleList, requestData);
	}
	
	/**
	 * 组装响应数据，并解析规则
	 * @param dateList
	 * @param ruleList
	 * @param tags 
	 * @return
	 */
	private List<OldCalendarResponseVO> handleDataList(List<WorkCalendarDate> dateList, 
			List<WorkCalendarRule> ruleList, OldCalendarQueryVO requestData) {
		List<OldCalendarResponseVO> result = new ArrayList<OldCalendarResponseVO>();
		OldCalendarResponseVO responseVO = null;
		OldCalendarIntervalCreateVO interval = null;
		List<OldCalendarIntervalCreateVO> intervals = null;
		
		// 条件
		Set<String> tagsCon = requestData.getTags();
		String workFrom = requestData.getDates().getFrom();
		String workTo = requestData.getDates().getTo();
		
		// 获取天数差
		Date startCon = DateUtil.parse(workFrom, DateUtil.FORMAT_DATE_YYYYMMDDHHMMSS);
		Date endCon = DateUtil.parse(workTo, DateUtil.FORMAT_DATE_YYYYMMDDHHMMSS);
		long betweenDay = DateUtil.between(startCon, endCon, DateUnit.DAY);
		
		// 开始~结束日期
		String from = null;
		String to = null;
		// 开始~结束日期对应的date类型
		Date start = null;
		Date end = null;
		// 偏移后的某天
		Date offsetDate = null;
		String formatDate = null;
		Set<String> tags = null;
		for(int i = 0; i <= betweenDay; i++) {
			responseVO = new OldCalendarResponseVO();
			interval = new OldCalendarIntervalCreateVO();
			intervals = new ArrayList<OldCalendarIntervalCreateVO>();
			tags = new HashSet<String>();
			
			// 计算出偏移后日期
			offsetDate = DateUtil.offset(startCon, DateField.DAY_OF_YEAR, i);		// 偏移后时间
			formatDate = DateUtil.format(offsetDate, DateUtil.FORMAT_DATE_YYYYMMDDHHMMSS);
			responseVO.setDate(formatDate);
			
			for (WorkCalendarDate calendarDate : dateList) {
				// 判断是否满足tags条件，不论满足不满足都需要去解析规则，判断此天是否有对应的tag
				if (CollectionUtil.isNotEmpty(tagsCon) 
						&& tagsCon.contains(calendarDate.getDictType().split("-")[0])) {
					tags.add(calendarDate.getDictType());	// 添加date对应的tag
				}
				
				// 解析规则，判断此天是否有着对应的规则信息，规则不包含code，只有tag
				if (CollectionUtil.isNotEmpty(ruleList)) {
					this.parseCalendarRules(ruleList, tags, offsetDate, formatDate);
				}
				
				// 获取作息日期段
				from = calendarDate.getWorkDateStart();
				to = calendarDate.getWorkDateEnd();
				start = DateUtil.parse(from, DateUtil.FORMAT_DATE_YYYYMMDDHHMMSS);
				end = DateUtil.parse(to, DateUtil.FORMAT_DATE_YYYYMMDDHHMMSS);
				
				// 判断当前日期，是否位于此段时间内 [start~end]
				if (offsetDate.equals(start) || offsetDate.equals(end)
						|| (offsetDate.after(start) && offsetDate.before(end))) {
					// 满足条件，封装code、tag、time信息，intervals已经作为sql条件的一部分，这里不再过滤
					String workTime = calendarDate.getWorkTime();
					interval.setCode(calendarDate.getDictCode());
					if (StringUtil.isNotBlank(workTime)) {
						String[] split = workTime.split(",");
						interval.setTime(Arrays.asList(split));
					}
					intervals.add(interval);
				}
			}
			
			// 添加进响应对象中
			responseVO.setDate(formatDate);
			responseVO.setTags(tags);
			responseVO.setIntervals(intervals);
			result.add(responseVO);
		}
		
		return result;
	}
	
	/**
	 * 解析老版服务中的规则信息，判断是否需要添加tag
	 * @param ruleList
	 * @param tags
	 * @param offsetDate 这一天
	 * @param formatDate 这一天的字符串形式，YYYYMMDDHHMMSS
	 */
	private void parseCalendarRules(List<WorkCalendarRule> ruleList, Set<String> tags, Date offsetDate, String formatDate) {
		// 获取月日
		String mmddStr = formatDate.substring(4, 8);
		Integer mmdd = Integer.valueOf(mmddStr);
		
		String tag = null;
		String type = null;
		Set<String> values = null;
		for (WorkCalendarRule rule : ruleList) {
			tag = rule.getDictType();
			type = rule.getDictCode();
			values = rule.getValue();
			
			switch (type) {
				case WorkCalendarConstant.WORK_CALENDAR_D_OF_W:	 //周
					// 1表示周日，2表示周一
					int dayOfWeek = DateUtil.dayOfWeek(offsetDate);	
					dayOfWeek = dayOfWeek - 1;
					if (dayOfWeek == 0) {
						dayOfWeek = 7;
					}
					String num = String.valueOf(dayOfWeek);
					if (values.contains(num)) {
						tags.add(tag);
					}
					break;
				case WorkCalendarConstant.WORK_CALENDAR_D_OF_M:	 //月
					// 当前日期为此月的第几天
					int ofMonth = DateUtil.dayOfMonth(offsetDate);
					String day = String.valueOf(ofMonth);
					Iterator<String> iterator = values.iterator();
					while(iterator.hasNext()) {
						String next = iterator.next();
						if (next.contains("-")) {
							String[] split2 = next.split("-");
							int start = Integer.parseInt(split2[0]);
							int end = Integer.parseInt(split2[1]);
							if (ofMonth >= start && ofMonth <= end) {
								tags.add(tag);
								break;
							}
						} else if (day.equals(next)) {
							tags.add(tag);
							break;
						}
					}
					break;
				case WorkCalendarConstant.WORK_CALENDAR_M_AND_D: //年
					iterator = values.iterator();
					while(iterator.hasNext()) {
						String next = iterator.next();
						if (next.contains("-")) {
							String[] split2 = next.split("-");
							Integer startInt = Integer.valueOf(split2[0]);
							Integer endInt = Integer.valueOf(split2[1]);
							if (mmdd >= startInt && mmdd <= endInt) {
								tags.add(tag);
								break;
							}
						} else if (mmddStr.equals(next)) {
							tags.add(tag);
							break;
						}
					}
					break;
				default: 
					break;
			}
		}
	}
	
}
