package com.persagy.calendar.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.persagy.calendar.constant.WorkCalendarConstant;
import com.persagy.calendar.handle.WorkCalendarDateHandler;
import com.persagy.calendar.pojo.dto.WorkCalendar;
import com.persagy.calendar.pojo.dto.WorkCalendarDate;
import com.persagy.calendar.pojo.dto.WorkCalendarObject;
import com.persagy.calendar.pojo.vo.CalendarDateResult;
import com.persagy.calendar.pojo.vo.CustomCalendarVO;
import com.persagy.calendar.pojo.vo.WorkCalendarDateCreateVO;
import com.persagy.calendar.pojo.vo.WorkCalendarDateDeleteVO;
import com.persagy.calendar.pojo.vo.WorkCalendarDateQueryVO;
import com.persagy.calendar.pojo.vo.WorkCalendarDateUpdateVO;
import com.persagy.calendar.pojo.vo.WorkCalendarMoreDateCreateVO;
import com.persagy.calendar.service.IWorkCalendarDateService;
import com.persagy.calendar.service.IWorkCalendarDictService;
import com.persagy.calendar.service.IWorkCalendarObjectService;
import com.persagy.calendar.service.IWorkCalendarService;
import com.persagy.common.enums.ResponseCode;
import com.persagy.common.exception.BusinessException;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 工作历作息时间表
 * 
 * @version 1.0.0
 * @company persagy 
 * @author zhangqiankun
 * @date 2020-10-03 13:54:22
 */
@Api(tags = "工作历作息时间")
@RestController
@RequestMapping(value = "/v2.0/workCalendarDate", method = RequestMethod.POST)
public class WorkCalendarDateController {
  
	@Value("${integrated.calendar.group-code:BR}")
	private String defaultGroupCode;
	
    @Autowired
    private IWorkCalendarService workCalendarService;
	
	@Autowired
	private WorkCalendarDateHandler workCalendarDateHandler;

    @Autowired
    private IWorkCalendarDateService workCalendarDateService;
    
	@Autowired
	private IWorkCalendarDictService workCalendarDictService;
    
    @Autowired
    private IWorkCalendarObjectService workCalendarObjectService;
    
    /**
     * 列表查询，这里可不需要集团编码
     */
    @ApiOperation(value = "列表查询")
    @RequestMapping(value = "queryWorkCalendarDateList")
    public ResponseResult queryWorkCalendarDateList(@RequestBody @Validated WorkCalendarDateQueryVO queryVO) {
    	/*if (StringUtil.isBlank(queryVO.getProjectId())) {
			return ResponseUtil.errorResult("项目ID不可为空");
		}*/
    	
    	if (StringUtil.isNotBlank(queryVO.getObjId())) {
    		QueryWrapper<WorkCalendarObject> queryWrapper = new WorkCalendarObject.Builder().createQueryWrapper()
    				.projectIdEq(queryVO.getProjectId()).objectIdEq(queryVO.getObjId()).builderQueryWrapper();
			WorkCalendarObject calendarObject = this.workCalendarObjectService.getOne(queryWrapper);
			if (calendarObject == null || StringUtil.isBlank(calendarObject.getCalendarId())) {
				return ResponseUtil.errorResult("此对象ID，不存在工作历作息时间信息");
			}
			if (StringUtil.isNotBlank(queryVO.getCalendarId()) && !queryVO.getCalendarId().equals(calendarObject.getCalendarId())) {
				return ResponseUtil.errorResult("此对象ID，不属于此工作历");
			}
			queryVO.setCalendarId(calendarObject.getCalendarId());
		}
		List<CalendarDateResult> dateList = this.workCalendarDateHandler.queryWorkCalendarDateList(queryVO);
		return ResponseUtil.successResult(dateList, CollectionUtil.isEmpty(dateList) ? 0L : dateList.size());
    }
    
	/**
     * 添加自定义作息时间
     * 
     * 自定义作息时间，一定是属于工作历级别的，这一点不允许修改，不然规则解析需要推翻重来，改动很大
     */
	@ApiOperation(value = "添加自定义作息时间")
	@RequestMapping(value = "createCustomCalendarDate")
    public ResponseResult createCustomCalendarDate(@RequestBody @Validated CustomCalendarVO<WorkCalendarDateCreateVO> createVO) {
		if (StringUtil.isBlank(createVO.getGroupCode())) {
			createVO.setGroupCode(defaultGroupCode);
		}
		
		this.workCalendarDateHandler.batchCreateCalendarDate(createVO);
		return ResponseUtil.successResult("添加成功");
	}
	
	/**
     * 多时间段添加工作历作息时间信息，且自定义的开始时间需大于当日，先删除此dictType下数据，再添加
     * 
     * 自定义作息时间，一定是属于工作历级别的，这一点不允许修改，不然规则解析需要推翻重来，改动很大
     */
	@ApiOperation(value = "多时间段添加工作历作息时间信息")
	@RequestMapping(value = "createCustomCalendarMoreDate")
    public ResponseResult createCustomCalendarMoreDate(@RequestBody @Validated WorkCalendarMoreDateCreateVO createVO) {
		/*if (BooleanUtil.isFalse(createVO.getIsDelete()) && CollectionUtil.isEmpty(createVO.getTimetable())) {
			return ResponseUtil.errorResult(ResponseCode.A0400.getCode(), "作息时间点集合timetable，不可为空");
		}*/
		if (StringUtil.isBlank(createVO.getGroupCode())) {
			createVO.setGroupCode(defaultGroupCode);
		}
		
		// 判断日期是否合法
		boolean thanToday = WorkCalendarDateHandler.isMoreThanToday(createVO.getWorkDateStart());
		if (!thanToday) {
			return ResponseUtil.errorResult(ResponseCode.A0402.getCode(), "开始作息日期不合法，历史时间不允许再添加自定义作息时间点");
		}
		// 判断工作历类型是否存在
		int dictTypeLevel = this.workCalendarDictService.getDictTypeLevel(createVO.getGroupCode(), createVO.getDictType());
		WorkCalendar calendar = null;
		if (dictTypeLevel == 2) {
			calendar = this.workCalendarService.getById(createVO.getCalendarId(), createVO.getGroupCode(), createVO.getProjectId());
			if (calendar == null) {
				throw new BusinessException("工作历类型不存在");
			}
		}
		this.workCalendarDateHandler.batchCreateCalendarMoreDate(createVO, calendar);
		return ResponseUtil.successResult("添加成功");
	}
	
    /**
     * 更新自定义作息时间
     * 
     * 自定义作息时间，一定是属于工作历级别的，这一点不允许修改，不然规则解析需要推翻重来，改动很大
     */
	@ApiOperation(value = "更新自定义作息时间")
	@RequestMapping(value = "updateCustomCalendarDate")
    public ResponseResult updateCustomCalendarDate(@RequestBody @Validated CustomCalendarVO<WorkCalendarDateUpdateVO> updateVO) {
		this.workCalendarDateHandler.batchUpdateCalendarDate(updateVO);
		return ResponseUtil.successResult("更新成功");
	}
	
    /**
     * 物理删除自定义作息时间，自定义的时间需大于当日
     */
	@ApiOperation(value = "物理删除自定义作息时间")
	@RequestMapping(value = "deleteCustomCalendarDate")
    public ResponseResult deleteCustomCalendarDate(@RequestBody @Validated WorkCalendarDateDeleteVO deleteVO) {
		if (StringUtil.isBlank(deleteVO.getGroupCode())) {
			deleteVO.setGroupCode(defaultGroupCode);
		}
		int dictTypeLevel = this.workCalendarDictService.getDictTypeLevel(deleteVO.getGroupCode(), deleteVO.getDictType());
		
		// 判断日期是否合法
		boolean thanToday = WorkCalendarDateHandler.isMoreThanToday(deleteVO.getWorkDateStart());
		if (!thanToday) {
			return ResponseUtil.errorResult(ResponseCode.A0402.getCode(), "作息日期不合法，历史数据不允许被删除");
		}
		QueryWrapper<WorkCalendarDate> queryWrapper = new WorkCalendarDate.Builder().createQueryWrapper().groupCodeEq(deleteVO.getGroupCode())
				.workDateGe(deleteVO.getWorkDateStart()).workDateLe(deleteVO.getWorkDateEnd())
				.projectIdEq(deleteVO.getProjectId(), dictTypeLevel).idEq(deleteVO.getId())
				.dictTypeEq(deleteVO.getDictType()).dictCodeIn(deleteVO.getDictCode())
				.customFlagEq(WorkCalendarConstant.CUSTOM_CALENDAR_DATE_YES).builderQueryWrapper();
		boolean result = this.workCalendarDateService.remove(queryWrapper);
		return result ? ResponseUtil.successResult("删除成功") : ResponseUtil.errorResult("删除失败");
	}
	
}
