资源添加开发完成

This commit is contained in:
YunaiV
2019-02-28 23:11:59 +08:00
parent f5b105973d
commit b1248d7e2a
13 changed files with 386 additions and 59 deletions

View File

@@ -1,6 +1,8 @@
package cn.iocoder.mall.admin.convert;
import cn.iocoder.mall.admin.api.bo.ResourceBO;
import cn.iocoder.mall.admin.api.dto.ResourceAddDTO;
import cn.iocoder.mall.admin.api.dto.ResourceUpdateDTO;
import cn.iocoder.mall.admin.dataobject.ResourceDO;
import org.mapstruct.Mapper;
import org.mapstruct.Mappings;
@@ -19,4 +21,10 @@ public interface ResourceConvert {
@Mappings({})
List<ResourceBO> convert(List<ResourceDO> resourceDOs);
@Mappings({})
ResourceDO convert(ResourceAddDTO resourceAddDTO);
@Mappings({})
ResourceDO convert(ResourceUpdateDTO resourceUpdateDTO);
}

View File

@@ -16,4 +16,13 @@ public interface ResourceMapper {
List<ResourceDO> selectListByTypeAndRoleIds(@Param("type") Integer type,
@Param("roleIds") Set<Integer> roleIds);
List<ResourceDO> selectListByType(@Param("type") Integer type);
ResourceDO selectByName(@Param("name") String name);
ResourceDO selectById(@Param("id") Integer id);
void insert(ResourceDO resource);
int update(ResourceDO resource);
}

View File

@@ -1,9 +1,15 @@
package cn.iocoder.mall.admin.service;
import cn.iocoder.common.framework.constant.SysErrorCodeEnum;
import cn.iocoder.common.framework.dataobject.BaseDO;
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.admin.api.ResourceService;
import cn.iocoder.mall.admin.api.bo.ResourceBO;
import cn.iocoder.mall.admin.api.constant.AdminErrorCodeEnum;
import cn.iocoder.mall.admin.api.constant.ResourceConstants;
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.dataobject.ResourceDO;
@@ -11,6 +17,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;
@@ -26,7 +33,7 @@ public class ResourceServiceImpl implements ResourceService {
}
@Override
public List<ResourceBO> getResourceByTypeAndRoleIds(Integer type, Set<Integer> roleIds) {
public List<ResourceBO> getResourcesByTypeAndRoleIds(Integer type, Set<Integer> roleIds) {
if (roleIds == null || roleIds.isEmpty()) {
return Collections.emptyList();
}
@@ -34,8 +41,100 @@ public class ResourceServiceImpl implements ResourceService {
}
@Override
public CommonResult<ResourceBO> addResource(ResourceAddDTO resourceAddDTO) {
return null;
public List<ResourceBO> getResourcesByType(Integer type) {
return ResourceConvert.INSTANCE.convert(resourceMapper.selectListByType (type));
}
@Override
@SuppressWarnings("Duplicates")
public CommonResult<ResourceBO> addResource(Integer adminId, ResourceAddDTO resourceAddDTO) {
// 补充未在 Validation 中校验的参数校验
if (!isValidResourceType(resourceAddDTO.getType())) {
return CommonResult.error(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "资源类型必须是菜单或 Url"); // TODO 有点搓
}
// 校验资源唯一性
if (resourceMapper.selectByName(resourceAddDTO.getName()) != null) {
return ServiceExceptionUtil.error(AdminErrorCodeEnum.RESOURCE_NAME_DUPLICATE.getCode());
}
// 校验父资源存在
if (resourceAddDTO.getPid() == null) {
resourceAddDTO.setPid(ResourceConstants.PID_ROOT);
}
if (checkParentExists(resourceAddDTO.getPid())) {
return ServiceExceptionUtil.error(AdminErrorCodeEnum.RESOURCE_PARENT_NOT_EXISTS.getCode());
}
// 存储到数据库
ResourceDO resource = ResourceConvert.INSTANCE.convert(resourceAddDTO);
if (ResourceConstants.PID_ROOT.equals(resourceAddDTO.getPid())) { // 根节点,必须没有操作
resource.setHandler(null);
}
resource.setCreateTime(new Date());
resource.setDeleted(BaseDO.DELETED_NO);
resourceMapper.insert(resource);
// TODO 操作日志
// 返回成功
return CommonResult.success(ResourceConvert.INSTANCE.convert(resource));
}
@Override
@SuppressWarnings("Duplicates")
public CommonResult<Boolean> updateResource(Integer adminId, ResourceUpdateDTO resourceUpdateDTO) {
// 校验更新的资源是否存在
if (resourceMapper.selectById(resourceUpdateDTO.getId()) == null) {
return ServiceExceptionUtil.error(AdminErrorCodeEnum.RESOURCE_NOT_EXISTS.getCode());
}
// 校验资源唯一性
ResourceDO existNameResource = resourceMapper.selectByName(resourceUpdateDTO.getName());
if (existNameResource != null && existNameResource.getId().equals(resourceUpdateDTO.getId())) {
return ServiceExceptionUtil.error(AdminErrorCodeEnum.RESOURCE_NAME_DUPLICATE.getCode());
}
// 不能设置自己为父资源
if (resourceUpdateDTO.getId().equals(resourceUpdateDTO.getPid())) {
return ServiceExceptionUtil.error(AdminErrorCodeEnum.RESOURCE_PARENT_ERROR.getCode());
}
// 校验父资源存在
if (resourceUpdateDTO.getPid() == null) {
resourceUpdateDTO.setPid(ResourceConstants.PID_ROOT);
}
if (checkParentExists(resourceUpdateDTO.getPid())) {
return ServiceExceptionUtil.error(AdminErrorCodeEnum.RESOURCE_PARENT_NOT_EXISTS.getCode());
}
// 更新到数据库
ResourceDO resource = ResourceConvert.INSTANCE.convert(resourceUpdateDTO);
if (ResourceConstants.PID_ROOT.equals(resourceUpdateDTO.getPid())) { // 根节点,必须没有操作
resource.setHandler(null);
}
resourceMapper.update(resource);
// TODO 操作日志
// 返回成功
return CommonResult.success(true);
}
@Override
public CommonResult<Boolean> deleteResource(Integer adminId, Integer resourceId) {
// 校验更新的资源是否存在
if (resourceMapper.selectById(resourceId) == null) {
return ServiceExceptionUtil.error(AdminErrorCodeEnum.RESOURCE_NOT_EXISTS.getCode());
}
// TODO 还有校验
// 更新到数据库
ResourceDO resource = new ResourceDO().setId(resourceId);
resource.setDeleted(BaseDO.DELETED_YES);
resourceMapper.update(resource);
// 返回成功
return CommonResult.success(true);
}
private boolean isValidResourceType(Integer type) {
return ResourceConstants.TYPE_MENU.equals(type)
|| ResourceConstants.TYPE_URL.equals(type);
}
private boolean checkParentExists(Integer pid) {
if (!ResourceConstants.PID_ROOT.equals(pid)) {
return resourceMapper.selectById(pid) == null;
}
return false;
}
}

View File

@@ -10,28 +10,92 @@
<!--)-->
<!--</insert>-->
<select id="selectByTypeAndHandler" resultType="ResourceDO">
SELECT
<sql id="FIELDS">
id, name, type, sort, display_name,
create_time, pid, handler
FROM resource
WHERE type = #{type}
AND handler = #{handler}
AND deleted = 0
</sql>
<select id="selectByTypeAndHandler" resultType="ResourceDO">
SELECT
<include refid="FIELDS"/>
FROM resource
WHERE type = #{type}
AND handler = #{handler}
AND deleted = 0
LIMIT 1
</select>
<select id="selectListByType" parameterType="Integer" resultType="ResourceDO">
SELECT
<include refid="FIELDS"/>
FROM resource
WHERE type = #{type}
AND deleted = 0
</select>
<select id="selectListByTypeAndRoleIds" resultType="ResourceDO">
SELECT
r.id, r.name, r.type, r.sort, r.display_name,
r.create_time, r.pid, r.handler
FROM resource r, role_resource rr
WHERE r.type = #{type}
AND deleted = 0
AND rr.role_id IN
SELECT
r.id, r.name, r.type, r.sort, r.display_name,
r.create_time, r.pid, r.handler
FROM resource r, role_resource rr
WHERE r.type = #{type}
AND deleted = 0
AND rr.role_id IN
<foreach item="roleId" collection="roleIds" separator="," open="(" close=")" index="">
#{roleId}
#{roleId}
</foreach>
AND r.id = rr.resource_id
AND r.id = rr.resource_id
</select>
<select id="selectByName" parameterType="String" resultType="ResourceDO">
SELECT
<include refid="FIELDS"/>
FROM resource
WHERE name = #{name}
AND deleted = 0
LIMIT 1
</select>
<select id="selectById" resultType="ResourceDO">
SELECT
<include refid="FIELDS"/>
FROM resource
WHERE id = #{id}
AND deleted = 0
</select>
<insert id="insert" parameterType="ResourceDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
INSERT INTO resource (
name, type, sort, display_name, handler,
pid, create_time, deleted
) VALUES (
#{name}, #{type}, #{sort}, #{displayName}, #{handler},
#{pid}, #{createTime}, #{deleted}
)
</insert>
<update id="update" parameterType="ResourceDO">
UPDATE resource
<set>
<if test="name != null">
name = #{name},
</if>
<if test="sort != null">
sort = #{sort},
</if>
<if test="displayName != null">
display_name = #{displayName},
</if>
<if test="pid != null">
pid = #{pid},
</if>
<if test="handler != null">
handler = #{handler},
</if>
<if test="deleted != null">
deleted = #{deleted}
</if>
</set>
WHERE id = #{id}
</update>
</mapper>