From 0ffac3149537ec697b73f864a0ef2b7c201227c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=9D=99?= Date: Thu, 16 Dec 2021 17:28:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=AE=A2=E5=8D=95=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E7=AD=9B=E9=80=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Endpoint/Api/Filters/OrderFilter.php | 9 +- app/Models/Order.php | 102 ++++++++++++++++++++++- 2 files changed, 104 insertions(+), 7 deletions(-) diff --git a/app/Endpoint/Api/Filters/OrderFilter.php b/app/Endpoint/Api/Filters/OrderFilter.php index 69692ee3..b638dca7 100644 --- a/app/Endpoint/Api/Filters/OrderFilter.php +++ b/app/Endpoint/Api/Filters/OrderFilter.php @@ -2,7 +2,6 @@ namespace App\Endpoint\Api\Filters; -use App\Models\Order; use EloquentFilter\ModelFilter; class OrderFilter extends ModelFilter @@ -11,19 +10,19 @@ class OrderFilter extends ModelFilter { switch ($status) { case 'pending': - $this->where('status', Order::STATUS_PENDING); + $this->pending(); break; case 'unreceived': - $this->where('status', [Order::STATUS_PAID, Order::STATUS_SHIPPED]); + $this->unreceived(); break; case 'completed': - $this->where('status', Order::STATUS_COMPLETED); + $this->completed(); break; case 'cancelled': - $this->where('status', Order::STATUS_CANCELLED); + $this->cancelled(); break; } } diff --git a/app/Models/Order.php b/app/Models/Order.php index 41f6b63b..d0b820e3 100644 --- a/app/Models/Order.php +++ b/app/Models/Order.php @@ -33,6 +33,7 @@ class Order extends Model */ protected $casts = [ 'completed_at' => 'datetime', + 'status' => 'int', ]; /** @@ -70,6 +71,65 @@ class Order extends Model self::PAY_WAY_BALANCE => '余额', ]; + /** + * 仅查询已过期的订单 + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeExpired($query) + { + return $query->where('status', static::STATUS_PENDING) + ->where('created_at', '<=', now()->subSeconds(static::expiresAt())); + } + + /** + * 仅查询待付款的订单 + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopePending($query) + { + return $query->where('status', static::STATUS_PENDING) + ->where('created_at', '>', now()->subSeconds(static::expiresAt())); + } + + /** + * 仅查询待收货的订单 + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeUnreceived($query) + { + return $query->where('status', [static::STATUS_PAID, static::STATUS_SHIPPED]); + } + + /** + * 仅查询已完成的订单 + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeCompleted($query) + { + return $query->where('status', static::STATUS_COMPLETED); + } + + /** + * 仅查询已取消的订单 + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeCancelled($query) + { + return $query->where(function ($query) { + $query->expired(); + })->orWhere('status', static::STATUS_CANCELLED); + } + /** * 属于此订单的商品 */ @@ -98,6 +158,32 @@ class Order extends Model return in_array($this->status, [static::STATUS_PENDING, static::STATUS_PAID]); } + /** + * 确认此订单是否是待付款 + * + * @return bool + */ + public function isPending(): bool + { + return $this->status === static::STATUS_PENDING; + } + + /** + * 获取订单券优惠金额 + * + * @return int + */ + public function getStatusAttribute() + { + if ($this->attributes['status'] === static::STATUS_PENDING + && $this->created_at->lte(now()->subSeconds(static::expiresAt())) + ) { + return static::STATUS_CANCELLED; + } + + return $this->attributes['status']; + } + /** * 获取订单券优惠金额 * @@ -165,8 +251,20 @@ class Order extends Model */ public function getExpiresAtAttribute() { - // todo 待支付订单过期时间 + if (! $this->isPending()) { + return 0; + } - return 3600; + return 1800; + } + + /** + * 待付款订单的过期时间(单位:秒) + * + * @return int + */ + public static function expiresAt(): int + { + return 1800; } }