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.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.dto.msg.MsgOtherSystemGiveDTO;
|
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.UserInviteValidNum;
|
import com.yeshi.fanli.entity.bus.user.UserSystemCoupon;
|
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.entity.system.SystemCoupon.CouponTypeEnum;
|
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.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.UserInviteValidNumService;
|
import com.yeshi.fanli.service.inter.user.msg.MsgAccountDetailService;
|
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.annotation.RequestSerializableByKeyService;
|
|
@Service
|
public class UserVIPPreInfoServiceImpl implements UserVIPPreInfoService {
|
|
@Resource
|
private UserVIPPreInfoMapper userVIPPreInfoMapper;
|
|
@Resource
|
private HongBaoV2CountService hongBaoV2CountService;
|
|
@Resource
|
private ThreeSaleSerivce threeSaleSerivce;
|
|
@Resource
|
private MsgAccountDetailService msgAccountDetailService;
|
|
@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;
|
|
|
@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;
|
}
|
|
@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 doneZiGou = hongBaoV2CountService.counOrderByUidAndOrderType(uid, Constant.VIP_ORDER_PAY,
|
HongBaoV2.TYPE_ZIGOU);
|
// 分享订单
|
long doneShare = hongBaoV2CountService.counOrderByUidAndOrderType(uid, Constant.VIP_ORDER_PAY,
|
HongBaoV2.TYPE_SHARE_GOODS);
|
|
// 有效粉丝
|
int doneFirst = 0;
|
int doneSecond = 0;
|
UserInviteValidNum userInviteValidNum = userInviteValidNumService.selectByPrimaryKey(uid);
|
if (userInviteValidNum != null) {
|
doneFirst = userInviteValidNum.getNumFirst() == null ? 0 : userInviteValidNum.getNumFirst();
|
doneSecond = userInviteValidNum.getNumSecond() == null ? 0 : userInviteValidNum.getNumSecond();
|
}
|
|
// 一阶段
|
boolean process1 = oneProcess(uid, doneZiGou, doneShare, doneFirst, doneSecond);
|
if (!process1) {
|
return;
|
}
|
|
// 二阶段
|
twoProcess(uid, doneZiGou, doneShare, doneFirst, doneSecond);
|
} catch (Exception e) {
|
LogHelper.errorDetailInfo(e);
|
}
|
}
|
|
/**
|
* 一阶段
|
*
|
* @param uid
|
* @param countZiGou
|
* @param countShare
|
* @param firstTeam
|
* @param secondTeam
|
*/
|
@Transactional(rollbackFor = Exception.class)
|
private boolean oneProcess(Long uid, long countZiGou, long countShare, long firstTeam, long secondTeam) {
|
boolean process = false;
|
String msg = "";
|
|
long limitZiGou = 0;
|
String zigou = userVipConfigService.getValueByKey("vip_pre_3_zigou_order_count");
|
if (!StringUtil.isNullOrEmpty(zigou)) {
|
limitZiGou = Long.parseLong(zigou);
|
}
|
|
long limitShare = 0;
|
String share = userVipConfigService.getValueByKey("vip_pre_3_share_order_count");
|
if (!StringUtil.isNullOrEmpty(share)) {
|
limitShare = Long.parseLong(share);
|
}
|
|
long limitFirst = 0;
|
String first = userVipConfigService.getValueByKey("vip_pre_3_first_level_team_count");
|
if (!StringUtil.isNullOrEmpty(first)) {
|
limitFirst = Long.parseLong(first);
|
}
|
|
long limitSecond = 0;
|
String second = userVipConfigService.getValueByKey("vip_pre_3_second_level_team_count");
|
if (!StringUtil.isNullOrEmpty(second)) {
|
limitSecond = Long.parseLong(second);
|
}
|
|
if (countZiGou >= limitZiGou) {
|
process = true;
|
msg = "恭喜你!返利订单达到 " + limitZiGou + "笔";
|
} else if (countShare >= limitShare) {
|
process = true;
|
msg = "恭喜你!分享订单达到 " + limitShare + "笔";
|
} else if (firstTeam >= limitFirst && secondTeam >= limitSecond) {
|
process = true;
|
msg = "直接粉丝达到" + limitFirst + "人,间接粉丝达到 " + limitSecond + "人";
|
}
|
|
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);
|
|
|
// 赠送金币
|
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));
|
|
// 消息
|
MsgOtherSystemGiveDTO dto = new MsgOtherSystemGiveDTO();
|
dto.setName("系统赠送 ");
|
dto.setReason("恭喜你!成功升级为普通会员");
|
dto.setDesc("获得金币" + goldcoinNum+"枚");
|
userOtherMsgNotificationService.vipUpgradeReward(uid, "详情查看,对应会员权益", dto);
|
}
|
|
// 是否存在下级脱离期限 -更新不脱离
|
userInviteSeparateService.updateInvalidByBossId(uid);
|
|
// 上级脱离关系
|
remindBoss(uid);
|
|
} catch (UserVIPPreInfoException e) {
|
e.printStackTrace();
|
}
|
}
|
return process;
|
}
|
|
|
private void remindBoss(Long uid) {
|
// 是否存在上级
|
ThreeSale threeSale = threeSaleSerivce.getMyBoss(uid);
|
if (threeSale == null)
|
return;
|
|
// 验证上级是否正常
|
Long bossId = threeSale.getBoss().getId();
|
UserInfo userInfo = userInfoService.getUserById(bossId);
|
if (userInfo == null || userInfo.getState() != UserInfo.STATE_NORMAL) {
|
return;
|
}
|
|
// 验证上级是否已是会员
|
UserVIPPreInfo oldInfo = userVIPPreInfoMapper.selectByUidAndProcess(bossId, UserVIPPreInfo.PROCESS_1);
|
if (oldInfo != null) {
|
return;
|
}
|
|
// 添加脱离关系倒计时
|
userInviteSeparateService.addPreSeparateRecord(uid, bossId);
|
}
|
|
|
|
/**
|
* 二阶段
|
*
|
* @param uid
|
* @param countZiGou
|
* @param countShare
|
* @param firstTeam
|
* @param secondTeam
|
*/
|
@Transactional(rollbackFor = Exception.class)
|
private boolean twoProcess(Long uid, long countZiGou, long countShare, long firstTeam, long secondTeam) {
|
boolean process = false;
|
String msg = "";
|
|
long limitZiGou = 0;
|
String zigou = userVipConfigService.getValueByKey("vip_pre_7_zigou_order_count");
|
if (!StringUtil.isNullOrEmpty(zigou)) {
|
limitZiGou = Long.parseLong(zigou);
|
}
|
|
long limitShare = 0;
|
String share = userVipConfigService.getValueByKey("vip_pre_7_share_order_count");
|
if (!StringUtil.isNullOrEmpty(share)) {
|
limitShare = Long.parseLong(share);
|
}
|
|
long limitFirst = 0;
|
String first = userVipConfigService.getValueByKey("vip_pre_7_first_level_team_count");
|
if (!StringUtil.isNullOrEmpty(first)) {
|
limitFirst = Long.parseLong(first);
|
}
|
|
long limitSecond = 0;
|
String second = userVipConfigService.getValueByKey("vip_pre_7_second_level_team_count");
|
if (!StringUtil.isNullOrEmpty(second)) {
|
limitSecond = Long.parseLong(second);
|
}
|
|
if (countZiGou >= limitZiGou) {
|
process = true;
|
msg = "恭喜你!返利订单达到 " + limitZiGou + "笔";
|
} else if (countShare >= limitShare) {
|
process = true;
|
msg = "恭喜你!分享订单达到 " + limitShare + "笔";
|
} else if (firstTeam >= limitFirst && secondTeam >= limitSecond) {
|
process = true;
|
msg = "直接粉丝达到" + limitFirst + "人,间接粉丝达到 " + limitSecond + "人";
|
}
|
|
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);
|
|
// 赠送奖励券
|
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()));
|
for (int i = 0; i < num; i++) {
|
userSystemCouponService.insertUserCoupon(uid, CouponTypeEnum.rebatePercentCoupon.name(),
|
UserSystemCoupon.SOURCE_SYSTEM_PUSH, percent, false);
|
}
|
|
// 消息
|
MsgOtherSystemGiveDTO dto = new MsgOtherSystemGiveDTO();
|
dto.setName("系统赠送 ");
|
dto.setReason("恭喜你!成功升级为高级会员");
|
dto.setDesc("获得返利奖励券" + num +"张");
|
userOtherMsgNotificationService.vipUpgradeReward(uid, "详情查看,对应会员权益", dto);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
LogHelper.error(e);
|
}
|
}
|
return process;
|
}
|
|
@Override
|
public UserVIPPreInfo selectByUidAndProcess(Long uid, int process) {
|
UserVIPPreInfo info = userVIPPreInfoMapper.selectByUidAndProcess(uid, process);
|
return info;
|
}
|
|
}
|