yujian
2020-05-06 eb1adc13eda955e8ef0fc9fe41fb246fa89b722d
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
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.ArticleOfficial;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.vo.dynamic.ArticleVO;
 
@Repository
public class ArticleOfficialDao {
 
    @Resource
    private MongoTemplate mongoTemplate;
 
    /**
     * 新增
     * 
     * @param record
     */
    public void save(ArticleOfficial 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, ArticleOfficial.class);
    }
 
    /**
     * 根据id查询数据
     * 
     * @param id
     * @return
     */
    public ArticleOfficial getById(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id));
        return mongoTemplate.findOne(query, ArticleOfficial.class);
    }
    
    
    /**
     * 删除
     * 
     * @param id
     * @return
     */
    public void deleteById(String id) {
        ArticleOfficial info = getById(id);
        if (info == null) {
            return;
        }
        mongoTemplate.remove(info);
    }
 
    /**
     * 查询
     * 
     * @return
     */
    public List<ArticleOfficial> 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)),
                                   new Criteria().andOperator(Criteria.where("content").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE))),
                                   new Criteria().andOperator(Criteria.where("tags").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE)))
                        ));
        // 分页
        query.skip(start).limit(count);
        query.with(new Sort(Sort.Direction.DESC,"weight"));
        
        return mongoTemplate.find(query, ArticleOfficial.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)),
                                   new Criteria().andOperator(Criteria.where("content").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE))),
                                   new Criteria().andOperator(Criteria.where("tags").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE)))
                        ));
        return mongoTemplate.count(query, ArticleOfficial.class);
    }
 
    /**
     * 查询有效
     * @param start
     * @param count
     * @param key
     * @return
     */
    public List<ArticleVO> queryValid(int start, int count, String key) {
        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));
        if (!StringUtil.isNullOrEmpty(key))
            query.addCriteria(new Criteria().orOperator(
                           Criteria.where("title").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE)),
                                   new Criteria().andOperator(Criteria.where("content").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE))),
                                   new Criteria().andOperator(Criteria.where("tags").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, ArticleVO.class);
    }
 
    /**
     * 统计有效
     * @param key
     * @return
     */
    public long countValid(String key) {
        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));
        if (!StringUtil.isNullOrEmpty(key))
            query.addCriteria(new Criteria().orOperator(
                           Criteria.where("title").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE)),
                                   new Criteria().andOperator(Criteria.where("content").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE))),
                                   new Criteria().andOperator(Criteria.where("tags").regex(Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE)))
                        ));
        return mongoTemplate.count(query, ArticleOfficial.class);
    }
 
}