Browse Source

fix(f03): preview兼容MySQL8返回的LocalDateTime(避免ClassCastException 500)

Xiaogang Liao 11 hours ago
parent
commit
813e8fe082

+ 11 - 2
cfc-backend/src/main/java/com/etotem/cfc/service/CfCommissionRecalcService.java

@@ -123,8 +123,8 @@ public class CfCommissionRecalcService {
                 "SELECT order_id, user_id, created_at FROM ppoint_consumption_log ORDER BY order_id");
         for (Map<String, Object> row : rows) {
             long orderId = ((Number) row.get("order_id")).longValue();
-            java.sql.Timestamp ts = (java.sql.Timestamp) row.get("created_at");
-            Date asOf = ts != null ? new Date(ts.getTime()) : now;
+            Object tsObj = row.get("created_at");
+            Date asOf = toDate(tsObj, now); // 兼容 MySQL8 驱动返回 LocalDateTime 与 java.sql.Timestamp
             Long buyerUserId = row.get("user_id") == null ? null : ((Number) row.get("user_id")).longValue();
             // 订单买家链路上两级推荐人(跳过同家庭)→ F03a/F03b 受益人
             List<Object[]> beneficiaries = resolveBeneficiaries(orderId, buyerUserId, asOf);
@@ -256,4 +256,13 @@ public class CfCommissionRecalcService {
                     "SELECT id FROM family_members WHERE user_id=? ORDER BY id LIMIT 1", Long.class, userId);
         } catch (Exception e) { return null; }
     }
+
+    /** JdbcTemplate 日期兼容:MySQL8 驱动返回 LocalDateTime,旧驱动返回 java.sql.Timestamp/java.util.Date。 */
+    private static Date toDate(Object v, Date def) {
+        if (v == null) return def;
+        if (v instanceof java.time.LocalDateTime) return java.util.Date.from(((java.time.LocalDateTime) v).atZone(java.time.ZoneId.systemDefault()).toInstant());
+        if (v instanceof java.sql.Timestamp) return new Date(((java.sql.Timestamp) v).getTime());
+        if (v instanceof Date) return (Date) v;
+        return def;
+    }
 }

+ 6 - 0
cfc-backend/src/test/java/com/etotem/cfc/unit/F03FinalFixesTest.java

@@ -58,4 +58,10 @@ class F03FinalFixesTest {
         String src = read("src/main/java/com/etotem/cfc/config/DatabaseInitializer.java");
         assertTrue(src.contains("f03_tier_redesign_done"), "档位重建应一次性标记,不覆盖运营调整");
     }
+
+    @Test void preview_handles_localdatetime_from_mysql8() throws Exception {
+        String src = read("src/main/java/com/etotem/cfc/service/CfCommissionRecalcService.java");
+        assertTrue(src.contains("LocalDateTime") && src.contains("toDate("),
+                "MySQL8 驱动返回 LocalDateTime,recalc 必须兼容转换,否则 preview 报 ClassCastException");
+    }
 }