diff --git a/xymanager_admin/src/main/java/com/shxy/xymanager_admin/controller/RoleController.java b/xymanager_admin/src/main/java/com/shxy/xymanager_admin/controller/RoleController.java index 24e7dcf..5987b94 100644 --- a/xymanager_admin/src/main/java/com/shxy/xymanager_admin/controller/RoleController.java +++ b/xymanager_admin/src/main/java/com/shxy/xymanager_admin/controller/RoleController.java @@ -6,6 +6,7 @@ import com.shxy.xymanager_common.bean.ServiceBody; import com.shxy.xymanager_common.bean.ServiceStatus; import com.shxy.xymanager_common.entity.TbPermission; import com.shxy.xymanager_common.entity.TbRole; +import com.shxy.xymanager_common.exception.ApiException; import com.shxy.xymanager_common.model.DyLineTreeListModel; import com.shxy.xymanager_common.model.PermissionDyLineTreeListModel; import com.shxy.xymanager_common.model.PermissionModel; @@ -48,7 +49,7 @@ public class RoleController extends BaseController { @ApiOperation("更新") public ResponseReult update(@Validated @RequestBody TbRole item) throws Exception { if (item.getId() == null) { - throw new Exception("ID不能为空!"); + throw new ApiException("id不能为空!"); } service.update(item); return ResponseReult.success("OK"); @@ -56,14 +57,17 @@ public class RoleController extends BaseController { @PostMapping("delete") @ApiOperation("删除") - public ResponseReult delete(@Validated @NotNull(message = "ID不能为空!") Integer id) throws Exception { + public ResponseReult delete(@Validated @NotNull(message = "id不能为空!") Integer id) throws Exception { + if (id == null) { + throw new ApiException("id不能为空!"); + } service.delete(id); return ResponseReult.success("OK"); } @GetMapping("getPermission") @ApiOperation("查询权限") - public ResponseReult> getPermission(@Validated @NotNull(message = "ID不能为空!") Integer id) throws Exception { + public ResponseReult> getPermission(@Validated @NotNull(message = "id不能为空!") Integer id) throws Exception { List result = service.getPermission(id); return ResponseReult.success(result); } diff --git a/xymanager_common/src/main/java/com/shxy/xymanager_common/constant/Constants.java b/xymanager_common/src/main/java/com/shxy/xymanager_common/constant/Constants.java index de55f17..13ba7b2 100644 --- a/xymanager_common/src/main/java/com/shxy/xymanager_common/constant/Constants.java +++ b/xymanager_common/src/main/java/com/shxy/xymanager_common/constant/Constants.java @@ -203,5 +203,6 @@ public class Constants { * 超级管理员 */ public static int SUPER_ADMIN = 0; + public static String SUPER_ADMIN_NAME = "超级管理员"; } \ No newline at end of file diff --git a/xymanager_service/src/main/java/com/shxy/xymanager_service/impl/RoleServiceImpl.java b/xymanager_service/src/main/java/com/shxy/xymanager_service/impl/RoleServiceImpl.java index 1269374..9b3195d 100644 --- a/xymanager_service/src/main/java/com/shxy/xymanager_service/impl/RoleServiceImpl.java +++ b/xymanager_service/src/main/java/com/shxy/xymanager_service/impl/RoleServiceImpl.java @@ -28,6 +28,7 @@ import java.util.Date; import java.util.List; import static com.shxy.xymanager_common.constant.Constants.SUPER_ADMIN; +import static com.shxy.xymanager_common.constant.Constants.SUPER_ADMIN_NAME; @Service @Slf4j @@ -47,11 +48,25 @@ public class RoleServiceImpl implements RoleService { TbRoleExample example = new TbRoleExample(); TbRoleExample.Criteria criteria = example.createCriteria(); List list = roleMapper.selectByExample(example); + TbRole item = new TbRole(); + item.setId(SUPER_ADMIN); + item.setName(SUPER_ADMIN_NAME); + list.add(0, item); return list; } @Override public boolean add(TbRole item) throws Exception { + if (SUPER_ADMIN_NAME.equals(item.getName())) { + throw new ApiException("名称不能使用" + SUPER_ADMIN_NAME); + } + TbRoleExample example = new TbRoleExample(); + TbRoleExample.Criteria criteria = example.createCriteria(); + criteria.andNameEqualTo(item.getName()); + List list = roleMapper.selectByExample(example); + if (list.size() > 0) { + throw new ApiException("名称已存在"); + } item.setCreateTime(new Date()); int r = roleMapper.insertSelective(item); return r > 0; @@ -59,8 +74,19 @@ public class RoleServiceImpl implements RoleService { @Override public void update(TbRole item) throws Exception { + if (SUPER_ADMIN_NAME.equals(item.getName())) { + throw new ApiException("名称不能使用" + SUPER_ADMIN_NAME); + } if (item.getId() == SUPER_ADMIN) { - throw new ApiException("不能修改超级管理员"); + throw new ApiException("不能修改" + SUPER_ADMIN_NAME); + } + TbRoleExample example = new TbRoleExample(); + TbRoleExample.Criteria criteria = example.createCriteria(); + criteria.andNameEqualTo(item.getName()); + criteria.andIdNotEqualTo(item.getId()); + List list = roleMapper.selectByExample(example); + if (list.size() > 0) { + throw new ApiException("名称已存在"); } roleMapper.updateByPrimaryKey(item); } @@ -68,9 +94,13 @@ public class RoleServiceImpl implements RoleService { @Override public void delete(Integer id) throws Exception { if (id == SUPER_ADMIN) { - throw new ApiException("不能删除超级管理员"); + throw new ApiException("不能删除" + SUPER_ADMIN_NAME); } roleMapper.deleteByPrimaryKey(id); + TbPermissionExample example = new TbPermissionExample(); + TbPermissionExample.Criteria criteria = example.createCriteria(); + criteria.andRoleIdEqualTo(id); + permissionMapper.deleteByExample(example); } @Override @@ -112,7 +142,7 @@ public class RoleServiceImpl implements RoleService { @Override public void changePermission(Integer roleId, List list) throws Exception { if (roleId == SUPER_ADMIN) { - throw new ApiException("不能修改超级管理员的权限"); + throw new ApiException("不能修改" + SUPER_ADMIN_NAME + "的权限"); } TbPermissionExample example = new TbPermissionExample(); TbPermissionExample.Criteria criteria = example.createCriteria();