yujian
2020-03-24 88c7df1d5089d8e39f356a68eaccc18f308e190c
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
package com.yeshi.fanli.service.impl.user.vip;
 
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.yeshi.fanli.dao.mybatis.user.vip.UserVIPPreInfoMapper;
import com.yeshi.fanli.dto.msg.MsgAccountVipDTO;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.bus.user.vip.UserVIPPreInfo;
import com.yeshi.fanli.exception.user.vip.UserVIPPreInfoException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.count.HongBaoV2CountService;
import com.yeshi.fanli.service.inter.user.invite.ThreeSaleSerivce;
import com.yeshi.fanli.service.inter.user.msg.MsgAccountDetailService;
import com.yeshi.fanli.service.inter.user.vip.UserVIPPreInfoService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.TimeUtil;
import com.yeshi.fanli.util.annotation.RequestSerializableByKeyService;
 
@Service
public class UserVIPPreInfoServiceImpl implements UserVIPPreInfoService {
 
    @Resource
    private UserVIPPreInfoMapper userVIPPreInfoMapper;
 
    @Resource
    private HongBaoV2CountService hongBaoV2CountService;
 
    @Resource
    private ThreeSaleSerivce threeSaleSerivce;
 
    @Resource
    private MsgAccountDetailService msgAccountDetailService;
 
    @Override
    public void addUserVIPPreInfo(UserVIPPreInfo info) throws UserVIPPreInfoException {
        if (info == null || info.getUid() == null || info.getProcess() == null)
            throw new UserVIPPreInfoException(1, "信息不完整");
 
        UserVIPPreInfo oldInfo = userVIPPreInfoMapper.selectByUidAndProcess(info.getUid(), info.getProcess());
        if (oldInfo != null)
            throw new UserVIPPreInfoException(2, "进度已存在");
        if (info.getCreateTime() == null)
            info.setCreateTime(new Date());
        userVIPPreInfoMapper.insertSelective(info);
    }
 
    @Override
    public UserVIPPreInfo getLatestProcessInfo(Long uid) {
        List<UserVIPPreInfo> infoList = userVIPPreInfoMapper.listByUid(uid);
        if (infoList == null || infoList.size() == 0)
            return null;
 
        Comparator<UserVIPPreInfo> cm = new Comparator<UserVIPPreInfo>() {
 
            @Override
            public int compare(UserVIPPreInfo o1, UserVIPPreInfo o2) {
                return o2.getProcess() - o1.getProcess();
            }
        };
 
        Collections.sort(infoList, cm);
        return infoList.get(0);
    }
 
    @Override
    public UserVIPPreInfo getProcessInfo(Long uid, Date time) {
        List<UserVIPPreInfo> infoList = userVIPPreInfoMapper.listByUid(uid);
        if (infoList == null || infoList.size() == 0)
            return null;
        Comparator<UserVIPPreInfo> cm = new Comparator<UserVIPPreInfo>() {
 
            @Override
            public int compare(UserVIPPreInfo o1, UserVIPPreInfo o2) {
                return o2.getProcess() - o1.getProcess();
            }
        };
        Collections.sort(infoList, cm);
 
        for (UserVIPPreInfo info : infoList) {
            if (time.getTime() >= info.getCreateTime().getTime())
                return info;
        }
        return null;
    }
 
    @RequestSerializableByKeyService(key = "#uid")
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void verifyVipPreInfo(Long uid, boolean inviteSuccess) {
        if (uid == null || uid <= 0)
            return;
 
        verifyCondition(uid);
 
        // 验证上级
        if (inviteSuccess) {
            UserInfo boss = threeSaleSerivce.getBoss(uid);
            if (boss != null) {
                verifyVipPreInfoBoss(boss.getId());
            }
        }
    }
 
    // 上级验证
    private void verifyVipPreInfoBoss(Long uid) {
        if (uid == null || uid <= 0)
            return;
 
        verifyCondition(uid);
    }
 
    private void verifyCondition(Long uid) {
        try {
            // 验证二阶段
            UserVIPPreInfo oldInfo = userVIPPreInfoMapper.selectByUidAndProcess(uid, UserVIPPreInfo.PROCESS_2);
            if (oldInfo != null) {
                return;
            }
 
            // 邀请订单
            long countZiGou = hongBaoV2CountService.counOrderByUidAndOrderType(uid, Constant.VIP_ORDER_PAY,
                    HongBaoV2.TYPE_ZIGOU);
            // 邀请订单
            long countShare = hongBaoV2CountService.counOrderByUidAndOrderType(uid, Constant.VIP_ORDER_PAY,
                    HongBaoV2.TYPE_SHARE_GOODS);
            // 队员
            long firstTeam = threeSaleSerivce.countFirstTeam(uid, 1);
            long secondTeam = threeSaleSerivce.countSecondTeam(uid, 1);
 
            // 一阶段
            boolean process1 = oneProcess(uid, countZiGou, countShare, firstTeam, secondTeam);
            if (!process1) {
                return;
            }
 
            // 二阶段
            twoProcess(uid, countZiGou, countShare, firstTeam, secondTeam);
        } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
        }
    }
 
    /**
     * 一阶段
     * 
     * @param uid
     * @param countZiGou
     * @param countShare
     * @param firstTeam
     * @param secondTeam
     */
    private boolean oneProcess(Long uid, long countZiGou, long countShare, long firstTeam, long secondTeam) {
        boolean process = false;
        String msg = "";
        if (countZiGou >= Constant.VIP_PROCESS_1_ZIGOU) {
            process = true;
            msg = "恭喜你!返利订单达到 " + Constant.VIP_PROCESS_1_ZIGOU + "笔";
        } else if (countShare >= Constant.VIP_PROCESS_1_SHARE) {
            process = true;
            msg = "恭喜你!分享订单达到 " + Constant.VIP_PROCESS_1_SHARE + "笔";
        } else if (firstTeam >= Constant.VIP_PROCESS_1_TEAM && secondTeam >= Constant.VIP_PROCESS_1_TEAM_SECOND) {
            process = true;
            msg = "直接粉丝达到" + Constant.VIP_PROCESS_1_TEAM + "人,间接粉丝达到 " + Constant.VIP_PROCESS_1_TEAM_SECOND + "人";
        }
 
        if (process) {
            try {
                UserVIPPreInfo info = new UserVIPPreInfo();
                info.setUid(uid);
                info.setProcess(UserVIPPreInfo.PROCESS_1);
                info.setCreateTime(new Date());
                info.setUpdateTime(new Date());
                addUserVIPPreInfo(info);
 
                // 消息
                MsgAccountVipDTO msgDto = new MsgAccountVipDTO();
                msgDto.setStatus("系统已将你的账户由快省达人升级为普通会员");
                msgDto.setEquity("从收到本消息起,你将获得全部普通会员权益");
                msgAccountDetailService.addMsgVIP(uid, msg, "如有疑问请联系我的-人工客服", msgDto);
            } catch (UserVIPPreInfoException e) {
                e.printStackTrace();
            }
        }
        return process;
    }
 
    /**
     * 二阶段
     * 
     * @param uid
     * @param countZiGou
     * @param countShare
     * @param firstTeam
     * @param secondTeam
     */
    private boolean twoProcess(Long uid, long countZiGou, long countShare, long firstTeam, long secondTeam) {
        boolean process = false;
        String msg = "";
        if (countZiGou >= Constant.VIP_PROCESS_2_ZIGOU) {
            process = true;
            msg = "恭喜你!返利订单达到 " + Constant.VIP_PROCESS_2_ZIGOU + "笔";
        } else if (countShare >= Constant.VIP_PROCESS_2_SHARE) {
            process = true;
            msg = "恭喜你!分享订单达到 " + Constant.VIP_PROCESS_2_SHARE + "笔";
        } else if (firstTeam >= Constant.VIP_PROCESS_2_TEAM && secondTeam >= Constant.VIP_PROCESS_2_TEAM_SECOND) {
            process = true;
            msg = "直接粉丝达到" + Constant.VIP_PROCESS_2_TEAM + "人,间接粉丝达到 " + Constant.VIP_PROCESS_2_TEAM_SECOND + "人";
        }
 
        if (process) {
            try {
                UserVIPPreInfo info = new UserVIPPreInfo();
                info.setUid(uid);
                info.setProcess(UserVIPPreInfo.PROCESS_2);
                info.setCreateTime(new Date());
                info.setUpdateTime(new Date());
                addUserVIPPreInfo(info);
 
                // 消息
                MsgAccountVipDTO msgDto = new MsgAccountVipDTO();
                msgDto.setStatus("系统已将你的账户由普通会员升级为高级会员");
                msgDto.setEquity("从收到本消息起,你将获得全部高级会员权益");
                msgAccountDetailService.addMsgVIP(uid, msg, "如有疑问请联系我的-人工客服", msgDto);
            } catch (UserVIPPreInfoException e) {
                e.printStackTrace();
            }
        }
        return process;
    }
 
}