红包状态修改

This commit is contained in:
2025-10-14 18:09:49 +08:00
parent 7a57493a19
commit 4aac4e9109
4 changed files with 143 additions and 27 deletions

View File

@@ -22,7 +22,7 @@ local currentTime = tonumber(ARGV[1])
-- 检查红包是否存在
local redpacketData = redis.call('HGETALL', redpacketKey)
if not redpacketData or #redpacketData == 0 then
return {0, "红包不存在"}
return {0, "红包不存在", 0}
end
-- 将哈希数据转为table
@@ -38,27 +38,27 @@ local endTime = tonumber(redpacket['end_time'])
if status == 0 then
if currentTime < startTime then
return {0, "红包还未开始"}
return {0, "红包还未开始", 0}
else
-- 更新状态为进行中
-- 更新状态为进行中(1)
redis.call('HSET', redpacketKey, 'status', 1)
status = 1
end
end
-- if status ~= 1 then
-- return {0, "红包已结束"}
-- end
if status ~= 1 then
return {0, "红包已结束", 0}
end
-- if currentTime > endTime then
-- redis.call('HSET', redpacketKey, 'status', 2)
-- return {0, "红包已结束"}
-- end
if currentTime > endTime then
redis.call('HSET', redpacketKey, 'status', 2)
return {0, "红包已结束", 0}
end
-- 检查是否已经抢过
local hasGrabbed = redis.call('SISMEMBER', userSetKey, userId)
if hasGrabbed == 1 then
return {0, "已经抢过该红包"}
return {0, "已经抢过该红包", 0}
end
-- 检查是否还有剩余
@@ -66,14 +66,17 @@ local leftAmount = tonumber(redpacket['left_amount'])
local leftCount = tonumber(redpacket['left_count'])
if leftCount <= 0 or leftAmount <= 0 then
return {0, "红包已抢完"}
return {0, "红包已抢完", 0}
end
-- 计算红包金额
local amount = 0
local isFinished = 0
if leftCount == 1 then
-- 最后一个红包,获得剩余所有金额
amount = leftAmount
isFinished = 1
else
-- 随机算法:二倍均值法,保证公平性
local maxAmount = leftAmount / leftCount * 2
@@ -82,6 +85,10 @@ else
if amount > leftAmount then
amount = leftAmount
end
-- 检查是否是最后一个(由于浮点数计算可能有误差)
if leftCount == 1 or (leftAmount - amount) < 0.01 then
isFinished = 1
end
end
-- 更新红包数据
@@ -94,12 +101,14 @@ redis.call('HSET', redpacketKey, 'left_count', newLeftCount)
-- 标记用户已抢
redis.call('SADD', userSetKey, userId)
-- 如果抢完了,更新状态
if newLeftCount == 0 then
-- 如果抢完了,更新状态为已结束(2)
if isFinished == 1 then
redis.call('HSET', redpacketKey, 'status', 2)
end
return {1, tostring(amount)}
return {1, tostring(amount), isFinished}
LUA;
}
}