diff --git a/src/main/java/com/gxwebsoft/cms/controller/CmsNavigationController.java b/src/main/java/com/gxwebsoft/cms/controller/CmsNavigationController.java index 74ccb10..33c0841 100644 --- a/src/main/java/com/gxwebsoft/cms/controller/CmsNavigationController.java +++ b/src/main/java/com/gxwebsoft/cms/controller/CmsNavigationController.java @@ -121,6 +121,24 @@ public class CmsNavigationController extends BaseController { public ApiResult update(@RequestBody CmsNavigation cmsNavigation) { // 单页模型绑定唯一性校验:同一租户下同一个单页只能绑定一个栏目 checkPageBindingUnique(cmsNavigation); + // 顶级栏目(parentId=0)的显示位置(top/bottom)发生变更时, + // 递归同步所有子级(含孙级)的 top/bottom,保持层级一致。 + if (cmsNavigation.getParentId() != null && cmsNavigation.getParentId() == 0 + && cmsNavigation.getNavigationId() != null + && (cmsNavigation.getTop() != null || cmsNavigation.getBottom() != null)) { + CmsNavigation oldNav = cmsNavigationService.getById(cmsNavigation.getNavigationId()); + if (oldNav != null) { + boolean topChanged = cmsNavigation.getTop() != null && !cmsNavigation.getTop().equals(oldNav.getTop()); + boolean bottomChanged = cmsNavigation.getBottom() != null && !cmsNavigation.getBottom().equals(oldNav.getBottom()); + if (topChanged || bottomChanged) { + cmsNavigationService.syncChildrenPosition( + cmsNavigation.getNavigationId(), + cmsNavigation.getTop() != null ? cmsNavigation.getTop() : oldNav.getTop(), + cmsNavigation.getBottom() != null ? cmsNavigation.getBottom() : oldNav.getBottom(), + getTenantId()); + } + } + } if (cmsNavigationService.updateById(cmsNavigation)) { // 修改成功事务处理 cmsNavigationService.saveAsync(cmsNavigation); diff --git a/src/main/java/com/gxwebsoft/cms/service/CmsNavigationService.java b/src/main/java/com/gxwebsoft/cms/service/CmsNavigationService.java index cb867f6..6c45ece 100644 --- a/src/main/java/com/gxwebsoft/cms/service/CmsNavigationService.java +++ b/src/main/java/com/gxwebsoft/cms/service/CmsNavigationService.java @@ -42,4 +42,15 @@ public interface CmsNavigationService extends IService { void saveAsync(CmsNavigation cmsNavigation); CmsNavigation getByIdRelByCodeRel(String code); + + /** + * 递归同步顶级栏目所有子级(含孙级)的显示位置(top/bottom)。 + * 仅当顶级栏目位置发生变更时调用;只更新 top/bottom 字段,不触发 saveAsync。 + * + * @param navigationId 顶级栏目ID + * @param top 顶部显示标记(1/0) + * @param bottom 底部显示标记(1/0) + * @param tenantId 租户ID + */ + void syncChildrenPosition(Integer navigationId, Integer top, Integer bottom, Integer tenantId); } diff --git a/src/main/java/com/gxwebsoft/cms/service/impl/CmsNavigationServiceImpl.java b/src/main/java/com/gxwebsoft/cms/service/impl/CmsNavigationServiceImpl.java index 2c13417..6370949 100644 --- a/src/main/java/com/gxwebsoft/cms/service/impl/CmsNavigationServiceImpl.java +++ b/src/main/java/com/gxwebsoft/cms/service/impl/CmsNavigationServiceImpl.java @@ -3,6 +3,7 @@ package com.gxwebsoft.cms.service.impl; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.gxwebsoft.cms.entity.CmsDesign; import com.gxwebsoft.cms.entity.CmsModel; @@ -23,6 +24,7 @@ import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.text.MessageFormat; +import java.util.ArrayList; import java.util.List; /** @@ -178,6 +180,40 @@ public class CmsNavigationServiceImpl extends ServiceImpl descendantIds = new ArrayList<>(); + collectDescendantIds(navigationId, tenantId, descendantIds); + if (descendantIds.isEmpty()) { + return; + } + LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper<>(); + updateWrapper.in(CmsNavigation::getNavigationId, descendantIds) + .set(CmsNavigation::getTop, top) + .set(CmsNavigation::getBottom, bottom); + update(updateWrapper); + } + + /** 递归收集某个栏目下所有后代栏目ID(含直接子级与更深层级) */ + private void collectDescendantIds(Integer parentId, Integer tenantId, List result) { + List children = list(new LambdaQueryWrapper() + .eq(CmsNavigation::getParentId, parentId) + .eq(CmsNavigation::getTenantId, tenantId) + .eq(CmsNavigation::getDeleted, 0) + .select(CmsNavigation::getNavigationId)); + if (children.isEmpty()) { + return; + } + for (CmsNavigation child : children) { + result.add(child.getNavigationId()); + collectDescendantIds(child.getNavigationId(), tenantId, result); + } + } + @Override public CmsNavigation getByIdRelByCodeRel(String code) { CmsNavigationParam param = new CmsNavigationParam();