package com.jeeplus.modules.API.works.approval;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.utils.ErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.workovertimeform.dao.WorkOvertimeFormDao;
import com.jeeplus.modules.workovertimeform.entity.WorkOvertimeForm;

@Controller
@RequestMapping(value = "${frontPath}/overtime")

public class OverTimeController extends BaseController {

	@Autowired
	private WorkOvertimeFormDao workOvertimeFormDao;

	/**
	 * 查询
	 * 
	 * @author 李韵轩
	 * @param userId
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "myOverTimeList", method = RequestMethod.GET)
	public AjaxJson myOverTimeList(String userId, HttpServletRequest request, HttpServletResponse response) {
		AjaxJson json = new AjaxJson();
		User user = new User();
		user.setId(userId);
		WorkOvertimeForm over = new WorkOvertimeForm();
		over.setUser(user);
		List<WorkOvertimeForm> list = workOvertimeFormDao.findList(over);
		json.put("data", list);
		json.setErrorCode(ErrorCode.code_1004);
		return json;
	}

	/**
	 * 新建加班申请
	 * 
	 * @author 朱明月
	 * @param request
	 * @param response
	 * @return
	 * @throws ParseException
	 */
	@ResponseBody
	@RequestMapping(value = "addOverTime", method = RequestMethod.POST)
	public AjaxJson addOverTime(HttpServletRequest request, HttpServletResponse response) throws ParseException {
		AjaxJson json = new AjaxJson();
		//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
		String id = request.getParameter("userId");
		String isHolidays = request.getParameter("isHolidays");
		String remarks = request.getParameter("remarks");
		String longTime = request.getParameter("longTime");
		User user = new User();
		WorkOvertimeForm over = new WorkOvertimeForm();
		user.setId(id);
		over.setUser(user);
		over.setStartTime(startTime);
		over.setEndTime(endTime);
		over.setLongTime(longTime);
		over.setIsHolidays(isHolidays);
		over.setRemarks(remarks);
		over.setCreateBy(user);
		over.setCreateBy(user);
		over.setUpdateBy(user);
		over.setCompanyId(UserUtils.getSelectCompany().getId());
		over.preInsert();
		workOvertimeFormDao.insert(over);
		json.setMsg("提交加班申请成功");
		json.setErrorCode(ErrorCode.code_1004);
		return json;
	}

	/**
	 * 修改加班申请
	 * 
	 * @throws ParseException
	 */
	@ResponseBody
	@RequestMapping(value = "updateOverTime", method = RequestMethod.POST)
	public AjaxJson updateOverTime(String overId, HttpServletRequest request, HttpServletResponse response)
			throws ParseException {
		AjaxJson json = new AjaxJson();
		//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
		String id = request.getParameter("userId");
		String isHolidays = request.getParameter("isHolidays");
		String remarks = request.getParameter("remarks");
		String longTime = request.getParameter("longTime");
		User user = new User();
		WorkOvertimeForm over = new WorkOvertimeForm();
		user.setId(id);
		if (workOvertimeFormDao.get(overId) != null) {
			if (remarks != null || remarks != "") {
				over.setRemarks(remarks);
			}
			if (startTime != null) {
				over.setStartTime(startTime);
			}
			if (endTime != null) {
				over.setEndTime(endTime);
			}
			if (longTime != null || longTime != "") {
				over.setLongTime(longTime);
			}
			if (isHolidays != null || isHolidays != "") {
				over.setIsHolidays(isHolidays);
			}
			if (user != null || remarks != "") {
				over.setCreateBy(user);
			}
		}
		over.preUpdate();
		workOvertimeFormDao.update(over);
		json.setMsg("提交加班申请成功");
		json.setErrorCode(ErrorCode.code_1004);
		return json;
	}

	/**
	 * 删除加班申请
	 * 
	 * @throws ParseException
	 */
	@ResponseBody
	@RequestMapping(value = "delOverTime", method = RequestMethod.GET)
	public AjaxJson delOverTime(String overId, HttpServletRequest request, HttpServletResponse response) {
		AjaxJson json = new AjaxJson();
		WorkOvertimeForm over = new WorkOvertimeForm();
		over.setId(overId);
		workOvertimeFormDao.delete(over);
		if (workOvertimeFormDao.get(over) == null) {
			json.setMsg("删除成功");
			json.setErrorCode(ErrorCode.code_1004);
		} else {
			json.setMsg("删除失败");
			json.setErrorCode(ErrorCode.code_2004);
		}
		return json;
	}
}