package com.itjob.action;

import java.util.List;

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

import org.apache.struts.action.*;

import com.itjob.dao.model.CallTimeLimit;
import com.itjob.service.CallTimeLimitManager;

/**
 * 开启关闭时间设置的Action
 * @author AmigoXie
 */
public class CallTimeLimitAction extends Action {
	protected CallTimeLimitManager callTimeLimitManager;


	public void setCallTimeLimitManager(CallTimeLimitManager callTimeLimitManager) {
		this.callTimeLimitManager = callTimeLimitManager;
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			String method = request.getParameter("method");
			if ("getCallTimeLimitList".equals(method)) {
				return getCallTimeLimitList(mapping, form, request, response);
			} else if ("insertCallTimeLimit".equals(method)) {
				return insertCallTimeLimit(mapping, form, request, response);
			} else if ("getCallTimeLimit".equals(method)) {
				return getCallTimeLimit(mapping, form, request, response);
			} else if ("updateCallTimeLimit".equals(method)) {
				return updateCallTimeLimit(mapping, form, request, response);
			} else if ("deleteCallTimeLimit".equals(method)) {
				return deleteCallTimeLimit(mapping, form, request, response);
			} else {
				request.setAttribute("errorMsg", "错误请求");
				return mapping.findForward("error");
			}
		} catch (Exception e) {
			request.setAttribute("errorMsg", e.getMessage());
			return mapping.findForward("error");
		}
	}
	
	/**
	 * 查询开启关闭时间设置列表
	 */
	public ActionForward getCallTimeLimitList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String userNumber = (String) request.getSession().getAttribute("userNumber");
		int limitType = -1;
		if (request.getParameter("limitType") != null) {
			limitType = Integer.parseInt(request.getParameter("limitType"));
		}
		
		List list = callTimeLimitManager.getCallTimeLimitList(userNumber, limitType);
		request.setAttribute("callTimeLimitList", list);
		return mapping.findForward("callTimeLimitList");
	}
	
	/**
	 * 添加开启关闭时间设置
	 */
	public ActionForward insertCallTimeLimit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		CallTimeLimit callTimeLimit = new CallTimeLimit();
		callTimeLimit.setUserNumber((String) request.getSession().getAttribute("userNumber"));
		callTimeLimit.setLimitType(Integer.parseInt(request.getParameter("limitType")));
		callTimeLimit.setStartTime(request.getParameter("startTime"));
		callTimeLimit.setEndTime(request.getParameter("endTime"));
		callTimeLimitManager.insertCallTimeLimit(callTimeLimit);
		request.setAttribute("returnPage", "/callTimeLimitAction.do?method=getCallTimeLimitList");
		return mapping.findForward("success");
	}
	
	/**
	 * 获得某个开启关闭时间设置
	 */
	public ActionForward getCallTimeLimit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		long id = Long.parseLong(request.getParameter("id"));
		CallTimeLimit callTimeLimit = callTimeLimitManager.getCallTimeLimit(id);
		request.setAttribute("callTimeLimit", callTimeLimit);
		return mapping.findForward("updateCallTimeLimit");
	}
	
	/**
	 * 修改某个开启关闭时间设置
	 */
	public ActionForward updateCallTimeLimit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		CallTimeLimit callTimeLimit = new CallTimeLimit();
		callTimeLimit.setId(Long.parseLong(request.getParameter("id")));
		callTimeLimit.setLimitType(Integer.parseInt(request.getParameter("limitType")));
		callTimeLimit.setStartTime(request.getParameter("startTime"));
		callTimeLimit.setEndTime(request.getParameter("endTime"));
		callTimeLimitManager.updateCallTimeLimit(callTimeLimit);
		request.setAttribute("returnPage", "/callTimeLimitAction.do?method=getCallTimeLimitList");
		return mapping.findForward("success");
	}
	
	/**
	 * 删除一到多个开启关闭时间设置
	 */
	public ActionForward deleteCallTimeLimit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String[] idArray = request.getParameterValues("id");
		callTimeLimitManager.deleteCallTimeLimit(idArray);
		request.setAttribute("returnPage", "/callTimeLimitAction.do?method=getCallTimeLimitList");
		return mapping.findForward("success");
	}
}
