package com.yeshi.fanli.dao.dynamic;
|
|
import java.util.Date;
|
import java.util.List;
|
import java.util.regex.Pattern;
|
|
import javax.annotation.Resource;
|
|
import org.springframework.data.domain.Sort;
|
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.Repository;
|
|
import com.yeshi.fanli.entity.dynamic.GoodsEvaluate;
|
import com.yeshi.fanli.util.StringUtil;
|
|
@Repository
|
public class GoodsEvaluateDao {
|
|
@Resource
|
private MongoTemplate mongoTemplate;
|
|
/**
|
* 新增
|
*
|
* @param record
|
*/
|
public void save(GoodsEvaluate record) {
|
if (record == null) {
|
return;
|
}
|
mongoTemplate.save(record);
|
}
|
|
|
/**
|
* 状态切换
|
*
|
* @param record
|
*/
|
public void updateSatate(String id, int state) {
|
Query query = new Query();
|
query.addCriteria(Criteria.where("id").is(id));
|
|
Update update = Update.update("state", state);
|
mongoTemplate.updateMulti(query, update, GoodsEvaluate.class);
|
}
|
|
|
|
/**
|
* 删除
|
* @param record
|
*/
|
public void remove(GoodsEvaluate record) {
|
if (record == null) {
|
return;
|
}
|
mongoTemplate.remove(record);
|
}
|
|
|
|
|
/**
|
* 根据id查询数据
|
*
|
* @param id
|
* @return
|
*/
|
public GoodsEvaluate getById(String id) {
|
Query query = new Query();
|
query.addCriteria(Criteria.where("id").is(id));
|
return mongoTemplate.findOne(query, GoodsEvaluate.class);
|
}
|
|
|
/**
|
* 删除
|
*
|
* @param id
|
* @return
|
*/
|
public void deleteById(String id) {
|
GoodsEvaluate info = getById(id);
|
if (info == null) {
|
return;
|
}
|
mongoTemplate.remove(info);
|
}
|
|
/**
|
* 查询
|
*
|
* @return
|
*/
|
public List<GoodsEvaluate> query(int start, int count, String key, Integer state, int dynamicType) {
|
Query query = new Query();
|
query.addCriteria(Criteria.where("dynamicType").is(dynamicType));
|
if (state != null) {
|
query.addCriteria(Criteria.where("state").is(state));
|
}
|
if (!StringUtil.isNullOrEmpty(key))
|
query.addCriteria(new Criteria().orOperator(
|
Criteria.where("title").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE))));
|
|
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) {
|
Query query = new Query();
|
query.addCriteria(Criteria.where("dynamicType").is(dynamicType));
|
|
if (state != null) {
|
query.addCriteria(Criteria.where("state").is(state));
|
}
|
if (!StringUtil.isNullOrEmpty(key))
|
query.addCriteria(new Criteria().orOperator(
|
Criteria.where("title").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE))));
|
return mongoTemplate.count(query, GoodsEvaluate.class);
|
}
|
|
/**
|
* 查询有效
|
* @param start
|
* @param count
|
* @param key
|
* @return
|
*/
|
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));
|
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);
|
}
|
|
/**
|
* 统计有效
|
* @param key
|
* @return
|
*/
|
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));
|
return mongoTemplate.count(query, GoodsEvaluate.class);
|
}
|
|
|
/**
|
* 查询有效
|
* @param start
|
* @param count
|
* @param key
|
* @return
|
*/
|
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));
|
return mongoTemplate.find(query, GoodsEvaluate.class);
|
}
|
|
|
/**
|
* 查询有效
|
* @param start
|
* @param count
|
* @param key
|
* @return
|
*/
|
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));
|
return mongoTemplate.find(query, GoodsEvaluate.class);
|
}
|
|
|
/**
|
* 查询有效
|
* @param start
|
* @param count
|
* @param key
|
* @return
|
*/
|
public List<GoodsEvaluate> queryOverdue() {
|
Date now = new Date();
|
Query query = new Query();
|
query.addCriteria(Criteria.where("endTime").lte(now));
|
query.with(new Sort(Sort.Direction.ASC,"createTime"));
|
return mongoTemplate.find(query, GoodsEvaluate.class);
|
}
|
|
|
/**
|
* 查询有效
|
* @param start
|
* @param count
|
* @param key
|
* @return
|
*/
|
public List<GoodsEvaluate> removeDownGoods() {
|
Query query = new Query();
|
query.addCriteria(Criteria.where("type").is("single"));
|
query.addCriteria(Criteria.where("goods.state").is(1));
|
return mongoTemplate.find(query, GoodsEvaluate.class);
|
}
|
}
|