New file |
| | |
| | | package com.yeshi.fanli.service.impl.redpack;
|
| | |
|
| | | import java.math.BigDecimal;
|
| | | import java.util.Date;
|
| | | import java.util.List;
|
| | |
|
| | | import javax.annotation.Resource;
|
| | |
|
| | | import org.springframework.stereotype.Service;
|
| | | import org.springframework.transaction.annotation.Transactional;
|
| | | import org.yeshi.utils.DateUtil;
|
| | |
|
| | | import com.google.gson.Gson;
|
| | | import com.yeshi.fanli.dao.mybatis.redpack.RedPackWinInviteMapper;
|
| | | import com.yeshi.fanli.dto.msg.MsgRedPackAddContentDTO;
|
| | | import com.yeshi.fanli.entity.bus.msg.MsgMoneyDetail.MsgTypeMoneyTypeEnum;
|
| | | import com.yeshi.fanli.entity.bus.user.UserExtraTaoBaoInfo;
|
| | | import com.yeshi.fanli.entity.bus.user.UserInfo;
|
| | | import com.yeshi.fanli.entity.order.CommonOrder;
|
| | | import com.yeshi.fanli.entity.redpack.RedPackWinInvite;
|
| | | import com.yeshi.fanli.entity.redpack.RedPackWinInvite.RedPackWinInviteTypeEnum;
|
| | | import com.yeshi.fanli.service.inter.money.msg.UserMoneyMsgNotificationService;
|
| | | import com.yeshi.fanli.service.inter.order.CommonOrderService;
|
| | | import com.yeshi.fanli.service.inter.redpack.RedPackBalanceService;
|
| | | import com.yeshi.fanli.service.inter.redpack.RedPackConfigService;
|
| | | import com.yeshi.fanli.service.inter.redpack.RedPackWinInviteService;
|
| | | import com.yeshi.fanli.service.inter.user.UserInfoService;
|
| | | import com.yeshi.fanli.service.inter.user.integral.IntegralTaskRecordService;
|
| | | import com.yeshi.fanli.service.inter.user.invite.ThreeSaleSerivce;
|
| | | import com.yeshi.fanli.service.inter.user.tb.UserExtraTaoBaoInfoService;
|
| | | import com.yeshi.fanli.util.Constant;
|
| | | import com.yeshi.fanli.util.MoneyBigDecimalUtil;
|
| | | import com.yeshi.fanli.util.StringUtil;
|
| | | import com.yeshi.fanli.util.annotation.RequestSerializableByKeyService;
|
| | | import com.yeshi.fanli.util.annotation.redpack.RedPackGetVersionLimit;
|
| | | import com.yeshi.fanli.util.factory.RedPackDetailFactory;
|
| | | import com.yeshi.fanli.vo.order.CommonOrderVO;
|
| | |
|
| | | @Service
|
| | | public class RedPackWinInviteServiceImpl implements RedPackWinInviteService {
|
| | |
|
| | | @Resource
|
| | | private RedPackWinInviteMapper redPackWinInviteMapper;
|
| | | |
| | | @Resource
|
| | | private RedPackConfigService redPackConfigService;
|
| | | |
| | | @Resource
|
| | | private RedPackBalanceService redPackBalanceService;
|
| | |
|
| | | @Resource
|
| | | private CommonOrderService commonOrderService;
|
| | | |
| | | @Resource
|
| | | private ThreeSaleSerivce threeSaleSerivce;
|
| | | |
| | | @Resource
|
| | | private UserMoneyMsgNotificationService userMoneyMsgNotificationService;
|
| | | |
| | | @Resource |
| | | private UserInfoService userInfoService;
|
| | | |
| | | @Resource |
| | | private UserExtraTaoBaoInfoService userExtraTaoBaoInfoService;
|
| | | |
| | | @Resource |
| | | private IntegralTaskRecordService integralTaskRecordService;
|
| | | |
| | | |
| | | @Override
|
| | | public List<RedPackWinInvite> getRewardList(int start, int count, Long uid) {
|
| | | return redPackWinInviteMapper.getRewardList(start, count, uid);
|
| | | }
|
| | | |
| | | |
| | | @Override
|
| | | public Long countRewardRecord(Long uid) {
|
| | | return redPackWinInviteMapper.countRewardRecord(uid);
|
| | | }
|
| | | |
| | | @RedPackGetVersionLimit(uid = "#uid")
|
| | | @RequestSerializableByKeyService(key = "#uid")
|
| | | @Transactional(rollbackFor = Exception.class)
|
| | | @Override
|
| | | public void orderArriveReward(Long uid, Integer source, String orderNo) throws Exception {
|
| | | if (uid == null || source == null || orderNo == null)
|
| | | return;
|
| | | |
| | | // 上一级的奖励
|
| | | UserInfo boss = threeSaleSerivce.getBoss(uid);
|
| | | if(boss != null) {
|
| | | firstReward(uid, source, orderNo);
|
| | | bossReward(boss.getId(), uid, source, orderNo);
|
| | | }
|
| | | }
|
| | | |
| | | |
| | | /**
|
| | | * 上级奖励
|
| | | * @param uid
|
| | | * @param teamUid
|
| | | * @param source
|
| | | * @param orderNo
|
| | | * @throws Exception
|
| | | */
|
| | | @RedPackGetVersionLimit(uid = "#uid")
|
| | | @Transactional
|
| | | private void bossReward(Long uid, Long teamUid, Integer source, String orderNo) throws Exception {
|
| | | firstSharedOrderRewardToBoss(uid, teamUid, source, orderNo);
|
| | | firstSharedOrderRewardTheMonthToBoss(uid, teamUid, source, orderNo);
|
| | | }
|
| | | |
| | | |
| | | /**
|
| | | * 被邀请人下首单返利(下单就给):
|
| | | * 完成首笔订单 (已到账) 首单=不限制金额、不限制商品、不限制数量、不限制订单类型)
|
| | | * @param uid
|
| | | * @param source
|
| | | * @param orderNo
|
| | | * @param bossId
|
| | | */
|
| | | @Transactional
|
| | | private void firstReward(Long uid, Integer source, String orderNo) throws Exception{
|
| | | // 奖励一次
|
| | | long total = redPackWinInviteMapper.countByUidAndType(uid, RedPackWinInviteTypeEnum.newUserReward.name());
|
| | | if (total > 0)
|
| | | return;
|
| | | |
| | | // 1、判断是否用户首个订单
|
| | | CommonOrderVO order = commonOrderService.firstValidOrderByUid(uid);
|
| | | if (order == null || source != order.getSourceType() || !orderNo.equals(order.getOrderNo())) |
| | | return;
|
| | | |
| | | // 金额
|
| | | BigDecimal money = new BigDecimal(redPackConfigService.getValueByKey("new_user_first_order"));
|
| | | |
| | | // 3、获得红包记录
|
| | | RedPackWinInvite winInvite = new RedPackWinInvite();
|
| | | winInvite.setCreateTime(new Date());
|
| | | winInvite.setUpdateTime(new Date());
|
| | | winInvite.setType(RedPackWinInviteTypeEnum.newUserReward);
|
| | | winInvite.setUid(uid);
|
| | | winInvite.setTeamUid(null);
|
| | | winInvite.setMoney(money);
|
| | | winInvite.setSource(source);
|
| | | winInvite.setOrderNo(orderNo);
|
| | | redPackWinInviteMapper.insertSelective(winInvite);
|
| | | |
| | | // 4、增加红包
|
| | | redPackBalanceService.addRedPack(uid, money, RedPackDetailFactory.createNewUserReward(winInvite));
|
| | | |
| | | //消息
|
| | | MsgRedPackAddContentDTO dto = new MsgRedPackAddContentDTO();
|
| | | dto.setTitle("红包增加");
|
| | | dto.setMoney("¥" + money.setScale(2));
|
| | | dto.setBalance("¥" + redPackBalanceService.getBalance(uid).setScale(2));
|
| | | userMoneyMsgNotificationService.redPackMsg(uid, MsgTypeMoneyTypeEnum.redPackNewUserReward, new Gson().toJson(dto), "红包可用于购买会员");
|
| | | }
|
| | | |
| | | |
| | | /**
|
| | | * 上级奖励:被邀请人完成首笔分享订单 (已到账) |
| | | * @param uid
|
| | | * @param source
|
| | | * @param orderNo
|
| | | * @param bossId
|
| | | */
|
| | | @Transactional
|
| | | private void firstSharedOrderRewardToBoss(Long uid, Long inviteeUid, Integer source, String orderNo) throws Exception{
|
| | | // 同一队员奖励一次
|
| | | long totalReward = redPackWinInviteMapper.countByUidAndTeamUidAndType(uid, inviteeUid, RedPackWinInviteTypeEnum.firstSharedOrder.name());
|
| | | if (totalReward > 0)
|
| | | return;
|
| | | |
| | | // 1、判断是否是改用户首个分享订单
|
| | | CommonOrderVO order = commonOrderService.firstValidOrderByUidAndType(inviteeUid, 2);
|
| | | if (order == null || source != order.getSourceType() || !orderNo.equals(order.getOrderNo())) |
| | | return;
|
| | | |
| | | // 2、判断是第几个人
|
| | | long total = redPackWinInviteMapper.countByUidAndType(uid, RedPackWinInviteTypeEnum.firstSharedOrder.name());
|
| | | |
| | | |
| | | // 计算应增加红包金额
|
| | | BigDecimal money = new BigDecimal(redPackConfigService.getValueByKey("first_shared_order"));
|
| | | long num = total % 9;
|
| | | if (num > 0) {
|
| | | BigDecimal addMoney = new BigDecimal(redPackConfigService.getValueByKey("first_shared_order_add"));
|
| | | money = MoneyBigDecimalUtil.add(money, MoneyBigDecimalUtil.mul(addMoney, new BigDecimal(num)));
|
| | | }
|
| | | |
| | | // 3、获得红包记录
|
| | | RedPackWinInvite winInvite = new RedPackWinInvite();
|
| | | winInvite.setCreateTime(new Date());
|
| | | winInvite.setUpdateTime(new Date());
|
| | | winInvite.setType(RedPackWinInviteTypeEnum.firstSharedOrder);
|
| | | winInvite.setUid(uid);
|
| | | winInvite.setTeamUid(inviteeUid);
|
| | | winInvite.setMoney(money);
|
| | | winInvite.setSource(source);
|
| | | winInvite.setOrderNo(orderNo);
|
| | | redPackWinInviteMapper.insertSelective(winInvite);
|
| | | |
| | | // 4、增加红包
|
| | | redPackBalanceService.addRedPack(uid, money, RedPackDetailFactory.createIncreaseReward(winInvite));
|
| | | |
| | | // 5、消息
|
| | | MsgRedPackAddContentDTO dto = new MsgRedPackAddContentDTO();
|
| | | dto.setTitle("红包增加");
|
| | | dto.setMoney("¥" + money.setScale(2));
|
| | | dto.setBalance("¥" + redPackBalanceService.getBalance(uid).setScale(2));
|
| | | userMoneyMsgNotificationService.redPackMsg(uid, MsgTypeMoneyTypeEnum.redPackFirstSharedOrder, new Gson().toJson(dto), "红包可用于购买会员");
|
| | | }
|
| | | |
| | | |
| | | /**
|
| | | * 上级奖励:次月开始产生满足条件的分享订单(分享订单实付款≥10元) 连续15个月 自然月获得(已到账)每月最高2元
|
| | | * @param uid
|
| | | * @param source
|
| | | * @param orderNo
|
| | | * @param bossId
|
| | | */
|
| | | @Transactional
|
| | | private void firstSharedOrderRewardTheMonthToBoss(Long uid, Long inviteeUid, Integer source, String orderNo) throws Exception{
|
| | | // 1、判断是否是上个月首单
|
| | | CommonOrderVO order = commonOrderService.firstValidOrderLastMonthByUidAndType(inviteeUid, 2);
|
| | | if (order == null || source != order.getSourceType() || !orderNo.equals(order.getOrderNo()) ||
|
| | | order.getAccountTime() == null) |
| | | return;
|
| | | |
| | | // 分享订单实付款≥10元
|
| | | if (order.getPayment() == null || order.getPayment().compareTo(Constant.RED_PACK_PAY_MIN) < 0) |
| | | return;
|
| | | |
| | | // 查询首单奖励时间
|
| | | CommonOrderVO firstorder = commonOrderService.firstValidOrderByUidAndType(inviteeUid, 2);
|
| | | if (firstorder == null || firstorder.getAccountTime() == null) |
| | | return;
|
| | | |
| | | // 第一次到账的次月开始连续15个月 自然月获得(已到账)
|
| | | int monthSpace = DateUtil.getMonthSpace( order.getAccountTime(), firstorder.getAccountTime());
|
| | | if (monthSpace == 0 || monthSpace > Constant.RED_PACK_REWARd_MONTH) |
| | | return;
|
| | | |
| | | |
| | | // 计算随机奖励
|
| | | double randomMoney = Constant.RED_PACK_RANDOM_MIN + Math.random() * (Constant.RED_PACK_RANDOM_MAX - Constant.RED_PACK_RANDOM_MIN);
|
| | | // 保留两位小数
|
| | | BigDecimal money = new BigDecimal(randomMoney).setScale(2, BigDecimal.ROUND_HALF_UP);
|
| | | |
| | | // 3、获得红包记录
|
| | | RedPackWinInvite winInvite = new RedPackWinInvite();
|
| | | winInvite.setCreateTime(new Date());
|
| | | winInvite.setUpdateTime(new Date());
|
| | | winInvite.setType(RedPackWinInviteTypeEnum.monthSharedOrder);
|
| | | winInvite.setUid(uid);
|
| | | winInvite.setTeamUid(inviteeUid);
|
| | | winInvite.setMoney(money);
|
| | | winInvite.setSource(source);
|
| | | winInvite.setOrderNo(orderNo);
|
| | | redPackWinInviteMapper.insertSelective(winInvite);
|
| | | |
| | | // 4、增加红包
|
| | | redPackBalanceService.addRedPack(uid, money, RedPackDetailFactory.createSeriesReward(winInvite));
|
| | | |
| | | //消息
|
| | | MsgRedPackAddContentDTO dto = new MsgRedPackAddContentDTO();
|
| | | dto.setTitle("红包增加");
|
| | | dto.setMoney("¥" + money.setScale(2));
|
| | | dto.setBalance("¥" + redPackBalanceService.getBalance(uid).setScale(2));
|
| | | userMoneyMsgNotificationService.redPackMsg(uid, MsgTypeMoneyTypeEnum.redPackMonthSharedOrder, new Gson().toJson(dto), "红包可用于购买会员");
|
| | | }
|
| | | |
| | | |
| | | @RequestSerializableByKeyService(key = "#uid")
|
| | | @Override
|
| | | @Transactional
|
| | | public void inviteSucceedReward(Long uid) throws Exception{
|
| | | // 验证是否存在上级
|
| | | UserInfo boss = threeSaleSerivce.getBoss(uid);
|
| | | if(boss == null) |
| | | return;
|
| | | |
| | | // 同一队员奖励一次
|
| | | long totalReward = redPackWinInviteMapper.countByUidAndTeamUidAndType(boss.getId(), uid, RedPackWinInviteTypeEnum.inviteSucceed.name());
|
| | | if (totalReward > 0)
|
| | | return;
|
| | | |
| | | // 1、绑定电话号码
|
| | | UserInfo userInfo = userInfoService.selectByPKey(uid);
|
| | | if (userInfo == null || StringUtil.isNullOrEmpty(userInfo.getPhone())) |
| | | return;
|
| | | |
| | | // 2、授权淘宝
|
| | | UserExtraTaoBaoInfo taoBaoInfo = userExtraTaoBaoInfoService.getByUid(uid);
|
| | | if (taoBaoInfo == null || StringUtil.isNullOrEmpty(taoBaoInfo.getTaoBaoUid()))
|
| | | return;
|
| | | |
| | | |
| | | boolean finishTask = false;
|
| | | // 3.1、产生任意有效订单(无需到账)
|
| | | CommonOrder commonOrder = commonOrderService.selectLatestValidByUid(uid);
|
| | | if (commonOrder != null) |
| | | finishTask = true;
|
| | | |
| | | // 3.2、单位时间类累计产生≥200金币
|
| | | if (!finishTask) {
|
| | | long totalGoldCoin = integralTaskRecordService.getTotalGoldCoinByUid(uid);
|
| | | if (totalGoldCoin >= Constant.RED_PACK_GOLD_COIN)
|
| | | finishTask = true;
|
| | | }
|
| | | |
| | | // 3.3、成功邀请一个好友
|
| | | if (!finishTask) {
|
| | | long countTeam = threeSaleSerivce.countSuccessFirstTeam(uid);
|
| | | if (countTeam >= 1)
|
| | | finishTask = true;
|
| | | }
|
| | | |
| | | if (finishTask) {
|
| | | // 奖励金额
|
| | | BigDecimal money = new BigDecimal(redPackConfigService.getValueByKey("invite_succeed"));
|
| | | |
| | | // 获得红包记录
|
| | | RedPackWinInvite winInvite = new RedPackWinInvite();
|
| | | winInvite.setCreateTime(new Date());
|
| | | winInvite.setUpdateTime(new Date());
|
| | | winInvite.setType(RedPackWinInviteTypeEnum.inviteSucceed);
|
| | | winInvite.setUid(boss.getId());
|
| | | winInvite.setTeamUid(uid);
|
| | | winInvite.setMoney(money);
|
| | | redPackWinInviteMapper.insertSelective(winInvite);
|
| | | |
| | | // 增加红包
|
| | | redPackBalanceService.addRedPack(boss.getId(), money, RedPackDetailFactory.createInvite(winInvite));
|
| | | |
| | | // 消息
|
| | | MsgRedPackAddContentDTO dto = new MsgRedPackAddContentDTO();
|
| | | dto.setTitle("红包增加");
|
| | | dto.setMoney("¥" + money.setScale(2));
|
| | | dto.setBalance("¥" + redPackBalanceService.getBalance(boss.getId()).setScale(2));
|
| | | userMoneyMsgNotificationService.redPackMsg(boss.getId(), MsgTypeMoneyTypeEnum.redPackInviteSucceed, new Gson().toJson(dto), "红包可用于购买会员");
|
| | | }
|
| | | }
|
| | | |
| | | |
| | | }
|