|
@@ -3,6 +3,7 @@ package com.zswl.dataservice.service.base;
|
|
|
import com.mongodb.client.result.UpdateResult;
|
|
import com.mongodb.client.result.UpdateResult;
|
|
|
import com.zswl.dataservice.utils.CommonUtil;
|
|
import com.zswl.dataservice.utils.CommonUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
import org.springframework.data.mongodb.core.query.Criteria;
|
|
import org.springframework.data.mongodb.core.query.Criteria;
|
|
@@ -33,15 +34,18 @@ public class CommonService {
|
|
|
public Object updateData(Map<String, Object> standardData, String collectionName) {
|
|
public Object updateData(Map<String, Object> standardData, String collectionName) {
|
|
|
collectionName = CommonUtil.getCollectionName(collectionName);
|
|
collectionName = CommonUtil.getCollectionName(collectionName);
|
|
|
Object id = standardData.get("id");
|
|
Object id = standardData.get("id");
|
|
|
- Query query = new Query(Criteria.where("_id").is(id));
|
|
|
|
|
- Update update = new Update();
|
|
|
|
|
- standardData.forEach((key, value) -> {
|
|
|
|
|
- if (!"id".equals(key)) {
|
|
|
|
|
- update.set(key, value);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- UpdateResult updateResult = mongoTemplate.upsert(query, update, collectionName);
|
|
|
|
|
- return updateResult.getUpsertedId();
|
|
|
|
|
|
|
+ if (id != null) {
|
|
|
|
|
+ Query query = new Query(Criteria.where("_id").is(id));
|
|
|
|
|
+ Update update = new Update();
|
|
|
|
|
+ standardData.forEach((key, value) -> {
|
|
|
|
|
+ if (!"id".equals(key)) {
|
|
|
|
|
+ update.set(key, value);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ UpdateResult updateResult = mongoTemplate.upsert(query, update, collectionName);
|
|
|
|
|
+ return updateResult.getUpsertedId();
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public Object updateData(Map<String, Object> where, Map<String, Object> standardData, String collectionName) {
|
|
public Object updateData(Map<String, Object> where, Map<String, Object> standardData, String collectionName) {
|