From a88afbf9c03055c1cadaa283e8eefc22bbc997f8 Mon Sep 17 00:00:00 2001 From: YunaiV <> Date: Thu, 28 Feb 2019 23:45:51 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B5=84=E6=BA=90=E5=88=A0=E9=99=A4=E5=AE=8C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/constant/AdminErrorCodeEnum.java | 3 ++- .../mall/admin/dao/ResourceMapper.java | 3 +++ .../mall/admin/dao/RoleResourceMapper.java | 6 +++++- .../mall/admin/dataobject/RoleResourceDO.java | 19 ++----------------- .../admin/service/ResourceServiceImpl.java | 11 ++++++++++- .../mall/admin/service/RoleServiceImpl.java | 2 +- .../main/resources/mapper/ResourceMapper.xml | 8 ++++++++ .../resources/mapper/RoleResourceMapper.xml | 18 +++++++++++++++++- 8 files changed, 48 insertions(+), 22 deletions(-) diff --git a/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/constant/AdminErrorCodeEnum.java b/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/constant/AdminErrorCodeEnum.java index ff053fb9e..1be91e3ca 100644 --- a/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/constant/AdminErrorCodeEnum.java +++ b/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/constant/AdminErrorCodeEnum.java @@ -29,7 +29,8 @@ public enum AdminErrorCodeEnum { RESOURCE_NAME_DUPLICATE(1002003000, "已经存在该名字的资源"), RESOURCE_PARENT_NOT_EXISTS(1002003001, "父资源不存在"), RESOURCE_PARENT_ERROR(1002003002, "不能设置自己为父资源"), - RESOURCE_NOT_EXISTS(1002003002, "资源不存在"), + RESOURCE_NOT_EXISTS(1002003003, "资源不存在"), + RESOURCE_EXISTS_CHILDREN(1002003004, "存在子资源,无法删除"), ; private final int code; diff --git a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/ResourceMapper.java b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/ResourceMapper.java index 0aaa79ba3..2201bc43c 100644 --- a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/ResourceMapper.java +++ b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/ResourceMapper.java @@ -25,4 +25,7 @@ public interface ResourceMapper { void insert(ResourceDO resource); int update(ResourceDO resource); + + int selectCountByPid(@Param("pid") Integer pid); + } \ No newline at end of file diff --git a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/RoleResourceMapper.java b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/RoleResourceMapper.java index 25daf33ce..7bcc9555b 100644 --- a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/RoleResourceMapper.java +++ b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/RoleResourceMapper.java @@ -11,6 +11,10 @@ public interface RoleResourceMapper { List selectByResourceHandler(@Param("resourceHandler") String resourceHandler); - List selectRoleByResourceId(@Param("resourceId") Integer resourceId); + List selectByResourceId(@Param("resourceId") Integer resourceId); + + int updateToDeletedByResourceId(@Param("resourceId") Integer resourceId); + + int updateToDeletedByRoleId(@Param("roleId") Integer roleId); } \ No newline at end of file diff --git a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dataobject/RoleResourceDO.java b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dataobject/RoleResourceDO.java index d8795b96a..09f77d475 100644 --- a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dataobject/RoleResourceDO.java +++ b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dataobject/RoleResourceDO.java @@ -1,11 +1,11 @@ package cn.iocoder.mall.admin.dataobject; -import java.util.Date; +import cn.iocoder.common.framework.dataobject.BaseDO; /** * {@link RoleDO} 和 {@link ResourceDO} 的关联表 */ -public class RoleResourceDO { +public class RoleResourceDO extends BaseDO { /** * 编号 @@ -19,12 +19,6 @@ public class RoleResourceDO { * 资源编号(外键:{@link ResourceDO} */ private Integer resourceId; - /** - * 创建时间 - */ - private Date createTime; - - // TODO 芋艿 删除状态 public Integer getId() { return id; @@ -44,15 +38,6 @@ public class RoleResourceDO { return this; } - public Date getCreateTime() { - return createTime; - } - - public RoleResourceDO setCreateTime(Date createTime) { - this.createTime = createTime; - return this; - } - public Integer getResourceId() { return resourceId; } diff --git a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/ResourceServiceImpl.java b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/ResourceServiceImpl.java index 733479862..f437159a0 100644 --- a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/ResourceServiceImpl.java +++ b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/ResourceServiceImpl.java @@ -12,6 +12,7 @@ import cn.iocoder.mall.admin.api.dto.ResourceAddDTO; import cn.iocoder.mall.admin.api.dto.ResourceUpdateDTO; import cn.iocoder.mall.admin.convert.ResourceConvert; import cn.iocoder.mall.admin.dao.ResourceMapper; +import cn.iocoder.mall.admin.dao.RoleResourceMapper; import cn.iocoder.mall.admin.dataobject.ResourceDO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -27,6 +28,9 @@ public class ResourceServiceImpl implements ResourceService { @Autowired private ResourceMapper resourceMapper; + @Autowired + private RoleResourceMapper roleResourceMapper; + public ResourceDO getResourceByTypeAndHandler(Integer type, String handler) { return resourceMapper.selectByTypeAndHandler(type, handler); @@ -116,11 +120,16 @@ public class ResourceServiceImpl implements ResourceService { if (resourceMapper.selectById(resourceId) == null) { return ServiceExceptionUtil.error(AdminErrorCodeEnum.RESOURCE_NOT_EXISTS.getCode()); } - // TODO 还有校验 + // 校验是否还有子资源 + if (resourceMapper.selectCountByPid(resourceId) > 0) { + return ServiceExceptionUtil.error(AdminErrorCodeEnum.RESOURCE_EXISTS_CHILDREN.getCode()); + } // 更新到数据库 ResourceDO resource = new ResourceDO().setId(resourceId); resource.setDeleted(BaseDO.DELETED_YES); resourceMapper.update(resource); + // 删除资源关联表 + roleResourceMapper.updateToDeletedByResourceId(resourceId); // 返回成功 return CommonResult.success(true); } diff --git a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/RoleServiceImpl.java b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/RoleServiceImpl.java index 70c2313ad..78f59bdb2 100644 --- a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/RoleServiceImpl.java +++ b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/RoleServiceImpl.java @@ -20,7 +20,7 @@ public class RoleServiceImpl implements RoleService { } public List getRoleByResourceId(Integer resourceId) { - return roleResourceMapper.selectRoleByResourceId(resourceId); + return roleResourceMapper.selectByResourceId(resourceId); } } \ No newline at end of file diff --git a/admin/admin-service-impl/src/main/resources/mapper/ResourceMapper.xml b/admin/admin-service-impl/src/main/resources/mapper/ResourceMapper.xml index fb829e8ce..2637df1fa 100644 --- a/admin/admin-service-impl/src/main/resources/mapper/ResourceMapper.xml +++ b/admin/admin-service-impl/src/main/resources/mapper/ResourceMapper.xml @@ -55,6 +55,7 @@ AND deleted = 0 LIMIT 1 + + + INSERT INTO resource ( name, type, sort, display_name, handler, diff --git a/admin/admin-service-impl/src/main/resources/mapper/RoleResourceMapper.xml b/admin/admin-service-impl/src/main/resources/mapper/RoleResourceMapper.xml index 7cebdd0fe..9ce9772b9 100644 --- a/admin/admin-service-impl/src/main/resources/mapper/RoleResourceMapper.xml +++ b/admin/admin-service-impl/src/main/resources/mapper/RoleResourceMapper.xml @@ -16,13 +16,29 @@ FROM resource r, role_resource rr WHERE r.handler = #{resourceHandler} AND r.id = rr.resource_id + AND rr.deleted = 0; - SELECT id, role_id, resource_id FROM role_resource WHERE resource_id = #{resourceId} + AND deleted = 0 + + UPDATE role_resource + SET deleted = 1 + WHERE resource_id = #{resourceId} + AND deleted = 0 + + + + UPDATE role_resource + SET deleted = 1 + WHERE role_id = #{roleId} + AND deleted = 0 + + \ No newline at end of file