红包状态修改

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

@@ -240,9 +240,36 @@ class PerformPerSecond
*/
public function processExpiredRedpackets()
{
// 查找已结束但未退款的红包
$now = time();
$processedCount = 0;
// 1. 处理到时间的未开始红包,更新为进行中
$pendingRedpackets = Db::name('redpacket')
->where('status', Redpacket::STATUS_PENDING)
->where('start_time', '<=', $now)
->select();
foreach ($pendingRedpackets as $redpacket) {
Db::name('redpacket')
->where('id', $redpacket['id'])
->update([
'status' => Redpacket::STATUS_ACTIVE,
'updatetime' => $now
]);
// 更新Redis缓存
$redis = Cache::store('redis')->handler();
$redisKey = "redpacket:{$redpacket['id']}";
$redis->hSet($redisKey, 'status', Redpacket::STATUS_ACTIVE);
$processedCount++;
}
// 2. 处理已过期的进行中红包,更新为已结束并退款
$expiredRedpackets = Db::name('redpacket')
->where(['end_time' => ['<',time()], 'status' => 1])
->where('status', Redpacket::STATUS_ACTIVE)
->where('end_time', '<', $now)
->where('left_count', '>', 0)
->select();
foreach ($expiredRedpackets as $redpacket) {
@@ -276,27 +303,28 @@ class PerformPerSecond
}
}
// 更新红包状态
// 更新红包状态为已结束
Db::name('redpacket')
->where('id', $redpacket['id'])
->update([
'status' => 3,
'updatetime' => time()
'status' => Redpacket::STATUS_FINISHED,
'updatetime' => $now
]);
// 清理Redis缓存
// 更新Redis缓存
$redis = Cache::store('redis')->handler();
$redisKey = "redpacket:{$redpacket['id']}";
$redis->del($redisKey);
$redis->hSet($redisKey, 'status', Redpacket::STATUS_FINISHED);
Db::commit();
$processedCount++;
} catch (\Exception $e) {
Db::rollback();
// 记录日志
\think\Log::error("红包退款失败: {$redpacket['id']}, 错误: " . $e->getMessage());
}
}
echo "处理过期红包-共". $processedCount . "条数据\n";
}
}