yujian
2020-03-16 8dcc1ffea99306ebaa9b48fb739f0b627f706d84
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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.mongodb.BasicDBObject;
import com.mongodb.QueryOperators;
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 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,"createTime"));
        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);
    }
}