@Autowired
private MongoTemplate mongoTemplate; //注入mongotemplate
/**
* 根据条件查询符合条件的记录
*/
@Override
public List findByCondition(MemberLabel memberLabel) {
int currentPage=memberLabel.getCurrentPage();//当前页
int pageSize=memberLabel.getPageSize();//页面大小
String sortType=memberLabel.getSortType();//排序类型
String sortField=memberLabel.getSortField();//排序字段
Query query=getMongoQuery(memberLabel);//生成Query(自定义方法)
if("asc".equals(sortType)){
query.with(new Sort(Sort.Direction.ASC,sortField));//添加排序功能
}else{
query.with(new Sort(Sort.Direction.DESC,sortField));//添加排序功能
}
query.skip((currentPage-1)*pageSize);//使用skip加limit实现分页
query.limit(pageSize);
System.out.println(query);//可以直接打印query,输出格式为BSON格式
List lists=mongoTemplate.find(query, MemberLabel.class);//调用mongotemplate的find方法,查询记录。
return lists;
}
/**
* 根据条件生成Query
*其中包含(A OR B)and (C OR D)查询 ;A
*/
public Query getMongoQuery(Object obj){
Query query = new Query();
if(null == obj){
return query;
}
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
List lists=new ArrayList();//会员类型复选模块OR查询
List lists1=new ArrayList();//特殊场景复选模块OR查询
Criteria cri1=Criteria.where("highQualityRate");//存放优质率
int cri1count=0;//判断是否传入优质率查询条件
Criteria cri2=Criteria.where("lowQualityRate");//存放淘汰率
int cri2count=0;//判断是否传入淘汰率查询条件
Criteria cri3=Criteria.where("modifyDate");//存放修改时间
int cri3count=0;//判断是否传入修改时间查询条件
for(PropertyDescriptor propertyDescriptor : propertyDescriptors){
if("class".equals(propertyDescriptor.getName())){
continue;
}
Method readMethod = propertyDescriptor.getReadMethod();
Object value = readMethod.invoke(obj);
if(null != value && !"".equals(value) && !"0".equals(value.toString()) && !"0.0".equals(value.toString())){//跳过空参数的拼接
if("currentPage".equals(propertyDescriptor.getDisplayName()) || "pageSize".equals(propertyDescriptor.getDisplayName()) || "sortType".equals(propertyDescriptor.getDisplayName()) || "sortField".equals(propertyDescriptor.getDisplayName())){
continue;
}//跳过排序分页参数的拼接
if("memberSource".equals(propertyDescriptor.getDisplayName())){//会员来源
String memberSources[]=value.toString().split(",");
query.addCriteria(Criteria.where("memberSource").in(memberSources));//in方法
}else if("memberStatus".equals(propertyDescriptor.getDisplayName())){//会员状态
String memberStatus[]=value.toString().split(",");
query.addCriteria(Criteria.where("memberStatus").in(memberStatus));
}else if("memberType".equals(propertyDescriptor.getDisplayName())){//会员类型
String memberTypes[]=value.toString().split(",");
lists.add(Criteria.where("memberType").in(memberTypes));
}else if("isBcp".equals(propertyDescriptor.getDisplayName())){//国富泰认真
lists.add(Criteria.where("isBcp").is(value));
}else if("isCredit".equals(propertyDescriptor.getDisplayName())){//实名认证
lists.add(Criteria.where("isBcp").is(value));
}else if("isBrokerage".equals(propertyDescriptor.getDisplayName())){//佣金会员
lists.add(Criteria.where("isBrokerage").is(value));
}else if("isTest".equals(propertyDescriptor.getDisplayName())){//测试
lists1.add(Criteria.where("isTest").ne("NT"));
}else if("isNc".equals(propertyDescriptor.getDisplayName())){//假企业用户
lists1.add(Criteria.where("isNc").is(value));
}else if("memberRepeat".equals(propertyDescriptor.getDisplayName())){//会员重复
lists1.add(Criteria.where("memberRepeat").is(value));
}else if("corporationName".equals(propertyDescriptor.getDisplayName())){//(模糊查询)
query.addCriteria(Criteria.where("corporationName").regex(".*?" + value + ".*"));
}else if("highQualityRateBegin".equals(propertyDescriptor.getDisplayName())){//优质率
cri1.gte(value);
cri1count++;
}else if("highQualityRateEnd".equals(propertyDescriptor.getDisplayName())){
cri1.lte(value);
cri1count++;
}else if("lowQualityRateBegin".equals(propertyDescriptor.getDisplayName())){//淘汰率
cri2.gte(value);
cri2count++;
}else if("lowQualityRateEnd".equals(propertyDescriptor.getDisplayName())){
cri2.lte(value);
cri2count++;
}else if("modifyDateStart".equals(propertyDescriptor.getDisplayName())){//修改时间
cri3.gte(value);
cri3count++;
}else if("modifyDateEnd".equals(propertyDescriptor.getDisplayName())){
cri3.lte(value);
cri3count++;
}else{
query.addCriteria(Criteria.where(propertyDescriptor.getDisplayName()).is(value));
}
}
}
Criteria arrs[]=new Criteria[lists.size()];
for(int i=0,j=lists.size();i
arrs[i]=lists.get(i);
}
Criteria arrs1[]=new Criteria[lists1.size()];
for(int i=0,j=lists1.size();i
arrs1[i]=lists1.get(i);
}
if(lists.size()>0 && lists1.size()>0){//实现 (A OR B)and (C OR D)式查询
query.addCriteria(new Criteria().andOperator(new Criteria().orOperator(arrs),new Criteria().orOperator(arrs1)));
}else if(lists.size()>0){
query.addCriteria(new Criteria().orOperator(arrs));
}else if(lists1.size()>0){
query.addCriteria(new Criteria().orOperator(arrs1));
}
if(cri1count>0){// a>3 and a<4式查询
query.addCriteria(cri1);
}
if(cri2count>0){
query.addCriteria(cri2);
}
if(cri3count>0){
query.addCriteria(cri3);
}
return query;
} catch (IntrospectionException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return query;
}