yujian
2019-01-23 bc2f5082efe5c863736ca335d210c2ed67f78ab4
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
package com.yeshi.fanli.service.impl.user;
 
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 org.yeshi.utils.DateUtil;
 
import com.yeshi.fanli.dao.mybatis.user.UserInfoExtraMapper;
import com.yeshi.fanli.dao.mybatis.user.UserRankRecordMapper;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.bus.user.UserInfoExtra;
import com.yeshi.fanli.entity.bus.user.UserRank;
import com.yeshi.fanli.entity.bus.user.UserRankRecord;
import com.yeshi.fanli.exception.ThreeSaleException;
import com.yeshi.fanli.exception.user.UserInfoExtraException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.hongbao.ThreeSaleSerivce;
import com.yeshi.fanli.service.inter.order.CommonOrderCountService;
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.service.inter.user.UserRankService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.account.UserUtil;
import com.yeshi.fanli.vo.user.UserInfoExtraVO;
 
 
@Service
public class UserInfoExtraServiceImpl implements UserInfoExtraService {
 
    @Resource
    private UserInfoExtraMapper userInfoExtraMapper;
    
    @Resource
    private UserRankRecordMapper userRankRecordMapper;
 
    @Resource
    private UserRankService userRankService;
 
    @Resource
    private CommonOrderCountService commonOrderCountService;
 
    @Resource
    private ConfigService configService;
    
    @Resource
    private ThreeSaleSerivce threeSaleSerivce;
    
    @Resource
    private UserInfoService userInfoService;
    
 
    @Override
    public UserInfoExtraVO getRankInfo(Long uid) throws UserInfoExtraException, Exception {
 
        UserInfoExtraVO extraVO = userInfoExtraMapper.getInfoExtraVOByUid(uid);
        if (extraVO == null) {
            throw new UserInfoExtraException(1, "用户附加信息不存在");
        }
 
        // 等级对照表
        String contrast = configService.get("user_rank_contrast");
        extraVO.setRankContrast(contrast);
        // 特别提醒
        String rankTip = configService.get("user_rank_tip");
        extraVO.setRankTip(rankTip);
 
        Integer rankOrderNum = extraVO.getRankOrderNum();
        if (rankOrderNum != null && rankOrderNum > 0) {
            String rankMsg = null;
            Integer rankSource = extraVO.getRankSource();
            switch (rankSource) {
            case Constant.TYPE_REBATE:
                rankMsg = "返利订单";
                break;
            case Constant.TYPE_SHAER:
                rankMsg = "分享订单";
                break;
            case Constant.TYPE_INVITE:
                rankMsg = "邀请订单";
                break;
            default:
                break;
            }
 
            List<Object> listMsg = new ArrayList<Object>();
 
            if (rankMsg != null) {
                String color_red = "#E5005C";
                String color_black = "#333333";
 
                Map<String, String> map1 = new HashMap<String, String>();
                map1.put("content", "上月");
                map1.put("color", color_black);
                listMsg.add(map1);
 
                Map<String, String> map2 = new HashMap<String, String>();
                map2.put("content", rankMsg);
                map2.put("color", color_red);
                listMsg.add(map2);
 
                Map<String, String> map3 = new HashMap<String, String>();
                map3.put("content", "达到了");
                map3.put("color", color_black);
                listMsg.add(map3);
 
                Map<String, String> map4 = new HashMap<String, String>();
                map4.put("content", rankOrderNum + "");
                map4.put("color", color_red);
                listMsg.add(map4);
 
                Map<String, String> map5 = new HashMap<String, String>();
                map5.put("content", "单");
                map5.put("color", color_black);
                listMsg.add(map5);
                extraVO.setGalaMsg(listMsg);
            }
        }
 
        return extraVO;
    }
 
    @Override
    public UserInfoExtra updateUserRankByUid(Long uid) throws UserInfoExtraException{
        
        boolean isupdateRank = true;
        
        UserInfoExtra userInfoExtra = userInfoExtraMapper.getInfoExtraByUid(uid);
        if (userInfoExtra != null && userInfoExtra.getId() != null) {
            // 判断当月是否已更新
            Date rankUpdateTime = userInfoExtra.getRankUpdateTime();
            if (rankUpdateTime != null && DateUtil.isSameMonth(rankUpdateTime, new Date())) {
                isupdateRank = false;
            } 
        } else {
            userInfoExtra = new UserInfoExtra();
            userInfoExtra.setUserInfo(new UserInfo(uid));
        }
        
        // 更新等级
        if (isupdateRank) {
            updateRank(userInfoExtra);
        }
        
        return userInfoExtra;
    }
    
    
    @Override
    public UserInfoExtra updateRank(UserInfoExtra userInfoExtra) throws UserInfoExtraException {
        UserInfo userInfo = userInfoExtra.getUserInfo();
        if (userInfo == null ) {
            throw new UserInfoExtraException(1, "用户不存在");
        }
        
        Long uid = userInfo.getId();
        if (uid == null ) {
            throw new UserInfoExtraException(1, "用户ID不存在");
        }        
                
        List<UserRank> listRank = userRankService.getAllRank();
        if (listRank == null || listRank.size() == 0) {
            throw new UserInfoExtraException(1, "系统等级不存在");
        }
 
        int selfOrderNum = 0;
        int sharedOrderNum = 0;
        int inviteOrderNum = 0;
        Map<String, Object> map = commonOrderCountService.lastMonthSettleOrderNumber(uid);
        if (map != null) {
            // 返利订单
            if (map.get("totalSelf") != null) {
                selfOrderNum = Integer.parseInt(map.get("totalSelf").toString());
            }
 
            // 分享订单
            if (map.get("totalShared") != null) {
                sharedOrderNum = Integer.parseInt(map.get("totalShared").toString());
            }
 
            // 邀请订单
            if (map.get("totalInvite") != null) {
                inviteOrderNum = Integer.parseInt(map.get("totalInvite").toString());
            }
        }
 
        UserRank rank = null;
        for (UserRank userRank : listRank) {
            // 邀请满足
            Integer inviteNum = userRank.getInviteNum();
            if (inviteOrderNum >= inviteNum) {
                rank = userRank;
                userInfoExtra.setRankOrderNum(inviteOrderNum);
                userInfoExtra.setRankSource(Constant.TYPE_INVITE);
            }
 
            // 分享满足
            int shareNum = userRank.getShareNum();
            if (sharedOrderNum >= shareNum) {
                rank = userRank;
                userInfoExtra.setRankOrderNum(sharedOrderNum);
                userInfoExtra.setRankSource(Constant.TYPE_SHAER);
            }
 
            // 返利满足
            int directNum = userRank.getRebateNum();
            if (selfOrderNum >= directNum) {
                rank = userRank;
                userInfoExtra.setRankOrderNum(selfOrderNum);
                userInfoExtra.setRankSource(Constant.TYPE_REBATE);
            }
 
            
            if (rank != null) {
                userInfoExtra.setUserRank(rank);
            } else {
                rank = userRank;
                // 默认最低等级:青铜
                userInfoExtra.setUserRank(rank);
                userInfoExtra.setRankOrderNum(0);
                userInfoExtra.setRankSource(Constant.TYPE_REBATE);
                break;
            }
        }
        // 等级更新时间
        userInfoExtra.setRankUpdateTime(new Date());
        // 保存信息并返回
        saveUserInfoExtra(userInfoExtra);
        
        UserRankRecord userRankRecord = new UserRankRecord();
        userRankRecord.setUid(uid);
        userRankRecord.setRankId(rank.getId());
        userRankRecord.setCreateTime(new Date());
        userRankRecordMapper.insertSelective(userRankRecord);
        
        return userInfoExtra;
    }
 
    @Override
    public UserInfoExtra saveUserInfoExtra(UserInfoExtra userInfoExtra) throws UserInfoExtraException {
 
        if (userInfoExtra == null) {
            throw new UserInfoExtraException(1, "附加信息不能为空");
        }
 
        Long extraId = userInfoExtra.getId();
        if (extraId != null) {
            userInfoExtra.setUpdateTime(new Date());
            userInfoExtraMapper.updateByPrimaryKeySelective(userInfoExtra);
        } else {
            UserInfo userInfo = userInfoExtra.getUserInfo();
            if (userInfo == null || userInfo.getId() == null) {
                throw new UserInfoExtraException(1, "用户不存在");
            }
 
            Long uid = userInfo.getId();
            UserInfoExtra extra = userInfoExtraMapper.getInfoExtraByUid(uid);
            if (extra != null && extra.getId() != null) {
                userInfoExtra.setId(extra.getId());
                userInfoExtra.setUpdateTime(new Date());
                userInfoExtraMapper.updateByPrimaryKeySelective(userInfoExtra);
            } else {
                userInfoExtra.setCreateTime(new Date());
                userInfoExtra.setUpdateTime(new Date());
                userInfoExtraMapper.insertSelective(userInfoExtra);
            }
 
        }
        return userInfoExtra;
    }
    
    @Override
    public String activateInviteCode(Long uid, String inviteCode) throws UserInfoExtraException{
        if (uid == null || inviteCode == null) {
            throw new UserInfoExtraException(1, "用户id、邀请码不能为空");
        }
        
        // 被邀请人信息
        UserInfo invitee = userInfoService.selectByPKey(uid);
        if (invitee == null) {
            throw new UserInfoExtraException(1, "用户不存在");
        }
        
        // 邀请人信息
        UserInfo inviter = userInfoService.getInfoByPhoneOrInviteCode(inviteCode, inviteCode);
        if (inviter == null) {
            throw new UserInfoExtraException(1, "请输入有效的邀请码");
        }
        
        // 绑定关系
        try {
            threeSaleSerivce.bindRelationshipByInviteCode(invitee, inviter);
        } catch (ThreeSaleException e) {
            try {
                LogHelper.errorDetailInfo(e);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            throw new UserInfoExtraException(1, "激活失败");
        }
        
        
        // 邀请码有效、生成邀请码
        String code = UserUtil.getInviteCode(uid); 
        if (code == null || code.trim().length() == 0) {
            throw new UserInfoExtraException(1, "激活码生成失败");
        }
        
        UserInfoExtra userInfoExtra = new UserInfoExtra();
        userInfoExtra.setUserInfo(invitee);
        userInfoExtra.setInviteCode(code);
        
        // 保存额外信息
        saveUserInfoExtra(userInfoExtra);
        
        
        Long id = userInfoExtra.getId();
        if (id == null) {
            throw new UserInfoExtraException(1, "激活码生成失败");
        }
        
        return code;
    }
    
    @Override
    public UserInfoExtra getUserInfoExtra(Long uid) {
        return userInfoExtraMapper.getInfoExtraByUid(uid);
    }
    
    @Override
    public UserInfoExtraVO getInfoExtraVOByUid(Long uid) {
        return userInfoExtraMapper.getInfoExtraVOByUid(uid);
    }
    
    
    @Override
    public String getUserInviteCode(Long uid) throws UserInfoExtraException {
        
        UserInfoExtra userInfoExtra = userInfoExtraMapper.getInfoExtraByUid(uid);
        if (userInfoExtra == null) {
            userInfoExtra = new UserInfoExtra();
        }
        
        // 邀请码
        String inviteCode = userInfoExtra.getInviteCode();
        
        if (inviteCode == null || inviteCode.trim().length() == 0) {
            // 判断用户
            int relationshipNum = threeSaleSerivce.getSuccessRelationshipNum(uid);
            
            if (relationshipNum > 0) {
                // 邀请码有效、生成邀请码
                inviteCode = UserUtil.getInviteCode(uid); 
                if (inviteCode == null || inviteCode.trim().length() == 0) {
                    throw new UserInfoExtraException(1, "激活码生成失败");
                }
                
                // 保存邀请码
                userInfoExtra.setUserInfo(new UserInfo(uid));
                userInfoExtra.setInviteCode(inviteCode);
                // 保存附加信息
                saveUserInfoExtra(userInfoExtra);
            }
        }
        
        return inviteCode;
    }
    
}