Explorar o código

登录异步优化

chao %!s(int64=3) %!d(string=hai) anos
pai
achega
6bb2483410

+ 2 - 1
src/main/java/com/caimei365/user/UserApplication.java

@@ -4,6 +4,7 @@ import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 import org.springframework.cloud.openfeign.EnableFeignClients;
+import org.springframework.scheduling.annotation.EnableAsync;
 
 
 /**
@@ -13,7 +14,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
  * @author : Charles
  * @date : 2021/2/22
  */
-
+@EnableAsync
 @EnableDiscoveryClient
 @SpringBootApplication
 @EnableFeignClients(basePackages = {"com.caimei365.user.feign"})

+ 66 - 0
src/main/java/com/caimei365/user/service/impl/AsyncService.java

@@ -0,0 +1,66 @@
+package com.caimei365.user.service.impl;
+
+import com.caimei365.user.mapper.*;
+import com.caimei365.user.model.po.UserBeansHistoryPo;
+import com.caimei365.user.model.vo.UserVo;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.util.Date;
+
+/**
+ * 异步方法 @Async
+ * 类中需要使用@Autowired或@Resource等注解自动注入,不能自己手动new对象
+ * 在启动类中增加@EnableAsync注解
+ * 在Async 方法上标注@Transactional是没用的。 在Async 方法调用的方法上标注@Transactional 有效。
+ * 调用被@Async标记的方法的调用者不能和被调用的方法在同一类中不然不会起作用!!!!!!!
+ * 使用@Async时要求是不能有返回值
+ * @author : Charles
+ * @date : 2021/11/12
+ */
+@Slf4j
+@Component
+public class AsyncService {
+    @Resource
+    private LoginMapper loginMapper;
+    @Resource
+    private RegisterMapper registerMapper;
+    @Resource
+    private ClubMapper clubMapper;
+    @Resource
+    private BaseMapper baseMapper;
+
+    @Async
+    public void loginUpdateBeans(Integer userId, Integer identity) {
+        boolean isClub = null != identity && (2 == identity || 4 == identity);
+        if (isClub){
+            // 查询今天是否有登录送豆
+            Integer id = clubMapper.findLoginBeans(userId);
+            if (id == null) {
+                //登录赠送10采美豆
+                UserVo user = baseMapper.getUserByUserId(userId);
+                UserBeansHistoryPo beansHistory = new UserBeansHistoryPo();
+                beansHistory.setUserId(user.getUserId());
+                beansHistory.setBeansType(12);
+                beansHistory.setType(1);
+                beansHistory.setNum(10);
+                beansHistory.setPushStatus(0);
+                beansHistory.setAddTime(new Date());
+                registerMapper.insertBeansHistory(beansHistory);
+                Integer userBeans = user.getUserBeans();
+                userBeans = userBeans == null ? 10 : userBeans + 10;
+                clubMapper.updateUserBeans(userId, userBeans);
+                log.info("【登录】-----每天初次登录赠送10采美豆,userId:" + userId);
+            }
+        }
+        // 改user表登录时间
+        try {
+            loginMapper.updateLogin(userId);
+            log.info("【登录】-----登陆时间录入,userId:" + userId);
+        } catch (Exception e) {
+            log.error("【登录】--登录时间记录异常" + e);
+        }
+    }
+}

+ 9 - 41
src/main/java/com/caimei365/user/service/impl/LoginServiceImpl.java

@@ -5,7 +5,10 @@ import com.alibaba.fastjson.JSONObject;
 import com.caimei365.user.components.CommonService;
 import com.caimei365.user.components.RedisService;
 import com.caimei365.user.components.WeChatService;
-import com.caimei365.user.mapper.*;
+import com.caimei365.user.mapper.LoginMapper;
+import com.caimei365.user.mapper.OperationMapper;
+import com.caimei365.user.mapper.RegisterMapper;
+import com.caimei365.user.mapper.SuperVipMapper;
 import com.caimei365.user.model.ResponseJson;
 import com.caimei365.user.model.dto.AuthBindDto;
 import com.caimei365.user.model.dto.LoginPasswordDto;
@@ -13,9 +16,7 @@ import com.caimei365.user.model.dto.ScanBindDto;
 import com.caimei365.user.model.dto.SuperVipDto;
 import com.caimei365.user.model.po.OperationPo;
 import com.caimei365.user.model.po.SuperVipPo;
-import com.caimei365.user.model.po.UserBeansHistoryPo;
 import com.caimei365.user.model.vo.UserLoginVo;
-import com.caimei365.user.model.vo.UserVo;
 import com.caimei365.user.service.LoginService;
 import com.caimei365.user.utils.JwtUtil;
 import com.caimei365.user.utils.Md5Util;
@@ -23,7 +24,6 @@ import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang.StringUtils;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.HttpHeaders;
-import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
@@ -54,9 +54,7 @@ public class LoginServiceImpl implements LoginService {
     @Resource
     private OperationMapper operationMapper;
     @Resource
-    private ClubMapper clubMapper;
-    @Resource
-    private BaseMapper baseMapper;
+    private AsyncService asyncService;
 
     /**
      * 小程序邀请码过期天数
@@ -294,6 +292,7 @@ public class LoginServiceImpl implements LoginService {
                 return ResponseJson.error(-3, "您的企业账号审核未通过", loginUser);
             }
         }
+        // 超级会员
         SuperVipDto end = findEnd(loginUser.getUserId());
         loginUser.setVipFlag(end.getVipFlag());
         if (1 == end.getVipFlag() && 4 == loginUser.getUserIdentity()) {
@@ -301,43 +300,12 @@ public class LoginServiceImpl implements LoginService {
             loginUser.setUserIdentity(2);
             loginUser.setUserPermission(2);
         }
-        // 登录赠送10采美豆
-        loginUpdateBeans(loginUser.getUserId(), loginUser.getUserIdentity());
-
+        // 登录异步赠送10采美豆
+        asyncService.loginUpdateBeans(loginUser.getUserId(), loginUser.getUserIdentity());
+        log.info("【登录】-----登录成功,userId:" + loginUser.getUserId());
         return ResponseJson.success("登录成功", loginUser);
     }
 
-    @Async
-    void loginUpdateBeans(Integer userId, Integer identity) {
-        if (null != identity && (2 == identity || 4 == identity)){
-            // 查询今天是否有登录送豆
-            Integer id = clubMapper.findLoginBeans(userId);
-            if (id == null) {
-                //登录赠送10采美豆
-                UserVo user = baseMapper.getUserByUserId(userId);
-                UserBeansHistoryPo beansHistory = new UserBeansHistoryPo();
-                beansHistory.setUserId(user.getUserId());
-                beansHistory.setBeansType(12);
-                beansHistory.setType(1);
-                beansHistory.setNum(10);
-                beansHistory.setPushStatus(0);
-                beansHistory.setAddTime(new Date());
-                registerMapper.insertBeansHistory(beansHistory);
-                Integer userBeans = user.getUserBeans();
-                userBeans = userBeans == null ? 10 : userBeans + 10;
-                clubMapper.updateUserBeans(userId, userBeans);
-                log.info("【登录成功】-----每天初次登录赠送10采美豆,userId:" + userId);
-            }
-        }
-        //改user表登录时间
-        try {
-            log.info("【登录成功】-----登陆时间录入,userId:" + userId);
-            loginMapper.updateLogin(userId);
-        } catch (Exception e) {
-            log.error("登录时间记录异常" + e);
-        }
-    }
-
     /**
      * 获取生成微信二维码的参数(www)
      *