|
|
@@ -17,6 +17,29 @@ class GiftCards extends Model implements GiftCardsContract
|
|
|
{
|
|
|
use HasFactory;
|
|
|
|
|
|
+ /**
|
|
|
+ * 未使用(可用)。
|
|
|
+ */
|
|
|
+ public const STATUS_UNUSED = 1;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 已使用(余额已用完)。
|
|
|
+ */
|
|
|
+ public const STATUS_USED = 2;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 已过期(超过 expirationdate,由 gift-cards:expire 定时任务写入)。
|
|
|
+ */
|
|
|
+ public const STATUS_EXPIRED = 3;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 「已经领过」的状态:未使用 + 已过期(已用完的不算)。
|
|
|
+ *
|
|
|
+ * 礼品卡过期后状态会从「未使用」变成「已过期」,但业务上依然算领过,
|
|
|
+ * 不能再拿积分重复兑换同一渠道的礼品卡 —— 所以前台列表和兑换接口统一用这个范围。
|
|
|
+ */
|
|
|
+ public const CLAIMED_STATUSES = [self::STATUS_UNUSED, self::STATUS_EXPIRED];
|
|
|
+
|
|
|
protected $table = 'gift_cards';
|
|
|
|
|
|
protected $fillable = [
|
|
|
@@ -104,13 +127,15 @@ class GiftCards extends Model implements GiftCardsContract
|
|
|
if (empty($giftcardAmount)) {
|
|
|
throw new \Exception('giftcardAmount is empty');
|
|
|
}
|
|
|
- $data = GiftCards::where('customer_id', $customerId)
|
|
|
- ->where('channel', $channel)
|
|
|
- ->count();
|
|
|
- if ($num != 0) {
|
|
|
- $giftNum =count($data);
|
|
|
- if($giftNum <= $num){
|
|
|
- throw new \Exception('gift num is not enough');
|
|
|
+ // $num > 0 表示该渠道最多允许「已领过」(未使用 + 已过期)这么多张,超出就报错
|
|
|
+ if ($num > 0) {
|
|
|
+ $claimedNum = GiftCards::where('customer_id', $customerId)
|
|
|
+ ->where('channel', $channel)
|
|
|
+ ->whereIn('giftcard_status', self::CLAIMED_STATUSES)
|
|
|
+ ->count();
|
|
|
+
|
|
|
+ if ($claimedNum >= $num) {
|
|
|
+ throw new \Exception("Gift card limit reached for channel {$channel} (max: {$num}).");
|
|
|
}
|
|
|
}
|
|
|
$expirationDate = self::calculateExpirationDate($expirationdate);
|
|
|
@@ -121,7 +146,7 @@ class GiftCards extends Model implements GiftCardsContract
|
|
|
'remaining_giftcard_amount' => $giftcardAmount,
|
|
|
'customer_id' => $customerId,
|
|
|
'expirationdate' => $expirationDate,
|
|
|
- 'giftcard_status' => 1,
|
|
|
+ 'giftcard_status' => self::STATUS_UNUSED,
|
|
|
'channel' => $channel,
|
|
|
'notes' => $notes
|
|
|
]);
|
|
|
@@ -156,6 +181,22 @@ class GiftCards extends Model implements GiftCardsContract
|
|
|
|
|
|
throw new \Exception('Invalid expiration date type. Must be integer (days), string (Y-m-d), or Carbon instance.');
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 这张礼品卡是否已过期。
|
|
|
+ *
|
|
|
+ * 状态已被定时任务标成「已过期」,或者过期时间已经过去,都算过期;
|
|
|
+ * expirationdate 为空的卡视为永久有效。
|
|
|
+ */
|
|
|
+ public function isExpired(): bool
|
|
|
+ {
|
|
|
+ if ((int) $this->giftcard_status === self::STATUS_EXPIRED) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->expirationdate !== null && $this->expirationdate->isPast();
|
|
|
+ }
|
|
|
+
|
|
|
public static function generateGiftCardNumber()
|
|
|
{
|
|
|
do {
|