【menu】更新菜单和按钮的逻辑

pull/3/head
fengshuonan 2021-01-09 17:02:55 +08:00
parent 31e5d2d12f
commit 5c7d5159d5
8 changed files with 58 additions and 206 deletions

View File

@ -11,17 +11,11 @@ import cn.stylefeng.roses.kernel.system.constants.SystemConstants;
* @date 2021/1/9 11:32
*/
public enum SysMenuButtonExceptionEnum implements AbstractExceptionEnum {
/**
* code
*/
BUTTON_CODE_REPEAT(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "90", "按钮code重复请检查buttonCode参数"),
/**
*
*/
MENU_BUTTON_NOT_EXIST(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "91", "菜单按钮不存在"),
;
MENU_BUTTON_NOT_EXIST(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "91", "菜单按钮不存在");
private final String errorCode;

View File

@ -20,52 +20,7 @@ public enum SysMenuExceptionEnum implements AbstractExceptionEnum {
/**
*
*/
MENU_NOT_EXIST(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "81", "菜单不存在"),
/**
*
*/
MENU_CODE_REPEAT(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "82", "菜单编码重复请检查code参数"),
/**
*
*/
MENU_NAME_REPEAT(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "83", "菜单名称重复请检查name参数"),
/**
*
*/
MENU_ROUTER_EMPTY(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "84", "路由地址为空请检查router参数"),
/**
*
*/
MENU_COMPONENT_EMPTY(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "85", "组件地址为空请检查component参数"),
/**
*
*/
MENU_OPEN_TYPE_EMPTY(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "86", "打开方式为空请检查openType参数"),
/**
*
*/
MENU_PERMISSION_EMPTY(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "87", "权限标识为空请检查permission参数"),
/**
*
*/
MENU_PERMISSION_ERROR(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "88", "权限标识格式错误请检查permission参数"),
/**
*
*/
MENU_PERMISSION_NOT_EXIST(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "89", "权限不存在请检查permission参数"),
/**
*
*/
PID_CANT_EQ_ID(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "811", "父级菜单不能为当前节点,请从新选择父级菜单");
MENU_NOT_EXIST(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "81", "菜单不存在");
private final String errorCode;

View File

@ -1,6 +1,7 @@
package cn.stylefeng.roses.kernel.system.pojo.menu;
import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest;
import cn.stylefeng.roses.kernel.validator.validators.unique.TableUniqueValue;
import lombok.Data;
import lombok.EqualsAndHashCode;
@ -47,6 +48,13 @@ public class SysMenuButtonRequest extends BaseRequest {
*
*/
@NotBlank(message = "按钮编码不能为空请检查buttonCode参数", groups = {add.class, edit.class})
@TableUniqueValue(
message = "按钮编码存在重复",
groups = {add.class, edit.class},
tableName = "sys_menu_button",
columnName = "button_code",
idFieldName = "button_id",
excludeLogicDeleteItems = true)
private String buttonCode;
/**

View File

@ -1,38 +0,0 @@
package cn.stylefeng.roses.kernel.system.pojo.menu;
import lombok.Data;
import java.io.Serializable;
/**
*
*
* @author luojie
* @date 2021/1/9 11:19
*/
@Data
public class SysMenuButtonResponse implements Serializable {
private static final long serialVersionUID = 6909784065876106042L;
/**
*
*/
private Long buttonId;
/**
* id
*/
private Long menuId;
/**
*
*/
private String buttonName;
/**
*
*/
private String buttonCode;
}

View File

@ -1,6 +1,7 @@
package cn.stylefeng.roses.kernel.menu.modular.controller;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.menu.modular.entity.SysMenuButton;
import cn.stylefeng.roses.kernel.menu.modular.service.SysMenuButtonService;
import cn.stylefeng.roses.kernel.resource.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.resource.api.annotation.GetResource;
@ -8,7 +9,6 @@ import cn.stylefeng.roses.kernel.resource.api.annotation.PostResource;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.system.pojo.menu.SysMenuButtonRequest;
import cn.stylefeng.roses.kernel.system.pojo.menu.SysMenuButtonResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@ -50,7 +50,7 @@ public class SysMenuButtonController {
*/
@GetResource(name = "获取菜单按钮详情", path = "/sysMenuButton/detail")
public ResponseData detail(@Validated(SysMenuButtonRequest.detail.class) SysMenuButtonRequest sysMenuButtonRequest) {
SysMenuButtonResponse detail = sysMenuButtonService.detail(sysMenuButtonRequest);
SysMenuButton detail = sysMenuButtonService.detail(sysMenuButtonRequest);
return new SuccessResponseData(detail);
}
@ -102,7 +102,7 @@ public class SysMenuButtonController {
*/
@GetResource(name = "获取菜单按钮列表", path = "/sysMenuButton/pageList")
public ResponseData pageList(@Validated(SysMenuButtonRequest.list.class) SysMenuButtonRequest sysMenuButtonRequest) {
PageResult<SysMenuButtonResponse> pageResult = sysMenuButtonService.pageList(sysMenuButtonRequest);
PageResult<SysMenuButton> pageResult = sysMenuButtonService.pageList(sysMenuButtonRequest);
return new SuccessResponseData(pageResult);
}

View File

@ -3,7 +3,6 @@ package cn.stylefeng.roses.kernel.menu.modular.service;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.menu.modular.entity.SysMenuButton;
import cn.stylefeng.roses.kernel.system.pojo.menu.SysMenuButtonRequest;
import cn.stylefeng.roses.kernel.system.pojo.menu.SysMenuButtonResponse;
import com.baomidou.mybatisplus.extension.service.IService;
/**
@ -31,7 +30,7 @@ public interface SysMenuButtonService extends IService<SysMenuButton> {
* @author luojie
* @date 2021/1/9 11:53
*/
SysMenuButtonResponse detail(SysMenuButtonRequest sysMenuButtonRequest);
SysMenuButton detail(SysMenuButtonRequest sysMenuButtonRequest);
/**
*
@ -68,7 +67,7 @@ public interface SysMenuButtonService extends IService<SysMenuButton> {
* @author luojie
* @date 2021/1/9 12:53
*/
PageResult<SysMenuButtonResponse> pageList(SysMenuButtonRequest sysMenuButtonRequest);
PageResult<SysMenuButton> pageList(SysMenuButtonRequest sysMenuButtonRequest);
/**
* id
@ -78,4 +77,5 @@ public interface SysMenuButtonService extends IService<SysMenuButton> {
* @date 2021/1/9 14:45
*/
void deleteMenuButtonByMenuId(Long menuId);
}

View File

@ -7,17 +7,14 @@ import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.menu.modular.entity.SysMenu;
import cn.stylefeng.roses.kernel.menu.modular.entity.SysMenuButton;
import cn.stylefeng.roses.kernel.menu.modular.mapper.SysMenuButtonMapper;
import cn.stylefeng.roses.kernel.menu.modular.service.SysMenuButtonService;
import cn.stylefeng.roses.kernel.menu.modular.service.SysMenuService;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.roses.kernel.system.exception.SystemModularException;
import cn.stylefeng.roses.kernel.system.exception.enums.SysMenuButtonExceptionEnum;
import cn.stylefeng.roses.kernel.system.exception.enums.SysMenuExceptionEnum;
import cn.stylefeng.roses.kernel.system.pojo.menu.SysMenuButtonRequest;
import cn.stylefeng.roses.kernel.system.pojo.menu.SysMenuButtonResponse;
import cn.stylefeng.roses.kernel.system.pojo.menu.SysMenuRequest;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@ -26,9 +23,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* service
@ -46,80 +41,57 @@ public class SysMenuButtonServiceImpl extends ServiceImpl<SysMenuButtonMapper, S
@Transactional(rollbackFor = Exception.class)
public void add(SysMenuButtonRequest sysMenuButtonRequest) {
Long menuId = sysMenuButtonRequest.getMenuId();
// 根据传过来的菜单id查询是否存在
findMenuExist(menuId);
// 查询菜单按钮code是否存在
findMenuButtonCodeExist(sysMenuButtonRequest);
// 判断菜单是否存在
findMenuExist(sysMenuButtonRequest.getMenuId());
SysMenuButton sysMenuButton = new SysMenuButton();
BeanUtil.copyProperties(sysMenuButtonRequest, sysMenuButton);
// 设置未删除
sysMenuButton.setDelFlag(YesOrNotEnum.N.getCode());
this.save(sysMenuButton);
}
@Override
public SysMenuButtonResponse detail(SysMenuButtonRequest sysMenuButtonRequest) {
// 构建查询条件 查询未删除的按钮
LambdaQueryWrapper<SysMenuButton> lqw = new LambdaQueryWrapper<>();
lqw.eq(SysMenuButton::getButtonId, sysMenuButtonRequest.getButtonId());
lqw.eq(SysMenuButton::getDelFlag, YesOrNotEnum.N.getCode());
lqw.last("LIMIT 1");
SysMenuButton sysMenuButton = this.getOne(lqw);
if (ObjectUtil.isEmpty(sysMenuButton)) {
throw new ServiceException(SysMenuButtonExceptionEnum.MENU_BUTTON_NOT_EXIST);
}
SysMenuButtonResponse response = new SysMenuButtonResponse();
BeanUtil.copyProperties(sysMenuButton, response);
return response;
public SysMenuButton detail(SysMenuButtonRequest sysMenuButtonRequest) {
return this.queryButton(sysMenuButtonRequest);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void edit(SysMenuButtonRequest sysMenuButtonRequest) {
Long menuId = sysMenuButtonRequest.getMenuId();
// 查询按钮是否存在
this.detail(sysMenuButtonRequest);
// 查询按钮所属菜单是否存在
findMenuExist(sysMenuButtonRequest.getMenuId());
// 根据传过来的菜单id查询是否存在
findMenuExist(menuId);
SysMenuButton button = this.queryButton(sysMenuButtonRequest);
BeanUtil.copyProperties(sysMenuButtonRequest, button);
SysMenuButton sysMenuButton = new SysMenuButton();
BeanUtil.copyProperties(sysMenuButtonRequest, sysMenuButton);
// 不更新删除状态
sysMenuButton.setDelFlag(null);
// 不更新所属菜单id
sysMenuButton.setMenuId(null);
// 不更新菜单code
sysMenuButton.setButtonCode(null);
button.setDelFlag(null);
this.updateById(sysMenuButton);
// 不更新所属菜单id
button.setMenuId(null);
// 不更新菜单code
button.setButtonCode(null);
this.updateById(button);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void delete(SysMenuButtonRequest sysMenuButtonRequest) {
// 查询按钮是否存在
this.detail(sysMenuButtonRequest);
// 查询条件
LambdaQueryWrapper<SysMenuButton> lqw = new LambdaQueryWrapper<>();
lqw.eq(SysMenuButton::getButtonId, sysMenuButtonRequest.getButtonId());
lqw.eq(SysMenuButton::getDelFlag, YesOrNotEnum.N.getCode());
// 查询按钮
SysMenuButton button = this.queryButton(sysMenuButtonRequest);
// 设置为删除状态
SysMenuButton entity = new SysMenuButton();
entity.setDelFlag(YesOrNotEnum.Y.getCode());
button.setDelFlag(YesOrNotEnum.Y.getCode());
this.update(entity, lqw);
this.updateById(button);
}
@Override
@ -128,29 +100,20 @@ public class SysMenuButtonServiceImpl extends ServiceImpl<SysMenuButtonMapper, S
Set<Long> buttonIds = sysMenuButtonRequest.getButtonIds();
if (ArrayUtil.isNotEmpty(buttonIds)) {
// 查询条件
LambdaQueryWrapper<SysMenuButton> lqw = new LambdaQueryWrapper<>();
lqw.in(SysMenuButton::getButtonId, buttonIds);
lqw.eq(SysMenuButton::getDelFlag, YesOrNotEnum.N.getCode());
LambdaQueryWrapper<SysMenuButton> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.in(SysMenuButton::getButtonId, buttonIds);
lambdaQueryWrapper.eq(SysMenuButton::getDelFlag, YesOrNotEnum.N.getCode());
// 设置为删除状态
SysMenuButton entity = new SysMenuButton();
entity.setDelFlag(YesOrNotEnum.Y.getCode());
this.update(entity, lqw);
this.update(entity, lambdaQueryWrapper);
}
}
/**
*
*
* @param sysMenuButtonRequest id
* @return
* @author luojie
* @date 2021/1/9 12:53
*/
@Override
public PageResult<SysMenuButtonResponse> pageList(SysMenuButtonRequest sysMenuButtonRequest) {
public PageResult<SysMenuButton> pageList(SysMenuButtonRequest sysMenuButtonRequest) {
String buttonName = sysMenuButtonRequest.getButtonName();
// 构造查询条件 根据菜单id查询菜单按钮列表
@ -162,27 +125,9 @@ public class SysMenuButtonServiceImpl extends ServiceImpl<SysMenuButtonMapper, S
wrapper.eq(SysMenuButton::getDelFlag, YesOrNotEnum.N.getCode());
Page<SysMenuButton> page = this.page(PageFactory.defaultPage(), wrapper);
// 实体转换
List<SysMenuButton> sysButtonList = page.getRecords();
List<SysMenuButtonResponse> sysButtonResponseList = sysButtonList.stream().map(b -> {
SysMenuButtonResponse sysButtonResponse = new SysMenuButtonResponse();
BeanUtil.copyProperties(b, sysButtonResponse);
return sysButtonResponse;
}).collect(Collectors.toList());
PageResult<SysMenuButtonResponse> pageResult = new PageResult<>();
BeanUtil.copyProperties(PageResultFactory.createPageResult(page), pageResult);
pageResult.setRows(sysButtonResponseList);
return pageResult;
return PageResultFactory.createPageResult(page);
}
/**
* id
*
* @param menuId id
* @author luojie
* @date 2021/1/9 14:45
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteMenuButtonByMenuId(Long menuId) {
@ -204,42 +149,30 @@ public class SysMenuButtonServiceImpl extends ServiceImpl<SysMenuButtonMapper, S
}
/**
*
*
*
* @param menuId id
* @author luojie
* @date 2021/1/9 12:10
*/
public void findMenuExist(Long menuId) {
// 根据传过来的菜单id查询是否存在
private void findMenuExist(Long menuId) {
SysMenuRequest sysMenuRequest = new SysMenuRequest();
sysMenuRequest.setMenuId(menuId);
SysMenu sysMenu = sysMenuService.detail(sysMenuRequest);
if (ObjectUtil.isEmpty(sysMenu)) {
throw new ServiceException(SysMenuExceptionEnum.MENU_NOT_EXIST);
}
sysMenuService.detail(sysMenuRequest);
}
/**
* code
*
*
* @param sysMenuButtonRequest
* @author luojie
* @date 2021/1/9 11:38
* @author fengshuonan
* @date 2020/3/27 9:13
*/
public void findMenuButtonCodeExist(SysMenuButtonRequest sysMenuButtonRequest) {
// 查询菜单按钮code是否存在
LambdaQueryWrapper<SysMenuButton> wrapper = new LambdaQueryWrapper<>();
wrapper.select(SysMenuButton::getButtonId);
wrapper.eq(SysMenuButton::getButtonCode, sysMenuButtonRequest.getButtonCode());
wrapper.eq(SysMenuButton::getDelFlag, YesOrNotEnum.N.getCode());
if (ObjectUtil.isNotEmpty(sysMenuButtonRequest.getButtonId())) {
wrapper.ne(SysMenuButton::getButtonId, sysMenuButtonRequest.getButtonId());
}
wrapper.last("LIMIT 1");
SysMenuButton button = getOne(wrapper);
if (ObjectUtil.isNotEmpty(button)) {
throw new ServiceException(SysMenuButtonExceptionEnum.BUTTON_CODE_REPEAT);
private SysMenuButton queryButton(SysMenuButtonRequest sysMenuButtonRequest) {
SysMenuButton button = this.getById(sysMenuButtonRequest.getButtonId());
if (ObjectUtil.isNull(button) || YesOrNotEnum.Y.getCode().equals(button.getDelFlag())) {
throw new SystemModularException(SysMenuButtonExceptionEnum.MENU_BUTTON_NOT_EXIST);
}
return button;
}
}

View File

@ -346,7 +346,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
*/
private SysMenu querySysMenu(SysMenuRequest sysMenuRequest) {
SysMenu sysMenu = this.getById(sysMenuRequest.getMenuId());
if (ObjectUtil.isNull(sysMenu)) {
if (ObjectUtil.isNull(sysMenu) || YesOrNotEnum.Y.getCode().equals(sysMenu.getDelFlag())) {
throw new ServiceException(SysMenuExceptionEnum.MENU_NOT_EXIST);
}
return sysMenu;