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() // .lambda().eq(AppMenu::getMenuName, dto.getMenuName()) // .eq(AppMenu::getStatus, Constants.STATUS_ACTIVE) // ) > 0) { // throw new ValidationException("菜单名称已经存在"); // } // 查找权限标识是否已经存在 if (menuMapper.selectCount(new QueryWrapper().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 searchOperationMenu() { List treeList = menuMapper.selectListOrderBySeq(); for (AppMenuTreeDTO appMenuTreeDTO : treeList) { appMenuTreeDTO.setLabel(appMenuTreeDTO.getMenuName()); appMenuTreeDTO.setPath(appMenuTreeDTO.getMenuHref()); appMenuTreeDTO.setName(appMenuTreeDTO.getMenuName()); } treeList = (List) 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().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().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 menus = menuMapper.selectList(new QueryWrapper().eq("PARENT_ID", id)); if (menus != null && menus.size() > 0) { for (AppMenu children : menus) { deleteMenu(children.getId()); } } } public void updateMenuShow(String id) { // 更新当前菜单及所有子菜单都变为隐藏 List menuIds = new ArrayList<>(); findAllChildMenuIds(id, menuIds); menuMapper.updateMenuSubaccountAccessFlag(menuIds, Constants.CUSTOM01.ONE.getDesc()); } public void updateMenuHidden(String id) { // 递归查找所有子菜单 List 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 menuIds) { // 首先将当前菜单 ID 加入菜单列表 menuIds.add(menuId); // 查询当前菜单的所有子菜单 List childMenuIds = menuMapper.findChildMenuIds(menuId); // 如果有子菜单,递归查找 if (childMenuIds != null && !childMenuIds.isEmpty()) { for (String childMenuId : childMenuIds) { findAllChildMenuIds(childMenuId, menuIds); // 递归查找子菜单 } } } // 获取供应商菜单信息 public List getPermissionMenu() { List list = getAppMenus(); if (!CollectionUtils.isEmpty(list)) { List allMenuList = menuMapper.selectList( new QueryWrapper().lambda().eq(AppMenu::getStatus, Constants.STATUS_ACTIVE)); return allMenuList; } return null; } private List getAppMenus() { LambdaQueryWrapper queryWrapper = new QueryWrapper() .lambda() .eq(AppMenu::getStatus, Constants.STATUS_ACTIVE) // .eq(AppMenu::getSubaccountAccessFlag,Constants.CUSTOM01.ONE.getDesc()) // .eq(AppMenu::getPermissionUq, Constants.APP_MENU_TYPE.SUPPLIER.getDesc()) ; List list = menuMapper.selectList(queryWrapper); return list; } public List getSupplierPermissionMenu() { // 获取所有订单信息 List list = getAppMenus(); // 遍历list,找到appMenu的permissionUq的值为supplier的菜单 // 找到 permissionUq 为 supplier 的菜单 List 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 allChildMenus = findAllChildren(appMenu.getId(), list); return allChildMenus; } return new ArrayList<>(); } public List getPartnerPermissionMenu() { // 获取所有订单信息 List list = getAppMenus(); // 遍历list,找到appMenu的permissionUq的值为supplier的菜单 // 找到 permissionUq 为 supplier 的菜单 List 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 allChildMenus = findAllChildren(appMenu.getId(), list); return allChildMenus; } return new ArrayList<>(); } /** * 递归查找所有子菜单 * * @param parentId 父菜单 ID * @param allMenus 所有菜单列表 * @return 所有子菜单 */ public static List findAllChildren(String parentId, List allMenus) { List children = allMenus.stream() .filter(menu -> parentId.equals(menu.getParentId())) .collect(Collectors.toList()); List allChildren = new ArrayList<>(children); for (AppMenu child : children) { allChildren.addAll(findAllChildren(child.getId(), allMenus)); } return allChildren; } }