Ver Fonte

Merge remote-tracking branch 'origin/dev' into package

bilingfeng há 1 ano atrás
pai
commit
5cd947bba9

+ 66 - 0
game-module/game-module-mybatis/src/main/java/com/zanxiang/game/module/mybatis/entity/UserAgentLog.java

@@ -0,0 +1,66 @@
+package com.zanxiang.game.module.mybatis.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.*;
+
+import java.io.Serializable;
+import java.time.LocalDateTime;
+
+/**
+ * @author : lingfeng
+ * @time : 2023-07-20
+ * @description : 用户渠道日志
+ */
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+@ToString
+@Builder
+@TableName("t_user_agent_log")
+public class UserAgentLog implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 主键
+     */
+    @TableId(value = "id", type = IdType.AUTO)
+    private Long id;
+
+    /**
+     * 用户id
+     */
+    private Long userId;
+
+    /**
+     * 旧的渠道id
+     */
+    private Long oldAgentId;
+
+    /**
+     * 旧的渠道标识参数
+     */
+    private String oldChannel;
+
+    /**
+     * 新渠道id
+     */
+    private Long newAgentId;
+
+    /**
+     * 新渠道标识参数
+     */
+    private String newChannel;
+
+    /**
+     * 创建时间
+     */
+    private LocalDateTime createTime;
+
+    /**
+     * 更新时间
+     */
+    private LocalDateTime updateTime;
+}

+ 12 - 0
game-module/game-module-mybatis/src/main/java/com/zanxiang/game/module/mybatis/mapper/UserAgentLogMapper.java

@@ -0,0 +1,12 @@
+package com.zanxiang.game.module.mybatis.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.zanxiang.game.module.mybatis.entity.UserAgentLog;
+
+/**
+ * @author : lingfeng
+ * @time : 2023-07-20
+ * @description : 用户渠道变更记录
+ */
+public interface UserAgentLogMapper extends BaseMapper<UserAgentLog> {
+}

+ 1 - 1
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/SDKApplication.java

@@ -23,7 +23,7 @@ public class SDKApplication {
 
     public static void main(String[] args) {
         SpringApplication.run(SDKApplication.class, args);
-        System.out.println("赞象SDK服务启动成功 <dubbo升级3.0, windows支持米大师直接支付> ( ´・・)ノ(._.`) \n" +
+        System.out.println("赞象SDK服务启动成功 <dubbo升级3.0, 用户渠道记录逻辑代码提交> ( ´・・)ノ(._.`) \n" +
                 " ___________ _   __\n" +
                 "/  ___|  _  \\ | / /\n" +
                 "\\ `--.| | | | |/ / \n" +

+ 31 - 0
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/service/IUserAgentLogService.java

@@ -0,0 +1,31 @@
+package com.zanxiang.game.module.sdk.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.zanxiang.game.module.mybatis.entity.User;
+import com.zanxiang.game.module.mybatis.entity.UserAgentLog;
+
+/**
+ * @author : lingfeng
+ * @time : 2023-07-20
+ * @description : 用户渠道记录
+ */
+public interface IUserAgentLogService extends IService<UserAgentLog> {
+
+    /**
+     * 注册代理日志
+     *
+     * @param user 用户
+     * @return boolean
+     */
+    boolean regAgentLog(User user);
+
+    /**
+     * 代理更新日志
+     *
+     * @param user       用户
+     * @param newAgentId 新代理id
+     * @param newChannel 新渠道
+     * @return boolean
+     */
+    boolean agentUpdateLog(User user, Long newAgentId, String newChannel);
+}

+ 43 - 35
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/service/impl/AgentServiceImpl.java

@@ -2,16 +2,14 @@ package com.zanxiang.game.module.sdk.service.impl;
 
 import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.zanxiang.game.module.mybatis.entity.Agent;
 import com.zanxiang.game.module.mybatis.entity.User;
 import com.zanxiang.game.module.mybatis.entity.UserLoginLog;
 import com.zanxiang.game.module.mybatis.mapper.AgentMapper;
-import com.zanxiang.game.module.sdk.service.IAgentService;
-import com.zanxiang.game.module.sdk.service.ICallBackService;
-import com.zanxiang.game.module.sdk.service.IUserLoginLogService;
-import com.zanxiang.game.module.sdk.service.IUserService;
+import com.zanxiang.game.module.sdk.service.*;
 import com.zanxiang.module.util.JsonUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.logging.log4j.util.Strings;
@@ -42,41 +40,51 @@ public class AgentServiceImpl extends ServiceImpl<AgentMapper, Agent> implements
     @Autowired
     private ICallBackService callBackService;
 
+    @Autowired
+    private IUserAgentLogService userAgentLogService;
+
     @Override
     public void userAgentUpdate(User user, String channel) {
         log.error("用户登录接收到的渠道参数 userId : {}, channel : {}", user.getId(), channel);
-        //渠道参数为空
-        if (Strings.isBlank(channel)) {
-            return;
-        }
-        //判断用户是否超过30天未登录
-        List<UserLoginLog> userLoginLogList = userLoginLogService.list(new LambdaQueryWrapper<UserLoginLog>()
-                .eq(UserLoginLog::getUserId, user.getId())
-                .orderByDesc(UserLoginLog::getCreateTime)
-                .last("limit 1"));
-        if (CollectionUtils.isEmpty(userLoginLogList)) {
-            return;
-        }
-        //最近登录时间
-        LocalDateTime lastLoginTime = userLoginLogList.get(0).getCreateTime();
-        //最近登录未超过30天
-        if (lastLoginTime.plusDays(30L).isAfter(LocalDateTime.now())) {
-            return;
-        }
-        //30天未登录, 更新渠道
-        Map<String, String> urlParamMap = this.channelTransform(channel);
-        //查询渠道
-        Agent agent = this.getAgentByKey(urlParamMap);
-        if (agent == null) {
-            return;
+        try {
+            //渠道参数为空
+            if (Strings.isBlank(channel)) {
+                return;
+            }
+            //判断用户是否超过30天未登录
+            List<UserLoginLog> userLoginLogList = userLoginLogService.list(new LambdaQueryWrapper<UserLoginLog>()
+                    .eq(UserLoginLog::getUserId, user.getId())
+                    .orderByDesc(UserLoginLog::getCreateTime)
+                    .last("limit 1"));
+            if (CollectionUtils.isEmpty(userLoginLogList)) {
+                return;
+            }
+            //最近登录时间
+            LocalDateTime lastLoginTime = userLoginLogList.get(0).getCreateTime();
+            //最近登录未超过30天
+            if (lastLoginTime.plusDays(30L).isAfter(LocalDateTime.now())) {
+                return;
+            }
+            //30天未登录, 更新渠道
+            Map<String, String> urlParamMap = this.channelTransform(channel);
+            //查询渠道
+            Agent agent = this.getAgentByKey(urlParamMap);
+            if (agent == null) {
+                return;
+            }
+            //更新用户信息
+            userService.update(new LambdaUpdateWrapper<User>()
+                    .set(User::getAgentId, agent.getId())
+                    .set(User::getChannel, channel)
+                    .set(User::getUpdateTime, LocalDateTime.now())
+                    .eq(User::getId, user.getId()));
+            //添加渠道变更记录
+            userAgentLogService.agentUpdateLog(user, agent.getId(), channel);
+            //回传用户信息
+            callBackService.userCallBack(user, urlParamMap);
+        } catch (Exception e) {
+            log.error("用户渠道更新异常, userId : {}, channel : {}, e : {}", user.getId(), channel, e.getMessage());
         }
-        //更新用户信息
-        user.setAgentId(agent.getId());
-        user.setChannel(channel);
-        user.setUpdateTime(LocalDateTime.now());
-        userService.updateById(user);
-        //回传用户信息
-        callBackService.userCallBack(user, urlParamMap);
     }
 
     @Override

+ 4 - 0
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/service/impl/LoginServiceImpl.java

@@ -87,6 +87,9 @@ public class LoginServiceImpl implements IRegisterLoginService {
     @Autowired
     private IGameAppletShellService gameAppletShellService;
 
+    @Autowired
+    private IUserAgentLogService userAgentLogService;
+
     @Override
     @Transactional(rollbackFor = Exception.class)
     public ResultVO<UserLoginVO> loginWxCode(LoginVxCodeParam param, UserData userData) {
@@ -238,6 +241,7 @@ public class LoginServiceImpl implements IRegisterLoginService {
         user.setShareUserId(shareUserId);
         userService.save(user);
         gameUserService.createGameUser(user);
+        userAgentLogService.regAgentLog(user);
         //非分享用户, 回传注册
         if (shareUserId == null) {
             callBackService.userCallBack(user, tuple3.getT2());

+ 72 - 0
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/service/impl/UserAgentLogServiceImpl.java

@@ -0,0 +1,72 @@
+package com.zanxiang.game.module.sdk.service.impl;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.zanxiang.game.module.mybatis.entity.Agent;
+import com.zanxiang.game.module.mybatis.entity.User;
+import com.zanxiang.game.module.mybatis.entity.UserAgentLog;
+import com.zanxiang.game.module.mybatis.mapper.UserAgentLogMapper;
+import com.zanxiang.game.module.sdk.service.IUserAgentLogService;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.logging.log4j.util.Strings;
+import org.springframework.stereotype.Service;
+
+import java.time.LocalDateTime;
+
+/**
+ * @author : lingfeng
+ * @time : 2023-07-20
+ * @description : 用户渠道记录
+ */
+@Slf4j
+@Service
+public class UserAgentLogServiceImpl extends ServiceImpl<UserAgentLogMapper, UserAgentLog> implements IUserAgentLogService {
+
+    @Override
+    public boolean regAgentLog(User user) {
+        if (Agent.DEFAULT_AGENT.equals(user.getAgentId()) || Strings.isBlank(user.getChannel())) {
+            return Boolean.FALSE;
+        }
+        try {
+            return super.save(this.transform(user));
+        } catch (Exception e) {
+            log.error("用户注册渠道记录保存异常, userId : {}, e : {}", user.getId(), e.getMessage());
+        }
+        return Boolean.FALSE;
+    }
+
+    @Override
+    public boolean agentUpdateLog(User user, Long newAgentId, String newChannel) {
+        if (newAgentId == null || Strings.isBlank(newChannel)) {
+            return Boolean.FALSE;
+        }
+        try {
+            return super.save(this.transform(user, newAgentId, newChannel));
+        } catch (Exception e) {
+            log.error("用户渠道更新记录保存异常, userId : {}, newAgentId : {}, newChannel : {}, e : {}",
+                    user.getId(), newAgentId, newChannel, e.getMessage());
+        }
+        return Boolean.FALSE;
+    }
+
+    private UserAgentLog transform(User user) {
+        return UserAgentLog.builder()
+                .userId(user.getId())
+                .newAgentId(user.getAgentId())
+                .newChannel(user.getChannel())
+                .createTime(LocalDateTime.now())
+                .updateTime(LocalDateTime.now())
+                .build();
+    }
+
+    private UserAgentLog transform(User user, Long newAgentId, String newChannel) {
+        return UserAgentLog.builder()
+                .userId(user.getId())
+                .oldAgentId(Agent.DEFAULT_AGENT.equals(user.getAgentId()) ? null : user.getAgentId())
+                .oldChannel(Agent.DEFAULT_AGENT.equals(user.getAgentId()) ? null : user.getChannel())
+                .newAgentId(newAgentId)
+                .newChannel(newChannel)
+                .createTime(LocalDateTime.now())
+                .updateTime(LocalDateTime.now())
+                .build();
+    }
+}