admin
2020-03-20 c3a5e164cbabbfc5f66e1356cb0de657ce3f58b9
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
package com.yeshi.fanli.service.impl.user.vip;
 
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
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.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.DateUtil;
 
import com.yeshi.fanli.dao.mybatis.user.vip.UserVIPInfoMapper;
import com.yeshi.fanli.dto.msg.MsgAccountVipDTO;
import com.yeshi.fanli.dto.msg.MsgOtherVIPDTO;
import com.yeshi.fanli.entity.bus.msg.MsgAccountDetail;
import com.yeshi.fanli.entity.bus.msg.MsgAccountDetail.MsgTypeAccountTypeEnum;
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.UserInviteSeparate;
import com.yeshi.fanli.entity.bus.user.UserSystemCoupon;
import com.yeshi.fanli.entity.bus.user.vip.UserVIPInfo;
import com.yeshi.fanli.entity.bus.user.vip.UserVIPPreInfo;
import com.yeshi.fanli.entity.integral.IntegralDetail;
import com.yeshi.fanli.entity.shop.BanLiShopOrder;
import com.yeshi.fanli.entity.system.ConfigKeyEnum;
import com.yeshi.fanli.entity.system.SystemCoupon.CouponTypeEnum;
import com.yeshi.fanli.exception.msg.MsgAccountDetailException;
import com.yeshi.fanli.exception.user.vip.UserVIPInfoException;
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.shop.BanLiShopOrderService;
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
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.msg.MsgAccountDetailService;
import com.yeshi.fanli.service.inter.user.vip.UserVIPInfoService;
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.TimeUtil;
 
@Service
public class UserVIPInfoServiceImpl implements UserVIPInfoService {
 
    @Resource
    private UserVIPInfoMapper userVIPInfoMapper;
 
    @Resource
    private UserInfoService userInfoService;
 
    @Resource
    private UserInfoExtraService userInfoExtraService;
 
    @Resource
    private UserVipConfigService userVipConfigService;
 
    @Lazy
    @Resource
    private HongBaoV2CountService hongBaoV2CountService;
 
    @Resource
    private IntegralDetailService integralDetailService;
 
    @Resource
    private BanLiShopOrderService banLiShopOrderService;
 
    @Resource
    private UserOtherMsgNotificationService userOtherMsgNotificationService;
 
    @Resource
    private ThreeSaleSerivce threeSaleSerivce;
 
    @Resource
    private UserInviteSeparateService userInviteSeparateService;
 
    @Resource
    private ConfigService configService;
 
    @Lazy
    @Resource
    private UserSystemCouponService userSystemCouponService;
    
    @Resource
    private UserVIPPreInfoService userVIPPreInfoService;
    
    @Resource
    private MsgAccountDetailService msgAccountDetailService;
    
    
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addUserVIPInfo(UserVIPInfo info) throws UserVIPInfoException {
        if (info.getId() == null) {
            throw new UserVIPInfoException(1, "信息不完整");
        }
        UserVIPInfo userInfo = userVIPInfoMapper.selectByPrimaryKey(info.getId());
        if (userInfo != null) {
            throw new UserVIPInfoException(1, "信息已存在");
        }
        // 初始化状态
        info.setState(UserVIPInfo.STATE_INVALID);
        info.setCreateTime(new Date());
        userVIPInfoMapper.insert(info);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void passVIPApply(Long uid) throws UserVIPInfoException {
        UserVIPInfo userVIPInfo = userVIPInfoMapper.selectByPrimaryKeyForUpdate(uid);
        if (userVIPInfo == null) {
            throw new UserVIPInfoException(1, "用户信息不存在");
        }
        if (userVIPInfo.getState() != UserVIPInfo.STATE_VERIFING) {
            throw new UserVIPInfoException(2, "申请未处于审核状态");
        }
 
        if (!verifyConform(uid))
            throw new UserVIPInfoException(1, "系统验证:不满足升级条件");
 
        UserVIPInfo info = new UserVIPInfo();
        info.setId(userVIPInfo.getId());
        info.setSuccessTime(new Date());
        info.setState(UserVIPInfo.STATE_SUCCESS);
        info.setUpdateTime(new Date());
        userVIPInfoMapper.updateByPrimaryKeySelective(info);
 
        // 下级的超级会员 不脱离
        userInviteSeparateService.updateInvalidByBossId(uid);
 
        // 额外信息
        UserInfoExtra userInfoExtra = userInfoExtraService.getByUidForUpdate(uid);
        if (userInfoExtra == null)
            throw new UserVIPInfoException(1, "用户信息不存在");
 
        // 添加金币
        IntegralDetail detail = new IntegralDetail();
        detail.setTitle("升级VIP福利");
        detail.setUid(uid);
        detail.setMoney(Constant.VIP_COLDCOIN_NUM);
        detail.setCreateTime(new Date());
        detail.setUniqueKey("VIP-" + uid);
        integralDetailService.insertSelective(detail);
        userInfoExtraService.addGoldCoinByUid(uid, Constant.VIP_COLDCOIN_NUM);
 
        try {
            // 奖励券
            BigDecimal percent = new BigDecimal(configService.get(ConfigKeyEnum.exchangeRebatePercent.getKey()));
            for (int i = 0; i < Constant.VIP_COUPON_REWARD_NUM; i++) {
                userSystemCouponService.insertUserCoupon(uid, CouponTypeEnum.rebatePercentCoupon.name(),
                        UserSystemCoupon.SOURCE_SYSTEM_PUSH, percent, false);
            }
 
            // 赠送免单券
            for (int i = 0; i < Constant.VIP_COUPON_GIVEFREE_NUM; i++) {
                userSystemCouponService.insertUserCoupon(uid, CouponTypeEnum.freeCouponGive.name(),
                        UserSystemCoupon.SOURCE_SYSTEM_PUSH, null, false);
            }
        } catch (Exception e) {
            throw new UserVIPInfoException(1, "券赠送失败");
        }
 
        // 消息
        MsgOtherVIPDTO msgOther = new MsgOtherVIPDTO();
        msgOther.setContent1("恭喜你,已成为超级会员");
        msgOther.setContent2("满足升级条件");
        msgOther.setContent3(TimeUtil.formatDateDot(new Date()));
        userOtherMsgNotificationService.passVIPApplyMsg(uid, "返利奖励券和、赠送免单券和金币请到我的-福利中心中查看", msgOther);
 
        // 通知上级
        callBoss(uid);
    }
 
    /**
     * 通知上级脱离关系
     * 
     * @param uid
     */
    @Transactional
    private void callBoss(Long uid) {
        // 是否存在上级
        ThreeSale threeSale = threeSaleSerivce.getMyBoss(uid);
        if (threeSale == null)
            return;
        // 上级是否为会员
        Long bossId = threeSale.getBoss().getId();
        UserVIPInfo bossVIP = userVIPInfoMapper.selectByPrimaryKeyForUpdate(bossId);
        if (bossVIP != null && bossVIP.getState() != null && bossVIP.getState() == UserVIPInfo.STATE_SUCCESS)
            return;
 
        // 之前插入记录失效
        userInviteSeparateService.updateStateByWorkerIdAndBossId(uid, bossId, UserInviteSeparate.STATE_INVALID);
 
        // 限制时间
        int limitDays = Integer.parseInt(configService.get(ConfigKeyEnum.inviteSeparateLimitDays.getKey()));
        UserInviteSeparate inviteSeparate = new UserInviteSeparate();
        inviteSeparate.setBossId(bossId);
        inviteSeparate.setWorkerId(uid);
        inviteSeparate.setState(UserInviteSeparate.STATE_INIT);
        inviteSeparate.setEndTime(DateUtil.plusDayDate(limitDays, new Date()));
        inviteSeparate.setCreateTime(new Date());
        inviteSeparate.setUpdateTime(new Date());
        userInviteSeparateService.insertSelective(inviteSeparate);
 
        UserInfo userInfo = userInfoService.selectByPKey(uid);
        // 消息
        MsgOtherVIPDTO msgboss = new MsgOtherVIPDTO();
        msgboss.setContent1(userInfo.getNickName() + " " + uid);
        msgboss.setContent2("于" + TimeUtil.formatDateDot(new Date()) + "成功升级成为超级会员");
        msgboss.setContent3("今日起" + limitDays + "天内,你未能成为超级会员将会与其脱离邀请关系 ");
        userOtherMsgNotificationService.teamVIPCallBoss(bossId, "如有疑问请联系我的-人工客服", msgboss);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void rejectVIPApply(Long uid, String reason) throws UserVIPInfoException {
        UserVIPInfo userInfo = userVIPInfoMapper.selectByPrimaryKeyForUpdate(uid);
        if (userInfo == null) {
            throw new UserVIPInfoException(1, "用户信息不存在");
        }
        if (userInfo.getState() != UserVIPInfo.STATE_VERIFING) {
            throw new UserVIPInfoException(2, "申请未处于审核状态");
        }
 
        UserVIPInfo info = new UserVIPInfo();
        info.setId(userInfo.getId());
        info.setState(UserVIPInfo.STATE_INVALID);
        info.setUpdateTime(new Date());
        userVIPInfoMapper.updateByPrimaryKeySelective(info);
 
        // 消息
        MsgOtherVIPDTO msgOther = new MsgOtherVIPDTO();
        msgOther.setContent1("很抱歉,未通过超级会员申请");
        msgOther.setContent2("未满足超级会员升级条件或疑似出现违规 ");
        userOtherMsgNotificationService.rejectVIPApply(uid, "如有疑问请联系我的-人工客服", msgOther);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void applyVIP(Long uid) throws UserVIPInfoException {
        UserVIPInfo userInfo = userVIPInfoMapper.selectByPrimaryKeyForUpdate(uid);
        if (userInfo == null) {
            userInfo = new UserVIPInfo();
            userInfo.setId(uid);
            addUserVIPInfo(userInfo);
        }
 
        if (userInfo.getState() != UserVIPInfo.STATE_INVALID)
            throw new UserVIPInfoException(2, "已经申请过");
 
        UserVIPInfo info = new UserVIPInfo();
        info.setId(userInfo.getId());
        info.setApplyTime(new Date());
        info.setState(UserVIPInfo.STATE_VERIFING);
        info.setUpdateTime(new Date());
        userVIPInfoMapper.updateByPrimaryKeySelective(info);
    }
 
    @Override
    public boolean isVIP(Long uid) {
        UserVIPInfo userInfo = userVIPInfoMapper.selectByPrimaryKey(uid);
        if (userInfo != null && userInfo.getState() == UserVIPInfo.STATE_SUCCESS)
            return true;
        else
            return false;
    }
 
    @Override
    public UserVIPInfo selectByUid(Long uid) {
        UserVIPInfo userInfo = userVIPInfoMapper.selectByPrimaryKey(uid);
        return userInfo;
    }
 
    @Override
    public Map<Long, Boolean> listByUids(List<Long> uids) {
        if (uids == null || uids.size() == 0)
            return new HashMap<>();
        List<UserVIPInfo> infoList = userVIPInfoMapper.listByUids(uids);
        // 整理数据
        Map<Long, Boolean> map = new HashMap<>();
        if (infoList != null)
            for (UserVIPInfo info : infoList)
                if (info != null && info.getState() == UserVIPInfo.STATE_SUCCESS)
                    map.put(info.getId(), true);
                else
                    map.put(info.getId(), false);
        for (Long uid : uids) {
            if (map.get(uid) == null)
                map.put(uid, false);
        }
        return map;
    }
 
    @Override
    public boolean verifyConform(Long uid) {
        if (uid == null || uid <= 0)
            return false;
 
        UserInfoExtra extra = userInfoExtraService.getUserInfoExtra(uid);
        if (extra == null)
            return false;
 
        // 1、直接粉丝(从 2020 年 1 月 1 日起直接粉丝产生有效订单)
        BigDecimal payMoney = new BigDecimal(userVipConfigService.getValueByKey("require_order_pay"));
//        long teamNum = hongBaoV2CountService.countValidOrderTeamUserByUid(uid,
//                TimeUtil.convertDateToTemp(Constant.VIP_ONLINE_TIME), payMoney);
        long teamNum = 0L;
        long vipBegin = TimeUtil.convertDateToTemp(Constant.VIP_ONLINE_TIME);
        List<ThreeSale> listThreeSale = threeSaleSerivce.getValidWorkerIdsByTime(uid, vipBegin);
        if (listThreeSale != null && listThreeSale.size() > 0) {
            for (ThreeSale three: listThreeSale) {
                UserInfo worker = three.getWorker();
                if (worker == null || worker.getId() == null) {
                    continue;
                }
                // 1、邀请关系成功后;2、单(分享 + 自购)实付款大于1元
                long countValid = hongBaoV2CountService.countValidOrderByUidAndTime(worker.getId(), three.getSucceedTime(), payMoney);
                if (countValid > 0) {
                    teamNum ++;
                }
            }
        }
        
        
        
        
        
 
        // 区分老用户和新用户
        String limtDate = userVipConfigService.getValueByKey("vip_execute_time");
        Date executeDate = null;
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
            executeDate = format.parse(limtDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (executeDate == null)
            return false;
 
        if (extra.getFirstLoginTime() == null || extra.getFirstLoginTime().getTime() < executeDate.getTime()) {
            long limitNum = Long.parseLong(userVipConfigService.getValueByKey("require_invite_num_old_user"));
            if (teamNum < limitNum)
                return false;
        } else {
            long limitNum = Long.parseLong(userVipConfigService.getValueByKey("require_invite_num_new_user"));
            if (teamNum < limitNum)
                return false;
        }
 
        // 2、累计自购返利≥100 元(从注册板栗快省起);
        BigDecimal fanMoney = new BigDecimal(userVipConfigService.getValueByKey("require_fan_money"));
        BigDecimal purchase = hongBaoV2CountService.getRewardMoneyBySelf(uid);
        if (purchase.compareTo(fanMoney) < 0)
            return false;
 
        // 3、累计金币≥10000 枚(从注册板栗快省起);
        BigDecimal goldCoin = integralDetailService.getCumulativeMoney(uid);
        BigDecimal limitGoldCoin = new BigDecimal(userVipConfigService.getValueByKey("require_gold_coin"));
        if (goldCoin.compareTo(limitGoldCoin) < 0)
            return false;
 
        // 4、使用红包≥1 次
        List<Integer> list = new ArrayList<Integer>();
        list.add(BanLiShopOrder.STATE_SUCCESS);
        long useHongBao = banLiShopOrderService.countByUidAndState(uid, list);
        long limitHongBao = Long.parseLong(userVipConfigService.getValueByKey("require_shop_buy"));
        if (useHongBao < limitHongBao)
            return false;
 
        // 符合条件
        return true;
    }
 
    @Override
    public List<UserVIPInfo> listQuery(int page, int count, String key, Integer state) {
        return userVIPInfoMapper.listQuery((page - 1) * count, count, key, state);
    }
 
    @Override
    public long countQuery(String key, Integer state) {
        return userVIPInfoMapper.countQuery(key, state);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void inviteSeparate(Long workerId, Long bossId) {
        if (workerId == null || bossId == null)
            return;
 
        // 查询记录
        UserInviteSeparate userInviteSeparate = userInviteSeparateService.selectByWorkerIdAndBossId(workerId, bossId);
        if (userInviteSeparate == null)
            return;
 
        // 是否存在邀请关系
        ThreeSale threeSale = threeSaleSerivce.getMyBoss(workerId);
        if (threeSale == null)
            return;
        Long bossIdExist = threeSale.getBoss().getId();
        if (bossIdExist == null || bossId.longValue() != bossIdExist.longValue())
            return;
 
        // 上级会员情况
        boolean separate = false;
        UserVIPInfo userVIPInfo = userVIPInfoMapper.selectByPrimaryKey(bossId);
        if (userVIPInfo != null && userVIPInfo.getState() != null
                && userVIPInfo.getState() == UserVIPInfo.STATE_SUCCESS) {
            if (userInviteSeparate.getEndTime().getTime() < userVIPInfo.getSuccessTime().getTime()) {
                separate = true; // 结束时间已经超过
            }
        } else {
            separate = true; // 上级非会员
        }
 
        if (!separate) {
            // 未脱离
            userInviteSeparateService.updateStateByWorkerIdAndBossId(workerId, bossId,
                    UserInviteSeparate.STATE_INVALID);
        } else {
            // 脱离关系
            userInviteSeparateService.updateStateByWorkerIdAndBossId(workerId, bossId,
                    UserInviteSeparate.STATE_SUCCESS);
 
            // 脱离邀请关系
            threeSaleSerivce.inviteSeparate(workerId, bossId);
 
            int limitDays = Integer.parseInt(configService.get(ConfigKeyEnum.inviteSeparateLimitDays.getKey()));
            // 消息
            UserInfo userInfo = userInfoService.selectByPKey(workerId);
            MsgOtherVIPDTO msgboss = new MsgOtherVIPDTO();
            msgboss.setContent1(userInfo.getNickName() + workerId + "于"
                    + TimeUtil.formatDateDot(userInviteSeparate.getCreateTime()) + "成功升级成为超级会员 ");
            msgboss.setContent2("很遗憾,你未能在" + limitDays + "天升级为超级会员 ");
            msgboss.setContent3("已与其脱离邀请关系");
            userOtherMsgNotificationService.teamSplitCallBoss(bossId, "如有疑问请联系我的-人工客服", msgboss);
        }
    }
 
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void applyVIPNew(Long uid) throws UserVIPInfoException {
        UserVIPPreInfo latestProcess = userVIPPreInfoService.getLatestProcessInfo(uid);
        if (latestProcess == null || latestProcess.getProcess() != UserVIPPreInfo.PROCESS_2) {
            throw new UserVIPInfoException(1, "该用户还不是高级会员");
        } 
        
        if (!verifyVipNew(uid))
            throw new UserVIPInfoException(1, "系统验证:不满足升级条件");
        
        UserVIPInfo userInfo = userVIPInfoMapper.selectByPrimaryKeyForUpdate(uid);
        if (userInfo == null) {
            userInfo = new UserVIPInfo();
            userInfo.setId(uid);
            // 添加记录
            addUserVIPInfo(userInfo);
        }
 
        if (userInfo.getState() != UserVIPInfo.STATE_INVALID)
            throw new UserVIPInfoException(2, "已经申请过");
 
        UserVIPInfo info = new UserVIPInfo();
        info.setId(userInfo.getId());
        info.setApplyTime(new Date());
        info.setState(UserVIPInfo.STATE_VERIFING);
        info.setUpdateTime(new Date());
        userVIPInfoMapper.updateByPrimaryKeySelective(info);
        
        
        MsgAccountDetail detail = new MsgAccountDetail();
        detail.setTitle("尊敬的高级会员,系统已收到你的超级会员升级申请,正在受理中");
        detail.setBeiZhu("如有疑问请联系我的-人工客服");
        detail.setRead(false);
        detail.setCreateTime(new Date());
        detail.setUser(new UserInfo(uid));
        detail.setContent("我们将会在48小时内完成审核");
        detail.setType(MsgTypeAccountTypeEnum.vipApply);
        try {
            msgAccountDetailService.addMsgAccountDetail(detail);
        } catch (MsgAccountDetailException e) {
            e.printStackTrace();
        }
    }
    
    
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void passVIPApplyNew(Long uid) throws UserVIPInfoException {
        UserVIPInfo userVIPInfo = userVIPInfoMapper.selectByPrimaryKeyForUpdate(uid);
        if (userVIPInfo == null) {
            throw new UserVIPInfoException(1, "用户信息不存在");
        }
        if (userVIPInfo.getState() != UserVIPInfo.STATE_VERIFING) {
            throw new UserVIPInfoException(2, "申请未处于审核状态");
        }
        
        UserVIPPreInfo latestProcess = userVIPPreInfoService.getLatestProcessInfo(uid);
        if (latestProcess == null || latestProcess.getProcess() != UserVIPPreInfo.PROCESS_2) {
            throw new UserVIPInfoException(1, "该用户还不是高级会员");
        } 
        
        if (!verifyVipNew(uid))
            throw new UserVIPInfoException(1, "系统验证:不满足升级条件");
        
        // 额外信息
        UserInfoExtra userInfoExtra = userInfoExtraService.getByUidForUpdate(uid);
        if (userInfoExtra == null)
            throw new UserVIPInfoException(1, "用户信息不存在");
 
        // 通过超级会员
        UserVIPInfo info = new UserVIPInfo();
        info.setId(userVIPInfo.getId());
        info.setSuccessTime(new Date());
        info.setState(UserVIPInfo.STATE_SUCCESS);
        info.setUpdateTime(new Date());
        userVIPInfoMapper.updateByPrimaryKeySelective(info);
 
        // 添加金币
        userInfoExtraService.addGoldCoinByUid(uid, Constant.VIP_COLDCOIN_NUM);
        // 添加金币明细
        IntegralDetail detail = new IntegralDetail();
        detail.setTitle("升级VIP福利");
        detail.setUid(uid);
        detail.setMoney(Constant.VIP_COLDCOIN_NUM);
        detail.setCreateTime(new Date());
        detail.setUniqueKey("VIP-" + uid);
        integralDetailService.insertSelective(detail);
                
        try {
            // 奖励券
            BigDecimal percent = new BigDecimal(configService.get(ConfigKeyEnum.exchangeRebatePercent.getKey()));
            for (int i = 0; i < Constant.VIP_COUPON_REWARD_NUM; i++) {
                userSystemCouponService.insertUserCoupon(uid, CouponTypeEnum.rebatePercentCoupon.name(),
                        UserSystemCoupon.SOURCE_SYSTEM_PUSH, percent, false);
            }
 
            // 赠送免单券
            for (int i = 0; i < Constant.VIP_COUPON_GIVEFREE_NUM; i++) {
                userSystemCouponService.insertUserCoupon(uid, CouponTypeEnum.freeCouponGive.name(),
                        UserSystemCoupon.SOURCE_SYSTEM_PUSH, null, false);
            }
        } catch (Exception e) {
            throw new UserVIPInfoException(1, "券赠送失败");
        }
 
        
        // 消息
        MsgAccountVipDTO msgDto = new MsgAccountVipDTO();
        msgDto.setStatus("已将你的账户由高级会员升级为超级会员");
        msgDto.setEquity("从收到本消息起,你将获得全部超级会员权益");
        msgAccountDetailService.addMsgVIP(uid, "恭喜你!经人工审核你满足升级超级会员条件", "如有疑问请联系我的-人工客服", msgDto);
    }
    
    
    /**
     * 验证是否符合VIP
     * @param uid
     * @return
     */
    private boolean verifyVipNew(Long uid) {
        // 邀请订单
        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);
        
        if (countZiGou >= Constant.VIP_PROCESS_3_ZIGOU || countShare >= Constant.VIP_PROCESS_3_SHARE
                || (firstTeam >= Constant.VIP_PROCESS_3_TEAM && secondTeam >= Constant.VIP_PROCESS_3_TEAM_SECOND)) {
            return true;
        } 
        return false;
    }
    
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void rejectVIPApplyNew(Long uid, String reason) throws UserVIPInfoException {
        UserVIPInfo userInfo = userVIPInfoMapper.selectByPrimaryKeyForUpdate(uid);
        if (userInfo == null) {
            throw new UserVIPInfoException(1, "用户信息不存在");
        }
        if (userInfo.getState() != UserVIPInfo.STATE_VERIFING) {
            throw new UserVIPInfoException(2, "申请未处于审核状态");
        }
 
        UserVIPInfo info = new UserVIPInfo();
        info.setId(userInfo.getId());
        info.setState(UserVIPInfo.STATE_INVALID);
        info.setUpdateTime(new Date());
        userVIPInfoMapper.updateByPrimaryKeySelective(info);
        
        // 消息
        MsgAccountVipDTO msgDto = new MsgAccountVipDTO();
        msgDto.setStatus("你的账号仍是高级会员");
        msgDto.setReason(reason);
        msgAccountDetailService.addMsgVIP(uid, "很抱歉!经人工审核你未满足或不符合升级超级会员的条件", "如有疑问请联系我的-人工客服", msgDto);
    }
}