Compare commits

...

1 Commits

Author SHA1 Message Date
taichu110 3bb1aafd68 菜单管理
1 year ago

@ -0,0 +1,160 @@
package com.intelligentHealthCare.service.impl;
import com.intelligentHealthCare.config.CacheNameConfig;
import com.intelligentHealthCare.dto.MenuDto;
import com.intelligentHealthCare.entity.*;
import com.intelligentHealthCare.exception.BizException;
import com.intelligentHealthCare.interceptor.AccountInfoInterceptor;
import com.intelligentHealthCare.repository.AccountRepository;
import com.intelligentHealthCare.repository.MenuRepository;
import com.intelligentHealthCare.repository.RoleMenuPermissionRepository;
import com.intelligentHealthCare.service.MenuService;
import com.intelligentHealthCare.service.RoleMenuPermissionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
@Service
@CacheConfig(cacheNames = {CacheNameConfig.PLATFORM_MENU_ACCOUNT, CacheNameConfig.PLATFORM_MENU})
public class MenuServiceImpl extends AbstractSimpleJpaService<Menu, MenuRepository> implements MenuService {
@Autowired
private RoleMenuPermissionService roleMenuPermissionService;
@Autowired
private RoleMenuPermissionRepository roleMenuPermissionRepository;
@Autowired
private AccountRepository accountRepository;
@Override
@Cacheable(cacheNames = CacheNameConfig.PLATFORM_MENU_ACCOUNT, key = "#accountId", unless = "#result==null")
public List<String> getMenusByAccountId(String accountId) {
//通过账号获取菜单
QAccountRolePermission accountRolePermission = QAccountRolePermission.accountRolePermission;
QRoleMenuPermission roleMenuPermission = QRoleMenuPermission.roleMenuPermission;
return queryFactory.select(roleMenuPermission).from(roleMenuPermission).leftJoin(accountRolePermission).on(roleMenuPermission.role.id
.eq(accountRolePermission.role.id)).where(accountRolePermission.account.id.eq(accountId)).fetch()
.stream().map(r -> r.getMenu().getMenuPath()).collect(Collectors.toList());
}
@Override
@Cacheable(cacheNames = CacheNameConfig.PLATFORM_MENU_ACCOUNT, key = "#accountId", unless = "#result==null")
public List<MenuDto> getMenuTreeByAccountId(String accountId) {
if (accountRepository.getOne(AccountInfoInterceptor.userId.get()).getSystemManager()) {
return this.getMenuTree();
}
QAccountRolePermission accountRolePermission = QAccountRolePermission.accountRolePermission;
QRoleMenuPermission roleMenuPermission = QRoleMenuPermission.roleMenuPermission;
LinkedHashMap<String, MenuDto> menuDtoMap = queryFactory.select(roleMenuPermission).from(roleMenuPermission).leftJoin(accountRolePermission).on(roleMenuPermission.role.id
.eq(accountRolePermission.role.id)).where(accountRolePermission.account.id.eq(accountId)).orderBy(roleMenuPermission.menu.menuSort.asc()).fetch()
.stream().map(RoleMenuPermission::getMenu).collect(Collectors.toMap(Menu::getId, v ->{
MenuDto menuDto = new MenuDto();
BeanUtils.copyProperties(v, menuDto);
return menuDto;
}, (key1, key2) -> key1, LinkedHashMap::new));
LinkedList<MenuDto> temp = new LinkedList<>();
menuDtoMap.forEach((k, v) -> {
if (v.getMenuParent().equals("0")) {
temp.add(v);
return;
}
MenuDto menuDto = menuDtoMap.get(v.getMenuParent());
if (menuDto != null) {
if (menuDto.getChildrenList() == null) {
menuDto.setChildrenList(new LinkedList<>());
}
menuDto.getChildrenList().add(v);
}else {
temp.add(v);
}
});
return temp;
}
@Override
public List<MenuDto> getMenuTree() {
Map<String, MenuDto> menuDtoMap = this.getAll().stream().sorted(Comparator.comparingInt(Menu::getMenuSort)).collect(Collectors.toMap(Menu::getId, v ->{
MenuDto menuDto = new MenuDto();
BeanUtils.copyProperties(v, menuDto);
return menuDto;
}, (key1, key2) -> key1, LinkedHashMap::new));
LinkedList<MenuDto> temp = new LinkedList<>();
menuDtoMap.forEach((k, v) -> {
if (v.getMenuParent().equals("0")) {
temp.add(v);
} else {
MenuDto menuDto = menuDtoMap.get(v.getMenuParent());
if (menuDto != null) {
if (menuDto.getChildrenList() == null) {
menuDto.setChildrenList(new LinkedList<>());
}
menuDto.getChildrenList().add(v);
}
}
});
return temp;
}
@Override
public List<Menu> getParentMenu() {
return repository.getByMenuParent("0");
}
@Override
public Boolean existSameMenuTitle(String menuTitle) {
List<Menu> menus = this.getByFilter(((root, criteriaQuery, criteriaBuilder) -> criteriaBuilder.equal(root.get(Menu_.menuTitle), menuTitle)));
return !menus.isEmpty();
}
@Override
public Boolean existSameMenuPath(String menuPath) {
List<Menu> menus = this.getByFilter(((root, criteriaQuery, criteriaBuilder) -> criteriaBuilder.equal(root.get(Menu_.menuPath), menuPath)));
return !menus.isEmpty();
}
@Override
@CacheEvict(allEntries = true)
public boolean delete(String id) {
List<Menu> menus = this.getByFilter(((root, criteriaQuery, criteriaBuilder) -> criteriaBuilder.equal(root.get(Menu_.menuParent), id)));
if (!menus.isEmpty()) {
throw new BizException("请先删除子级菜单");
}
return super.delete(id);
}
/**
*
*
* @param menu
* @return
*/
@Override
@CacheEvict(allEntries = true)
public Menu update(Menu menu) {
Menu queryMenu = this.getById(menu.getId());
if (menu.getMenuParent().equals("0")) {
if (!queryMenu.getMenuPath().equals(menu.getMenuPath())) {
List<Menu> childrenList = this.getByFilter(((root, criteriaQuery, criteriaBuilder) -> criteriaBuilder.equal(root.get(Menu_.menuParent), menu.getId())));
childrenList.forEach(children -> {
children.setMenuPath(children.getMenuPath().replace(queryMenu.getMenuPath(), menu.getMenuPath()));
});
super.saveAll(childrenList);
}
}
return super.update(menu);
}
@Override
@CacheEvict(allEntries = true)
public Menu save(Menu menu) {
return super.save(menu);
}
}
Loading…
Cancel
Save