diff --git a/app/Console/Commands/AdminSendCoupon.php b/app/Console/Commands/AdminSendCoupon.php index 22ce547d..f237a498 100644 --- a/app/Console/Commands/AdminSendCoupon.php +++ b/app/Console/Commands/AdminSendCoupon.php @@ -50,52 +50,55 @@ class AdminSendCoupon extends Command */ public function handle() { - //获取需要执行的任务数据包 - $logs = CouponTaskLog::where('status', '0')->where('num', '>', 0)->orderBy('id')->limit($this->limit)->get(); - $logs = $logs->groupBy('task_id'); - foreach ($logs as $taskId => $taskLogs) { - $task = CouponSendTask::find($taskId); - $coupon = Coupon::find($task->coupon_id); - //如果优惠券限量,则获取优惠券余量,只发送前面的人; - $failedLogs = collect([]); - if ($coupon->limit > 0 && $coupon->stock < ($taskLogs->count() * $task->num)) { - $failedLogs = $taskLogs->slice($coupon->stock); - $taskLogs = $taskLogs->slice(0, $coupon->stock); - } - - //批量插入用户优惠券 - $insertUserCoupons = []; - foreach ($taskLogs as $taskLog) { - for ($i=0; $i< $taskLog->num; $i++) { - $insertUserCoupons[] = CouponService::createUserCouponData($taskLog->user_id, $coupon); - } - } - try { - DB::beginTransaction(); - - count($insertUserCoupons) > 0 && UserCoupon::insert($insertUserCoupons); - //更新任务日志状态; - CouponTaskLog::whereIn('id', $taskLogs->pluck('id')->toArray())->update(['status'=>2]); - $failedLogs->count() == 0 || CouponTaskLog::whereIn('id', $failedLogs->pluck('id')->toArray())->update(['status'=>3, 'remarks'=>'库存余量不足']); - //更新任务对应券发送量,余量; - $coupon->increment('sent', $taskLogs->count()); - if ($coupon->limit > 0) {//限量才减少余量 - $coupon->decrement('stock', $taskLogs->count()); + while (true) { + //获取需要执行的任务数据包 + $logs = CouponTaskLog::where('status', '0')->where('num', '>', 0)->orderBy('id')->limit($this->limit)->get(); + $logs = $logs->groupBy('task_id'); + foreach ($logs as $taskId => $taskLogs) { + $task = CouponSendTask::find($taskId); + $coupon = Coupon::find($task->coupon_id); + //如果优惠券限量,则获取优惠券余量,只发送前面的人; + $failedLogs = collect([]); + if ($coupon->limit > 0 && $coupon->stock < ($taskLogs->count() * $task->num)) { + $failedLogs = $taskLogs->slice($coupon->stock); + $taskLogs = $taskLogs->slice(0, $coupon->stock); } - //更新如果是任务最后一批,更新任务状态; - $taskTotalnum = CouponTaskLog::where('task_id', $taskId)->where('num', '>', 0)->count(); - $taskDidnum = CouponTaskLog::where('task_id', $taskId)->where('num', '>', 0)->where('status', '>', 0)->count(); - if ($taskTotalnum == $taskDidnum) { - $task->update(['status' => 3]); + //批量插入用户优惠券 + $insertUserCoupons = []; + foreach ($taskLogs as $taskLog) { + for ($i=0; $i< $taskLog->num; $i++) { + $insertUserCoupons[] = CouponService::createUserCouponData($taskLog->user_id, $coupon); + } + } + try { + DB::beginTransaction(); + + count($insertUserCoupons) > 0 && UserCoupon::insert($insertUserCoupons); + //更新任务日志状态; + CouponTaskLog::whereIn('id', $taskLogs->pluck('id')->toArray())->update(['status'=>2]); + $failedLogs->count() == 0 || CouponTaskLog::whereIn('id', $failedLogs->pluck('id')->toArray())->update(['status'=>3, 'remarks'=>'库存余量不足']); + //更新任务对应券发送量,余量; + $coupon->increment('sent', $taskLogs->count()); + if ($coupon->limit > 0) {//限量才减少余量 + $coupon->decrement('stock', $taskLogs->count()); + } + + //更新如果是任务最后一批,更新任务状态; + $taskTotalnum = CouponTaskLog::where('task_id', $taskId)->where('num', '>', 0)->count(); + $taskDidnum = CouponTaskLog::where('task_id', $taskId)->where('num', '>', 0)->where('status', '>', 0)->count(); + if ($taskTotalnum == $taskDidnum) { + $task->update(['status' => 3]); + } + DB::commit(); + } catch (Throwable $th) { + DB::rollBack(); + report($th); } - DB::commit(); - } catch (Throwable $th) { - DB::rollBack(); - report($th); } + $this->info('执行成功'); + + sleep(60); } - $this->info('执行成功'); - return Command::SUCCESS; } } diff --git a/app/Console/Commands/Dealer/OrderSettleCommand.php b/app/Console/Commands/Dealer/OrderSettleCommand.php index 1eb5a4d1..709d4cf8 100644 --- a/app/Console/Commands/Dealer/OrderSettleCommand.php +++ b/app/Console/Commands/Dealer/OrderSettleCommand.php @@ -46,7 +46,7 @@ class OrderSettleCommand extends Command DealerOrder::where([ 'status' => DealerOrderStatus::Completed, 'settle_state' => DealerOrderSettleState::Processed, - ])->whereNotNull('shippinged_time')->chunkById(200, function ($orders, &$page) { + ])->whereNotNull('shippinged_time')->chunkById(200, function ($orders) use (&$page) { $orders->load('dealer.userInfo'); foreach ($orders as $order) { diff --git a/app/Console/Commands/OrderCloseExpiredCommand.php b/app/Console/Commands/OrderCloseExpiredCommand.php index b3c240b5..04b15f07 100644 --- a/app/Console/Commands/OrderCloseExpiredCommand.php +++ b/app/Console/Commands/OrderCloseExpiredCommand.php @@ -31,7 +31,7 @@ class OrderCloseExpiredCommand extends Command while (true) { $page = 0; - Order::select('id')->expired()->chunkById(1000, function ($orders, &$page) { + Order::select('id')->expired()->chunkById(1000, function ($orders) use (&$page) { Order::whereIn('id', $orders->pluck('id')->all())->where('status', Order::STATUS_PENDING)->update([ 'status' => Order::STATUS_CANCELLED, ]); @@ -44,7 +44,7 @@ class OrderCloseExpiredCommand extends Command } elseif ($page === 1) { sleep(30); } else { - sleep(60); + sleep(15); } } } diff --git a/app/Console/Commands/OrderCompleteCommand.php b/app/Console/Commands/OrderCompleteCommand.php index eb58d117..dd1ec224 100644 --- a/app/Console/Commands/OrderCompleteCommand.php +++ b/app/Console/Commands/OrderCompleteCommand.php @@ -29,10 +29,24 @@ class OrderCompleteCommand extends Command */ public function handle(OrderService $orderService) { - Order::with('packages')->completable()->chunkById(200, function ($orders) use ($orderService) { - foreach ($orders as $order) { - $orderService->confirm($order); + while (true) { + $page = 0; + + Order::with('packages')->completable()->chunkById(200, function ($orders) use ($orderService, &$page) { + foreach ($orders as $order) { + $orderService->confirm($order); + } + + $page++; + }); + + if ($page === 0) { + sleep(60); + } elseif ($page === 1) { + sleep(30); + } else { + sleep(15); } - }); + } } } diff --git a/app/Console/Commands/OrderRefundCommand.php b/app/Console/Commands/OrderRefundCommand.php index 0ae1c80e..877b8b44 100644 --- a/app/Console/Commands/OrderRefundCommand.php +++ b/app/Console/Commands/OrderRefundCommand.php @@ -37,28 +37,40 @@ class OrderRefundCommand extends Command */ public function handle() { - OrderRefundLog::pending()->chunkById(200, function ($logs) { - foreach ($logs as $log) { - try { - $method = 'refundBy'.Str::studly($log->order->pay_way); + while (true) { + $page = 0; - if (! method_exists($this, $method)) { - throw new BizException('退款方式暂不支持'); + OrderRefundLog::pending()->chunkById(200, function ($logs) use (&$page) { + foreach ($logs as $log) { + try { + $method = 'refundBy'.Str::studly($log->order->pay_way); + + if (! method_exists($this, $method)) { + throw new BizException('退款方式暂不支持'); + } + + $this->{$method}($log); + } catch (Throwable $e) { + report($e); + + $log->update([ + 'status' => OrderRefundLog::STATUS_FAILED, + 'failed_reason' => $e->getMessage(), + ]); } - - $this->{$method}($log); - } catch (Throwable $e) { - report($e); - - $log->update([ - 'status' => OrderRefundLog::STATUS_FAILED, - 'failed_reason' => $e->getMessage(), - ]); } - } - }); - return 0; + $page++; + }); + + if ($page === 0) { + sleep(60); + } elseif ($page === 1) { + sleep(30); + } else { + sleep(15); + } + } } /** diff --git a/app/Console/Commands/OrderSettleCommand.php b/app/Console/Commands/OrderSettleCommand.php index 29700887..efcbf55c 100644 --- a/app/Console/Commands/OrderSettleCommand.php +++ b/app/Console/Commands/OrderSettleCommand.php @@ -33,31 +33,43 @@ class OrderSettleCommand extends Command */ public function handle() { - // 只查询可结算的订单,并且没有处理中的售后单 - // 检查订单是否有未执行的分销任务 - Order::whereDoesntHave('afterSales', function ($query) { - return $query->processing(); - })->whereDoesntHave('distributionPreIncomeJobs', function ($query) { - return $query->pending(); - })->settlable()->chunkById(200, function ($orders) { - $orders->load(['user', 'afterSales']); + while (true) { + $page = 0; - foreach ($orders as $order) { - try { - DB::beginTransaction(); + // 只查询可结算的订单,并且没有处理中的售后单 + // 检查订单是否有未执行的分销任务 + Order::whereDoesntHave('afterSales', function ($query) { + return $query->processing(); + })->whereDoesntHave('distributionPreIncomeJobs', function ($query) { + return $query->pending(); + })->settlable()->chunkById(200, function ($orders) use (&$page) { + $orders->load(['user', 'afterSales']); - $this->settle($order); + foreach ($orders as $order) { + try { + DB::beginTransaction(); - DB::commit(); - } catch (Throwable $e) { - DB::rollBack(); + $this->settle($order); - report($e); + DB::commit(); + } catch (Throwable $e) { + DB::rollBack(); + + report($e); + } } - } - }); - return 0; + $page++; + }); + + if ($page === 0) { + sleep(60); + } elseif ($page === 1) { + sleep(30); + } else { + sleep(15); + } + } } /** diff --git a/app/Console/Commands/PushMessageCommand.php b/app/Console/Commands/PushMessageCommand.php index f03ab6df..5ca23273 100644 --- a/app/Console/Commands/PushMessageCommand.php +++ b/app/Console/Commands/PushMessageCommand.php @@ -34,36 +34,49 @@ class PushMessageCommand extends Command */ public function handle() { - PushMessageTask::with('message')->where('is_pushed', 0)->chunkById(100, function ($tasks) { - $mallPushService = new MallUnipushService(); - $merchantPushService = new MerchantUnipushService(); - foreach ($tasks as $task) { - try { - DB::beginTransaction(); - $status = 2; //默认失败 - //如果是系统消息 - if ($task->message instanceof Message) { - if ((bool) app_settings('unipush.is_use')) {//如果开启了推送 - $status = $mallPushService->push($task->sn, $task->message) ? 1 : 2; - } - } elseif ($task->message instanceof MerchantMessage) { - if ((bool) app_settings('unipush.merchant_is_use')) {//如果开启了推送 - $status = $merchantPushService->push($task->sn, $task->message) ? 1 : 2; + while (true) { + $page = 0; + + PushMessageTask::with('message')->where('is_pushed', 0)->chunkById(100, function ($tasks) use (&$page) { + $mallPushService = new MallUnipushService(); + $merchantPushService = new MerchantUnipushService(); + foreach ($tasks as $task) { + try { + DB::beginTransaction(); + $status = 2; //默认失败 + //如果是系统消息 + if ($task->message instanceof Message) { + if ((bool) app_settings('unipush.is_use')) {//如果开启了推送 + $status = $mallPushService->push($task->sn, $task->message) ? 1 : 2; + } + } elseif ($task->message instanceof MerchantMessage) { + if ((bool) app_settings('unipush.merchant_is_use')) {//如果开启了推送 + $status = $merchantPushService->push($task->sn, $task->message) ? 1 : 2; + } } + + $task->update([ + 'is_pushed'=> 1, + 'status'=> $status, + ]); + + DB::commit(); + } catch (Throwable $th) { + DB::rollBack(); + report($th); } - - $task->update([ - 'is_pushed'=> 1, - 'status'=> $status, - ]); - - DB::commit(); - } catch (Throwable $th) { - DB::rollBack(); - report($th); } + + $page++; + }); + + if ($page === 0) { + sleep(60); + } elseif ($page === 1) { + sleep(30); + } else { + sleep(15); } - }); - return Command::SUCCESS; + } } } diff --git a/worker.conf b/worker.conf new file mode 100644 index 00000000..d5bc6bfe --- /dev/null +++ b/worker.conf @@ -0,0 +1,111 @@ +# 【批零】自动分配订单 +[program:zcs-dealer-order-allocate] +process_name=%(program_name)s_%(process_num)02d +command=php /www/wwwroot/admin.zichunsheng.cn/current/artisan dealer:order-auto-allocate +autostart=true +autorestart=true +stopasgroup=true +killasgroup=true +user=deployer +numprocs=1 +redirect_stderr=true +stdout_logfile=/www/wwwroot/admin.zichunsheng.cn/current/storage/logs/dealer-order-allocate.log +stopwaitsecs=600 + +# 【批零】处理待结算的已付款订单 +[program:zcs-dealer-order-process] +process_name=%(program_name)s_%(process_num)02d +command=php /www/wwwroot/admin.zichunsheng.cn/current/artisan dealer:order-process +autostart=true +autorestart=true +stopasgroup=true +killasgroup=true +user=deployer +numprocs=1 +redirect_stderr=true +stdout_logfile=/www/wwwroot/admin.zichunsheng.cn/current/storage/logs/dealer-order-process.log +stopwaitsecs=900 + +# 【批零】结算已完成的订单 +[program:zcs-dealer-order-settle] +process_name=%(program_name)s_%(process_num)02d +command=php /www/wwwroot/admin.zichunsheng.cn/current/artisan dealer:order-settle +autostart=true +autorestart=true +stopasgroup=true +killasgroup=true +user=deployer +numprocs=1 +redirect_stderr=true +stdout_logfile=/www/wwwroot/admin.zichunsheng.cn/current/storage/logs/dealer-order-settle.log +stopwaitsecs=300 + +# 【商城】关闭过期订单 +[program:order-close-expired] +process_name=%(program_name)s_%(process_num)02d +command=php /www/wwwroot/admin.zichunsheng.cn/current/artisan order:close-expired +autostart=true +autorestart=true +stopasgroup=true +killasgroup=true +user=deployer +numprocs=1 +redirect_stderr=true +stdout_logfile=/www/wwwroot/admin.zichunsheng.cn/current/storage/logs/order-close.log +stopwaitsecs=300 + +# 【商城】订单自动完成 +[program:order-complete] +process_name=%(program_name)s_%(process_num)02d +command=php /www/wwwroot/admin.zichunsheng.cn/current/artisan order:complete +autostart=true +autorestart=true +stopasgroup=true +killasgroup=true +user=deployer +numprocs=1 +redirect_stderr=true +stdout_logfile=/www/wwwroot/admin.zichunsheng.cn/current/storage/logs/order-complete.log +stopwaitsecs=300 + +# 【商城】订单退款 +[program:order-refund] +process_name=%(program_name)s_%(process_num)02d +command=php /www/wwwroot/admin.zichunsheng.cn/current/artisan order:refund +autostart=true +autorestart=true +stopasgroup=true +killasgroup=true +user=deployer +numprocs=1 +redirect_stderr=true +stdout_logfile=/www/wwwroot/admin.zichunsheng.cn/current/storage/logs/order-refund.log +stopwaitsecs=300 + +# 【商城】订单结算 +[program:order-settle] +process_name=%(program_name)s_%(process_num)02d +command=php /www/wwwroot/admin.zichunsheng.cn/current/artisan order:settle +autostart=true +autorestart=true +stopasgroup=true +killasgroup=true +user=deployer +numprocs=1 +redirect_stderr=true +stdout_logfile=/www/wwwroot/admin.zichunsheng.cn/current/storage/logs/order-settle.log +stopwaitsecs=300 + +# 【商城】消息推送 +[program:message-push] +process_name=%(program_name)s_%(process_num)02d +command=php /www/wwwroot/admin.zichunsheng.cn/current/artisan message:push +autostart=true +autorestart=true +stopasgroup=true +killasgroup=true +user=deployer +numprocs=1 +redirect_stderr=true +stdout_logfile=/www/wwwroot/admin.zichunsheng.cn/current/storage/logs/message-push.log +stopwaitsecs=300