laravel command schedule 定时执行多任务
一、生成命令,同步erp运单号进入平台
php artisan make:command Sync_Erp_Express
<?php namespace App\Console\Commands; use App\Models\Erp\ProcurementContract; use App\Models\Prod\Order; use Illuminate\Console\Command; use Illuminate\Support\Facades\Log; class Sync_Erp_Express extends Command { /** * 同步erp运单号到撮合平台 * The name and signature of the console command. * * @var string */ protected $signature = 'SyncErpExpress'; /** * The console command description. * * @var string */ protected $description = 'Sync Erp Express'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed * 定时查询未发货的订单,通过订单号查询erp里是否发货 */ public function handle() { Log::info("start",['SyncErpExpress starting']); //查询平台所有未发货订单 $order = Order::where('status','=',1)->select('order_no')->get(); if($order->isEmpty()) return; $order_no_arr = []; foreach ($order as $k=>$v){ $order_no_arr[] = $v->order_no; } //dump($order_no_arr); //查询erp 平台采购信息,express_numbers运单号,更新平台信息 $res = ProcurementContract::whereIn('contract_number',$order_no_arr)->whereNotNull('express_numbers')->select('contract_number','express_numbers')->get(); if($res->isEmpty()) return; foreach ($res as $key=>$value){ if($value['express_numbers']){ Order::where('order_no','=',$value['contract_number'])->update(['express_num'=>$value['express_numbers'],'status'=>2,'is_send'=>1]); } } } }
二、生成命令,同步erp收款状态更新平台收款状态
php artisan make:command Sync_Erp_Pay
<?php namespace App\Console\Commands; use App\Models\Erp\ErpSalerContractDetail; use App\Models\Prod\Order; use Illuminate\Console\Command; use Illuminate\Support\Facades\Log; class Sync_Erp_Pay extends Command { /** * erp收到货款后,同步撮合平台 * The name and signature of the console command. * * @var string */ protected $signature = 'SyncErpPay'; /** * The console command description. * * @var string */ protected $description = 'Sync Erp Pay'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** *同步erp 支付信息到平台 * Execute the console command. * * @return mixed */ public function handle() { // Log::info("start",['SyncErpPay starting']); //查询平台所有未支付的订单 $order = Order::whereIn('status',[1,2])->where("is_pay","=",0)->select('order_no')->get()->toArray(); if(empty($order)) return; $order_no_arr = array_column($order, 'order_no'); //查询销售合同saler_contract_details,receipt_status 3全部收款 4 超额收款 $res = ErpSalerContractDetail::whereIn('contract_number',$order_no_arr)->whereIn('receipt_status',[3,4])->select('contract_number','receipt_status')->get(); if($res->isEmpty()) return; foreach ($res as $key=>$value){ Order::where('order_no','=',$value['contract_number'])->update(['is_pay'=>1]); } } }
三、App\Console\Kernel类中定义调度任务
<?php namespace App\Console; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; class Kernel extends ConsoleKernel { /** * The Artisan commands provided by your application. * * @var array */ protected $commands = [ //命令定义 Commands\Sync_Erp_Express::class, Commands\Sync_Erp_Pay::class, ]; /** * Define the application's command schedule. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void */ protected function schedule(Schedule $schedule) { // $schedule->command('inspire') // ->hourly(); //每30分钟同步运单号信息 $schedule->command('SyncErpExpress')->everyThirtyMinutes(); //每15分钟同步付款状态 $schedule->command('SyncErpPay')->everyFifteenMinutes(); } /** * Register the commands for the application. * * @return void */ protected function commands() { $this->load(__DIR__.'/Commands'); require base_path('routes/console.php'); } }
注释:
->everyMinute(); 每分钟运行一次任务
->everyFiveMinutes(); 每五分钟运行一次任务
->everyTenMinutes(); 每十分钟运行一次任务
->everyThirtyMinutes(); 每三十分钟运行一次任务
->hourly(); 每小时运行一次任务
->daily(); 每天凌晨零点运行任务
->dailyAt(‘13:00’); 每天13:00运行任务
->twiceDaily(1, 13); 每天1:00 & 13:00运行任务
->weekly(); 每周运行一次任务
->monthly(); 每月运行一次任务
四、crontab 执行定时任务
* * * * * php /var/www/html/artisan schedule:run
总结通过schedule 可以添加调用多任务,但是crontab 只需要执行一个任务
如果分别调用 php artisan SyncErpExpress,php artisan SyncErpPay 则需要起多个crontab,比较麻烦