Forráskód Böngészése

修改日志信息

zc 2 hónapja
szülő
commit
2ddd2d3c6c

+ 2 - 0
alien-entity/src/main/java/shop/alien/mapper/StoreUserMapper.java

@@ -34,4 +34,6 @@ public interface StoreUserMapper extends BaseMapper<StoreUser> {
     List<Map<String,Object>> selectStore(@Param("userPhones") String userPhones);
     List<Map<String,Object>> selectStore(@Param("userPhones") String userPhones);
 
 
     StoreUser getRemoveUser(@Param("id") String id);
     StoreUser getRemoveUser(@Param("id") String id);
+
+    StoreUser getUserIncludeDeleted(@Param("id") Integer id);
 }
 }

+ 6 - 0
alien-entity/src/main/resources/mapper/StoreUserMapper.xml

@@ -10,4 +10,10 @@
         FROM store_user
         FROM store_user
         where id = #{id}
         where id = #{id}
     </select>
     </select>
+
+    <select id="getUserIncludeDeleted" resultType="shop.alien.entity.store.StoreUser">
+        select id, phone, name, nick_name
+        FROM store_user
+        where id = #{id}
+    </select>
 </mapper>
 </mapper>

+ 138 - 5
alien-store-platform/src/main/java/shop/alien/storeplatform/aspect/PlatformOperationLogAspect.java

@@ -27,6 +27,7 @@ import shop.alien.entity.storePlatform.StorePlatformOperationLog;
 import shop.alien.mapper.StorePlatformMenuMapper;
 import shop.alien.mapper.StorePlatformMenuMapper;
 import shop.alien.mapper.StoreUserMapper;
 import shop.alien.mapper.StoreUserMapper;
 import shop.alien.storeplatform.annotation.PlatformOperationLog;
 import shop.alien.storeplatform.annotation.PlatformOperationLog;
+import shop.alien.storeplatform.dto.CreateAccountDto;
 import shop.alien.storeplatform.dto.CreateRoleDto;
 import shop.alien.storeplatform.dto.CreateRoleDto;
 import shop.alien.storeplatform.dto.UpdateRoleDto;
 import shop.alien.storeplatform.dto.UpdateRoleDto;
 import shop.alien.storeplatform.service.PlatformOperationLogService;
 import shop.alien.storeplatform.service.PlatformOperationLogService;
@@ -34,6 +35,10 @@ import com.alibaba.fastjson.JSONObject;
 import shop.alien.storeplatform.service.StorePlatformRoleMenuService;
 import shop.alien.storeplatform.service.StorePlatformRoleMenuService;
 import shop.alien.storeplatform.service.StorePlatformRoleQueryService;
 import shop.alien.storeplatform.service.StorePlatformRoleQueryService;
 import shop.alien.storeplatform.util.LoginUserUtil;
 import shop.alien.storeplatform.util.LoginUserUtil;
+import shop.alien.storeplatform.dto.UpdateAccountDto;
+import shop.alien.entity.store.StorePlatformUserRole;
+import shop.alien.mapper.StorePlatformUserRoleMapper;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletRequest;
 import java.lang.reflect.Method;
 import java.lang.reflect.Method;
@@ -59,6 +64,7 @@ public class PlatformOperationLogAspect {
     private final StorePlatformRoleMenuService storePlatformRoleMenuService;
     private final StorePlatformRoleMenuService storePlatformRoleMenuService;
     private final StorePlatformMenuMapper storePlatformMenuMapper;
     private final StorePlatformMenuMapper storePlatformMenuMapper;
     private final StoreUserMapper storeUserMapper;
     private final StoreUserMapper storeUserMapper;
+    private final StorePlatformUserRoleMapper storePlatformUserRoleMapper;
     private final StorePlatformRoleQueryService storePlatformRoleQueryService;
     private final StorePlatformRoleQueryService storePlatformRoleQueryService;
     private final SpelExpressionParser spelExpressionParser = new SpelExpressionParser();
     private final SpelExpressionParser spelExpressionParser = new SpelExpressionParser();
     private final DefaultParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
     private final DefaultParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
@@ -73,6 +79,7 @@ public class PlatformOperationLogAspect {
         MethodSignature signature = (MethodSignature) joinPoint.getSignature();
         MethodSignature signature = (MethodSignature) joinPoint.getSignature();
         Method method = signature.getMethod();
         Method method = signature.getMethod();
         PlatformOperationLog annotation = method.getAnnotation(PlatformOperationLog.class);
         PlatformOperationLog annotation = method.getAnnotation(PlatformOperationLog.class);
+        SubAccountSnapshot beforeSnapshot = buildSubAccountSnapshot(annotation, joinPoint.getArgs());
 
 
         Object result;
         Object result;
         try {
         try {
@@ -83,7 +90,7 @@ public class PlatformOperationLogAspect {
 
 
         if (annotation != null && shouldRecord(result)) {
         if (annotation != null && shouldRecord(result)) {
             try {
             try {
-                StorePlatformOperationLog logRecord = buildLogRecord(joinPoint, annotation, result);
+                StorePlatformOperationLog logRecord = buildLogRecord(joinPoint, annotation, result, beforeSnapshot);
                 platformOperationLogService.save(logRecord);
                 platformOperationLogService.save(logRecord);
             } catch (Exception e) {
             } catch (Exception e) {
                 log.warn("平台操作记录失败: {}", e.getMessage());
                 log.warn("平台操作记录失败: {}", e.getMessage());
@@ -100,7 +107,8 @@ public class PlatformOperationLogAspect {
         return true;
         return true;
     }
     }
 
 
-    private StorePlatformOperationLog buildLogRecord(ProceedingJoinPoint joinPoint, PlatformOperationLog annotation, Object result) {
+    private StorePlatformOperationLog buildLogRecord(ProceedingJoinPoint joinPoint, PlatformOperationLog annotation, Object result,
+                                                     SubAccountSnapshot beforeSnapshot) {
         MethodSignature signature = (MethodSignature) joinPoint.getSignature();
         MethodSignature signature = (MethodSignature) joinPoint.getSignature();
         Method method = signature.getMethod();
         Method method = signature.getMethod();
         Object[] args = joinPoint.getArgs();
         Object[] args = joinPoint.getArgs();
@@ -123,7 +131,7 @@ public class PlatformOperationLogAspect {
         logRecord.setOperationType(annotation.type());
         logRecord.setOperationType(annotation.type());
         logRecord.setOperationTime(new Date());
         logRecord.setOperationTime(new Date());
 
 
-        String content = resolveContent(annotation, context, args);
+        String content = resolveContent(annotation, context, args, beforeSnapshot);
         logRecord.setOperationContent(content);
         logRecord.setOperationContent(content);
         logRecord.setOperationParams(buildParamsJson(parameterNames, args));
         logRecord.setOperationParams(buildParamsJson(parameterNames, args));
 
 
@@ -194,13 +202,26 @@ public class PlatformOperationLogAspect {
         return ip;
         return ip;
     }
     }
 
 
-    private String resolveContent(PlatformOperationLog annotation, EvaluationContext context, Object[] args) {
+    private String resolveContent(PlatformOperationLog annotation, EvaluationContext context, Object[] args,
+                                  SubAccountSnapshot beforeSnapshot) {
         if ("账号操作记录".equals(annotation.module()) && annotation.type().contains("移除角色")) {
         if ("账号操作记录".equals(annotation.module()) && annotation.type().contains("移除角色")) {
             String content = buildAccountRoleRemoveContent(args);
             String content = buildAccountRoleRemoveContent(args);
             if (StringUtils.hasText(content)) {
             if (StringUtils.hasText(content)) {
                 return content;
                 return content;
             }
             }
         }
         }
+        if ("账号操作记录".equals(annotation.module()) && annotation.type().contains("新增子账号")) {
+            String content = buildCreateSubAccountContent(args);
+            if (StringUtils.hasText(content)) {
+                return content;
+            }
+        }
+        if ("账号操作记录".equals(annotation.module()) && annotation.type().contains("修改子账号")) {
+            String content = buildUpdateSubAccountContent(args, beforeSnapshot);
+            if (StringUtils.hasText(content)) {
+                return content;
+            }
+        }
         if ("角色操作记录".equals(annotation.module())) {
         if ("角色操作记录".equals(annotation.module())) {
             return buildRoleOperationContent(annotation.type(), args);
             return buildRoleOperationContent(annotation.type(), args);
         }
         }
@@ -213,6 +234,118 @@ public class PlatformOperationLogAspect {
         return annotation.type();
         return annotation.type();
     }
     }
 
 
+    private SubAccountSnapshot buildSubAccountSnapshot(PlatformOperationLog annotation, Object[] args) {
+        if (annotation == null || args == null || args.length == 0) {
+            return null;
+        }
+        if (!"账号操作记录".equals(annotation.module()) || !annotation.type().contains("修改子账号")) {
+            return null;
+        }
+        if (!(args[0] instanceof UpdateAccountDto)) {
+            return null;
+        }
+        UpdateAccountDto dto = (UpdateAccountDto) args[0];
+        if (dto.getUserId() == null || dto.getStoreId() == null) {
+            return null;
+        }
+        StoreUser storeUser = storeUserMapper.selectById(dto.getUserId());
+        String phone = storeUser != null ? storeUser.getPhone() : null;
+        String accountName = null;
+        Long roleId = null;
+
+        LambdaQueryWrapper<StorePlatformUserRole> userRoleQuery = new LambdaQueryWrapper<>();
+        userRoleQuery.eq(StorePlatformUserRole::getUserId, dto.getUserId())
+                .eq(StorePlatformUserRole::getStoreId, dto.getStoreId())
+                .eq(StorePlatformUserRole::getDeleteFlag, 0)
+                .last("LIMIT 1");
+        StorePlatformUserRole userRole = storePlatformUserRoleMapper.selectOne(userRoleQuery);
+        if (userRole != null) {
+            accountName = userRole.getAccountName();
+            roleId = userRole.getRoleId();
+        }
+
+        if (!StringUtils.hasText(accountName) && storeUser != null) {
+            accountName = StringUtils.hasText(storeUser.getName())
+                    ? storeUser.getName()
+                    : (StringUtils.hasText(storeUser.getNickName()) ? storeUser.getNickName() : storeUser.getPhone());
+        }
+        return new SubAccountSnapshot(phone, accountName, roleId);
+    }
+
+    private String buildUpdateSubAccountContent(Object[] args, SubAccountSnapshot beforeSnapshot) {
+        if (beforeSnapshot == null || args == null || args.length == 0) {
+            return null;
+        }
+        if (!(args[0] instanceof UpdateAccountDto)) {
+            return null;
+        }
+        UpdateAccountDto dto = (UpdateAccountDto) args[0];
+        StringBuilder changed = new StringBuilder();
+
+        if (StringUtils.hasText(dto.getPhone()) && !dto.getPhone().equals(beforeSnapshot.phone)) {
+            appendChange(changed, "手机号", beforeSnapshot.phone, dto.getPhone());
+        }
+        if (StringUtils.hasText(dto.getAccountName()) && !dto.getAccountName().equals(beforeSnapshot.accountName)) {
+            appendChange(changed, "账号名", beforeSnapshot.accountName, dto.getAccountName());
+        }
+        if (dto.getRoleId() != null && !dto.getRoleId().equals(beforeSnapshot.roleId)) {
+            String oldRoleName = resolveRoleName(beforeSnapshot.roleId);
+            String newRoleName = resolveRoleName(dto.getRoleId());
+            appendChange(changed, "角色", oldRoleName, newRoleName);
+        }
+
+        if (changed.length() == 0) {
+            return "修改子账号(无变更)";
+        }
+        return "修改子账号," + changed;
+    }
+
+    private String buildCreateSubAccountContent(Object[] args) {
+        if (args == null || args.length == 0) {
+            return null;
+        }
+        if (!(args[0] instanceof CreateAccountDto)) {
+            return null;
+        }
+        CreateAccountDto dto = (CreateAccountDto) args[0];
+        StringBuilder content = new StringBuilder();
+        content.append("新增子账号");
+        if (StringUtils.hasText(dto.getPhone())) {
+            content.append(",手机号:").append(dto.getPhone());
+        }
+        if (StringUtils.hasText(dto.getAccountName())) {
+            content.append(",账号名:").append(dto.getAccountName());
+        }
+        if (dto.getRoleId() != null) {
+            String roleName = resolveRoleName(dto.getRoleId());
+            content.append(",角色:").append(roleName);
+        }
+        return content.toString();
+    }
+
+    private void appendChange(StringBuilder builder, String label, String oldValue, String newValue) {
+        if (builder.length() > 0) {
+            builder.append(";");
+        }
+        builder.append(label)
+                .append(":")
+                .append(StringUtils.hasText(oldValue) ? oldValue : "空")
+                .append("→")
+                .append(StringUtils.hasText(newValue) ? newValue : "空");
+    }
+
+    private static class SubAccountSnapshot {
+        private final String phone;
+        private final String accountName;
+        private final Long roleId;
+
+        private SubAccountSnapshot(String phone, String accountName, Long roleId) {
+            this.phone = phone;
+            this.accountName = accountName;
+            this.roleId = roleId;
+        }
+    }
+
     private String buildAccountRoleRemoveContent(Object[] args) {
     private String buildAccountRoleRemoveContent(Object[] args) {
         if (args == null || args.length < 2) {
         if (args == null || args.length < 2) {
             return null;
             return null;
@@ -235,7 +368,7 @@ public class PlatformOperationLogAspect {
     }
     }
 
 
     private String resolveAccountName(Integer userId) {
     private String resolveAccountName(Integer userId) {
-        StoreUser storeUser = storeUserMapper.selectById(userId);
+        StoreUser storeUser = storeUserMapper.getUserIncludeDeleted(userId);
         if (storeUser == null) {
         if (storeUser == null) {
             return "未知";
             return "未知";
         }
         }