You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
2.8 KiB
103 lines
2.8 KiB
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Common\OrderStatus;
|
|
use App\Models\AgentProduct;
|
|
use App\Models\AgentProductSpec;
|
|
use App\Models\Order;
|
|
use App\Models\Product;
|
|
use App\Models\ProductSpec;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class OrderTimeout implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
protected $orderNumber = '';
|
|
protected $redis = '';
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct($orderNumber, $delay)
|
|
{
|
|
$this->orderNumber = $orderNumber;
|
|
$this->delay = $delay;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle()
|
|
{
|
|
//需要处理的订单状态
|
|
$status_arr = [OrderStatus::UNPAID, OrderStatus::PAY_EARNEST, OrderStatus::OFFLINE_UNPAID];
|
|
|
|
//记录最小ID,下次查询时按ID正序查询,只处理大于该ID的订单,避免重复扫描数据库
|
|
/*$min_id = Order::query()
|
|
->whereIn('status', $status_arr)
|
|
->whereNotNull('timeout')
|
|
->min('id');*/
|
|
$v = Order::with('orderProductItem')
|
|
// ->where('id', '>=', $min_id)
|
|
->whereIn('status', $status_arr)
|
|
->whereNotNull('timeout')
|
|
->where('order_no', $this->orderNumber)
|
|
->first();
|
|
if (!empty($v) && !is_null($v->timeout) && strtotime($v->timeout) <= time()) {
|
|
DB::beginTransaction();
|
|
try {
|
|
//取消订单
|
|
$v->status = OrderStatus::CANCEL;
|
|
$v->save();
|
|
|
|
//供应商产品加库存
|
|
Product::whereIn('id', explode(',', $v->product_ids))->increment('stock', $v->num);
|
|
|
|
//代理商产品加库存
|
|
AgentProduct::where('id', $v->agent_product_id)->increment('stock', $v->num);
|
|
|
|
if (!$v->orderProductItem->isEmpty()) {
|
|
$item = $v->orderProductItem->toArray();
|
|
|
|
//供应商产品规格加库存
|
|
foreach ($item as $v2) {
|
|
if ($v2['product_spec_id']) {
|
|
if (is_array($v2['product_spec_id'])) {
|
|
ProductSpec::whereIn('id', $v2['product_spec_id'])->increment('stock', $v->num);
|
|
} else {
|
|
ProductSpec::where('id', $v2['product_spec_id'])->increment('stock', $v->num);
|
|
}
|
|
}
|
|
}
|
|
|
|
//代理商产品规格加库存
|
|
foreach ($item as $v2) {
|
|
if ($v2['agent_product_spec_id']) {
|
|
if (is_array($v2['agent_product_spec_id'])) {
|
|
AgentProductSpec::whereIn('id', $v2['agent_product_spec_id'])->increment('stock', $v->num);
|
|
} else {
|
|
AgentProductSpec::where('id', $v2['agent_product_spec_id'])->increment('stock', $v->num);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
DB::commit();
|
|
} catch (\Exception $exception) {
|
|
DB::rollBack();
|
|
}
|
|
}
|
|
}
|
|
}
|