package com.persagy.calendar.handle;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

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

import com.persagy.calendar.pojo.dto.WorkCalendar;
import com.persagy.calendar.pojo.dto.WorkCalendarRule;
import com.persagy.calendar.pojo.vo.CustomCalendarVO;
import com.persagy.calendar.pojo.vo.WorkCalendarRuleCreateVO;
import com.persagy.calendar.pojo.vo.WorkCalendarRuleUpdateVO;
import com.persagy.calendar.service.IWorkCalendarDictService;
import com.persagy.calendar.service.IWorkCalendarRuleService;
import com.persagy.calendar.service.IWorkCalendarService;
import com.persagy.common.enums.ResponseCode;
import com.persagy.common.exception.BusinessException;

import cn.hutool.core.util.BooleanUtil;

/**
 * @version 定义规则信息
 * @description 
 * @company persagy
 * @author zhangqiankun
 * @since 2020年10月3日:	下午1:03:19
 */
@Component
public class WorkCalendarRuleHandler {
	
	@Autowired
	private IWorkCalendarService workCalendarService;
	
	@Autowired
	private IWorkCalendarDictService workCalendarDictService;
	
	@Autowired
	private IWorkCalendarRuleService workCalendarRuleService;
	
	/**
	 * 批量创建规则信息
	 * 
	 * @param createVO
	 */
	@Transactional
	public void batchCreateCalednarRule(CustomCalendarVO<WorkCalendarRuleCreateVO> createVO) {
		boolean result = false;
		WorkCalendarRule workCalendarRule = null;
		List<WorkCalendarRuleCreateVO> batchInfo = createVO.getBatchInfo();
		Set<String> calendarIds = new HashSet<String>();
		Map<String, Integer> dictTypeLevelMap = new HashMap<String, Integer>();
		for (WorkCalendarRuleCreateVO rule : batchInfo) {
			// 优先判断工作历信息是否存在
			if (!calendarIds.contains(rule.getCalendarId())) {
				WorkCalendar workCalendar = this.workCalendarService.getById(rule.getCalendarId(), createVO.getGroupCode(), createVO.getProjectId());
				if (workCalendar == null) {
					throw new BusinessException("工作历主信息不存在");
				}
			}
			// 判断当前字典类型级别
			Integer dictTypeLevel = dictTypeLevelMap.get(rule.getDictType());
			if (dictTypeLevel == null) {
				dictTypeLevel = this.workCalendarDictService.getDictTypeLevel(createVO.getGroupCode(), rule.getDictType());
				dictTypeLevelMap.put(rule.getDictType(), dictTypeLevel);
			}
			
			workCalendarRule = new WorkCalendarRule();
			BeanUtils.copyProperties(rule, workCalendarRule);
			workCalendarRule.setId(null);
			workCalendarRule.setGroupCode(createVO.getGroupCode());
			workCalendarRule.setProjectId(dictTypeLevel == 0 ? "0" : createVO.getProjectId());
			if (dictTypeLevel != 2) {
				workCalendarRule.setCalendarId("0");
			}
			workCalendarRule.setUpdateUser(createVO.getUserId());
			
			// 是否需要删除旧规则
			if (BooleanUtil.isTrue(createVO.getIsDelete()) && !calendarIds.contains(rule.getCalendarId())) {
				this.workCalendarRuleService.removeWorkCalendarRule(createVO.getGroupCode(), createVO.getProjectId(), null, rule.getCalendarId(), rule.getDictType(), dictTypeLevel);
			}
			result = this.workCalendarRuleService.createWorkCalendarRule(workCalendarRule);
			if (!result) {
				throw new BusinessException("工作历规则信息创建失败");
			}
			calendarIds.add(rule.getCalendarId());
		}
		calendarIds.clear();
	}
	
	/**
	 * 批量更新规则信息
	 */
	@Transactional
	public void batchUpdateCalednarRule(CustomCalendarVO<WorkCalendarRuleUpdateVO> updateVO) {
		boolean result = false;
		WorkCalendarRule workCalendarRule = null;
		List<WorkCalendarRuleUpdateVO> batchInfo = updateVO.getBatchInfo();
		Set<String> calendarIds = new HashSet<String>();
		Map<String, Integer> dictTypeLevelMap = new HashMap<String, Integer>();
		for (WorkCalendarRuleUpdateVO rule : batchInfo) {
			// 优先判断工作历信息是否存在
			if (!calendarIds.contains(rule.getCalendarId())) {
				WorkCalendar workCalendar = this.workCalendarService.getById(rule.getCalendarId(), updateVO.getGroupCode(), updateVO.getProjectId());
				if (workCalendar == null) {
					throw new BusinessException("工作历主信息不存在");
				}
			}
			// 再判断原规则数据是否存在
			WorkCalendarRule calendar = this.workCalendarRuleService.getById(rule.getId());
			if (calendar == null) {
				throw new BusinessException(ResponseCode.C0320.getDesc());
			}
			// 判断当前字典类型级别
			Integer dictTypeLevel = dictTypeLevelMap.get(rule.getDictType());
			if (dictTypeLevel == null) {
				dictTypeLevel = this.workCalendarDictService.getDictTypeLevel(updateVO.getGroupCode(), rule.getDictType());
				dictTypeLevelMap.put(rule.getDictType(), dictTypeLevel);
			}
			
			// 是否需要删除旧规则
			if (BooleanUtil.isTrue(updateVO.getIsDelete()) && !calendarIds.contains(rule.getCalendarId())) {
				this.workCalendarRuleService.removeWorkCalendarRule(updateVO.getGroupCode(), updateVO.getProjectId(), null, rule.getCalendarId(), rule.getDictType(), dictTypeLevel);
			}
			workCalendarRule = new WorkCalendarRule();
			BeanUtils.copyProperties(rule, workCalendarRule);
			workCalendarRule.setGroupCode(updateVO.getGroupCode());
			workCalendarRule.setProjectId(dictTypeLevel == 0 ? "0" : updateVO.getProjectId());
			if (dictTypeLevel != 2) {
				workCalendarRule.setCalendarId("0");
			}
			workCalendarRule.setUpdateUser(updateVO.getUserId());
			result = this.workCalendarRuleService.updateWorkCalendarRule(workCalendarRule);
			if (!result) {
				throw new BusinessException("工作历规则信息更新失败");
			}
			calendarIds.add(rule.getCalendarId());
		}
		calendarIds.clear();
	}
	
}
