package com.sumbytes.helloblog.menu.service.impl; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.sumbytes.common.base.domain.Result; import com.sumbytes.common.enums.ErrorEnum; import com.sumbytes.common.util.ExceptionUtil; import com.sumbytes.common.util.PageUtil; import com.sumbytes.helloblog.menu.dao.MenuDao; import com.sumbytes.helloblog.menu.domain.po.Menu; import com.sumbytes.helloblog.menu.domain.vo.MenuVO; import com.sumbytes.helloblog.menu.service.MenuService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import java.util.ArrayList; import java.util.List; import java.util.Optional; @Service public class MenuServiceImpl implements MenuService { @Autowired private MenuDao menuDao; @Override public Result saveMenu(MenuVO menuVO) { menuDao.insert(new Menu() .setIcon(menuVO.getIcon()) .setParentId(menuVO.getParentId()) .setSort(menuVO.getSort()) .setTitle(menuVO.getTitle()) .setUrl(menuVO.getUrl())); return Result.createWithSuccessMessage(); } @Override public Result getMenu(Long id) { Menu menu=menuDao.selectById(id); if (menu==null){ ExceptionUtil.rollback(ErrorEnum.DATA_NO_EXIST); } return Result.createWithModel(menu); } @Override public Result getMenuList(MenuVO menuVO) { menuVO= Optional.ofNullable(menuVO).orElse(new MenuVO()); Page page= Optional.of(PageUtil.checkAndInitPage(menuVO)).orElse(PageUtil.initPage()); if (!StringUtils.isEmpty(menuVO.getKeywords())){ menuVO.setKeywords("%"+menuVO.getKeywords()+"%"); } List menuVOList=menuDao.selectMenuList(page,menuVO); return Result.createWithPaging(menuVOList, PageUtil.initPageInfo(page)); } @Override public Result updateMenu(MenuVO menuVO) { this.menuDao.updateById( new Menu() .setId(menuVO.getId()) .setIcon(menuVO.getIcon()) .setTitle(menuVO.getTitle()) .setParentId(menuVO.getParentId()) .setSort(menuVO.getSort()) .setUrl(menuVO.getUrl()) ); return Result.createWithSuccessMessage(); } @Override public Result deleteMenu(Long id) { this.menuDao.deleteById(id); return Result.createWithSuccessMessage(); } @Override public Result getFrontMenuList(MenuVO menuVO) { List menus = menuDao.selectMenuList(menuVO.setParentId(0L)); List menuVOS=new ArrayList<>(); menus.forEach(menu -> { menuVOS.add(new MenuVO() .setId(menu.getId()) .setIcon(menu.getIcon()) .setTitle(menu.getTitle()) .setParentId(menu.getParentId()) .setSort(menu.getSort()) .setUrl(menu.getUrl()) .setChild(menuDao.selectMenuList(menuVO.setParentId(menu.getId()))) ); }); return Result.createWithModels(menuVOS); } }