1.封装mongodb查询操作

master
x_z 3 years ago
parent f501525bdf
commit ba44eff5e3

@ -0,0 +1,165 @@
package com.glxp.udi.admin.mongo.dao;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import com.glxp.udi.admin.entity.inout.OrderMdEntity;
import com.glxp.udi.admin.mongo.MongoDBName;
import com.glxp.udi.admin.mongo.dao.base.MongoBaseDao;
import com.glxp.udi.admin.mongo.utils.MongoPageHelper;
import com.glxp.udi.admin.mongo.utils.PageResult;
import com.glxp.udi.admin.req.inout.OrderFilterRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.List;
/**
*
*/
@Component
public class OrderDao implements MongoBaseDao<OrderMdEntity, OrderFilterRequest> {
@Resource
private MongoTemplate mongoTemplate;
@Resource
private MongoPageHelper mongoPageHelper;
@Override
public PageResult<OrderMdEntity> pageQuery(Query query, OrderFilterRequest orderFilterRequest, int page, int size) {
if (null == query && orderFilterRequest != null) {
query = buildQuery(orderFilterRequest);
}
return mongoPageHelper.pageQuery(query, OrderMdEntity.class, page, size, MongoDBName.NAME_ORDER);
}
@Override
public List<OrderMdEntity> findList(Query query, OrderFilterRequest orderFilterRequest) {
if (null == query && null != orderFilterRequest) {
query = buildQuery(orderFilterRequest);
}
return mongoTemplate.find(query, OrderMdEntity.class, MongoDBName.NAME_ORDER);
}
@Override
public OrderMdEntity findOne(Query query, OrderFilterRequest orderFilterRequest) {
if (query == null && null != orderFilterRequest) {
query = buildQuery(orderFilterRequest);
}
return mongoTemplate.findOne(query, OrderMdEntity.class, MongoDBName.NAME_ORDER);
}
@Override
public void update(Query query, Update update, OrderMdEntity orderMdEntity) {
if (null == query) {
query = buildQuery(orderMdEntity);
}
if (null == update) {
update = buildUpdate(orderMdEntity);
}
mongoTemplate.updateMulti(query, update, OrderMdEntity.class, MongoDBName.NAME_ORDER);
}
@Override
public void delete(Query query, OrderFilterRequest orderFilterRequest) {
if (null == query && null != orderFilterRequest) {
query = buildQuery(orderFilterRequest);
}
mongoTemplate.remove(query, OrderMdEntity.class, MongoDBName.NAME_ORDER);
}
@Override
public long count(Query query, OrderFilterRequest orderFilterRequest) {
if (null == query && null != orderFilterRequest) {
query = buildQuery(orderFilterRequest);
}
return mongoTemplate.count(query, OrderMdEntity.class, MongoDBName.NAME_ORDER);
}
@Override
public void insert(OrderMdEntity orderMdEntity) {
mongoTemplate.insert(orderMdEntity, MongoDBName.NAME_ORDER);
}
@Override
public void insert(List<OrderMdEntity> records) {
mongoTemplate.insert(records, MongoDBName.NAME_ORDER);
}
/**
* mongodb
*
* @param orderFilterRequest
* @return
*/
private Query buildQuery(OrderFilterRequest orderFilterRequest) {
Query query = new Query();
Field[] fields = ReflectUtil.getFields(OrderFilterRequest.class);
for (Field field : fields) {
Object fieldValue = ReflectUtil.getFieldValue(orderFilterRequest, field);
if (ObjectUtil.isNotNull(fieldValue)) {
if ("id".equals(field.getName())) {
query.addCriteria(Criteria.where("orderId").is(fieldValue));
} else if ("invCodes".equals(field.getName())) {
query.addCriteria(Criteria.where("locStorageCode").in(fieldValue));
} else if ("actDate".equals(field.getName())) {
query.addCriteria(Criteria.where("actDate").gte(fieldValue + " 00:00:00").andOperator(Criteria.where("actDate").lte(fieldValue + " 23:59:59")));
} else if ("isError".equals(field.getName())) {
query.addCriteria(Criteria.where("status").ne(fieldValue));
} else {
query.addCriteria(Criteria.where(field.getName()).is(fieldValue));
}
} else {
if ("receiveStatus".equals(field.getName())) {
query.addCriteria(Criteria.where("receiveStatus").ne(0));
}
}
}
return query;
}
/**
* mongodb
*
* @param orderMdEntity
* @return
*/
private Query buildQuery(OrderMdEntity orderMdEntity) {
Query query = new Query();
Field[] fields = ReflectUtil.getFields(OrderMdEntity.class);
for (Field field : fields) {
Object fieldValue = ReflectUtil.getFieldValue(orderMdEntity, field);
if (ObjectUtil.isNotEmpty(fieldValue)) {
if ("id".equals(field.getName())) {
query.addCriteria(Criteria.where("_id").is(fieldValue));
}
}
}
return query;
}
/**
* mongodb
*
* @param orderMdEntity
* @return
*/
private Update buildUpdate(OrderMdEntity orderMdEntity) {
Update update = new Update();
Field[] fields = ReflectUtil.getFields(OrderMdEntity.class);
for (Field field : fields) {
Object fieldValue = ReflectUtil.getFieldValue(orderMdEntity, field);
if (ObjectUtil.isNotEmpty(fieldValue)) {
if (!field.getName().equals("id") && !field.getName().equals("orderId")) {
update.set(field.getName(), fieldValue);
}
}
}
return update;
}
}

@ -0,0 +1,157 @@
package com.glxp.udi.admin.mongo.dao;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import com.glxp.udi.admin.entity.inout.ErpOrderEntity;
import com.glxp.udi.admin.mongo.MongoDBName;
import com.glxp.udi.admin.mongo.dao.base.MongoBaseDao;
import com.glxp.udi.admin.mongo.utils.MongoPageHelper;
import com.glxp.udi.admin.mongo.utils.PageResult;
import com.glxp.udi.admin.req.inout.FilterErpOrderRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
@Component
public class OrderDetailDao implements MongoBaseDao<ErpOrderEntity, FilterErpOrderRequest> {
@Resource
private MongoTemplate mongoTemplate;
@Resource
private MongoPageHelper mongoPageHelper;
@Override
public PageResult<ErpOrderEntity> pageQuery(Query query, FilterErpOrderRequest filterErpOrderRequest, int page, int size) {
if (null == query && null != filterErpOrderRequest) {
query = buildQuery(filterErpOrderRequest);
}
return mongoPageHelper.pageQuery(query, ErpOrderEntity.class, page, size, MongoDBName.NAME_ORDER_DETAIL);
}
@Override
public List<ErpOrderEntity> findList(Query query, FilterErpOrderRequest filterErpOrderRequest) {
if (null == query && null != filterErpOrderRequest) {
query = buildQuery(filterErpOrderRequest);
}
return mongoTemplate.find(query, ErpOrderEntity.class, MongoDBName.NAME_ORDER_DETAIL);
}
@Override
public ErpOrderEntity findOne(Query query, FilterErpOrderRequest filterErpOrderRequest) {
if (null == query && null != filterErpOrderRequest) {
query = buildQuery(filterErpOrderRequest);
}
return mongoTemplate.findOne(query, ErpOrderEntity.class, MongoDBName.NAME_ORDER_DETAIL);
}
@Override
public void update(Query query, Update update, ErpOrderEntity erpOrderEntity) {
if (null == query) {
query = buildQuery(erpOrderEntity);
}
if (null == update) {
update = buildUpdate(erpOrderEntity);
}
mongoTemplate.updateMulti(query, update, ErpOrderEntity.class, MongoDBName.NAME_ORDER_DETAIL);
}
@Override
public void delete(Query query, FilterErpOrderRequest filterErpOrderRequest) {
if (null == query) {
query = buildQuery(filterErpOrderRequest);
}
mongoTemplate.remove(query, ErpOrderEntity.class, MongoDBName.NAME_ORDER_DETAIL);
}
@Override
public long count(Query query, FilterErpOrderRequest filterErpOrderRequest) {
if (null == query) {
query = buildQuery(filterErpOrderRequest);
}
return mongoTemplate.count(query, ErpOrderEntity.class, MongoDBName.NAME_ORDER_DETAIL);
}
@Override
public void insert(ErpOrderEntity erpOrderEntity) {
mongoTemplate.insert(erpOrderEntity, MongoDBName.NAME_ORDER_DETAIL);
}
@Override
public void insert(List<ErpOrderEntity> records) {
mongoTemplate.insert(records, MongoDBName.NAME_ORDER_DETAIL);
}
/**
* mongodb
*
* @param filterErpOrderRequest
* @return
*/
private Query buildQuery(FilterErpOrderRequest filterErpOrderRequest) {
Query query = new Query();
Field[] fields = ReflectUtil.getFields(FilterErpOrderRequest.class);
Arrays.stream(fields).forEach(field -> {
Object fieldValue = ReflectUtil.getFieldValue(filterErpOrderRequest, field);
if (ObjectUtil.isNotEmpty(fieldValue)) {
if ("orderId".equals(field.getName())) {
query.addCriteria(Criteria.where("orderIdFk").is(fieldValue));
} else {
query.addCriteria(Criteria.where(field.getName()).is(fieldValue));
}
}
});
return query;
}
/**
* mongodb
*
* @param erpOrderEntity
* @return
*/
private Query buildQuery(ErpOrderEntity erpOrderEntity) {
Query query = new Query();
Field[] fields = ReflectUtil.getFields(ErpOrderEntity.class);
Arrays.stream(fields).forEach(field -> {
Object fieldValue = ReflectUtil.getFieldValue(erpOrderEntity, field);
if (ObjectUtil.isNotEmpty(fieldValue)) {
if ("orderId".equals(field.getName())) {
query.addCriteria(Criteria.where("orderIdFk").is(fieldValue));
} else {
query.addCriteria(Criteria.where(field.getName()).is(fieldValue));
}
}
});
return query;
}
/**
*
*
* @param erpOrderEntity
* @return
*/
private Update buildUpdate(ErpOrderEntity erpOrderEntity) {
Update update = new Update();
Field[] fields = ReflectUtil.getFields(ErpOrderEntity.class);
Arrays.stream(fields).forEach(field -> {
Object fieldValue = ReflectUtil.getFieldValue(erpOrderEntity, field);
if (ObjectUtil.isNotEmpty(fieldValue)) {
if (!"orderId".equals(field.getName()) && !"orderIdFk".equals(field.getName())) {
update.set(field.getName(), fieldValue);
}
}
});
return update;
}
}

@ -0,0 +1,84 @@
package com.glxp.udi.admin.mongo.dao.base;
import com.glxp.udi.admin.mongo.utils.PageResult;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import java.util.List;
/**
* mongo
*/
public interface MongoBaseDao<T, Q> {
/**
*
*
* @param query
* @param q
* @param page
* @param size
* @return
*/
PageResult<T> pageQuery(Query query, Q q, int page, int size);
/**
*
*
* @param query
* @param q
* @return
*/
List<T> findList(Query query, Q q);
/**
*
*
* @param query
* @param q
* @return
*/
T findOne(Query query, Q q);
/**
*
*
* @param query
* @param update
* @param t
*/
void update(Query query, Update update, T t);
/**
*
*
* @param query
* @param q
*/
void delete(Query query, Q q);
/**
*
*
* @param query
* @param q
* @return
*/
long count(Query query, Q q);
/**
*
*
* @param t
*/
void insert(T t);
/**
*
*
* @param records
*/
void insert(List<T> records);
}
Loading…
Cancel
Save