package cn.stylefeng.guns.sys.modular.rest.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.sys.modular.rest.entity.RestMenu;
import cn.stylefeng.guns.sys.modular.rest.mapper.RestMenuMapper;
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 RestMenuService extends ServiceImpl {
@Resource
private RestMenuMapper restMenuMapper;
/**
* 添加菜单
*
* @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);
}
//组装属性,设置父级菜单编号
RestMenu 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();
RestMenu menu = this.getById(id);
if (menu == null) {
throw new RequestEmptyException();
}
//设置父级菜单编号
RestMenu 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(RestMenu oldMenu, RestMenu newMenu) {
List menus = restMenuMapper.getMenusLikePcodes(oldMenu.getCode());
for (RestMenu menu : menus) {
//更新pcode
if (oldMenu.getCode().equals(menu.getPcode())) {
menu.setPcode(newMenu.getCode());
}
//更新pcodes
String oldPcodesPrefix = oldMenu.getPcodes() + "[" + oldMenu.getCode() + "],";
String oldPcodesSuffix = menu.getPcodes().substring(oldPcodesPrefix.length());
String menuPcodes = newMenu.getPcodes() + "[" + newMenu.getCode() + "]," + oldPcodesSuffix;
menu.setPcodes(menuPcodes);
//更新levels
int level = StrUtil.count(menuPcodes, "[");
menu.setLevels(level);
//更新systemType
menu.setSystemType(newMenu.getSystemType());
this.updateById(menu);
}
}
/**
* 删除菜单
*
* @author stylefeng
* @Date 2017/5/5 22:20
*/
@Transactional
public void delMenu(Long menuId) {
//删除菜单
this.restMenuMapper.deleteById(menuId);
//删除关联的relation
this.restMenuMapper.deleteRelationByMenu(menuId);
}
/**
* 删除菜单包含所有子菜单
*
* @author stylefeng
* @Date 2017/6/13 22:02
*/
@Transactional
public void delMenuContainSubMenus(Long menuId) {
RestMenu menu = restMenuMapper.selectById(menuId);
//删除当前菜单
delMenu(menuId);
//删除所有子菜单
List menus = restMenuMapper.getMenusLikePcodes(menu.getCode());
for (RestMenu temp : menus) {
delMenu(temp.getMenuId());
}
}
/**
* 根据条件查询菜单
*
* @return
* @date 2017年2月12日 下午9:14:34
*/
public Page