From ba44eff5e393fbd381e29b8938cf0c0d757289c8 Mon Sep 17 00:00:00 2001 From: x_z Date: Wed, 14 Sep 2022 18:18:54 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=B0=81=E8=A3=85mongodb=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../glxp/udi/admin/mongo/dao/OrderDao.java | 165 ++++++++++++++++++ .../udi/admin/mongo/dao/OrderDetailDao.java | 157 +++++++++++++++++ .../admin/mongo/dao/base/MongoBaseDao.java | 84 +++++++++ 3 files changed, 406 insertions(+) create mode 100644 src/main/java/com/glxp/udi/admin/mongo/dao/OrderDao.java create mode 100644 src/main/java/com/glxp/udi/admin/mongo/dao/OrderDetailDao.java create mode 100644 src/main/java/com/glxp/udi/admin/mongo/dao/base/MongoBaseDao.java diff --git a/src/main/java/com/glxp/udi/admin/mongo/dao/OrderDao.java b/src/main/java/com/glxp/udi/admin/mongo/dao/OrderDao.java new file mode 100644 index 0000000..c174870 --- /dev/null +++ b/src/main/java/com/glxp/udi/admin/mongo/dao/OrderDao.java @@ -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 { + + @Resource + private MongoTemplate mongoTemplate; + @Resource + private MongoPageHelper mongoPageHelper; + + + @Override + public PageResult 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 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 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; + } +} diff --git a/src/main/java/com/glxp/udi/admin/mongo/dao/OrderDetailDao.java b/src/main/java/com/glxp/udi/admin/mongo/dao/OrderDetailDao.java new file mode 100644 index 0000000..9ce921f --- /dev/null +++ b/src/main/java/com/glxp/udi/admin/mongo/dao/OrderDetailDao.java @@ -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 { + + @Resource + private MongoTemplate mongoTemplate; + @Resource + private MongoPageHelper mongoPageHelper; + + + @Override + public PageResult 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 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 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; + } + +} diff --git a/src/main/java/com/glxp/udi/admin/mongo/dao/base/MongoBaseDao.java b/src/main/java/com/glxp/udi/admin/mongo/dao/base/MongoBaseDao.java new file mode 100644 index 0000000..0056cac --- /dev/null +++ b/src/main/java/com/glxp/udi/admin/mongo/dao/base/MongoBaseDao.java @@ -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 { + + /** + * 分页查询 + * + * @param query 查询条件 + * @param q 查询参数实体,用于构造查询条件 + * @param page 页码 + * @param size 条数 + * @return + */ + PageResult pageQuery(Query query, Q q, int page, int size); + + /** + * 查询集合 + * + * @param query 查询条件 + * @param q 查询参数实体,用于构造查询条件 + * @return + */ + List 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 records); + + +}