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.google.common.collect.Lists;
import com.persagy.calendar.handle.WorkCalendarDateHandler;
import com.persagy.calendar.handle.WorkCalendarLabelHandler;
import com.persagy.calendar.pojo.dto.WorkCalendarLabel;
import com.persagy.calendar.pojo.vo.WorkCalendarLabelQueryVO;
import com.persagy.calendar.pojo.vo.WorkCalendarLabelUpdateVO;
import com.persagy.calendar.pojo.vo.WorkCalendarMoreDateCreateVO;
import com.persagy.calendar.pojo.vo.WorkLabelBatchUpdateVO;
import com.persagy.common.enums.ResponseCode;
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 
 * @description 
 * @company persagy
 * @author zhangqiankun
 * @since 2021年3月3日:	下午5:48:32
 */
@RestController
@Api(tags = "标签管理")
@RequestMapping(value = "/v2.0/calendarLabel", method = RequestMethod.POST)
public class WorkCalendarLabelController {
	
	@Value("${integrated.calendar.group-code:BR}")
	private String defaultGroupCode;
	
	@Autowired
	private WorkCalendarLabelHandler workCalendarLabelHandler;
	
	/**
     * 日期标签属性列表查询
     */
    @ApiOperation(value = "列表查询")
    @RequestMapping(value = "queryCalendarLabelList")
    public ResponseResult queryCalendarLabelList(@RequestBody @Validated WorkCalendarLabelQueryVO queryVO) {
    	if (StringUtil.isBlank(queryVO.getGroupCode())) {
			queryVO.setGroupCode(defaultGroupCode);
		}
		List<WorkCalendarLabel> dateList = this.workCalendarLabelHandler.queryCalendarLabelList(queryVO);
		if (CollectionUtil.isEmpty(dateList)) {
			return ResponseUtil.successResult(Lists.newArrayList(), 0L);
		}
		return ResponseUtil.successResult(dateList, (long)dateList.size());
    }
    
	/**
	 * 查询标签的有效期 dictType必须存在
	 * 
	 * @param queryVO 
	 * @return
	 */
    @ApiOperation(value = "查询标签的有效期")
    @RequestMapping(value = "queryCalendarLabelValid")
    public ResponseResult queryCalendarLabelValid(@RequestBody @Validated WorkCalendarLabelQueryVO queryVO) {
    	if (StringUtil.isBlank(queryVO.getGroupCode())) {
			queryVO.setGroupCode(defaultGroupCode);
		}
    	
    	if (StringUtil.isBlank(queryVO.getProjectId())) {
    		return ResponseUtil.errorResult(ResponseCode.A0400.getCode(), "项目ID，不可为空");
		}
    	if (StringUtil.isBlank(queryVO.getDictType())) {
    		return ResponseUtil.errorResult(ResponseCode.A0400.getCode(), "标签类型，不可为空");
		}
		List<WorkCalendarLabel> dateList = this.workCalendarLabelHandler.queryCalendarLabelValid(queryVO);
		if (CollectionUtil.isEmpty(dateList)) {
			return ResponseUtil.successResult(Lists.newArrayList(), 0L);
		}
		return ResponseUtil.successResult(dateList, (long)dateList.size());
    }
	
	/**
	 * 创建或更新日期标签属性
	 * @param updateVO
	 * @return
	 */
	@ApiOperation(value = "创建或更新日期标签属性")
    @RequestMapping(value = "updateLabelInfo")
	public ResponseResult updateLabelInfo(@RequestBody @Validated WorkCalendarLabelUpdateVO updateVO) {
		if (StringUtil.isBlank(updateVO.getGroupCode())) {
			updateVO.setGroupCode(defaultGroupCode);
		}
		
		ResponseResult validity = WorkCalendarLabelHandler.checkDataValidity(updateVO);
		if (validity != null) {
			return validity;
		}
		this.workCalendarLabelHandler.updateLabelInfo(updateVO);
		return ResponseUtil.successResult("更新成功");
	}
	
	/**
	 * 定义工作历单日属性
	 * @param updateVO
	 * @return
	 */
	@ApiOperation(value = "更新日期标签属性集")
    @RequestMapping(value = "updateCalendarDateAttribute")
	public ResponseResult updateCalendarDateAttribute(@RequestBody @Validated WorkLabelBatchUpdateVO updateVO) {
		if (StringUtil.isBlank(updateVO.getGroupCode())) {
			updateVO.setGroupCode(defaultGroupCode);
		}
		
		List<WorkCalendarLabelUpdateVO> labels = updateVO.getLabels();
		List<WorkCalendarMoreDateCreateVO> times = updateVO.getTimes();
		if (CollectionUtil.isNotEmpty(labels) || CollectionUtil.isNotEmpty(times)) {
			if (CollectionUtil.isNotEmpty(labels)) {
				ResponseResult validity = null;
				for (WorkCalendarLabelUpdateVO workCalendarLabelUpdateVO : labels) {
					validity = WorkCalendarLabelHandler.checkDataValidity(workCalendarLabelUpdateVO);
					if (validity != null) {
						return validity;
					}
				}
			}
			
			if (CollectionUtil.isNotEmpty(times)) {
				for (WorkCalendarMoreDateCreateVO workCalendarMoreDateCreateVO : times) {
					boolean valid = WorkCalendarDateHandler.isMoreThanToday(workCalendarMoreDateCreateVO.getWorkDateStart());
					if (!valid) {
						return ResponseUtil.errorResult("times中的开始日期必须大于当日");
					}
				}
			}
			this.workCalendarLabelHandler.updateCalendarDateAttribute(updateVO);
			return ResponseUtil.successResult("更新成功");
		}
		return ResponseUtil.errorResult("labels和times必须存在一个");
	}
	
}
