提交 d71f7867 authored 作者: Matrix's avatar Matrix

feat(书签功能): 优化了保存用户配置项的代码

改为了反射实现,这样可以减少代码的修改
上级 72acf514
......@@ -30,6 +30,11 @@ import java.util.List;
@ApiModel("用户配置")
public class UserConfig extends BaseModel {
public UserConfig(Long userId, List<BookmarkConfig> bookmark) {
this.userId = userId;
this.bookmark = bookmark;
}
/**
* 用户id
*/
......@@ -52,4 +57,6 @@ public class UserConfig extends BaseModel {
@ApiModelProperty(value = "接收消息配置")
private MessageConfig messageConfig;
}
package org.matrix.local.service;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mongodb.BasicDBObject;
import com.mongodb.client.result.UpdateResult;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.matrix.local.entity.Project;
import org.matrix.local.entity.User;
import org.matrix.local.entity.UserConfig;
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.UserInfo;
import org.matrix.local.enums.ConfigType;
......@@ -25,6 +24,7 @@ import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -58,6 +58,7 @@ public class UserService {
* @param configType 配置类型
* @return 是否更新/插入成功
*/
@SneakyThrows
public boolean upsertConfig(UserConfig userConfig, ConfigType configType) {
Long userId = userConfig.getUserId();
......@@ -68,37 +69,34 @@ public class UserService {
}
// 如果是用户的第一次配置,即配置库里没有配置项,则全部保存
boolean success;
Query userQuery = Query.query(Criteria.where("userId").is(userId));
boolean isFirst = !mongoTemplate.exists(userQuery, "user_config");
if (isFirst){
boolean firstConfig = !mongoTemplate.exists(userQuery, "user_config");
if (firstConfig){
mongoTemplate.save(userConfig, COLLECTION_NAME);
success = true;
return true;
}else {
// 依据配置类型的不同来更新对应的字段,如果是all,则全部替换
Update update;
switch (configType){
case BOOK_MARK:
log.info("[用户配置] 正在给id = {} 的用户更新<书签>配置,新的书签是 {}",userId,userConfig.getBookmark());
update = Update.update(configType.getPropertyName(), userConfig.getBookmark());
UpdateResult result = mongoTemplate.upsert(userQuery, update, COLLECTION_NAME);
success = true;
break;
case ALL:
if (configType == ConfigType.ALL){
log.info("[用户配置] 正在给id = {} 的用户更新<全局>配置,新的配置是 {}",userId,userConfig);
Document doc = new Document();
mongoTemplate.getConverter().write(userConfig,doc);
update = Update.fromDocument(doc);
mongoTemplate.upsert(userQuery, update, COLLECTION_NAME);
success = true;
break;
default:
throw new RuntimeException("用户配置类型错误!当前给出的配置类型是: " + configType);
return true;
}else {
// 通过反射获取userConfig的属性值
String propertyName = configType.getPropertyName();
Field field = userConfig.getClass().getDeclaredField(propertyName);
field.setAccessible(true);
Object value = field.get(userConfig);
// 更新mongo中的数据
log.info("[用户配置] 正在给id = {} 的用户更新<{}>配置,新的{}是 {}",userId,configType.getDes(),configType.getDes(),value);
update = Update.update(configType.getPropertyName(), value);
mongoTemplate.upsert(userQuery, update, COLLECTION_NAME);
return true;
}
}
return success;
}
/**
......
......@@ -4,10 +4,9 @@ import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.matrix.database.vo.CommonResult;
import org.matrix.database.vo.CommonResultObj;
import org.matrix.local.entity.BookmarkConfig;
import org.matrix.local.entity.UserConfig;
import org.matrix.local.entity.config.BookmarkConfig;
import org.matrix.local.entity.config.UserConfig;
import org.matrix.local.entity.vo.UserInfo;
import org.matrix.local.enums.ConfigType;
import org.matrix.local.service.UserService;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论