package com.itjob.action;

import java.io.PrintWriter;
import java.util.List;

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

import org.apache.struts.action.*;

import com.itjob.dao.model.CallBarring;
import com.itjob.service.CallBarringManager;

/**
 * 黑白名单设置的Action
 * @author AmigoXie
 */
public class CallBarringAction extends Action {
	protected CallBarringManager callBarringManager;

	public void setCallBarringManager(CallBarringManager callBarringManager) {
		this.callBarringManager = callBarringManager;
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String method = request.getParameter("method");
		if ("getCallBarringList".equals(method)) {
			return getCallBarringList(mapping, form, request, response);
		} else if ("insertCallBarring".equals(method)) {
			return insertCallBarring(mapping, form, request, response);
		} else if ("getCallBarring".equals(method)) {
			return getCallBarring(mapping, form, request, response);
		} else if ("updateCallBarring".equals(method)) {
			return updateCallBarring(mapping, form, request, response);
		} else if ("deleteCallBarring".equals(method)) {
			return deleteCallBarring(mapping, form, request, response);
		} else if ("validLimitPhone".equals(method)) {
			return validLimitPhone(mapping, form, request, response);
		} else {
			request.setAttribute("errorMsg", "错误请求");
			return mapping.findForward("error");
		}
	}
	
	/**
	 * 查询黑/白名单列表
	 */
	public ActionForward getCallBarringList(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 = callBarringManager.getCallBarringList(userNumber, limitType);
		request.setAttribute("callBarringList", list);
		return mapping.findForward("callBarringList");
	}
	
	/**
	 * 添加黑/白名单
	 */
	public ActionForward insertCallBarring(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		CallBarring callBarring = new CallBarring();
		callBarring.setLimitPhone(request.getParameter("limitPhone"));
		callBarring.setLimitType(Integer.parseInt(request.getParameter("limitType")));
		callBarring.setDescription(request.getParameter("description"));
		callBarring.setUserNumber((String) request.getSession().getAttribute("userNumber"));
		callBarringManager.insertCallBarring(callBarring);
		request.setAttribute("returnPage", "/callBarringAction.do?method=getCallBarringList");
		return mapping.findForward("success");
	}
	
	/**
	 * 获得某个黑/白名单
	 */
	public ActionForward getCallBarring(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		long id = Long.parseLong(request.getParameter("id"));
		CallBarring callBarring = callBarringManager.getCallBarring(id);
		request.setAttribute("callBarring", callBarring);
		return mapping.findForward("updateCallBarring");
	}
	
	/**
	 * 修改黑/白名单
	 */
	public ActionForward updateCallBarring(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		CallBarring callBarring = new CallBarring();
		callBarring.setId(Long.parseLong(request.getParameter("id")));
		callBarring.setLimitPhone(request.getParameter("limitPhone"));
		callBarring.setLimitType(Integer.parseInt(request.getParameter("limitType")));
		callBarring.setDescription(request.getParameter("description"));
		callBarringManager.updateCallBarring(callBarring);
		request.setAttribute("returnPage", "/callBarringAction.do?method=getCallBarringList");
		return mapping.findForward("success");
	}
	
	/**
	 * 删除一到多个黑/白名单
	 */
	public ActionForward deleteCallBarring(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String[] idArray = request.getParameterValues("id");
		callBarringManager.deleteCallBarring(idArray);
		request.setAttribute("returnPage", "/callBarringAction.do?method=getCallBarringList");
		return mapping.findForward("success");
	}
	
	/**
	 * 验证黑白名单号码是否已存在（ajax验证）
	 */
	public ActionForward validLimitPhone(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		 // 处理中文问题
		 request.setCharacterEncoding("utf-8");
         response.setContentType("text/html;charset=utf-8");
         String limitPhone = request.getParameter("limitPhone");
         System.out.println("limitPhone=" + limitPhone);
         String userNumber = (String) request.getSession().getAttribute("userNumber");
         int re = callBarringManager.validLimitPhone(userNumber, limitPhone);
         System.out.println("re=" + re);
         String msg = "";
         if (re == 1) {
        	 msg = "该号码已被设置，不允许添加";
         }
         PrintWriter out = response.getWriter();
         out.write(msg); 
         out.flush();
         out.close();
         return null;
	}
}
