diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index a034680..b48cf40 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -31,12 +31,10 @@ class Kernel extends ConsoleKernel //$schedule->command('inspire')->hourly(); $schedule->command('order-items:create-daily')->everyMinute(); - //自动扣款 + //自动扣款,下午两点开始扣款,每小时可处理6000个子订单,每天可以处理60000个子订单 $schedule->call(function () { - (new OrderItems())->autoCheckout(true); - })->everyMinute() - ->timezone('Asia/Shanghai') - ->between('00:00', '21:59'); + (new OrderItems())->autoCheckout(); + })->everyMinute()->timezone('Asia/Shanghai')->between('14:00', '23:59'); //自动退款 $schedule->call(function () { diff --git a/app/Models/OrderItems.php b/app/Models/OrderItems.php index ab30477..c6ff949 100755 --- a/app/Models/OrderItems.php +++ b/app/Models/OrderItems.php @@ -5,6 +5,7 @@ namespace App\Models; use App\Customer; use Carbon\Carbon; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Log; class OrderItems extends SoftDeletesModel { @@ -136,11 +137,11 @@ class OrderItems extends SoftDeletesModel return $this; } - public function autoCheckout($before_today = false) + public function autoCheckout() { $threshold = 100; $last_id = cache("last_auto_checkout_order_item_id", 0); - dump("last_auto_checkout_order_item_id:" . $last_id); + $unpaid_order_items = $this ->whereHas("order", function ($query) { $query->where("status", Orders::STATUS_ONGOING); @@ -148,11 +149,6 @@ class OrderItems extends SoftDeletesModel ->whereNull("paid_at") ->where("id", ">", $last_id); - if ($before_today) { - $today = strtotime(date("Y-m-d")); - $unpaid_order_items->whereRaw("UNIX_TIMESTAMP(`service_date`) < {$today}"); - } - $unpaid_order_items = $unpaid_order_items ->orderBy("id") ->limit($threshold) @@ -162,6 +158,7 @@ class OrderItems extends SoftDeletesModel return; } + Log::channel("daily_auto_checkout")->info("Last id:" . $last_id); foreach ($unpaid_order_items as $item) { cache(['last_auto_checkout_order_item_id' => $item->id], now()->addSeconds(90)); $customer = $item->order->customer; @@ -189,6 +186,7 @@ class OrderItems extends SoftDeletesModel $item->order->refreshTotal(); } + Log::channel("daily_auto_checkout")->info("From " . $unpaid_order_items->first()->id . " to " . $unpaid_order_items->last()->id); } public function balance() diff --git a/config/logging.php b/config/logging.php index 088c204..9759d86 100644 --- a/config/logging.php +++ b/config/logging.php @@ -37,7 +37,7 @@ return [ 'channels' => [ 'stack' => [ 'driver' => 'stack', - 'channels' => ['single'], + 'channels' => ['daily'], 'ignore_exceptions' => false, ], @@ -47,6 +47,12 @@ return [ 'level' => 'debug', ], + 'daily_auto_checkout' => [ + 'driver' => 'daily', + 'path' => storage_path('logs/daily_auto_checkout.log'), + 'level' => 'debug', + ], + 'daily' => [ 'driver' => 'daily', 'path' => storage_path('logs/laravel.log'),