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);
|
}
|
|
}
|