package com.lyq.action.user;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.lyq.action.BaseAction;
import com.lyq.model.user.User;
import com.opensymphony.xwork2.ModelDriven;
/**
 * ÓÃ»§Action
 * @author Li Yongqiang
 */
@Scope("prototype")
@Controller("userAction")
public class UserAction extends BaseAction implements ModelDriven<User>{
	private static final long serialVersionUID = 1L;
	public String login() throws Exception{
		return USER_LOGIN;
	}
	/**
	 * µÇÂ¼ 
	 * @return
	 * @throws Exception
	 */
	public String logon() throws Exception{
		//ÑéÖ¤ÓÃ»§ÃûºÍÃÜÂë
		User loginUser = userDao.login(user.getUsername(), user.getPassword());
		if(loginUser != null){//Í¨¹ýÑéÖ¤
			session.put("admin", loginUser);//½«¹ÜÀíÔ±ÐÅÏ¢±£´æÔÚSession¶ÔÏóÖÐ
		}else{
			addFieldError("", "ÓÃ»§Ãû»òÃÜÂë²»ÕýÈ·£¡");//·µ»Ø´íÎóÌáÊ¾ÐÅÏ¢
			return USER_LOGIN;//·µ»ØºóÌ¨µÇÂ¼Ò³Ãæ
		}
		return MANAGER;//·µ»ØºóÌ¨¹ÜÀíÒ³Ãæ
	}
	/**
	 * ÍË³ö
	 * @return String
	 * @throws Exception
	 */
	public String logout() throws Exception{
		if(session != null && session.size() > 0){
			session.clear();
		}
		return INDEX;
	}
	// ÓÃ»§¶ÔÏó
	private User user = new User();
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	@Override
	public User getModel() {
		return user;
	}
}
