admin
2020-05-20 98b1a0affd69bbe63223c21fdd2c404e8bedfccb
fanli/src/main/java/com/yeshi/fanli/service/impl/order/OrderMoneySettleServiceImpl.java
@@ -2,7 +2,6 @@
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
@@ -29,6 +28,7 @@
import com.yeshi.fanli.dto.HongBao;
import com.yeshi.fanli.dto.mq.order.OrderTopicTagEnum;
import com.yeshi.fanli.dto.mq.order.body.OrderMoneyRecievedMQMsg;
import com.yeshi.fanli.dto.mq.order.body.TeamRewardPreRecieveMQMsg;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.money.TeamRewardDebt;
@@ -52,9 +52,7 @@
import com.yeshi.fanli.service.inter.order.CommonOrderService;
import com.yeshi.fanli.service.inter.order.OrderMoneySettleService;
import com.yeshi.fanli.service.inter.user.UserSystemCouponService;
import com.yeshi.fanli.service.manger.money.TeamEincomeManager;
import com.yeshi.fanli.service.manger.order.TeamRewardManager;
import com.yeshi.fanli.service.manger.order.TeamSubsidyManager;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.TimeUtil;
import com.yeshi.fanli.util.cmq.HongBaoRecieveCMQManager;
@@ -109,8 +107,6 @@
   @Resource
   private TeamRewardManager teamRewardManager;
   @Resource
   private TeamSubsidyManager teamSubsidyManager;
   // 下级被封禁,红包失效
   private void invalidHongBaoForbidden(Long id) {
@@ -123,7 +119,7 @@
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void inviteSettleTB(Long uid) throws OrderMoneySettleException {
   public void inviteSettleTB(Long uid, Date maxPregetTime) throws OrderMoneySettleException {
      // 查询UID的一二级邀请赚
      int sourceType = Constant.SOURCE_TYPE_TAOBAO;
      List<Integer> types = new ArrayList<>();
@@ -131,13 +127,13 @@
      types.add(HongBaoV2.TYPE_ERJI);
      types.add(HongBaoV2.TYPE_SHARE_YIJI);
      types.add(HongBaoV2.TYPE_SHARE_ERJI);
      long count = hongBaoV2Mapper.countCanBalanceHongBaoByTypeAndUid(types, uid, sourceType);
      long count = hongBaoV2Mapper.countCanBalanceHongBaoByTypeAndUid(types, uid, sourceType, maxPregetTime);
      List<HongBaoV2> hongBaoList = new ArrayList<>();
      // 1000条数据为1页
      int page = (int) (count % 1000 == 0 ? count / 1000 : count / 1000 + 1);
      for (int i = 0; i < page; i++) {
         List<HongBaoV2> tempHongBaoList = hongBaoV2Mapper.listCanBalanceHongBaoByTypeAndUid(types, uid, sourceType,
               i * 1000, 1000);
               maxPregetTime, i * 1000, 1000);
         if (tempHongBaoList != null && tempHongBaoList.size() > 0)
            hongBaoList.addAll(tempHongBaoList);
      }
@@ -161,12 +157,13 @@
         }
      }
      // 邀请赚到账事务消息
      OrderMoneyRecievedMQMsg mqMsg = new OrderMoneyRecievedMQMsg(OrderMoneyRecievedMQMsg.TYPE_INVITE, uid,
            sourceType, null, null, new Date(), 0);
      String taskKey = getTaskKey(uid);
      TeamRewardPreRecieveMQMsg mqMsg = new TeamRewardPreRecieveMQMsg(taskKey, uid, sourceType, maxPregetTime,
            new Date());
      Message msg = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER, OrderTopicTagEnum.teamRewardPreRecieved, mqMsg);
      String taskKey = getTaskKey(uid);
      msg.setKey(taskKey);
      // 添加事务消息
      try {
@@ -174,14 +171,16 @@
            @Override
            public TransactionStatus execute(Message arg0, Object arg1) {
               try {
                  fanliInvite(hongBaoList, uid, Constant.SOURCE_TYPE_TAOBAO, taskKey);
                  fanliInvite(hongBaoList, uid, Constant.SOURCE_TYPE_TAOBAO, taskKey, maxPregetTime);
               } catch (Exception e) {
                  e.printStackTrace();
                  return TransactionStatus.RollbackTransaction;
               }
               return TransactionStatus.CommitTransaction;
            }
         }, null);
      } catch (Exception e) {
         e.printStackTrace();
         LogHelper.mqError(e.getMessage(), msg.getTopic(), msg.getTag(), new Gson().toJson(mqMsg));
      }
      System.out.println(new Gson().toJson(mqMsg));
@@ -189,7 +188,7 @@
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void inviteSettleJD(Long uid) throws OrderMoneySettleException {
   public void inviteSettleJD(Long uid, Date maxPregetTime) throws OrderMoneySettleException {
      // 查询UID的一二级邀请赚
      int sourceType = Constant.SOURCE_TYPE_JD;
      List<Integer> types = new ArrayList<>();
@@ -197,13 +196,13 @@
      types.add(HongBaoV2.TYPE_ERJI);
      types.add(HongBaoV2.TYPE_SHARE_YIJI);
      types.add(HongBaoV2.TYPE_SHARE_ERJI);
      long count = hongBaoV2Mapper.countCanBalanceHongBaoByTypeAndUid(types, uid, sourceType);
      long count = hongBaoV2Mapper.countCanBalanceHongBaoByTypeAndUid(types, uid, sourceType, maxPregetTime);
      List<HongBaoV2> hongBaoList = new ArrayList<>();
      // 1000条数据为1页
      int page = (int) (count % 1000 == 0 ? count / 1000 : count / 1000 + 1);
      for (int i = 0; i < page; i++) {
         List<HongBaoV2> tempHongBaoList = hongBaoV2Mapper.listCanBalanceHongBaoByTypeAndUid(types, uid, sourceType,
               i * 1000, 1000);
               maxPregetTime, i * 1000, 1000);
         if (tempHongBaoList != null && tempHongBaoList.size() > 0)
            hongBaoList.addAll(tempHongBaoList);
      }
@@ -228,10 +227,11 @@
      }
      // 邀请赚到账事务消息
      OrderMoneyRecievedMQMsg mqMsg = new OrderMoneyRecievedMQMsg(OrderMoneyRecievedMQMsg.TYPE_INVITE, uid,
            sourceType, null, null, new Date(), 0);
      Message msg = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER, OrderTopicTagEnum.teamRewardPreRecieved, mqMsg);
      String taskKey = getTaskKey(uid);
      TeamRewardPreRecieveMQMsg mqMsg = new TeamRewardPreRecieveMQMsg(taskKey, uid, sourceType, maxPregetTime,
            new Date());
      Message msg = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER, OrderTopicTagEnum.teamRewardPreRecieved, mqMsg);
      msg.setKey(taskKey);
      // 添加事务消息
      try {
@@ -239,7 +239,7 @@
            @Override
            public TransactionStatus execute(Message arg0, Object arg1) {
               try {
                  fanliInvite(hongBaoList, uid, sourceType, taskKey);
                  fanliInvite(hongBaoList, uid, sourceType, taskKey, maxPregetTime);
               } catch (Exception e) {
                  e.printStackTrace();
                  return TransactionStatus.RollbackTransaction;
@@ -254,7 +254,7 @@
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void inviteSettlePDD(Long uid) throws OrderMoneySettleException {
   public void inviteSettlePDD(Long uid, Date maxPregetTime) throws OrderMoneySettleException {
      // 查询UID的一二级邀请赚
      int sourceType = Constant.SOURCE_TYPE_PDD;
      List<Integer> types = new ArrayList<>();
@@ -262,13 +262,13 @@
      types.add(HongBaoV2.TYPE_ERJI);
      types.add(HongBaoV2.TYPE_SHARE_YIJI);
      types.add(HongBaoV2.TYPE_SHARE_ERJI);
      long count = hongBaoV2Mapper.countCanBalanceHongBaoByTypeAndUid(types, uid, sourceType);
      long count = hongBaoV2Mapper.countCanBalanceHongBaoByTypeAndUid(types, uid, sourceType, maxPregetTime);
      List<HongBaoV2> hongBaoList = new ArrayList<>();
      // 1000条数据为1页
      int page = (int) (count % 1000 == 0 ? count / 1000 : count / 1000 + 1);
      for (int i = 0; i < page; i++) {
         List<HongBaoV2> tempHongBaoList = hongBaoV2Mapper.listCanBalanceHongBaoByTypeAndUid(types, uid, sourceType,
               i * 1000, 1000);
               maxPregetTime, i * 1000, 1000);
         if (tempHongBaoList != null && tempHongBaoList.size() > 0)
            hongBaoList.addAll(tempHongBaoList);
      }
@@ -293,10 +293,10 @@
      }
      // 邀请赚到账事务消息
      OrderMoneyRecievedMQMsg mqMsg = new OrderMoneyRecievedMQMsg(OrderMoneyRecievedMQMsg.TYPE_INVITE, uid,
            sourceType, null, null, new Date(), 0);
      Message msg = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER, OrderTopicTagEnum.teamRewardPreRecieved, mqMsg);
      String taskKey = getTaskKey(uid);
      TeamRewardPreRecieveMQMsg mqMsg = new TeamRewardPreRecieveMQMsg(taskKey, uid, sourceType, maxPregetTime,
            new Date());
      Message msg = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER, OrderTopicTagEnum.teamRewardPreRecieved, mqMsg);
      msg.setKey(taskKey);
      // 添加事务消息
      try {
@@ -304,7 +304,7 @@
            @Override
            public TransactionStatus execute(Message arg0, Object arg1) {
               try {
                  fanliInvite(hongBaoList, uid, sourceType, taskKey);
                  fanliInvite(hongBaoList, uid, sourceType, taskKey, maxPregetTime);
               } catch (Exception e) {
                  return TransactionStatus.RollbackTransaction;
               }
@@ -318,7 +318,7 @@
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void shareSettleTB(Long uid) throws OrderMoneySettleException {
   public void shareSettleTB(Long uid, Date maxPregetTime) throws OrderMoneySettleException {
      /**
       * 处理分享赚
       */
@@ -327,13 +327,13 @@
      // 查询UID的分享赚订单
      types.add(HongBaoV2.TYPE_SHARE_GOODS);
      long count = hongBaoV2Mapper.countCanBalanceHongBaoByTypeAndUid(types, uid, sourceType);
      long count = hongBaoV2Mapper.countCanBalanceHongBaoByTypeAndUid(types, uid, sourceType, maxPregetTime);
      List<HongBaoV2> hongBaoList = new ArrayList<>();
      // 1000条数据为1页
      int page = (int) (count % 1000 == 0 ? count / 1000 : count / 1000 + 1);
      for (int i = 0; i < page; i++) {
         List<HongBaoV2> tempHongBaoList = hongBaoV2Mapper.listCanBalanceHongBaoByTypeAndUid(types, uid, sourceType,
               i * 1000, 1000);
               maxPregetTime, i * 1000, 1000);
         if (tempHongBaoList != null && tempHongBaoList.size() > 0)
            hongBaoList.addAll(tempHongBaoList);
      }
@@ -364,7 +364,7 @@
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void shareSettleJD(Long uid) throws OrderMoneySettleException {
   public void shareSettleJD(Long uid, Date maxPregetTime) throws OrderMoneySettleException {
      /**
       * 处理分享赚
       */
@@ -373,13 +373,13 @@
      // 查询UID的分享赚订单
      types.add(HongBaoV2.TYPE_SHARE_GOODS);
      long count = hongBaoV2Mapper.countCanBalanceHongBaoByTypeAndUid(types, uid, sourceType);
      long count = hongBaoV2Mapper.countCanBalanceHongBaoByTypeAndUid(types, uid, sourceType, maxPregetTime);
      List<HongBaoV2> hongBaoList = new ArrayList<>();
      // 1000条数据为1页
      int page = (int) (count % 1000 == 0 ? count / 1000 : count / 1000 + 1);
      for (int i = 0; i < page; i++) {
         List<HongBaoV2> tempHongBaoList = hongBaoV2Mapper.listCanBalanceHongBaoByTypeAndUid(types, uid, sourceType,
               i * 1000, 1000);
               maxPregetTime, i * 1000, 1000);
         if (tempHongBaoList != null && tempHongBaoList.size() > 0)
            hongBaoList.addAll(tempHongBaoList);
      }
@@ -405,7 +405,7 @@
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void shareSettlePDD(Long uid) throws OrderMoneySettleException {
   public void shareSettlePDD(Long uid, Date maxPregetTime) throws OrderMoneySettleException {
      /**
       * 处理分享赚
       */
@@ -414,13 +414,13 @@
      // 查询UID的分享赚订单
      types.add(HongBaoV2.TYPE_SHARE_GOODS);
      long count = hongBaoV2Mapper.countCanBalanceHongBaoByTypeAndUid(types, uid, sourceType);
      long count = hongBaoV2Mapper.countCanBalanceHongBaoByTypeAndUid(types, uid, sourceType, maxPregetTime);
      List<HongBaoV2> hongBaoList = new ArrayList<>();
      // 1000条数据为1页
      int page = (int) (count % 1000 == 0 ? count / 1000 : count / 1000 + 1);
      for (int i = 0; i < page; i++) {
         List<HongBaoV2> tempHongBaoList = hongBaoV2Mapper.listCanBalanceHongBaoByTypeAndUid(types, uid, sourceType,
               i * 1000, 1000);
               maxPregetTime, i * 1000, 1000);
         if (tempHongBaoList != null && tempHongBaoList.size() > 0)
            hongBaoList.addAll(tempHongBaoList);
      }
@@ -592,7 +592,7 @@
   }
   @Transactional
   private void fanliInvite(List<HongBaoV2> hongBaoList, Long uid, int sourceType, String key)
   private void fanliInvite(List<HongBaoV2> hongBaoList, Long uid, int sourceType, String key, Date maxPreGetTime)
         throws TeamEincomeRecordException, ParamsException, TeamRewardDebtException {
      List<Long> hbIdList = new ArrayList<>();
      BigDecimal invitemoney = new BigDecimal(0);
@@ -633,11 +633,8 @@
      // 邀请赚到账
      if (invitemoney.compareTo(new BigDecimal(0)) > 0) {
         Calendar ca = Calendar.getInstance();
         Date date = new Date(TimeUtil.convertToTimeTemp(
               ca.get(Calendar.YEAR) + "-" + (ca.get(Calendar.MONTH) + 1) + "-25", "yyyy-M-dd"));
         // 获取当前的月份
         teamRewardManager.addToEincome(uid, date, invitemoney, sourceType);
         teamRewardManager.addToEincome(uid, maxPreGetTime, invitemoney, sourceType);
      }
      hongBaoV2SettleTempService.addTemp(hbIdList, key);
@@ -645,7 +642,8 @@
      // 所有的返利到账红包ID
      for (Long hongBaoId : hbIdList) {
         try {
            HongBaoRecieveCMQManager.getInstance().addHongBaoRecieveMsg(hongBaoId);
            if (Constant.ENABLE_MQ)
               HongBaoRecieveCMQManager.getInstance().addHongBaoRecieveMsg(hongBaoId);
         } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
         }
@@ -664,7 +662,6 @@
      List<Long> hbIdList = new ArrayList<>();
      Set<String> drawBackOrders = new HashSet<String>();
      List<Long> recieveHongBaoIds = new ArrayList<>();
      int shareGoodsCount = 0;
      Set<String> shareOrders = new HashSet<>();
      for (HongBaoV2 hongBao : hongBaoList) {
@@ -688,7 +685,6 @@
            HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByHongBaoId(hongBao.getId());
            if (hongBaoOrder != null) {
               balanceTime = hongBaoOrder.getCommonOrder().getSettleTime();
               shareGoodsCount += hongBaoOrder.getCommonOrder().getCount();
            }
            shareOrders.add(hongBaoOrder.getCommonOrder().getOrderNo());
            if (balanceTime != null
@@ -705,22 +701,8 @@
      if (sharemoney.compareTo(new BigDecimal(0)) > 0) {
         // 添加新版详情记录
         try {
            // 计算本月的有效订单,失效订单,维权订单
            Calendar ca = Calendar.getInstance();
            Date maxDate = new Date(TimeUtil
                  .convertToTimeTemp(ca.get(Calendar.YEAR) + "-" + (ca.get(Calendar.MONTH) + 1), "yyyy-M"));
            ca.add(Calendar.MONTH, -1);
            Date minDate = new Date(TimeUtil
                  .convertToTimeTemp(ca.get(Calendar.YEAR) + "-" + (ca.get(Calendar.MONTH) + 1), "yyyy-M"));
            long validCount = hongBaoV2Mapper.countShareOrderCountByUidAndSettleTime(uid, CommonOrder.STATE_JS,
                  minDate, maxDate, Constant.SOURCE_TYPE_TAOBAO);
            long weiQuanCount = hongBaoV2Mapper.countShareOrderCountByUidAndSettleTime(uid, CommonOrder.STATE_WQ,
                  minDate, maxDate, Constant.SOURCE_TYPE_TAOBAO);
            UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createShare(uid, Constant.SOURCE_TYPE_TAOBAO,
                  (int) validCount, (int) weiQuanCount, sharemoney, new Date());
                  sharemoney, new Date());
            // 添加资金
            userMoneyService.addUserMoney(uid, sharemoney, userMoneyDetail);
@@ -737,9 +719,8 @@
         }
         // 新版通知
         userMoneyMsgNotificationService.shareOrderReceived(uid, Constant.SOURCE_TYPE_TAOBAO, shareOrders.size(),
               shareGoodsCount, sharemoney, userInfoMapper.selectAvailableByPrimaryKey(uid).getMyHongBao(), null,
               null);
         userMoneyMsgNotificationService.shareOrderReceived(uid, Constant.SOURCE_TYPE_TAOBAO, sharemoney,
               userInfoMapper.selectAvailableByPrimaryKey(uid).getMyHongBao(), new Date());
         for (String orderId : drawBackOrders)
            taoBaoWeiQuanDrawBackService.doWeiQuanShare(orderId);
@@ -750,7 +731,8 @@
      // 所有的返利到账红包ID
      for (Long hongBaoId : recieveHongBaoIds) {
         try {
            HongBaoRecieveCMQManager.getInstance().addHongBaoRecieveMsg(hongBaoId);
            if (Constant.ENABLE_MQ)
               HongBaoRecieveCMQManager.getInstance().addHongBaoRecieveMsg(hongBaoId);
         } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
         }
@@ -769,7 +751,6 @@
      BigDecimal sharemoney = new BigDecimal(0);
      List<Long> hbIdList = new ArrayList<>();
      List<Long> recieveHongBaoIds = new ArrayList<>();
      int shareGoodsCount = 0;
      Set<String> shareOrders = new HashSet<>();
      for (HongBaoV2 hongBao : hongBaoList) {
@@ -790,9 +771,6 @@
            hbIdList.add(hongBao.getId());
            // 2018-08-05 过后的订单才处理维权
            HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByHongBaoId(hongBao.getId());
            if (hongBaoOrder != null) {
               shareGoodsCount += hongBaoOrder.getCommonOrder().getCount();
            }
            shareOrders.add(hongBaoOrder.getCommonOrder().getOrderNo());
         }
      }
@@ -804,22 +782,8 @@
      if (sharemoney.compareTo(new BigDecimal(0)) > 0) {
         // 添加新版详情记录
         try {
            // 计算本月的有效订单,失效订单,维权订单
            Calendar ca = Calendar.getInstance();
            Date maxDate = new Date(TimeUtil
                  .convertToTimeTemp(ca.get(Calendar.YEAR) + "-" + (ca.get(Calendar.MONTH) + 1), "yyyy-M"));
            ca.add(Calendar.MONTH, -1);
            Date minDate = new Date(TimeUtil
                  .convertToTimeTemp(ca.get(Calendar.YEAR) + "-" + (ca.get(Calendar.MONTH) + 1), "yyyy-M"));
            long validCount = hongBaoV2Mapper.countShareOrderCountByUidAndSettleTime(uid, CommonOrder.STATE_JS,
                  minDate, maxDate, sourceType);
            long weiQuanCount = hongBaoV2Mapper.countShareOrderCountByUidAndSettleTime(uid, CommonOrder.STATE_WQ,
                  minDate, maxDate, sourceType);
            UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createShare(uid, sourceType, (int) validCount,
                  (int) weiQuanCount, sharemoney, new Date());
            UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createShare(uid, sourceType, sharemoney,
                  new Date());
            // 添加资金
            userMoneyService.addUserMoney(uid, sharemoney, userMoneyDetail);
@@ -835,10 +799,8 @@
            }
         }
         // 新版通知
         userMoneyMsgNotificationService.shareOrderReceived(uid, Constant.SOURCE_TYPE_TAOBAO, shareOrders.size(),
               shareGoodsCount, sharemoney, userInfoMapper.selectAvailableByPrimaryKey(uid).getMyHongBao(), null,
               null);
         userMoneyMsgNotificationService.shareOrderReceived(uid, sourceType, sharemoney,
               userInfoMapper.selectAvailableByPrimaryKey(uid).getMyHongBao(), new Date());
      }
      hongBaoV2SettleTempService.addTemp(recieveHongBaoIds, key);
@@ -846,27 +808,14 @@
      // 所有的返利到账红包ID
      for (Long hongBaoId : recieveHongBaoIds) {
         try {
            HongBaoRecieveCMQManager.getInstance().addHongBaoRecieveMsg(hongBaoId);
            if (Constant.ENABLE_MQ)
               HongBaoRecieveCMQManager.getInstance().addHongBaoRecieveMsg(hongBaoId);
         } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
         }
      }
   }
   /**
    * 邀请补贴结算
    *
    * @param orderId
    * @param sourceType
    * @throws ParamsException
    * @throws TeamEincomeRecordException
    */
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void inviteSubsidySettle(Long uid, String taskKey)
         throws OrderMoneySettleException, TeamEincomeRecordException, ParamsException {
      teamSubsidyManager.addToTeamEincome(uid, taskKey);
   }
   private String getTaskKey(Long uid) {
      return uid + "-" + UUID.randomUUID().toString();