package com.award.dao.user;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.RowMapper;
import com.award.bean.User;
import com.award.dao.BaseDao;
import com.award.dao.Page;
import com.award.util.MD5Utils;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
public class UserDao extends BaseDao
{
private static final String FIND_ALL = "select * from user ";
private static final String UPDATE_ID = "update user set name=:name,password=:password,email=:email,time=:time,type=:type where id=:id";
private static final String UPDATE_IDNOPASS = "update user set name=:name,email=:email,time=:time,type=:type where id=:id";
private static final String FIND_NAME = "select * from user where name=:name ";
private static final String DETELTE_ID = "delete from user where id=:id";
private Logger _Logger = LoggerFactory.getLogger(UserDao.class);
/**
* 删除记录
*/
public void delete(User user)
{
Map<String, Object> _mapParamMapper = new HashMap<String, Object>();
try
{
//System.out.println(user.getId());
_mapParamMapper.put("id", user.getId());
super.update(DETELTE_ID, _mapParamMapper);
_Logger.debug("delete user " + user.getId() + " password success");
}
catch (Exception e)
{
_Logger.debug("delete user " + user.getId() + " password failed");
e.printStackTrace();
}
}
/**
* 保存
*
* @param user
* @return
*/
public User save(User user)
{
try
{
user.setId(saveAndReturnKey(user).intValue());
_Logger.debug("save user " + user.getName() + " success");
}
catch (Exception e)
{
_Logger.debug("save user " + user.getName() + " failed");
e.printStackTrace();
}
return user;
}
/**
* 修改密码
*
* @param user
*/
public void update(User user)
{
Map<String, Object> _mapParamMapper = new HashMap<String, Object>();
try
{
_mapParamMapper.put("id", user.getId());
_mapParamMapper.put("password", user.getPassword());
super.update(UPDATE_ID, _mapParamMapper);
_Logger.debug("updated user " + user.getName() + " password success");
}
catch (Exception e)
{
_Logger.debug("updated user " + user.getName() + " password failed");
e.printStackTrace();
}
}
/**
* 修改用户信息 改密码
*
* @param user
*/
public void updateUser(User user)
{
Map<String, Object> _mapParamMapper = new HashMap<String, Object>();
try
{
_mapParamMapper.put("id", user.getId());
_mapParamMapper.put("name", user.getName());
_mapParamMapper.put("password", user.getPassword());
//System.out.println(user.getPassword());
_mapParamMapper.put("email", user.getEmail());
_mapParamMapper.put("time", user.getTime());
_mapParamMapper.put("type", user.getType());
super.update(UPDATE_ID, _mapParamMapper);
_Logger.debug("updated user " + user.getName() + " password success");
}
catch (Exception e)
{
_Logger.debug("updated user " + user.getName() + " password failed");
e.printStackTrace();
}
}
//defrdsfsd
/**
* 修改用户信息 不改密码
*
* @param user
*/
public void updateUserNoPass(User user)
{
Map<String, Object> _mapParamMapper = new HashMap<String, Object>();
try
{
_mapParamMapper.put("id", user.getId());
_mapParamMapper.put("name", user.getName());
//_mapParamMapper.put("password", user.getPassword());
//System.out.println(user.getPassword());
_mapParamMapper.put("email", user.getEmail());
_mapParamMapper.put("time", user.getTime());
_mapParamMapper.put("type", user.getType());
super.update(UPDATE_IDNOPASS, _mapParamMapper);
_Logger.debug("updated user " + user.getName() + " password success");
}
catch (Exception e)
{
_Logger.debug("updated user " + user.getName() + " password failed");
e.printStackTrace();
}
}
/**
* 用户名,邮箱唯一性
*
* @param sWhere
* @return
*/
@SuppressWarnings("unchecked")
public boolean findWhere(String sWhere)
{
boolean bFlag = true;
try
{
List<User> _lstUsers = super.findForList(FIND_ALL + sWhere, new UserRowMapper());
if (_lstUsers.size() > 0)
{
bFlag = true;
}
else
{
bFlag = false;
}
_Logger.debug("find user by" + sWhere + " success");
}
catch (Exception e)
{
bFlag = false;
_Logger.debug("find user by" + sWhere + " failed");
e.printStackTrace();
}
return bFlag;
}
@SuppressWarnings("unchecked")
public User find(String name)
{
User _user = new User();
try
{
Map<String, Object> _mapParamMapper = new HashMap<String, Object>();
_mapParamMapper.put("name", name);
List<User> _lstUsers = super.findForList(FIND_NAME, _mapParamMapper, new UserRowMapper());
if (_lstUsers.size() > 0)
{
_user = _lstUsers.get(0);
}
_Logger.debug("find user by " + name + " success");
}
catch (Exception e)
{
_Logger.debug("find user by " + name + " failed");
e.printStackTrace();
}
return _user;
}
@SuppressWarnings("unchecked")
public List<User> findAll(Integer _startIndex, Integer _pageSize)
{
List<User> _list = super.findForList(FIND_ALL, new UserRowMapper());
try
{
// _list = new ArrayList<User>();
_Logger.debug("find user success");
}
catch (Exception e)
{
_Logger.debug("find user failed");
e.printStackTrace();
}
return _list;
}
/**
* 分页查询
*
* @param sWhere
* @param _startIndex
* @param _pageSize
* @return
*/
public Page getPageBySQL(Integer _startIndex, Integer _pageSize)
{
Page _page = new Page();
try
{
_page = super.getPageBySQL(FIND_ALL, _startIndex, _pageSize, new UserRowMapper());
_Logger.debug("find user success");
}
catch (Exception e)
{
_Logger.debug("find user failed");
e.printStackTrace();
}
return _page;
}
private class UserRowMapper implements RowMapper<User>
{
public User mapRow(ResultSet _rs, int iRowNum) throws SQLException
{
User _user = new User();
_user.setId(_rs.getInt("id"));
_user.setName(_rs.getString("name"));
_user.setPassword(_rs.getString("password"));
_user.setEmail(_rs.getString("email"));
_user.setTime(_rs.getTimestamp("time").toString());
_user.setType(_rs.getString("type"));
return _user;
}
}
}