Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yudao-cloud
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
hblj
yudao-cloud
Commits
30e2d087
提交
30e2d087
authored
2月 21, 2021
作者:
YunaiV
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
增加环境搭建文档
上级
4e926d8d
隐藏空白字符变更
内嵌
并排
正在显示
11 个修改的文件
包含
261 行增加
和
249 行删除
+261
-249
README.md
README.md
+5
-2
GlobalErrorCodeConstants.java
...n/framework/exception/enums/GlobalErrorCodeConstants.java
+0
-1
ServiceExceptionUtil.java
...common/framework/exception/util/ServiceExceptionUtil.java
+9
-9
RedisKeyDefine.java
.../main/java/cn/iocoder/mall/redis/core/RedisKeyDefine.java
+3
-3
DataDictSimpleVO.java
...anagementweb/controller/datadict/vo/DataDictSimpleVO.java
+0
-3
UserUpdateStatusReqVO.java
...nagementweb/controller/user/vo/UserUpdateStatusReqVO.java
+1
-0
PayTransactionService.java
...entweb/service/pay/transaction/PayTransactionService.java
+6
-1
PermissionManager.java
...l/systemservice/manager/permission/PermissionManager.java
+2
-2
ResourceManager.java
...all/systemservice/manager/permission/ResourceManager.java
+2
-2
ResourceService.java
...all/systemservice/service/permission/ResourceService.java
+3
-226
ResourceServiceImpl.java
...systemservice/service/permission/ResourceServiceImpl.java
+230
-0
没有找到文件。
README.md
浏览文件 @
30e2d087
...
@@ -26,7 +26,10 @@
...
@@ -26,7 +26,10 @@
> 一起交流,Get 知识。
> 一起交流,Get 知识。
*
我们迫切希望更多的参与进来,可以加入「交流群」,一起骚聊。
*
我们迫切希望更多的参与进来,可以加入「交流群」,一起骚聊。
*
[
《Onemall 电商开源项目 —— 应用分层》
](
http://www.iocoder.cn/Onemall/Application-layer/?vip&onemall
)
*
[
《Onemall 电商开源项目 —— 应用分层》
](
http://www.iocoder.cn/Onemall/Application-layer/?onemall
)
*
[
《Onemall 电商开源项目 —— 搭建调试环境》
](
http://www.iocoder.cn/Onemall/build-debugger-environment/?onemall
)
*
前端项目地址:
<https://github.com/YunaiV/onemall-web>
# 演示
# 演示
...
@@ -95,7 +98,7 @@
...
@@ -95,7 +98,7 @@
## 搭建环境
## 搭建环境
[
搭建调试环境
](
http
s://gitee.com/zhijiantianya/onemall/blob/master/docs/setup/quick-start.md
)
[
搭建调试环境
](
http
://www.iocoder.cn/Onemall/build-debugger-environment/
)
## 架构图
## 架构图
...
...
common/common-framework/src/main/java/cn/iocoder/common/framework/exception/enums/GlobalErrorCodeConstants.java
浏览文件 @
30e2d087
...
@@ -28,7 +28,6 @@ public interface GlobalErrorCodeConstants {
...
@@ -28,7 +28,6 @@ public interface GlobalErrorCodeConstants {
ErrorCode
UNKNOWN
=
new
ErrorCode
(
999
,
"未知错误"
);
ErrorCode
UNKNOWN
=
new
ErrorCode
(
999
,
"未知错误"
);
static
boolean
isMatch
(
Integer
code
)
{
static
boolean
isMatch
(
Integer
code
)
{
return
code
!=
null
return
code
!=
null
&&
code
>=
SUCCESS
.
getCode
()
&&
code
<=
UNKNOWN
.
getCode
();
&&
code
>=
SUCCESS
.
getCode
()
&&
code
<=
UNKNOWN
.
getCode
();
...
...
common/common-framework/src/main/java/cn/iocoder/common/framework/exception/util/ServiceExceptionUtil.java
浏览文件 @
30e2d087
...
@@ -15,7 +15,7 @@ import java.util.concurrent.ConcurrentMap;
...
@@ -15,7 +15,7 @@ import java.util.concurrent.ConcurrentMap;
* 目的在于,格式化异常信息提示。
* 目的在于,格式化异常信息提示。
* 考虑到 String.format 在参数不正确时会报错,因此使用 {} 作为占位符,并使用 {@link #doFormat(int, String, Object...)} 方法来格式化
* 考虑到 String.format 在参数不正确时会报错,因此使用 {} 作为占位符,并使用 {@link #doFormat(int, String, Object...)} 方法来格式化
*
*
* 因为 {@link #
messages
} 里面默认是没有异常信息提示的模板的,所以需要使用方自己初始化进去。目前想到的有几种方式:
* 因为 {@link #
MESSAGES
} 里面默认是没有异常信息提示的模板的,所以需要使用方自己初始化进去。目前想到的有几种方式:
*
*
* 1. 异常提示信息,写在枚举类中,例如说,cn.iocoder.oceans.user.api.constants.ErrorCodeEnum 类 + ServiceExceptionConfiguration
* 1. 异常提示信息,写在枚举类中,例如说,cn.iocoder.oceans.user.api.constants.ErrorCodeEnum 类 + ServiceExceptionConfiguration
* 2. 异常提示信息,写在 .properties 等等配置文件
* 2. 异常提示信息,写在 .properties 等等配置文件
...
@@ -29,29 +29,29 @@ public class ServiceExceptionUtil {
...
@@ -29,29 +29,29 @@ public class ServiceExceptionUtil {
/**
/**
* 错误码提示模板
* 错误码提示模板
*/
*/
private
static
ConcurrentMap
<
Integer
,
String
>
messages
=
new
ConcurrentHashMap
<>();
private
static
final
ConcurrentMap
<
Integer
,
String
>
MESSAGES
=
new
ConcurrentHashMap
<>();
public
static
void
putAll
(
Map
<
Integer
,
String
>
messages
)
{
public
static
void
putAll
(
Map
<
Integer
,
String
>
messages
)
{
ServiceExceptionUtil
.
messages
.
putAll
(
messages
);
ServiceExceptionUtil
.
MESSAGES
.
putAll
(
messages
);
}
}
public
static
void
put
(
Integer
code
,
String
message
)
{
public
static
void
put
(
Integer
code
,
String
message
)
{
ServiceExceptionUtil
.
messages
.
put
(
code
,
message
);
ServiceExceptionUtil
.
MESSAGES
.
put
(
code
,
message
);
}
}
public
static
void
delete
(
Integer
code
,
String
message
)
{
public
static
void
delete
(
Integer
code
,
String
message
)
{
ServiceExceptionUtil
.
messages
.
remove
(
code
,
message
);
ServiceExceptionUtil
.
MESSAGES
.
remove
(
code
,
message
);
}
}
// ========== 和 ServiceException 的集成 ==========
// ========== 和 ServiceException 的集成 ==========
public
static
ServiceException
exception
(
ErrorCode
errorCode
)
{
public
static
ServiceException
exception
(
ErrorCode
errorCode
)
{
String
messagePattern
=
messages
.
getOrDefault
(
errorCode
.
getCode
(),
errorCode
.
getMessage
());
String
messagePattern
=
MESSAGES
.
getOrDefault
(
errorCode
.
getCode
(),
errorCode
.
getMessage
());
return
exception0
(
errorCode
.
getCode
(),
messagePattern
);
return
exception0
(
errorCode
.
getCode
(),
messagePattern
);
}
}
public
static
ServiceException
exception
(
ErrorCode
errorCode
,
Object
...
params
)
{
public
static
ServiceException
exception
(
ErrorCode
errorCode
,
Object
...
params
)
{
String
messagePattern
=
messages
.
getOrDefault
(
errorCode
.
getCode
(),
errorCode
.
getMessage
());
String
messagePattern
=
MESSAGES
.
getOrDefault
(
errorCode
.
getCode
(),
errorCode
.
getMessage
());
return
exception0
(
errorCode
.
getCode
(),
messagePattern
,
params
);
return
exception0
(
errorCode
.
getCode
(),
messagePattern
,
params
);
}
}
...
@@ -62,7 +62,7 @@ public class ServiceExceptionUtil {
...
@@ -62,7 +62,7 @@ public class ServiceExceptionUtil {
* @return 异常
* @return 异常
*/
*/
public
static
ServiceException
exception
(
Integer
code
)
{
public
static
ServiceException
exception
(
Integer
code
)
{
return
exception0
(
code
,
messages
.
get
(
code
));
return
exception0
(
code
,
MESSAGES
.
get
(
code
));
}
}
/**
/**
...
@@ -73,7 +73,7 @@ public class ServiceExceptionUtil {
...
@@ -73,7 +73,7 @@ public class ServiceExceptionUtil {
* @return 异常
* @return 异常
*/
*/
public
static
ServiceException
exception
(
Integer
code
,
Object
...
params
)
{
public
static
ServiceException
exception
(
Integer
code
,
Object
...
params
)
{
return
exception0
(
code
,
messages
.
get
(
code
),
params
);
return
exception0
(
code
,
MESSAGES
.
get
(
code
),
params
);
}
}
public
static
ServiceException
exception0
(
Integer
code
,
String
messagePattern
,
Object
...
params
)
{
public
static
ServiceException
exception0
(
Integer
code
,
String
messagePattern
,
Object
...
params
)
{
...
...
common/mall-spring-boot-starter-redis/src/main/java/cn/iocoder/mall/redis/core/RedisKeyDefine.java
浏览文件 @
30e2d087
...
@@ -37,7 +37,7 @@ public class RedisKeyDefine {
...
@@ -37,7 +37,7 @@ public class RedisKeyDefine {
*
*
* 如果是使用分布式锁,设置为 {@link java.util.concurrent.locks.Lock} 类型
* 如果是使用分布式锁,设置为 {@link java.util.concurrent.locks.Lock} 类型
*/
*/
private
final
Class
valueType
;
private
final
Class
<?>
valueType
;
/**
/**
* 过期时间
* 过期时间
*
*
...
@@ -45,7 +45,7 @@ public class RedisKeyDefine {
...
@@ -45,7 +45,7 @@ public class RedisKeyDefine {
*/
*/
private
final
Duration
timeout
;
private
final
Duration
timeout
;
public
RedisKeyDefine
(
String
keyTemplate
,
KeyTypeEnum
keyType
,
Class
valueType
,
Duration
timeout
)
{
public
RedisKeyDefine
(
String
keyTemplate
,
KeyTypeEnum
keyType
,
Class
<?>
valueType
,
Duration
timeout
)
{
this
.
keyTemplate
=
keyTemplate
;
this
.
keyTemplate
=
keyTemplate
;
this
.
keyType
=
keyType
;
this
.
keyType
=
keyType
;
this
.
valueType
=
valueType
;
this
.
valueType
=
valueType
;
...
@@ -60,7 +60,7 @@ public class RedisKeyDefine {
...
@@ -60,7 +60,7 @@ public class RedisKeyDefine {
return
keyType
;
return
keyType
;
}
}
public
Class
getValueType
()
{
public
Class
<?>
getValueType
()
{
return
valueType
;
return
valueType
;
}
}
...
...
management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/datadict/vo/DataDictSimpleVO.java
浏览文件 @
30e2d087
...
@@ -11,13 +11,10 @@ import javax.validation.constraints.NotEmpty;
...
@@ -11,13 +11,10 @@ import javax.validation.constraints.NotEmpty;
public
class
DataDictSimpleVO
{
public
class
DataDictSimpleVO
{
@ApiModelProperty
(
value
=
"大类枚举值"
,
required
=
true
,
example
=
"gender"
)
@ApiModelProperty
(
value
=
"大类枚举值"
,
required
=
true
,
example
=
"gender"
)
@NotEmpty
(
message
=
"大类枚举值不能为空"
)
private
String
enumValue
;
private
String
enumValue
;
@ApiModelProperty
(
value
=
"小类数值"
,
required
=
true
,
example
=
"1"
)
@ApiModelProperty
(
value
=
"小类数值"
,
required
=
true
,
example
=
"1"
)
@NotEmpty
(
message
=
"小类数值不能为空"
)
private
String
value
;
private
String
value
;
@ApiModelProperty
(
value
=
"展示名"
,
required
=
true
,
example
=
"男"
)
@ApiModelProperty
(
value
=
"展示名"
,
required
=
true
,
example
=
"男"
)
@NotEmpty
(
message
=
"展示名不能为空"
)
private
String
displayName
;
private
String
displayName
;
}
}
management-web-app/src/main/java/cn/iocoder/mall/managementweb/controller/user/vo/UserUpdateStatusReqVO.java
浏览文件 @
30e2d087
...
@@ -15,6 +15,7 @@ public class UserUpdateStatusReqVO {
...
@@ -15,6 +15,7 @@ public class UserUpdateStatusReqVO {
@ApiModelProperty
(
value
=
"用户编号"
,
required
=
true
)
@ApiModelProperty
(
value
=
"用户编号"
,
required
=
true
)
@NotNull
(
message
=
"用户编号不能为空"
)
@NotNull
(
message
=
"用户编号不能为空"
)
private
Integer
userId
;
private
Integer
userId
;
@ApiModelProperty
(
value
=
"状态"
,
required
=
true
,
example
=
"1"
,
notes
=
"见 CommonStatusEnum 枚举"
)
@ApiModelProperty
(
value
=
"状态"
,
required
=
true
,
example
=
"1"
,
notes
=
"见 CommonStatusEnum 枚举"
)
@NotNull
(
message
=
"状态不能为空"
)
@NotNull
(
message
=
"状态不能为空"
)
@InEnum
(
value
=
CommonStatusEnum
.
class
,
message
=
"修改状态必须是 {value}"
)
@InEnum
(
value
=
CommonStatusEnum
.
class
,
message
=
"修改状态必须是 {value}"
)
...
...
management-web-app/src/main/java/cn/iocoder/mall/managementweb/service/pay/transaction/PayTransactionService.java
浏览文件 @
30e2d087
...
@@ -6,13 +6,18 @@ import cn.iocoder.mall.managementweb.controller.pay.vo.transaction.PayTransactio
...
@@ -6,13 +6,18 @@ import cn.iocoder.mall.managementweb.controller.pay.vo.transaction.PayTransactio
import
cn.iocoder.mall.managementweb.controller.pay.vo.transaction.PayTransactionRespVO
;
import
cn.iocoder.mall.managementweb.controller.pay.vo.transaction.PayTransactionRespVO
;
import
cn.iocoder.mall.managementweb.convert.pay.transaction.PayTransactionConvert
;
import
cn.iocoder.mall.managementweb.convert.pay.transaction.PayTransactionConvert
;
import
cn.iocoder.mall.payservice.rpc.transaction.dto.PayTransactionRespDTO
;
import
cn.iocoder.mall.payservice.rpc.transaction.dto.PayTransactionRespDTO
;
import
com.google.common.collect.Lists
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
javax.annotation.Resource
;
import
java.util.List
;
import
java.util.stream.Stream
;
@Service
@Service
public
class
PayTransactionService
{
public
class
PayTransactionService
{
@
Autowired
@
Resource
private
PayTransactionClient
payTransactionClient
;
private
PayTransactionClient
payTransactionClient
;
public
PageResult
<
PayTransactionRespVO
>
pagePayTransaction
(
PayTransactionPageReqVO
pageReqVO
)
{
public
PageResult
<
PayTransactionRespVO
>
pagePayTransaction
(
PayTransactionPageReqVO
pageReqVO
)
{
...
...
system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/manager/permission/PermissionManager.java
浏览文件 @
30e2d087
...
@@ -7,7 +7,7 @@ import cn.iocoder.mall.systemservice.rpc.permission.dto.PermissionAssignAdminRol
...
@@ -7,7 +7,7 @@ import cn.iocoder.mall.systemservice.rpc.permission.dto.PermissionAssignAdminRol
import
cn.iocoder.mall.systemservice.rpc.permission.dto.PermissionAssignRoleResourceDTO
;
import
cn.iocoder.mall.systemservice.rpc.permission.dto.PermissionAssignRoleResourceDTO
;
import
cn.iocoder.mall.systemservice.rpc.permission.dto.PermissionCheckDTO
;
import
cn.iocoder.mall.systemservice.rpc.permission.dto.PermissionCheckDTO
;
import
cn.iocoder.mall.systemservice.service.permission.PermissionService
;
import
cn.iocoder.mall.systemservice.service.permission.PermissionService
;
import
cn.iocoder.mall.systemservice.service.permission.ResourceService
;
import
cn.iocoder.mall.systemservice.service.permission.ResourceService
Impl
;
import
cn.iocoder.mall.systemservice.service.permission.RoleService
;
import
cn.iocoder.mall.systemservice.service.permission.RoleService
;
import
cn.iocoder.mall.systemservice.service.permission.bo.ResourceBO
;
import
cn.iocoder.mall.systemservice.service.permission.bo.ResourceBO
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
@@ -31,7 +31,7 @@ public class PermissionManager {
...
@@ -31,7 +31,7 @@ public class PermissionManager {
@Autowired
@Autowired
private
RoleService
roleService
;
private
RoleService
roleService
;
@Autowired
@Autowired
private
ResourceService
resourceService
;
private
ResourceService
Impl
resourceService
;
/**
/**
* 获得角色拥有的资源编号
* 获得角色拥有的资源编号
...
...
system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/manager/permission/ResourceManager.java
浏览文件 @
30e2d087
...
@@ -4,7 +4,7 @@ import cn.iocoder.mall.systemservice.convert.permission.ResourceConvert;
...
@@ -4,7 +4,7 @@ import cn.iocoder.mall.systemservice.convert.permission.ResourceConvert;
import
cn.iocoder.mall.systemservice.rpc.permission.dto.ResourceCreateDTO
;
import
cn.iocoder.mall.systemservice.rpc.permission.dto.ResourceCreateDTO
;
import
cn.iocoder.mall.systemservice.rpc.permission.dto.ResourceUpdateDTO
;
import
cn.iocoder.mall.systemservice.rpc.permission.dto.ResourceUpdateDTO
;
import
cn.iocoder.mall.systemservice.rpc.permission.vo.ResourceVO
;
import
cn.iocoder.mall.systemservice.rpc.permission.vo.ResourceVO
;
import
cn.iocoder.mall.systemservice.service.permission.ResourceService
;
import
cn.iocoder.mall.systemservice.service.permission.ResourceService
Impl
;
import
cn.iocoder.mall.systemservice.service.permission.RoleService
;
import
cn.iocoder.mall.systemservice.service.permission.RoleService
;
import
cn.iocoder.mall.systemservice.service.permission.bo.ResourceBO
;
import
cn.iocoder.mall.systemservice.service.permission.bo.ResourceBO
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
@@ -20,7 +20,7 @@ import java.util.List;
...
@@ -20,7 +20,7 @@ import java.util.List;
public
class
ResourceManager
{
public
class
ResourceManager
{
@Autowired
@Autowired
private
ResourceService
resourceService
;
private
ResourceService
Impl
resourceService
;
@Autowired
@Autowired
private
RoleService
roleService
;
private
RoleService
roleService
;
...
...
system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/permission/ResourceService.java
浏览文件 @
30e2d087
package
cn
.
iocoder
.
mall
.
systemservice
.
service
.
permission
;
package
cn
.
iocoder
.
mall
.
systemservice
.
service
.
permission
;
import
cn.iocoder.common.framework.util.CollectionUtils
;
import
cn.iocoder.common.framework.exception.util.ServiceExceptionUtil
;
import
cn.iocoder.mall.systemservice.convert.permission.ResourceConvert
;
import
cn.iocoder.mall.systemservice.dal.mysql.dataobject.permission.ResourceDO
;
import
cn.iocoder.mall.systemservice.dal.mysql.dataobject.permission.RoleResourceDO
;
import
cn.iocoder.mall.systemservice.dal.mysql.mapper.permission.ResourceMapper
;
import
cn.iocoder.mall.systemservice.dal.mysql.mapper.permission.RoleResourceMapper
;
import
cn.iocoder.mall.systemservice.enums.SystemErrorCodeConstants
;
import
cn.iocoder.mall.systemservice.enums.permission.ResourceIdEnum
;
import
cn.iocoder.mall.systemservice.enums.permission.ResourceTypeEnum
;
import
cn.iocoder.mall.systemservice.service.permission.bo.ResourceBO
;
import
cn.iocoder.mall.systemservice.service.permission.bo.ResourceCreateBO
;
import
cn.iocoder.mall.systemservice.service.permission.bo.ResourceUpdateBO
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.validation.annotation.Validated
;
import
javax.validation.Valid
;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.List
;
import
static
cn
.
iocoder
.
mall
.
systemservice
.
enums
.
SystemErrorCodeConstants
.*;
/**
/**
* 资源 Service
* 资源 Service 接口
*/
*/
@Service
public
interface
ResourceService
{
@Validated
public
class
ResourceService
{
@Autowired
private
ResourceMapper
resourceMapper
;
@Autowired
private
RoleResourceMapper
roleResourceMapper
;
/**
* 创建资源
*
* @param createBO 创建资源 BO
* @return 资源
*/
public
ResourceBO
createResource
(
@Valid
ResourceCreateBO
createBO
)
{
// 校验父资源存在
checkParentResource
(
createBO
.
getPid
(),
null
);
// 校验资源(自己)
checkResource
(
createBO
.
getPid
(),
createBO
.
getName
(),
null
);
// 插入数据库
ResourceDO
resourceDO
=
ResourceConvert
.
INSTANCE
.
convert
(
createBO
);
initResourceProperty
(
resourceDO
);
resourceMapper
.
insert
(
resourceDO
);
// 返回
return
ResourceConvert
.
INSTANCE
.
convert
(
resourceDO
);
}
/**
* 更新资源
*
* @param updateBO 更新资源 BO
*/
public
void
updateResource
(
@Valid
ResourceUpdateBO
updateBO
)
{
// 校验更新的资源是否存在
if
(
resourceMapper
.
selectById
(
updateBO
.
getId
())
==
null
)
{
throw
ServiceExceptionUtil
.
exception
(
RESOURCE_NOT_EXISTS
);
}
// 校验父资源存在
checkParentResource
(
updateBO
.
getPid
(),
updateBO
.
getId
());
// 校验资源(自己)
checkResource
(
updateBO
.
getPid
(),
updateBO
.
getName
(),
updateBO
.
getId
());
// 更新到数据库
ResourceDO
updateObject
=
ResourceConvert
.
INSTANCE
.
convert
(
updateBO
);
initResourceProperty
(
updateObject
);
resourceMapper
.
updateById
(
updateObject
);
}
/**
* 删除资源
*
* @param resourceId 资源编号
*/
public
void
deleteResource
(
Integer
resourceId
)
{
// 校验更新的资源是否存在
if
(
resourceMapper
.
selectById
(
resourceId
)
==
null
)
{
throw
ServiceExceptionUtil
.
exception
(
SystemErrorCodeConstants
.
RESOURCE_NOT_EXISTS
);
}
// 校验是否还有子资源
if
(
resourceMapper
.
selectCountByPid
(
resourceId
)
>
0
)
{
throw
ServiceExceptionUtil
.
exception
(
SystemErrorCodeConstants
.
RESOURCE_EXISTS_CHILDREN
);
}
// 校验删除的资源是否存在
if
(
resourceMapper
.
selectById
(
resourceId
)
==
null
)
{
throw
ServiceExceptionUtil
.
exception
(
RESOURCE_NOT_EXISTS
);
}
// 标记删除
resourceMapper
.
deleteById
(
resourceId
);
// 删除授予给角色的权限
roleResourceMapper
.
deleteByResourceId
(
resourceId
);
}
/**
* 获得资源
*
* @param resourceId 资源编号
* @return 资源
*/
public
ResourceBO
getResource
(
Integer
resourceId
)
{
ResourceDO
resourceDO
=
resourceMapper
.
selectById
(
resourceId
);
return
ResourceConvert
.
INSTANCE
.
convert
(
resourceDO
);
}
/**
* 获得资源列表
*
* @param resourceIds 资源编号列表
* @return 资源列表
*/
public
List
<
ResourceBO
>
listResources
(
List
<
Integer
>
resourceIds
)
{
List
<
ResourceDO
>
resourceDOs
=
resourceMapper
.
selectBatchIds
(
resourceIds
);
return
ResourceConvert
.
INSTANCE
.
convertList
(
resourceDOs
);
}
/**
* 获得资源全列表
*
* @return 资源全列表
*/
public
List
<
ResourceBO
>
listResources
()
{
List
<
ResourceDO
>
resourceDOs
=
resourceMapper
.
selectList
(
null
);
return
ResourceConvert
.
INSTANCE
.
convertList
(
resourceDOs
);
}
/**
* 获得指定类型的资源列表
*
* @param type 资源类型,允许空
* @return 资源列表
*/
public
List
<
ResourceBO
>
listResourcesByType
(
Integer
type
)
{
List
<
ResourceDO
>
resourceDOs
=
resourceMapper
.
selectListByType
(
type
);
return
ResourceConvert
.
INSTANCE
.
convertList
(
resourceDOs
);
}
/**
* 获得角色拥有的资源列表
*
* @param roleIds 角色编号
* @param type 资源类型,允许空
* @return 资源列表
*/
public
List
<
ResourceBO
>
listRoleResourcesByType
(
Collection
<
Integer
>
roleIds
,
Integer
type
)
{
List
<
RoleResourceDO
>
roleResourceDOs
=
roleResourceMapper
.
selectListByRoleIds
(
roleIds
);
if
(
CollectionUtils
.
isEmpty
(
roleResourceDOs
))
{
return
Collections
.
emptyList
();
}
List
<
ResourceDO
>
resourceDOs
=
resourceMapper
.
selectListByIdsAndType
(
CollectionUtils
.
convertSet
(
roleResourceDOs
,
RoleResourceDO:
:
getResourceId
),
type
);
return
ResourceConvert
.
INSTANCE
.
convertList
(
resourceDOs
);
}
/**
* 校验父资源是否合法
*
* 1. 不能设置自己为父资源
* 2. 父资源不存在
* 3. 父资源必须是 {@link ResourceTypeEnum#MENU} 菜单类型
*
* @param pid 父资源编号
* @param childId 当前资源编号
*/
private
void
checkParentResource
(
Integer
pid
,
Integer
childId
)
{
if
(
pid
==
null
||
ResourceIdEnum
.
ROOT
.
getId
().
equals
(
pid
))
{
return
;
}
// 不能设置自己为父资源
if
(
pid
.
equals
(
childId
))
{
throw
ServiceExceptionUtil
.
exception
(
RESOURCE_PARENT_ERROR
);
}
ResourceDO
resource
=
resourceMapper
.
selectById
(
pid
);
// 父资源不存在
if
(
resource
==
null
)
{
throw
ServiceExceptionUtil
.
exception
(
RESOURCE_PARENT_NOT_EXISTS
);
}
// 父资源必须是菜单类型
if
(!
ResourceTypeEnum
.
MENU
.
getType
().
equals
(
resource
.
getType
()))
{
throw
ServiceExceptionUtil
.
exception
(
RESOURCE_PARENT_NOT_MENU
);
}
}
/**
* 校验资源是否合法
*
* 1. 校验相同父资源编号下,是否存在相同的资源名
*
* @param name 资源名字
* @param pid 父资源编号
* @param id 资源编号
*/
private
void
checkResource
(
Integer
pid
,
String
name
,
Integer
id
)
{
ResourceDO
resource
=
resourceMapper
.
selectByPidAndName
(
pid
,
name
);
if
(
resource
==
null
)
{
return
;
}
// 如果 id 为空,说明不用比较是否为相同 id 的资源
if
(
id
==
null
)
{
throw
ServiceExceptionUtil
.
exception
(
RESOURCE_NAME_DUPLICATE
);
}
if
(!
resource
.
getId
().
equals
(
id
))
{
throw
ServiceExceptionUtil
.
exception
(
RESOURCE_NAME_DUPLICATE
);
}
}
/**
* 初始化资源的通用属性。
*
* 例如说,只有菜单类型的资源,才设置 icon
*
* @param resource 资源
*/
private
void
initResourceProperty
(
ResourceDO
resource
)
{
// 资源为按钮类型时,无需 route、icon、view 属性,进行置空
if
(
ResourceTypeEnum
.
BUTTON
.
getType
().
equals
(
resource
.
getType
()))
{
resource
.
setRoute
(
null
);
resource
.
setIcon
(
null
);
resource
.
setView
(
null
);
}
}
}
}
system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/permission/ResourceServiceImpl.java
0 → 100644
浏览文件 @
30e2d087
package
cn
.
iocoder
.
mall
.
systemservice
.
service
.
permission
;
import
cn.iocoder.common.framework.util.CollectionUtils
;
import
cn.iocoder.common.framework.exception.util.ServiceExceptionUtil
;
import
cn.iocoder.mall.systemservice.convert.permission.ResourceConvert
;
import
cn.iocoder.mall.systemservice.dal.mysql.dataobject.permission.ResourceDO
;
import
cn.iocoder.mall.systemservice.dal.mysql.dataobject.permission.RoleResourceDO
;
import
cn.iocoder.mall.systemservice.dal.mysql.mapper.permission.ResourceMapper
;
import
cn.iocoder.mall.systemservice.dal.mysql.mapper.permission.RoleResourceMapper
;
import
cn.iocoder.mall.systemservice.enums.SystemErrorCodeConstants
;
import
cn.iocoder.mall.systemservice.enums.permission.ResourceIdEnum
;
import
cn.iocoder.mall.systemservice.enums.permission.ResourceTypeEnum
;
import
cn.iocoder.mall.systemservice.service.permission.bo.ResourceBO
;
import
cn.iocoder.mall.systemservice.service.permission.bo.ResourceCreateBO
;
import
cn.iocoder.mall.systemservice.service.permission.bo.ResourceUpdateBO
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.validation.annotation.Validated
;
import
javax.validation.Valid
;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.List
;
import
static
cn
.
iocoder
.
mall
.
systemservice
.
enums
.
SystemErrorCodeConstants
.*;
/**
* 资源 Service
*/
@Service
@Validated
public
class
ResourceServiceImpl
implements
ResourceService
{
@Autowired
private
ResourceMapper
resourceMapper
;
@Autowired
private
RoleResourceMapper
roleResourceMapper
;
/**
* 创建资源
*
* @param createBO 创建资源 BO
* @return 资源
*/
public
ResourceBO
createResource
(
@Valid
ResourceCreateBO
createBO
)
{
// 校验父资源存在
checkParentResource
(
createBO
.
getPid
(),
null
);
// 校验资源(自己)
checkResource
(
createBO
.
getPid
(),
createBO
.
getName
(),
null
);
// 插入数据库
ResourceDO
resourceDO
=
ResourceConvert
.
INSTANCE
.
convert
(
createBO
);
initResourceProperty
(
resourceDO
);
resourceMapper
.
insert
(
resourceDO
);
// 返回
return
ResourceConvert
.
INSTANCE
.
convert
(
resourceDO
);
}
/**
* 更新资源
*
* @param updateBO 更新资源 BO
*/
public
void
updateResource
(
@Valid
ResourceUpdateBO
updateBO
)
{
// 校验更新的资源是否存在
if
(
resourceMapper
.
selectById
(
updateBO
.
getId
())
==
null
)
{
throw
ServiceExceptionUtil
.
exception
(
RESOURCE_NOT_EXISTS
);
}
// 校验父资源存在
checkParentResource
(
updateBO
.
getPid
(),
updateBO
.
getId
());
// 校验资源(自己)
checkResource
(
updateBO
.
getPid
(),
updateBO
.
getName
(),
updateBO
.
getId
());
// 更新到数据库
ResourceDO
updateObject
=
ResourceConvert
.
INSTANCE
.
convert
(
updateBO
);
initResourceProperty
(
updateObject
);
resourceMapper
.
updateById
(
updateObject
);
}
/**
* 删除资源
*
* @param resourceId 资源编号
*/
public
void
deleteResource
(
Integer
resourceId
)
{
// 校验更新的资源是否存在
if
(
resourceMapper
.
selectById
(
resourceId
)
==
null
)
{
throw
ServiceExceptionUtil
.
exception
(
SystemErrorCodeConstants
.
RESOURCE_NOT_EXISTS
);
}
// 校验是否还有子资源
if
(
resourceMapper
.
selectCountByPid
(
resourceId
)
>
0
)
{
throw
ServiceExceptionUtil
.
exception
(
SystemErrorCodeConstants
.
RESOURCE_EXISTS_CHILDREN
);
}
// 校验删除的资源是否存在
if
(
resourceMapper
.
selectById
(
resourceId
)
==
null
)
{
throw
ServiceExceptionUtil
.
exception
(
RESOURCE_NOT_EXISTS
);
}
// 标记删除
resourceMapper
.
deleteById
(
resourceId
);
// 删除授予给角色的权限
roleResourceMapper
.
deleteByResourceId
(
resourceId
);
}
/**
* 获得资源
*
* @param resourceId 资源编号
* @return 资源
*/
public
ResourceBO
getResource
(
Integer
resourceId
)
{
ResourceDO
resourceDO
=
resourceMapper
.
selectById
(
resourceId
);
return
ResourceConvert
.
INSTANCE
.
convert
(
resourceDO
);
}
/**
* 获得资源列表
*
* @param resourceIds 资源编号列表
* @return 资源列表
*/
public
List
<
ResourceBO
>
listResources
(
List
<
Integer
>
resourceIds
)
{
List
<
ResourceDO
>
resourceDOs
=
resourceMapper
.
selectBatchIds
(
resourceIds
);
return
ResourceConvert
.
INSTANCE
.
convertList
(
resourceDOs
);
}
/**
* 获得资源全列表
*
* @return 资源全列表
*/
public
List
<
ResourceBO
>
listResources
()
{
List
<
ResourceDO
>
resourceDOs
=
resourceMapper
.
selectList
(
null
);
return
ResourceConvert
.
INSTANCE
.
convertList
(
resourceDOs
);
}
/**
* 获得指定类型的资源列表
*
* @param type 资源类型,允许空
* @return 资源列表
*/
public
List
<
ResourceBO
>
listResourcesByType
(
Integer
type
)
{
List
<
ResourceDO
>
resourceDOs
=
resourceMapper
.
selectListByType
(
type
);
return
ResourceConvert
.
INSTANCE
.
convertList
(
resourceDOs
);
}
/**
* 获得角色拥有的资源列表
*
* @param roleIds 角色编号
* @param type 资源类型,允许空
* @return 资源列表
*/
public
List
<
ResourceBO
>
listRoleResourcesByType
(
Collection
<
Integer
>
roleIds
,
Integer
type
)
{
List
<
RoleResourceDO
>
roleResourceDOs
=
roleResourceMapper
.
selectListByRoleIds
(
roleIds
);
if
(
CollectionUtils
.
isEmpty
(
roleResourceDOs
))
{
return
Collections
.
emptyList
();
}
List
<
ResourceDO
>
resourceDOs
=
resourceMapper
.
selectListByIdsAndType
(
CollectionUtils
.
convertSet
(
roleResourceDOs
,
RoleResourceDO:
:
getResourceId
),
type
);
return
ResourceConvert
.
INSTANCE
.
convertList
(
resourceDOs
);
}
/**
* 校验父资源是否合法
*
* 1. 不能设置自己为父资源
* 2. 父资源不存在
* 3. 父资源必须是 {@link ResourceTypeEnum#MENU} 菜单类型
*
* @param pid 父资源编号
* @param childId 当前资源编号
*/
private
void
checkParentResource
(
Integer
pid
,
Integer
childId
)
{
if
(
pid
==
null
||
ResourceIdEnum
.
ROOT
.
getId
().
equals
(
pid
))
{
return
;
}
// 不能设置自己为父资源
if
(
pid
.
equals
(
childId
))
{
throw
ServiceExceptionUtil
.
exception
(
RESOURCE_PARENT_ERROR
);
}
ResourceDO
resource
=
resourceMapper
.
selectById
(
pid
);
// 父资源不存在
if
(
resource
==
null
)
{
throw
ServiceExceptionUtil
.
exception
(
RESOURCE_PARENT_NOT_EXISTS
);
}
// 父资源必须是菜单类型
if
(!
ResourceTypeEnum
.
MENU
.
getType
().
equals
(
resource
.
getType
()))
{
throw
ServiceExceptionUtil
.
exception
(
RESOURCE_PARENT_NOT_MENU
);
}
}
/**
* 校验资源是否合法
*
* 1. 校验相同父资源编号下,是否存在相同的资源名
*
* @param name 资源名字
* @param pid 父资源编号
* @param id 资源编号
*/
private
void
checkResource
(
Integer
pid
,
String
name
,
Integer
id
)
{
ResourceDO
resource
=
resourceMapper
.
selectByPidAndName
(
pid
,
name
);
if
(
resource
==
null
)
{
return
;
}
// 如果 id 为空,说明不用比较是否为相同 id 的资源
if
(
id
==
null
)
{
throw
ServiceExceptionUtil
.
exception
(
RESOURCE_NAME_DUPLICATE
);
}
if
(!
resource
.
getId
().
equals
(
id
))
{
throw
ServiceExceptionUtil
.
exception
(
RESOURCE_NAME_DUPLICATE
);
}
}
/**
* 初始化资源的通用属性。
*
* 例如说,只有菜单类型的资源,才设置 icon
*
* @param resource 资源
*/
private
void
initResourceProperty
(
ResourceDO
resource
)
{
// 资源为按钮类型时,无需 route、icon、view 属性,进行置空
if
(
ResourceTypeEnum
.
BUTTON
.
getType
().
equals
(
resource
.
getType
()))
{
resource
.
setRoute
(
null
);
resource
.
setIcon
(
null
);
resource
.
setView
(
null
);
}
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论