yujian
2020-04-16 4bf96fb425e655e1a099aa750bb4bd6c9e158261
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
package com.yeshi.fanli.service.impl.user.vip;
 
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.context.annotation.Lazy;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.DateUtil;
 
import com.yeshi.fanli.dao.mybatis.user.vip.UserVIPPreInfoMapper;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.bus.user.ThreeSale;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.bus.user.UserInfoExtra;
import com.yeshi.fanli.entity.bus.user.UserInfoRegister;
import com.yeshi.fanli.entity.bus.user.UserInviteValidNum;
import com.yeshi.fanli.entity.bus.user.UserSystemCoupon;
import com.yeshi.fanli.entity.bus.user.vip.UserLevelEnum;
import com.yeshi.fanli.entity.bus.user.vip.UserLevelUpgradedNotify;
import com.yeshi.fanli.entity.bus.user.vip.UserVIPPreInfo;
import com.yeshi.fanli.entity.integral.IntegralDetail;
import com.yeshi.fanli.entity.system.ConfigKeyEnum;
import com.yeshi.fanli.exception.user.vip.UserVIPPreInfoException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.count.HongBaoV2CountService;
import com.yeshi.fanli.service.inter.msg.UserOtherMsgNotificationService;
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
import com.yeshi.fanli.service.inter.user.UserInfoRegisterService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.service.inter.user.UserInviteSeparateService;
import com.yeshi.fanli.service.inter.user.UserSystemCouponService;
import com.yeshi.fanli.service.inter.user.integral.IntegralDetailService;
import com.yeshi.fanli.service.inter.user.invite.ThreeSaleSerivce;
import com.yeshi.fanli.service.inter.user.invite.UserInviteMsgNotificationService;
import com.yeshi.fanli.service.inter.user.invite.UserInviteValidNumService;
import com.yeshi.fanli.service.inter.user.msg.UserAccountMsgNotificationService;
import com.yeshi.fanli.service.inter.user.vip.UserLevelUpgradedNotifyService;
import com.yeshi.fanli.service.inter.user.vip.UserVIPPreInfoService;
import com.yeshi.fanli.service.inter.user.vip.UserVipConfigService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
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 UserVipConfigService userVipConfigService;
    
    @Resource
    private UserInviteValidNumService userInviteValidNumService;
    
    @Resource
    private UserInfoExtraService userInfoExtraService;
    
    @Resource
    private IntegralDetailService integralDetailService;
    
    @Resource
    private ConfigService configService;
    
    @Lazy
    @Resource
    private UserSystemCouponService userSystemCouponService;
    
    @Resource
    private UserInviteSeparateService userInviteSeparateService;
    
    @Resource
    private UserInfoService userInfoService;
    
    @Resource
    private UserOtherMsgNotificationService userOtherMsgNotificationService;
    
    @Resource
    private UserAccountMsgNotificationService userAccountMsgNotificationService;
    
    @Resource
    private UserInfoRegisterService userInfoRegisterService;
    
    @Resource
    private UserInviteMsgNotificationService userInviteMsgNotificationService;
    
    @Resource
    private UserLevelUpgradedNotifyService userLevelUpgradedNotifyService;
    
    @Resource(name = "taskExecutor")
    private TaskExecutor executor;
    
 
    @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 getVipByProcess(Long uid, int process){
        return userVIPPreInfoMapper.selectByUidAndProcess(uid, process);
    }
    
 
    @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;
    }
 
 
    @Override
    @RequestSerializableByKeyService(key = "#uid")
    @Transactional(rollbackFor = Exception.class)
    public void upgradeVipByFanLiOrder(Long uid) {
        UserVIPPreInfo latest = getLatestProcessInfo(uid);
        if (latest != null && latest.getProcess() == UserVIPPreInfo.PROCESS_2) {
            return;
        }
        
        if (latest == null) {
            String inviteCode = userInfoExtraService.getInviteCodeByUid(uid);
            if (StringUtil.isNullOrEmpty(inviteCode)) {
                return; //未激活邀请码
            }
        } 
        
        // 自购订单到账
        long doneZiGou = hongBaoV2CountService.counOrderByUidAndOrderType(uid, Constant.VIP_ORDER_PAY, HongBaoV2.TYPE_ZIGOU);
        // 高级会员限制条件
        long limitZiGou7 = 0;
        String zigou7 = userVipConfigService.getValueByKey("vip_pre_7_zigou_order_count");
        if (!StringUtil.isNullOrEmpty(zigou7)) {
            limitZiGou7 = Long.parseLong(zigou7);
        }
        
        // 验证是否满足升级高级会员
        if (doneZiGou >= limitZiGou7) {
            twoProcess(uid, limitZiGou7, null, Constant.TYPE_REBATE);
            return;
        } 
        
        // 已经成为普通会员
        if (latest != null && latest.getProcess() == UserVIPPreInfo.PROCESS_1) {
            return;
        }
        
        // 普通会员条件
        long limitZiGou3 = 0;
        String zigou3 = userVipConfigService.getValueByKey("vip_pre_3_zigou_order_count");
        if (!StringUtil.isNullOrEmpty(zigou3)) {
            limitZiGou3 = Long.parseLong(zigou3);
        }
        if (doneZiGou >= limitZiGou3) {
            oneProcess(uid, limitZiGou3, null, Constant.TYPE_REBATE);
        } 
    }
    
    
    @Override
    @RequestSerializableByKeyService(key = "#uid")
    @Transactional(rollbackFor = Exception.class)
    public void upgradeVipByShareOrder(Long uid) {
        UserVIPPreInfo latest = getLatestProcessInfo(uid);
        if (latest != null && latest.getProcess() == UserVIPPreInfo.PROCESS_2) {
            return;
        }
        
        if (latest == null) {
            String inviteCode = userInfoExtraService.getInviteCodeByUid(uid);
            if (StringUtil.isNullOrEmpty(inviteCode)) {
                return; //未激活邀请码
            }
        } 
        
        // 分享订单已到账
        long doneShare = hongBaoV2CountService.counOrderByUidAndOrderType(uid, Constant.VIP_ORDER_PAY,
                HongBaoV2.TYPE_SHARE_GOODS);
        // 高级会员限制条件
        long limitShare7 = 0;
        String share7 = userVipConfigService.getValueByKey("vip_pre_7_share_order_count");
        if (!StringUtil.isNullOrEmpty(share7)) {
            limitShare7 = Long.parseLong(share7);
        }
        
        // 验证是否满足升级高级会员
        if (doneShare >= limitShare7) {
            twoProcess(uid, limitShare7, null, Constant.TYPE_SHAER);
            return;
        } 
        
        // 已经成为普通会员
        if (latest != null && latest.getProcess() == UserVIPPreInfo.PROCESS_1) {
            return;
        }
        
        // 普通会员条件
        long limitShare3 = 0;
        String share3 = userVipConfigService.getValueByKey("vip_pre_3_share_order_count");
        if (!StringUtil.isNullOrEmpty(share3)) {
            limitShare3 = Long.parseLong(share3);
        }
        if (doneShare >= limitShare3) {
            oneProcess(uid, limitShare3, null, Constant.TYPE_SHAER);
        } 
    }
    
    
    @Override
    @RequestSerializableByKeyService(key = "#tid")
    @Transactional(rollbackFor = Exception.class)
    public void upgradeVipByTeamNum(Long tid) {
        UserInfo boss = threeSaleSerivce.getBoss(tid);
        if (boss == null) {
            return;
        }
        Long bossId = boss.getId();
        
        // 验证上上级 -间接粉丝是否满足
        executor.execute(new Runnable() {
            @Override
            public void run() {
                upgradeVipByTeamNum(bossId);
            }
        });
        
        UserVIPPreInfo latest = getLatestProcessInfo(bossId);
        if (latest != null && latest.getProcess() == UserVIPPreInfo.PROCESS_2) {
            return;
        }
        
        // 有效粉丝
        int doneFirst = 0;
        int doneSecond = 0;
        UserInviteValidNum userInviteValidNum = userInviteValidNumService.selectByPrimaryKey(bossId);
        if (userInviteValidNum != null) {
            doneFirst = userInviteValidNum.getNumFirst() == null ? 0 : userInviteValidNum.getNumFirst();
            doneSecond = userInviteValidNum.getNumSecond() == null ? 0 : userInviteValidNum.getNumSecond();
        }
                    
        long limitFirst7 = 0;
        String first7 = userVipConfigService.getValueByKey("vip_pre_7_first_level_team_count");
        if (!StringUtil.isNullOrEmpty(first7)) {
            limitFirst7 = Long.parseLong(first7);
        }
        long limitSecond7 = 0;
        String second7 = userVipConfigService.getValueByKey("vip_pre_7_second_level_team_count");
        if (!StringUtil.isNullOrEmpty(second7)) {
            limitSecond7 = Long.parseLong(second7);
        }
        
        // 验证是否满足升级高级会员
        if (doneFirst >= limitFirst7 && doneSecond >= limitSecond7) {
            twoProcess(bossId, limitFirst7, limitSecond7, null);
            return;
        }
 
        // 已经成为普通会员
        if (latest != null && latest.getProcess() == UserVIPPreInfo.PROCESS_1) {
            return;
        }
        
        // 普通会员 条件
        long limitFirst3 = 0;
        String first = userVipConfigService.getValueByKey("vip_pre_3_first_level_team_count");
        if (!StringUtil.isNullOrEmpty(first)) {
            limitFirst3 = Long.parseLong(first);
        }
        
        long limitSecond3 = 0;
        String second = userVipConfigService.getValueByKey("vip_pre_3_second_level_team_count");
        if (!StringUtil.isNullOrEmpty(second)) {
            limitSecond3 = Long.parseLong(second);
        }
        
        if (doneFirst >= limitFirst3 && doneSecond >= limitSecond3) {
            oneProcess(bossId, limitFirst3, limitSecond3, null);
        }
    }
    
    
    
    
    /**
     * 一阶段
     * 
     * @param uid
     * @param countZiGou
     * @param countShare
     * @param firstTeam
     * @param secondTeam
     */
    @Transactional(rollbackFor = Exception.class)
    private void oneProcess(Long uid, long limit1, Long limit2, Integer type) {
        try {
            String item = null;
            if (type == null) {
                item = "邀请粉丝";
            } else if (type == Constant.TYPE_REBATE) {
                item = "返利订单";
            } else {
                item = "分享订单";
            }
 
            Date upgradeTime = new Date();
            UserVIPPreInfo info = new UserVIPPreInfo();
            info.setUid(uid);
            info.setProcess(UserVIPPreInfo.PROCESS_1);
            info.setCreateTime(upgradeTime);
            info.setUpdateTime(upgradeTime);
            addUserVIPPreInfo(info);
 
            // 升级消息
            Date registerTime = new Date(1577836800000L);
            UserInfoRegister userInfoRegister = userInfoRegisterService.selectByPrimaryKey(uid);
            if (userInfoRegister != null && userInfoRegister.getCreateTime() != null) {
                registerTime = userInfoRegister.getCreateTime();
            }
            int daysBetween = TimeUtil.getDayDifferenceCount(registerTime, new Date());
            userAccountMsgNotificationService.vipPreUpgrade(uid, "快省达人", "普通会员", daysBetween, limit1, limit2, type);
            
            // 升级弹框提示
            UserLevelUpgradedNotify notify = new UserLevelUpgradedNotify();
            notify.setUid(uid);
            notify.setFromLevel(UserLevelEnum.daRen);
            notify.setToLevel(UserLevelEnum.normalVIP);
            notify.setValid(true);
            notify.setCreateTime(new Date());
            userLevelUpgradedNotifyService.addUserLevelUpgradedNotify(notify);
            
            // 金币奖励
            goldcoinReward(uid);
            
            // 是否存在下级脱离期限 -更新不脱离
            userInviteSeparateService.updateInvalidByBossId(uid);
            // 提醒上级
            remindBoss(uid, "普通会员", item, upgradeTime, true);
        } catch (Exception e) {
           e.printStackTrace();
        }
    }
    
    
    
    /**
     * 二阶段
     * 
     * @param uid
     * @param countZiGou
     * @param countShare
     * @param firstTeam
     * @param secondTeam
     */
    @Transactional(rollbackFor = Exception.class)
    private void twoProcess(Long uid, long limit1, Long limit2, Integer type) {
        try {
            String item = null;
            if (type == null) {
                item = "邀请粉丝";
            } else if (type == Constant.TYPE_REBATE) {
                item = "返利订单";
            } else {
                item = "分享订单";
            }
 
            Date upgradeTime = new Date();
            UserVIPPreInfo info = new UserVIPPreInfo();
            info.setUid(uid);
            info.setProcess(UserVIPPreInfo.PROCESS_2);
            info.setCreateTime(upgradeTime);
            info.setUpdateTime(upgradeTime);
            addUserVIPPreInfo(info);
 
            // 升级弹框提示
            UserLevelUpgradedNotify notify = new UserLevelUpgradedNotify();
            notify.setUid(uid);
            notify.setFromLevel(UserLevelEnum.normalVIP);
            notify.setToLevel(UserLevelEnum.highVIP);
            notify.setValid(true);
            notify.setCreateTime(new Date());
            userLevelUpgradedNotifyService.addUserLevelUpgradedNotify(notify);
 
            // 升级福利-赠送奖励券
            String rewardCoupon = userVipConfigService.getValueByKey("vip_pre_7_gift_reward_coupon");
            if (!StringUtil.isNullOrEmpty(rewardCoupon)) {
                int num = Integer.parseInt(rewardCoupon);
                BigDecimal percent = new BigDecimal(configService.get(ConfigKeyEnum.exchangeRebatePercent.getKey()));
                userSystemCouponService.rewardCouponWin(uid, UserSystemCoupon.SOURCE_SYSTEM_PUSH, num, true, percent);
            }
                        
            // 升级消息
            int daysBetween = 0;
            boolean divorced = false;
            String beforeName = "快省达人";
            UserVIPPreInfo pre1 = userVIPPreInfoMapper.selectByUidAndProcess(uid, UserVIPPreInfo.PROCESS_1);
            if (pre1 != null) {
                beforeName = "普通会员";
                daysBetween = DateUtil.daysBetween(pre1.getCreateTime(), upgradeTime);
            } else {
                Date registerTime = new Date(1577836800000L);
                UserInfoRegister userInfoRegister = userInfoRegisterService.selectByPrimaryKey(uid);
                if (userInfoRegister != null && userInfoRegister.getCreateTime() != null) {
                    registerTime = userInfoRegister.getCreateTime();
                }
                daysBetween = TimeUtil.getDayDifferenceCount(registerTime, upgradeTime);
                
                // 金币奖励 (直接升级高级会员补齐一级奖励)
                goldcoinReward(uid);
                // 需要发送脱离信息
                divorced = true;
            }
            userAccountMsgNotificationService.vipPreUpgrade(uid, beforeName, "高级会员", daysBetween, limit1, limit2, type);
 
            // 提醒上级
            remindBoss(uid, "高级会员", item, upgradeTime, divorced);
        } catch (Exception e) {
            e.printStackTrace();
            LogHelper.error(e);
        }
    }
    
    /**
     * 升级奖励金币
     * @param uid
     */
    private void goldcoinReward(Long uid) {
        String goldcoin = userVipConfigService.getValueByKey("vip_pre_3_gift_goldcoin");
        if (!StringUtil.isNullOrEmpty(goldcoin)) {
            int goldcoinNum = Integer.parseInt(goldcoin);
            // 添加金币明细
            IntegralDetail detail = new IntegralDetail();
            detail.setTitle("升级VIP福利");
            detail.setUid(uid);
            detail.setMoney(Integer.parseInt(goldcoin));
            detail.setCreateTime(new Date());
            detail.setUniqueKey("VIP-3-" + uid);
            integralDetailService.insertSelective(detail);
            // 添加金币
            userInfoExtraService.addGoldCoinByUid(uid, Integer.parseInt(goldcoin));
            // 消息
            UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
            userOtherMsgNotificationService.goldCoinSystemGiveMsg(uid, goldcoinNum, userInfoExtra.getGoldCoin());
        }
    }
    
 
    @Override
    public void remindBoss(Long uid, String rankName, String item, Date time, boolean divorced) {
        // 是否存在上级
        ThreeSale threeSale = threeSaleSerivce.getMyBoss(uid);
        if (threeSale == null)
            return;
        Long bossId = threeSale.getBoss().getId();
        UserInfo userInfo = userInfoService.selectByPKey(uid);
        
        // 粉丝升级提醒
        userInviteMsgNotificationService.fansUpgrade(bossId, "直接粉丝", rankName, userInfo.getNickName(), item, time);
        
        // 是否存在上上级
        ThreeSale threeSaleSuper = threeSaleSerivce.getMyBoss(bossId);
        if (threeSaleSuper != null) { // 粉丝升级提醒
            Long bossIdSuper = threeSaleSuper.getBoss().getId();
            userInviteMsgNotificationService.fansUpgrade(bossIdSuper, "间接粉丝", rankName, userInfo.getNickName(), item, time);
        }
        
        // 是否升级脱离
        if (!divorced) {
            return;
        }
        
        UserVIPPreInfo latestInfo = getLatestProcessInfo(bossId);
        if (latestInfo == null) {
            // 添加脱离关系倒计时
            userInviteSeparateService.addPreSeparateRecord(uid, bossId);
            
            // 直接粉丝脱离-开始脱离提醒
            userInviteMsgNotificationService.fansPreDivorced(bossId, rankName, userInfo.getNickName(), time);
            
            //间接粉丝脱离-开始脱离提醒
            if (threeSaleSuper != null) { 
                UserInfo boss = userInfoService.selectByPKey(bossId);
                Long bossIdSuper = threeSaleSuper.getBoss().getId();
                userInviteMsgNotificationService.fansPredivorcedIndirect(bossIdSuper, rankName, userInfo.getNickName(),
                        boss.getNickName(), time);
            } 
        }
    }
    
    @Override
    public UserVIPPreInfo selectByUidAndProcess(Long uid, int process) {
        return userVIPPreInfoMapper.selectByUidAndProcess(uid, process);
    }
 
}