yujian
2019-11-04 148af6b2f57be264c84d0991237fbd7ddea491a4
fanli/src/main/java/com/yeshi/fanli/service/impl/redpack/RedPackWinInviteServiceImpl.java
@@ -14,7 +14,9 @@
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;
@@ -22,9 +24,15 @@
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;
@@ -49,26 +57,63 @@
   @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;
      
      // 自己的奖励
      firstReward(uid, source, orderNo);
      // 上一级的奖励
      UserInfo boss = threeSaleSerivce.getBoss(uid);
      if(boss != null) {
         firstSharedOrderRewardToBoss(boss.getId(), uid, source, orderNo);
         firstSharedOrderRewardTheMonthToBoss(boss.getId(), uid, source, orderNo);
         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
@@ -76,9 +121,14 @@
    */
   @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()))
      if (order == null || source != order.getSourceType() || !orderNo.equals(order.getOrderNo()))
         return;
      
      // 金额
@@ -117,13 +167,19 @@
    */
   @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()))
      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"));
@@ -166,9 +222,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;
      
@@ -203,6 +259,7 @@
      winInvite.setSource(source);
      winInvite.setOrderNo(orderNo);
      redPackWinInviteMapper.insertSelective(winInvite);
      // 4、增加红包
      redPackBalanceService.addRedPack(uid, money, RedPackDetailFactory.createSeriesReward(winInvite));
      
@@ -215,19 +272,76 @@
   }
   
   
   public List<RedPackWinInvite> getRewardList() {
   @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;
      }
      
      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(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), "红包可用于购买会员");
      }
   }
   
   
}