yujian
2020-05-16 aa9776a39b03e4eab22a42bd40156eabb7ddf75a
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package com.yeshi.fanli.service.impl.user.invite;
 
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
 
import com.yeshi.fanli.dao.user.invite.TeamFansInfoDao;
import com.yeshi.fanli.dto.user.ThreeSaleFocusDTO;
import com.yeshi.fanli.dto.vip.UserVIPLevel;
import com.yeshi.fanli.entity.bus.user.TeamFansInfo;
import com.yeshi.fanli.entity.bus.user.ThreeSale;
import com.yeshi.fanli.entity.bus.user.ThreeSaleExtraInfo;
import com.yeshi.fanli.entity.bus.user.UserActiveLog;
import com.yeshi.fanli.entity.bus.user.UserExtraTaoBaoInfo;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.bus.user.UserInfoExtra;
import com.yeshi.fanli.entity.bus.user.vip.UserLevelEnum;
import com.yeshi.fanli.service.inter.count.HongBaoV2CountService;
import com.yeshi.fanli.service.inter.order.CommonOrderService;
import com.yeshi.fanli.service.inter.user.UserActiveLogService;
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.service.inter.user.invite.TeamFansInfoService;
import com.yeshi.fanli.service.inter.user.invite.ThreeSaleExtraInfoSerivce;
import com.yeshi.fanli.service.inter.user.invite.ThreeSaleSerivce;
import com.yeshi.fanli.service.inter.user.invite.UserInviteService;
import com.yeshi.fanli.service.inter.user.tb.UserExtraTaoBaoInfoService;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.user.UserLevelUtil;
 
@Service
public class TeamFansInfoServiceImpl implements TeamFansInfoService {
 
    @Resource
    private TeamFansInfoDao teamFansInfoDao;
    
    @Resource
    private ThreeSaleSerivce threeSaleSerivce;
    
    @Resource
    private  UserInfoService userInfoService;
    
    @Resource
    private UserInfoExtraService userInfoExtraService;
    
    @Resource
    private ThreeSaleExtraInfoSerivce threeSaleExtraInfoSerivce;
    
    @Resource
    private UserExtraTaoBaoInfoService userExtraTaoBaoInfoService;
    
    @Resource
    private HongBaoV2CountService hongBaoV2CountService;
    
    @Resource
    private UserInviteService userInviteService;
    
    @Resource
    private CommonOrderService commonOrderService;
    
    @Resource
    private UserActiveLogService userActiveLogService;
 
    
    
    
    @Override
    public void save(TeamFansInfo record) {
        if (record == null || record.getWorkerId() == null ) {
            return;
        }
        record.setId(record.getWorkerId().toString());
        
        TeamFansInfo teamFansInfo = teamFansInfoDao.get(record.getId());
        if (teamFansInfo != null) {
            teamFansInfoDao.updateSelective(record);
        } else {
            initData(record);
            record.setCrateTime(new Date());
            teamFansInfoDao.save(record);
        }
    }
    
    
    /**
     * 初始化数据
     * @param record
     */
    private void initData(TeamFansInfo record) {
        if (StringUtil.isNullOrEmpty(record.getNickName()))
             record.setNickName("");
        if (record.getLevel() == null)
             record.setLevel(UserLevelEnum.daRen);
        if (record.getStateValid() == null)
            record.setStateValid(false);
        if (record.getTaobaoBind() == null)
             record.setTaobaoBind(false);
        if (record.getWeixinId() == null)
            record.setWeixinId("");
        if (record.getPhone() == null)
            record.setPhone("");
        if (record.getPhoneOpen() == null)
            record.setPhoneOpen(true);
        if (record.getInviteCode() == null)
            record.setInviteCode("");
        if (record.getActiveTime() == null)
            record.setActiveTime(new Date());
        if (record.getFansNum() == null)
            record.setFansNum(0);
        if (record.getIncome() == null)
            record.setIncome(0);
        if (record.getIncomeTime() == null)
            record.setIncomeTime(new Date());
        if (record.getMemoName() == null)
            record.setMemoName("");
        if (record.getTags() == null)
            record.setTags("");
        if (record.getMemoNameSup() == null)
            record.setMemoNameSup("");
        if (record.getTagsSup() == null)
            record.setTagsSup("");
        
        if (StringUtil.isNullOrEmpty(record.getWeixinId())) {
            record.setWeixinIdExist(false);
        } else {
            record.setWeixinIdExist(true);
        }
            
    }
    
    @Override
    public void addFansInfo(Long workerId, Long bossId, Date successTime) {
        UserInfo userInfo = userInfoService.selectByPKey(workerId);
        if (userInfo == null) {
            return;
        }
 
        String phone = "";
        if (!StringUtil.isNullOrEmpty(userInfo.getPhone())) {
            phone = userInfo.getPhone();
        }
 
        // 备注名
        String memoName = null;
        ThreeSaleExtraInfo threeSaleExtraInfo = threeSaleExtraInfoSerivce.getbyBossIdAndWorkerId(bossId,
                workerId);
        if (threeSaleExtraInfo != null) {
            if (!StringUtil.isNullOrEmpty(threeSaleExtraInfo.getNickname())) {
                memoName = threeSaleExtraInfo.getNickname();
            }
        }
 
        // 微信号、活跃时间
        String inviteCode = "";
        String weixinId = "";
        boolean weixinBind = false;
        UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(workerId);
        if (userInfoExtra != null) {
            String weiXin = userInfoExtra.getWeiXin();
            if (!StringUtil.isNullOrEmpty(weiXin)) {
                weixinId = weiXin;
                weixinBind = true;
            }
            String inviteCodeVip = userInfoExtra.getInviteCodeVip();
            String inviteCode2 = userInfoExtra.getInviteCode();
            if (!StringUtil.isNullOrEmpty(inviteCode2)) {
                if (!StringUtil.isNullOrEmpty(inviteCodeVip)) {
                    inviteCode = inviteCodeVip;
                } else {
                    inviteCode = inviteCode2;
                }
            }
        }
 
        Date activeTime = new Date(1514736000000L); // 默认20181-1-1;
        UserActiveLog userActiveLog = userActiveLogService.getUserLatestActiveInfo(workerId);
        if (userActiveLog != null) {
            activeTime = userActiveLog.getCreateTime();
        }
 
        // 淘宝授权
        boolean taobaoBind = false;
        UserExtraTaoBaoInfo taoBaoInfo = userExtraTaoBaoInfoService.getByUid(workerId);
        if (taoBaoInfo != null && !StringUtil.isNullOrEmpty(taoBaoInfo.getRelationId())
                && !StringUtil.isNullOrEmpty(taoBaoInfo.getSpecialId())) {
            taobaoBind = true;
        }
 
        // 粉丝数量
        long firstTeam = threeSaleSerivce.countFirstTeam(workerId);
        long secondTeam = threeSaleSerivce.countSecondTeam(workerId);
        int fansNum = (int) (firstTeam + secondTeam);
 
        // 收入
        BigDecimal income = hongBaoV2CountService.sumMoneyArrivalByUidAndNearDay(workerId, 60);
        if (income == null)
            income = new BigDecimal(0);
 
        // 有效粉丝
        boolean stateValid = false;
        long countValid = commonOrderService.countOrderByUidAndSettled(workerId, BigDecimal.valueOf(1));
        if (countValid > 0) {
            stateValid = true;
        }
 
        // 查询粉丝等级
        UserLevelEnum level = null;
        UserVIPLevel vipLevel = userInviteService.getVIPLevelByUid(workerId);
        if (vipLevel != null && vipLevel.getLevel() != null) {
            level = UserLevelUtil.getByLevel(vipLevel.getLevel());
        }
        if (level == null) {
            level = UserLevelEnum.daRen;
        }
        level = UserLevelUtil.getShowLevel(level);
 
        // 是否存在上上级
        Long bossSuperId = null;
        ThreeSale superThree = threeSaleSerivce.selectByWorkerId(bossId);
        if (superThree != null)
            bossSuperId = superThree.getBoss().getId();
 
        TeamFansInfo focusInfo = new TeamFansInfo();
        focusInfo.setBossId(bossId);
        focusInfo.setWorkerId(workerId);
        focusInfo.setBossSuperId(bossSuperId);
        focusInfo.setJoinTime(successTime);
        focusInfo.setNickName(userInfo.getNickName());
        focusInfo.setMemoName(memoName);
        focusInfo.setWeixinIdExist(weixinBind);
        focusInfo.setWeixinId(weixinId);
        focusInfo.setPhone(phone);
        focusInfo.setPhoneOpen(true);
        focusInfo.setInviteCode(inviteCode);
        focusInfo.setActiveTime(activeTime);
        focusInfo.setFansNum(fansNum);
        focusInfo.setTaobaoBind(taobaoBind);
        focusInfo.setIncome(income.multiply(BigDecimal.valueOf(100)).setScale(2, BigDecimal.ROUND_DOWN).intValue());
        focusInfo.setIncomeTime(new Date());
        focusInfo.setStateValid(stateValid);
        focusInfo.setLevel(level);
        focusInfo.setId(workerId.toString());
        save(focusInfo);
    }
    
    
    @Override
    public void updateMemoName(Long workerId, int type, String memoName) {
        TeamFansInfo teamFansInfo = new TeamFansInfo();
        teamFansInfo.setWorkerId(workerId);
        if (type == 1) {
            teamFansInfo.setMemoName(memoName);
        } else {
            teamFansInfo.setMemoNameSup(memoName);
        }
        save(teamFansInfo);
    }
    
    
    @Override
    public void updateTags(Long workerId, int type, String tags) {
        TeamFansInfo teamFansInfo = new TeamFansInfo();
        teamFansInfo.setWorkerId(workerId);
        if (type == 1) {
            teamFansInfo.setTags(tags);
        } else {
            teamFansInfo.setTagsSup(tags);
        }
        save(teamFansInfo);
    }
    
    
    @Override
    public void updateNickName(Long workerId, String nickName) {
        TeamFansInfo teamFansInfo = new TeamFansInfo();
        teamFansInfo.setWorkerId(workerId);
        teamFansInfo.setNickName(nickName);
        save(teamFansInfo);
    }
    
    
    @Override
    public void updateStateValid(Long workerId, boolean stateValid) {
        TeamFansInfo teamFansInfo = new TeamFansInfo();
        teamFansInfo.setWorkerId(workerId);
        teamFansInfo.setStateValid(stateValid);
        save(teamFansInfo);
    }
    
    @Override
    public void updateTaobaoBind(Long workerId, boolean taobaoBind) {
        TeamFansInfo teamFansInfo = new TeamFansInfo();
        teamFansInfo.setWorkerId(workerId);
        teamFansInfo.setTaobaoBind(taobaoBind);
        save(teamFansInfo);
    }
    
    @Override
    public void updateActiveTime(Long workerId, Date activeTime) {
        TeamFansInfo teamFansInfo = new TeamFansInfo();
        teamFansInfo.setWorkerId(workerId);
        teamFansInfo.setActiveTime(activeTime);
        save(teamFansInfo);
    }
 
    
    @Override
    public void updateWeixinId(Long workerId, String weixinId) {
        TeamFansInfo teamFansInfo = new TeamFansInfo();
        teamFansInfo.setWorkerId(workerId);
        teamFansInfo.setWeixinId(weixinId);
        if (StringUtil.isNullOrEmpty(weixinId)) {
            teamFansInfo.setWeixinIdExist(false);
        } else {
            teamFansInfo.setWeixinIdExist(true);
        }
        save(teamFansInfo);
    }
    
    
    @Override
    public void updateInviteCode(Long workerId, String inviteCode) {
        TeamFansInfo teamFansInfo = new TeamFansInfo();
        teamFansInfo.setWorkerId(workerId);
        teamFansInfo.setInviteCode(inviteCode);
        save(teamFansInfo);
    }
    
    @Override
    public void updatePhone(Long workerId, String phone) {
        TeamFansInfo teamFansInfo = new TeamFansInfo();
        teamFansInfo.setWorkerId(workerId);
        teamFansInfo.setPhone(phone);
        save(teamFansInfo);
    }
    
    @Override
    public void updateLevel(Long workerId, UserLevelEnum level) {
        TeamFansInfo teamFansInfo = new TeamFansInfo();
        teamFansInfo.setLevel(level);
        teamFansInfo.setWorkerId(workerId);
        save(teamFansInfo);
    }
    
    @Override
    public void updatePhoneOpen(Long workerId, boolean phoneOpen) {
        TeamFansInfo teamFansInfo = new TeamFansInfo();
        teamFansInfo.setWorkerId(workerId);
        teamFansInfo.setPhoneOpen(phoneOpen);
        save(teamFansInfo);
    }
    
    
    @Override
    public void updateIncome(Long workerId, BigDecimal income) {
        if (income == null) {
            income = BigDecimal.ZERO;
        }
        
        TeamFansInfo teamFansInfo = new TeamFansInfo();
        teamFansInfo.setWorkerId(workerId);
        teamFansInfo.setIncome(income.multiply(BigDecimal.valueOf(100)).setScale(0, BigDecimal.ROUND_DOWN).intValue());
        teamFansInfo.setIncomeTime(new Date());
        save(teamFansInfo);
    }
    
    @Override
    public void updateFansNum(Long workerId) {
        long firstTeam = threeSaleSerivce.countFirstTeam(workerId);
        long secondTeam = threeSaleSerivce.countSecondTeam(workerId);
        int fansNum = (int) (firstTeam + secondTeam);
        TeamFansInfo teamFansInfo = new TeamFansInfo();
        teamFansInfo.setWorkerId(workerId);
        teamFansInfo.setFansNum(fansNum);
        save(teamFansInfo);
    }
    
    
    @Override
    public List<TeamFansInfo> query(int start, int count, int type, Long uid, ThreeSaleFocusDTO focusDTO) {
        return teamFansInfoDao.query(start, count, type, uid, focusDTO);
    }
    
    @Override
    public long count(int type, Long uid, ThreeSaleFocusDTO focusDTO) {
        return teamFansInfoDao.count(type, uid, focusDTO);
    }
    
    @Override
    public TeamFansInfo getbyWorkerId(Long workerId) {
        return teamFansInfoDao.getbyWorkerId(workerId);
    }
}