菜单管理

master^2
lzq 2026-03-03 17:53:29 +08:00
parent a0f1160b3f
commit fdcc4b3a81
2 changed files with 13 additions and 14 deletions

View File

@ -18,7 +18,7 @@ import lombok.Setter;
@Setter @Setter
@Constraint @Constraint
public class MenuAddParam implements Constrained { public class MenuAddParam implements Constrained {
@NotNull @NotNull(message = "未指定客户端")
private ClientCode clientCode; private ClientCode clientCode;
/** /**

View File

@ -27,6 +27,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -69,7 +70,7 @@ public class MenuService extends ServiceImpl<MenuMapper, MenuEntity> implements
Integer sort = menuEntity.getSort(); Integer sort = menuEntity.getSort();
if (sort == null || sort == 0) { if (sort == null || sort == 0) {
Integer maxSort = this.getOne(Wrappers.<MenuEntity>query().eq("pid", pid).orderByDesc("sort").select("IFNULL(MAX(sort),0) sort")).getSort(); Integer maxSort = this.getOne(Wrappers.<MenuEntity>query().eq("pid", pid).select("IFNULL(MAX(sort),0) sort")).getSort();
menuEntity.setSort(maxSort + 1); menuEntity.setSort(maxSort + 1);
} else { } else {
this.update(Wrappers.<MenuEntity>lambdaUpdate().eq(MenuEntity::getPid, pid).ge(MenuEntity::getSort, sort).setIncrBy(MenuEntity::getSort, 1)); this.update(Wrappers.<MenuEntity>lambdaUpdate().eq(MenuEntity::getPid, pid).ge(MenuEntity::getSort, sort).setIncrBy(MenuEntity::getSort, 1));
@ -108,29 +109,27 @@ public class MenuService extends ServiceImpl<MenuMapper, MenuEntity> implements
} }
Long oldPid = oldMenuEntity.getPid(); Long oldPid = oldMenuEntity.getPid();
if (pid != null && !pid.equals(oldPid)) { if (pid != null && !pid.equals(oldPid)) {
MenuEntity parent = this.getById(pid); MenuEntity parent;
if (pid == 0) {
parent = new MenuEntity()
.setTier(0)
.setBreadcrumb(new LinkedList<>())
.setId(0L);
} else {
parent = this.getById(pid);
}
Assert.notNull(parent, () -> Exceptions.exception("上级菜单不存在")); Assert.notNull(parent, () -> Exceptions.exception("上级菜单不存在"));
String title = menuEntity.getTitle(); String title = menuEntity.getTitle();
List<String> breadcrumb = parent.getBreadcrumb(); List<String> breadcrumb = parent.getBreadcrumb();
breadcrumb.add(StrUtil.isBlank(title) ? oldMenuEntity.getTitle() : title); breadcrumb.add(StrUtil.isBlank(title) ? oldMenuEntity.getTitle() : title);
menuEntity.setBreadcrumb(breadcrumb) menuEntity.setBreadcrumb(breadcrumb)
.setTier(parent.getTier() + 1); .setTier(parent.getTier() + 1);
this.modifyChild(menuEntity); this.modifyChild(menuEntity);
} }
Integer sort = menuEntity.getSort(); Integer sort = menuEntity.getSort();
if (sort != null && sort != 0) { if (sort != null && sort != 0) {
List<MenuEntity> list = this.list(Wrappers.<MenuEntity>lambdaQuery() this.update(Wrappers.<MenuEntity>lambdaUpdate().eq(MenuEntity::getPid, pid).ge(MenuEntity::getSort, sort).setIncrBy(MenuEntity::getSort, 1));
.eq(MenuEntity::getPid, pid == null ? oldPid : pid)
.ge(MenuEntity::getSort, sort)
.ne(MenuEntity::getId, id)
.select(MenuEntity::getSort, MenuEntity::getId)
);
if (CollUtil.isNotEmpty(list)) {
list.forEach(item -> item.setSort(sort + 1));
this.updateBatchById(list);
}
} }
this.updateById(menuEntity); this.updateById(menuEntity);