fix: gift card redemption limits validation

This commit is contained in:
xboard
2025-07-21 09:28:00 +08:00
parent 61773a13b4
commit 768e14bdb9
2 changed files with 6 additions and 6 deletions

View File

@@ -110,7 +110,7 @@ class GiftCardCode extends Model
public function isAvailable(): bool
{
// 检查状态
if ($this->status !== self::STATUS_UNUSED) {
if (in_array($this->status, [self::STATUS_EXPIRED, self::STATUS_DISABLED])) {
return false;
}

View File

@@ -222,27 +222,27 @@ class GiftCardTemplate extends Model
*/
public function checkUsageLimit(User $user): bool
{
$conditions = $this->conditions ?? [];
$limits = $this->limits ?? [];
// 检查每用户最大使用次数
if (isset($conditions['max_use_per_user'])) {
if (isset($limits['max_use_per_user'])) {
$usedCount = $this->usages()
->where('user_id', $user->id)
->count();
if ($usedCount >= $conditions['max_use_per_user']) {
if ($usedCount >= $limits['max_use_per_user']) {
return false;
}
}
// 检查冷却时间
if (isset($conditions['cooldown_hours'])) {
if (isset($limits['cooldown_hours'])) {
$lastUsage = $this->usages()
->where('user_id', $user->id)
->orderBy('created_at', 'desc')
->first();
if ($lastUsage && isset($lastUsage->created_at)) {
$cooldownTime = $lastUsage->created_at + ($conditions['cooldown_hours'] * 3600);
$cooldownTime = $lastUsage->created_at + ($limits['cooldown_hours'] * 3600);
if (time() < $cooldownTime) {
return false;
}