package com.mzl.flower.service.system;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.mzl.flower.config.exception.ValidationException;
|
import com.mzl.flower.constant.Constants;
|
import com.mzl.flower.dto.request.system.CreateAppMenuDTO;
|
import com.mzl.flower.dto.request.system.UpdateAppMenuDTO;
|
import com.mzl.flower.dto.response.system.AppMenuDTO;
|
import com.mzl.flower.dto.response.system.AppMenuTreeDTO;
|
import com.mzl.flower.entity.system.AppMenu;
|
import com.mzl.flower.enums.TrueOrFalseEnum;
|
import com.mzl.flower.mapper.system.AppMenuMapper;
|
import com.mzl.flower.service.BaseService;
|
import com.mzl.flower.utils.TreeBuilderUtil;
|
import com.mzl.flower.utils.UUIDGenerator;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.util.CollectionUtils;
|
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.stream.Collectors;
|
|
@Service
|
@Transactional
|
public class AppMenuService extends BaseService {
|
@Autowired
|
private AppMenuMapper menuMapper;
|
|
public AppMenu getMenu(String id) {
|
return menuMapper.selectById(id);
|
}
|
|
public void addMenu(CreateAppMenuDTO dto) {
|
AppMenu menu = new AppMenu();
|
// 查找菜单名称是否已经存在
|
// if (menuMapper.selectCount(new QueryWrapper<AppMenu>()
|
// .lambda().eq(AppMenu::getMenuName, dto.getMenuName())
|
// .eq(AppMenu::getStatus, Constants.STATUS_ACTIVE)
|
// ) > 0) {
|
// throw new ValidationException("菜单名称已经存在");
|
// }
|
// 查找权限标识是否已经存在
|
if (menuMapper.selectCount(new QueryWrapper<AppMenu>().lambda().eq(AppMenu::getPermissionUq, dto.getPermissionUq()).eq(AppMenu::getStatus, Constants.STATUS_ACTIVE)) > 0) {
|
throw new ValidationException("权限标识已经存在");
|
}
|
|
BeanUtils.copyProperties(dto, menu);
|
menu.setId(UUIDGenerator.getUUID());
|
menu.setIsVisible(Constants.Y);
|
menu.setStatus(Constants.STATUS_ACTIVE);
|
menu.setSubaccountAccessFlag(Constants.CUSTOM01.ONE.getDesc());
|
|
menuMapper.insert(menu);
|
}
|
|
public List<AppMenuTreeDTO> searchOperationMenu() {
|
List<AppMenuTreeDTO> treeList = menuMapper.selectListOrderBySeq();
|
for (AppMenuTreeDTO appMenuTreeDTO : treeList) {
|
appMenuTreeDTO.setLabel(appMenuTreeDTO.getMenuName());
|
appMenuTreeDTO.setPath(appMenuTreeDTO.getMenuHref());
|
appMenuTreeDTO.setName(appMenuTreeDTO.getMenuName());
|
}
|
treeList = (List<AppMenuTreeDTO>) TreeBuilderUtil.buildListToTree(treeList);
|
return treeList;
|
}
|
|
public AppMenuDTO getMenuDetail(String id) {
|
AppMenu menu = getMenu(id);
|
|
//返回对象
|
AppMenuDTO menuDTO = new AppMenuDTO();
|
if (menu != null) {
|
BeanUtils.copyProperties(menu, menuDTO);
|
}
|
return menuDTO;
|
}
|
|
public void updateMenu(UpdateAppMenuDTO dto) {
|
AppMenu menu = getMenu(dto.getId());
|
if(null==menu){
|
throw new ValidationException("菜单不存在");
|
}
|
// if (menuMapper.selectCount(new QueryWrapper<AppMenu>().lambda().eq(AppMenu::getMenuName, dto.getMenuName()).eq(AppMenu::getStatus, Constants.STATUS_ACTIVE).ne(AppMenu::getId, menu.getId())) > 0) {
|
// throw new ValidationException("菜单名称已经存在");
|
// }
|
// 查找权限标识是否已经存在
|
if (menuMapper.selectCount(new QueryWrapper<AppMenu>().lambda().eq(AppMenu::getPermissionUq, dto.getPermissionUq()).eq(AppMenu::getStatus, Constants.STATUS_ACTIVE).ne(AppMenu::getId, menu.getId())) > 0) {
|
throw new ValidationException("权限标识已经存在");
|
}
|
Integer subaccountAccessFlag = menu.getSubaccountAccessFlag();
|
BeanUtils.copyProperties(dto, menu);
|
menu.setSubaccountAccessFlag(subaccountAccessFlag);
|
menuMapper.updateById(menu);
|
}
|
|
public void deleteMenu(String id) {
|
AppMenu menu = getMenu(id);
|
menu.setStatus(Constants.STATUS_DELETED);
|
menuMapper.updateById(menu);
|
|
List<AppMenu> menus = menuMapper.selectList(new QueryWrapper<AppMenu>().eq("PARENT_ID", id));
|
if (menus != null && menus.size() > 0) {
|
for (AppMenu children : menus) {
|
deleteMenu(children.getId());
|
}
|
}
|
}
|
|
public void updateMenuShow(String id) {
|
// 更新当前菜单及所有子菜单都变为隐藏
|
List<String> menuIds = new ArrayList<>();
|
findAllChildMenuIds(id, menuIds);
|
menuMapper.updateMenuSubaccountAccessFlag(menuIds, Constants.CUSTOM01.ONE.getDesc());
|
}
|
|
public void updateMenuHidden(String id) {
|
// 递归查找所有子菜单
|
List<String> menuIds = new ArrayList<>();
|
findAllChildMenuIds(id, menuIds);
|
menuMapper.updateMenuSubaccountAccessFlag(menuIds, Constants.CUSTOM01.ZERO.getDesc());
|
}
|
|
|
/**
|
* 递归查找当前菜单及所有子菜单的 ID
|
*
|
* @param menuId 当前菜单 ID
|
* @param menuIds 存储菜单 ID 的列表
|
*/
|
private void findAllChildMenuIds(String menuId, List<String> menuIds) {
|
// 首先将当前菜单 ID 加入菜单列表
|
menuIds.add(menuId);
|
|
// 查询当前菜单的所有子菜单
|
List<String> childMenuIds = menuMapper.findChildMenuIds(menuId);
|
|
// 如果有子菜单,递归查找
|
if (childMenuIds != null && !childMenuIds.isEmpty()) {
|
for (String childMenuId : childMenuIds) {
|
findAllChildMenuIds(childMenuId, menuIds); // 递归查找子菜单
|
}
|
}
|
}
|
|
// 获取供应商菜单信息
|
public List<AppMenu> getPermissionMenu() {
|
|
List<AppMenu> list = getAppMenus();
|
|
if (!CollectionUtils.isEmpty(list)) {
|
|
List<AppMenu> allMenuList = menuMapper.selectList(
|
new QueryWrapper<AppMenu>().lambda().eq(AppMenu::getStatus, Constants.STATUS_ACTIVE));
|
|
return allMenuList;
|
}
|
|
return null;
|
}
|
|
private List<AppMenu> getAppMenus() {
|
LambdaQueryWrapper<AppMenu> queryWrapper = new QueryWrapper<AppMenu>()
|
.lambda()
|
.eq(AppMenu::getStatus, Constants.STATUS_ACTIVE)
|
// .eq(AppMenu::getSubaccountAccessFlag,Constants.CUSTOM01.ONE.getDesc())
|
// .eq(AppMenu::getPermissionUq, Constants.APP_MENU_TYPE.SUPPLIER.getDesc())
|
;
|
List<AppMenu> list = menuMapper.selectList(queryWrapper);
|
return list;
|
}
|
|
|
public List<AppMenu> getSupplierPermissionMenu() {
|
// 获取所有订单信息
|
List<AppMenu> list = getAppMenus();
|
// 遍历list,找到appMenu的permissionUq的值为supplier的菜单
|
// 找到 permissionUq 为 supplier 的菜单
|
List<AppMenu> supplierMenus = list.stream()
|
.filter(menu -> Constants.DEFAULT_SUB_ACCOUNT_SUPPLIER.equals(menu.getPermissionUq()))
|
.collect(Collectors.toList());
|
if(!CollectionUtils.isEmpty(supplierMenus)){
|
AppMenu appMenu = supplierMenus.get(0);
|
|
// 递归查找所有子菜单
|
List<AppMenu> allChildMenus = findAllChildren(appMenu.getId(), list);
|
|
return allChildMenus;
|
}
|
|
return new ArrayList<>();
|
}
|
|
public List<AppMenu> getPartnerPermissionMenu() {
|
// 获取所有订单信息
|
List<AppMenu> list = getAppMenus();
|
// 遍历list,找到appMenu的permissionUq的值为supplier的菜单
|
// 找到 permissionUq 为 supplier 的菜单
|
List<AppMenu> supplierMenus = list.stream()
|
.filter(menu -> Constants.DEFAULT_SUB_ACCOUNT_PARTNER.equals(menu.getPermissionUq()))
|
.collect(Collectors.toList());
|
if(!CollectionUtils.isEmpty(supplierMenus)){
|
AppMenu appMenu = supplierMenus.get(0);
|
|
// 递归查找所有子菜单
|
List<AppMenu> allChildMenus = findAllChildren(appMenu.getId(), list);
|
|
return allChildMenus;
|
}
|
|
return new ArrayList<>();
|
}
|
|
/**
|
* 递归查找所有子菜单
|
*
|
* @param parentId 父菜单 ID
|
* @param allMenus 所有菜单列表
|
* @return 所有子菜单
|
*/
|
public static List<AppMenu> findAllChildren(String parentId, List<AppMenu> allMenus) {
|
List<AppMenu> children = allMenus.stream()
|
.filter(menu -> parentId.equals(menu.getParentId()))
|
.collect(Collectors.toList());
|
|
List<AppMenu> allChildren = new ArrayList<>(children);
|
for (AppMenu child : children) {
|
allChildren.addAll(findAllChildren(child.getId(), allMenus));
|
}
|
|
return allChildren;
|
}
|
|
|
}
|