6
0
Fork 0

自动完成订单

release
李静 2021-12-28 14:27:02 +08:00
parent 63739c1948
commit 779babf26e
3 changed files with 53 additions and 2 deletions

View File

@ -0,0 +1,38 @@
<?php
namespace App\Console\Commands;
use App\Models\Order;
use App\Services\OrderService;
use Illuminate\Console\Command;
class OrderCompleteCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'order:complete';
/**
* The console command description.
*
* @var string
*/
protected $description = '将可自动完成的订单标记为已完成';
/**
* Execute the console command.
*
* @return int
*/
public function handle(OrderService $orderService)
{
Order::with('packages')->completable()->chunkById(200, function ($orders) use ($orderService) {
foreach ($orders as $order) {
$orderService->confirm($order);
}
});
}
}

View File

@ -94,6 +94,16 @@ class Order extends Model
->where('created_at', '<=', now()->subSeconds(app_settings('app.order_payment_expires_at')));
}
/**
* 仅查询可自动完成的订单
*/
public function scopeCompletable()
{
return $this->where('status', static::STATUS_PAID)
->where('shipping_state', static::SHIPPING_STATE_PROCESSED)
->where('auto_complete_at', '<=', now());
}
/**
* 下单人
*

View File

@ -800,8 +800,11 @@ class OrderService
$orderPackageService = new OrderPackageService();
// 获取订单的未作废未签收包裹
$packages = $order->packages()->unchecked()->where('is_failed', false)->get();
$order->loadMissing('packages');
$packages = $order->packages()->filter(function ($item) {
return ! $item->is_failed && ! $item->isChecked();
});
foreach ($packages as $package) {
$orderPackageService->checkPackage($package, true);