修复 consume 缺行不建行导致 sentTotal 丢失的问题
This commit is contained in:
+8
-3
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user