yujian
2019-11-07 3f76ec837b343e5a8538f2ce50010714d16cdeef
fanli/src/main/java/com/yeshi/fanli/service/impl/redpack/RedPackWinInviteServiceImpl.java
@@ -31,6 +31,8 @@
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;
@@ -77,7 +79,15 @@
   }
   
   @Override
   public BigDecimal getRewardMoney(Long uid) {
      return redPackWinInviteMapper.getRewardMoney(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;
@@ -85,58 +95,25 @@
      // 上一级的奖励
      UserInfo boss = threeSaleSerivce.getBoss(uid);
      if(boss != null) {
         firstReward(uid, source, orderNo);
         firstSharedOrderRewardToBoss(boss.getId(), uid, source, orderNo);
         firstSharedOrderRewardTheMonthToBoss(boss.getId(), uid, source, orderNo);
         bossReward(boss.getId(), uid, source, orderNo);
      }
   }
   
   /**
    * 被邀请人下首单返利(下单就给):
    * 完成首笔订单 (已到账) 首单=不限制金额、不限制商品、不限制数量、不限制订单类型)
    * 上级奖励
    * @param uid
    * @param teamUid
    * @param source
    * @param orderNo
    * @param bossId
    * @throws Exception
    */
   @RedPackGetVersionLimit(uid = "#uid")
   @Transactional
   private void firstReward(Long uid, Integer source, String orderNo) throws Exception{
      // 奖励一次
      long total = redPackWinInviteMapper.countByUidAndType(uid, RedPackWinInviteTypeEnum.newUserReward.name());
      if (total > 1)
         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), "红包可用于购买会员");
   private void  bossReward(Long uid, Long teamUid, Integer source, String orderNo)  throws Exception {
      firstSharedOrderRewardToBoss(uid, teamUid, source, orderNo);
      firstSharedOrderRewardTheMonthToBoss(uid, teamUid, source, orderNo);
   }
   
   /**
    * 上级奖励:被邀请人完成首笔分享订单 (已到账) 
@@ -149,16 +126,17 @@
   private void firstSharedOrderRewardToBoss(Long uid, Long inviteeUid, Integer source, String orderNo) throws Exception{
      // 同一队员奖励一次
      long totalReward = redPackWinInviteMapper.countByUidAndTeamUidAndType(uid, inviteeUid, RedPackWinInviteTypeEnum.firstSharedOrder.name());
      if (totalReward > 1)
      if (totalReward > 0)
         return;
      
      // 1、判断是否是改用户首个分享订单
      CommonOrderVO order = commonOrderService.firstValidOrderByUidAndType(inviteeUid, 2);
      if (order == null || source == order.getSourceType() || orderNo.equals(order.getOrderNo()))
      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"));
@@ -201,9 +179,9 @@
    */
   @Transactional
   private void firstSharedOrderRewardTheMonthToBoss(Long uid, Long inviteeUid, Integer source, String orderNo) throws Exception{
      // 1、判断是否是本月首单
      CommonOrderVO order = commonOrderService.firstValidOrderTheMonthByUidAndType(inviteeUid, 2);
      if (order == null || source == order.getSourceType() || orderNo.equals(order.getOrderNo()) ||
      // 1、判断是否是上个月首单
      CommonOrderVO order = commonOrderService.firstValidOrderLastMonthByUidAndType(inviteeUid, 2);
      if (order == null || source != order.getSourceType() || !orderNo.equals(order.getOrderNo()) ||
            order.getAccountTime() == null) 
         return;
      
@@ -251,9 +229,10 @@
   }
   
   
   @RequestSerializableByKeyService(key = "#uid")
   @Override
   @Transactional
   private void inviteSucceedReward(Long uid) throws Exception{
   public void inviteSucceedReward(Long uid) throws Exception{
      // 验证是否存在上级
      UserInfo boss = threeSaleSerivce.getBoss(uid);
      if(boss == null) 
@@ -261,7 +240,7 @@
      
      // 同一队员奖励一次
      long totalReward = redPackWinInviteMapper.countByUidAndTeamUidAndType(boss.getId(), uid, RedPackWinInviteTypeEnum.inviteSucceed.name());
      if (totalReward > 1)
      if (totalReward > 0)
         return;
      
      // 1、绑定电话号码