package com.yeshi.buwan.service.imp;
|
|
import java.io.Serializable;
|
import java.util.List;
|
|
import javax.annotation.Resource;
|
|
import org.hibernate.HibernateException;
|
import org.hibernate.Session;
|
import org.springframework.cache.annotation.Cacheable;
|
import org.springframework.orm.hibernate4.HibernateCallback;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import com.yeshi.buwan.dao.LoginUserDao;
|
import com.yeshi.buwan.dao.UserDao;
|
import com.yeshi.buwan.domain.LoginUser;
|
import com.yeshi.buwan.domain.SystemInfo;
|
import com.yeshi.buwan.domain.UserData;
|
import com.yeshi.buwan.domain.UserInfo;
|
import com.yeshi.buwan.util.Constant;
|
import com.yeshi.buwan.util.StringUtil;
|
|
@Service
|
public class UserService {
|
|
public UserService() {
|
|
}
|
|
@Resource
|
private UserDao userDao;
|
|
@Resource
|
private LoginUserDao loginUserDao;
|
|
public UserDao getUserDao() {
|
return userDao;
|
}
|
|
public void setUserDao(UserDao userDao) {
|
this.userDao = userDao;
|
}
|
|
// 用户操作
|
public List<UserInfo> getUserList(int system, int page) {
|
return userDao.list("from UserInfo u where u.system.id=? order by u.createtime desc",
|
(page - 1) * Constant.pageCount, Constant.pageCount, new String[]{system + ""});
|
}
|
|
// 获取用户数量
|
public long getUserInfoCount(int system) {
|
|
return userDao.getCount("select count(*) from UserInfo u where u.system.id=?", new String[]{system + ""});
|
}
|
|
// 获取总的用户数量
|
public long getUserInfoCount() {
|
|
return userDao.getCount("select count(*) from UserInfo u");
|
}
|
|
// 获取用户的页数
|
public long getUserInfoPage(int system) {
|
long count = getUserInfoCount(system);
|
return count % Constant.pageCount == 0 ? count / Constant.pageCount : count / Constant.pageCount + 1;
|
}
|
|
// 获取某个用户的详细信息
|
@Cacheable(value = "longTimeCache", key = "'getUserInfo'+'-'+#id")
|
public UserInfo getUserInfo(String id) {
|
|
return userDao.find(UserInfo.class, id);
|
}
|
|
// 更新用户信息
|
public void updateUserInfo(UserInfo userInfo) {
|
userDao.update(userInfo);
|
}
|
|
public void updateLoginUserInfo(LoginUser lu) {
|
loginUserDao.update(lu);
|
}
|
|
public String getUid(String device, String system, String imei, String mac, String lat, String lng) {
|
UserInfo info;
|
info = new UserInfo();
|
info.setDevice(device);
|
if (!StringUtil.isNullOrEmpty(imei))
|
info.setImei(imei);
|
if (!StringUtil.isNullOrEmpty(mac))
|
info.setMac(mac);
|
if (!StringUtil.isNullOrEmpty(lat))
|
info.setLat(lat);
|
if (!StringUtil.isNullOrEmpty(lng))
|
info.setLng(lng);
|
String s;
|
List<UserInfo> list = userDao.list("from UserInfo u where u.device=? and u.system.id=" + system,
|
device);
|
|
if (list.size() <= 0) {//
|
info.setScore("0");
|
info.setCreatetime((new StringBuilder(String.valueOf(System.currentTimeMillis()))).toString());
|
SystemInfo sys = new SystemInfo();
|
sys.setId(system);
|
info.setSystem(sys);
|
userDao.create(info);
|
s = queryUid(device, system);
|
} else {
|
UserInfo u = (UserInfo) list.get(0);
|
s = u.getId();
|
if (!StringUtil.isNullOrEmpty(imei))
|
u.setImei(imei);
|
if (!StringUtil.isNullOrEmpty(mac))
|
u.setMac(mac);
|
if (!StringUtil.isNullOrEmpty(lat))
|
u.setLat(lat);
|
if (!StringUtil.isNullOrEmpty(lng))
|
u.setLng(lng);
|
userDao.update(u);
|
}
|
|
return s;
|
}
|
|
@SuppressWarnings("unchecked")
|
public String queryUid(String device, String system) {
|
String s = "0";
|
List<UserInfo> list = userDao.list("from UserInfo u where u.device=? and u.system.id=" + system,
|
new String[]{device});
|
if (list.size() <= 0)
|
s = "0";
|
else
|
s = ((UserInfo) list.get(0)).getId();
|
return s;
|
}
|
|
@Cacheable(value = "userCache", key = "'getUserData'+'-'+#uid")
|
public UserData getUserData(String uid) {
|
UserData data = new UserData();
|
data.setCollectCount((new StringBuilder(String.valueOf(userDao.getCount(
|
(new StringBuilder("select count(*) from Collection c where c.user.id=")).append(uid).toString()))))
|
.toString());
|
data.setWatchCount((new StringBuilder(String.valueOf(userDao.getCount(
|
(new StringBuilder("select count(*) from GetScoreWatch c where c.user.id=")).append(uid).toString()))))
|
.toString());
|
List<UserInfo> user = userDao
|
.list((new StringBuilder("select count(*) from UserInfo u where u.id=")).append(uid).toString());
|
// if (user != null && user.size() > 0)
|
// data.setScore(((UserInfo) user.get(0)).getScore());
|
// else
|
// data.setScore("0");
|
return data;
|
}
|
|
@SuppressWarnings("unchecked")
|
public LoginUser getLoginUser(final String openid, final String detailSystem, final int type, final String portrait,
|
final String name, final String ipInfo) {
|
return (LoginUser) userDao.excute(new HibernateCallback<LoginUser>() {
|
public LoginUser doInHibernate(Session session) throws HibernateException {
|
try {
|
List<LoginUser> list = session
|
.createQuery("from LoginUser lu where lu.openid=? and lu.loginType=?")
|
.setParameter(0, openid).setParameter(1, type).list();
|
|
if (list != null && list.size() > 0)
|
return list.get(0);
|
else {
|
session.getTransaction().begin();
|
LoginUser lu = new LoginUser();
|
lu.setCreatetime(System.currentTimeMillis() + "");
|
lu.setDetailsystem(detailSystem);
|
lu.setLoginType(type);
|
lu.setName(name);
|
lu.setOpenid(openid);
|
lu.setPortrait(portrait);
|
lu.setIpinfo(ipInfo);
|
session.save(lu);
|
session.flush();
|
session.getTransaction().commit();
|
return lu;
|
}
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
session.getTransaction().rollback();
|
}
|
return null;
|
}
|
});
|
|
}
|
|
public LoginUser getLoginUser(String id) {
|
return loginUserDao.find(LoginUser.class, id);
|
}
|
|
public LoginUser getLoginUserByOpenId(String openid) {
|
List<LoginUser> list = loginUserDao.list("from LoginUser lu where lu.openid=?", new Serializable[]{openid});
|
if (list != null && list.size() > 0)
|
return list.get(0);
|
return null;
|
}
|
|
/**
|
* 用户注销
|
*
|
* @param uid
|
*/
|
@Transactional
|
public void unRegister(Long uid) throws Exception {
|
LoginUser user = loginUserDao.find(LoginUser.class, uid + "");
|
if (user == null) {
|
throw new Exception("用户不存在");
|
}
|
if (user.getState() != LoginUser.STATE_NORMAL) {
|
throw new Exception("账户已被注销");
|
}
|
user.setState(LoginUser.STATE_UNREGISTER);
|
loginUserDao.update(user);
|
}
|
|
// 邮箱注册
|
public String registerByEmail(final LoginUser user) {
|
return userDao.excute(new HibernateCallback<String>() {
|
@SuppressWarnings("unchecked")
|
public String doInHibernate(Session session) throws HibernateException {
|
List<LoginUser> list = session.createQuery("from LoginUser lu where lu.openid=? and lu.loginType=?")
|
.setParameter(0, user.getOpenid()).setParameter(1, user.getLoginType()).list();
|
if (list != null && list.size() > 0)
|
return "该邮箱已注册";
|
try {
|
session.getTransaction().begin();
|
session.save(user);
|
session.flush();
|
session.getTransaction().commit();
|
return "注册成功";
|
} catch (Exception e) {
|
return "注册失败";
|
}
|
}
|
}) + "";
|
}
|
|
/**
|
* 邮箱登录
|
*
|
* @param user
|
* @return
|
*/
|
public LoginUser loginByEmail(final LoginUser user) {
|
return (LoginUser) userDao.excute(new HibernateCallback<LoginUser>() {
|
@SuppressWarnings("unchecked")
|
public LoginUser doInHibernate(Session session) throws HibernateException {
|
List<LoginUser> list = session.createQuery("from LoginUser lu where lu.openid=? and lu.loginType=?")
|
.setParameter(0, user.getOpenid()).setParameter(1, user.getLoginType()).list();
|
if (list != null && list.size() > 0) {
|
if (list.get(0).getPwd().equalsIgnoreCase(user.getPwd()))
|
return list.get(0);
|
}
|
return null;
|
}
|
});
|
}
|
}
|