/**
 * Company Name : 中贸促信息技术有限责任公司
 * Project Name:project
 * File Name:LoginController.java
 * Package Name:org.ccpit.admin
 * Date:2015年9月15日上午11:20:00
 * Copyright (c) 2015, dingwei@ccpit.org All Rights Reserved.
 *
*/

package org.ccpit.admin;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.ccpit.base.controller.BaseController;
import org.ccpit.base.controller.ValidateCodeController;
import org.ccpit.base.logManage.LogInfoService;
import org.ccpit.base.user.User;
import org.ccpit.base.user.UserService;
import org.ccpit.base.utils.PasswordHash;
import org.ccpit.base.utils.UserRolesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * ClassName:LoginController <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO 用户登录控制类 <br/>
 * Date:     2015年9月15日 上午11:20:00 <br/>
 * @author   dingwei
 * @version  
 * @since    JDK 1.6
 * @see 	 
 */
@Controller
@RequestMapping("/admin")
public class LoginController extends BaseController{

	private static final Logger log = LoggerFactory.getLogger(LoginController.class);
	@Autowired
	private UserService userService;
	@Autowired
	private LogInfoService logInfoService;
    @Autowired
    private UserRolesUtil userRolesUtil;
	
	@RequestMapping("/login")
	public Object login(HttpServletRequest request, String validateCode){
		log.info("user login!");
		Map<String, Object> result = new HashMap<String, Object>();
		ModelAndView mv = null;
		String loginName = request.getParameter("loginName");
		String pass = request.getParameter("passId");
		HttpSession session = request.getSession();
		if(null != loginName && null != pass && null != validateCode){
			if (StringUtils.isEmpty(validateCode)){
				session.setAttribute("info", "请输入验证码...");
				mv = new ModelAndView("/login/login");
			}else {
				if (!ValidateCodeController.validate(request,validateCode)){
					session.setAttribute("info", "验证码输入错误，请重新输入");
					mv = new ModelAndView("/login/login");
				}else {
					User user = userService.getUserByLoginName(loginName);
					if(null == user){
						session.setAttribute("info", "用户信息不存在，请联系系统管理员...");
						mv = new ModelAndView("/login/login");
					}else {
						try {
							if(PasswordHash.validatePassword(pass,user.getPassword())){
								if("back".equals(user.getUserType())){
									if(1 == user.getCountStatus()){
										user.setLoginCount(null == user.getLoginCount() ? 1 : user.getLoginCount() + 1);
										userService.updateUser(user);
										result.put("flag", true);
	                                    session.removeAttribute(BaseController.USER_IN_SESSION);	
	                                    session.setAttribute(BaseController.USER_IN_SESSION, user);
	                                    request.setAttribute("userName", user.getUsername());
	                                    //记录用户操作日志信息
	                                    logInfoService.recordLog(request, "系统登录");
	                                    //重载权限模块
	                                    userRolesUtil.initOrReload(request);
	                                    result.put("flag", true);
	                                    mv = new ModelAndView("redirect:index");
									}else {
										session.setAttribute("info", "当前登录用户账号失效，请与系统管理员进行联系！");
										mv = new ModelAndView("/login/login");
									}
								}else {
									session.setAttribute("info", "当前登录用户不是后台用户，请与系统管理员进行联系！");
									mv = new ModelAndView("/login/login");
								}
							}else {
								session.setAttribute("info", "密码输入错误，请重新输入！");
								mv = new ModelAndView("/login/login");
							}
						} catch (Exception e) {
							e.printStackTrace();
							log.error("密码加密失败...");
						}
					}
				}
			}
		}else {
			mv = new ModelAndView("/login/login");
		}
		return mv;
	}
	/**
	 * 
	 * logout:(系统退出). <br/>
	 * @author dingwei
	 * @param request
	 * @return
	 * @since JDK 1.6
	 */
	@RequestMapping("/logout")
	public Object logout(HttpServletRequest request) {
		//记录用户操作日志信息
		logInfoService.recordLog(request, "系统退出");
		request.getSession().removeAttribute("user_in_session");
		request.getSession().removeAttribute(BaseController.USER_IN_SESSION);
		ModelAndView mv = new ModelAndView("/login/login");
		return mv;
	}
	/**
	 * 
	 * goLoginPage:(进入登录页面). <br/>
	 * @author dingwei
	 * @param request
	 * @return
	 * @since JDK 1.6
	 */
	@RequestMapping("/goLoginPage")
	public Object goLoginPage(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/login/login");
		return mv;
	}
}

