Browse Source

修改订单的计算,改为即算即用,以及合赢奖改为次日凌晨三点结

Yangzw 7 months ago
parent
commit
fe5a239d31
10 changed files with 381 additions and 40 deletions
  1. 3 0
      feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/controller/admin/dailyawardrecords/vo/DailyAwardRecordsSaveReqVO.java
  2. 4 1
      feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/dal/dataobject/dailyawardrecords/DailyAwardRecordsDO.java
  3. 0 2
      feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/dal/dataobject/ptprofitlog/PtProfitLogDO.java
  4. 10 10
      feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/dal/mysql/ptprofitlog/PtProfitLogMapper.java
  5. 6 5
      feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/service/dailyawardrecords/DailyAwardRecordsService.java
  6. 6 3
      feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/service/dailyawardrecords/DailyAwardRecordsServiceImpl.java
  7. 3 3
      feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/service/ordercalc/OrderCalcServiceImpl.java
  8. 6 6
      feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/service/partitioncrash/PartitionCrashServiceImpl.java
  9. 8 10
      feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/service/sharepath/SharePathService.java
  10. 335 0
      feifan-module-mall/feifan-module-trade-biz/src/main/java/cn/newfeifan/mall/module/trade/service/order/TradeOrderUpdateServiceImpl.java

+ 3 - 0
feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/controller/admin/dailyawardrecords/vo/DailyAwardRecordsSaveReqVO.java

@@ -30,4 +30,7 @@ public class DailyAwardRecordsSaveReqVO {
 
     @Schema(description = "产生积分的用户ID,例如:下订单的用户ID", example = "31502")
     private Long generateUserId;
+
+    @Schema(description = "合赢奖超出额度")
+    private Long accumulatedQuotaAmount;
 }

+ 4 - 1
feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/dal/dataobject/dailyawardrecords/DailyAwardRecordsDO.java

@@ -45,5 +45,8 @@ public class DailyAwardRecordsDO extends BaseDO {
      * 产生积分的用户ID,例如:下订单的用户ID
      */
     private Long generateUserId;
-
+    /**
+     * 合赢奖超出额度
+     */
+    private Long accumulatedQuotaAmount;
 }

+ 0 - 2
feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/dal/dataobject/ptprofitlog/PtProfitLogDO.java

@@ -77,8 +77,6 @@ public class PtProfitLogDO extends BaseDO {
      * 直推奖超出额度
      */
     private Long ancestorQuotaAmount;
-
-
     /**
      * 产生积分的用户ID,例如:下订单的用户ID
      */

+ 10 - 10
feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/dal/mysql/ptprofitlog/PtProfitLogMapper.java

@@ -27,18 +27,18 @@ public interface PtProfitLogMapper extends BaseMapperX<PtProfitLogDO> {
         List<Integer> profitStatus = Arrays.asList(
                 RECOMMENDED_PERSON_QUOTA.getType(),
                 DIRECT_REFERRAL_QUOTA.getType(),
-                SMALL_QUOTA_CRASH.getType(),
+//                SMALL_QUOTA_CRASH.getType(),
                 ORDER_PAY_INTEGRAL.getType(),
                 ORDER_REFUND_INTEGRAL.getType(),
                 ORDER_CANCEL_BY_USER_REFUND_INTEGRAL.getType(),
                 ORDER_CANCEL_BY_SYSTEM_REFUND_INTEGRAL.getType(),
-                ORDER_PAY_INTEGRAL_ANCESTER.getType(),
-                ORDER_REFUND_INTEGRAL_CONSUMER.getType(),
-                ORDER_REFUND_INTEGRAL_ANCESTER.getType(),
-                ORDER_PAY_INTEGRAL_CONSUMER.getType(),
+//                ORDER_PAY_INTEGRAL_ANCESTER.getType(),
+//                ORDER_REFUND_INTEGRAL_CONSUMER.getType(),
+//                ORDER_REFUND_INTEGRAL_ANCESTER.getType(),
+//                ORDER_PAY_INTEGRAL_CONSUMER.getType(),
                 SMALL_QUOTA_CRASH_SETTLE.getType(),
-                ORDER_PAY_FREEZE_HIGH_QUOTA.getType(),
-                ORDER_REFUND_ORDER_PAY_FREEZE_HIGH_QUOTA.getType(),
+//                ORDER_PAY_FREEZE_HIGH_QUOTA.getType(),
+//                ORDER_REFUND_ORDER_PAY_FREEZE_HIGH_QUOTA.getType(),
                 MANUAL_RETURN_INTEGRAL.getType(),
                 WITHDRAWAL.getType(),
                 WITHDRAWAL_ERROR.getType()
@@ -56,13 +56,13 @@ public interface PtProfitLogMapper extends BaseMapperX<PtProfitLogDO> {
                     GROSS_PROFIT_ANCESTOR_QUOTA_PERC_EXCEED_MAXIMUM_LIMIT.getType(),
                     RECOMMENDED_PERSON_QUOTA.getType(),
                     DIRECT_REFERRAL_QUOTA.getType(),
-                    SMALL_QUOTA_CRASH.getType(),
+//                    SMALL_QUOTA_CRASH.getType(),
                     ORDER_PAY_INTEGRAL.getType(),
                     ORDER_REFUND_INTEGRAL.getType(),
                     ORDER_CANCEL_BY_USER_REFUND_INTEGRAL.getType(),
                     ORDER_CANCEL_BY_SYSTEM_REFUND_INTEGRAL.getType(),
-                    ORDER_PAY_FREEZE_HIGH_QUOTA.getType(),
-                    ORDER_REFUND_ORDER_PAY_FREEZE_HIGH_QUOTA.getType(),
+//                    ORDER_PAY_FREEZE_HIGH_QUOTA.getType(),
+//                    ORDER_REFUND_ORDER_PAY_FREEZE_HIGH_QUOTA.getType(),
                     WITHDRAWAL.getType(),
                     WITHDRAWAL_ERROR.getType()
             );

+ 6 - 5
feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/service/dailyawardrecords/DailyAwardRecordsService.java

@@ -53,12 +53,13 @@ public interface DailyAwardRecordsService {
     /**
      * 添加每日用户获得合赢奖记录
      *
-     * @param userId 用户编号
-     * @param generateUserId 来源用户
-     * @param amount 获得的合赢奖
-     * @param logId 日志编号
+     * @param userId                 用户编号
+     * @param generateUserId         来源用户
+     * @param amount                 获得的合赢奖
+     * @param logId                  日志编号
+     * @param accumulatedQuotaAmount 超出的合赢奖
      */
-    void addDailyAwardRecords(Long userId,Long generateUserId, Long amount, Long logId);
+    void addDailyAwardRecords(Long userId, Long generateUserId, Long amount, Long logId, Long accumulatedQuotaAmount);
 
     /**
      * 获取七天前的合赢奖记录,将他转换成可用积分

+ 6 - 3
feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/service/dailyawardrecords/DailyAwardRecordsServiceImpl.java

@@ -86,7 +86,7 @@ public class DailyAwardRecordsServiceImpl implements DailyAwardRecordsService {
     }
 
     @Override
-    public void addDailyAwardRecords(Long userId,Long generateUserId, Long amount, Long logId) {
+    public void addDailyAwardRecords(Long userId, Long generateUserId, Long amount, Long logId, Long accumulatedQuotaAmount) {
         DailyAwardRecordsSaveReqVO createReqVO = DailyAwardRecordsSaveReqVO
                 .builder()
                 .userId(userId)
@@ -94,6 +94,7 @@ public class DailyAwardRecordsServiceImpl implements DailyAwardRecordsService {
                 .amount(amount)
                 .settled(false)
                 .logId(logId)
+                .accumulatedQuotaAmount(accumulatedQuotaAmount)
                 .build();
         DailyAwardRecordsDO dailyAwardRecords = BeanUtils.toBean(createReqVO, DailyAwardRecordsDO.class);
         dailyAwardRecordsMapper.insert(dailyAwardRecords);
@@ -103,8 +104,9 @@ public class DailyAwardRecordsServiceImpl implements DailyAwardRecordsService {
     public void transitionBonus() {
         //获取到当前7天前未结算的记录
         LambdaQueryWrapper<DailyAwardRecordsDO> wrapper = new LambdaQueryWrapper<DailyAwardRecordsDO>()
-                .eq(DailyAwardRecordsDO::getSettled, false)
-                .apply("date(create_time) <= DATE_SUB(now(), INTERVAL 7 DAY)");
+                .eq(DailyAwardRecordsDO::getSettled, false);
+                //时间条件暂时关闭,因为现在的计算程序改为即算即用.后续如若改回,打开注释即可
+//                .apply("date(create_time) <= DATE_SUB(now(), INTERVAL 7 DAY)");
 //                .apply("date(create_time) <= DATE_SUB('2024-06-21', INTERVAL 7 DAY)");
 
         List<DailyAwardRecordsDO> dailyAwardRecordsSaveReqVOS = dailyAwardRecordsMapper.selectList(wrapper);
@@ -130,6 +132,7 @@ public class DailyAwardRecordsServiceImpl implements DailyAwardRecordsService {
                     .freezeAmount(-dailyAwardRecordsDO.getAmount())
                     .afterFreezeAmount(integralDO.getFreezeQuota())
                     .generateUserId(dailyAwardRecordsDO.getGenerateUserId())
+                    .accumulatedQuotaAmount(dailyAwardRecordsDO.getAccumulatedQuotaAmount())
                     .build();
             ptProfitLogService.createPtProfitLog(winWinLog);
 

+ 3 - 3
feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/service/ordercalc/OrderCalcServiceImpl.java

@@ -168,7 +168,9 @@ public class OrderCalcServiceImpl implements OrderCalcService {
             OrderPercentageDO orderPercentageDO = orderPercentageService.queryStatus();
             // 当前计算百分比模板
             String percentTemplate = JsonUtils.toJsonString(orderPercentageDO);
-            calc(tradeOrderDO, orderPercentageDO, percentTemplate);
+
+            // 订单完成的七天后计算,分配权益,现在是即算即得所以注释了,后续变动可以打开
+//            calc(tradeOrderDO, orderPercentageDO, percentTemplate);
 
             /*
             计算合赢奖三部曲:
@@ -342,8 +344,6 @@ public class OrderCalcServiceImpl implements OrderCalcService {
 
         // 平台收益日志
         List<PtProfitLogSaveReqVO> ptProfitLogSaveReqVOS = new ArrayList<>();
-        // 修改用户钱包
-        List<IntegralSaveReqVO> integralSaveReqVOS = new ArrayList<>();
 
         // todo 修改用户身价 如何从订单中获取身价?
         BigDecimal oneHundred = new BigDecimal(BigInteger.ONE);

+ 6 - 6
feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/service/partitioncrash/PartitionCrashServiceImpl.java

@@ -297,7 +297,7 @@ public class PartitionCrashServiceImpl implements PartitionCrashService {
                             integralDO.getFreezeQuota(), JsonUtils.toJsonString(orderPercentageDO), ptReplenish, -amount, highQuota - amount);
 
                     //记录每日用户有合赢将的记录,后面用来结算的
-                    dailyAwardRecordsService.addDailyAwardRecords(integralDO.getUserId(),integralDO.getUserId(),amount, logId);
+                    dailyAwardRecordsService.addDailyAwardRecords(integralDO.getUserId(),integralDO.getUserId(),amount, logId,ptReplenish);
                 }
 
                 //直推人的上级的本次添加积分
@@ -327,7 +327,7 @@ public class PartitionCrashServiceImpl implements PartitionCrashService {
                             parentIntegralDo.getFreezeQuota(), JsonUtils.toJsonString(orderPercentageDO), parentPtReplenish, -parentAmount, parentHighQuota - parentAmount);
 
                     //记录每日用户有合赢将的记录,后面用来结算的
-                    dailyAwardRecordsService.addDailyAwardRecords(parentIntegralDo.getUserId(),integralDO.getUserId(),parentAmount,logId);
+                    dailyAwardRecordsService.addDailyAwardRecords(parentIntegralDo.getUserId(),integralDO.getUserId(),parentAmount,logId,parentPtReplenish);
                 }
 
                 if (!ptReplenish.equals(0L)) {
@@ -400,7 +400,7 @@ public class PartitionCrashServiceImpl implements PartitionCrashService {
                 //amount用于记录本次添加的值
                 Long amount = 0L;
                 //平台需要补回的收益
-                Long ptReplenish = entry.getValue();
+//                Long ptReplenish = entry.getValue();
 
                 //如果直推人的可获取额度大于0,则能够获取
                 if (highQuota.compareTo(0L) > 0) {
@@ -408,12 +408,12 @@ public class PartitionCrashServiceImpl implements PartitionCrashService {
                         //如果最大额度 > 直推人可获取额度,则直接获取
                         amount = entry.getValue();
                         //平台没有需要补回的收益
-                        ptReplenish = 0L;
+//                        ptReplenish = 0L;
                     } else {
                         //如果最大额度 < 直推人可获取额度,则获取自己的最大额度
                         amount = highQuota;
                         //用户本次没有拿到的直推奖
-                        ptReplenish = entry.getValue() - highQuota;
+//                        ptReplenish = entry.getValue() - highQuota;
                     }
 
                     integralService.updateIntegral(BeanUtils.toBean(integralDO, IntegralSaveReqVO.class));
@@ -541,7 +541,7 @@ public class PartitionCrashServiceImpl implements PartitionCrashService {
     public PartitionCrashDO crashYesterdayNo(Long userId,Integer yesterday) {
         LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT); // 今天的开始时间,即午夜12点
         LocalDateTime localDateTime = todayStart.minusDays(yesterday);
-        String yesterdayString = "CRASH:" + userId + ":" + localDateTime.toString();
+        String yesterdayString = "CRASH:" + userId + ":" + localDateTime;
 
         PartitionCrashDO partitionCrashDO = partitionCrashMapper.selectOne(new LambdaQueryWrapperX<PartitionCrashDO>().eqIfPresent(PartitionCrashDO::getPartNo, yesterdayString));
         List<PartitionCrashDO> partitionCrashDOS = partitionCrashMapper.selectList((new LambdaQueryWrapperX<PartitionCrashDO>()

+ 8 - 10
feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/service/sharepath/SharePathService.java

@@ -17,8 +17,6 @@ import cn.newfeifan.mall.module.member.controller.admin.user.vo.MemberUserRespVO
  */
 public interface SharePathService {
 
-    Long createSharePath(SharePathSaveReqVO createReqVO);
-
     void createSharePathByUserId(SharePathSaveReqVO createReqVO);
 
     void createSharePathByUserId(List<Long> userIds);
@@ -60,7 +58,7 @@ public interface SharePathService {
     /**
      * 转让分销用户关系
      *
-     * @param sharePathtransFerenceRelaReqVO
+     * @param sharePathtransFerenceRelaReqVO 转让关系
      */
     void transferenceRela(SharePathtransFerenceRelaReqVO sharePathtransFerenceRelaReqVO);
 
@@ -68,8 +66,8 @@ public interface SharePathService {
     /**
      * 通过直推人, 获取当前的直推关系
      *
-     * @param userId
-     * @return
+     * @param userId 用户id
+     * @return 推荐人
      */
     SharePathDO getSharePathByDescendant(Long userId);
 
@@ -77,17 +75,17 @@ public interface SharePathService {
     /**
      * 获取儿子订单计算分区
      *
-     * @param userId
-     * @return
+     * @param userId 用户id
+     * @return 订单计算
      */
     List<OrderCalcDO> selectSons(Long userId, List<Long> userIds);
 
     /**
      * 获取兄弟订单分区
      *
-     * @param userId
-     * @param userId1
-     * @return
+     * @param userId 用户id
+     * @param userId1 ids
+     * @return 订单计算
      */
     List<OrderCalcDO> selectBrothers(Long userId, List<Long> userId1);
 

+ 335 - 0
feifan-module-mall/feifan-module-trade-biz/src/main/java/cn/newfeifan/mall/module/trade/service/order/TradeOrderUpdateServiceImpl.java

@@ -12,10 +12,21 @@ import cn.newfeifan.mall.framework.common.enums.UserTypeEnum;
 import cn.newfeifan.mall.framework.common.util.json.JsonUtils;
 import cn.newfeifan.mall.framework.common.util.number.MoneyUtils;
 import cn.newfeifan.mall.module.distri.constant.DistriConstants;
+import cn.newfeifan.mall.module.distri.controller.admin.integral.vo.IntegralSaveReqVO;
+import cn.newfeifan.mall.module.distri.controller.admin.ptprofit.vo.PtProfitSaveReqVO;
+import cn.newfeifan.mall.module.distri.controller.admin.ptprofitlog.vo.PtProfitLogSaveReqVO;
+import cn.newfeifan.mall.module.distri.dal.dataobject.integral.IntegralDO;
 import cn.newfeifan.mall.module.distri.dal.dataobject.orderpercentage.OrderPercentageDO;
+import cn.newfeifan.mall.module.distri.dal.dataobject.ptprofit.PtProfitDO;
+import cn.newfeifan.mall.module.distri.dal.dataobject.sharepath.SharePathDO;
 import cn.newfeifan.mall.module.distri.dal.mysql.orderpercentage.OrderPercentageMapper;
 import cn.newfeifan.mall.module.distri.enums.CaclEnum;
+import cn.newfeifan.mall.module.distri.enums.SocialStatusEnum;
+import cn.newfeifan.mall.module.distri.service.duser.DuserService;
 import cn.newfeifan.mall.module.distri.service.integral.IntegralService;
+import cn.newfeifan.mall.module.distri.service.ptprofit.PtProfitService;
+import cn.newfeifan.mall.module.distri.service.ptprofitlog.PtProfitLogService;
+import cn.newfeifan.mall.module.distri.service.sharepath.SharePathService;
 import cn.newfeifan.mall.module.member.api.address.MemberAddressApi;
 import cn.newfeifan.mall.module.member.api.address.dto.MemberAddressRespDTO;
 import cn.newfeifan.mall.module.member.service.user.MemberUserService;
@@ -74,11 +85,13 @@ import com.kuaidi100.sdk.request.SubscribeReq;
 import lombok.extern.slf4j.Slf4j;
 import org.jetbrains.annotations.NotNull;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
 import java.math.BigDecimal;
+import java.math.BigInteger;
 import java.math.RoundingMode;
 import java.text.DecimalFormat;
 import java.time.LocalDateTime;
@@ -151,6 +164,16 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
 
     @Resource
     private AdminUserService adminUserService;
+    @Resource
+    private PtProfitService ptProfitService;
+    @Resource
+    private PtProfitLogService ptProfitLogService;
+    @Resource
+    @Lazy
+    private SharePathService sharePathService;
+    @Resource
+    @Lazy
+    private DuserService duserService;
 
     // =================== Order ===================
 
@@ -1213,6 +1236,318 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
             // 发送给微信消息给系统用户待发货消息
             sentWcChatMessage(order);
         }
+
+        // 七天获得变为即算即得,后续如果恢复把这里注释即可,并打开平台定时任务得订单结算
+        calc(TradeOrderDOList, orderPercentageDO, JsonUtils.toJsonString(orderPercentageDO));
+    }
+
+    /**
+     * 计算订单支付成功后,订单项的冻结积分转换
+     * @param tradeOrderDOList 订单列表
+     * @param orderPercentageDO 参数百分比
+     * @param percentTemplate 计算百分比模板
+     */
+    private void calc(List<TradeOrderDO> tradeOrderDOList, OrderPercentageDO orderPercentageDO, String percentTemplate) {
+        // 平台收益日志
+        List<PtProfitLogSaveReqVO> ptProfitLogSaveReqVOS = new ArrayList<>();
+
+        // todo 修改用户身价 如何从订单中获取身价?
+        BigDecimal oneHundred = new BigDecimal(BigInteger.ONE);
+
+
+        // 计算,毛利中100 之外的百分比
+        BigDecimal otherOneHundred = oneHundred.subtract(new BigDecimal(orderPercentageDO.getGrossProfitUserQuotaPerc()))
+                .subtract(new BigDecimal(orderPercentageDO.getGrossProfitAncestorQuotaPerc()))
+                .subtract(new BigDecimal(orderPercentageDO.getGrossProfitPlatformQuotaPerc()));
+
+
+        tradeOrderDOList.forEach(k -> {
+            PtProfitDO ptProfitDO = ptProfitService.getPtProfit();
+            // 平台收益
+            List<PtProfitSaveReqVO> ptProfitSaveReqVOS = new ArrayList<>();
+
+            List<TradeOrderItemDO> tradeOrderItemDOS = tradeOrderItemMapper.selectListByOrderId(k.getId());
+
+            // 计算
+            // 计算利润: (价格 - 成本价格) * 产品数量
+            //用户自得和直推奖不用计算,从orderItem里面获取
+            final BigDecimal[] profit = {new BigDecimal("0"), new BigDecimal("0"), new BigDecimal("0"), new BigDecimal("0")};
+            tradeOrderItemDOS.forEach(j -> {
+                BigDecimal onePrice = new BigDecimal(j.getPrice());
+                BigDecimal oneCostPrice = new BigDecimal(j.getCostPrice());
+                BigDecimal productCount = new BigDecimal(j.getCount());
+                BigDecimal oneGrossProfit = onePrice.subtract(oneCostPrice).multiply(productCount);
+                profit[0] = profit[0].add(oneGrossProfit);  // 注意这里的改动
+                profit[1] = profit[1].add(new BigDecimal(j.getIncreaseIntegral()));     //自得积分
+                profit[2] = profit[2].add(new BigDecimal(j.getAncestorIncreaseIntegral()));     //直推奖
+                profit[3] = profit[3].add(new BigDecimal(j.getFreezeHighQuota()));     //用户获得冻结最大可用额度
+
+                // 遍历订单项拿到订单项编号
+                //将当前订单项设置为已分配权益
+                TradeOrderItemDO item = new TradeOrderItemDO();
+                item.setId(j.getId());
+                item.setDistributeBenefit(true);
+                tradeOrderItemMapper.updateById(item);
+            });
+
+
+            // 计算毛利: 利润 * 1
+            BigDecimal grossProfit = profit[0].multiply(new BigDecimal(orderPercentageDO.getGrossProfitPerc())).setScale(4, RoundingMode.DOWN);
+
+            // 计算平台收益
+            BigDecimal grossProfitAfterBonus = profit[0].multiply(oneHundred.subtract(new BigDecimal(orderPercentageDO.getGrossProfitPerc()))).setScale(4, RoundingMode.DOWN);
+
+            if (otherOneHundred.compareTo(new BigDecimal(BigInteger.ZERO)) > 0) {
+                grossProfitAfterBonus = grossProfitAfterBonus.add(grossProfit.multiply(otherOneHundred).setScale(4, RoundingMode.DOWN));
+            }
+
+            // 用户所得: 毛利 * 0.35
+            BigDecimal descendantQuota = profit[1];
+
+            // 直推奖: 毛利 * 0.3 (上一级)
+            BigDecimal ancestorQuota = profit[2];
+
+            // 平台服务费: 毛利 * 0.05
+            BigDecimal platformQuota = grossProfit.multiply(new BigDecimal(orderPercentageDO.getGrossProfitPlatformQuotaPerc())).setScale(4, RoundingMode.DOWN);
+
+
+            // 计算当前下单人的毛利的十倍
+//            BigDecimal highQuota = grossProfit.multiply(new BigDecimal("10")).setScale(4, RoundingMode.DOWN);
+            BigDecimal highQuota = profit[3];
+
+            // 乘以 10000 并转换为整数
+            Long ancestorQuotaInt = ancestorQuota.longValue();
+            Long descendantQuotaInt = descendantQuota.longValue();
+            Long highQuotaInt = highQuota.longValue();
+            Long grossProfitAfterBonusInt = grossProfitAfterBonus.multiply(new BigDecimal("10000")).setScale(0, RoundingMode.DOWN).longValue();
+            Long platformQuotaInt = platformQuota.multiply(new BigDecimal("10000")).setScale(0, RoundingMode.DOWN).longValue();
+
+
+            // 获取当前下单人关系
+            SharePathDO sharePath = sharePathService.getSharePathByDescendant(k.getUserId());
+            // 如果sharePath为空, 则说明没有直推人关系
+            IntegralDO integralDOAncestor = integralService.selectByUser(sharePath.getAncestor());      //父级
+            IntegralDO integralDO = integralService.selectByUser(sharePath.getDescendant());            //自己
+            IntegralDO ptIntegral = integralService.selectByUser(1L);                         //pt
+
+
+            // ========== 身价计算 ==============
+            //推荐人身价计算
+            double orderAncestorSocialStatus = Double.parseDouble(orderPercentageDO.getOrderAncestorSocialStatus());
+            double ancestorSocial = k.getTotalPrice() * orderAncestorSocialStatus / 100;
+            duserService.updateDuserSocial(integralDOAncestor.getUserId(), (long) ancestorSocial, SocialStatusEnum.SHOPPING_ANCESTOR_SOCIAL.getStatus());
+
+            //自得身价计算
+            double orderUserSocialStatus = Double.parseDouble(orderPercentageDO.getOrderUserSocialStatus());
+            double descendantSocial = k.getTotalPrice() * orderUserSocialStatus / 100;
+            duserService.updateDuserSocial(integralDO.getUserId(), (long) descendantSocial, SocialStatusEnum.SHOPPING_SOCIAL.getStatus());
+
+            /*
+            原本这里有添加到distri_order_calc表的记录,但是现在要合赢奖和(直推奖、自得奖)分开,所以现在搬走了
+             */
+
+            // ========== 修改用户钱包 =================
+
+            //直推奖也要限制额度
+            Long ancestorHighQuota = integralDOAncestor.getHighQuota() + new BigDecimal(orderPercentageDO.getBaseMaxQuota()).longValue();//加上基础的最大额度值
+
+            //amount用于记录本次添加的值
+            Long amount = 0L;
+            //平台需要补回的收益
+            Long ptReplenish = ancestorQuotaInt;
+
+            //如果直推人的可获取额度大于0,则能够获取
+            if (ancestorHighQuota.compareTo(0L) > 0) {
+                if (ancestorHighQuota.compareTo(ancestorQuotaInt) > 0) {
+                    //如果最大额度 > 直推人可获取额度,则直接获取
+                    amount = ancestorQuotaInt;
+                    //平台没有需要补回的收益
+                    ptReplenish = 0L;
+                } else {
+                    //如果最大额度 < 直推人可获取额度,则获取自己的最大额度
+                    amount = ancestorHighQuota;
+                    //用户本次没有拿到的直推奖
+                    ptReplenish = ancestorQuotaInt - ancestorHighQuota;
+                }
+            }
+            changeUserWallet(integralDO, integralDOAncestor, ptIntegral, amount,
+                    descendantQuotaInt, highQuotaInt, grossProfitAfterBonusInt, platformQuotaInt, ptReplenish);
+
+            // ========== 修改平台信息 =================
+            // 每个订单计算的过程
+            // 增加平台收益
+            // 平台服务费
+            PtProfitSaveReqVO ptProfitSaveReqVO =
+                    PtProfitSaveReqVO.builder()
+                            .ptAdd(platformQuotaInt)
+                            .ptGrossAdd(grossProfitAfterBonusInt)
+                            .ptTotalAdd(platformQuotaInt + grossProfitAfterBonusInt).build();
+
+
+            // 当直推人获取的积分额度不足时,平台收益要加回来,并生成log
+            if (ptReplenish.compareTo(0L) > 0) {
+                calcIntegral(ptProfitSaveReqVOS, ptProfitSaveReqVO, ptReplenish,
+                        k.getId(), k.getNo());
+            }
+
+            ptProfitSaveReqVOS.add(ptProfitSaveReqVO);
+            // ========== 增加日志记录 =================
+
+            // 平台记录
+            // 平台总收益
+            // 平台总收益
+            PtProfitLogSaveReqVO ptTotal = PtProfitLogSaveReqVO.builder()
+                    .orderId(k.getId())
+                    .profitStatus(CaclEnum.PLATFORM_TOTAL_ADD.getType())
+                    .orderNo(k.getNo())
+                    .amount(platformQuotaInt + grossProfitAfterBonusInt)
+                    .afterAmount(ptProfitDO.getPtTotalAdd() + platformQuotaInt + grossProfitAfterBonusInt)
+                    .percentTemplate(percentTemplate)
+                    .generateUserId(integralDO.getUserId())
+                    .build();
+            // 平台收益
+            PtProfitLogSaveReqVO ptGrossAddLog = PtProfitLogSaveReqVO.builder()
+                    .orderId(k.getId())
+                    .profitStatus(CaclEnum.PLATFORM_REVENUE.getType())
+                    .orderNo(k.getNo())
+                    .amount(grossProfitAfterBonusInt)
+                    .afterAmount(ptProfitDO.getPtGrossAdd() + ptProfitSaveReqVO.getPtGrossAdd())
+                    .percentTemplate(percentTemplate)
+                    .generateUserId(integralDO.getUserId())
+                    .build();
+
+            // 平台服务费
+            PtProfitLogSaveReqVO ptAddLog = PtProfitLogSaveReqVO.builder()
+                    .orderId(k.getId())
+                    .profitStatus(CaclEnum.PLATFORM_SERVICE_FEE.getType())
+                    .orderNo(k.getNo())
+                    .amount(platformQuotaInt)
+                    .afterAmount(ptProfitDO.getPtAdd() + ptProfitSaveReqVO.getPtAdd())
+                    .percentTemplate(percentTemplate)
+                    .generateUserId(integralDO.getUserId())
+                    .build();
+
+
+            // 直推奖记录
+            PtProfitLogSaveReqVO tjrLog = PtProfitLogSaveReqVO.builder()
+                    .orderId(k.getId())
+                    .profitStatus(CaclEnum.RECOMMENDED_PERSON_QUOTA.getType())
+                    .orderNo(k.getNo())
+                    .userId(sharePath.getAncestor())
+                    .amount(amount)
+                    .afterAmount(integralDOAncestor.getCurrentQuota())
+                    .freezeAmount(-amount)
+                    .afterFreezeAmount(integralDOAncestor.getFreezeQuota())
+                    .ancestorQuotaAmount(ptReplenish)
+                    .maxAvailablePointsAmount(-amount)
+                    .afterMaxAvailablePointsAmount(ancestorHighQuota - amount)
+                    .percentTemplate(percentTemplate)
+                    .generateUserId(integralDO.getUserId())
+                    .build();
+
+
+            // 用户自得记录 + 户冻结积分转化成可用 + 扣减最高可用额度记录
+            PtProfitLogSaveReqVO ztrLog = PtProfitLogSaveReqVO.builder()
+                    .orderId(k.getId())
+                    .profitStatus(CaclEnum.DIRECT_REFERRAL_QUOTA.getType())
+                    .orderNo(k.getNo())
+                    .userId(sharePath.getDescendant())
+                    .amount(descendantQuotaInt)
+                    .afterAmount(integralDO.getCurrentQuota())
+                    .freezeAmount(-descendantQuotaInt)
+                    .afterFreezeAmount(integralDO.getFreezeQuota())
+                    .maxAvailablePointsAmount(highQuotaInt)
+                    .afterMaxAvailablePointsAmount(integralDO.getHighQuota() + new BigDecimal(orderPercentageDO.getBaseMaxQuota()).longValue())
+                    .freezeHighQuota(-highQuotaInt)
+                    .afterFreezeHighQuota(integralDO.getFreezeHighQuota())
+                    .percentTemplate(percentTemplate)
+                    .generateUserId(integralDO.getUserId())
+                    .build();
+
+            ptProfitLogSaveReqVOS.add(ptTotal);
+            ptProfitLogSaveReqVOS.add(ptGrossAddLog);
+            ptProfitLogSaveReqVOS.add(ptAddLog);
+            ptProfitLogSaveReqVOS.add(tjrLog);
+            ptProfitLogSaveReqVOS.add(ztrLog);
+
+
+            PtProfitDO ptProfit = ptProfitService.getPtProfit();
+            // 计算总积分
+            Long total = ptProfitSaveReqVOS.stream().mapToLong(PtProfitSaveReqVO::getPtTotalAdd).sum();
+            Long add = ptProfitSaveReqVOS.stream().mapToLong(PtProfitSaveReqVO::getPtAdd).sum();
+            Long grossAdd = ptProfitSaveReqVOS.stream().mapToLong(PtProfitSaveReqVO::getPtGrossAdd).sum();
+            ptProfit.setPtAdd(ptProfit.getPtAdd() + add);
+            ptProfit.setPtTotalAdd(ptProfit.getPtTotalAdd() + total);
+            ptProfit.setPtGrossAdd(ptProfit.getPtGrossAdd() + grossAdd);
+            PtProfitSaveReqVO ptProfitSaveReqVO2 = PtProfitSaveReqVO.builder()
+                    .id(ptProfitDO.getId())
+                    .ptTotalAdd(ptProfit.getPtTotalAdd())
+                    .ptAdd(ptProfit.getPtAdd())
+                    .ptGrossAdd(ptProfit.getPtGrossAdd()).build();
+            ptProfitService.updatePtProfit(ptProfitSaveReqVO2);
+        });
+
+        // 信息整合
+        ptProfitLogService.saveBatch(ptProfitLogSaveReqVOS);
+    }
+
+    /**
+     * 在用户获取直推奖的时候溢出的需要加入到平台,并记录log
+     */
+    private void calcIntegral(List<PtProfitSaveReqVO> ptProfitSaveReqVOS, PtProfitSaveReqVO ptProfitSaveReqVO
+            , Long ancestorQuotaInt, Long orderId, String orderNo) {
+        ptProfitSaveReqVO.setPtGrossAdd(ptProfitSaveReqVO.getPtGrossAdd() + ancestorQuotaInt);
+        ptProfitSaveReqVO.setPtTotalAdd(ptProfitSaveReqVO.getPtGrossAdd() + ptProfitSaveReqVO.getPtAdd());
+
+        PtProfitLogSaveReqVO ptProfitLog = PtProfitLogSaveReqVO.builder()
+                .orderId(orderId)
+                .profitStatus(CaclEnum.GROSS_PROFIT_ANCESTOR_QUOTA_PERC_EXCEED_MAXIMUM_LIMIT.getType())
+                .orderNo(orderNo)
+                .amount(ancestorQuotaInt)
+                .afterAmount(ptProfitSaveReqVOS.stream().mapToLong(PtProfitSaveReqVO::getPtGrossAdd).sum() + ancestorQuotaInt)
+                .build();
+        ptProfitLogService.createPtProfitLog(ptProfitLog);
+    }
+
+    /**
+     * 修改用户钱包
+     *
+     * @param integralDO         当前用户钱包
+     * @param integralDOAncestor 推荐人钱包
+     * @param pt                 pt钱包
+     * @param ancestorQuota      推荐人额度
+     * @param descendantQuota    直推人额度
+     * @param highQuota          最大可用额度
+     * @param ptA                pt收益
+     * @param ptB                pt服务费
+     * @param ptReplenish        超出没有获取到的积分
+     */
+    private void changeUserWallet(IntegralDO integralDO, IntegralDO integralDOAncestor, IntegralDO pt,
+                                  Long ancestorQuota, Long descendantQuota, Long highQuota, Long ptA, Long ptB, Long ptReplenish) {
+        // 修改用户钱包
+        // 增加直推人额度
+        integralDO.setHighQuota(integralDO.getHighQuota() + highQuota);
+        //增加累计峰值
+        integralDO.setHighQuotaTotal(integralDO.getHighQuotaTotal() + highQuota);
+        integralDO.setFreezeHighQuota(integralDO.getFreezeHighQuota() - highQuota);
+        // todo 增加累计额度
+
+        integralDO.setCurrentQuota(integralDO.getCurrentQuota() + descendantQuota);
+        integralDO.setFreezeQuota(integralDO.getFreezeQuota() - descendantQuota);
+        // 增加推荐人额度
+        integralDOAncestor.setCurrentQuota(integralDOAncestor.getCurrentQuota() + ancestorQuota);
+        // 减少推荐人的可获取额度
+        integralDOAncestor.setHighQuota(integralDOAncestor.getHighQuota() - ancestorQuota);
+        integralDOAncestor.setAncestorQuota(integralDOAncestor.getAncestorQuota() + ancestorQuota);
+        integralDOAncestor.setFreezeQuota(integralDOAncestor.getFreezeQuota() - ancestorQuota - ptReplenish);
+        integralService.updateIntegral(cn.newfeifan.mall.framework.common.util.object.BeanUtils.toBean(integralDOAncestor, IntegralSaveReqVO.class));
+
+        // 增加平台额度
+        pt.setCurrentQuota(pt.getCurrentQuota() + ptA + ptB);
+
+        integralService.updateIntegral(cn.newfeifan.mall.framework.common.util.object.BeanUtils.toBean(integralDO, IntegralSaveReqVO.class));
+        integralService.updateIntegral(cn.newfeifan.mall.framework.common.util.object.BeanUtils.toBean(pt, IntegralSaveReqVO.class));
+
     }
 
     private void sentWcChatMessage(TradeOrderDO order) {