yujian
2020-05-12 374b2786507c2d5242dd0ff0676e8d529b6940b9
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
package com.yeshi.fanli.service.impl.user.vip;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
 
import com.yeshi.fanli.dao.mybatis.user.vip.TeamUserLevelStatisticMapper;
import com.yeshi.fanli.entity.bus.user.ThreeSale;
import com.yeshi.fanli.entity.bus.user.UserInviteValidRecord;
import com.yeshi.fanli.entity.bus.user.vip.TeamUserLevelStatistic;
import com.yeshi.fanli.entity.bus.user.vip.UserLevelEnum;
import com.yeshi.fanli.service.inter.user.invite.ThreeSaleSerivce;
import com.yeshi.fanli.service.inter.user.vip.TeamUserLevelStatisticService;
import com.yeshi.fanli.service.manger.user.UserLevelManager;
 
@Service
public class TeamUserLevelStatisticServiceImpl implements TeamUserLevelStatisticService {
 
    @Resource
    private UserLevelManager userLevelManager;
 
    @Resource
    private TeamUserLevelStatisticMapper teamUserLevelStatisticMapper;
 
    @Resource
    private ThreeSaleSerivce threeSaleSerivce;
 
    @Override
    public TeamUserLevelStatistic selectByUid(Long uid) {
        return teamUserLevelStatisticMapper.selectByPrimaryKey(uid);
    }
 
    @Override
    public List<TeamUserLevelStatistic> listByUids(List<Long> uids) {
 
        List<TeamUserLevelStatistic> resultList = new ArrayList<>();
 
        List<TeamUserLevelStatistic> list = teamUserLevelStatisticMapper.listByUids(uids);
        // 放入Map中
        Map<Long, TeamUserLevelStatistic> map = new HashMap<>();
        if (list != null)
            for (TeamUserLevelStatistic s : list) {
                map.put(s.getId(), s);
            }
 
        for (Long uid : uids) {
            if (map.get(uid) == null) {
                initData(uid);
                TeamUserLevelStatistic statistic = selectByUid(uid);
                resultList.add(statistic);
            } else {
                resultList.add(map.get(uid));
            }
        }
 
        return resultList;
    }
 
    @Override
    public void initData(Long uid) {
        int pageSize = 1000;
 
        // 直接粉丝统计
        int daRenFirstCount = 0;
        int highFirstCount = 0;
        int superFirstCount = 0;
        int tearcherFirstCount = 0;
        for (int i = 0; i < 100; i++) {
            List<ThreeSale> list = threeSaleSerivce.listFirstTeam(i * pageSize, pageSize, uid);
            if (list == null || list.size() == 0) {
                break;
            }
 
            for (ThreeSale ts : list) {
                UserLevelEnum level = userLevelManager.getUserLevel(ts.getWorker().getId());
                if (level == UserLevelEnum.daRen || level == UserLevelEnum.normalVIP) {
                    daRenFirstCount++;
                } else if (level == UserLevelEnum.highVIP) {
                    highFirstCount++;
                } else if (level == UserLevelEnum.superVIP) {
                    superFirstCount++;
                } else if (level == UserLevelEnum.tearcher) {
                    tearcherFirstCount++;
                }
            }
        }
 
        // 间接粉丝统计
        int daRenSecondCount = 0;
        int highSecondCount = 0;
        int superSecondCount = 0;
        int tearcherSecondCount = 0;
        for (int i = 0; i < 100; i++) {
            List<ThreeSale> list  = threeSaleSerivce.listSecondTeam(i * pageSize, pageSize, uid);
            if (list == null || list.size() == 0) {
                break;
            }
 
            for (ThreeSale ts : list) {
                UserLevelEnum level = userLevelManager.getUserLevel(ts.getWorker().getId());
                if (level == UserLevelEnum.daRen || level == UserLevelEnum.normalVIP) {
                    daRenSecondCount++;
                } else if (level == UserLevelEnum.highVIP) {
                    highSecondCount++;
                } else if (level == UserLevelEnum.superVIP) {
                    superSecondCount++;
                } else if (level == UserLevelEnum.tearcher) {
                    tearcherSecondCount++;
                }
            }
        }
 
        UserLevelEnum level = userLevelManager.getUserLevel(uid);
        TeamUserLevelStatistic statistic = new TeamUserLevelStatistic();
        statistic.setDaRenFirstCount(daRenFirstCount);
        statistic.setDaRenSecondCount(daRenSecondCount);
        statistic.setHighFirstCount(highFirstCount);
        statistic.setHighSecondCount(highSecondCount);
        statistic.setSuperFirstCount(superFirstCount);
        statistic.setSuperSecondCount(superSecondCount);
        statistic.setTearcherFirstCount(tearcherFirstCount);
        statistic.setTearcherSecondCount(tearcherSecondCount);
        statistic.setId(uid);
        statistic.setLevel(level);
        add(statistic);
    }
 
    @Override
    public void setUserLevel(Long uid, UserLevelEnum level) {
 
        TeamUserLevelStatistic old = selectByUid(uid);
        if (old == null) {
            initData(uid);
            old = selectByUid(uid);
        }
 
        TeamUserLevelStatistic update = new TeamUserLevelStatistic();
        update.setId(old.getId());
        update.setLevel(level);
        update.setUpdateTime(new Date());
        teamUserLevelStatisticMapper.updateByPrimaryKeySelective(update);
    }
 
    @Override
    public void add(TeamUserLevelStatistic statistic) {
        TeamUserLevelStatistic old = selectByUid(statistic.getId());
        if (old == null) {
            if (statistic.getCreateTime() == null)
                statistic.setCreateTime(new Date());
            teamUserLevelStatisticMapper.insertSelective(statistic);
        } else {
            statistic.setUpdateTime(new Date());
            teamUserLevelStatisticMapper.updateByPrimaryKeySelective(statistic);
        }
    }
 
    @Override
    public void updateUserLevel(Long uid) {
        UserLevelEnum level = userLevelManager.getUserLevel(uid);
        TeamUserLevelStatistic statistic = new TeamUserLevelStatistic();
        statistic.setId(uid);
        statistic.setLevel(level);
        statistic.setUpdateTime(new Date());
        teamUserLevelStatisticMapper.updateByPrimaryKeySelective(statistic);
    }
 
}