package com.persagy.calendar.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.persagy.calendar.handle.WorkCalendarHandler;
import com.persagy.calendar.pojo.dto.OldWorkCalendar;
import com.persagy.calendar.pojo.dto.WorkCalendar;
import com.persagy.calendar.pojo.old.OldCalendarIntervalCreateVO;
import com.persagy.calendar.pojo.old.OldCalendarIntervalDeleteVO;
import com.persagy.calendar.pojo.old.OldCalendarIntervalsByTag;
import com.persagy.calendar.pojo.old.OldCalendarIntervalsVO;
import com.persagy.calendar.pojo.old.OldCalendarQueryVO;
import com.persagy.calendar.pojo.old.OldCalendarResponseVO;
import com.persagy.calendar.pojo.old.OldCalendarRuleCreateVO;
import com.persagy.calendar.pojo.old.OldCalendarRuleDeleteVO;
import com.persagy.calendar.pojo.old.OldCalendarRuleVO;
import com.persagy.calendar.pojo.old.OldCalendarTagsVO;
import com.persagy.calendar.pojo.old.OldWorkCalendarVO;
import com.persagy.calendar.service.IWorkCalendarService;
import com.persagy.calendar.service.OldWorkCalendarDateService;
import com.persagy.calendar.utils.ResponseOldResult;
import com.persagy.calendar.utils.ResponseOldResultUtil;
import com.persagy.common.constant.HistoryConstant;
import com.persagy.common.utils.StringUtil;

import cn.hutool.core.collection.CollectionUtil;

/**
 * @version 
 * @description 老服务兼容controller
 * @company persagy
 * @author zhangqiankun
 * @since 2020年10月3日:	下午2:41:21
 */

@RestController
@RequestMapping(value = "/data-platform-3/calendar", method = RequestMethod.POST)
public class OldWorkCalendarController {
	
	// 老版工作历默认projectId
	private static final String DEFAULT_PROJECT_ID = HistoryConstant.DEFAULT_PROJECT_ID;
	
	@Autowired
    private IWorkCalendarService workCalendarService;
    
    @Autowired
    private WorkCalendarHandler workCalendarHandler;
    
    @Autowired
    private OldWorkCalendarDateService oldWorkCalendarDateService;
	
    /**
     * 创建工作历,name groupId联合唯一
     */
	@RequestMapping(value = "create")
	public ResponseOldResult create(@RequestBody @Validated OldWorkCalendarVO oldWorkCalendar) {
		// 判断是否唯一
		boolean result = workCalendarService.checkProjetcIdAndName(null, DEFAULT_PROJECT_ID, null, oldWorkCalendar.getName());
		if (!result) {
			return ResponseOldResultUtil.errorResultMsg("name已存在");
		}
		
		WorkCalendar workCalendar = new WorkCalendar();
		workCalendar.setProjectId(DEFAULT_PROJECT_ID);
		workCalendar.setCalendarName(oldWorkCalendar.getName());
		workCalendar.setCalendarDesc(oldWorkCalendar.getDesc());
		result = workCalendarService.save(workCalendar);
		
		return result ? ResponseOldResultUtil.successResultId(workCalendar.getId())
				: ResponseOldResultUtil.errorResultMsg("创建工作历失败");
	}
	
	/**
     * 创建一套与已有工作历内容一样的工作历,name groupId联合唯一
     */
	@RequestMapping(value = "create_from")
	public ResponseOldResult create_from(@RequestBody @Validated OldWorkCalendarVO oldWorkCalendar) {
		String from_id = oldWorkCalendar.getFrom_id();
		if (StringUtil.isBlank(from_id)) {
			return ResponseOldResultUtil.errorResultMsg("from_id不可为空");
		}
		//判断原信息是否存在
		WorkCalendar calendar = workCalendarService.getById(from_id, null, DEFAULT_PROJECT_ID);
		if (calendar == null) {
			return ResponseOldResultUtil.errorResultMsg("原工作历信息不存在");
		}
		
		// 判断是否唯一
		boolean result = workCalendarService.checkProjetcIdAndName(null, DEFAULT_PROJECT_ID, null, oldWorkCalendar.getName());
		if (!result) {
			return ResponseOldResultUtil.errorResultMsg("name已存在");
		}
		
		WorkCalendar workCalendar = new WorkCalendar();
		workCalendar.setProjectId(DEFAULT_PROJECT_ID);
		workCalendar.setCalendarName(oldWorkCalendar.getName());
		workCalendar.setCalendarDesc(oldWorkCalendar.getDesc());
		result = this.workCalendarHandler.createOldWorkCalendarFromId(from_id, DEFAULT_PROJECT_ID, workCalendar);
		return result ? ResponseOldResultUtil.successResultId(workCalendar.getId()) : ResponseOldResultUtil.errorResultMsg("创建工作历失败");
	}
	
    /**
     * 修改工作历,name groupId联合唯一
     */
	@RequestMapping(value = "update")
	public ResponseOldResult update(@RequestBody @Validated OldWorkCalendarVO oldWorkCalendar) {
		String id = oldWorkCalendar.getId();
		if (StringUtil.isBlank(id)) {
			return ResponseOldResultUtil.errorResultMsg("id不可为空");
		}
		// 判断是否唯一
		boolean result = workCalendarService.checkProjetcIdAndName(id, DEFAULT_PROJECT_ID, null, oldWorkCalendar.getName());
		if (!result) {
			return ResponseOldResultUtil.errorResultMsg("name已存在");
		}
		
		WorkCalendar workCalendar = new WorkCalendar();
		workCalendar.setId(id);
		workCalendar.setProjectId(DEFAULT_PROJECT_ID);
		workCalendar.setCalendarName(oldWorkCalendar.getName());
		workCalendar.setCalendarDesc(oldWorkCalendar.getDesc());
		result = workCalendarService.updateWorkCalendar(workCalendar);
		
		return result ? ResponseOldResultUtil.successResult()
				: ResponseOldResultUtil.errorResultMsg("更新工作历失败");
	}
	
	/**
     * 删除工作历，级联删除规则
     */
	@RequestMapping(value = "delete")
	public ResponseOldResult delete(@RequestBody Map<String, String> requestMap) {
		String id = requestMap.get("id");
		if (StringUtil.isBlank(id)) {
			return ResponseOldResultUtil.errorResultMsg("id不可为空");
		}
		
		return this.workCalendarHandler.deleteWorkCalendar(id);
	}
	
	/**
     * 查询工作历,默认只查老的工作历信息
     * 无需参数
     */
	@RequestMapping(value = "query")
	public ResponseOldResult query(@RequestBody Map<String, String> map) {
		// 默认只查正常的
		OldWorkCalendar oldWorkCalendar = new OldWorkCalendar();
		oldWorkCalendar.setProjectId(DEFAULT_PROJECT_ID);
		List<OldWorkCalendar> calendarList = oldWorkCalendarDateService.queryOldCalendarList(oldWorkCalendar);
		return ResponseOldResultUtil.successResultContent(calendarList, 
				CollectionUtil.isEmpty(calendarList) ? 0 : calendarList.size());
	}
	
	/**
     * 创建规则
     */
	@RequestMapping(value = "create_rules")
	public ResponseOldResult createRules(@RequestBody @Validated OldCalendarRuleVO<OldCalendarRuleCreateVO> requestData) {
		boolean result = this.oldWorkCalendarDateService.createOldCalendarRule(requestData);
		return result ? ResponseOldResultUtil.successResultContent("", requestData.getRules().size())
				: ResponseOldResultUtil.errorResultMsg("创建规则失败");
	}
	
	/**
     * 删除规则
     */
	@RequestMapping(value = "del_rules")
	public ResponseOldResult delRules(@RequestBody @Validated OldCalendarRuleVO<OldCalendarRuleDeleteVO> requestData) {
		boolean result = this.oldWorkCalendarDateService.deleteOldCalendarRule(requestData);
		return result ? ResponseOldResultUtil.successResultContent("", requestData.getRules().size())
				: ResponseOldResultUtil.errorResultMsg("删除规则失败");
	}
	
	/**
     * 更新规则
     */
	@RequestMapping(value = "update_rule")
	public ResponseOldResult updateRule(@RequestBody @Validated OldCalendarRuleCreateVO requestData) {
		// 判断工作历信息是否存在
		WorkCalendar calendar = workCalendarService.getById(requestData.getId(), null, DEFAULT_PROJECT_ID);
		if (calendar == null) {
			return ResponseOldResultUtil.errorResultMsg("工作历信息不存在");
		}
		
		boolean result = this.oldWorkCalendarDateService.updateOldCalendarRule(requestData);
		return result ? ResponseOldResultUtil.successResult()
				: ResponseOldResultUtil.errorResultMsg("更新规则失败");
	}
	
	/**
     * 查询规则
     * id 必填 tag选填
     */
	@RequestMapping(value = "query_rules")
	public ResponseOldResult queryRules(@RequestBody @Validated Map<String, String> requestData) {
		String id = requestData.get("id");
		if (StringUtil.isBlank(id)) {
			return ResponseOldResultUtil.errorResultMsg("id不可为空");
		}
		List<OldWorkCalendar> list = this.oldWorkCalendarDateService.queryOldCalendarRuleList(id, requestData.get("tag"), DEFAULT_PROJECT_ID);
		return ResponseOldResultUtil.successResultContent(list, CollectionUtil.isEmpty(list) ? 0 : list.size());
	}
	
	/**
     * 设置作息日期
     */
	@RequestMapping(value = "set_tags")
	public ResponseOldResult setTags(@RequestBody @Validated OldCalendarTagsVO requestData) {
		// 判断工作历信息是否存在
		WorkCalendar calendar = workCalendarService.getById(requestData.getId(), null, DEFAULT_PROJECT_ID);
		if (calendar == null) {
			return ResponseOldResultUtil.errorResultMsg("工作历信息不存在");
		}
		
		boolean result = this.oldWorkCalendarDateService.setTags(requestData.getId(), requestData.getTags());
		return result ? ResponseOldResultUtil.successResult() : ResponseOldResultUtil.errorResultMsg("设置作息日期失败");
	}
	
	/**
     * 删除作息日期
     */
	@RequestMapping(value = "unset_tags")
	public ResponseOldResult unsetTags(@RequestBody @Validated OldCalendarTagsVO requestData) {
		// 判断工作历信息是否存在
		WorkCalendar calendar = workCalendarService.getById(requestData.getId(), null, DEFAULT_PROJECT_ID);
		if (calendar == null) {
			return ResponseOldResultUtil.errorResultMsg("工作历信息不存在");
		}
		
		boolean result = this.oldWorkCalendarDateService.unsetTags(requestData.getId(), requestData.getTags());
		return result ? ResponseOldResultUtil.successResult()
				: ResponseOldResultUtil.errorResultMsg("删除作息日期失败");
	}
	
	/**
     * 设置作息时间段
     */
	@RequestMapping(value = "set_intervals")
	public ResponseOldResult setIntervals(@RequestBody @Validated OldCalendarIntervalsVO<OldCalendarIntervalCreateVO> requestData) {
		// 判断工作历信息是否存在
		WorkCalendar calendar = workCalendarService.getById(requestData.getId(), null, DEFAULT_PROJECT_ID);
		if (calendar == null) {
			return ResponseOldResultUtil.errorResultMsg("工作历信息不存在");
		}
		
		boolean result = this.oldWorkCalendarDateService.setIntervals(requestData.getId(), requestData.getIntervals());
		return result ? ResponseOldResultUtil.successResult()
				: ResponseOldResultUtil.errorResultMsg("设置作息日期失败");
	}
	
	/**
     * 删除作息时间段
     */
	@RequestMapping(value = "unset_intervals")
	public ResponseOldResult unsetIntervals(@RequestBody @Validated OldCalendarIntervalsVO<OldCalendarIntervalDeleteVO> requestData) {
		// 判断工作历信息是否存在
		WorkCalendar calendar = workCalendarService.getById(requestData.getId(), null, DEFAULT_PROJECT_ID);
		if (calendar == null) {
			return ResponseOldResultUtil.errorResultMsg("工作历信息不存在");
		}
		
		boolean result = this.oldWorkCalendarDateService.unsetIntervals(requestData.getId(), requestData.getIntervals());
		return result ? ResponseOldResultUtil.successResult()
				: ResponseOldResultUtil.errorResultMsg("设置作息日期失败");
	}
	
	/**
     * 根据tag设置作息时段
     */
	@RequestMapping(value = "set_intervals_by_tag")
	public ResponseOldResult setIntervalsByTag(@RequestBody @Validated OldCalendarIntervalsByTag requestData) {
		// 判断工作历信息是否存在
		WorkCalendar calendar = workCalendarService.getById(requestData.getId(), null, DEFAULT_PROJECT_ID);
		if (calendar == null) {
			return ResponseOldResultUtil.errorResultMsg("工作历信息不存在");
		}
		
		boolean result = this.oldWorkCalendarDateService.setIntervalsByTag(requestData);
		return result ? ResponseOldResultUtil.successResult()
				: ResponseOldResultUtil.errorResultMsg("设置作息日期失败");
	}
	
	/**
	 * 查询工作历详情，包含解析过程
	 * @param requestData
	 * @return
	 */
	@RequestMapping(value = "query_details")
	public ResponseOldResult queryDetails(@RequestBody @Validated OldCalendarQueryVO requestData) {
		// 判断工作历信息是否存在
		WorkCalendar calendar = workCalendarService.getById(requestData.getId(), null, DEFAULT_PROJECT_ID);
		if (calendar == null) {
			return ResponseOldResultUtil.errorResultMsg("工作历信息不存在");
		}
		
		if (CollectionUtil.isEmpty(requestData.getTags()) && CollectionUtil.isEmpty(requestData.getIntervals())) {
			return ResponseOldResultUtil.errorResultMsg("tags和intervals必填其一");
		}
		
		List<OldCalendarResponseVO> details = this.workCalendarHandler.queryOldCalendarDetails(requestData);
		return ResponseOldResultUtil.successResultContent(details, details.size());
	}
}
