From f788607ff771a47bc60d6a86e00b3433c40f3d2c Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期五, 24 九月 2021 15:22:03 +0800
Subject: [PATCH] 接入视频直播

---
 src/main/java/com/yeshi/buwan/service/imp/UserService.java |  616 ++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 404 insertions(+), 212 deletions(-)

diff --git a/src/main/java/com/yeshi/buwan/service/imp/UserService.java b/src/main/java/com/yeshi/buwan/service/imp/UserService.java
index f291d87..fd96e7a 100644
--- a/src/main/java/com/yeshi/buwan/service/imp/UserService.java
+++ b/src/main/java/com/yeshi/buwan/service/imp/UserService.java
@@ -1,10 +1,17 @@
 package com.yeshi.buwan.service.imp;
 
 import java.io.Serializable;
+import java.util.Date;
 import java.util.List;
 
 import javax.annotation.Resource;
 
+import com.yeshi.buwan.dao.user.LoginUserExtraDao;
+import com.yeshi.buwan.domain.user.LoginUserExtra;
+import com.yeshi.buwan.dto.user.LoginInfoDto;
+import com.yeshi.buwan.exception.user.LoginUserException;
+import com.yeshi.buwan.exception.user.RegisterUserException;
+import com.yeshi.buwan.service.inter.LoginUserService;
 import org.hibernate.HibernateException;
 import org.hibernate.Session;
 import org.springframework.cache.annotation.Cacheable;
@@ -12,10 +19,10 @@
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
-import com.yeshi.buwan.dao.LoginUserDao;
+import com.yeshi.buwan.dao.user.LoginUserDao;
 import com.yeshi.buwan.dao.UserDao;
-import com.yeshi.buwan.domain.LoginUser;
-import com.yeshi.buwan.domain.SystemInfo;
+import com.yeshi.buwan.domain.user.LoginUser;
+import com.yeshi.buwan.domain.system.SystemInfo;
 import com.yeshi.buwan.domain.UserData;
 import com.yeshi.buwan.domain.UserInfo;
 import com.yeshi.buwan.util.Constant;
@@ -24,242 +31,427 @@
 @Service
 public class UserService {
 
-	public UserService() {
+    public UserService() {
 
-	}
+    }
 
-	@Resource
-	private UserDao userDao;
+    @Resource
+    private UserDao userDao;
 
-	@Resource
-	private LoginUserDao loginUserDao;
+    @Resource
+    private LoginUserDao loginUserDao;
 
-	public UserDao getUserDao() {
-		return userDao;
-	}
+    @Resource
+    private LoginUserExtraDao loginUserExtraDao;
 
-	public void setUserDao(UserDao userDao) {
-		this.userDao = userDao;
-	}
+    @Resource
+    private LoginUserService loginUserService;
 
-	// 鐢ㄦ埛鎿嶄綔
-	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) {
+    // 鐢ㄦ埛鎿嶄綔
+    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 + ""});
+    }
 
-		return userDao.getCount("select count(*)  from UserInfo u where u.system.id=?", new String[] { system + "" });
-	}
+    // 鑾峰彇鐢ㄦ埛鏁伴噺
+    public long getUserInfoCount(int system) {
 
-	// 鑾峰彇鎬荤殑鐢ㄦ埛鏁伴噺
-	public long getUserInfoCount() {
+        return userDao.getCount("select count(*)  from UserInfo u where u.system.id=?", new String[]{system + ""});
+    }
 
-		return userDao.getCount("select count(*)  from UserInfo u");
-	}
+    // 鑾峰彇鎬荤殑鐢ㄦ埛鏁伴噺
+    public long getUserInfoCount() {
 
-	// 鑾峰彇鐢ㄦ埛鐨勯〉鏁�
-	public long getUserInfoPage(int system) {
-		long count = getUserInfoCount(system);
-		return count % Constant.pageCount == 0 ? count / Constant.pageCount : count / Constant.pageCount + 1;
-	}
+        return userDao.getCount("select count(*)  from UserInfo u");
+    }
 
-	// 鑾峰彇鏌愪釜鐢ㄦ埛鐨勮缁嗕俊鎭�
-	@Cacheable(value = "longTimeCache", key = "'getUserInfo'+'-'+#id")
-	public UserInfo getUserInfo(String id) {
+    // 鑾峰彇鐢ㄦ埛鐨勯〉鏁�
+    public long getUserInfoPage(int system) {
+        long count = getUserInfoCount(system);
+        return count % Constant.pageCount == 0 ? count / Constant.pageCount : count / Constant.pageCount + 1;
+    }
 
-		return userDao.find(UserInfo.class, id);
-	}
+    // 鑾峰彇鏌愪釜鐢ㄦ埛鐨勮缁嗕俊鎭�
+    @Cacheable(value = "longTimeCache", key = "'getUserInfo'+'-'+#id")
+    public UserInfo getUserInfo(String id) {
 
-	// 鏇存柊鐢ㄦ埛淇℃伅
-	public void updateUserInfo(UserInfo userInfo) {
-		userDao.update(userInfo);
-	}
+        return userDao.find(UserInfo.class, id);
+    }
 
-	public void updateLoginUserInfo(LoginUser lu) {
-		loginUserDao.update(lu);
-	}
+    // 鏇存柊鐢ㄦ埛淇℃伅
+    public void updateUserInfo(UserInfo userInfo) {
+        userDao.update(userInfo);
+    }
 
-	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,
-				new String[] { device });
+    public void updateLoginUserInfo(LoginUser lu) {
+        loginUserDao.updateSelective(lu);
+    }
 
-		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);
-		}
+    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);
 
-		return s;
-	}
+        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);
+        }
 
-	@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;
-	}
+        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 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;
+    }
 
-	@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();
+    @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;
+    }
 
-					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;
-			}
-		});
+    /**
+     * 鐧诲綍
+     *
+     * @param dto
+     * @return
+     * @throws LoginUserException
+     */
+    public LoginUser login(LoginInfoDto dto) throws LoginUserException {
 
-	}
+        LoginUser loginUser = null;
 
-	public LoginUser getLoginUser(String id) {
-		return loginUserDao.find(LoginUser.class, id);
-	}
+        LoginUserExtra loginUserExtra = null;
+        switch (dto.getLoginType()) {
+            case LoginUser.LOGIN_TYPE_EMAIL: {
+                if (StringUtil.isNullOrEmpty(dto.getEmail()) || StringUtil.isNullOrEmpty(dto.getPwd())) {
+                    throw new LoginUserException(LoginUserException.CODE_PARAMS_NOT_ENOUGH, "閭鍜屽瘑鐮佷笉鑳戒负绌�");
+                }
+                List<LoginUser> userList = loginUserDao.list("from LoginUser lu where lu.systemId=? and lu.email=? and lu.state=?", dto.getSystemId(), dto.getEmail(), LoginUser.STATE_NORMAL);
+                if (userList == null || userList.size() == 0) {
+                    throw new LoginUserException(LoginUserException.CODE_NO_USER, "鐢ㄦ埛涓嶅瓨鍦�");
+                }
 
-	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;
-	}
+                if (!dto.getPwd().equalsIgnoreCase(userList.get(0).getPwd())) {
+                    throw new LoginUserException(LoginUserException.CODE_PWD_WRONG, "瀵嗙爜閿欒");
+                }
+                loginUser = userList.get(0);
+                loginUserExtra = new LoginUserExtra(loginUser.getId());
+                loginUserExtra.setEmail(dto.getEmail());
+            }
+            break;
+            case LoginUser.LOGIN_TYPE_QQ: {
+                if (dto.getQqUserInfo() == null || StringUtil.isNullOrEmpty(dto.getQqUserInfo().getOpenId())) {
+                    throw new LoginUserException(LoginUserException.CODE_PARAMS_NOT_ENOUGH, "QQ鎺堟潈淇℃伅涓虹┖");
+                }
+                List<LoginUser> userList = loginUserDao.list("from LoginUser lu where lu.systemId=? and lu.qqOpenId=? and lu.state=?", dto.getSystemId(), dto.getQqUserInfo().getOpenId(), LoginUser.STATE_NORMAL);
+                if (userList == null || userList.size() == 0) {
+                    throw new LoginUserException(LoginUserException.CODE_NO_USER, "鐢ㄦ埛涓嶅瓨鍦�");
+                }
+                loginUser = userList.get(0);
+                loginUserExtra = new LoginUserExtra(loginUser.getId());
+                loginUserExtra.setQqPortrait(dto.getQqUserInfo().getPortrait());
+                loginUserExtra.setQqOpenId(dto.getQqUserInfo().getOpenId());
+                loginUserExtra.setQqNickName(dto.getQqUserInfo().getName());
+            }
+            break;
+            case LoginUser.LOGIN_TYPE_WX: {
+                if (dto.getWeiXinUser() == null || StringUtil.isNullOrEmpty(dto.getWeiXinUser().getOpenid())) {
+                    throw new LoginUserException(LoginUserException.CODE_PARAMS_NOT_ENOUGH, "寰俊鎺堟潈淇℃伅涓虹┖");
+                }
+                List<LoginUser> userList = loginUserDao.list("from LoginUser lu where lu.systemId=? and lu.wxOpenId=? and lu.state=?", dto.getSystemId(), dto.getWeiXinUser().getOpenid(), LoginUser.STATE_NORMAL);
+                if (userList == null || userList.size() == 0) {
+                    throw new LoginUserException(LoginUserException.CODE_NO_USER, "鐢ㄦ埛涓嶅瓨鍦�");
+                }
+                loginUser = userList.get(0);
 
-	/**
-	 * 鐢ㄦ埛娉ㄩ攢
-	 * 
-	 * @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);
-	}
+                //鏇存柊寰俊淇℃伅
+                loginUser.setWxUnionId(dto.getWeiXinUser().getUnionid());
 
-	// 閭娉ㄥ唽
-	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 "娉ㄥ唽澶辫触";
-				}
-			}
-		}) + "";
-	}
+                loginUserExtra = new LoginUserExtra(loginUser.getId());
+                loginUserExtra.setWxPortrait(dto.getWeiXinUser().getHeadimgurl());
+                loginUserExtra.setWxOpenId(dto.getWeiXinUser().getOpenid());
+                loginUserExtra.setWxNickName(dto.getWeiXinUser().getNickname());
+                loginUserExtra.setWxSex(dto.getWeiXinUser().getSex());
+                loginUserExtra.setWxUnionId(dto.getWeiXinUser().getUnionid());
+            }
+            break;
+            case LoginUser.LOGIN_TYPE_PHONE: {
 
-	/**
-	 * 閭鐧诲綍
-	 * 
-	 * @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;
-			}
-		});
-	}
+                if (StringUtil.isNullOrEmpty(dto.getPhone())) {
+                    throw new LoginUserException(LoginUserException.CODE_PARAMS_NOT_ENOUGH, "鎵嬫満鍙蜂负绌�");
+                }
+                List<LoginUser> userList = loginUserDao.list("from LoginUser lu where lu.systemId=? and lu.phone=? and lu.state=?", dto.getSystemId(), dto.getPhone(), LoginUser.STATE_NORMAL);
+                if (userList == null || userList.size() == 0) {
+                    throw new LoginUserException(LoginUserException.CODE_NO_USER, "鐢ㄦ埛涓嶅瓨鍦�");
+                }
+                loginUser = userList.get(0);
+                loginUserExtra = new LoginUserExtra(loginUser.getId());
+            }
+            break;
+        }
+
+        if (loginUser != null) {
+            loginUser.setLoginType(dto.getLoginType());
+            loginUserDao.updateSelective(loginUser);
+        }
+
+        if (loginUserExtra != null) {
+            loginUserExtra.setUtdId(dto.getUtdId());
+            loginUserExtraDao.updateSelective(loginUserExtra);
+        }
+
+        return loginUser;
+    }
+
+
+    /**
+     * 娉ㄥ唽
+     *
+     * @param dto
+     */
+    public LoginUser register(LoginInfoDto dto) throws RegisterUserException {
+        LoginUser loginUser = null;
+        LoginUserExtra loginUserExtra = null;
+        switch (dto.getLoginType()) {
+            case LoginUser.LOGIN_TYPE_EMAIL: {
+                if (StringUtil.isNullOrEmpty(dto.getEmail()) || StringUtil.isNullOrEmpty(dto.getPwd())) {
+                    throw new RegisterUserException(RegisterUserException.CODE_PARAMS_NOT_ENOUGH, "閭鍜屽瘑鐮佷笉鑳戒负绌�");
+                }
+                List<LoginUser> userList = loginUserDao.list("from LoginUser lu where lu.systemId=? and lu.email=? and lu.state=?", dto.getSystemId(), dto.getEmail(), LoginUser.STATE_NORMAL);
+                if (userList != null && userList.size() > 0) {
+                    throw new RegisterUserException(RegisterUserException.CODE_USER_EXIST, "鐢ㄦ埛宸插瓨鍦�");
+                }
+
+                loginUser = new LoginUser();
+                loginUser.setLoginType(LoginUser.LOGIN_TYPE_EMAIL);
+                loginUser.setEmail(dto.getEmail());
+                loginUser.setPwd(dto.getPwd());
+                loginUser.setName(dto.getNickName());
+                loginUser.setState(LoginUser.STATE_NORMAL);
+                loginUser.setSystemId(dto.getSystemId());
+                loginUserExtra = new LoginUserExtra();
+                loginUserExtra.setEmail(dto.getEmail());
+            }
+            break;
+            case LoginUser.LOGIN_TYPE_QQ: {
+                if (dto.getQqUserInfo() == null || StringUtil.isNullOrEmpty(dto.getQqUserInfo().getOpenId())) {
+                    throw new RegisterUserException(RegisterUserException.CODE_PARAMS_NOT_ENOUGH, "QQ鎺堟潈淇℃伅涓虹┖");
+                }
+
+                List<LoginUser> userList = loginUserDao.list("from LoginUser lu where lu.systemId=? and lu.qqOpenId=? and lu.state=?", dto.getSystemId(), dto.getQqUserInfo().getOpenId(), LoginUser.STATE_NORMAL);
+                if (userList != null && userList.size() > 0) {
+                    throw new RegisterUserException(RegisterUserException.CODE_USER_EXIST, "鐢ㄦ埛宸插瓨鍦�");
+                }
+
+                loginUser = new LoginUser();
+                loginUser.setLoginType(LoginUser.LOGIN_TYPE_QQ);
+                loginUser.setQqOpenId(dto.getQqUserInfo().getOpenId());
+                loginUser.setName(dto.getQqUserInfo().getName());
+                loginUser.setPortrait(dto.getQqUserInfo().getPortrait());
+
+
+                loginUserExtra = new LoginUserExtra();
+                loginUserExtra.setQqPortrait(dto.getQqUserInfo().getPortrait());
+                loginUserExtra.setQqOpenId(dto.getQqUserInfo().getOpenId());
+                loginUserExtra.setQqNickName(dto.getQqUserInfo().getName());
+            }
+            break;
+            case LoginUser.LOGIN_TYPE_WX: {
+                if (dto.getWeiXinUser() == null || StringUtil.isNullOrEmpty(dto.getWeiXinUser().getOpenid())) {
+                    throw new RegisterUserException(RegisterUserException.CODE_PARAMS_NOT_ENOUGH, "寰俊鎺堟潈淇℃伅涓虹┖");
+                }
+                List<LoginUser> userList = loginUserDao.list("from LoginUser lu where lu.systemId=? and lu.wxOpenId=? and lu.state=?", dto.getSystemId(), dto.getWeiXinUser().getOpenid(), LoginUser.STATE_NORMAL);
+                if (userList != null && userList.size() > 0) {
+                    throw new RegisterUserException(RegisterUserException.CODE_USER_EXIST, "鐢ㄦ埛宸插瓨鍦�");
+                }
+                loginUser = new LoginUser();
+                loginUser.setLoginType(LoginUser.LOGIN_TYPE_WX);
+                loginUser.setWxUnionId(dto.getWeiXinUser().getUnionid());
+                loginUser.setName(dto.getWeiXinUser().getNickname());
+                loginUser.setWxOpenId(dto.getWeiXinUser().getOpenid());
+                loginUser.setPortrait(dto.getWeiXinUser().getHeadimgurl());
+
+
+                loginUserExtra = new LoginUserExtra();
+                loginUserExtra.setWxPortrait(dto.getWeiXinUser().getHeadimgurl());
+                loginUserExtra.setWxOpenId(dto.getWeiXinUser().getOpenid());
+                loginUserExtra.setWxNickName(dto.getWeiXinUser().getNickname());
+                loginUserExtra.setWxSex(dto.getWeiXinUser().getSex());
+                loginUserExtra.setWxUnionId(dto.getWeiXinUser().getUnionid());
+            }
+            break;
+            case LoginUser.LOGIN_TYPE_PHONE: {
+
+                if (StringUtil.isNullOrEmpty(dto.getPhone())) {
+                    throw new RegisterUserException(RegisterUserException.CODE_PARAMS_NOT_ENOUGH, "鎵嬫満鍙蜂负绌�");
+                }
+
+                List<LoginUser> userList = loginUserDao.list("from LoginUser lu where lu.systemId=? and lu.phone=? and lu.state=?", dto.getSystemId(), dto.getPhone(), LoginUser.STATE_NORMAL);
+                if (userList != null && userList.size() > 0) {
+                    throw new RegisterUserException(RegisterUserException.CODE_USER_EXIST, "鐢ㄦ埛宸插瓨鍦�");
+                }
+
+                loginUser = new LoginUser();
+                loginUser.setLoginType(LoginUser.LOGIN_TYPE_PHONE);
+                loginUser.setName(null);
+                loginUser.setPhone(dto.getPhone());
+
+                loginUserExtra = new LoginUserExtra();
+            }
+            break;
+        }
+
+        if (loginUser == null) {
+            throw new RegisterUserException(RegisterUserException.CODE_PARAMS_NOT_ENOUGH, "娉ㄥ唽绫诲瀷鍑洪敊");
+        }
+
+
+        loginUser.setIpinfo(dto.getIpInfo());
+        loginUser.setState(LoginUser.STATE_NORMAL);
+        loginUser.setSystemId(dto.getSystemId());
+        loginUser.setCreatetime(System.currentTimeMillis() + "");
+
+        Serializable uid = loginUserDao.save(loginUser);
+        loginUser.setId(uid + "");
+
+        if (StringUtil.isNullOrEmpty(loginUser.getName())) {
+            //璁剧疆榛樿鐢ㄦ埛鏄电О
+            String nickName = "鏃犲悕姘�";
+            LoginUser update = new LoginUser();
+            update.setId(uid + "");
+            update.setName(nickName);
+            loginUserDao.updateSelective(update);
+        }
+
+        loginUserExtra.setId(loginUser.getId());
+        loginUserExtra.setCreateTime(new Date());
+        loginUserService.initExtra(loginUserExtra);
+
+        return loginUser;
+    }
+
+    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.updateSelective(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 {
+                    user.setState(LoginUser.STATE_NORMAL);
+                    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.email=?")
+                        .setParameter(0, user.getOpenid()).list();
+                if (list != null && list.size() > 0) {
+                    if (list.get(0).getPwd().equalsIgnoreCase(user.getPwd()))
+                        return list.get(0);
+                }
+
+                return null;
+            }
+        });
+    }
 }

--
Gitblit v1.8.0