保存方法代码:
public PlatformMenuEntity saveMenu(PlatformMenuDto saveDto) {
PlatformMenuEntity entity = new PlatformMenuEntity();
if (StringUtils.isNotEmpty(saveDto.getMenuName())) {
BeanUtils.copyProperties(saveDto, entity);
// 按钮
JSONArray buttonObjects = JSONArray.parseArray(saveDto.getButtonTableJsonData());
List<PlatformMenuButtonVo> menuButtonList= buttonObjects.toJavaList(PlatformMenuButtonVo.class);
// 列表
JSONArray listViewsObjects = JSONArray.parseArray(saveDto.getListViewsTableJsonData());
List<PlatformMenuListViewsVo> listViewsList= listViewsObjects.toJavaList(PlatformMenuListViewsVo.class);
// 表单
JSONArray formViewsObjects = JSONArray.parseArray(saveDto.getFormViewsTableJsonData());
List<PlatformMenuFormViewsVo> formViewsList= formViewsObjects.toJavaList(PlatformMenuFormViewsVo.class);
if (StringUtils.isEmpty(entity.getIsShow())) {
entity.setIsShow("1");
}
String parentId = entity.getParentId();
if (StringUtils.isEmpty(parentId)) {
parentId = "0";
}
Date date = new Date();
LoginUser loginUser = SecurityUtils.getLoginUser();
entity.setUpdateBy(loginUser.getUser().getUserName());
entity.setUpdateTime(date);
String newParentIds = "";
// 获取修改前的parentIds,用于更新子节点的parentIds
String oldParentIds = entity.getParentIds();
// 菜单类型(M目录 C菜单 )
if ("C".equals(entity.getMenuType())) {
entity.setIsMenu("1");
} else {
entity.setIsMenu("0");
}
// 获取修改前的lineage,用于更新子节点的lineage
String oldLineage = entity.getLineage();
// 获取子集数量
int childCount = platformMenuMapper.hasChildByMenuId(parentId);
// 查询子集最大世系
String childMaxLineage = platformMenuMapper.findMaxLineageByParentId(parentId);
int num = 1;
// 排序号
if (entity.getOrderNum() == null || entity.getOrderNum() == 0) {
entity.setOrderNum((childCount + 1) * 10);
}
// 新增模块
if (StringUtils.isEmpty(entity.getMenuId())) {
String parentIds = "";
entity.setMenuId(UUID.randomUUID().toString());
if (StringUtils.isEmpty(childMaxLineage)) {
num = childCount + 1;
} else {
if (childMaxLineage.endsWith(".")) {
childMaxLineage = childMaxLineage.substring(0, childMaxLineage.length() -1);
}
int siteIndex = childMaxLineage.lastIndexOf(".");
siteIndex = siteIndex + 1;
Integer maxLineage = Integer.valueOf(childMaxLineage.substring(siteIndex, childMaxLineage.length()));
if (maxLineage != null) {
num = maxLineage + 1;
} else {
num = childCount + 1;
}
}
if ("0".equals(parentId)) {
String newLineage = String.format("%05d", num);
entity.setLineage(newLineage+".");
} else {
PlatformMenuEntity parentInfo = selectPlatformMenuById(parentId);
String parentLineage = parentInfo.getLineage();
if (StringUtils.isEmpty(parentLineage)) {
parentLineage = "";
}
String newLineage = String.format("%05d", num);
entity.setLineage(parentLineage + newLineage + ".");
parentIds = parentInfo.getParentIds();
}
newParentIds = parentIds + parentId + ",";
entity.setParentId(parentId);
// 设置新的父节点串
entity.setParentIds(newParentIds);
// 世系赋值
oldLineage = entity.getLineage();
Integer level = newParentIds.split(",").length;
entity.setMenuLevel(level);
entity.setCreateBy(loginUser.getUser().getUserName());
entity.setCreateTime(date);
insertPlatformMenu(entity);
}
// 修改模块
else {
PlatformMenuEntity menuInfo = selectPlatformMenuById(entity.getMenuId());
String oldParentId = menuInfo.getParentId();
// 原所有父级ID
oldParentIds = menuInfo.getParentIds();
entity.setParentIds(oldParentIds);
// 世系赋值
oldLineage = menuInfo.getLineage();
String parentIds = "";
if (!oldParentId.equals(parentId)) {
if (StringUtils.isEmpty(childMaxLineage)) {
num = childCount + 1;
} else {
if (childMaxLineage.endsWith(".")) {
childMaxLineage = childMaxLineage.substring(0, childMaxLineage.length() -1);
}
int siteIndex = childMaxLineage.lastIndexOf(".");
siteIndex = siteIndex + 1;
Integer maxLineage = Integer.valueOf(childMaxLineage.substring(siteIndex, childMaxLineage.length()));
if (maxLineage != null) {
num = maxLineage + 1;
} else {
num = childCount + 1;
}
}
if ("0".equals(parentId)) {
String newLineage = String.format("%05d", num); // 补5位0
entity.setLineage(newLineage+".");
} else {
PlatformMenuEntity parentInfo = selectPlatformMenuById(parentId);
String parentLineage = parentInfo.getLineage();
if (StringUtils.isEmpty(parentLineage)) {
parentLineage = "";
}
String newLineage = String.format("%05d", num);
entity.setLineage(parentLineage + newLineage + ".");
parentIds = parentInfo.getParentIds();
}
newParentIds = parentIds + parentId + ",";
entity.setParentId(parentId);
// 设置新的父节点串
entity.setParentIds(newParentIds);
} else {
entity.setLineage(oldLineage);
}
Integer level = entity.getParentIds().split(",").length;
entity.setMenuLevel(level);
updatePlatformMenu(entity);
if (!oldParentId.equals(parentId)) {
// 更新子节点 parentIds
PlatformMenuEntity p = new PlatformMenuEntity();
p.setParentIds("%,"+entity.getMenuId()+",%");
List<PlatformMenuEntity> list = platformMenuMapper.findByParentIdsLike(p);
for (PlatformMenuEntity e : list){
if (e.getParentIds() != null && oldParentIds != null && StringUtils.isNotEmpty(oldLineage)){
e.setParentIds(e.getParentIds().replace(oldParentIds, entity.getParentIds()));
e.setLineage(e.getLineage().replaceFirst(oldLineage, entity.getLineage()));
platformMenuMapper.updateParentIds(e);
}
}
}
}
// 批量新增按钮
platformMenuButtonService.insertBatchButton(entity.getMenuId(), loginUser.getUser().getUserId(), date, menuButtonList);
// 批量新增列表视图
platformMenuListViewsService.insertBatchListViews(entity.getMenuId(), loginUser.getUser().getUserId(), date, listViewsList);
// 批量新增表单视图
platformMenuFormViewsService.insertBatchFormViews(entity.getMenuId(), loginUser.getUser().getUserId(), date, formViewsList);
//刷新缓存
PlatformMenuUtils.clearPlatformCache();
} else {
throw new CustomException("数据空转换异常", 100100501);
}
return entity;
}