package com.itjob.service;

import java.text.SimpleDateFormat;
import java.util.*;

import com.itjob.dao.BaseDao;
import com.itjob.dao.model.Role;
import com.itjob.dao.model.User;
/**
 * 用户信息管理的业务逻辑实现类.
 */
public class UserManagerImpl implements UserManager {
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private BaseDao dao;

	public void setDao(BaseDao dao) {
		this.dao = dao;
	}
	
	/**
	 * 用户登录
	 * @param loginName 登录名
	 * @param userPasswd 登录密码
	 * @return 0:登录成功，1:用户不存在，2:密码错误
	 */
	public int userLogin(String loginName, String userPasswd) throws Exception {
		LinkedList<Object> params = new LinkedList<Object>();
		params.add(loginName);
		List list = dao.hqlQuery("from User where loginName=?",
					params.toArray());
		if (list != null && list.size() > 0) {
			User user = (User) list.get(0);
			if (userPasswd.equals(user.getUserPasswd())) {
				return 0;
			} else {
				return 2;
			}
		} else {
			return 1;
		}
	}
	
	/**
	 * 获得用户信息
	 * @param loginName 登录名
	 * @return 返回用户信息对象
	 */
	public User getUser(String loginName) throws Exception {
		User user = (User) dao.getObject(User.class, loginName);
		return user;
	}
	
	/**
	 * 获得用户列表
	 */
	public List getUserList() throws Exception {
		List<Map> returnList = new ArrayList<Map>();
		List list = dao.hqlQuery("from User");
		User user = null;
		Map<String, String> map = null;
		for (int i = 0; i < list.size(); i++) {
			map = new HashMap<String, String>();
			user = (User) list.get(i);
			map.put("num", String.valueOf(i + 1));
			map.put("loginName", user.getLoginName());
			map.put("realName", user.getRealName());
			map.put("tel", user.getTel());
			map.put("email", user.getEmail());
			map.put("createTime", sdf.format(user.getCreateTime()));
			returnList.add(map);
		}
		return returnList;
	}
	
	/**
	 * 保存用户信息
	 * @param user 用户信息对象 
	 */
	public void saveUser(User user) throws Exception {
		user.setCreateTime(new Date());
		user.setLoginTimes(0);
		dao.saveObject(user);
	}
	
	/**
	 * 更新用户信息
	 * @param user 用户信息对象 
	 */
	public void updateUser(User user) throws Exception {
		User userPo = (User) dao.getObject(User.class, user.getLoginName());
		userPo.setUserPasswd(user.getUserPasswd());
		userPo.setRealName(user.getRealName());
		userPo.setTel(user.getTel());
		userPo.setEmail(user.getEmail());
		dao.updateObject(userPo);
	}
	
	/**
	 * 删除一到多个用户
	 * @param loginNameArray 用户登录名数组
	 */
	public void deleteUser(String[] loginNameArray) throws Exception {
		User user = null;
		for (int i = 0; i < loginNameArray.length; i++) {
			user = (User) dao.getObject(User.class, loginNameArray[i]);
			dao.deleteObject(user);
		}
	}
	
	/**
	 * 更新用户的所属角色信息
	 * @param user 用户对象
	 * @param roleIds 多个角色ID时以“,”隔开
	 */
	public void updateUserRole(String loginName, String roleIds) throws Exception {
		User user = (User) dao.getObject(User.class, loginName);
		Set<Role> roles = new HashSet<Role>(0);
		String[]  roleIdArray = roleIds.split(",");
		Role role = null;
		for (int i = 0; i < roleIdArray.length; i++) {
			role = (Role) dao.getObject(Role.class, Long.parseLong(roleIdArray[i]));
			roles.add(role);
		}
		user.setRoles(roles);
		dao.updateObject(user);
	}
}
