Просмотр исходного кода

fix(f03): F03档位业绩单位从P点改为实付金额(元,money_amount/100)实时+重算都改, 奖金基数仍P点; 排除退款单; legacy/F03c保持旧P点

Xiaogang Liao 4 часов назад
Родитель
Сommit
6d4f439822

+ 16 - 0
cfc-backend/src/main/java/com/etotem/cfc/mapper/PpointConsumptionLogMapper.java

@@ -69,4 +69,20 @@ public interface PpointConsumptionLogMapper extends BaseMapper<PpointConsumption
     })
     })
     int sumPpointsByPaidAt(@Param("userIds") Collection<Long> userIds,
     int sumPpointsByPaidAt(@Param("userIds") Collection<Long> userIds,
                            @Param("since") Date since, @Param("until") Date until);
                            @Param("since") Date since, @Param("until") Date until);
+
+    /**
+     * F03 档位业绩:按订单实付金额(money_amount, 分转元)聚合,按 paid_at 归属。
+     * 业绩门槛单位=元;排除退款订单。until 用 &lt; (exclusive) 以排除被计算订单自身。
+     */
+    @Select({
+            "<script>",
+            "SELECT COALESCE(SUM(money_amount), 0) / 100 FROM product_orders",
+            "WHERE buyer_id IN",
+            "<foreach collection='userIds' item='uid' open='(' separator=',' close=')'>#{uid}</foreach>",
+            "AND paid_at &gt;= #{since} AND paid_at &lt; #{until}",
+            "AND COALESCE(refund_status, 0) = 0",
+            "</script>"
+    })
+    int sumPaidAmountYuan(@Param("userIds") Collection<Long> userIds,
+                          @Param("since") Date since, @Param("until") Date until);
 }
 }

+ 17 - 17
cfc-backend/src/main/java/com/etotem/cfc/service/CfReferralService.java

@@ -237,7 +237,7 @@ public class CfReferralService {
         for (Map.Entry<Long, Integer> e : minLevel.entrySet()) {
         for (Map.Entry<Long, Integer> e : minLevel.entrySet()) {
             int w = 11 - e.getValue(); // L1→10(100%), L10→1(10%), L11→0
             int w = 11 - e.getValue(); // L1→10(100%), L10→1(10%), L11→0
             if (w <= 0) continue;
             if (w <= 0) continue;
-            long pp = sumAnnualPpointsAsOf(Collections.singletonList(e.getKey()), since, end);
+            long pp = sumAnnualPaidYuan(Collections.singletonList(e.getKey()), since, new Date(end.getTime() + 1));
             total += (int) Math.round(pp * w / 10.0);
             total += (int) Math.round(pp * w / 10.0);
         }
         }
         return total;
         return total;
@@ -247,7 +247,7 @@ public class CfReferralService {
     public int getSelfAnnualPpointsAt(Long referrerUserId, Date asOf) {
     public int getSelfAnnualPpointsAt(Long referrerUserId, Date asOf) {
         if (referrerUserId == null) return 0;
         if (referrerUserId == null) return 0;
         Date end = asOf != null ? asOf : new Date();
         Date end = asOf != null ? asOf : new Date();
-        return sumAnnualPpointsAsOf(Collections.singletonList(referrerUserId), rollingYearAgo(end), end);
+        return sumAnnualPaidYuan(Collections.singletonList(referrerUserId), rollingYearAgo(end), new Date(end.getTime() + 1));
     }
     }
 
 
     /** F03a 档位业绩 = 本人100% + 团队加权(self 只加一次,团队遍历不含本人)。 */
     /** F03a 档位业绩 = 本人100% + 团队加权(self 只加一次,团队遍历不含本人)。 */
@@ -255,7 +255,7 @@ public class CfReferralService {
         if (referrerUserId == null) return 0;
         if (referrerUserId == null) return 0;
         Date end = asOf != null ? asOf : new Date();
         Date end = asOf != null ? asOf : new Date();
         Date since = rollingYearAgo(end);
         Date since = rollingYearAgo(end);
-        int self = sumAnnualPpointsAsOf(Collections.singletonList(referrerUserId), since, end);
+        int self = sumAnnualPaidYuan(Collections.singletonList(referrerUserId), since, new Date(end.getTime() + 1));
         return self + getWeightedTeamAnnualPpoints(referrerUserId, asOf);
         return self + getWeightedTeamAnnualPpoints(referrerUserId, asOf);
     }
     }
 
 
@@ -270,7 +270,7 @@ public class CfReferralService {
         if (referrerUserId == null || orderPaidAt == null) return 0;
         if (referrerUserId == null || orderPaidAt == null) return 0;
         Date since = rollingYearAgo(orderPaidAt);
         Date since = rollingYearAgo(orderPaidAt);
         // self(按 paid_at,排除自身)
         // self(按 paid_at,排除自身)
-        int self = ppointConsumptionLogMapper.sumPpointsByPaidAt(
+        int self = ppointConsumptionLogMapper.sumPaidAmountYuan(
                 Collections.singletonList(referrerUserId), since, orderPaidAt);
                 Collections.singletonList(referrerUserId), since, orderPaidAt);
         // 团队逐层权重(按 paid_at 归属,排除自身)
         // 团队逐层权重(按 paid_at 归属,排除自身)
         LambdaQueryWrapper<ReferralTree> qw = new LambdaQueryWrapper<ReferralTree>()
         LambdaQueryWrapper<ReferralTree> qw = new LambdaQueryWrapper<ReferralTree>()
@@ -291,7 +291,7 @@ public class CfReferralService {
             for (Map.Entry<Long, Integer> e : minLevel.entrySet()) {
             for (Map.Entry<Long, Integer> e : minLevel.entrySet()) {
                 int w = 11 - e.getValue();
                 int w = 11 - e.getValue();
                 if (w <= 0) continue;
                 if (w <= 0) continue;
-                long pp = ppointConsumptionLogMapper.sumPpointsByPaidAt(
+                long pp = ppointConsumptionLogMapper.sumPaidAmountYuan(
                         Collections.singletonList(e.getKey()), since, orderPaidAt);
                         Collections.singletonList(e.getKey()), since, orderPaidAt);
                 teamWeighted += (int) Math.round(pp * w / 10.0);
                 teamWeighted += (int) Math.round(pp * w / 10.0);
             }
             }
@@ -302,7 +302,7 @@ public class CfReferralService {
     /** F03b 自身业绩(重算专用,按 paid_at 归属、排除自身)。 */
     /** F03b 自身业绩(重算专用,按 paid_at 归属、排除自身)。 */
     public int getSelfPpointsForRecalc(Long referrerUserId, Date orderPaidAt) {
     public int getSelfPpointsForRecalc(Long referrerUserId, Date orderPaidAt) {
         if (referrerUserId == null || orderPaidAt == null) return 0;
         if (referrerUserId == null || orderPaidAt == null) return 0;
-        return ppointConsumptionLogMapper.sumPpointsByPaidAt(
+        return ppointConsumptionLogMapper.sumPaidAmountYuan(
                 Collections.singletonList(referrerUserId), rollingYearAgo(orderPaidAt), orderPaidAt);
                 Collections.singletonList(referrerUserId), rollingYearAgo(orderPaidAt), orderPaidAt);
     }
     }
 
 
@@ -354,23 +354,23 @@ public class CfReferralService {
 
 
     /** 按用户集合分批求和滚动一年 P 点(IN 分批避免超长 SQL) */
     /** 按用户集合分批求和滚动一年 P 点(IN 分批避免超长 SQL) */
     private int sumAnnualPpoints(List<Long> userIds) {
     private int sumAnnualPpoints(List<Long> userIds) {
-        if (userIds == null || userIds.isEmpty()) {
-            return 0;
+        if (userIds == null || userIds.isEmpty()) return 0;
+        // F03c 冻结口径专用:保持旧 P 点等权求和(不改为元),供 legacy 快照匹配
+        int total = 0;
+        for (int i = 0; i < userIds.size(); i += USER_ID_BATCH_SIZE) {
+            List<Long> batch = userIds.subList(i, Math.min(i + USER_ID_BATCH_SIZE, userIds.size()));
+            total += ppointConsumptionLogMapper.sumPpointsSince(batch, rollingYearAgo());
         }
         }
-        return sumAnnualPpointsAsOf(userIds, rollingYearAgo(), null);
+        return total;
     }
     }
 
 
-    /** 按用户集合分批求和 [since, until] 窗口 P 点(until=null 表示无上界) */
-    private int sumAnnualPpointsAsOf(List<Long> userIds, Date since, Date until) {
-        if (userIds == null || userIds.isEmpty()) {
-            return 0;
-        }
+    /** 按用户集合分批求和 [since, until) 窗口实付金额(元)。 */
+    private int sumAnnualPaidYuan(List<Long> userIds, Date since, Date until) {
+        if (userIds == null || userIds.isEmpty()) return 0;
         int total = 0;
         int total = 0;
         for (int i = 0; i < userIds.size(); i += USER_ID_BATCH_SIZE) {
         for (int i = 0; i < userIds.size(); i += USER_ID_BATCH_SIZE) {
             List<Long> batch = userIds.subList(i, Math.min(i + USER_ID_BATCH_SIZE, userIds.size()));
             List<Long> batch = userIds.subList(i, Math.min(i + USER_ID_BATCH_SIZE, userIds.size()));
-            total += until == null
-                    ? ppointConsumptionLogMapper.sumPpointsSince(batch, since)
-                    : ppointConsumptionLogMapper.sumPpointsSinceUntil(batch, since, until);
+            total += ppointConsumptionLogMapper.sumPaidAmountYuan(batch, since, until);
         }
         }
         return total;
         return total;
     }
     }

+ 8 - 7
cfc-backend/src/test/java/com/etotem/cfc/service/CfReferralWeightedPpointsTest.java

@@ -48,7 +48,7 @@ class CfReferralWeightedPpointsTest {
         when(referralTreeMapper.selectList(any())).thenReturn(Arrays.asList(
         when(referralTreeMapper.selectList(any())).thenReturn(Arrays.asList(
                 tree(1L, 101L, 1), tree(1L, 102L, 2), tree(1L, 110L, 10)));
                 tree(1L, 101L, 1), tree(1L, 102L, 2), tree(1L, 110L, 10)));
         // 按用户集合返回P点:用 thenAnswer 区分
         // 按用户集合返回P点:用 thenAnswer 区分
-        when(ppointConsumptionLogMapper.sumPpointsSinceUntil(anyCollection(), any(Date.class), any(Date.class)))
+        when(ppointConsumptionLogMapper.sumPaidAmountYuan(anyCollection(), any(Date.class), any(Date.class)))
                 .thenAnswer(inv -> {
                 .thenAnswer(inv -> {
                     Collection<Long> ids = inv.getArgument(0);
                     Collection<Long> ids = inv.getArgument(0);
                     int sum = 0;
                     int sum = 0;
@@ -66,7 +66,7 @@ class CfReferralWeightedPpointsTest {
         // 同一后代 103 既在 L1 又在 L2(脏数据)→ 取 min(level)=1 权重0.9;本人1混入 → 排除
         // 同一后代 103 既在 L1 又在 L2(脏数据)→ 取 min(level)=1 权重0.9;本人1混入 → 排除
         when(referralTreeMapper.selectList(any())).thenReturn(Arrays.asList(
         when(referralTreeMapper.selectList(any())).thenReturn(Arrays.asList(
                 tree(1L, 103L, 1), tree(1L, 103L, 2), tree(1L, 1L, 1)));
                 tree(1L, 103L, 1), tree(1L, 103L, 2), tree(1L, 1L, 1)));
-        when(ppointConsumptionLogMapper.sumPpointsSinceUntil(anyCollection(), any(Date.class), any(Date.class)))
+        when(ppointConsumptionLogMapper.sumPaidAmountYuan(anyCollection(), any(Date.class), any(Date.class)))
                 .thenReturn(10000);
                 .thenReturn(10000);
         assertEquals(10000, service.getWeightedTeamAnnualPpoints(1L, asOf));
         assertEquals(10000, service.getWeightedTeamAnnualPpoints(1L, asOf));
     }
     }
@@ -74,7 +74,7 @@ class CfReferralWeightedPpointsTest {
     @Test
     @Test
     void f03a_tier_ppoints_is_self_plus_weighted_once() {
     void f03a_tier_ppoints_is_self_plus_weighted_once() {
         // self=10000(用户1本人),团队加权=10000(用户101 L1 ×100%)
         // self=10000(用户1本人),团队加权=10000(用户101 L1 ×100%)
-        when(ppointConsumptionLogMapper.sumPpointsSinceUntil(anyCollection(), any(Date.class), any(Date.class)))
+        when(ppointConsumptionLogMapper.sumPaidAmountYuan(anyCollection(), any(Date.class), any(Date.class)))
                 .thenAnswer(inv -> {
                 .thenAnswer(inv -> {
                     Collection<Long> ids = inv.getArgument(0);
                     Collection<Long> ids = inv.getArgument(0);
                     if (ids.size() == 1 && ids.contains(1L)) return 10000; // self
                     if (ids.size() == 1 && ids.contains(1L)) return 10000; // self
@@ -88,7 +88,7 @@ class CfReferralWeightedPpointsTest {
     @Test
     @Test
     void recalc_weight_L1_is_100_percent() {
     void recalc_weight_L1_is_100_percent() {
         // 重算专用:按 paid_at 归属,L1直推自购100%计入;李志清51,400×100%=51,400≥32,000→至富70%
         // 重算专用:按 paid_at 归属,L1直推自购100%计入;李志清51,400×100%=51,400≥32,000→至富70%
-        when(ppointConsumptionLogMapper.sumPpointsByPaidAt(anyCollection(), any(Date.class), any(Date.class)))
+        when(ppointConsumptionLogMapper.sumPaidAmountYuan(anyCollection(), any(Date.class), any(Date.class)))
                 .thenAnswer(inv -> {
                 .thenAnswer(inv -> {
                     Collection<Long> ids = inv.getArgument(0);
                     Collection<Long> ids = inv.getArgument(0);
                     return ids.contains(101L) ? 51400 : 0;
                     return ids.contains(101L) ? 51400 : 0;
@@ -111,11 +111,12 @@ class CfReferralWeightedPpointsTest {
     void asOf_filters_ppoints_by_created_at() {
     void asOf_filters_ppoints_by_created_at() {
         // 验证 asOf 截止:mapper 应被调用时传入 asOf(滚动窗口右界=asOf)
         // 验证 asOf 截止:mapper 应被调用时传入 asOf(滚动窗口右界=asOf)
         when(referralTreeMapper.selectList(any())).thenReturn(Collections.singletonList(tree(1L, 101L, 1)));
         when(referralTreeMapper.selectList(any())).thenReturn(Collections.singletonList(tree(1L, 101L, 1)));
-        when(ppointConsumptionLogMapper.sumPpointsSinceUntil(anyCollection(), any(Date.class), any(Date.class)))
+        when(ppointConsumptionLogMapper.sumPaidAmountYuan(anyCollection(), any(Date.class), any(Date.class)))
                 .thenReturn(10000);
                 .thenReturn(10000);
         service.getWeightedTeamAnnualPpoints(1L, asOf);
         service.getWeightedTeamAnnualPpoints(1L, asOf);
         org.mockito.ArgumentCaptor<Date> cap = org.mockito.ArgumentCaptor.forClass(Date.class);
         org.mockito.ArgumentCaptor<Date> cap = org.mockito.ArgumentCaptor.forClass(Date.class);
-        verify(ppointConsumptionLogMapper, atLeastOnce()).sumPpointsSinceUntil(anyCollection(), any(Date.class), cap.capture());
-        assertTrue(cap.getAllValues().stream().anyMatch(d -> d.equals(asOf)), "应以订单时刻asOf为窗口右界");
+        verify(ppointConsumptionLogMapper, atLeastOnce()).sumPaidAmountYuan(anyCollection(), any(Date.class), cap.capture());
+        assertTrue(cap.getAllValues().stream().anyMatch(d -> Math.abs(d.getTime() - asOf.getTime()) <= 1),
+                "应以订单时刻asOf为窗口右界(允许+1ms exclusive容差)");
     }
     }
 }
 }

+ 3 - 4
cfc-backend/src/test/java/com/etotem/cfc/unit/AsOfPaidAtTest.java

@@ -14,8 +14,8 @@ class AsOfPaidAtTest {
 
 
     @Test void mapper_aggregates_ppoints_by_order_paid_at() throws Exception {
     @Test void mapper_aggregates_ppoints_by_order_paid_at() throws Exception {
         String src = read("src/main/java/com/etotem/cfc/mapper/PpointConsumptionLogMapper.java");
         String src = read("src/main/java/com/etotem/cfc/mapper/PpointConsumptionLogMapper.java");
-        assertTrue(src.contains("JOIN product_orders"), "P点业绩聚合必须JOIN product_orders按paid_at归属");
-        assertTrue(src.contains("o.paid_at"), "业绩时间基准应为 product_orders.paid_at,而非日志created_at");
+        assertTrue(src.contains("JOIN product_orders"), "业绩聚合必须JOIN product_orders按paid_at归属");
+        assertTrue(src.contains("paid_at"), "业绩时间基准应为 product_orders.paid_at");
     }
     }
 
 
     @Test void recalc_uses_paid_at_and_excludes_self() throws Exception {
     @Test void recalc_uses_paid_at_and_excludes_self() throws Exception {
@@ -36,7 +36,6 @@ class AsOfPaidAtTest {
 
 
     @Test void referral_has_paid_at_aggregation_method() throws Exception {
     @Test void referral_has_paid_at_aggregation_method() throws Exception {
         String src = read("src/main/java/com/etotem/cfc/service/CfReferralService.java");
         String src = read("src/main/java/com/etotem/cfc/service/CfReferralService.java");
-        assertTrue(src.contains("PaidAt") || src.contains("paid_at"),
-            "CfReferralService应有按paid_at的业绩聚合重载供重算使用");
+        assertTrue(src.contains("sumPaidAmountYuan"), "业绩应按实付金额(元)聚合,不是P点");
     }
     }
 }
 }