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

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

改为了反射实现,这样可以减少代码的修改
上级 72acf514
...@@ -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;
} }
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;
} }
/** /**
......
...@@ -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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论