|
|
@@ -1,7 +1,9 @@
|
|
|
package cn.reghao.autodop.dmaster.auth.service;
|
|
|
|
|
|
+import cn.reghao.autodop.dmaster.auth.entity.MenuType;
|
|
|
import cn.reghao.autodop.dmaster.auth.entity.po.Menu;
|
|
|
import cn.reghao.autodop.dmaster.auth.entity.po.Role;
|
|
|
+import cn.reghao.autodop.dmaster.auth.entity.vo.MenuVO;
|
|
|
import cn.reghao.autodop.dmaster.auth.repository.MenuRepository;
|
|
|
import cn.reghao.autodop.dmaster.auth.repository.RoleRepository;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
@@ -26,9 +28,29 @@ public class ResourceService {
|
|
|
this.roleRepository = roleRepository;
|
|
|
}
|
|
|
|
|
|
- public void addResource(Menu menu) {
|
|
|
+ public synchronized void addResource(Menu menu) {
|
|
|
+ // menu 最多只能有两个 parent,即最多只能有三级菜单
|
|
|
+ checkMenu(menu);
|
|
|
+ // 调整 menu 组内元素的位置
|
|
|
+ adjustPosition(menu);
|
|
|
+ menu.setIsEnabled(true);
|
|
|
+ Menu menuEntity = menuRepository.save(menu);
|
|
|
+
|
|
|
+ Set<Role> roles = menu.getRoles();
|
|
|
+ roles.forEach(role -> role.getMenus().add(menuEntity));
|
|
|
+ roleRepository.saveAll(roles);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查菜单的层级
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ * @date 2021-07-15 上午11:15
|
|
|
+ */
|
|
|
+ private void checkMenu(Menu menu) {
|
|
|
int pid = menu.getPid();
|
|
|
- /*if (pid != 0) {
|
|
|
+ if (pid != 0) {
|
|
|
Menu menu1 = getResourceById(pid);
|
|
|
int pid1 = menu1.getPid();
|
|
|
if (pid1 != 0) {
|
|
|
@@ -39,144 +61,125 @@ public class ResourceService {
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
- }*/
|
|
|
-
|
|
|
- menu.setIsEnabled(true);
|
|
|
- int pos = menu.getPos()+1;
|
|
|
- if (adjustPosition(pid, pos)) {
|
|
|
- menu.setPos(pos);
|
|
|
- //menuRepository.save(menu);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * TODO 某个 menu 的位置调整后,调整同一组内其他 menu 的相对位置
|
|
|
+ * 调整同一 pid 组内 menu 的位置
|
|
|
*
|
|
|
- * @param pid 组 ID
|
|
|
- * @param pos 当前 menu 的最新位置
|
|
|
- * @return
|
|
|
* @date 2021-05-18 上午9:54
|
|
|
*/
|
|
|
- private boolean adjustPosition(int pid, int pos) {
|
|
|
- List<Menu> menus = menuRepository.findByPid(pid);
|
|
|
- int size = menus.size();
|
|
|
- if (pos > size+1) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // 组内没有menu 或新增的 menu 在组内排在最后一位
|
|
|
- if (menus.isEmpty() || pos == size) {
|
|
|
- return true;
|
|
|
+ private void adjustPosition(Menu menu) {
|
|
|
+ int pid = menu.getPid();
|
|
|
+ if (pid != 0) {
|
|
|
+ Menu pMenu = menuRepository.getOne(pid);
|
|
|
+ if (pMenu.getType().equals(MenuType.PAGE.name())) {
|
|
|
+ log.error("父级菜单的类型不能是 PAGE");
|
|
|
+ return;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- // 新增的 menu 在组内排在第 1 位
|
|
|
- if (pos == 0) {
|
|
|
- // 组内的元素均后移一位
|
|
|
- List<Menu> tmpList = menus.stream().peek(menu1 -> {
|
|
|
- int tmp = menu1.getPos();
|
|
|
- menu1.setPos(tmp+1);
|
|
|
- }).collect(Collectors.toList());
|
|
|
- menuRepository.saveAll(tmpList);
|
|
|
- return true;
|
|
|
+ List<Menu> menus = menuRepository.findByPid(pid);
|
|
|
+ menus.sort(Comparator.comparingInt(Menu::getPos));
|
|
|
+ // 组内没有 menu 或新增的 menu 在组内排在最后一位
|
|
|
+ if (menus.isEmpty()) {
|
|
|
+ menu.setPos(1);
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
- // 新增的 menu 插入到组中间
|
|
|
- Map<Integer, Menu> map = menus.stream().collect(Collectors.toMap(Menu::getPos, menu1 -> menu1));
|
|
|
- // 需要调整顺序的 menu
|
|
|
- List<Menu> tmpList = new ArrayList<>();
|
|
|
- for (int i = pos+1; i <= size; i++) {
|
|
|
- Menu menu = map.get(i);
|
|
|
- int tmp = menu.getPos();
|
|
|
- menu.setPos(tmp+1);
|
|
|
- menu.setUpdateTime(LocalDateTime.now());
|
|
|
- tmpList.add(menu);
|
|
|
+ // menu 的位置
|
|
|
+ int pos = menu.getPos()+1;
|
|
|
+ int size = menus.size();
|
|
|
+ if (pos == 1) {
|
|
|
+ // menu 的位置在 menu 组的首位, 组内的其他元素位置向后移一位
|
|
|
+ menu.setPos(1);
|
|
|
+ moveBackward(menus, 1);
|
|
|
+ } else if (pos >= size+1) {
|
|
|
+ // menu 的位置在 menu 组的末尾, 组内的其他元素位置不变
|
|
|
+ menu.setPos(size+1);
|
|
|
+ } else {
|
|
|
+ // menu 的位置在 menu 组的中间, pos 开始的元素位置向后移一位
|
|
|
+ menu.setPos(pos);
|
|
|
+ moveBackward(menus, pos);
|
|
|
}
|
|
|
- menuRepository.saveAll(tmpList);
|
|
|
- return true;
|
|
|
}
|
|
|
|
|
|
- public boolean updateResource(Menu menu) {
|
|
|
- Optional<Menu> optionalMenu = menuRepository.findById(menu.getId());
|
|
|
- if (!optionalMenu.isPresent()) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- Menu menuEntity = optionalMenu.get();
|
|
|
- int oldPos = menuEntity.getPos();
|
|
|
- int newPos = menu.getPos();
|
|
|
- // menu 位置发生改变
|
|
|
- if (oldPos != newPos+1) {
|
|
|
- Map<Integer, Menu> map = menuRepository.findByPid(menu.getPid()).stream()
|
|
|
- .collect(Collectors.toMap(Menu::getPos, menu1 -> menu1));
|
|
|
- int size = map.size();
|
|
|
-
|
|
|
- // 元素移动到最后一位
|
|
|
- if (newPos == size) {
|
|
|
- List<Menu> tmpList = new ArrayList<>();
|
|
|
- for (int i = oldPos+1; i <= newPos; i++) {
|
|
|
- Menu tmp = map.get(i);
|
|
|
- tmp.setPos(tmp.getPos()-1);
|
|
|
- tmpList.add(tmp);
|
|
|
- }
|
|
|
- menuRepository.saveAll(tmpList);
|
|
|
- menu.setUpdateTime(LocalDateTime.now());
|
|
|
- menuRepository.save(menu);
|
|
|
- return true;
|
|
|
- } else if (newPos < size) {
|
|
|
- adjustPosition1(map, oldPos, newPos+1);
|
|
|
- } else {
|
|
|
- return false;
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 向后移动元素
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ * @date 2021-07-15 下午5:07
|
|
|
+ */
|
|
|
+ private void moveBackward(List<Menu> menus, int startPos) {
|
|
|
+ for (int i = startPos-1; i < menus.size(); i++) {
|
|
|
+ Menu tmpMenu = menus.get(i);
|
|
|
+ int newPos = tmpMenu.getPos()+1;
|
|
|
+ tmpMenu.setPos(newPos);
|
|
|
}
|
|
|
-
|
|
|
- menu.setPos(newPos+1);
|
|
|
- menu.setUpdateTime(LocalDateTime.now());
|
|
|
- menuRepository.save(menu);
|
|
|
- return true;
|
|
|
+ menuRepository.saveAll(menus);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * TODO 调整组内 menu 的顺序
|
|
|
+ * 向前移动元素
|
|
|
*
|
|
|
* @param
|
|
|
* @return
|
|
|
- * @date 2021-05-19 下午2:15
|
|
|
+ * @date 2021-07-15 下午5:41
|
|
|
*/
|
|
|
- private void adjustPosition1(Map<Integer, Menu> map, int oldPos, int newPos) {
|
|
|
- List<Menu> tmpList = new ArrayList<>();
|
|
|
- if (oldPos < newPos) {
|
|
|
- // 元素向后移动
|
|
|
- // oldPos 前面的元素保持不变
|
|
|
- // newPos 后面的元素保持不变
|
|
|
- // (oldPos, newPos] 之间的元素向前移动一位
|
|
|
-
|
|
|
- for (int i = oldPos+1; i <= newPos; i++) {
|
|
|
- Menu tmp = map.get(i);
|
|
|
- tmp.setPos(tmp.getPos()-1);
|
|
|
- tmpList.add(tmp);
|
|
|
- }
|
|
|
- } else {
|
|
|
- // 元素向前移动
|
|
|
- // newPos 前面的元素保持不变
|
|
|
- // oldPos 后面的元素保持不变
|
|
|
- // [newPos, oldPos) 之间的元素向后移动一位
|
|
|
- for (int i = newPos; i < oldPos; i++) {
|
|
|
- Menu tmp = map.get(i);
|
|
|
- tmp.setPos(tmp.getPos()+1);
|
|
|
- tmpList.add(tmp);
|
|
|
- }
|
|
|
+ private void moveForward(List<Menu> menus, int startPos) {
|
|
|
+ for (int i = startPos; i < menus.size(); i++) {
|
|
|
+ Menu tmpMenu = menus.get(i);
|
|
|
+ int newPos = tmpMenu.getPos()-1;
|
|
|
+ tmpMenu.setPos(newPos);
|
|
|
}
|
|
|
- menuRepository.saveAll(tmpList);
|
|
|
+ menuRepository.saveAll(menus);
|
|
|
}
|
|
|
|
|
|
- public void deleteResource(Menu menu) {
|
|
|
+ public synchronized void updateResource(MenuVO vo, Set<Role> roles) {
|
|
|
+ int menuId = vo.getMenuId();
|
|
|
+ Menu menuEntity = menuRepository.findById(menuId).orElse(null);
|
|
|
+ if (menuEntity == null) {
|
|
|
+ log.error("menu 不存在");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ int pid = vo.getPid();
|
|
|
+ if (pid != menuEntity.getPid()) {
|
|
|
+ // menu 更换到了新的 menu 组
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ int oldPos = menuEntity.getPos();
|
|
|
+ int pos = vo.getPos();
|
|
|
+ if (oldPos != pos) {
|
|
|
+ // menu 在组内的位置发生变化
|
|
|
+ List<Menu> menus = menuRepository.findByPid(pid);
|
|
|
+ menus.sort(Comparator.comparingInt(Menu::getPos));
|
|
|
+ }
|
|
|
+
|
|
|
+ menuEntity.setName(vo.getName());
|
|
|
+ menuEntity.setUrl(vo.getUrl());
|
|
|
+ menuEntity.setIcon(vo.getIcon());
|
|
|
+ menuEntity.setUpdateTime(LocalDateTime.now());
|
|
|
+ menuRepository.save(menuEntity);
|
|
|
+
|
|
|
+ roles.forEach(role -> role.getMenus().add(menuEntity));
|
|
|
+ roleRepository.saveAll(roles);
|
|
|
+ }
|
|
|
+
|
|
|
+ public synchronized void deleteResource(Menu menu) {
|
|
|
// 删除 Role 关联的 Menu
|
|
|
for (Role role : menu.getRoles()) {
|
|
|
role.getMenus().remove(menu);
|
|
|
roleRepository.save(role);
|
|
|
}
|
|
|
|
|
|
- // TODO 重新调整组内 menu 的位置
|
|
|
+ // 重新调整组内排序
|
|
|
+ int pid = menu.getPid();
|
|
|
+ List<Menu> menus = menuRepository.findByPid(pid);
|
|
|
+ menus.sort(Comparator.comparingInt(Menu::getPos));
|
|
|
+ int pos = menu.getPos();
|
|
|
+ moveForward(menus, pos);
|
|
|
menuRepository.delete(menu);
|
|
|
}
|
|
|
|
|
|
@@ -196,6 +199,7 @@ public class ResourceService {
|
|
|
|
|
|
public Map<Integer, String> getMapByPid(int pid) {
|
|
|
List<Menu> menus = menuRepository.findByPid(pid);
|
|
|
+ menus.sort(Comparator.comparingInt(Menu::getPos));
|
|
|
Map<Integer, String> map = new HashMap<>();
|
|
|
menus.forEach(menu -> {
|
|
|
map.put(menu.getPos(), menu.getName());
|
|
|
@@ -204,6 +208,6 @@ public class ResourceService {
|
|
|
}
|
|
|
|
|
|
public Menu getResourceById(int id) {
|
|
|
- return menuRepository.findById(id).get();
|
|
|
+ return menuRepository.findById(id).orElse(null);
|
|
|
}
|
|
|
}
|