Sfoglia il codice sorgente

feat(k8): 迁移499 K8终身会员权益(3订单幂等补录+设备维护提醒表+6提醒生成, 复用日期计算器) + TDD 4例

Xiaogang Liao 5 giorni fa
parent
commit
09f98dffa5

+ 79 - 0
cfc-backend/src/main/java/com/etotem/cfc/config/DatabaseInitializer.java

@@ -3651,6 +3651,10 @@ insertSysConfigSeed("gift_membership_pay_price", "198000", "送会员商品达
         //          写入 product_order_auto_complete 27 字符 > 原列宽 20,触发 Data truncation,
         //          写入 product_order_auto_complete 27 字符 > 原列宽 20,触发 Data truncation,
         //          操作日志缺失)。幂等:MODIFY 可重复执行(已是 64 时语义等价 no-op)。
         //          操作日志缺失)。幂等:MODIFY 可重复执行(已是 64 时语义等价 no-op)。
         widenUserOperationLogType();
         widenUserOperationLogType();
+
+        // 迁移499: 还原水机 K8 终身会员权益(3 名 LIFETIME 补录交易 + 设备维护提醒表)
+        // 幂等:order_no 唯一 + 提醒唯一键 uk_reminder,重复执行不产生重复数据
+        migrateK8LifetimeBenefits();
     }
     }
 
 
     /**
     /**
@@ -3668,6 +3672,81 @@ insertSysConfigSeed("gift_membership_pay_price", "198000", "送会员商品达
         }
         }
     }
     }
 
 
+    /**
+     * 迁移499: 还原水机 K8 终身会员权益(spec 2026-11-19)。
+     * <p>① 创建设备维护提醒表 device_maintenance_reminders(唯一键 uk_reminder 幂等);
+     * ② 为 3 名 LIFETIME 会员补录 K8 交易订单(order_no 唯一守卫,INSERT IGNORE);
+     * ③ 按购买日推算洗机/滤芯到期,生成 6 条维护提醒(INSERT IGNORE)。</p>
+     * <p>整体 try-catch 吞异常,避免历史库缺列导致启动失败;幂等可重复执行。</p>
+     */
+    private void migrateK8LifetimeBenefits() {
+        try {
+            // ① 设备维护提醒表
+            jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS device_maintenance_reminders (" +
+                    "id BIGINT AUTO_INCREMENT PRIMARY KEY, " +
+                    "buyer_id BIGINT NOT NULL COMMENT '购买人user_id', " +
+                    "family_id BIGINT COMMENT '家庭ID', " +
+                    "product_id BIGINT NOT NULL COMMENT '来源设备product_id(9=还原水机)', " +
+                    "service_type VARCHAR(20) NOT NULL COMMENT 'wash_machine=洗机/replace_filter=换滤芯', " +
+                    "purchased_at DATETIME NOT NULL, " +
+                    "due_date DATE NOT NULL, " +
+                    "window_start_date DATE NOT NULL, " +
+                    "status VARCHAR(20) NOT NULL DEFAULT 'pending' COMMENT 'pending/done/dismissed', " +
+                    "created_at DATETIME DEFAULT CURRENT_TIMESTAMP, " +
+                    "updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, " +
+                    "UNIQUE KEY uk_reminder (buyer_id, product_id, service_type, due_date), " +
+                    "INDEX idx_family_status (family_id, status)" +
+                    ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='设备维护提醒'");
+
+            // ② 补录 3 名终身会员的 K8 交易记录(order_no 唯一守卫)
+            Object[][] members = {
+                    {53L, 71L, "2026-06-26"},
+                    {54L, 57L, "2026-09-27"},
+                    {55L, 59L, "2026-09-27"}
+            };
+            for (Object[] m : members) {
+                Long buyerId = (Long) m[0];
+                Long familyId = (Long) m[1];
+                String paidDate = (String) m[2];
+                String orderNo = "K8-LIFETIME-" + buyerId + "-" + paidDate.replace("-", "");
+                jdbcTemplate.update(
+                        "INSERT IGNORE INTO product_orders (order_no, product_id, product_name, buyer_id, family_id, "
+                                + "quantity, unit_price, total_amount, status, paid_at, created_at, updated_at) "
+                                + "VALUES (?, 9, '还原水机', ?, ?, 1, 4080000, 4080000, 'paid', ?, NOW(), NOW())",
+                        orderNo, buyerId, familyId, paidDate + " 00:00:00");
+            }
+
+            // ③ 生成设备维护提醒(复用 K8MaintenanceCalculator,插入幂等)
+            for (Object[] m : members) {
+                Long buyerId = (Long) m[0];
+                Long familyId = (Long) m[1];
+                java.time.LocalDateTime paidAt = java.time.LocalDateTime.of(
+                        Integer.parseInt(((String) m[2]).substring(0, 4)),
+                        Integer.parseInt(((String) m[2]).substring(5, 7)),
+                        Integer.parseInt(((String) m[2]).substring(8, 10)), 0, 0);
+                com.etotem.cfc.service.K8MaintenanceCalculator.MaintenanceDates d =
+                        com.etotem.cfc.service.K8MaintenanceCalculator.deriveMaintenance(paidAt);
+                insertMaintenanceReminder(buyerId, familyId, "wash_machine", paidAt, d.washDueDate, d.washWindowStart);
+                insertMaintenanceReminder(buyerId, familyId, "replace_filter", paidAt, d.filterDueDate, d.filterWindowStart);
+            }
+            log.info("迁移499: K8终身会员权益补录完成(3订单+6提醒)");
+        } catch (Exception e) {
+            log.warn("迁移499: K8终身会员权益补录失败(忽略): {}", e.getMessage());
+        }
+    }
+
+    /** 幂等插入单条设备维护提醒(唯一键 uk_reminder 去重)。 */
+    private void insertMaintenanceReminder(Long buyerId, Long familyId, String serviceType,
+                                           java.time.LocalDateTime purchasedAt,
+                                           java.time.LocalDate dueDate, java.time.LocalDate windowStart) {
+        jdbcTemplate.update(
+                "INSERT IGNORE INTO device_maintenance_reminders "
+                        + "(buyer_id, family_id, product_id, service_type, purchased_at, due_date, window_start_date, status) "
+                        + "VALUES (?, ?, 9, ?, ?, ?, ?, 'pending')",
+                buyerId, familyId, serviceType, purchasedAt,
+                java.sql.Date.valueOf(dueDate), java.sql.Date.valueOf(windowStart));
+    }
+
 
 
     /**
     /**
      * 迁移475: 创建资源匹配结果表(SC-H-05,能力键 butler.resource.match)。
      * 迁移475: 创建资源匹配结果表(SC-H-05,能力键 butler.resource.match)。

+ 17 - 0
cfc-backend/src/main/resources/schema.sql

@@ -7639,3 +7639,20 @@ CREATE TABLE IF NOT EXISTS cf_platform_revenue_log (
     KEY idx_created (created_at)
     KEY idx_created (created_at)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='平台抽成R0流水(独立入平台账)';
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='平台抽成R0流水(独立入平台账)';
 
 
+-- 设备维护提醒(还原水机K8终身会员权益,spec 2026-11-19)
+CREATE TABLE IF NOT EXISTS device_maintenance_reminders (
+    id BIGINT AUTO_INCREMENT PRIMARY KEY,
+    buyer_id BIGINT NOT NULL COMMENT '购买人user_id',
+    family_id BIGINT COMMENT '家庭ID',
+    product_id BIGINT NOT NULL COMMENT '来源设备product_id(9=还原水机)',
+    service_type VARCHAR(20) NOT NULL COMMENT 'wash_machine=洗机/replace_filter=换滤芯',
+    purchased_at DATETIME NOT NULL,
+    due_date DATE NOT NULL,
+    window_start_date DATE NOT NULL,
+    status VARCHAR(20) NOT NULL DEFAULT 'pending' COMMENT 'pending/done/dismissed',
+    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
+    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+    UNIQUE KEY uk_reminder (buyer_id, product_id, service_type, due_date),
+    INDEX idx_family_status (family_id, status)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='设备维护提醒';
+

+ 67 - 0
cfc-backend/src/test/java/com/etotem/cfc/unit/K8BenefitMigrationTest.java

@@ -0,0 +1,67 @@
+package com.etotem.cfc.unit;
+
+import org.junit.jupiter.api.Test;
+
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+/**
+ * TDD 契约:K8 补录+提醒迁移(spec §4.1/§4.3,纯源码文本断言,无 Spring/DB)。
+ * 断言 DatabaseInitializer 含幂等迁移方法:3 个 order_no、新表 DDL、唯一键、INSERT IGNORE。
+ */
+class K8BenefitMigrationTest {
+
+    private String read(String rel) throws Exception {
+        Path p = Paths.get(rel);
+        if (!Files.exists(p)) p = Paths.get("../cfc-backend/" + rel);
+        return new String(Files.readAllBytes(p), StandardCharsets.UTF_8);
+    }
+
+    @Test
+    void migration_method_exists() throws Exception {
+        String src = read("src/main/java/com/etotem/cfc/config/DatabaseInitializer.java");
+        assertTrue(src.contains("migrateK8LifetimeBenefits"),
+                "DatabaseInitializer 必须含 migrateK8LifetimeBenefits 幂等迁移方法");
+    }
+
+    @Test
+    void three_order_nos_with_idempotent_insert() throws Exception {
+        String src = read("src/main/java/com/etotem/cfc/config/DatabaseInitializer.java");
+        int idx = src.indexOf("migrateK8LifetimeBenefits");
+        assertTrue(idx >= 0);
+        String body = src.substring(idx, Math.min(src.length(), idx + 6000));
+        // order_no 运行时拼接:"K8-LIFETIME-" + buyerId + "-" + paidDate.replace("-","")
+        assertTrue(body.contains("K8-LIFETIME-"), "迁移应含 K8-LIFETIME- 订单号前缀");
+        assertTrue(body.contains("replace(\"-\", \"\")"), "订单号应去掉购买日横线(yyyyMMdd)");
+        for (String d : new String[]{"2026-06-26", "2026-09-27"}) {
+            assertTrue(body.contains(d), "迁移应含购买日 " + d);
+        }
+        for (String id : new String[]{"53L", "54L", "55L"}) {
+            assertTrue(body.contains(id), "迁移应含会员id " + id);
+        }
+        assertTrue(body.contains("INSERT IGNORE") || body.contains("insertIgnore"),
+                "补录必须幂等(INSERT IGNORE)");
+    }
+
+    @Test
+    void reminder_table_ddl_and_unique_key() throws Exception {
+        String init = read("src/main/java/com/etotem/cfc/config/DatabaseInitializer.java");
+        assertTrue(init.contains("device_maintenance_reminders"), "迁移应创建提醒表");
+        assertTrue(init.contains("uk_reminder"), "提醒表应有唯一键 uk_reminder");
+        String schema = read("src/main/resources/schema.sql");
+        assertTrue(schema.contains("device_maintenance_reminders"), "schema.sql 应同步新表 DDL");
+    }
+
+    @Test
+    void reminder_uses_calculator_for_due_dates() throws Exception {
+        String src = read("src/main/java/com/etotem/cfc/config/DatabaseInitializer.java");
+        int idx = src.indexOf("migrateK8LifetimeBenefits");
+        String body = src.substring(idx, Math.min(src.length(), idx + 6000));
+        assertTrue(body.contains("K8MaintenanceCalculator") || body.contains("deriveMaintenance"),
+                "迁移应复用 K8MaintenanceCalculator 推算到期日,避免日期逻辑重复");
+    }
+}