Browse Source

支付生成直推人积分

Ben 5 months ago
parent
commit
be9b85a198
12 changed files with 89 additions and 18 deletions
  1. 1 0
      feifan-framework/feifan-spring-boot-starter-mybatis/src/main/java/cn/newfeifan/mall/framework/mybatis/core/mapper/BaseMapperX.java
  2. 2 1
      feifan-module-distri/feifan-module-distri-api/src/main/java/cn/newfeifan/mall/module/distri/enums/CaclEnum.java
  3. 8 1
      feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/controller/app/ptprofitlog/vo/PtProfitLogSaveReqVO.java
  4. 7 1
      feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/dal/dataobject/ptprofitlog/PtProfitLogDO.java
  5. 3 1
      feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/service/integral/IntegralService.java
  6. 5 2
      feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/service/integral/IntegralServiceImpl.java
  7. 3 1
      feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/service/ptprofitlog/PtProfitLogService.java
  8. 4 1
      feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/service/ptprofitlog/PtProfitLogServiceImpl.java
  9. 6 4
      feifan-module-mall/feifan-module-trade-biz/src/main/java/cn/newfeifan/mall/module/trade/controller/app/order/AppTradeOrderController.java
  10. 3 0
      feifan-module-mall/feifan-module-trade-biz/src/main/java/cn/newfeifan/mall/module/trade/dal/dataobject/order/TradeOrderItemDO.java
  11. 6 0
      feifan-module-mall/feifan-module-trade-biz/src/main/java/cn/newfeifan/mall/module/trade/dal/mysql/order/TradeOrderMapper.java
  12. 41 6
      feifan-module-mall/feifan-module-trade-biz/src/main/java/cn/newfeifan/mall/module/trade/service/order/TradeOrderUpdateServiceImpl.java

+ 1 - 0
feifan-framework/feifan-spring-boot-starter-mybatis/src/main/java/cn/newfeifan/mall/framework/mybatis/core/mapper/BaseMapperX.java

@@ -17,6 +17,7 @@ import com.github.yulichang.base.MPJBaseMapper;
 import com.github.yulichang.interfaces.MPJBaseJoin;
 import com.github.yulichang.wrapper.MPJLambdaWrapper;
 import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
 
 import java.util.Collection;
 import java.util.List;

+ 2 - 1
feifan-module-distri/feifan-module-distri-api/src/main/java/cn/newfeifan/mall/module/distri/enums/CaclEnum.java

@@ -23,7 +23,8 @@ public enum CaclEnum {
     GROSS_PROFIT_BONUS_QUOTA_PERC_EXCEED_MAXIMUM_LIMIT(14, "合赢奖超额"),
     GROSS_PROFIT_ANCESTOR_QUOTA_PERC_EXCEED_MAXIMUM_LIMIT(15, "直推奖超额"),
     ORDER_CANCEL_BY_USER_REFUND_INTEGRAL(16,"取消订单,退回积分"),
-    ORDER_CANCEL_BY_SYSTEM_REFUND_INTEGRAL(17,"订单超时未支付,退回积分")
+    ORDER_CANCEL_BY_SYSTEM_REFUND_INTEGRAL(17,"订单超时未支付,退回积分"),
+    ORDER_PAY_INTEGRAL_ANCESTER(18, "直推人购物获得冻结积分")//上级直推人 购物 获得积分
     ;
 
     /**

+ 8 - 1
feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/controller/app/ptprofitlog/vo/PtProfitLogSaveReqVO.java

@@ -16,7 +16,14 @@ public class PtProfitLogSaveReqVO {
     @Schema(description = "收益类型 1:平台服务器费 2:平台收益 3用户下单返回毛利额度 4直推人额度 5合赢奖", example = "2")
     private Integer profitStatus;
 
-    @Schema(description = "用户ID , 只有收益类型为 3,4才有值", example = "6982")
+
+    /**
+     * 产生积分的用户ID,例如:下订单的用户ID
+     */
+    @Schema(description = "产生积分的用户ID", example = "6982")
+    private Long generateUserId;
+
+    @Schema(description = "积分变动的用户ID", example = "6982")
     private Long userId;
 
     @Schema(description = "订单Id", example = "26464")

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

@@ -24,9 +24,15 @@ public class PtProfitLogDO extends BaseDO {
      */
     private Integer profitStatus;
     /**
-     * 用户ID , 只有收益类型为 3,4才有值
+     * 实际变动积分的用户ID , 只有收益类型为 3,4才有值
      */
     private Long userId;
+
+    /**
+     * 产生积分的用户ID,例如:下订单的用户ID
+     */
+    private Long generateUserId;
+
     /**
      * 订单Id
      */

+ 3 - 1
feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/service/integral/IntegralService.java

@@ -65,13 +65,15 @@ public interface IntegralService {
     /**
      * 修改用户积分
      *
+     * @param generateUserId 导致产生积分变动的用户ID
      * @param userId 要修改积分的用户
      * @param caclEnum 导致积分变化的事件类型枚举
      * @param amount 用户积分变化金额,可为负数
      * @param freezeAmount 用户冻结积分变化金额,可为负数
      * @return 返回是否执行成功。执行失败,通常是就是用户积分不足,没积分抵扣
      */
-    Boolean updateUserIntegral(Long userId, CaclEnum caclEnum, Long amount, Long freezeAmount,
+    Boolean updateUserIntegral(Long generateUserId,//导致产生积分变动的用户ID
+            Long userId, CaclEnum caclEnum, Long amount, Long freezeAmount,
                                Long tradeOrderId,
                                String orderNum
                                );

+ 5 - 2
feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/service/integral/IntegralServiceImpl.java

@@ -98,6 +98,7 @@ public class IntegralServiceImpl implements IntegralService {
      * add by Ben
      * 修改用户积分
      *
+     * @param generateUserId 要修改积分的用户
      * @param userId 要修改积分的用户
      * @param caclEnum 导致积分变化的事件类型枚举
      * @param amount 用户可用积分变化金额,可为负数
@@ -105,7 +106,8 @@ public class IntegralServiceImpl implements IntegralService {
      * @return 返回是否执行成功。执行失败,通常是就是用户积分不足,没积分抵扣
      */
     @Override
-    public Boolean updateUserIntegral(Long userId, CaclEnum caclEnum, Long amount, Long freezeAmount,
+    public Boolean updateUserIntegral(Long generateUserId,//导致产生积分变动的用户ID
+            Long userId, CaclEnum caclEnum, Long amount, Long freezeAmount,
                                       Long tradeOrderId,
                                       String OrderNum
                                       ) {
@@ -135,7 +137,8 @@ public class IntegralServiceImpl implements IntegralService {
             }
 
             //记录用户的积分变动日志
-            ptProfitLogService.addMessage(userId, caclEnum, amount, afterAmount,
+            ptProfitLogService.addMessage(generateUserId,//导致产生积分变动的用户ID
+                    userId, caclEnum, amount, afterAmount,
                     freezeAmount, afterFreezeAmount,null,tradeOrderId,OrderNum);
 
         }else if(effectRowNum<1){//如果钱包积分不足,不够抵扣

+ 3 - 1
feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/service/ptprofitlog/PtProfitLogService.java

@@ -58,7 +58,9 @@ public interface PtProfitLogService {
     void saveBatch(List<PtProfitLogSaveReqVO> ptProfitLogSaveReqVOS);
 
     //记录用户的积分变动日志
-    void addMessage(Long userId, CaclEnum caclEnum, Long amount,
+    void addMessage(Long generateUserId,//导致产生积分变动的用户ID
+                    Long userId, //实际变动积分的用户ID
+                    CaclEnum caclEnum, Long amount,
                     Long afterAmount, Long freezeAmount,
                     Long afterFreezeAmount, String percentTemplate,
                     Long tradeOrderId,

+ 4 - 1
feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/service/ptprofitlog/PtProfitLogServiceImpl.java

@@ -110,7 +110,9 @@ public class PtProfitLogServiceImpl implements PtProfitLogService {
      * @param OrderNum
      */
     @Override
-    public void addMessage(Long userId, CaclEnum caclEnum, Long amount, Long afterAmount,
+    public void addMessage(Long generateUserId,//导致产生积分变动的用户ID
+                           Long userId, //实际变动积分的用户ID
+                           CaclEnum caclEnum, Long amount, Long afterAmount,
                            Long freezeAmount, Long afterFreezeAmount,
                            String percentTemplate,
                            Long tradeOrderId,
@@ -121,6 +123,7 @@ public class PtProfitLogServiceImpl implements PtProfitLogService {
                 .amount(amount)
                 .percentTemplate(percentTemplate)
                 .userId(userId)
+                .generateUserId(generateUserId)
                 .profitStatus(caclEnum.getType()).orderId(tradeOrderId).orderNo(OrderNum)
                 .freezeAmount(freezeAmount).afterFreezeAmount(afterFreezeAmount)
                 .build();

+ 6 - 4
feifan-module-mall/feifan-module-trade-biz/src/main/java/cn/newfeifan/mall/module/trade/controller/app/order/AppTradeOrderController.java

@@ -78,11 +78,13 @@ public class AppTradeOrderController {
 
         CommonResult<PayOrderRespVO> success = success(PayOrderConvert.INSTANCE.convert(newPayOrderDO));
 
+        boolean test = true;
+        if(true)
         //如果在上一行代码,把支付订单状态从“待支付”,改为“已支付”,则证明订单支付成功,则改trade_order表状态为待发货,并且分配积分
-        if(oldPayOrderDO.getStatus().equals(PayOrderStatusEnum.WAITING.getStatus())
-                &&
-                newPayOrderDO.getStatus().equals(PayOrderStatusEnum.SUCCESS.getStatus())
-        )
+//        if(oldPayOrderDO.getStatus().equals(PayOrderStatusEnum.WAITING.getStatus())
+//                &&
+//                newPayOrderDO.getStatus().equals(PayOrderStatusEnum.SUCCESS.getStatus())
+//        )
         {
             //只有支付订单id参数payOrderId是有用的。
             List list = tradeOrderUpdateService.updateOrderPaid(id);

+ 3 - 0
feifan-module-mall/feifan-module-trade-biz/src/main/java/cn/newfeifan/mall/module/trade/dal/dataobject/order/TradeOrderItemDO.java

@@ -38,6 +38,9 @@ public class TradeOrderItemDO extends BaseDO {
     //用户获得积分 add by Ben
     private Long increaseIntegral;
 
+    //推荐用户获得积分 add by Ben 20240604
+    private Long ancestorIncreaseIntegral;
+
     // ========== 订单项基本信息 ==========
     /**
      * 编号

+ 6 - 0
feifan-module-mall/feifan-module-trade-biz/src/main/java/cn/newfeifan/mall/module/trade/dal/mysql/order/TradeOrderMapper.java

@@ -9,6 +9,8 @@ import cn.newfeifan.mall.module.trade.controller.app.order.vo.AppTradeOrderPageR
 import cn.newfeifan.mall.module.trade.dal.dataobject.order.TradeOrderDO;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
 
 import java.time.LocalDateTime;
 import java.util.List;
@@ -18,6 +20,10 @@ import java.util.Set;
 @Mapper
 public interface TradeOrderMapper extends BaseMapperX<TradeOrderDO> {
 
+    //add by Ben 20240604
+    @Select("SELECT ancestor FROM `distri_share_path` where descendant = #{userId} and depth=1")
+    Long selectAncesterByMemberUserId(@Param("userId") Long userId);
+
     default int updateByIdAndStatus(Long id, Integer status, TradeOrderDO update) {
         return update(update, new LambdaUpdateWrapper<TradeOrderDO>()
                 .eq(TradeOrderDO::getId, id).eq(TradeOrderDO::getStatus, status));

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

@@ -641,7 +641,7 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
                 Long tradeOrderId = order.getId();
                 String OrderNum = order.getNo();
                 Long payIntegral = order.getPayIntegral();//每个订单使用的积分
-                integralService.updateUserIntegral(userId, CaclEnum.ORDER_PAY_INTEGRAL, payIntegral * (-1), 0L, tradeOrderId, OrderNum);
+                integralService.updateUserIntegral(userId,userId, CaclEnum.ORDER_PAY_INTEGRAL, payIntegral * (-1), 0L, tradeOrderId, OrderNum);
             }
         }
 
@@ -713,20 +713,25 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
         return virtualOrder;
     }
 
+
     /**
-     * 把商品的推广费总额,转换为直推人获得的冻结积分
+     * 把商品的推广费总额,转换为消费者本人(直推人)、或消费者的推荐人 获得的冻结积分
      *
      * @param promotionExpenses 商品的推广费总额
+     * @param grossProfitPerc 推荐人/消费者本人额度 百分比
      * @return 输入推广费,对应的直推人获得的冻结积分
      */
     public static Long promotionExpenses2GrossProfitAncestorQuota(
+            /*
+            商品的推广费总额
+             */
             Integer promotionExpenses,
             /*
              * 毛利 = 成本 * 0.38... (推广费用)
              */
             String grossProfitPerc,
             /*
-             * 推广-推荐人额度 百分比
+             * 推广-推荐人/消费者本人额度 百分比
              */
             String grossProfitAncestorQuotaPerc
     ) {
@@ -873,6 +878,7 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
         //查询订单提成比例
         QueryWrapper<OrderPercentageDO> queryWrapper = new QueryWrapper<>();
         OrderPercentageDO orderPercentageDO = orderPercentageMapper.selectOne(queryWrapper);//表中只有一条记录
+
         /*
          * 毛利 = 成本 * 0.38... (推广费用)
          */
@@ -886,9 +892,19 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
          */
         String grossProfitUserQuotaPerc = orderPercentageDO.getGrossProfitUserQuotaPerc();
 
+        /*
+         * 推荐人(上一级) 百分比
+         */
+        String grossProfitAncestorQuotaPerc = orderPercentageDO.getGrossProfitAncestorQuotaPerc();
+
         //add by Ben 根据支付订单id,查询所有 支付订单 对应的 订单
         List<TradeOrderDO> TradeOrderDOList = tradeOrderMapper.selectList("pay_order_id", payOrderId);
 
+        //购物者ID
+        Long memberUserIdOfTradeOrder = TradeOrderDOList.get(0).getUserId();
+        //20240604 推荐人(上级用户ID)
+        Long ancesterUserId = tradeOrderMapper.selectAncesterByMemberUserId(memberUserIdOfTradeOrder);
+
         for (TradeOrderDO tradeOrderDO : TradeOrderDOList) {
 
             Long tradeOrderId = tradeOrderDO.getId();
@@ -943,6 +959,9 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
             }
 
             Long totalFreezeAmount = 0L;//用户总的冻结积分
+
+            Long totalAncestorFreezeAmount = 0L;//推荐人总的冻结积分
+
             for (TradeOrderItemDO tradeOrderItemDO : TradeOrderItemList) {
                 ProductSkuDO productSkuDO = skuDOMap.get(tradeOrderItemDO.getSkuId());
                 //这类商品的总推广费
@@ -956,12 +975,22 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
                                 grossProfitUserQuotaPerc//消费者自得额度 百分比
                         );
 
+
+                //把商品项的推广费,转换为这项商品推荐人(上级)获得的冻结积分
+                Long ancestorItemFreezeAmount =
+                        promotionExpenses2GrossProfitAncestorQuota(promotionExpenses,
+                                grossProfitPerc,//毛利 = 成本 * 0.38... (推广费用)
+                                grossProfitAncestorQuotaPerc//消费者自得额度 百分比
+                        );
+
                 totalFreezeAmount += itemFreezeAmount;
+                totalAncestorFreezeAmount += ancestorItemFreezeAmount;
 
                 //记录用户在此订单项获得的冻结积分
                 TradeOrderItemDO toid = new TradeOrderItemDO();
                 toid.setId(tradeOrderItemDO.getId());
                 toid.setIncreaseIntegral(itemFreezeAmount);
+                toid.setAncestorIncreaseIntegral(ancestorItemFreezeAmount);//20240504 add by Ben
                 tradeOrderItemMapper.updateById(toid);
             }
 
@@ -969,7 +998,13 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
 //            Integer freezeAmount = promotionExpenses2GrossProfitAncestorQuota(orderPercentageMapper,totalPromotionExpenses);
 
             String OrderNum = order.getNo();
-            integralService.updateUserIntegral(tradeOrderDO.getUserId(), CaclEnum.ORDER_PAY_INTEGRAL, 0L, totalFreezeAmount, tradeOrderId, OrderNum);
+            //修改购物者本人获得的冻结积分
+            integralService.updateUserIntegral(tradeOrderDO.getUserId(),tradeOrderDO.getUserId(),CaclEnum.ORDER_PAY_INTEGRAL, 0L, totalFreezeAmount, tradeOrderId, OrderNum);
+
+            //修改推荐人(上级)获得的冻结积分
+            integralService.updateUserIntegral(tradeOrderDO.getUserId(),ancesterUserId, CaclEnum.ORDER_PAY_INTEGRAL_ANCESTER, 0L, totalAncestorFreezeAmount, tradeOrderId, OrderNum);
+            log.warn("[TradeOrderUpdateServiceImpl.updateOrderPaid]$$$:[tradeOrderId({}),ancesterUserId({}),totalAncestorFreezeAmount({})]" + new Date(),
+                    tradeOrderId,ancesterUserId, totalAncestorFreezeAmount);
 
             allTradeOrderTotalFreezeAmount += totalFreezeAmount;//用户获得总的待确权冻结积分
 
@@ -1268,7 +1303,7 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
         String orderNum = order.getNo();
         //如果订单有使用积分,则退回积分
         if (payIntegral > 0) {
-            integralService.updateUserIntegral(userId, CaclEnum.ORDER_CANCEL_BY_USER_REFUND_INTEGRAL, payIntegral, 0L, tradeOrderId, orderNum);
+            integralService.updateUserIntegral(userId,userId, CaclEnum.ORDER_CANCEL_BY_USER_REFUND_INTEGRAL, payIntegral, 0L, tradeOrderId, orderNum);
         }
     }
 
@@ -1300,7 +1335,7 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
                 Long userId = order.getUserId();
                 //如果订单有使用积分,则退回积分
                 if (payIntegral > 0) {
-                    integralService.updateUserIntegral(userId, CaclEnum.ORDER_CANCEL_BY_SYSTEM_REFUND_INTEGRAL, payIntegral, 0L, tradeOrderId, orderNum);
+                    integralService.updateUserIntegral(userId, userId,CaclEnum.ORDER_CANCEL_BY_SYSTEM_REFUND_INTEGRAL, payIntegral, 0L, tradeOrderId, orderNum);
                 }
 
                 count++;