修复 consume 缺行不建行导致 sentTotal 丢失的问题

This commit is contained in:
yuming
2026-08-15 17:40:31 +08:00
parent 8d63b47577
commit 07ee98f838
2 changed files with 20 additions and 3 deletions
+8 -3
View File
@@ -38,12 +38,17 @@ function grant(openid, count = 1) {
return getBalance(openid)
}
// 消费:即便该 openid 从未 grant 过也要建行,否则 sentTotal 会静默丢失
// (新建行 balance 直接落 0,等价于「从 0 扣减再取 MAX(0, ...)」)
function consume(openid, count = 1) {
const n = _normalize(count)
db.prepare(`
UPDATE subscribe_quota
SET balance = MAX(0, balance - @n), sentTotal = sentTotal + @n, updateTime = @now
WHERE openid = @openid
INSERT INTO subscribe_quota (openid, balance, grantedTotal, sentTotal, updateTime)
VALUES (@openid, 0, 0, @n, @now)
ON CONFLICT(openid) DO UPDATE SET
balance = MAX(0, balance - @n),
sentTotal = sentTotal + @n,
updateTime = @now
`).run({ openid, n, now: Date.now() })
return getBalance(openid)
}