admin
2020-02-18 ddf5db7d31af51fcf697eac0cee7dd46ff73bcc1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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.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);
    }
 
    /**
     * 根据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) {
        Query query = new Query();
        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) {
        Query query = new Query();
        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) {
        Date now = new Date();
        Query query = new Query();
        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,"createTime"));
        return mongoTemplate.find(query, GoodsEvaluate.class);
    }
 
    /**
     * 统计有效
     * @param key
     * @return
     */
    public long countValid() {
        Date now = new Date();
        Query query = new Query();
        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);
    }
 
}