yujian
2020-05-12 fb885c8bff26484f2bb21e697f182cc35bc4fa63
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package com.yeshi.fanli.dao.user.invite;
 
import java.math.BigDecimal;
import java.util.ArrayList;
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.dto.user.ThreeSaleFocusDTO;
import com.yeshi.fanli.entity.bus.user.TeamFansInfo;
import com.yeshi.fanli.util.StringUtil;
 
@Repository
public class TeamFansInfoDao {
 
    @Resource
    private MongoTemplate mongoTemplate;
 
    /**
     * 新增
     * 
     * @param record
     */
    public void save(TeamFansInfo record) {
        if (record == null) {
            return;
        }
        mongoTemplate.save(record);
    }
 
    /**
     * 更新标签
     * 
     * @param record
     */
    public void updateMemoName(Long workerId, int type, String memoName) {
        if (memoName == null) {
            memoName = "";
        }
 
        Query query = new Query();
        query.addCriteria(Criteria.where("workerId").is(workerId));
 
        Update update = null;
        if (type == 1) {
            update = Update.update("memoName", memoName);
        } else {
            update = Update.update("memoNameSup", memoName);
        }
        mongoTemplate.updateFirst(query, update, TeamFansInfo.class);
    }
 
    /**
     * 更新标签
     * 
     * @param record
     */
    public void updateTags(Long workerId, int type, String tags) {
        if (tags == null) {
            tags = "";
        }
 
        Query query = new Query();
        query.addCriteria(Criteria.where("workerId").is(workerId));
 
        Update update = null;
        if (type == 1) {
            update = Update.update("tags", tags);
        } else {
            update = Update.update("tagsSup", tags);
        }
        mongoTemplate.updateFirst(query, update, TeamFansInfo.class);
    }
 
    /**
     * 更新昵称
     * 
     * @param workerId
     * @param nickName
     */
    public void updateNickName(Long workerId, String nickName) {
        if (nickName == null) {
            nickName = "";
        }
 
        Query query = new Query();
        query.addCriteria(Criteria.where("workerId").is(workerId));
        Update update = Update.update("nickName", nickName);
        mongoTemplate.updateFirst(query, update, TeamFansInfo.class);
    }
 
    /**
     * 更新是否有效粉丝
     * 
     * @param workerId
     * @param stateValid
     */
    public void updateStateValid(Long workerId, boolean stateValid) {
        Query query = new Query();
        query.addCriteria(Criteria.where("workerId").is(workerId));
        Update update = Update.update("stateValid", stateValid);
        mongoTemplate.updateFirst(query, update, TeamFansInfo.class);
    }
 
    /**
     * 更新淘宝绑定
     * 
     * @param workerId
     * @param taobaoBind
     */
    public void updateTaobaoBind(Long workerId, boolean taobaoBind) {
        Query query = new Query();
        query.addCriteria(Criteria.where("workerId").is(workerId));
        Update update = Update.update("taobaoBind", taobaoBind);
        mongoTemplate.updateFirst(query, update, TeamFansInfo.class);
    }
 
    /**
     * 更新微信绑定
     * 
     * @param workerId
     * @param weixinBind
     */
    public void updateWeixinBind(Long workerId, boolean weixinBind) {
        Query query = new Query();
        query.addCriteria(Criteria.where("workerId").is(workerId));
        Update update = Update.update("weixinBind", weixinBind);
        mongoTemplate.updateFirst(query, update, TeamFansInfo.class);
    }
 
    /**
     * 更新活跃时间
     * 
     * @param workerId
     * @param activeTime
     */
    public void updateActiveTime(Long workerId, Date activeTime) {
        Query query = new Query();
        query.addCriteria(Criteria.where("workerId").is(workerId));
        Update update = Update.update("activeTime", activeTime);
        mongoTemplate.updateFirst(query, update, TeamFansInfo.class);
    }
 
    /**
     * 更新粉丝数量
     * 
     * @param workerId
     * @param fansNum
     */
    public void updateFansNum(Long workerId, int fansNum) {
        Query query = new Query();
        query.addCriteria(Criteria.where("workerId").is(workerId));
        Update update = Update.update("fansNum", fansNum);
        mongoTemplate.updateFirst(query, update, TeamFansInfo.class);
    }
 
    /**
     * 更新收入
     * 
     * @param workerId
     * @param income
     */
    public void updateIncome(Long workerId, BigDecimal income) {
        Query query = new Query();
        query.addCriteria(Criteria.where("workerId").is(workerId));
        Update update = Update.update("income", income).set("incomeTime", new Date());
        mongoTemplate.updateMulti(query, update, TeamFansInfo.class);
    }
 
    /**
     * 统计
     * 
     * @return
     */
    public TeamFansInfo getbyWorkerId(Long workerId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("workerId").is(workerId));
        return mongoTemplate.findOne(query, TeamFansInfo.class);
    }
 
    /**
     * 查询
     * 
     * @return
     */
    public List<TeamFansInfo> query(int start, int count, int type, Long uid, ThreeSaleFocusDTO focusDTO) {
        Query query = createQuery(type, uid, focusDTO);
        query.skip(start).limit(count);
        query.with(new Sort(Sort.Direction.DESC, "weight"));
        return mongoTemplate.find(query, TeamFansInfo.class);
    }
 
    /**
     * 统计
     * 
     * @return
     */
    public long count(int type, Long uid, ThreeSaleFocusDTO focusDTO) {
        Query query = createQuery(type, uid, focusDTO);
        return mongoTemplate.count(query, TeamFansInfo.class);
    }
 
    private Query createQuery(int type, Long uid, ThreeSaleFocusDTO focusDTO) {
        Query query = new Query();
        List<Criteria> list = new ArrayList<Criteria>();
 
        if (type == 1) {
            list.add(Criteria.where("bossId").is(uid));
        } else {
            list.add(Criteria.where("bossSuperId").is(uid));
        }
 
        if (focusDTO != null) {
            if (focusDTO.getStateValid() != null)
                list.add(Criteria.where("stateValid").is(focusDTO.getStateValid()));
 
            if (focusDTO.getTaobaoBind() != null)
                list.add(Criteria.where("taobaoBind").is(focusDTO.getTaobaoBind()));
 
            if (focusDTO.getWeixinBind() != null)
                list.add(Criteria.where("weixinBind").is(focusDTO.getWeixinBind()));
 
            if (!StringUtil.isNullOrEmpty(focusDTO.getLevel()))
                list.add(Criteria.where("level").is(focusDTO.getLevel()));
 
            if (focusDTO.getMinFansNum() != null)
                list.add(Criteria.where("fansNum").gte(focusDTO.getMinFansNum()));
 
            if (focusDTO.getMaxFansNum() != null)
                list.add(Criteria.where("fansNum").lte(focusDTO.getMaxFansNum()));
 
            if (!StringUtil.isNullOrEmpty(focusDTO.getMinJoinTime()))
                list.add(Criteria.where("joinTime").gte(focusDTO.getMinJoinTime()));
 
            if (!StringUtil.isNullOrEmpty(focusDTO.getMaxJoinTime()))
                list.add(Criteria.where("joinTime").lte(focusDTO.getMaxJoinTime()));
 
            if (!StringUtil.isNullOrEmpty(focusDTO.getMinActiveTime()))
                list.add(Criteria.where("activeTime").gte(focusDTO.getMinActiveTime()));
 
            if (!StringUtil.isNullOrEmpty(focusDTO.getMaxActiveTime()))
                list.add(Criteria.where("activeTime").lte(focusDTO.getMaxActiveTime()));
 
            if (focusDTO.getMinIncome() != null)
                list.add(Criteria.where("income").gte(focusDTO.getMinIncome()));
 
            if (focusDTO.getMaxIncome() != null)
                list.add(Criteria.where("income").lte(focusDTO.getMaxIncome()));
 
            if (!StringUtil.isNullOrEmpty(focusDTO.getKey())) {
                if (type == 1) {
                    list.add(new Criteria().orOperator(    Criteria.where("nickName")
                                            .regex(Pattern.compile("^.*" + focusDTO.getKey() + ".*$",
                                                    Pattern.CASE_INSENSITIVE)),
                                    new Criteria().andOperator(Criteria.where("memoName")
                                            .regex(Pattern.compile("^.*" + focusDTO.getKey() + ".*$",
                                                    Pattern.CASE_INSENSITIVE))),
                                    new Criteria().andOperator(Criteria.where("tags").regex(Pattern
                                            .compile("^.*" + focusDTO.getKey() + ".*$", Pattern.CASE_INSENSITIVE)))));
                } else {
                    list.add(new Criteria().orOperator(Criteria.where("nickName")
                                            .regex(Pattern.compile("^.*" + focusDTO.getKey() + ".*$",
                                                    Pattern.CASE_INSENSITIVE)),
                                    new Criteria().andOperator(Criteria.where("memoNameSup")
                                            .regex(Pattern.compile("^.*" + focusDTO.getKey() + ".*$",
                                                    Pattern.CASE_INSENSITIVE))),
                                    new Criteria().andOperator(Criteria.where("tagsSup").regex(Pattern
                                            .compile("^.*" + focusDTO.getKey() + ".*$", 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 query;
    }
 
}