Bladeren bron

子账号功能代码格式优化

qinxuyang 2 maanden geleden
bovenliggende
commit
40c05f50f2

+ 94 - 204
alien-gateway/src/main/java/shop/alien/gateway/service/impl/StoreUserServiceImpl.java

@@ -52,9 +52,9 @@ public class StoreUserServiceImpl extends ServiceImpl<StoreUserGatewayMapper, St
     private final BaseRedisService baseRedisService;
 
     private final StorePlatformUserRoleGatewayMapper storePlatformUserRoleMapper;
-    
+
     private final StorePlatformRoleMenuGatewayMapper storePlatformRoleMenuMapper;
-    
+
     private final StorePlatformRoleGatewayMapper storePlatformRoleMapper;
 
     /**
@@ -96,125 +96,107 @@ public class StoreUserServiceImpl extends ServiceImpl<StoreUserGatewayMapper, St
         tokenMap.put("userType", "store");
         storeUserVo.setToken(JwtUtil.createJWT("store_" + storeUser.getPhone(), storeUser.getName(), JSONObject.toJSONString(tokenMap), effectiveTimeIntLong));
         baseRedisService.setString("store_" + storeUser.getPhone(), storeUserVo.getToken());
-//        if(storeUserVo.getStoreId() == null){
-            if(storeUserVo.getAccountType() == 1){
-                // 主账号登录,判断是否有子账号
-                // 通过 store_platform_user_role 表中的关联记录判断
-                // 查询该门店下是否有其他账号(子账号)在 store_platform_user_role 表中有关联记录
-                    LambdaQueryWrapper<StorePlatformUserRole> roleWrapper = new LambdaQueryWrapper<>();
-                    roleWrapper.eq(StorePlatformUserRole::getUserId, storeUser.getId()) // 排除当前账号自己
-                            .eq(StorePlatformUserRole::getDeleteFlag, 0);
-                    long subAccountCount = storePlatformUserRoleMapper.selectCount(roleWrapper);
-                    if(subAccountCount > 0){
-                        storeUserVo.setHasSubAccount(true);
-                    }else{
-                        storeUserVo.setHasSubAccount(false);
-                    }
-                    log.info("主账号登录,检查子账号 - userId: {}, storeId: {}, hasSubAccount: {}, subAccountCount: {}",
-                            storeUser.getId(), storeUserVo.getStoreId(), false, subAccountCount);
-            }else {
-                // 子账号切换如果有storeId 则为子账号切换无感登录 否则为正常登录
-                if (storeUser.getStoreId() != null) {
-                    LambdaQueryWrapper<StorePlatformUserRole> roleWrapper = new LambdaQueryWrapper<>();
-                    roleWrapper.eq(StorePlatformUserRole::getUserId, storeUser.getId())
-                            .eq(StorePlatformUserRole::getStoreId, storeUser.getStoreId())
-                            .eq(StorePlatformUserRole::getDeleteFlag, 0);
-                    StorePlatformUserRole userRole = storePlatformUserRoleMapper.selectOne(roleWrapper);
-
-                    if (userRole == null) {
-                        log.warn("子账号未关联目标门店 - userId: {}, storeId: {}", storeUser.getId(), storeUser.getStoreId());
-                        return R.fail("切换失败,请确认子账号是否关联了该门店");
-                    }
 
-                    int finalStoreId = userRole.getStoreId(); // 从关联表获取门店ID
-                    StoreInfo storeInfo = storeInfoMapper.selectById(finalStoreId);
-//                    long roleId = userRole.getRoleId() != null ? userRole.getRoleId() : null;
-                    // 查询角色名称
-                    if (userRole.getRoleId() != null) {
-                        StorePlatformRole role = storePlatformRoleMapper.selectById(userRole.getRoleId());
-                        if (role != null) {
-                            storeUserVo.setRoleName(role.getRoleName());
-                        }
+        if (storeUserVo.getAccountType() == 1) {
+            // 主账号登录,判断是否有子账号
+            // 通过 store_platform_user_role 表中的关联记录判断
+            // 查询该门店下是否有其他账号(子账号)在 store_platform_user_role 表中有关联记录
+            LambdaQueryWrapper<StorePlatformUserRole> roleWrapper = new LambdaQueryWrapper<>();
+            roleWrapper.eq(StorePlatformUserRole::getUserId, storeUser.getId()) // 排除当前账号自己
+                    .eq(StorePlatformUserRole::getDeleteFlag, 0);
+            long subAccountCount = storePlatformUserRoleMapper.selectCount(roleWrapper);
+            if (subAccountCount > 0) {
+                storeUserVo.setHasSubAccount(true);
+            } else {
+                storeUserVo.setHasSubAccount(false);
+            }
+            log.info("主账号登录,检查子账号 - userId: {}, storeId: {}, hasSubAccount: {}, subAccountCount: {}",
+                    storeUser.getId(), storeUserVo.getStoreId(), false, subAccountCount);
+        } else {
+            // 子账号切换如果有storeId 则为子账号切换无感登录 否则为正常登录
+            if (storeUser.getStoreId() != null) {
+                LambdaQueryWrapper<StorePlatformUserRole> roleWrapper = new LambdaQueryWrapper<>();
+                roleWrapper.eq(StorePlatformUserRole::getUserId, storeUser.getId())
+                        .eq(StorePlatformUserRole::getStoreId, storeUser.getStoreId())
+                        .eq(StorePlatformUserRole::getDeleteFlag, 0);
+                StorePlatformUserRole userRole = storePlatformUserRoleMapper.selectOne(roleWrapper);
+
+                if (userRole == null) {
+                    log.warn("子账号未关联目标门店 - userId: {}, storeId: {}", storeUser.getId(), storeUser.getStoreId());
+                    return R.fail("切换失败,请确认子账号是否关联了该门店");
+                }
+
+                int finalStoreId = userRole.getStoreId(); // 从关联表获取门店ID
+                StoreInfo storeInfo = storeInfoMapper.selectById(finalStoreId);
+                // 查询角色名称
+                if (userRole.getRoleId() != null) {
+                    StorePlatformRole role = storePlatformRoleMapper.selectById(userRole.getRoleId());
+                    if (role != null) {
+                        storeUserVo.setRoleName(role.getRoleName());
                     }
-                    storeUserVo.setRoleId(userRole.getRoleId());
-                    storeUserVo.setStoreId(storeInfo.getId());
-                    storeUserVo.setHasSubAccount(true);
-                    storeUserVo.setRoleName(userRole.getAccountName());
-                } else {
+                }
+                storeUserVo.setRoleId(userRole.getRoleId());
+                storeUserVo.setStoreId(storeInfo.getId());
+                storeUserVo.setHasSubAccount(true);
+                storeUserVo.setRoleName(userRole.getAccountName());
+            } else {
+
+                // 子账号登录,查询当前子账号权限最多的那个门店和角色id
+                log.info("子账号登录,storeId为空,查询权限最多的门店 - userId: {}", storeUser.getId());
+                // 查询子账号关联的所有门店和角色
+                LambdaQueryWrapper<StorePlatformUserRole> roleWrapper = new LambdaQueryWrapper<>();
+                roleWrapper.eq(StorePlatformUserRole::getUserId, storeUser.getId())
+                        .eq(StorePlatformUserRole::getDeleteFlag, 0);
+                List<StorePlatformUserRole> userRoles = storePlatformUserRoleMapper.selectList(roleWrapper);
+
+                if (userRoles != null && !userRoles.isEmpty()) {
+                    // 计算每个角色的菜单权限数量,选择权限最多的门店
+                    StorePlatformUserRole maxPermissionRole = null;
+                    int maxPermissionCount = -1;
 
-                    // 子账号登录,查询当前子账号权限最多的那个门店和角色id
-                    log.info("子账号登录,storeId为空,查询权限最多的门店 - userId: {}", storeUser.getId());
-                    // 查询子账号关联的所有门店和角色
-                    LambdaQueryWrapper<StorePlatformUserRole> roleWrapper = new LambdaQueryWrapper<>();
-                    roleWrapper.eq(StorePlatformUserRole::getUserId, storeUser.getId())
-                            .eq(StorePlatformUserRole::getDeleteFlag, 0);
-                    List<StorePlatformUserRole> userRoles = storePlatformUserRoleMapper.selectList(roleWrapper);
-
-                    if (userRoles != null && !userRoles.isEmpty()) {
-                        // 计算每个角色的菜单权限数量,选择权限最多的门店
-                        StorePlatformUserRole maxPermissionRole = null;
-                        int maxPermissionCount = -1;
-
-                        for (StorePlatformUserRole userRole : userRoles) {
-                            if (userRole.getRoleId() != null) {
-                                // 查询该角色的菜单权限数量
-                                Integer permissionCount = storePlatformRoleMenuMapper.countMenuByRoleId(userRole.getRoleId());
-                                if (permissionCount == null) {
-                                    permissionCount = 0;
-                                }
-
-                                // 如果权限数量更多,或者权限数量相同但角色ID更大,则更新
-                                if (permissionCount > maxPermissionCount ||
-                                        (permissionCount == maxPermissionCount &&
-                                                (maxPermissionRole == null || userRole.getRoleId() > maxPermissionRole.getRoleId()))) {
-                                    maxPermissionCount = permissionCount;
-                                    maxPermissionRole = userRole;
-                                }
+                    for (StorePlatformUserRole userRole : userRoles) {
+                        if (userRole.getRoleId() != null) {
+                            // 查询该角色的菜单权限数量
+                            Integer permissionCount = storePlatformRoleMenuMapper.countMenuByRoleId(userRole.getRoleId());
+                            if (permissionCount == null) {
+                                permissionCount = 0;
                             }
-                        }
 
-                        if (maxPermissionRole != null) {
-                            // 设置门店ID和角色ID
-                            storeUserVo.setStoreId(maxPermissionRole.getStoreId());
-                            storeUserVo.setRoleId(maxPermissionRole.getRoleId());
-                            storeUserVo.setHasSubAccount(true);
-
-                            // 查询角色名称
-                            if (maxPermissionRole.getRoleId() != null) {
-                                StorePlatformRole role = storePlatformRoleMapper.selectById(maxPermissionRole.getRoleId());
-                                if (role != null) {
-                                    storeUserVo.setRoleName(role.getRoleName());
-                                }
+                            // 如果权限数量更多,或者权限数量相同但角色ID更大,则更新
+                            if (permissionCount > maxPermissionCount ||
+                                    (permissionCount == maxPermissionCount &&
+                                            (maxPermissionRole == null || userRole.getRoleId() > maxPermissionRole.getRoleId()))) {
+                                maxPermissionCount = permissionCount;
+                                maxPermissionRole = userRole;
                             }
+                        }
+                    }
 
-                            log.info("子账号权限最多的门店查询成功 - userId: {}, storeId: {}, roleId: {}, roleName: {}, 权限数量: {}",
-                                    storeUser.getId(), maxPermissionRole.getStoreId(), maxPermissionRole.getRoleId(),
-                                    storeUserVo.getRoleName(), maxPermissionCount);
-                        } else {
-                            log.warn("子账号关联的门店都没有角色ID - userId: {}", storeUser.getId());
+                    if (maxPermissionRole != null) {
+                        // 设置门店ID和角色ID
+                        storeUserVo.setStoreId(maxPermissionRole.getStoreId());
+                        storeUserVo.setRoleId(maxPermissionRole.getRoleId());
+                        storeUserVo.setHasSubAccount(true);
+
+                        // 查询角色名称
+                        if (maxPermissionRole.getRoleId() != null) {
+                            StorePlatformRole role = storePlatformRoleMapper.selectById(maxPermissionRole.getRoleId());
+                            if (role != null) {
+                                storeUserVo.setRoleName(role.getRoleName());
+                            }
                         }
+
+                        log.info("子账号权限最多的门店查询成功 - userId: {}, storeId: {}, roleId: {}, roleName: {}, 权限数量: {}",
+                                storeUser.getId(), maxPermissionRole.getStoreId(), maxPermissionRole.getRoleId(),
+                                storeUserVo.getRoleName(), maxPermissionCount);
                     } else {
-                        log.warn("子账号未关联任何门店 - userId: {}", storeUser.getId());
+                        log.warn("子账号关联的门店都没有角色ID - userId: {}", storeUser.getId());
                     }
+                } else {
+                    log.warn("子账号未关联任何门店 - userId: {}", storeUser.getId());
                 }
             }
-
-//                // 如果storeId不为空,查询指定的门店
-//                LambdaQueryWrapper<StorePlatformUserRole> roleWrapper = new LambdaQueryWrapper<>();
-//                roleWrapper.eq(StorePlatformUserRole::getUserId, userId)
-//                        .eq(StorePlatformUserRole::getStoreId, storeId)
-//                        .eq(StorePlatformUserRole::getDeleteFlag, 0);
-//                StorePlatformUserRole userRole = storePlatformUserRoleMapper.selectOne(roleWrapper);
-//
-//                if (userRole == null) {
-//                    log.warn("子账号未关联目标门店 - userId: {}, storeId: {}", userId, storeId);
-//                    return R.fail("切换失败,请确认子账号是否关联了该门店");
-//                }
-//
-//                finalStoreId = userRole.getStoreId(); // 从关联表获取门店ID
-//                storeInfo = storeInfoMapper.selectById(finalStoreId);
-//                roleId = userRole.getRoleId() != null ? userRole.getRoleId() : null;
-
+        }
 
         // 查询门店信息(如果storeId不为空)
         StoreInfo storeInfo = null;
@@ -249,118 +231,26 @@ public class StoreUserServiceImpl extends ServiceImpl<StoreUserGatewayMapper, St
             log.warn("账号被禁用 - userId: {}", userId);
             return R.fail("账号被禁用");
         }
-//
-//        // 3. 判断是主账号还是子账号,并验证门店关联
-//        Long roleId = null;
-//        StoreInfo storeInfo = null;
-//
-//        // 判断是主账号还是子账号
-//        if (storeUser.getStoreId() != null && (storeId == null || storeUser.getStoreId().equals(storeId))) {
-//            // 主账号:直接回显 store_user 的门店 id
-//            finalStoreId = storeUser.getStoreId();
-//            roleId = null; // 主账号没有角色id
-//            if (finalStoreId != null) {
-//                storeInfo = storeInfoMapper.selectById(finalStoreId);
-//            }
-//        } else {
-//            // 子账号:查询角色关联表,回显子账号关联的门店 id
-//            if (storeId == null) {
-//                // 如果storeId为空,查询子账号权限最多的门店
-//                LambdaQueryWrapper<StorePlatformUserRole> roleWrapper = new LambdaQueryWrapper<>();
-//                roleWrapper.eq(StorePlatformUserRole::getUserId, userId)
-//                        .eq(StorePlatformUserRole::getDeleteFlag, 0);
-//                List<StorePlatformUserRole> userRoles = storePlatformUserRoleMapper.selectList(roleWrapper);
-//
-//                if (userRoles != null && !userRoles.isEmpty()) {
-//                    // 计算每个角色的菜单权限数量,选择权限最多的门店
-//                    StorePlatformUserRole maxPermissionRole = null;
-//                    int maxPermissionCount = -1;
-//
-//                    for (StorePlatformUserRole userRole : userRoles) {
-//                        if (userRole.getRoleId() != null) {
-//                            // 查询该角色的菜单权限数量
-//                            Integer permissionCount = storePlatformRoleMenuMapper.countMenuByRoleId(userRole.getRoleId());
-//                            if (permissionCount == null) {
-//                                permissionCount = 0;
-//                            }
-//
-//                            // 如果权限数量更多,或者权限数量相同但角色ID更大,则更新
-//                            if (permissionCount > maxPermissionCount ||
-//                                (permissionCount == maxPermissionCount &&
-//                                 (maxPermissionRole == null || userRole.getRoleId() > maxPermissionRole.getRoleId()))) {
-//                                maxPermissionCount = permissionCount;
-//                                maxPermissionRole = userRole;
-//                            }
-//                        }
-//                    }
-//
-//                    if (maxPermissionRole != null) {
-//                        finalStoreId = maxPermissionRole.getStoreId();
-//                        roleId = maxPermissionRole.getRoleId() != null ? maxPermissionRole.getRoleId() : null;
-//                        storeInfo = storeInfoMapper.selectById(finalStoreId);
-//                    } else {
-//                        log.warn("子账号关联的门店都没有角色ID - userId: {}", userId);
-//                        return R.fail("切换失败,子账号未关联任何门店");
-//                    }
-//                } else {
-//                    log.warn("子账号未关联任何门店 - userId: {}", userId);
-//                    return R.fail("切换失败,子账号未关联任何门店");
-//                }
-//            } else {
-//                // 如果storeId不为空,查询指定的门店
-//                LambdaQueryWrapper<StorePlatformUserRole> roleWrapper = new LambdaQueryWrapper<>();
-//                roleWrapper.eq(StorePlatformUserRole::getUserId, userId)
-//                        .eq(StorePlatformUserRole::getStoreId, storeId)
-//                        .eq(StorePlatformUserRole::getDeleteFlag, 0);
-//                StorePlatformUserRole userRole = storePlatformUserRoleMapper.selectOne(roleWrapper);
-//
-//                if (userRole == null) {
-//                    log.warn("子账号未关联目标门店 - userId: {}, storeId: {}", userId, storeId);
-//                    return R.fail("切换失败,请确认子账号是否关联了该门店");
-//                }
-//
-//                finalStoreId = userRole.getStoreId(); // 从关联表获取门店ID
-//                storeInfo = storeInfoMapper.selectById(finalStoreId);
-//                roleId = userRole.getRoleId() != null ? userRole.getRoleId() : null;
-//            }
-//        }
 
-        // 4. 删除旧的token(参考switchingStates逻辑)
+        // 3. 删除旧的token(参考switchingStates逻辑)
         baseRedisService.delete("store_" + storeUser.getPhone());
         baseRedisService.delete("storePlatform_" + storeUser.getPhone());
         log.info("删除用户token - phone: {}", storeUser.getPhone());
 
-        // 5. 创建临时用户对象用于生成token(设置正确的门店ID)
+        // 4. 创建临时用户对象用于生成token(设置正确的门店ID)
         StoreUser tempUser = new StoreUser();
         BeanUtils.copyProperties(storeUser, tempUser);
         tempUser.setAccountType(accountType);
         tempUser.setStoreId(storeId);
 
-        // 6. 生成token(类似登录接口)
+        // 6. 生成token(类似登录接口)走切换账号逻辑
         R<StoreUserVo> tokenResult = createToKen(tempUser);
         if (tokenResult.getCode() != 200 || tokenResult.getData() == null) {
             log.error("生成token失败 - userId: {}", userId);
             return R.fail("切换失败,token生成失败");
         }
 
-        // 7. 设置角色ID和门店ID(如果没有角色id,该字段为null)
         StoreUserVo storeUserVo = tokenResult.getData();
-//        storeUserVo.setRoleId(roleId);
-//        storeUserVo.setStoreId(finalStoreId); // 确保回显正确的门店ID
-//
-//        // 查询并设置角色名称
-//        if (roleId != null) {
-//            StorePlatformRole role = storePlatformRoleMapper.selectById(roleId);
-//            if (role != null) {
-//                storeUserVo.setRoleName(role.getRoleName());
-//            }
-//        }
-//
-//        if(storeInfo != null){
-//            storeUserVo.setName(storeInfo.getStoreName());
-//        }
-//
-//        log.info("切换门店成功 - userId: {}, storeId: {}, roleId: {}", userId, finalStoreId, roleId);
         return R.data(storeUserVo);
     }
 

+ 2 - 0
alien-store/src/main/java/shop/alien/store/service/impl/SubAccountStoreServiceImpl.java

@@ -51,8 +51,10 @@ public class SubAccountStoreServiceImpl implements SubAccountStoreService {
             }
         }
         if(list.size()>0 || storeUser.getStoreId() != null){
+            //查出主子账号所有门店信息回显
             storeList = subAccountStoreMapper.selectSubAccountStoreListByUserIdTwo(userId);
         }else{
+            //主账号没有门店 子账号有门店 查出主账号 账号信息和子账号门店信息组合回显
             storeList = subAccountStoreMapper.selectSubAccountStoreListByUserId(userId);
         }
         log.info("查询子账号关联门店列表完成 - 用户ID: {}, 门店数量: {}", userId, storeList != null ? storeList.size() : 0);