mirror of
https://gitee.com/freshday/radar.git
synced 2026-03-22 04:37:16 +08:00
prefs: 重新封装访问mongodb 的api
feihu wang
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package com.pgmmers.radar.service.impl.data;
|
||||
|
||||
import com.pgmmers.radar.service.data.MongoService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class MongoServiceImpl implements MongoService {
|
||||
|
||||
@Autowired
|
||||
private MongoTemplate mongoTemplate;
|
||||
|
||||
@Override
|
||||
public MongoTemplate getMongoTemplate() {
|
||||
return mongoTemplate;
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,8 @@ import com.mongodb.client.AggregateIterable;
|
||||
import com.mongodb.client.FindIterable;
|
||||
import com.mongodb.client.model.Filters;
|
||||
import com.pgmmers.radar.enums.FieldType;
|
||||
import com.pgmmers.radar.service.data.MongoService;
|
||||
import com.pgmmers.radar.service.engine.AggregateCommand;
|
||||
import com.pgmmers.radar.service.impl.util.MongodbUtil;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -15,16 +15,20 @@ import java.util.Date;
|
||||
import java.util.List;
|
||||
import org.bson.Document;
|
||||
import org.bson.conversions.Bson;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class AggregateCommandImpl implements AggregateCommand {
|
||||
|
||||
@Autowired
|
||||
private MongoService mongoService;
|
||||
|
||||
@Override
|
||||
public long count(String modelId, String searchField, Object searchFieldValue, String refDateName, Date begin,
|
||||
Date end) {
|
||||
String collectionName = "entity_" + modelId;
|
||||
Long qty = MongodbUtil.count(collectionName, Filters.and(Filters.eq(searchField, searchFieldValue),
|
||||
Long qty = mongoService.count(collectionName, Filters.and(Filters.eq(searchField, searchFieldValue),
|
||||
Filters.gte(refDateName, begin.getTime()), Filters.lte(refDateName, end.getTime())));
|
||||
return qty;
|
||||
}
|
||||
@@ -33,7 +37,7 @@ public class AggregateCommandImpl implements AggregateCommand {
|
||||
public long distinctCount(String modelId, String searchField, Object searchFieldValue, String refDateName,
|
||||
Date begin, Date end, String distinctBy) {
|
||||
String collectionName = "entity_" + modelId;
|
||||
Long qty = MongodbUtil.distinctCount(collectionName, Filters.and(Filters.eq(searchField, searchFieldValue),
|
||||
Long qty = mongoService.distinctCount(collectionName, Filters.and(Filters.eq(searchField, searchFieldValue),
|
||||
Filters.gte(refDateName, begin.getTime()), Filters.lte(refDateName, end.getTime())), distinctBy);
|
||||
return qty;
|
||||
}
|
||||
@@ -49,7 +53,7 @@ public class AggregateCommandImpl implements AggregateCommand {
|
||||
Document group = new Document("$group", new Document("_id", null).append("sum", new Document("$sum", "$"
|
||||
+ funcField)));
|
||||
List<Bson> pipeline = Arrays.asList(match, group);
|
||||
AggregateIterable<Document> it = MongodbUtil.aggregate(collectionName, pipeline);
|
||||
AggregateIterable<Document> it = mongoService.aggregate(collectionName, pipeline);
|
||||
Document doc = it.first();
|
||||
if (doc != null) {
|
||||
sum = new BigDecimal(doc.get("sum").toString());
|
||||
@@ -70,7 +74,7 @@ public class AggregateCommandImpl implements AggregateCommand {
|
||||
Document group = new Document("$group", new Document("_id", null).append("avg", new Document("$avg", "$"
|
||||
+ funcField)));
|
||||
List<Bson> pipeline = Arrays.asList(match, group);
|
||||
AggregateIterable<Document> it = MongodbUtil.aggregate(collectionName, pipeline);
|
||||
AggregateIterable<Document> it = mongoService.aggregate(collectionName, pipeline);
|
||||
Document doc = it.first();
|
||||
if (doc != null) {
|
||||
avg = new BigDecimal(doc.get("avg").toString());
|
||||
@@ -88,7 +92,7 @@ public class AggregateCommandImpl implements AggregateCommand {
|
||||
|
||||
Document sort = new Document("$sort", new Document(funcField, 1));
|
||||
List<Bson> pipeline = Arrays.asList(match, sort);
|
||||
AggregateIterable<Document> it = MongodbUtil.aggregate(collectionName, pipeline);
|
||||
AggregateIterable<Document> it = mongoService.aggregate(collectionName, pipeline);
|
||||
List<Document> docList = new ArrayList<>();
|
||||
it.forEach(new Block<Document>() {
|
||||
|
||||
@@ -132,7 +136,7 @@ public class AggregateCommandImpl implements AggregateCommand {
|
||||
Document group = new Document("$group", new Document("_id", null).append("max", new Document("$max", "$"
|
||||
+ funcField)));
|
||||
List<Bson> pipeline = Arrays.asList(match, group);
|
||||
AggregateIterable<Document> it = MongodbUtil.aggregate(collectionName, pipeline);
|
||||
AggregateIterable<Document> it = mongoService.aggregate(collectionName, pipeline);
|
||||
Document doc = it.first();
|
||||
if (doc != null) {
|
||||
max = new BigDecimal(doc.get("max").toString());
|
||||
@@ -151,7 +155,7 @@ public class AggregateCommandImpl implements AggregateCommand {
|
||||
Document group = new Document("$group", new Document("_id", null).append("min", new Document("$min", "$"
|
||||
+ funcField)));
|
||||
List<Bson> pipeline = Arrays.asList(match, group);
|
||||
AggregateIterable<Document> it = MongodbUtil.aggregate(collectionName, pipeline);
|
||||
AggregateIterable<Document> it = mongoService.aggregate(collectionName, pipeline);
|
||||
Document doc = it.first();
|
||||
if (doc != null) {
|
||||
min = new BigDecimal(doc.get("min").toString());
|
||||
@@ -187,7 +191,7 @@ public class AggregateCommandImpl implements AggregateCommand {
|
||||
String collectionName = "entity_" + modelId;
|
||||
Bson filter = Filters.and(Filters.eq(searchField, searchFieldValue), Filters.gte(refDateName, begin.getTime()),
|
||||
Filters.lte(refDateName, end.getTime()));
|
||||
FindIterable<Document> findIt = MongodbUtil.find(collectionName, filter);
|
||||
FindIterable<Document> findIt = mongoService.find(collectionName, filter);
|
||||
List<BigDecimal> records = new ArrayList<>();
|
||||
BigDecimal sum = new BigDecimal("0");
|
||||
findIt.forEach(new Block<Document>() {
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.pgmmers.radar.service.impl.model;
|
||||
import com.pgmmers.radar.dal.model.ModelDal;
|
||||
import com.pgmmers.radar.service.cache.CacheService;
|
||||
import com.pgmmers.radar.service.cache.SubscribeHandle;
|
||||
import com.pgmmers.radar.service.impl.util.MongodbUtil;
|
||||
import com.pgmmers.radar.service.data.MongoService;
|
||||
import com.pgmmers.radar.service.model.EntityService;
|
||||
import com.pgmmers.radar.vo.model.ModelVO;
|
||||
import java.util.ArrayList;
|
||||
@@ -28,6 +28,9 @@ public class EntityServiceImpl implements EntityService, SubscribeHandle {
|
||||
@Autowired
|
||||
private CacheService cacheService;
|
||||
|
||||
@Autowired
|
||||
private MongoService mongoService;
|
||||
|
||||
private List<ModelVO> modelList = new ArrayList<>();
|
||||
|
||||
@PostConstruct
|
||||
@@ -53,13 +56,13 @@ public class EntityServiceImpl implements EntityService, SubscribeHandle {
|
||||
}
|
||||
Document filter = new Document();
|
||||
filter.append(model.getEntryName(), doc.get(model.getEntryName()));
|
||||
long qty = MongodbUtil.count(tmpUrl, filter);
|
||||
long qty = mongoService.count(tmpUrl, filter);
|
||||
if (qty > 0) {
|
||||
logger.info("record has already exsit!");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
MongodbUtil.insert(tmpUrl, doc);
|
||||
mongoService.insert(tmpUrl, doc);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -96,13 +99,13 @@ public class EntityServiceImpl implements EntityService, SubscribeHandle {
|
||||
//设置查询条件
|
||||
Document filter = new Document();
|
||||
filter.append(model.getEntryName(), doc.get(model.getEntryName()));
|
||||
long qty = MongodbUtil.count(tmpUrl, filter);
|
||||
long qty = mongoService.count(tmpUrl, filter);
|
||||
if (qty > 0) {
|
||||
logger.info("record has already exsit!");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
MongodbUtil.insert(tmpUrl, doc);
|
||||
mongoService.insert(tmpUrl, doc);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ import com.pgmmers.radar.enums.StatusType;
|
||||
import com.pgmmers.radar.service.cache.CacheService;
|
||||
import com.pgmmers.radar.service.cache.SubscribeHandle;
|
||||
import com.pgmmers.radar.service.common.CommonResult;
|
||||
import com.pgmmers.radar.service.impl.util.MongodbUtil;
|
||||
import com.pgmmers.radar.service.data.MongoService;
|
||||
import com.pgmmers.radar.service.model.ModelService;
|
||||
import com.pgmmers.radar.service.search.SearchEngineService;
|
||||
import com.pgmmers.radar.vo.model.FieldVO;
|
||||
@@ -52,6 +52,9 @@ public class ModelServiceImpl implements ModelService, SubscribeHandle {
|
||||
@Autowired
|
||||
private SearchEngineService searchService;
|
||||
|
||||
@Autowired
|
||||
private MongoService mongoService;
|
||||
|
||||
private List<ModelVO> modelList = new ArrayList<>();
|
||||
|
||||
@PostConstruct
|
||||
@@ -171,8 +174,8 @@ public class ModelServiceImpl implements ModelService, SubscribeHandle {
|
||||
List<FieldVO> fields = modelDal.listField(id);
|
||||
List<PreItemVO> items = modelDal.listPreItem(id, null);
|
||||
String collectionName = "entity_" + id;
|
||||
MongodbUtil.mongoTemplate.getCollection(collectionName).drop();
|
||||
MongodbUtil.mongoTemplate.createCollection(collectionName);
|
||||
mongoService.getCollection(collectionName).drop();
|
||||
mongoService.getMongoTemplate().createCollection(collectionName);
|
||||
List<IndexModel> indexes = new ArrayList<>();
|
||||
|
||||
if (fields == null) {
|
||||
@@ -197,7 +200,7 @@ public class ModelServiceImpl implements ModelService, SubscribeHandle {
|
||||
|
||||
indexes.add(ttlIndex);
|
||||
|
||||
MongodbUtil.getCollection(collectionName).createIndexes(indexes);
|
||||
mongoService.getCollection(collectionName).createIndexes(indexes);
|
||||
//
|
||||
|
||||
// 重建es index
|
||||
|
||||
@@ -12,7 +12,13 @@ import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
/**
|
||||
* 重新封装类。
|
||||
* @author feihu.wang
|
||||
* @since 2020.04.15
|
||||
*/
|
||||
@Deprecated
|
||||
//@Component
|
||||
public class MongodbUtil implements InitializingBean {
|
||||
|
||||
public static MongoTemplate mongoTemplate;
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.pgmmers.radar.service.data;
|
||||
|
||||
import com.mongodb.client.AggregateIterable;
|
||||
import com.mongodb.client.FindIterable;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import org.bson.BsonValue;
|
||||
import org.bson.Document;
|
||||
import org.bson.conversions.Bson;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* mongo db access entry.
|
||||
*
|
||||
* @author feihu.wang
|
||||
* @since 2020-04-15
|
||||
*/
|
||||
public interface MongoService {
|
||||
|
||||
MongoTemplate getMongoTemplate();
|
||||
|
||||
default MongoCollection<Document> getCollection(String collectionName) {
|
||||
return getMongoTemplate().getCollection(collectionName);
|
||||
}
|
||||
|
||||
default void insert(String collectionName, Document doc) {
|
||||
MongoCollection<Document> collection = getCollection(collectionName);
|
||||
collection.insertOne(doc);
|
||||
}
|
||||
|
||||
default long count(String collectionName, Bson filter) {
|
||||
MongoCollection<Document> collection = getCollection(collectionName);
|
||||
return collection.countDocuments(filter);
|
||||
}
|
||||
|
||||
default long distinctCount(String collectionName, Bson filter, String fieldName) {
|
||||
MongoCollection<Document> collection = getCollection(collectionName);
|
||||
long count = 0;
|
||||
Iterator<BsonValue> it = collection.distinct(fieldName, filter, BsonValue.class).iterator();
|
||||
while (it.hasNext()) {
|
||||
it.next();
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
default AggregateIterable<Document> aggregate(String collectionName, List<Bson> pipeline) {
|
||||
MongoCollection<Document> collection = getCollection(collectionName);
|
||||
return collection.aggregate(pipeline);
|
||||
}
|
||||
|
||||
default FindIterable<Document> find(String collectionName, Bson filter) {
|
||||
MongoCollection<Document> collection = getCollection(collectionName);
|
||||
return collection.find(filter);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user