6
0
Fork 0

优化订单状态筛选

release
李静 2021-12-16 17:28:06 +08:00
parent 37856defd6
commit 0ffac31495
2 changed files with 104 additions and 7 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}