package com.lgx8.right.dao.impl;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;
import com.lgx8.common.PageArgument;
import com.lgx8.common.PageList;
import com.lgx8.common.dao.impl.BaseDao;
import com.lgx8.right.common.EncryptUtil;
import com.lgx8.right.dao.IUserDao;
import com.lgx8.right.entities.User;
/**
*
* @author lihui
*
*/
@SuppressWarnings("unchecked")
public class UserDao extends BaseDao implements IUserDao {
@Transactional
public void createUser(User user) {
/**用户密码MD5加密*/
String md5Passwd = EncryptUtil.toMD5(user.getPassword());
user.setPassword(md5Passwd.toUpperCase());
getHibernateTemplate().persist(user);
}
public User findUser(String username, String password) {
String inputPass = EncryptUtil.toMD5(password);
if(null==inputPass) inputPass = "";
inputPass = inputPass.toUpperCase();
String hql = "from User t where t.enabled = true and t.username='"+username+"' and t.password ='"+inputPass+"'";
List<User> list = getHibernateTemplate().find(hql);
if(list!=null&&list.size()>0) {
return list.get(0);
} else {
return null;
}
}
@Transactional
public void updateUser(User user) {
getHibernateTemplate().update(user);
}
public User findUser(String name, Object value) {
String hql = "from User t where t.enabled = true and t."+name+"=?";
List<User> list = getHibernateTemplate().find(hql,new Object[]{value});
if(list!=null&&list.size()>0) {
return list.get(0);
} else {
return null;
}
}
public PageList findUserByConditions(String hql, Object[] objs, PageArgument ar) {
int pageNum = ar.getCurPage();
int pageSize = ar.getPageSize();
return findByPage4Report(hql, objs, pageNum, pageSize);
}
public User findUserByPhone(String mobile,String carNum) {
String hql = "from User t where t.enabled = true and t.mobile=? or t.card.id = ?";
List<User> list = getHibernateTemplate().find(hql,new Object[]{mobile,carNum});
if(list!=null&&list.size()!=0){
return list.get(0);
}
return null;
}
public User findUserById(long id) {
String hql = "from User t where t.enabled = true and t.id =" + id;
List<User> list = getHibernateTemplate().find(hql);
if(list!=null&&list.size()>0) {
return list.get(0);
} else {
return null;
}
}
public User findUserByUserName(String userName) {
String hql = "from User t where t.enabled = true and t.username = '"+userName+"'";
List<User> list = getHibernateTemplate().find(hql);
if(list!=null&&list.size()>0) {
return list.get(0);
} else {
return null;
}
}
@Transactional
public void delUser(User user) {
if(user != null)
{
user.setEnabled(false);
this.getHibernateTemplate().update(user);
}
}
public List<User> findUsers(String name, Object value) {
String hql = "from User t where t.enabled = true and t."+name+"=?";
List<User> list = getHibernateTemplate().find(hql,new Object[]{value});
return list;
}
public String checkMobileExists(String mobile) {
String hql = " from User where enabled = true and mobile = '"+mobile+"'";
List<User> list = getHibernateTemplate().find(hql);
if(list != null && list.size() > 0)
{
return "0";
}
return "1";
}
@Transactional
public void resetUserPassword(Long userId, String password) {
/**用户密码MD5加密*/
String md5Passwd = EncryptUtil.toMD5(password);
User user = this.findUserById(userId);
user.setPassword(md5Passwd.toUpperCase());
getHibernateTemplate().update(user);
}
public User loginUser(String loginName, String password) {
String inputPass = EncryptUtil.toMD5(password);
if(null==inputPass) inputPass = "";
inputPass = inputPass.toUpperCase();
String hql = "from User t where t.enabled = true and (t.username='"+loginName+"' or t.mobile='"+loginName+"' or cardid = '"+loginName+"') and t.password ='"+inputPass+"'";
List<User> list = getHibernateTemplate().find(hql);
if(list!=null&&list.size()>0) {
return list.get(0);
} else {
return null;
}
}
@Transactional
public void lockUser(Long userId, Boolean locked) {
// TODO Auto-generated method stub
User user = this.findUserById(userId);
user.setLocked(locked);
getHibernateTemplate().update(user);
}
public List<User> listUser(String hql) {
return this.getHibernateTemplate().find(hql);
}
public User getUserByMobileOrCard(String params) {
String hql = "from User t where t.enabled = true and ( t.mobile='"+params+"' or cardid = '"+params+"') ";
List<User> list = getHibernateTemplate().find(hql);
if(list!=null&&list.size()>0) {
return list.get(0);
} else {
return null;
}
}
}