瀏覽代碼

子账号管理中台bug修复

wuchen 2 月之前
父節點
當前提交
8e7fca9150
共有 1 個文件被更改,包括 25 次插入23 次删除
  1. 25 23
      alien-store/src/main/java/shop/alien/store/service/impl/StoreUserServiceImpl.java

+ 25 - 23
alien-store/src/main/java/shop/alien/store/service/impl/StoreUserServiceImpl.java

@@ -561,7 +561,6 @@ public class StoreUserServiceImpl extends ServiceImpl<StoreUserMapper, StoreUser
         return list == null ? Collections.emptyList() : list.stream().map(StorePlatformUserRole::getUserId).distinct().collect(Collectors.toList());
     }
 
-
     @Override
     public R<StoreUserVo> editStoreUser(StoreUser storeUser) {
         storeUserMapper.updateById(storeUser);
@@ -600,40 +599,33 @@ public class StoreUserServiceImpl extends ServiceImpl<StoreUserMapper, StoreUser
                     throw new RuntimeException("请先删除主账号下的子账号后再禁用");
                 }
             }
-            // 禁用时删除 token
-            baseRedisService.delete("store_" + storeUser.getPhone());
         }
-        // 根据当前状态切另一个状态
         int newStatus = storeUser.getStatus() == 0 ? 1 : 0;
-        if(storeUser.getAccountType() != null && storeUser.getAccountType() == 1){
-            LambdaUpdateWrapper<StoreUser> queryWrapper = new LambdaUpdateWrapper<>();
-            queryWrapper.eq(StoreUser::getId, storeUser.getId())
-                    .set(StoreUser::getStatus, newStatus);
-            int updateCount = storeUserMapper.update(null, queryWrapper);
-            if (updateCount <= 0) {
-                throw new RuntimeException("更新失败");
-            }
+        // 是否既是主账号又在 store_platform_user_role 中有记录(即也是子账号)
+        long roleCount = storePlatformUserRoleMapper.selectCount(new LambdaQueryWrapper<StorePlatformUserRole>().eq(StorePlatformUserRole::getUserId, storeUser.getId()));
+        boolean isBothMainAndSub = (storeUser.getAccountType() != null && storeUser.getAccountType() == 1) && roleCount > 0;
+        if (isBothMainAndSub && newStatus == 1) {
+            // 既是主账号也是子账号且本次为禁用:只禁用子账号角色,不更新 store_user,主账号保持启用
+            LambdaUpdateWrapper<StorePlatformUserRole> roleUpdateWrapper = new LambdaUpdateWrapper<>();
+            roleUpdateWrapper.eq(StorePlatformUserRole::getUserId, storeUser.getId()).set(StorePlatformUserRole::getStatus, 1);
+            storePlatformUserRoleMapper.update(null, roleUpdateWrapper);
             StoreUserVo vo = new StoreUserVo();
             vo.setId(storeUser.getId());
-            vo.setStatus(newStatus);
-            vo.setSwitchStatus(newStatus == 0);
+            vo.setStatus(1);
+            vo.setSwitchStatus(false);
             return R.data(vo);
         }
+        if (newStatus == 1) {
+            baseRedisService.delete("store_" + storeUser.getPhone());
+        }
         LambdaUpdateWrapper<StoreUser> queryWrapper = new LambdaUpdateWrapper<>();
-        queryWrapper.eq(StoreUser::getId, storeUser.getId())
-                .set(StoreUser::getStatus, newStatus);
+        queryWrapper.eq(StoreUser::getId, storeUser.getId()).set(StoreUser::getStatus, newStatus);
         int updateCount = storeUserMapper.update(null, queryWrapper);
         if (updateCount <= 0) {
             throw new RuntimeException("更新失败");
         }
-//        StoreUserVo vo = new StoreUserVo();
-//        vo.setId(storeUser.getId());
-//        vo.setStatus(newStatus);
-//        vo.setSwitchStatus(newStatus == 0);
-        // 子账号启用/禁用时同步更新 store_platform_user_role 的 status(0 启用,1 禁用)
         LambdaUpdateWrapper<StorePlatformUserRole> roleUpdateWrapper = new LambdaUpdateWrapper<>();
-        roleUpdateWrapper.eq(StorePlatformUserRole::getUserId, storeUser.getId())
-                .set(StorePlatformUserRole::getStatus, newStatus);
+        roleUpdateWrapper.eq(StorePlatformUserRole::getUserId, storeUser.getId()).set(StorePlatformUserRole::getStatus, newStatus);
         storePlatformUserRoleMapper.update(null, roleUpdateWrapper);
         // 返回更新后的状态,供前端及时展示
         StoreUserVo vo = new StoreUserVo();
@@ -643,6 +635,16 @@ public class StoreUserServiceImpl extends ServiceImpl<StoreUserMapper, StoreUser
         return R.data(vo);
     }
 
+    /**
+     *  分页查询
+     * @param
+     * @param
+     * @param id
+     * @param phone
+     * @param status
+     * @param accountType
+     * @return
+     */
     @Override
     public String exportExcel(String id, String phone, String status, Integer accountType) throws IOException {
         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");