Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
kt-keystone
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Matrix
kt-keystone
Commits
d71f7867
提交
d71f7867
authored
6月 20, 2022
作者:
Matrix
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(书签功能): 优化了保存用户配置项的代码
改为了反射实现,这样可以减少代码的修改
上级
72acf514
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
35 行增加
和
31 行删除
+35
-31
UserConfig.java
.../main/java/org/matrix/local/entity/config/UserConfig.java
+7
-0
UserService.java
...r/src/main/java/org/matrix/local/service/UserService.java
+26
-28
UserConfigController.java
.../org/matrix/autotest/controller/UserConfigController.java
+2
-3
没有找到文件。
kt-user/src/main/java/org/matrix/local/entity/config/UserConfig.java
浏览文件 @
d71f7867
...
@@ -30,6 +30,11 @@ import java.util.List;
...
@@ -30,6 +30,11 @@ import java.util.List;
@ApiModel
(
"用户配置"
)
@ApiModel
(
"用户配置"
)
public
class
UserConfig
extends
BaseModel
{
public
class
UserConfig
extends
BaseModel
{
public
UserConfig
(
Long
userId
,
List
<
BookmarkConfig
>
bookmark
)
{
this
.
userId
=
userId
;
this
.
bookmark
=
bookmark
;
}
/**
/**
* 用户id
* 用户id
*/
*/
...
@@ -52,4 +57,6 @@ public class UserConfig extends BaseModel {
...
@@ -52,4 +57,6 @@ public class UserConfig extends BaseModel {
@ApiModelProperty
(
value
=
"接收消息配置"
)
@ApiModelProperty
(
value
=
"接收消息配置"
)
private
MessageConfig
messageConfig
;
private
MessageConfig
messageConfig
;
}
}
kt-user/src/main/java/org/matrix/local/service/UserService.java
浏览文件 @
d71f7867
package
org
.
matrix
.
local
.
service
;
package
org
.
matrix
.
local
.
service
;
import
com.baomidou.mybatisplus.core.toolkit.Wrappers
;
import
com.baomidou.mybatisplus.core.toolkit.Wrappers
;
import
com.mongodb.BasicDBObject
;
import
lombok.SneakyThrows
;
import
com.mongodb.client.result.UpdateResult
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.bson.Document
;
import
org.bson.Document
;
import
org.matrix.local.entity.Project
;
import
org.matrix.local.entity.Project
;
import
org.matrix.local.entity.User
;
import
org.matrix.local.entity.User
;
import
org.matrix.local.entity.UserConfig
;
import
org.matrix.local.entity.UserProject
;
import
org.matrix.local.entity.UserProject
;
import
org.matrix.local.entity.config.UserConfig
;
import
org.matrix.local.entity.vo.LoginInfo
;
import
org.matrix.local.entity.vo.LoginInfo
;
import
org.matrix.local.entity.vo.UserInfo
;
import
org.matrix.local.entity.vo.UserInfo
;
import
org.matrix.local.enums.ConfigType
;
import
org.matrix.local.enums.ConfigType
;
...
@@ -25,6 +24,7 @@ import org.springframework.stereotype.Service;
...
@@ -25,6 +24,7 @@ import org.springframework.stereotype.Service;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
java.lang.reflect.Field
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
...
@@ -58,6 +58,7 @@ public class UserService {
...
@@ -58,6 +58,7 @@ public class UserService {
* @param configType 配置类型
* @param configType 配置类型
* @return 是否更新/插入成功
* @return 是否更新/插入成功
*/
*/
@SneakyThrows
public
boolean
upsertConfig
(
UserConfig
userConfig
,
ConfigType
configType
)
{
public
boolean
upsertConfig
(
UserConfig
userConfig
,
ConfigType
configType
)
{
Long
userId
=
userConfig
.
getUserId
();
Long
userId
=
userConfig
.
getUserId
();
...
@@ -68,37 +69,34 @@ public class UserService {
...
@@ -68,37 +69,34 @@ public class UserService {
}
}
// 如果是用户的第一次配置,即配置库里没有配置项,则全部保存
// 如果是用户的第一次配置,即配置库里没有配置项,则全部保存
boolean
success
;
Query
userQuery
=
Query
.
query
(
Criteria
.
where
(
"userId"
).
is
(
userId
));
Query
userQuery
=
Query
.
query
(
Criteria
.
where
(
"userId"
).
is
(
userId
));
boolean
isFirst
=
!
mongoTemplate
.
exists
(
userQuery
,
"user_config"
);
boolean
firstConfig
=
!
mongoTemplate
.
exists
(
userQuery
,
"user_config"
);
if
(
isFirst
){
if
(
firstConfig
){
mongoTemplate
.
save
(
userConfig
,
COLLECTION_NAME
);
mongoTemplate
.
save
(
userConfig
,
COLLECTION_NAME
);
success
=
true
;
return
true
;
}
else
{
}
else
{
// 依据配置类型的不同来更新对应的字段,如果是all,则全部替换
Update
update
;
Update
update
;
switch
(
configType
){
case
BOOK_MARK:
if
(
configType
==
ConfigType
.
ALL
){
log
.
info
(
"[用户配置] 正在给id = {} 的用户更新<书签>配置,新的书签是 {}"
,
userId
,
userConfig
.
getBookmark
());
log
.
info
(
"[用户配置] 正在给id = {} 的用户更新<全局>配置,新的配置是 {}"
,
userId
,
userConfig
);
update
=
Update
.
update
(
configType
.
getPropertyName
(),
userConfig
.
getBookmark
());
Document
doc
=
new
Document
();
UpdateResult
result
=
mongoTemplate
.
upsert
(
userQuery
,
update
,
COLLECTION_NAME
);
mongoTemplate
.
getConverter
().
write
(
userConfig
,
doc
);
success
=
true
;
update
=
Update
.
fromDocument
(
doc
);
break
;
mongoTemplate
.
upsert
(
userQuery
,
update
,
COLLECTION_NAME
);
case
ALL:
return
true
;
log
.
info
(
"[用户配置] 正在给id = {} 的用户更新<全局>配置,新的配置是 {}"
,
userId
,
userConfig
);
}
else
{
Document
doc
=
new
Document
();
// 通过反射获取userConfig的属性值
mongoTemplate
.
getConverter
().
write
(
userConfig
,
doc
);
String
propertyName
=
configType
.
getPropertyName
();
update
=
Update
.
fromDocument
(
doc
);
Field
field
=
userConfig
.
getClass
().
getDeclaredField
(
propertyName
);
mongoTemplate
.
upsert
(
userQuery
,
update
,
COLLECTION_NAME
);
field
.
setAccessible
(
true
);
success
=
true
;
Object
value
=
field
.
get
(
userConfig
);
break
;
// 更新mongo中的数据
default
:
log
.
info
(
"[用户配置] 正在给id = {} 的用户更新<{}>配置,新的{}是 {}"
,
userId
,
configType
.
getDes
(),
configType
.
getDes
(),
value
);
throw
new
RuntimeException
(
"用户配置类型错误!当前给出的配置类型是: "
+
configType
);
update
=
Update
.
update
(
configType
.
getPropertyName
(),
value
);
mongoTemplate
.
upsert
(
userQuery
,
update
,
COLLECTION_NAME
);
return
true
;
}
}
}
}
return
success
;
}
}
/**
/**
...
...
kt-web/src/main/java/org/matrix/autotest/controller/UserConfigController.java
浏览文件 @
d71f7867
...
@@ -4,10 +4,9 @@ import com.google.common.collect.Lists;
...
@@ -4,10 +4,9 @@ import com.google.common.collect.Lists;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
import
io.swagger.annotations.ApiOperation
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.matrix.database.vo.CommonResult
;
import
org.matrix.database.vo.CommonResultObj
;
import
org.matrix.database.vo.CommonResultObj
;
import
org.matrix.local.entity.BookmarkConfig
;
import
org.matrix.local.entity.
config.
BookmarkConfig
;
import
org.matrix.local.entity.UserConfig
;
import
org.matrix.local.entity.
config.
UserConfig
;
import
org.matrix.local.entity.vo.UserInfo
;
import
org.matrix.local.entity.vo.UserInfo
;
import
org.matrix.local.enums.ConfigType
;
import
org.matrix.local.enums.ConfigType
;
import
org.matrix.local.service.UserService
;
import
org.matrix.local.service.UserService
;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论