From 7e7db2fa55a9a3af46d4fd8ede0dee147f101d64 Mon Sep 17 00:00:00 2001 From: yujian <yujian@163.com> Date: 星期六, 09 五月 2020 21:41:27 +0800 Subject: [PATCH] 2.1需求 --- fanli/src/main/java/com/yeshi/fanli/dao/dynamic/GoodsEvaluateDao.java | 127 +++++++++++++++++++++++++++++++++--------- 1 files changed, 100 insertions(+), 27 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/dao/dynamic/GoodsEvaluateDao.java b/fanli/src/main/java/com/yeshi/fanli/dao/dynamic/GoodsEvaluateDao.java index 5f8832e..1b167d0 100644 --- a/fanli/src/main/java/com/yeshi/fanli/dao/dynamic/GoodsEvaluateDao.java +++ b/fanli/src/main/java/com/yeshi/fanli/dao/dynamic/GoodsEvaluateDao.java @@ -1,5 +1,6 @@ package com.yeshi.fanli.dao.dynamic; +import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.regex.Pattern; @@ -96,31 +97,57 @@ * * @return */ - public List<GoodsEvaluate> query(int start, int count, String key, Integer state, int dynamicType) { + public List<GoodsEvaluate> query(int start, int count, String key, Integer state, int dynamicType, String typeEnum) { Query query = new Query(); - query.addCriteria(Criteria.where("dynamicType").is(dynamicType)); + List<Criteria> list = new ArrayList<Criteria>(); + list.add(Criteria.where("dynamicType").is(dynamicType)); + if (state != null) { - query.addCriteria(Criteria.where("state").is(state)); + list.add(Criteria.where("state").is(state)); } + + if (!StringUtil.isNullOrEmpty(typeEnum)) + list.add(Criteria.where("type").is(typeEnum)); + if (!StringUtil.isNullOrEmpty(key)) - query.addCriteria(new Criteria().orOperator( + list.add(new Criteria().orOperator( Criteria.where("title").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE)))); - + + if (list.size() > 0) { + Criteria[] cas = new Criteria[list.size()]; + for (int i = 0; i < list.size(); i++) + cas[i] = list.get(i); + query.addCriteria(new Criteria().andOperator(cas)); + } + query.skip(start).limit(count); query.with(new Sort(Sort.Direction.DESC,"weight")).with(new Sort(Sort.Direction.DESC,"createTime")); return mongoTemplate.find(query, GoodsEvaluate.class); } - public long count(String key, Integer state, int dynamicType) { + public long count(String key, Integer state, int dynamicType, String typeEnum) { Query query = new Query(); - query.addCriteria(Criteria.where("dynamicType").is(dynamicType)); - + List<Criteria> list = new ArrayList<Criteria>(); + list.add(Criteria.where("dynamicType").is(dynamicType)); + if (state != null) { - query.addCriteria(Criteria.where("state").is(state)); + list.add(Criteria.where("state").is(state)); } + + if (!StringUtil.isNullOrEmpty(typeEnum)) + list.add(Criteria.where("type").is(typeEnum)); + if (!StringUtil.isNullOrEmpty(key)) - query.addCriteria(new Criteria().orOperator( + list.add(new Criteria().orOperator( Criteria.where("title").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE)))); + + if (list.size() > 0) { + Criteria[] cas = new Criteria[list.size()]; + for (int i = 0; i < list.size(); i++) + cas[i] = list.get(i); + query.addCriteria(new Criteria().andOperator(cas)); + } + return mongoTemplate.count(query, GoodsEvaluate.class); } @@ -134,10 +161,20 @@ public List<GoodsEvaluate> queryValid(int start, int count, int dynamicType) { Date now = new Date(); Query query = new Query(); - query.addCriteria(Criteria.where("dynamicType").is(dynamicType)); - query.addCriteria(Criteria.where("state").is(1)); - query.addCriteria(Criteria.where("startTime").lte(now)); - query.addCriteria(Criteria.where("endTime").gte(now)); + List<Criteria> list = new ArrayList<Criteria>(); + + list.add(Criteria.where("dynamicType").is(dynamicType)); + list.add(Criteria.where("state").is(1)); + list.add(Criteria.where("startTime").lte(now)); + list.add(Criteria.where("endTime").gte(now)); + + if (list.size() > 0) { + Criteria[] cas = new Criteria[list.size()]; + for (int i = 0; i < list.size(); i++) + cas[i] = list.get(i); + query.addCriteria(new Criteria().andOperator(cas)); + } + query.skip(start).limit(count); query.with(new Sort(Sort.Direction.DESC,"weight")).with(new Sort(Sort.Direction.DESC,"startTime")); return mongoTemplate.find(query, GoodsEvaluate.class); @@ -151,10 +188,19 @@ public long countValid(int dynamicType) { Date now = new Date(); Query query = new Query(); - query.addCriteria(Criteria.where("dynamicType").is(dynamicType)); - query.addCriteria(Criteria.where("state").is(1)); - query.addCriteria(Criteria.where("startTime").lte(now)); - query.addCriteria(Criteria.where("endTime").gte(now)); + List<Criteria> list = new ArrayList<Criteria>(); + + list.add(Criteria.where("dynamicType").is(dynamicType)); + list.add(Criteria.where("state").is(1)); + list.add(Criteria.where("startTime").lte(now)); + list.add(Criteria.where("endTime").gte(now)); + + if (list.size() > 0) { + Criteria[] cas = new Criteria[list.size()]; + for (int i = 0; i < list.size(); i++) + cas[i] = list.get(i); + query.addCriteria(new Criteria().andOperator(cas)); + } return mongoTemplate.count(query, GoodsEvaluate.class); } @@ -168,9 +214,18 @@ */ public List<GoodsEvaluate> querySingleExist() { Query query = new Query(); - query.addCriteria(Criteria.where("dynamicType").is(1)); - query.addCriteria(Criteria.where("state").is(1)); - query.addCriteria(Criteria.where("goods.goodsType").is(1)); + List<Criteria> list = new ArrayList<Criteria>(); + + list.add(Criteria.where("dynamicType").is(1)); + list.add(Criteria.where("state").is(1)); + list.add(Criteria.where("goods.goodsType").is(1)); + + if (list.size() > 0) { + Criteria[] cas = new Criteria[list.size()]; + for (int i = 0; i < list.size(); i++) + cas[i] = list.get(i); + query.addCriteria(new Criteria().andOperator(cas)); + } return mongoTemplate.find(query, GoodsEvaluate.class); } @@ -184,10 +239,19 @@ */ public List<GoodsEvaluate> queryExist(int goodsType, Long goodsId) { Query query = new Query(); - query.addCriteria(Criteria.where("dynamicType").is(1)); - query.addCriteria(Criteria.where("state").is(1)); - query.addCriteria(Criteria.where("imgList.goods.goodsId").is(goodsId)); - query.addCriteria(Criteria.where("imgList.goods.goodsType").is(goodsType)); + List<Criteria> list = new ArrayList<Criteria>(); + list.add(Criteria.where("dynamicType").is(1)); + list.add(Criteria.where("state").is(1)); + list.add(Criteria.where("imgList.goods.goodsId").is(goodsId)); + list.add(Criteria.where("imgList.goods.goodsType").is(goodsType)); + + if (list.size() > 0) { + Criteria[] cas = new Criteria[list.size()]; + for (int i = 0; i < list.size(); i++) + cas[i] = list.get(i); + query.addCriteria(new Criteria().andOperator(cas)); + } + return mongoTemplate.find(query, GoodsEvaluate.class); } @@ -217,8 +281,17 @@ */ public List<GoodsEvaluate> removeDownGoods() { Query query = new Query(); - query.addCriteria(Criteria.where("type").is("single")); - query.addCriteria(Criteria.where("goods.state").is(1)); + List<Criteria> list = new ArrayList<Criteria>(); + list.add(Criteria.where("type").is("single")); + list.add(Criteria.where("goods.state").is(1)); + + if (list.size() > 0) { + Criteria[] cas = new Criteria[list.size()]; + for (int i = 0; i < list.size(); i++) + cas[i] = list.get(i); + query.addCriteria(new Criteria().andOperator(cas)); + } + return mongoTemplate.find(query, GoodsEvaluate.class); } } -- Gitblit v1.8.0