From d73687bc6115007145b4aab050e4e29ff87fd8ae Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期一, 01 三月 2021 18:44:36 +0800
Subject: [PATCH] 布丸代码优化

---
 src/main/java/com/yeshi/buwan/service/imp/UserService.java |  261 +++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 216 insertions(+), 45 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 657acc8..75b446e 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.dto.user.QQUserInfo;
+import com.yeshi.buwan.exception.user.LoginUserException;
+import com.yeshi.buwan.exception.user.RegisterUserException;
 import org.hibernate.HibernateException;
 import org.hibernate.Session;
 import org.springframework.cache.annotation.Cacheable;
@@ -20,6 +27,7 @@
 import com.yeshi.buwan.domain.UserInfo;
 import com.yeshi.buwan.util.Constant;
 import com.yeshi.buwan.util.StringUtil;
+import org.yeshi.utils.entity.wx.WeiXinUser;
 
 @Service
 public class UserService {
@@ -34,13 +42,9 @@
     @Resource
     private LoginUserDao loginUserDao;
 
-    public UserDao getUserDao() {
-        return userDao;
-    }
+    @Resource
+    private LoginUserExtraDao loginUserExtraDao;
 
-    public void setUserDao(UserDao userDao) {
-        this.userDao = userDao;
-    }
 
     // 鐢ㄦ埛鎿嶄綔
     public List<UserInfo> getUserList(int system, int page) {
@@ -155,52 +159,219 @@
 
 
     /**
+     * 鐧诲綍
      *
-     * @param openid
-     * @param detailSystem
-     * @param type 1-QQ 2-寰俊
-     * @param portrait
-     * @param name
-     * @param ipInfo
+     * @param dto
      * @return
+     * @throws LoginUserException
      */
-    @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();
+    public LoginUser login(LoginInfoDto dto) throws LoginUserException {
 
-                    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);
-                        lu.setState(LoginUser.STATE_NORMAL);
-                        session.save(lu);
-                        session.flush();
-                        session.getTransaction().commit();
-                        return lu;
-                    }
+        LoginUser loginUser = null;
 
-                } catch (Exception e) {
-                    e.printStackTrace();
-                    session.getTransaction().rollback();
+        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, "閭鍜屽瘑鐮佷笉鑳戒负绌�");
                 }
-                return null;
-            }
-        });
+                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, "鐢ㄦ埛涓嶅瓨鍦�");
+                }
 
+                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);
+
+                //鏇存柊寰俊淇℃伅
+                loginUser.setWxUnionId(dto.getWeiXinUser().getUnionid());
+
+                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: {
+
+                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.update(loginUser);
+        }
+
+        if (loginUserExtra != null) {
+            loginUserExtraDao.updateSelective(loginUserExtra);
+        }
+
+        return loginUser;
+    }
+
+
+    /**
+     * 娉ㄥ唽
+     *
+     * @param dto
+     */
+    public void 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());
+
+
+                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());
+
+
+                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());
+        loginUserExtraDao.save(loginUserExtra);
     }
 
     public LoginUser getLoginUser(String id) {

--
Gitblit v1.8.0