Ver código fonte

增加 子账号关联注销逻辑

liudongzhi 11 horas atrás
pai
commit
5527b668c3

+ 130 - 0
alien-store/src/main/java/shop/alien/store/service/impl/StoreUserServiceImpl.java

@@ -741,6 +741,132 @@ public class StoreUserServiceImpl extends ServiceImpl<StoreUserMapper, StoreUser
         return list == null ? Collections.emptyList() : list.stream().map(StorePlatformUserRole::getUserId).distinct().collect(Collectors.toList());
     }
 
+
+    /**
+     * 主账号注销时:解除该店铺下所有子账号绑定;若子账号仅绑定此店铺且非主账号,则逻辑删除 store_user。
+     * 逻辑参照 {@code StorePlatformUserRoleServiceImpl#batchDeleteSubAccounts}。
+     */
+    private void unbindSubAccountsOnMainAccountLogout(Integer storeId, Integer mainAccountUserId) {
+        if (storeId == null) {
+            return;
+        }
+        List<Integer> subAccountUserIds = getSubAccountUserIdsByStoreId(storeId, mainAccountUserId);
+        if (CollectionUtils.isEmpty(subAccountUserIds)) {
+            return;
+        }
+        log.info("主账号注销,解除店铺子账号绑定: storeId={}, mainAccountUserId={}, subAccountCount={}",
+                storeId, mainAccountUserId, subAccountUserIds.size());
+        for (Integer userId : subAccountUserIds) {
+            unbindSubAccountFromStore(userId, storeId);
+        }
+    }
+
+    /**
+     * 注销时解除当前账号作为其他店铺子账号的绑定(仅清除 store_platform_user_role,不删除 store_user)。
+     */
+    private void unbindSelfAsSubAccountOnOtherStores(StoreUser storeUser) {
+        if (storeUser == null || storeUser.getId() == null) {
+            return;
+        }
+        LambdaQueryWrapper<StorePlatformUserRole> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.eq(StorePlatformUserRole::getUserId, storeUser.getId())
+                .eq(StorePlatformUserRole::getDeleteFlag, 0);
+        if (storeUser.getStoreId() != null) {
+            queryWrapper.ne(StorePlatformUserRole::getStoreId, storeUser.getStoreId());
+        }
+        List<StorePlatformUserRole> roles = storePlatformUserRoleMapper.selectList(queryWrapper);
+        if (CollectionUtils.isEmpty(roles)) {
+            return;
+        }
+        Set<Integer> otherStoreIds = roles.stream()
+                .map(StorePlatformUserRole::getStoreId)
+                .filter(Objects::nonNull)
+                .collect(Collectors.toCollection(LinkedHashSet::new));
+        log.info("注销账号,解除自身在其他店铺的子账号绑定: userId={}, storeIds={}", storeUser.getId(), otherStoreIds);
+        for (Integer storeId : otherStoreIds) {
+            LambdaUpdateWrapper<StorePlatformUserRole> updateWrapper = new LambdaUpdateWrapper<>();
+            updateWrapper.eq(StorePlatformUserRole::getUserId, storeUser.getId())
+                    .eq(StorePlatformUserRole::getStoreId, storeId)
+                    .eq(StorePlatformUserRole::getDeleteFlag, 0)
+                    .set(StorePlatformUserRole::getDeleteFlag, 1);
+            int updated = storePlatformUserRoleMapper.update(null, updateWrapper);
+            if (updated > 0) {
+                log.info("已解除自身子账号绑定: userId={}, storeId={}, count={}", storeUser.getId(), storeId, updated);
+            }
+        }
+    }
+
+    /**
+     * 解除单个子账号与指定店铺的绑定;仅绑定一个店铺且非主账号时逻辑删除 store_user。
+     */
+    private void unbindSubAccountFromStore(Integer userId, Integer storeId) {
+        if (userId == null || storeId == null) {
+            return;
+        }
+        long subAccountCount = countActiveSubAccountRelations(userId);
+
+        LambdaUpdateWrapper<StorePlatformUserRole> roleUpdateWrapper = new LambdaUpdateWrapper<>();
+        roleUpdateWrapper.eq(StorePlatformUserRole::getUserId, userId)
+                .eq(StorePlatformUserRole::getStoreId, storeId)
+                .eq(StorePlatformUserRole::getDeleteFlag, 0)
+                .set(StorePlatformUserRole::getDeleteFlag, 1);
+        int roleUpdateResult = storePlatformUserRoleMapper.update(null, roleUpdateWrapper);
+        if (roleUpdateResult <= 0) {
+            log.warn("主账号注销解除子账号绑定失败: userId={}, storeId={}", userId, storeId);
+            return;
+        }
+
+        StoreUser subUser = storeUserMapper.selectById(userId);
+        clearStoreUserToken(subUser);
+
+        if (subAccountCount == 1) {
+            if (isStoreMainAccount(subUser)) {
+
+
+                log.info("子账号同时是主账号,仅解除绑定不删除 store_user: userId={}, storeId={}", userId, storeId);
+            } 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("子账号仅绑定一个店铺,已逻辑删除 store_user: userId={}, storeId={}", userId, storeId);
+                } else {
+                    log.warn("子账号逻辑删除 store_user 失败: userId={}", userId);
+                }
+            }
+        } else {
+            log.info("子账号绑定多个店铺,仅解除当前店铺绑定: userId={}, storeId={}, totalBindings={}",
+                    userId, storeId, subAccountCount);
+        }
+    }
+
+    private boolean isStoreMainAccount(StoreUser user) {
+        if (user == null) {
+            return false;
+        }
+        if (user.getStoreId() != null && user.getStoreId() > 0) {
+            return true;
+        }
+        return user.getAccountType() != null && user.getAccountType() == 1;
+    }
+
+    private void clearStoreUserToken(StoreUser storeUser) {
+        if (storeUser == null || storeUser.getPhone() == null) {
+            return;
+        }
+        try {
+            String tokenKey = "store_" + storeUser.getPhone();
+            if (baseRedisService.getString(tokenKey) != null) {
+                baseRedisService.delete(tokenKey);
+                log.info("清除子账号 token: userId={}, phone={}", storeUser.getId(), storeUser.getPhone());
+            }
+        } catch (Exception e) {
+            log.warn("清除子账号 token 异常: userId={}, msg={}", storeUser.getId(), e.getMessage());
+        }
+    }
+
     private List<Integer> getSubAccountUserIds(Integer excludeUserId) {
         if (excludeUserId == null) return Collections.emptyList();
         LambdaQueryWrapper<StorePlatformUserRole> w = new LambdaQueryWrapper<>();
@@ -1160,6 +1286,10 @@ public class StoreUserServiceImpl extends ServiceImpl<StoreUserMapper, StoreUser
             if (num > 0) {
                 storeLogoutSyncService.applyCoolingForStoreUser(
                         storeUser, storeUserVo.getLogoutReason(), storeUserVo.getLogoutCode());
+                if (storeUser.getStoreId() != null) {
+                    unbindSubAccountsOnMainAccountLogout(storeUser.getStoreId(), storeUser.getId());
+                }
+                unbindSelfAsSubAccountOnOtherStores(storeUser);
                 // 发送通知
                 LifeNotice lifeMessage = new LifeNotice();
                 lifeMessage.setReceiverId("store_" + storeUser.getPhone());