yujian
2020-05-21 ccc87b5ac3d6271f95bb8bd89d2f38d765365702
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
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 org.springframework.data.domain.Sort;
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.dao.MongodbBaseDao;
import com.yeshi.fanli.dto.user.ThreeSaleFocusDTO;
import com.yeshi.fanli.entity.bus.user.TeamFansInfo;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TimeUtil;
 
@Repository
public class TeamFansInfoDao extends MongodbBaseDao<TeamFansInfo> {
 
 
    /**
     * 选择性更新
     * @param record
     */
    public void updateSelective(TeamFansInfo record) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(record.getId()));
 
        Update update = new Update();
        if (record.getNickName() != null)
            update.set("nickName", record.getNickName());
        if (record.getLevel() != null)
            update.set("level", record.getLevel().name());
        if (record.getStateValid() != null)
            update.set("stateValid", record.getStateValid());
        if (record.getTaobaoBind() != null)
            update.set("taobaoBind", record.getTaobaoBind());
        if (record.getWeixinId() != null)
            update.set("weixinId", record.getWeixinId());
        if (record.getWeixinIdExist() != null)
            update.set("weixinIdExist", record.getWeixinIdExist());
        if (record.getPhone() != null)
            update.set("phone", record.getPhone());
        if (record.getPhoneOpen() != null)
            update.set("phoneOpen", record.getPhoneOpen());
        if (record.getInviteCode() != null)
            update.set("inviteCode", record.getInviteCode());
        if (record.getActiveTime() != null)
            update.set("activeTime", record.getActiveTime());
        if (record.getFansNum() != null)
            update.set("fansNum", record.getFansNum());
        if (record.getIncome() != null)
            update.set("income", record.getIncome());
        if (record.getIncomeTime() != null)
            update.set("incomeTime", record.getIncomeTime());
        if (record.getMemoName() != null)
            update.set("memoName", record.getMemoName());
        if (record.getTags() != null)
            update.set("tags", record.getTags());
        if (record.getMemoNameSup() != null)
            update.set("memoNameSup", record.getMemoNameSup());
        if (record.getTagsSup() != null)
            update.set("tagsSup", record.getTagsSup());
        update.set("updateTime", new Date());
        update(query, update);
    }
 
    
    
    /**
     * 统计
     * 
     * @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, "joinTime"));
        return findList(query);
    }
 
    /**
     * 统计
     * 
     * @return
     */
    public long count(int type, Long uid, ThreeSaleFocusDTO focusDTO) {
        Query query = createQuery(type, uid, focusDTO);
        return count(query);
    }
 
    private Query createQuery(int type, Long uid, ThreeSaleFocusDTO focusDTO) {
        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("weixinIdExist").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 (focusDTO.getMinIncome() != null) {
                int income = focusDTO.getMinIncome().multiply(BigDecimal.valueOf(100)).setScale(2, BigDecimal.ROUND_DOWN).intValue();
                list.add(Criteria.where("income").gte(income));
            }
 
            if (focusDTO.getMaxIncome() != null) {
                int income = focusDTO.getMaxIncome().multiply(BigDecimal.valueOf(100)).setScale(2, BigDecimal.ROUND_DOWN).intValue();
                list.add(Criteria.where("income").lte(income));
            }
            
            if (!StringUtil.isNullOrEmpty(focusDTO.getMinJoinTime())) {
                Date date = TimeUtil.parseDotYYYYMMDD(focusDTO.getMinJoinTime());
                list.add(Criteria.where("joinTime").gte(date));
            }
 
            if (!StringUtil.isNullOrEmpty(focusDTO.getMaxJoinTime())) {
                Date date = TimeUtil.parseDotCommon(focusDTO.getMaxJoinTime() + " 23:59:59");
                list.add(Criteria.where("joinTime").lte(date));
            }
            
            if (!StringUtil.isNullOrEmpty(focusDTO.getMinActiveTime())) {
                Date date = TimeUtil.parseDotYYYYMMDD(focusDTO.getMinActiveTime());
                list.add(Criteria.where("activeTime").gte(date));
            }
 
            if (!StringUtil.isNullOrEmpty(focusDTO.getMaxActiveTime())) {
                Date date = TimeUtil.parseDotCommon(focusDTO.getMaxActiveTime() + " 23:59:59");
                list.add(Criteria.where("activeTime").lte(date));
            }
 
            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("weixinId")
                                            .regex(Pattern.compile("^.*" + focusDTO.getKey() + ".*$",
                                                    Pattern.CASE_INSENSITIVE))),
                                    new Criteria().andOperator(Criteria.where("inviteCode")
                                            .regex(Pattern.compile("^.*" + focusDTO.getKey() + ".*$",
                                                    Pattern.CASE_INSENSITIVE))),
                                    new Criteria().andOperator(Criteria.where("phone")
                                            .regex(Pattern.compile("^.*" + focusDTO.getKey() + ".*$",
                                                    Pattern.CASE_INSENSITIVE)).andOperator(Criteria.where("phoneOpen").is(true))),
                                    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("weixinId")
                                            .regex(Pattern.compile("^.*" + focusDTO.getKey() + ".*$",
                                                    Pattern.CASE_INSENSITIVE))),
                                    new Criteria().andOperator(Criteria.where("inviteCode")
                                            .regex(Pattern.compile("^.*" + focusDTO.getKey() + ".*$",
                                                    Pattern.CASE_INSENSITIVE))),
                                    new Criteria().andOperator(Criteria.where("phone")
                                            .regex(Pattern.compile("^.*" + focusDTO.getKey() + ".*$",
                                                    Pattern.CASE_INSENSITIVE)).andOperator(Criteria.where("phoneOpen").is(true))),
                                    new Criteria().andOperator(Criteria.where("tagsSup").regex(Pattern
                                            .compile("^.*" + focusDTO.getKey() + ".*$", Pattern.CASE_INSENSITIVE)))));
                }
            }
        }
 
        Query query = new Query();
        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;
    }
 
    
    /**
     * 根据用户信息匹配
     * @param key
     * @return
     */
    public List<TeamFansInfo> queryByUserInfo(String key) {
        if (StringUtil.isNullOrEmpty(key))
            return null;
        
        Long workerId = null;
        try {
            workerId = Long.parseLong(key);
        } catch (Exception e) {
        }
        
        Criteria criteria = null;
        if (workerId == null) {
            criteria = new Criteria().orOperator(Criteria.where("phone").is(key),
                    new Criteria().orOperator(Criteria.where("inviteCode").is(key)));
        } else {
            criteria = new Criteria().orOperator(Criteria.where("phone").is(key),
                    new Criteria().orOperator(Criteria.where("inviteCode").is(key)),
                    new Criteria().orOperator(Criteria.where("workerId").is(workerId)));
        }
        
        Query query = new Query(criteria);
        return findList(query);
    }
 
}