合并外部维护版本并同步自动每日功能

This commit is contained in:
2026-09-28 13:55:07 +08:00
parent 2d81615f78
commit 9040bcb5e5
59 changed files with 3176 additions and 179 deletions
+24
View File
@@ -208,6 +208,30 @@ class RedemptionCodeStore:
if starts_utc >= ends_utc:
raise ValueError("失效时间必须晚于生效时间")
now = utc_now()
# 同名的"已停用/已删除且从未兑换"旧码会让 UNIQUE 约束挡住重建,
# 直接在旧记录上复用(更新内容并重新启用),历史 id 保持连续。
stale = connection.execute(
"""SELECT id FROM redemption_codes
WHERE code_normalized = ? AND redeemed_count = 0
AND (deleted_at_utc IS NOT NULL OR enabled = 0)""",
(normalized,),
).fetchone()
if stale is not None:
connection.execute(
"""UPDATE redemption_codes SET
code_display = ?, points = ?, starts_at_utc = ?, ends_at_utc = ?,
max_redemptions = ?, enabled = ?, deleted_at_utc = NULL,
updated_at_utc = ?
WHERE id = ?""",
(display, points, starts_utc, ends_utc, max_value,
int(bool(enabled)), now, stale["id"]),
)
connection.commit()
row = connection.execute(
"SELECT * FROM redemption_codes WHERE id=?", (stale["id"],)
).fetchone()
assert row is not None
return self._serialize_code(row)
try:
cursor = connection.execute(
"""INSERT INTO redemption_codes (