master
parent
a1c8705f12
commit
8575f7bd4c
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\OrderItems;
|
||||
use App\Models\Orders;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class CreateTodayOrderItems extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'order-items:create-daily';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Create today order items';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$threshold = 50;
|
||||
DB::enableQueryLog();
|
||||
|
||||
//获取正在进行中的订单,即使已经到了截止日,只要状态是在进行中的都继续生成
|
||||
$unGeneratedOrders = (new Orders())->whereIn("status", [Orders::STATUS_ONGOING])
|
||||
->whereRaw("DATEDIFF(`from_date`, now()) <= 0")
|
||||
->whereDoesntHave("orderItems", function ($query) {
|
||||
$query->whereRaw("DATEDIFF(`service_date`, now()) = 0");
|
||||
})
|
||||
->orderBy("id")
|
||||
->limit($threshold)
|
||||
->get();
|
||||
|
||||
foreach ($unGeneratedOrders as $order) {
|
||||
$service_date = date("Y-m-d");
|
||||
if (Carbon::parse($service_date)->greaterThan($order->to_date)) {
|
||||
DB::table((new Orders())->getTable())->where("id", $order->id)->update([
|
||||
"to_date" => $service_date,
|
||||
"updated_at" => date("Y-m-d H:i:s")
|
||||
]);
|
||||
}
|
||||
(new OrderItems())->createItem($order->id, $service_date);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue