admin
2019-11-16 a6f34b99ee20e528b766c8ed5a61bd9c5aa0e7ae
fanli/src/main/java/com/yeshi/fanli/service/impl/redpack/RedPackWinInviteServiceImpl.java
@@ -14,7 +14,11 @@
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.ThreeSale;
import com.yeshi.fanli.entity.bus.user.UserActiveLog;
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;
@@ -22,9 +26,16 @@
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.UserActiveLogService;
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.VersionUtil;
import com.yeshi.fanli.util.annotation.RequestSerializableByKeyService;
import com.yeshi.fanli.util.factory.RedPackDetailFactory;
import com.yeshi.fanli.vo.order.CommonOrderVO;
@@ -49,64 +60,75 @@
   @Resource
   private UserMoneyMsgNotificationService userMoneyMsgNotificationService;
   
   @Resource
   private UserInfoService userInfoService;
   @Resource
   private UserExtraTaoBaoInfoService userExtraTaoBaoInfoService;
   @Resource
   private IntegralTaskRecordService integralTaskRecordService;
   @Resource
   private UserActiveLogService userActiveLogService;
   
   @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);
   }
   @Override
   public BigDecimal getRewardMoney(Long uid) {
      return redPackWinInviteMapper.getRewardMoney(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)
      if (uid == null || source == null || StringUtil.isNullOrEmpty(orderNo))
         return;
      
      // 自己的奖励
      firstReward(uid, source, orderNo);
      ThreeSale threeSale = threeSaleSerivce.getByWorkerIdAndTime(uid, Constant.RED_PACK_REWARD_TIME);
      if(threeSale == null)
         return;
      Long bossId = threeSale.getBoss().getId();
      UserActiveLog activeLog = userActiveLogService.getUserLatestActiveInfo(bossId);
      if (activeLog == null)
         return;
      
      // 上一级的奖励
      UserInfo boss = threeSaleSerivce.getBoss(uid);
      if(boss != null) {
         firstSharedOrderRewardToBoss(boss.getId(), uid, source, orderNo);
         firstSharedOrderRewardTheMonthToBoss(boss.getId(), uid, source, orderNo);
      }
      // 小于2.0.2版本不增加
      if (!VersionUtil.greaterThan_2_1("appstore".equalsIgnoreCase(activeLog.getChannel()) ? "ios" : "android",
            activeLog.getVersionCode()))
         return;
      // 奖励上级
      bossReward(bossId, uid, source, orderNo);
   }
   
   /**
    *   新人奖励:完成首笔订单 (已到账) 首单=不限制金额、不限制商品、不限制数量、不限制订单类型)
    * 上级奖励
    * @param uid
    * @param teamUid
    * @param source
    * @param orderNo
    * @param bossId
    * @throws Exception
    */
   @Transactional
   private void firstReward(Long uid, Integer source, String orderNo) throws Exception{
      // 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);
   }
   
   /**
    * 上级奖励:被邀请人完成首笔分享订单 (已到账) 
@@ -117,9 +139,19 @@
    */
   @Transactional
   private void firstSharedOrderRewardToBoss(Long uid, Long inviteeUid, Integer source, String orderNo) throws Exception{
      // 1、判断是否是改用户首个分享订单
      CommonOrderVO order = commonOrderService.firstValidOrderByUidAndType(inviteeUid, 2);
      if (order == null || source == order.getSourceType() || orderNo.equals(order.getOrderNo()))
      // 1、同一队员奖励一次
      long totalReward = redPackWinInviteMapper.countByUidAndTeamUidAndType(uid, inviteeUid, RedPackWinInviteTypeEnum.firstSharedOrder.name());
      if (totalReward > 0)
         return;
      // 2、是否下级的首个分享订单
      CommonOrder commonOrder = commonOrderService.getFirstShareOrderByUid(inviteeUid);
      if (commonOrder == null || !orderNo.equals(commonOrder.getOrderNo()) || source != commonOrder.getSourceType())
         return ;
      // 3、到账奖励金额 是否大于5
      BigDecimal totalRewardMoney = commonOrderService.getTotalRewardMoneyByOrderNoAndSourceType(orderNo, source);
      if (totalRewardMoney == null || totalRewardMoney.compareTo(Constant.RED_PACK_ORDER_REWARD_MIN) < 0)
         return;
      
      // 2、判断是第几个人
@@ -153,10 +185,11 @@
      dto.setTitle("红包增加");
      dto.setMoney("¥" + money.setScale(2));
      dto.setBalance("¥" + redPackBalanceService.getBalance(uid).setScale(2));
      userMoneyMsgNotificationService.redPackMsg(uid, MsgTypeMoneyTypeEnum.redPackFirstSharedOrder, new Gson().toJson(dto), "红包可用于购买会员");
      userMoneyMsgNotificationService.redPackMsg(uid, MsgTypeMoneyTypeEnum.redPackFirstSharedOrder,
            new Gson().toJson(dto), "请到“我的-红包”中查看");
   }
   
   /**
    * 上级奖励:次月开始产生满足条件的分享订单(分享订单实付款≥10元) 连续15个月 自然月获得(已到账)每月最高2元
    * @param uid
@@ -166,9 +199,10 @@
    */
   @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;
      
@@ -176,15 +210,15 @@
      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;
      // 2、是否下级的首个分享订单
//      CommonOrder firstorder = commonOrderService.getFirstShareOrderByUid(inviteeUid);
//      if (firstorder == null || (orderNo.equals(firstorder.getOrderNo()) && source == firstorder.getSourceType()));
//         return;
      
      // 第一次到账的次月开始连续15个月 自然月获得(已到账)
      int monthSpace = DateUtil.getMonthSpace( order.getAccountTime(), firstorder.getAccountTime());
      if (monthSpace == 0 || monthSpace > Constant.RED_PACK_REWARd_MONTH)
         return;
//      int monthSpace = DateUtil.getMonthSpace(order.getThirdCreateTime(), firstorder.getThirdCreateTime());
//      if (monthSpace == 0 || monthSpace > Constant.RED_PACK_REWARD_MONTH)
//         return;
      
      
      // 计算随机奖励
@@ -203,6 +237,7 @@
      winInvite.setSource(source);
      winInvite.setOrderNo(orderNo);
      redPackWinInviteMapper.insertSelective(winInvite);
      // 4、增加红包
      redPackBalanceService.addRedPack(uid, money, RedPackDetailFactory.createSeriesReward(winInvite));
      
@@ -211,23 +246,93 @@
      dto.setTitle("红包增加");
      dto.setMoney("¥" + money.setScale(2));
      dto.setBalance("¥" + redPackBalanceService.getBalance(uid).setScale(2));
      userMoneyMsgNotificationService.redPackMsg(uid, MsgTypeMoneyTypeEnum.redPackMonthSharedOrder, new Gson().toJson(dto), "红包可用于购买会员");
      userMoneyMsgNotificationService.redPackMsg(uid, MsgTypeMoneyTypeEnum.redPackMonthSharedOrder,
            new Gson().toJson(dto), "请到“我的-红包”中查看");
   }
   
   
   public List<RedPackWinInvite> getRewardList() {
   @Override
   @RequestSerializableByKeyService(key = "#uid")
   @Transactional(rollbackFor = Exception.class)
   public void inviteSucceedReward(Long uid) throws Exception{
      // 验证是否存在上级
      ThreeSale threeSale = threeSaleSerivce.getByWorkerIdAndTime(uid, Constant.RED_PACK_REWARD_TIME);
      if (threeSale == null)
         return;
      // 上级id
      Long bossId = threeSale.getBoss().getId();
      UserActiveLog activeLog = userActiveLogService.getUserLatestActiveInfo(bossId);
      if (activeLog == null)
         return;
      // 小于2.0.2版本不增加
      if (!VersionUtil.greaterThan_2_1("appstore".equalsIgnoreCase(activeLog.getChannel()) ? "ios" : "android",
            activeLog.getVersionCode()))
         return;
      // 同一队员奖励一次
      long totalReward = redPackWinInviteMapper.countByUidAndTeamUidAndType(bossId, 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;
      }
      
      return null;
      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(bossId);
         winInvite.setTeamUid(uid);
         winInvite.setMoney(money);
         redPackWinInviteMapper.insertSelective(winInvite);
         // 增加红包
         redPackBalanceService.addRedPack(bossId, money, RedPackDetailFactory.createInvite(winInvite));
         // 消息
         MsgRedPackAddContentDTO dto = new MsgRedPackAddContentDTO();
         dto.setTitle("红包增加");
         dto.setMoney("¥" + money.setScale(2));
         dto.setBalance("¥" + redPackBalanceService.getBalance(bossId).setScale(2));
         userMoneyMsgNotificationService.redPackMsg(bossId, MsgTypeMoneyTypeEnum.redPackInviteSucceed,
               new Gson().toJson(dto), "请到“我的-红包”中查看");
      }
   }
   
   
}