Explorar o código

商家pc端 分权限 第十二笔提交

liudongzhi hai 2 meses
pai
achega
c03086a6d1

+ 165 - 20
alien-store-platform/src/main/java/shop/alien/storeplatform/service/impl/StorePlatformUserRoleServiceImpl.java

@@ -91,6 +91,61 @@ public class StorePlatformUserRoleServiceImpl extends ServiceImpl<StorePlatformU
         return storePlatformUserRoleMapper.delete(queryWrapper) >= 0;
     }
 
+//    @Override
+//    public boolean removeRole(Integer userId, Long roleId, Integer storeId) {
+//        if (userId == null || roleId == null || storeId == null) {
+//            log.error("参数不能为空: userId={}, roleId={}, storeId={}", userId, roleId, storeId);
+//            return false;
+//        }
+//
+//        try {
+//            // 1. 先查询该用户在所有店铺下的子账号数量(未删除的)
+//            LambdaQueryWrapper<StorePlatformUserRole> countWrapper = new LambdaQueryWrapper<>();
+//            countWrapper.eq(StorePlatformUserRole::getUserId, userId)
+//                    .eq(StorePlatformUserRole::getDeleteFlag, 0);
+//            long subAccountCount = storePlatformUserRoleMapper.selectCount(countWrapper);
+//
+//            log.info("用户在所有店铺下的子账号数量: userId={}, count={}", userId, subAccountCount);
+//
+//            // 2. 逻辑删除 store_platform_user_role 表的记录
+//            LambdaUpdateWrapper<StorePlatformUserRole> updateWrapper = new LambdaUpdateWrapper<>();
+//            updateWrapper.eq(StorePlatformUserRole::getUserId, userId)
+//                    .eq(StorePlatformUserRole::getRoleId, roleId)
+//                    .eq(StorePlatformUserRole::getStoreId, storeId)
+//                    .eq(StorePlatformUserRole::getDeleteFlag, 0)
+//                    .set(StorePlatformUserRole::getDeleteFlag, 1); // 1代表删除
+//            int updateResult = storePlatformUserRoleMapper.update(null, updateWrapper);
+//
+//            if (updateResult <= 0) {
+//                log.error("逻辑删除 store_platform_user_role 记录失败: userId={}, roleId={}, storeId={}", userId, roleId, storeId);
+//                return false;
+//            }
+//
+//            // 3. 如果只是一个账号的子账号(删除前只有1个),则同时逻辑删除 store_user 表
+//            if (subAccountCount == 1) {
+//                LambdaUpdateWrapper<StoreUser> userUpdateWrapper = new LambdaUpdateWrapper<>();
+//                userUpdateWrapper.eq(StoreUser::getId, userId)
+//                        .eq(StoreUser::getDeleteFlag, 0)
+//                        .set(StoreUser::getDeleteFlag, 1); // 1代表删除
+//                int userUpdateResult = storeUserMapper.update(null, userUpdateWrapper);
+//
+//                if (userUpdateResult > 0) {
+//                    log.info("用户只有一个子账号,已同时逻辑删除 store_user 记录: userId={}", userId);
+//                } else {
+//                    log.warn("逻辑删除 store_user 记录失败或记录不存在: userId={}", userId);
+//                }
+//            } else {
+//                log.info("用户有多个子账号(count={}),仅删除 store_platform_user_role 记录,不删除 store_user 记录: userId={}", subAccountCount, userId);
+//            }
+//
+//            return true;
+//        } catch (Exception e) {
+//            log.error("移除用户角色失败: userId={}, roleId={}, storeId={}", userId, roleId, storeId, e);
+//            return false;
+//        }
+//    }
+
+
     @Override
     public boolean removeRole(Integer userId, Long roleId, Integer storeId) {
         if (userId == null || roleId == null || storeId == null) {
@@ -104,7 +159,7 @@ public class StorePlatformUserRoleServiceImpl extends ServiceImpl<StorePlatformU
             countWrapper.eq(StorePlatformUserRole::getUserId, userId)
                     .eq(StorePlatformUserRole::getDeleteFlag, 0);
             long subAccountCount = storePlatformUserRoleMapper.selectCount(countWrapper);
-            
+
             log.info("用户在所有店铺下的子账号数量: userId={}, count={}", userId, subAccountCount);
 
             // 2. 逻辑删除 store_platform_user_role 表的记录
@@ -115,24 +170,36 @@ public class StorePlatformUserRoleServiceImpl extends ServiceImpl<StorePlatformU
                     .eq(StorePlatformUserRole::getDeleteFlag, 0)
                     .set(StorePlatformUserRole::getDeleteFlag, 1); // 1代表删除
             int updateResult = storePlatformUserRoleMapper.update(null, updateWrapper);
-            
+
             if (updateResult <= 0) {
                 log.error("逻辑删除 store_platform_user_role 记录失败: userId={}, roleId={}, storeId={}", userId, roleId, storeId);
                 return false;
             }
 
-            // 3. 如果只是一个账号的子账号(删除前只有1个),则同时逻辑删除 store_user 表
+            // 3. 如果只是一个账号的子账号(删除前只有1个),则需要进一步判断是否为主账号,再决定是否同时逻辑删除 store_user 表
             if (subAccountCount == 1) {
-                LambdaUpdateWrapper<StoreUser> userUpdateWrapper = new LambdaUpdateWrapper<>();
-                userUpdateWrapper.eq(StoreUser::getId, userId)
-                        .eq(StoreUser::getDeleteFlag, 0)
-                        .set(StoreUser::getDeleteFlag, 1); // 1代表删除
-                int userUpdateResult = storeUserMapper.update(null, userUpdateWrapper);
-                
-                if (userUpdateResult > 0) {
-                    log.info("用户只有一个子账号,已同时逻辑删除 store_user 记录: userId={}", userId);
+                // 查询用户信息以判断是否为主账号
+                StoreUser user = storeUserMapper.selectById(userId);
+                if (user != null) {
+                    // 如果用户有 storeId 字段值,说明是主账号,不应删除
+                    if (user.getStoreId() != null && user.getStoreId() > 0) {
+                        log.info("用户是主账号,不删除 store_user 记录: userId={}, storeId={}", userId, user.getStoreId());
+                    } else {
+                        // 不是主账号,可以安全删除
+                        LambdaUpdateWrapper<StoreUser> userUpdateWrapper = new LambdaUpdateWrapper<>();
+                        userUpdateWrapper.eq(StoreUser::getId, userId)
+                                .eq(StoreUser::getDeleteFlag, 0)
+                                .set(StoreUser::getDeleteFlag, 1); // 1代表删除
+                        int userUpdateResult = storeUserMapper.update(null, userUpdateWrapper);
+
+                        if (userUpdateResult > 0) {
+                            log.info("用户只有一个子账号且不是主账号,已同时逻辑删除 store_user 记录: userId={}", userId);
+                        } else {
+                            log.warn("逻辑删除 store_user 记录失败或记录不存在: userId={}", userId);
+                        }
+                    }
                 } else {
-                    log.warn("逻辑删除 store_user 记录失败或记录不存在: userId={}", userId);
+                    log.warn("未找到用户信息: userId={}", userId);
                 }
             } else {
                 log.info("用户有多个子账号(count={}),仅删除 store_platform_user_role 记录,不删除 store_user 记录: userId={}", subAccountCount, userId);
@@ -145,6 +212,8 @@ public class StorePlatformUserRoleServiceImpl extends ServiceImpl<StorePlatformU
         }
     }
 
+
+
     @Override
     public boolean createAccountAndAssignRole(String phone, String accountName, Integer storeId, Long roleId) {
         if (phone == null || phone.isEmpty() || storeId == null || roleId == null) {
@@ -377,6 +446,34 @@ public class StorePlatformUserRoleServiceImpl extends ServiceImpl<StorePlatformU
         }
     }
 
+//    @Override
+//    public boolean batchDeleteSubAccounts(List<Integer> userIds, Integer storeId) {
+//        if (userIds == null || userIds.isEmpty()) {
+//            log.error("用户ID列表不能为空");
+//            return false;
+//        }
+//        if (storeId == null) {
+//            log.error("店铺ID不能为空");
+//            return false;
+//        }
+//
+//        try {
+//            // 批量逻辑删除用户角色关联关系
+//            LambdaUpdateWrapper<StorePlatformUserRole> updateWrapper = new LambdaUpdateWrapper<>();
+//            updateWrapper.eq(StorePlatformUserRole::getStoreId, storeId)
+//                    .in(StorePlatformUserRole::getUserId, userIds)
+//                    .set(StorePlatformUserRole::getDeleteFlag, 1); // 1代表删除
+//
+//            int result = storePlatformUserRoleMapper.update(null, updateWrapper);
+//            log.info("批量删除子账号成功: storeId={}, userIds={}, 删除数量={}", storeId, userIds, result);
+//            return result > 0;
+//        } catch (Exception e) {
+//            log.error("批量删除子账号失败: storeId={}, userIds={}", storeId, userIds, e);
+//            return false;
+//        }
+//    }
+
+
     @Override
     public boolean batchDeleteSubAccounts(List<Integer> userIds, Integer storeId) {
         if (userIds == null || userIds.isEmpty()) {
@@ -389,15 +486,63 @@ public class StorePlatformUserRoleServiceImpl extends ServiceImpl<StorePlatformU
         }
 
         try {
-            // 批量逻辑删除用户角色关联关系
-            LambdaUpdateWrapper<StorePlatformUserRole> updateWrapper = new LambdaUpdateWrapper<>();
-            updateWrapper.eq(StorePlatformUserRole::getStoreId, storeId)
-                    .in(StorePlatformUserRole::getUserId, userIds)
-                    .set(StorePlatformUserRole::getDeleteFlag, 1); // 1代表删除
+            for (Integer userId : userIds) {
+                // 1. 先查询该用户在所有店铺下的子账号数量(未删除的)
+                LambdaQueryWrapper<StorePlatformUserRole> countWrapper = new LambdaQueryWrapper<>();
+                countWrapper.eq(StorePlatformUserRole::getUserId, userId)
+                        .eq(StorePlatformUserRole::getDeleteFlag, 0);
+                long subAccountCount = storePlatformUserRoleMapper.selectCount(countWrapper);
+
+                log.info("用户在所有店铺下的子账号数量: userId={}, count={}", userId, subAccountCount);
+
+                // 2. 逻辑删除 store_platform_user_role 表的记录(只删除当前店铺的记录)
+                LambdaUpdateWrapper<StorePlatformUserRole> updateWrapper = new LambdaUpdateWrapper<>();
+                updateWrapper.eq(StorePlatformUserRole::getUserId, userId)
+                        .eq(StorePlatformUserRole::getStoreId, storeId)
+                        .eq(StorePlatformUserRole::getDeleteFlag, 0)
+                        .set(StorePlatformUserRole::getDeleteFlag, 1); // 1代表删除
 
-            int result = storePlatformUserRoleMapper.update(null, updateWrapper);
-            log.info("批量删除子账号成功: storeId={}, userIds={}, 删除数量={}", storeId, userIds, result);
-            return result > 0;
+                int result = storePlatformUserRoleMapper.update(null, updateWrapper);
+
+                if (result <= 0) {
+                    log.warn("逻辑删除 store_platform_user_role 记录失败: userId={}, storeId={}", userId, storeId);
+                    continue; // 继续处理下一个用户
+                }
+
+                log.info("逻辑删除 store_platform_user_role 记录成功: userId={}, storeId={}, 删除数量={}", userId, storeId, result);
+
+                // 3. 判断是否只有一个子账号,如果是则同时逻辑删除 store_user 表
+                if (subAccountCount == 1) {
+
+                    StoreUser user = storeUserMapper.selectById(userId);
+
+                    if (user != null) {
+                        // 如果用户有 storeId 字段值,说明是主账号,不应删除
+                        if (user.getStoreId() != null && user.getStoreId() > 0) {
+                            log.info("用户是主账号,不删除 store_user 记录: userId={}, storeId={}", userId, user.getStoreId());
+                        } else {
+                            // 不是主账号,可以安全删除
+                            LambdaUpdateWrapper<StoreUser> userUpdateWrapper = new LambdaUpdateWrapper<>();
+                            userUpdateWrapper.eq(StoreUser::getId, userId)
+                                    .eq(StoreUser::getDeleteFlag, 0)
+                                    .set(StoreUser::getDeleteFlag, 1); // 1代表删除
+                            int userUpdateResult = storeUserMapper.update(null, userUpdateWrapper);
+
+                            if (userUpdateResult > 0) {
+                                log.info("用户只有一个子账号且不是主账号,已同时逻辑删除 store_user 记录: userId={}", userId);
+                            } else {
+                                log.warn("逻辑删除 store_user 记录失败或记录不存在: userId={}", userId);
+                            }
+                        }
+                    } else {
+                        log.warn("未找到用户信息: userId={}", userId);
+                    }
+                } else {
+                    log.info("用户有多个子账号(count={}),仅删除 store_platform_user_role 记录,不删除 store_user 记录: userId={}", subAccountCount, userId);
+                }
+            }
+            log.info("批量删除子账号完成: storeId={}, userIds={}", storeId, userIds);
+            return true;
         } catch (Exception e) {
             log.error("批量删除子账号失败: storeId={}, userIds={}", storeId, userIds, e);
             return false;