package com.skic.servlet;

import java.io.IOException;
import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.coding.util.Coding;
import com.skic.DTO.UserDTO;
import com.skic.service.CheckLoginService;

public class CheckLoginServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = -477275992604759935L;
	private CheckLoginService cks = new CheckLoginService(); 
		
	public CheckLoginServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log

	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String userName = request.getParameter("userName");
		String passWord = request.getParameter("passWord");
		String passWordBase64 = null;
		try {
			passWordBase64 = new BigInteger(Coding.encryptSHA(passWord.getBytes())).toString();//对密码进行加密比较
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String adminflag = "";
		String vipflag = "";
		String rootflag = "";
		RequestDispatcher rd = null;
		String forwardString = null;
		if (userName==null||passWord==null) {
			request.setAttribute("msg", "用户名或密码为空");
			rd = request.getRequestDispatcher("/page/messagePage/error.jsp");
			rd.forward(request, response);
		}else {
			UserDTO user = new UserDTO();
			user.setUserName(userName);
			user.setPassword(passWordBase64);//比对加密后的密码
			Map<String, Integer> rsMap = cks.CheckUser(user);
			request.getSession().setAttribute("login_status", rsMap.get("login_status"));//存储登录状态
			if (rsMap.get("login_status")==1) {
				request.setAttribute("msg", "登录成功");
				if (rsMap.get("adminflag")==1) {
					adminflag = "管理员";
				}
				if (rsMap.get("vipflag")==1) {
					vipflag = "VIP用户";
				}
				if (rsMap.get("rootflag")==1) {
					rootflag = "超级管理员";
				}
				request.getSession().setAttribute("adminflag", adminflag);			
				request.getSession().setAttribute("vipflag", vipflag);
				request.getSession().setAttribute("rootflag", rootflag);
				System.out.println("adminflag="+adminflag+"  vipflag="+" rootflag=");
				request.setAttribute("RedirctURL", "/page/homepage.jsp");
				request.setAttribute("Redirct", "前往主页");
				//forwardString = "/messagePage/messageShow.jsp";
			}else {
				request.setAttribute("msg", "账号不存在,请注册");
				request.setAttribute("RedirctURL", "register.jsp");
				request.setAttribute("Redirct", "返回登录");
			}
			forwardString = "/page/messagePage/messageShow.jsp";
			rd = request.getRequestDispatcher(forwardString);
			rd.forward(request, response);
		}
	}

	public void init() throws ServletException {

	}

}
