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.

75 lines
2.0 KiB

<?php
namespace App\Console\Commands;
use App\Models\OrderItems;
use App\Models\Orders;
use App\Models\Project;
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();
// 获取所有状态为1的项目
$projects = Project::where("status", 1)->pluck("id")->toArray();
//获取正在进行中的订单,即使已经到了截止日,只要状态是在进行中的都继续生成
$unGeneratedOrders = (new Orders())->whereIn("status", [Orders::STATUS_ONGOING])
->whereIn("project_id", $projects)
->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($order->to_date)->diffInDays($service_date, true) > 0) {
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;
}
}