diff --git a/app/Admin/Forms/Settings/Dealer.php b/app/Admin/Forms/Settings/Dealer.php index 02b1b5d9..b9ec39fd 100644 --- a/app/Admin/Forms/Settings/Dealer.php +++ b/app/Admin/Forms/Settings/Dealer.php @@ -41,6 +41,7 @@ class Dealer extends Form $this->currency('upgrade_amount_'.DealerLvl::Contracted->value, '签约门槛')->symbol('¥'); $this->currency('upgrade_amount_'.DealerLvl::Special->value, '特邀门槛')->symbol('¥'); $this->currency('upgrade_amount_'.DealerLvl::Gold->value, '金牌门槛')->symbol('¥'); + $this->number('order_auto_allocate_times', '订单自动分配时间(小时)')->min(1); $this->table('purchase_rules', '进货补贴规则', function ($table) { $table->number('price', '阶梯(万)'); $table->currency('rate', '比例(百分比)')->symbol('%'); @@ -84,9 +85,10 @@ class Dealer extends Form $dealerSettings = (array) Setting::where('key', 'dealer')->value('value'); return [ 'fee_rate'=>$dealerSettings['fee_rate'] ?? '', - 'upgrade_amount_'.DealerLvl::Contracted->value => $dealerSettings['upgrade_amount_'.DealerLvl::Contracted->value]??'', - 'upgrade_amount_'.DealerLvl::Special->value => $dealerSettings['upgrade_amount_'.DealerLvl::Special->value]??'', - 'upgrade_amount_'.DealerLvl::Gold->value => $dealerSettings['upgrade_amount_'.DealerLvl::Gold->value]??'', + 'order_auto_allocate_times'=>$dealerSettings['order_auto_allocate_times'] ?? 1, + 'upgrade_amount_'.DealerLvl::Contracted->value => $dealerSettings['upgrade_amount_'.DealerLvl::Contracted->value] ?? '', + 'upgrade_amount_'.DealerLvl::Special->value => $dealerSettings['upgrade_amount_'.DealerLvl::Special->value] ?? '', + 'upgrade_amount_'.DealerLvl::Gold->value => $dealerSettings['upgrade_amount_'.DealerLvl::Gold->value] ?? '', 'bank'=>$dealerSettings['bank'] ?? [], 'alipay'=>$dealerSettings['alipay'] ?? [], 'wechat' =>$dealerSettings['wechat'] ?? [], diff --git a/app/Console/Commands/Dealer/OrderAutoAllocate.php b/app/Console/Commands/Dealer/OrderAutoAllocate.php new file mode 100644 index 00000000..b23603f1 --- /dev/null +++ b/app/Console/Commands/Dealer/OrderAutoAllocate.php @@ -0,0 +1,57 @@ +where('consignor_id', '>', 1)//到1用户或者公司的订单不需要再分配 + ->where('allocated_at', '<', now()->subHours(app_settings('dealer.order_auto_allocate_times'))) + ->chunkById(200, function ($orders) { + $orders->load([ + 'consignor', + ]); + $orderService = new OrderService(); + foreach ($orders as $order) { + try { + DB::beginTransaction(); + $orderService->updateOrderConsignor($order); + DB::commit(); + } catch (Throwable $th) { + DB::rollBack(); + report($th); + } + } + }); + + return 0; + } +} diff --git a/app/Models/DealerOrder.php b/app/Models/DealerOrder.php index 93b4c788..2e13b390 100644 --- a/app/Models/DealerOrder.php +++ b/app/Models/DealerOrder.php @@ -27,6 +27,7 @@ class DealerOrder extends Model 'paied_time'=>'datetime', 'shipping_time'=>'datetime', 'shippinged_time'=>'datetime', + 'allocated_at'=>'datetime', ]; protected $fillable = [ @@ -43,6 +44,7 @@ class DealerOrder extends Model 'paied_time', 'shipping_time', 'shippinged_time', + 'allocated_at', //分配时间 ]; public function getOrderStatusAttribute() diff --git a/app/Models/DealerOrderAllocateLog.php b/app/Models/DealerOrderAllocateLog.php new file mode 100644 index 00000000..45f72ee1 --- /dev/null +++ b/app/Models/DealerOrderAllocateLog.php @@ -0,0 +1,19 @@ +consignee_telephone = $shippingAddress->telephone; $order->consignee_zone = $shippingAddress->zone; $order->consignee_address = $shippingAddress->address; + $order->allocated_at = now();//记录分配时间 $order->save(); break; } catch (QueryException $e) { @@ -216,12 +218,23 @@ class OrderService * * @return void */ - protected function updateOrderConsignor(DealerOrder $order) + public function updateOrderConsignor(DealerOrder $order) { - $consignor = $this->getConsignor($order->user, $order->consignor); - $order->update([ - 'consignor_id' => $consignor?->user_id, - ]); + //只处理当前订单有发货人的情况 + if ($order->consignor) { + $consignor = $this->getConsignor($order->user, $order->totalAmount, $order->consignor); + $oldConsignor = $order->consignor; + $order->update([ + 'allocated_at' => now(), + 'consignor_id' => $consignor?->user_id, + ]); + //记录分配日志 + DealerOrderAllocateLog::create([ + 'order_id'=>$order->id, + 'last_consignor_id'=>$oldConsignor->id, + 'new_consignor_id' =>$order->consignor_id, + ]); + } } /** @@ -288,7 +301,7 @@ class OrderService } } - public function getConsignor(User $user, $totalAmount, ?User $lastConsignor = null) + private function getConsignor(User $user, $totalAmount, ?User $lastConsignor = null) { $rules = [ [ diff --git a/database/migrations/2022_01_17_145248_add_allocated_at_to_dealer_orders_table.php b/database/migrations/2022_01_17_145248_add_allocated_at_to_dealer_orders_table.php new file mode 100644 index 00000000..97b31d72 --- /dev/null +++ b/database/migrations/2022_01_17_145248_add_allocated_at_to_dealer_orders_table.php @@ -0,0 +1,34 @@ +timestamp('allocated_at')->nullable()->comment('分配时间'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('dealer_orders', function (Blueprint $table) { + // + $table->dropColumn('allocated_at'); + }); + } +} diff --git a/database/migrations/2022_01_17_145556_create_dealer_order_allocate_logs_table.php b/database/migrations/2022_01_17_145556_create_dealer_order_allocate_logs_table.php new file mode 100644 index 00000000..8dd6fcb7 --- /dev/null +++ b/database/migrations/2022_01_17_145556_create_dealer_order_allocate_logs_table.php @@ -0,0 +1,34 @@ +id(); + $table->unsignedBigInteger('order_id')->comment('订单ID'); + $table->unsignedBigInteger('last_consignor_id')->comment('上个接单人'); + $table->unsignedBigInteger('new_consignor_id')->nullable()->comment('新接单人'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('dealer_order_allocate_logs'); + } +} diff --git a/database/seeders/AppSettingSeeder.php b/database/seeders/AppSettingSeeder.php index c5a383ae..13e6167b 100644 --- a/database/seeders/AppSettingSeeder.php +++ b/database/seeders/AppSettingSeeder.php @@ -185,6 +185,9 @@ class AppSettingSeeder extends Seeder // 合约经销商升级金额 'upgrade_amount_'.DealerLvl::Contracted->value => '26400', + //超过N小时未接单自动分配 + 'order_auto_allocate_times'=>'1', + 'purchase_rules'=>[ ['price' => '1', 'rate' => '2.00'], ['price' => '2', 'rate' => '3.00'],