package cn.stylefeng.guns.sys.modular.system.service;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.base.pojo.node.MenuNode;
import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.constant.factory.ConstantFactory;
import cn.stylefeng.guns.sys.core.constant.state.MenuStatus;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.core.listener.ConfigListener;
import cn.stylefeng.guns.base.i18n.context.UserTranslationContext;
import cn.stylefeng.guns.base.i18n.enums.TranslationEnum;
import cn.stylefeng.guns.sys.modular.system.entity.Menu;
import cn.stylefeng.guns.sys.modular.system.mapper.MenuMapper;
import cn.stylefeng.guns.sys.modular.system.model.MenuDto;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
*
* 菜单表 服务实现类
*
*
* @author stylefeng
* @since 2018-12-07
*/
@Service
public class MenuService extends ServiceImpl {
@Resource
private MenuMapper menuMapper;
/**
* 添加菜单
*
* @author fengshuonan
* @Date 2018/12/23 5:59 PM
*/
@Transactional
public void addMenu(MenuDto menuDto) {
if (ToolUtil.isOneEmpty(menuDto, menuDto.getCode(), menuDto.getName(), menuDto.getPid(), menuDto.getMenuFlag(), menuDto.getUrl(), menuDto.getSystemType())) {
throw new RequestEmptyException();
}
//判断是否已经存在该编号
String existedMenuName = ConstantFactory.me().getMenuNameByCode(menuDto.getCode());
if (ToolUtil.isNotEmpty(existedMenuName)) {
throw new ServiceException(BizExceptionEnum.EXISTED_THE_MENU);
}
//组装属性,设置父级菜单编号
Menu resultMenu = this.menuSetPcode(menuDto);
resultMenu.setStatus(MenuStatus.ENABLE.getCode());
this.save(resultMenu);
}
/**
* 更新菜单
*
* @author fengshuonan
* @Date 2019/2/27 4:09 PM
*/
@Transactional(rollbackFor = Exception.class)
public void updateMenu(MenuDto menuDto) {
//如果菜单为空
if (menuDto == null || ToolUtil.isOneEmpty(menuDto.getMenuId(), menuDto.getCode())) {
throw new RequestEmptyException();
}
//获取旧的菜单
Long id = menuDto.getMenuId();
Menu menu = this.getById(id);
if (menu == null) {
throw new RequestEmptyException();
}
//设置父级菜单编号
Menu resultMenu = this.menuSetPcode(menuDto);
//查找该节点的子集合,并修改相应的pcodes和level(因为修改菜单后,层级可能变化了)
updateSubMenuLevels(menu, resultMenu);
this.updateById(resultMenu);
}
/**
* 更新所有子菜单的结构
*
* @param oldMenu 原来的菜单
* @param newMenu 新菜单
* @author fengshuonan
* @Date 2019/2/27 4:25 PM
*/
@Transactional(rollbackFor = Exception.class)
public void updateSubMenuLevels(Menu oldMenu, Menu newMenu) {
List