Преглед на файлове

中台bug修复 返回值类型更新 分页查询 根据主账号id去找子账号id 主账号id和子账号id相同

wuchen преди 2 месеца
родител
ревизия
cac201d49d

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

@@ -36,4 +36,13 @@ public interface StoreUserMapper extends BaseMapper<StoreUser> {
     StoreUser getRemoveUser(@Param("id") String id);
 
     StoreUser getUserIncludeDeleted(@Param("id") Integer id);
+
+    /**
+     * 通过 store_user 与 store_platform_user_role 联表查询主账号下的子账号 id 列表(store_user.id)。
+     * 用于删除/禁用时判断主账号是否有关联子账号、主账号下子账号数量等。
+     *
+     * @param mainAccountId 主账号 id(store_user.id)
+     * @return 子账号的 store_user.id 列表
+     */
+    List<Integer> selectSubAccountUserIdsByMainAccountIdWithRole(@Param("mainAccountId") Integer mainAccountId);
 }

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

@@ -16,4 +16,14 @@
         FROM store_user
         where id = #{id}
     </select>
+
+    <!-- 通过 store_user 与 store_platform_user_role 联表查主账号下的子账号 id(store_user.id),用于删除/禁用时用子账号 id 判断 -->
+    <select id="selectSubAccountUserIdsByMainAccountIdWithRole" resultType="java.lang.Integer">
+        SELECT DISTINCT u.id
+        FROM store_user u
+        INNER JOIN store_platform_user_role r ON u.id = r.user_id AND r.delete_flag = 0
+        WHERE u.sub_account_id = #{mainAccountId}
+          AND u.account_type = 2
+          AND u.delete_flag = 0
+    </select>
 </mapper>

+ 17 - 2
alien-store/src/main/java/shop/alien/store/controller/StoreUserController.java

@@ -288,6 +288,13 @@ public class StoreUserController {
      */
     @ApiOperation("web端切换商家端用户状态(主账号有子账号时禁止禁用;返回更新后状态供前端及时展示)")
     @ApiOperationSupport(order = 7)
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "subAccountId", value = "子账号用户ID(store_user.id),子账号启用/禁用时必传", dataType = "int", paramType = "body"),
+            @ApiImplicitParam(name = "id", value = "主账号时为主账号ID;子账号时可传主账号id(与 parentAccountId 一致)", dataType = "int", paramType = "body"),
+            @ApiImplicitParam(name = "parentAccountId", value = "主账号ID(子账号启用/禁用时必传,与分页列表一致)", dataType = "int", paramType = "body"),
+            @ApiImplicitParam(name = "accountType", value = "账号类型:1-主账号,2-子账号", dataType = "int", paramType = "body"),
+            @ApiImplicitParam(name = "status", value = "当前状态:0-启用,1-禁用(用于切换,点击禁用传0,点击启用传1)", dataType = "int", paramType = "body")
+    })
     @PutMapping("/switchingStates")
     public R<StoreUserVo> switchingStates(@RequestBody StoreUserVo storeUser) {
         log.info("StoreUserController.switchingStates?storeUser={}", storeUser);
@@ -415,10 +422,18 @@ public class StoreUserController {
     }
 
     /**
-     * 手动删除商家账号及店铺
-     *  进行校验
+     * 手动删除商家账号及店铺。
+     * 主账号:有关联店铺或有关联子账号禁止删除。
+     * 子账号:按 userId(subAccountId)+storeId 逻辑删除角色;若主账号下仅 1 个子账号且非主账号则同时逻辑删除 store_user。
      */
     @ApiOperation("手动删除商家账号及店铺")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "subAccountId", value = "子账号用户ID(store_user.id)", dataType = "int", paramType = "body"),
+            @ApiImplicitParam(name = "id", value = "主账号时为主账号ID;子账号时可作主账号id(parentAccountId)时传 parentAccountId", dataType = "int", paramType = "body"),
+            @ApiImplicitParam(name = "parentAccountId", value = "主账号ID(子账号删除时必传,与分页列表一致)", dataType = "int", paramType = "body"),
+            @ApiImplicitParam(name = "accountType", value = "账号类型:1-主账号,2-子账号", dataType = "int", paramType = "body"),
+            @ApiImplicitParam(name = "phone", value = "子账号联系电话(可选,与 subAccountId 二选一)", dataType = "String", paramType = "body")
+    })
     @PostMapping("/deleteStoreAccountInfo")
     public R<String> deleteStoreAccountInfo(@RequestBody StoreUserVo storeUserVo) {
         log.info("StoreUserController.deleteStoreAccountInfo?phone={}, id={}", storeUserVo.getPhone(), storeUserVo.getSubAccountId());

+ 163 - 83
alien-store/src/main/java/shop/alien/store/service/impl/StoreUserServiceImpl.java

@@ -537,7 +537,7 @@ public class StoreUserServiceImpl extends ServiceImpl<StoreUserMapper, StoreUser
                 StoreUser mainAccount = role.getStoreId() == null ? null : mainAccountMap.get(role.getStoreId());
                 StoreUserVo storeUserVo = new StoreUserVo();
                 BeanUtils.copyProperties(subAccount, storeUserVo);
-                // 分页:返回主账号id(id/parentAccountId)及主账号对应的子账号联系方式id(subAccountId),禁用/删除时用 subAccountId
+                // 分页:返回主账号id(id/parentAccountId)及子账号id(subAccountId),禁用/删除时用 subAccountId;状态以 store_platform_user_role.status 为准,启用/禁用后分页再查即展示最新状态
                 Integer mainId = mainAccount != null ? mainAccount.getId() : subAccount.getSubAccountId();
                 storeUserVo.setId(mainId != null ? mainId : subAccount.getId());
                 storeUserVo.setSubAccountId(subAccount.getId());
@@ -631,73 +631,114 @@ public class StoreUserServiceImpl extends ServiceImpl<StoreUserMapper, StoreUser
 
 
     /**
-     * web端切换商家端用户状态(禁用/启用)。
-     * 禁用逻辑:1)主账号:有关联子账号则禁止禁用,无关联子账号可禁用;2)子账号:可禁用;
-     * 若既是主账号也是子账号且本次为禁用:只禁用子账号角色(store_platform_user_role),不更新 store_user,主账号保持启用;子账号在列表按 status 展示为禁用。
+     * 启用/禁用(参考 deleteStoreAccountInfo 数据格式)。
+     * 要求1:子账号禁用时,若该账号既是主账号也是子账号,只禁用子账号角色,不禁用主账号;分页查询时页面展示最新状态(以 store_platform_user_role.status 为准)。
+     * 要求2:子账号可以禁用。
+     * 要求3:主账号底下有关联子账号时不能禁用。
+     * 状态:0 启用/正常,1 禁用;接口返回 newStatus 便于前端及分页展示最新状态。
      */
     @Override
     public R<StoreUserVo> switchingStates(StoreUserVo storeUserParam) {
-        // 默认传 0 表示执行禁用(状态改为 1),分页列表展示为禁用;传 1 表示执行启用(状态改为 0),分页列表展示为启用
+        if (storeUserParam == null) {
+            throw new RuntimeException("参数不能为空");
+        }
+        // 当前状态 0=启用 1=禁用;点击禁用则 newStatus=1,点击启用则 newStatus=0
         int currentStatus = storeUserParam.getStatus() != null ? storeUserParam.getStatus() : 0;
         int newStatus = (currentStatus == 0) ? 1 : 0;
         boolean isMainAccount = storeUserParam.getAccountType() != null && storeUserParam.getAccountType() == 1;
 
-        if(storeUserParam.getAccountType()==2){
-            StoreUser storeUser = storeUserMapper.selectById(storeUserParam.getSubAccountId());
+        // ---------- 子账号:要求2 子账号可以禁用 ----------
+        if (storeUserParam.getAccountType() != null && storeUserParam.getAccountType() == 2) {
+            Integer subAccountId = storeUserParam.getSubAccountId();
+            if (subAccountId == null) {
+                throw new RuntimeException("子账号 id 不能为空");
+            }
+            StoreUser storeUser = storeUserMapper.selectById(subAccountId);
             if (storeUser == null) {
                 throw new RuntimeException("用户不存在");
             }
-            // 统计该用户在 platform 中未删除的角色数,用于判断是否「既是主账号也是子账号」
-            long roleCount = storePlatformUserRoleMapper.selectCount(
-                    new LambdaQueryWrapper<StorePlatformUserRole>()
-                            .eq(StorePlatformUserRole::getUserId, storeUser.getId())
-                            .eq(StorePlatformUserRole::getDeleteFlag, 0));
-            log.info("员工状态的status roleCount={}",roleCount);
-            boolean isBothMainAndSub = isMainAccount && roleCount > 0;
-            // 1 既是主账号也是子账号且本次为禁用:只禁用子账号角色,不更新 store_user,主账号保持启用;子账号在列表上按 role.status 展示为禁用
-            if (isBothMainAndSub) {
-                LambdaUpdateWrapper<StorePlatformUserRole> roleUpdateWrapper = new LambdaUpdateWrapper<>();
-                roleUpdateWrapper.eq(StorePlatformUserRole::getUserId, storeUser.getId())
-                        .eq(StorePlatformUserRole::getDeleteFlag, 0)
-                        .set(StorePlatformUserRole::getStatus, currentStatus);
-                storePlatformUserRoleMapper.update(null, roleUpdateWrapper);
-                StoreUserVo vo = new StoreUserVo();
-                vo.setId(storeUser.getId());
-                vo.setStatus(currentStatus);
-                vo.setSwitchStatus(newStatus == 0);
-                return R.data(vo);
+            Integer mainAccountId = storeUserParam.getParentAccountId() != null ? storeUserParam.getParentAccountId() : storeUser.getSubAccountId();
+            Integer storeId = null;
+            if (mainAccountId != null) {
+                StoreUser mainAccount = storeUserMapper.selectById(mainAccountId);
+                if (mainAccount != null) storeId = mainAccount.getStoreId();
+            }
+
+            // 要求1:若该账号既是主账号也是子账号,只禁用子账号角色,不禁用主账号(同一手机号存在主账号则只更新 role)
+            boolean isBothMainAndSub = false;
+            if (storeUser.getPhone() != null && !storeUser.getPhone().isEmpty()) {
+                long mainSamePhone = storeUserMapper.selectCount(
+                        new LambdaQueryWrapper<StoreUser>()
+                                .eq(StoreUser::getPhone, storeUser.getPhone())
+                                .eq(StoreUser::getAccountType, 1)
+                                .eq(StoreUser::getDeleteFlag, 0));
+                isBothMainAndSub = mainSamePhone > 0;
             }
+
             LambdaUpdateWrapper<StorePlatformUserRole> roleUpdateWrapper = new LambdaUpdateWrapper<>();
+            if (storeId != null) {
+                roleUpdateWrapper.eq(StorePlatformUserRole::getStoreId, storeId);
+            }
             roleUpdateWrapper.eq(StorePlatformUserRole::getUserId, storeUser.getId())
                     .eq(StorePlatformUserRole::getDeleteFlag, 0)
-                    .set(StorePlatformUserRole::getStatus, currentStatus);
+                    .set(StorePlatformUserRole::getStatus, newStatus);
             storePlatformUserRoleMapper.update(null, roleUpdateWrapper);
+
+            if (isBothMainAndSub) {
+                // 既是主账号也是子账号:只改了 role,不更新 store_user,主账号保持启用;分页列表以 role.status 展示为最新状态
+                StoreUserVo vo = new StoreUserVo();
+                vo.setId(mainAccountId != null ? mainAccountId : storeUser.getId());
+                vo.setSubAccountId(storeUser.getId());
+                vo.setParentAccountId(mainAccountId);
+                vo.setStatus(newStatus);
+                vo.setSwitchStatus(newStatus == 0);
+                return R.data(vo);
+            }
+
             baseRedisService.delete("store_" + storeUser.getPhone());
             LambdaUpdateWrapper<StoreUser> userUpdateWrapper = new LambdaUpdateWrapper<>();
             userUpdateWrapper.eq(StoreUser::getId, storeUser.getId()).set(StoreUser::getStatus, newStatus);
             storeUserMapper.update(null, userUpdateWrapper);
+            long enabledRoleCount = storePlatformUserRoleMapper.selectCount(
+                    new LambdaQueryWrapper<StorePlatformUserRole>()
+                            .eq(StorePlatformUserRole::getUserId, storeUser.getId())
+                            .eq(StorePlatformUserRole::getDeleteFlag, 0)
+                            .eq(StorePlatformUserRole::getStatus, 0));
+            if (enabledRoleCount == 0) {
+                baseRedisService.delete("store_" + storeUser.getPhone());
+                userUpdateWrapper.eq(StoreUser::getId, storeUser.getId()).set(StoreUser::getStatus, newStatus);
+                storeUserMapper.update(null, userUpdateWrapper);
+            }
             StoreUserVo vo = new StoreUserVo();
-            vo.setId(storeUser.getId());
+            vo.setId(mainAccountId != null ? mainAccountId : storeUser.getId());
+            vo.setSubAccountId(storeUser.getId());
+            vo.setParentAccountId(mainAccountId);
             vo.setStatus(newStatus);
             vo.setSwitchStatus(newStatus == 0);
             return R.data(vo);
-
         }
 
-        // 本次为禁用(newStatus==1):主账号有关联子账号则禁止禁用,无关联子账号可禁用
+        // ---------- 主账号:要求3 底下有关联子账号不能禁用 ----------
         if (newStatus == 1 && isMainAccount) {
-            List<StoreUser> subAccounts = getSubAccountsByMainAccountId(storeUserParam.getId());
-            if (subAccounts != null && !subAccounts.isEmpty()) {
+            List<Integer> subAccountIds = storeUserMapper.selectSubAccountUserIdsByMainAccountIdWithRole(storeUserParam.getId());
+            if (subAccountIds != null && !subAccountIds.isEmpty()) {
                 throw new RuntimeException("请先删除主账号下的子账号后再禁用");
             }
         }
-        // 启用/禁用:更新 store_user 的 status(禁用 0→1,启用 1→0),列表即时展示用返回的 vo
+
+        // 主账号启用/禁用:更新 store_user.status,禁用时删 token;返回 newStatus 便于分页展示最新状态
         LambdaUpdateWrapper<StoreUser> userUpdateWrapper = new LambdaUpdateWrapper<>();
-        userUpdateWrapper.eq(StoreUser::getId, storeUserParam.getId()).set(StoreUser::getStatus, currentStatus);
+        userUpdateWrapper.eq(StoreUser::getId, storeUserParam.getId()).set(StoreUser::getStatus, newStatus);
         storeUserMapper.update(null, userUpdateWrapper);
+        if (newStatus == 1) {
+            StoreUser u = storeUserMapper.selectById(storeUserParam.getId());
+            if (u != null && u.getPhone() != null) {
+                baseRedisService.delete("store_" + u.getPhone());
+            }
+        }
         StoreUserVo vo = new StoreUserVo();
         vo.setId(storeUserParam.getId());
-        vo.setStatus(currentStatus);
+        vo.setStatus(newStatus);
         vo.setSwitchStatus(newStatus == 0);
         return R.data(vo);
     }
@@ -1072,86 +1113,125 @@ public class StoreUserServiceImpl extends ServiceImpl<StoreUserMapper, StoreUser
     }
 
     /**
-     * 删除商家账号:主账号下有店铺或有关联子账号禁止删除;子账号则逻辑删除 store_platform_user_role,
-     * 若主账号下仅关联一个子账号则同时逻辑删除 store_platform_user_role 和 store_user,若多个子账号则仅逻辑删除 store_platform_user_role。
+     * 删除商家账号。
+     * 主账号:有关联店铺禁止删除,有关联子账号禁止删除。
+     * 子账号:参照 removeRole 逻辑,按 userId/storeId 逻辑删除 store_platform_user_role;若该主账号下仅 1 个子账号且非主账号则同时逻辑删除 store_user 并清 token。
      */
     @Override
     public String deleteStoreAccountInfo(StoreUserVo storeUserVo) {
-        // 解析用户:优先用子账号联系方式 phone 查;未传 phone 时用 id/subAccountId 查 store_user,后端从库中拿到子账号联系方式(phone)
+        if (storeUserVo == null) {
+            log.warn("deleteStoreAccountInfo 参数不能为空");
+            return "删除失败";
+        }
+        // 1. 解析用户:子账号 id(userId) 优先用 subAccountId,其次 id;或通过 phone 查
+        Integer userId = null;
         StoreUser storeUser = null;
-        if ((storeUserVo.getId() != null || storeUserVo.getSubAccountId() != null)) {
+        if (storeUserVo.getSubAccountId() != null || storeUserVo.getId() != null) {
             Integer idParam = storeUserVo.getSubAccountId() != null ? storeUserVo.getSubAccountId() : storeUserVo.getId();
             storeUser = storeUserMapper.selectOne(new LambdaQueryWrapper<StoreUser>()
                     .eq(StoreUser::getId, idParam).eq(StoreUser::getDeleteFlag, 0));
             if (storeUser != null) {
-                log.info("deleteStoreAccountInfo 通过 id 查到用户,后端拿到子账号联系方式: userId={}, phone={}", storeUser.getId(), storeUser.getPhone());
+                userId = storeUser.getId();
+                log.info("deleteStoreAccountInfo 通过子账号 id 查到用户: userId={}, phone={}", userId, storeUser.getPhone());
             }
         }
-        if (storeUserVo == null) {
-            log.warn("deleteStoreAccountInfo 用户不存在或已删除,请传子账号联系电话(phone)或用户id");
-            return "删除失败";
-        }
-        // 通过 phone 再尝试解析用户(前端可能只传 phone)
         if (storeUser == null && storeUserVo.getPhone() != null) {
             storeUser = storeUserMapper.selectOne(new LambdaQueryWrapper<StoreUser>()
                     .eq(StoreUser::getPhone, storeUserVo.getPhone()).eq(StoreUser::getDeleteFlag, 0));
+            if (storeUser != null) userId = storeUser.getId();
         }
-        if (storeUser == null) {
-            log.warn("deleteStoreAccountInfo 用户不存在或已删除,请传子账号联系电话(phone)或用户id");
+        if (userId == null || storeUser == null) {
+            log.warn("deleteStoreAccountInfo 用户不存在或已删除,请传 userId(subAccountId/id)或 phone");
             return "删除失败";
         }
-        Integer userId = storeUser.getId();
 
-        // 删除主账号校验:该主账号下有店铺禁止删除、该主账号下有子账号禁止删除(主账号列表返回的 id 即主账号 id)
+        // 2. 主账号删除校验:有关联店铺禁止删除,有关联子账号禁止删除
         if (storeUserVo.getAccountType() != null && storeUserVo.getAccountType() == 1) {
-            if (storeUser.getStoreId() != null) {
-                log.error("该主账号下存在店铺,禁止删除");
+            Integer mainIdForCheck = storeUserVo.getId();
+            if (mainIdForCheck == null) {
+                log.warn("deleteStoreAccountInfo 删除主账号时 id 不能为空");
+                return "删除失败";
+            }
+            StoreUser mainUser = storeUserMapper.selectById(mainIdForCheck);
+            if (mainUser != null && mainUser.getStoreId() != null && mainUser.getStoreId() > 0) {
+                log.error("该主账号下存在关联店铺,禁止删除");
                 return "当前账号下存在店铺 禁止删除";
             }
-            Integer mainIdForCheck = storeUserVo.getId();
-            if (mainIdForCheck != null) {
-                List<StoreUser> subAccounts = getSubAccountsByMainAccountId(mainIdForCheck);
-                if (subAccounts != null && !subAccounts.isEmpty()) {
-                    log.error("该主账号下存在关联子账号,禁止删除");
-                    return "当前账号下存在子账号禁止删除";
-                }
+            List<Integer> subAccountIds = storeUserMapper.selectSubAccountUserIdsByMainAccountIdWithRole(mainIdForCheck);
+            if (subAccountIds != null && !subAccountIds.isEmpty()) {
+                log.error("该主账号下存在关联子账号,禁止删除");
+                return "当前账号下存在子账号禁止删除";
             }
+            return null;
         }
 
-        // 主账号 id:优先用子账号 store_user.sub_account_id;无则用前端列表返回的 parentAccountId(与分页接口 records 字段一致)
+        // 3. 子账号删除:入参等价于 userId、主账号 id(mainAccountId)、主账号店铺 storeId;先查子账号 id 列表再判断
         Integer mainAccountId = storeUser.getSubAccountId() != null ? storeUser.getSubAccountId() : storeUserVo.getParentAccountId();
         if (mainAccountId == null) {
-            log.warn("deleteStoreAccountInfo 无法确定主账号,请传 parentAccountId 或保证子账号 store_user.sub_account_id 有值");
+            log.warn("deleteStoreAccountInfo 无法确定主账号,请传 parentAccountId 或保证子账号 sub_account_id 有值");
             return "删除失败";
         }
-        // 查询主账号下的子账号数量(按 store_user 统计:account_type=2 且 sub_account_id=主账号id)
-        List<StoreUser> subAccountsUnderMain = getSubAccountsByMainAccountId(mainAccountId);
-        int subAccountCount = subAccountsUnderMain != null ? subAccountsUnderMain.size() : 0;
-        log.info("主账号下子账号数量: mainAccountId={}, subAccountCount={}", mainAccountId, subAccountCount);
-
-        // 仅删除该主账号对应店铺下的角色:取主账号的 store_id,按 userId + storeId 逻辑删除 store_platform_user_role
-        StoreUser mainAccount = storeUserMapper.selectById(mainAccountId);
-        Integer storeId = mainAccount != null ? mainAccount.getStoreId() : null;
+
+        // 3.1 查询主账号下的子账号 id 列表,根据子账号 id 判断数量及是否包含当前 userId
+        List<Integer> subAccountIds = storeUserMapper.selectSubAccountUserIdsByMainAccountIdWithRole(mainAccountId);
+        if (subAccountIds == null) subAccountIds = Collections.emptyList();
+        int subAccountCount = subAccountIds.size();
+        log.info("deleteStoreAccountInfo 主账号下子账号数量: userId={}, mainAccountId={}, storeId={}, subAccountCount={}", userId, mainAccountId, subAccountCount);
+        if (!subAccountIds.contains(userId)) {
+            log.warn("deleteStoreAccountInfo 当前用户不在主账号子账号列表中: userId={}, mainAccountId={}", userId, mainAccountId);
+            return "删除失败";
+        }
+
+        // 3.2 逻辑删除 store_platform_user_role(按 userId + storeId,delete_flag=1)
         LambdaUpdateWrapper<StorePlatformUserRole> roleUpdateWrapper = new LambdaUpdateWrapper<>();
-        roleUpdateWrapper.eq(StorePlatformUserRole::getUserId, userId).eq(StorePlatformUserRole::getDeleteFlag, 0);
-        if (storeId != null) {
-            roleUpdateWrapper.eq(StorePlatformUserRole::getStoreId, storeId);
+        roleUpdateWrapper.eq(StorePlatformUserRole::getUserId, userId)
+                .eq(StorePlatformUserRole::getDeleteFlag, 0)
+                .set(StorePlatformUserRole::getDeleteFlag, 1);
+        int roleUpdateResult = storePlatformUserRoleMapper.update(null, roleUpdateWrapper);
+        if (roleUpdateResult <= 0) {
+            log.error("deleteStoreAccountInfo 逻辑删除 store_platform_user_role 失败: userId={}", userId);
+            return "删除失败";
         }
-        roleUpdateWrapper.set(StorePlatformUserRole::getDeleteFlag, 1);
-        storePlatformUserRoleMapper.update(null, roleUpdateWrapper);
 
-        // 主账号下子账号唯一:逻辑删除 store_platform_user_role 和 store_user;不唯一:只逻辑删除 store_platform_user_role
+        // 3.3 根据子账号 id 查询用户信息及联系方式,删除 Redis token(key: store_手机号),参照 removeRole
+        StoreUser subUser = storeUserMapper.selectById(userId);
+        if (subUser != null && subUser.getPhone() != null) {
+            String tokenKey = "store_" + subUser.getPhone();
+            try {
+                String existingToken = baseRedisService.getString(tokenKey);
+                if (existingToken != null) {
+                    baseRedisService.delete(tokenKey);
+                    log.info("deleteStoreAccountInfo 清除用户 token 成功: userId={}, phone={}, tokenKey={}", userId, subUser.getPhone(), tokenKey);
+                } else {
+                    log.warn("deleteStoreAccountInfo 用户 token 不存在或已过期: userId={}, phone={}", userId, subUser.getPhone());
+                }
+            } catch (Exception e) {
+                log.warn("deleteStoreAccountInfo 清除 token 异常: userId={}, tokenKey={}", userId, tokenKey, e);
+            }
+        }
+
+        // 3.4 若该主账号下仅 1 个子账号,则进一步判断是否为主账号,再决定是否同时逻辑删除 store_user(参照 removeRole)
         if (subAccountCount == 1) {
-            LambdaUpdateWrapper<StoreUser> userUpdateWrapper = new LambdaUpdateWrapper<>();
-            userUpdateWrapper.eq(StoreUser::getId, userId)
-                    .eq(StoreUser::getDeleteFlag, 0)
-                    .set(StoreUser::getDeleteFlag, 1);
-            storeUserMapper.update(null, userUpdateWrapper);
-            log.info("主账号下子账号唯一,已逻辑删除 store_platform_user_role 和 store_user: userId={}", userId);
-            String tokenKey = "store_" + storeUser.getPhone();
-            baseRedisService.delete(tokenKey);
+            if (subUser != null) {
+                if (subUser.getStoreId() != null && subUser.getStoreId() > 0) {
+                    log.info("deleteStoreAccountInfo 用户是主账号,不删除 store_user 记录: userId={}, storeId={}", userId, subUser.getStoreId());
+                } else {
+                    LambdaUpdateWrapper<StoreUser> userUpdateWrapper = new LambdaUpdateWrapper<>();
+                    userUpdateWrapper.eq(StoreUser::getId, userId)
+                            .eq(StoreUser::getDeleteFlag, 0)
+                            .set(StoreUser::getDeleteFlag, 1);
+                    int userUpdateResult = storeUserMapper.update(null, userUpdateWrapper);
+                    if (userUpdateResult > 0) {
+                        log.info("deleteStoreAccountInfo 用户只有一个子账号且不是主账号,已同时逻辑删除 store_user 记录: userId={}", userId);
+                    } else {
+                        log.warn("deleteStoreAccountInfo 逻辑删除 store_user 记录失败或记录不存在: userId={}", userId);
+                    }
+                }
+            } else {
+                log.warn("deleteStoreAccountInfo 未找到用户信息: userId={}", userId);
+            }
         } else {
-            log.info("主账号下子账号数不为1,仅逻辑删除 store_platform_user_role,不删 store_user: mainAccountId={}, subAccountCount={}", mainAccountId, subAccountCount);
+            log.info("deleteStoreAccountInfo 用户有多个子账号(count={}),仅删除 store_platform_user_role 记录,不删除 store_user 记录: userId={}", subAccountCount, userId);
         }
         return null;
     }