Newer
Older
huludao / src / main / java / com / newfiber / api / pc / service / impl / MenusServiceImpl.java
package com.newfiber.api.pc.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.newfiber.api.config.NodeTree;
import com.newfiber.api.core.exception.RRException;
import com.newfiber.api.pc.dao.MenuButtonsRepository;
import com.newfiber.api.pc.dao.MenusRepository;
import com.newfiber.api.pc.dto.MenuDTO;
import com.newfiber.api.pc.model.zhz.Menu;
import com.newfiber.api.pc.model.zhz.MenuButtons;
import com.newfiber.api.pc.service.MenusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 菜单管理业务类
 * @Author:zhz
 * @CreateDate:2020/11/19 15:00
 * @Description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MenusServiceImpl extends ServiceImpl<MenusRepository,Menu> implements MenusService {

    @Autowired
    private MenuButtonsRepository menuButtonsRepository;

    @Autowired
    private MenusRepository menusRepository;


    @Override
    public List<Menu> selectMenuTree() {
        List<Menu> list =  menusRepository.selectMenuTree();
        NodeTree nodeTree = new NodeTree(list);
        List<Menu> menuTrees = nodeTree.buildTree();
        return menuTrees;
    }

    @Override
    public void addMenu(MenuDTO menuDTO) {
        if(menuDTO == null){
            //这边没整合统一的返回code,暂时先写着
            throw new RRException("传入参数为空",601);
        }
        Menu menu = new Menu();
        //随机生成uuid设置为节点编号
        menu.setMenuNo(IdUtil.simpleUUID());
        menu.setCreateTime(new Date());
        menu.setMenuType(menuDTO.getMenuType());
        menu.setMenuTypeZn(menuDTO.getMenuTypeZn());
        menu.setMenuName(menuDTO.getMenuName());
        menu.setMenuGrade(menuDTO.getMenuGrade());
        menu.setMenuGradeZn(menuDTO.getMenuGradeZn());
        menu.setMenuState(menuDTO.getMenuState());
        menu.setMenuStateZn(menuDTO.getMenuStateZn());
        menu.setMenuValue(menuDTO.getMenuValue());
        menu.setRemark(menuDTO.getRemark());
        menu.setOrderSort(menuDTO.getOrderSort());
        menu.setParentMenuId(StringUtils.isEmpty(menuDTO.getParentMenuId()) ? "0" : menuDTO.getParentMenuId());
        boolean insert = this.insert(menu);
        if(!insert){
            throw new RRException("新增数据失败",504);
        }
    }

    @Override
    public void updateMenu(MenuDTO menuDTO) {
        Menu menu = this.selectById(menuDTO.getSerial());
        if(menu == null){
            throw new RRException("没有该条菜单数据",602);
        }

        menu.setUpdateTime(new Date());
        menu.setMenuType(menuDTO.getMenuType());
        menu.setMenuTypeZn(menuDTO.getMenuTypeZn());
        menu.setMenuName(menuDTO.getMenuName());
        menu.setMenuGrade(menuDTO.getMenuGrade());
        menu.setMenuGradeZn(menuDTO.getMenuGradeZn());
        menu.setMenuState(menuDTO.getMenuState());
        menu.setMenuStateZn(menuDTO.getMenuStateZn());
        menu.setMenuValue(menuDTO.getMenuValue());
        menu.setRemark(menuDTO.getRemark());
        menu.setOrderSort(menuDTO.getOrderSort());
        menu.setParentMenuId(StringUtils.isEmpty(menuDTO.getParentMenuId()) ? "0" : menuDTO.getParentMenuId());
        boolean b = this.updateById(menu);
        if(!b){
            throw new RRException("修改失败!",505);
        }
    }

    @Override
    public void deleteMenu(String menuNo) {
        //查询改菜单底下是否有其他二级菜单
        EntityWrapper<Menu> wrappe = new EntityWrapper();
        wrappe.eq("parent_menu_id",menuNo);
        List<Menu> menus = this.selectList(wrappe);
        if(menus.size() > 0){
            throw new RRException("该菜单底下有其他菜单数据,请先删除",600);
        }
        //再查看改菜单下是否有按钮数据
        EntityWrapper<MenuButtons> menu = new EntityWrapper<>();
        menu.eq("menu_no",menuNo);
        List<MenuButtons> menuButtons = menuButtonsRepository.selectList(menu);
        if(menuButtons.size() > 0){
            throw new RRException("该菜单底下有其他菜单数据,请先删除",600);
        }
        //可以执行删除操作
        EntityWrapper<Menu> wrappe1 = new EntityWrapper();
        wrappe1.eq("menu_no",menuNo);
        boolean delete = this.delete(wrappe1);
        if(!delete){
            throw new RRException("删除菜单错误",504);
        }
    }
}