package zr.zrpower.web.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import zr.zrpower.common.util.*;
import zr.zrpower.ejb.sys.UserService;
import zr.zrpower.entity.sys.SessionUser;

public class CheckLogin
    extends HttpServlet {
  private static final String CONTENT_TYPE = "text/html; charset=GBK";

  //Initialize global variables
  public void init() throws ServletException {
  }

  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    response.setContentType(CONTENT_TYPE);
     request.setCharacterEncoding("GBK");
     response.setContentType("text/html; charset=GBK");
     HttpSession session = request.getSession(true);
     String strAutologin = request.getParameter("autologin");
     if (strAutologin==null){strAutologin="";}


     if (!strAutologin.equals("1"))
     {
       String strtxtlzm = request.getParameter("txtlzm");
       String strSessionlzm=session.getAttribute("RANDOMVALIDATECODEKEY").toString();
       if(!strtxtlzm.equals(strSessionlzm))
       {
            response.sendRedirect("index.jsp?MessageType=2");
            return;
       }
     }

     if (request.getContentLength()>0 || strAutologin.equals("1")){
        String strLoginID = request.getParameter("loginid");
        String strPassword = request.getParameter("password");

        if (strAutologin.equals("1"))
        {
              strPassword = "autologinpassword";
        }

        UserService userService = null;
        try {
          userService = new UserService();
        }
        catch (Exception ex) {
          session.setAttribute("errMsg","调用登录验证中间件发生错误\n错误信息："+ex.toString());
          session.setAttribute("errPath","RELOGIN");
          response.sendRedirect("error.jsp");
        }

        FunctionMessage fmLoginInfo = null;
        fmLoginInfo = userService.Login(strLoginID,strPassword);

        if (fmLoginInfo != null){
            //登录成功
            if (fmLoginInfo.getResult()){
                 SessionUser user = (SessionUser)fmLoginInfo.getValue();
                 session.setAttribute("userinfo",user);
                 
                 //
                 user = (SessionUser) session.getAttribute("userinfo");
                 if("6".equals(user.getUserImage())){
                	 response.sendRedirect("main1.jsp");
                 }else{
                	 response.sendRedirect("main.jsp");
                 }
                 user = null;
                 userService  = null;

            }else{
               session.setAttribute("errMsg",fmLoginInfo.getMessage());
               response.sendRedirect("index.jsp?MessageType=1");
               return;
            }

            userService  = null;
        }
        else{
          session.setAttribute("errMsg","调用中间件发生错误,未取得验证信息..");
          session.setAttribute("errPath","RELOGIN");
          response.sendRedirect("error.jsp");
          return;
        }
     }
     else{
       session.setAttribute("errMsg", "用户未登录！");
       session.setAttribute("errPath","RELOGIN");
       response.sendRedirect("error.jsp");
    }
  }

  //Process the HTTP Post request
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    doGet(request, response);
  }

  //Clean up resources
  public void destroy() {
  }
}
