72 lines
2.0 KiB
PHP
72 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Exceptions\OrderPackageAlreadyCheckedException;
|
|
use App\Models\OrderPackage;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
class OrderPackageService
|
|
{
|
|
/**
|
|
* 更新包裹状态
|
|
*
|
|
* @param \App\Models\OrderPackage $packages
|
|
* @param int $status
|
|
* @return void
|
|
*/
|
|
public function updatePackageStatus(OrderPackage $package, int $status): void
|
|
{
|
|
// 如果不是签收包裹,则直接更新包裹状态
|
|
if (! in_array($status, [OrderPackage::STATUS_CHECK, OrderPackage::STATUS_AUTOCHECK])) {
|
|
$package->update([
|
|
'status'=>$status,
|
|
]);
|
|
|
|
return;
|
|
}
|
|
|
|
// 签收包裹
|
|
$this->checkPackage($package, $status === OrderPackage::STATUS_AUTOCHECK);
|
|
|
|
// 如果订单包裹未发完
|
|
// if ($package->order->isShipped()) {
|
|
// return;
|
|
// }
|
|
|
|
// 如果订单有未作废且未签收的包裹
|
|
// if (! $package->order->where('is_failed', false)->unchecked()->exists()) {
|
|
// return;
|
|
// }
|
|
|
|
// $package->order->markAsCompleted();
|
|
}
|
|
|
|
/**
|
|
* 签收包裹
|
|
*
|
|
* @param \App\Models\OrderPackage $package
|
|
* @param bool $auto
|
|
* @param \Illuminate\Support\Carbon|null $time
|
|
* @return void
|
|
*
|
|
* @throws \App\Exceptions\OrderPackageAlreadyCheckedException
|
|
*/
|
|
public function checkPackage(OrderPackage $package, bool $auto = false, ?Carbon $time = null)
|
|
{
|
|
if ($package->isChecked()) {
|
|
throw new OrderPackageAlreadyCheckedException('包裹已签收');
|
|
}
|
|
|
|
$package->update([
|
|
'status'=> $auto ? OrderPackage::STATUS_AUTOCHECK : OrderPackage::STATUS_CHECK,
|
|
'checked_at' => $time ?: now(),
|
|
]);
|
|
|
|
// 更新商品的售后过期时间
|
|
$package->orderProducts()->update([
|
|
'after_expire_at'=> $package->checked_at->addDays(app_settings('app.sale_after_expire_days', 7)),
|
|
]);
|
|
}
|
|
}
|