package com.goafanti.admin.controller;



import com.goafanti.admin.service.DepartmentService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.model.WorkingHours;
import com.goafanti.common.utils.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;

@Controller
@RequestMapping(value = "/api/admin/department")
public class AdminDepartmentApiController extends CertifyApiController {

	@Resource
	private DepartmentService 	departmentService;

	/**
	 * 新增工作时间
	 */
	@RequestMapping(value = "/workingHours/add", method = RequestMethod.POST)
	public Result add(WorkingHours in) {
		Result res = new Result();
		if (in.getType()==null||StringUtils.isBlank(in.getName())||
				StringUtils.isBlank(in.getStart())||StringUtils.isBlank(in.getRestStart())||
				StringUtils.isBlank(in.getEnd())||StringUtils.isBlank(in.getRestEnd())) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", ""));
			return res;
		}
		if (departmentService.checkWorkingHoursType(in)) {
			res.getError().add(buildError(ErrorConstants.PARAM_BEING_ERROR, "", "分类"));
			return res;
		}
		res.setData(departmentService.addWorkingHours(in));
		return res;
	}


	/**
	 * 新增工作时间
	 */
	@RequestMapping(value = "/workingHours/update", method = RequestMethod.POST)
	public Result update(WorkingHours in) {
		Result res = new Result();
		if (departmentService.checkWorkingHoursType(in)) {
			res.getError().add(buildError(ErrorConstants.PARAM_BEING_ERROR, "", "分类"));
			return res;
		}
		res.setData(departmentService.updateWorkingHours(in));
		return res;
	}

	/**
	 * 删除工作时间
	 */
	@RequestMapping(value = "/workingHours/delete", method = RequestMethod.POST)
	public Result delete(Integer id) {
		Result res = new Result();
		if (id==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", ""));
			return res;
		}
		if (departmentService.checkDepWorkingHouresType(id)) {
			res.getError().add(buildError( "已分配无法删除！。", "已分配无法删除！"));
			return res;
		}
		res.setData(departmentService.deleteWorkingHours(id));
		return res;
	}

	/**
	 * 工作时间列表
	 */
	@RequestMapping(value = "/workingHours/list", method = RequestMethod.GET)
	public Result list() {
		Result res = new Result();
		res.setData(departmentService.selectWorkingHours());
		return res;
	}

	/**
	 * 工作时间列表
	 */
	@RequestMapping(value = "/workingHours/get", method = RequestMethod.GET)
	public Result get(String depId) {
		Result res = new Result();
		if (depId==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", ""));
			return res;
		}
		res.setData(departmentService.getWorkingHours(depId));
		return res;
	}
}
