|
|
|
@ -0,0 +1,182 @@
|
|
|
|
|
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.InvProductEntity;
|
|
|
|
|
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.inventory.FilterInvProductRequest;
|
|
|
|
|
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;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
@Component
|
|
|
|
|
public class InvProductDao implements MongoBaseDao<InvProductEntity, FilterInvProductRequest> {
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private MongoTemplate mongoTemplate;
|
|
|
|
|
@Resource
|
|
|
|
|
private MongoPageHelper mongoPageHelper;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public PageResult<InvProductEntity> pageQuery(Query query, FilterInvProductRequest filterInvProductRequest, int page, int size) {
|
|
|
|
|
if (null == query && null != filterInvProductRequest) {
|
|
|
|
|
query = buildQuery(filterInvProductRequest);
|
|
|
|
|
}
|
|
|
|
|
return mongoPageHelper.pageQuery(query, InvProductEntity.class, page, size, MongoDBName.NAME_INV_PRODUCT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<InvProductEntity> findList(Query query, FilterInvProductRequest filterInvProductRequest) {
|
|
|
|
|
if (null == query && null != filterInvProductRequest) {
|
|
|
|
|
query = buildQuery(filterInvProductRequest);
|
|
|
|
|
}
|
|
|
|
|
return mongoTemplate.find(query, InvProductEntity.class, MongoDBName.NAME_INV_PRODUCT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public InvProductEntity findOne(Query query, FilterInvProductRequest filterInvProductRequest) {
|
|
|
|
|
if (null == query && null != filterInvProductRequest) {
|
|
|
|
|
query = buildQuery(filterInvProductRequest);
|
|
|
|
|
}
|
|
|
|
|
return mongoTemplate.findOne(query, InvProductEntity.class, MongoDBName.NAME_INV_PRODUCT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void update(Query query, Update update, InvProductEntity invProductEntity) {
|
|
|
|
|
if (null == query) {
|
|
|
|
|
query = buildQuery(invProductEntity);
|
|
|
|
|
}
|
|
|
|
|
if (null == update) {
|
|
|
|
|
update = buildUpdate(invProductEntity);
|
|
|
|
|
}
|
|
|
|
|
mongoTemplate.updateMulti(query, update, InvProductEntity.class, MongoDBName.NAME_INV_PRODUCT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void delete(Query query, FilterInvProductRequest filterInvProductRequest) {
|
|
|
|
|
if (null == query && null != filterInvProductRequest) {
|
|
|
|
|
query = buildQuery(filterInvProductRequest);
|
|
|
|
|
}
|
|
|
|
|
mongoTemplate.remove(query, InvProductEntity.class, MongoDBName.NAME_INV_PRODUCT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public long count(Query query, FilterInvProductRequest filterInvProductRequest) {
|
|
|
|
|
if (null == query && null != filterInvProductRequest) {
|
|
|
|
|
query = buildQuery(filterInvProductRequest);
|
|
|
|
|
}
|
|
|
|
|
return mongoTemplate.count(query, InvProductEntity.class, MongoDBName.NAME_INV_PRODUCT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void insert(InvProductEntity invProductEntity) {
|
|
|
|
|
mongoTemplate.insert(invProductEntity, MongoDBName.NAME_INV_PRODUCT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void insert(List<InvProductEntity> records) {
|
|
|
|
|
mongoTemplate.insert(records, MongoDBName.NAME_INV_PRODUCT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据库存查询参数,构造mongodb查询条件
|
|
|
|
|
*
|
|
|
|
|
* @param filterInvProductRequest
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private Query buildQuery(FilterInvProductRequest filterInvProductRequest) {
|
|
|
|
|
Query query = new Query();
|
|
|
|
|
Field[] fields = ReflectUtil.getFields(FilterInvProductRequest.class);
|
|
|
|
|
Arrays.stream(fields).forEach(field -> {
|
|
|
|
|
Object fieldValue = ReflectUtil.getFieldValue(filterInvProductRequest, field);
|
|
|
|
|
if (ObjectUtil.isNotEmpty(fieldValue)) {
|
|
|
|
|
switch (field.getName()) {
|
|
|
|
|
case "uuids":
|
|
|
|
|
query.addCriteria(Criteria.where("relIdFk").in(fieldValue));
|
|
|
|
|
break;
|
|
|
|
|
case "invCodes":
|
|
|
|
|
query.addCriteria(Criteria.where("locStorageCode").in(fieldValue));
|
|
|
|
|
break;
|
|
|
|
|
case "cpmctymc": {
|
|
|
|
|
Pattern pattern = Pattern.compile("^.*" + fieldValue + ".*$");
|
|
|
|
|
query.addCriteria(Criteria.where("productsName").regex(pattern));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "zczbhhzbapzbh": {
|
|
|
|
|
Pattern pattern = Pattern.compile("^.*" + fieldValue + ".*$");
|
|
|
|
|
query.addCriteria(Criteria.where("zczbhhzbapzbh").regex(pattern));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "manufactory": {
|
|
|
|
|
Pattern pattern = Pattern.compile("^.*" + fieldValue + ".*$");
|
|
|
|
|
query.addCriteria(Criteria.where("manufactory").regex(pattern));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "ggxh": {
|
|
|
|
|
Pattern pattern = Pattern.compile("^.*" + fieldValue + ".*$");
|
|
|
|
|
query.addCriteria(Criteria.where("ggxh").regex(pattern));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
query.addCriteria(Criteria.where(field.getName()).is(fieldValue));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return query;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据库存实体,构造mongodb查询条件
|
|
|
|
|
*
|
|
|
|
|
* @param invProductEntity
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private Query buildQuery(InvProductEntity invProductEntity) {
|
|
|
|
|
Query query = new Query();
|
|
|
|
|
Field[] fields = ReflectUtil.getFields(InvProductEntity.class);
|
|
|
|
|
Arrays.stream(fields).forEach(field -> {
|
|
|
|
|
Object fieldValue = ReflectUtil.getFieldValue(invProductEntity, field);
|
|
|
|
|
if (ObjectUtil.isNotEmpty(fieldValue)) {
|
|
|
|
|
if ("id".equals(field.getName())) {
|
|
|
|
|
query.addCriteria(Criteria.where("_id").is(fieldValue));
|
|
|
|
|
} else {
|
|
|
|
|
query.addCriteria(Criteria.where(field.getName()).is(fieldValue));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return query;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据库存实体,构造mongodb更新条件
|
|
|
|
|
*
|
|
|
|
|
* @param invProductEntity
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private Update buildUpdate(InvProductEntity invProductEntity) {
|
|
|
|
|
Update update = new Update();
|
|
|
|
|
Field[] fields = ReflectUtil.getFields(InvProductEntity.class);
|
|
|
|
|
Arrays.stream(fields).forEach(field -> {
|
|
|
|
|
Object fieldValue = ReflectUtil.getFieldValue(invProductEntity, field);
|
|
|
|
|
if (ObjectUtil.isNotEmpty(fieldValue)) {
|
|
|
|
|
if (!"id".equals(field.getName())) {
|
|
|
|
|
update.set(field.getName(), fieldValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return update;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|