6
0
Fork 0

添加自动分配订单动作

release
vine_liutk 2022-01-17 15:42:41 +08:00 committed by 李静
parent 6585376e3a
commit fb1fc3160f
8 changed files with 173 additions and 9 deletions

View File

@ -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'] ?? [],

View File

@ -0,0 +1,57 @@
<?php
namespace App\Console\Commands\Dealer;
use App\Enums\DealerOrderStatus;
use App\Models\DealerOrder;
use App\Services\Dealer\OrderService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Throwable;
class OrderAutoAllocate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'dealer:order-auto-allocate';
/**
* The console command description.
*
* @var string
*/
protected $description = '自动分配订单';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
DealerOrder::where('status', DealerOrderStatus::Pending)
->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;
}
}

View File

@ -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()

View File

@ -0,0 +1,19 @@
<?php
namespace App\Models;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class DealerOrderAllocateLog extends Model
{
use HasFactory;
use HasDateTimeFormatter;
protected $fillable = [
'order_id',
'last_consignor_id',
'new_consignor_id',
];
}

View File

@ -6,6 +6,7 @@ use App\Enums\DealerLvl;
use App\Enums\DealerOrderStatus;
use App\Exceptions\BizException;
use App\Models\DealerOrder;
use App\Models\DealerOrderAllocateLog;
use App\Models\DealerProduct;
use App\Models\DealerUserProductLog;
use App\Models\ShippingAddress;
@ -86,6 +87,7 @@ class OrderService
$order->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 = [
[

View File

@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddAllocatedAtToDealerOrdersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('dealer_orders', function (Blueprint $table) {
//
$table->timestamp('allocated_at')->nullable()->comment('分配时间');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('dealer_orders', function (Blueprint $table) {
//
$table->dropColumn('allocated_at');
});
}
}

View File

@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateDealerOrderAllocateLogsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('dealer_order_allocate_logs', function (Blueprint $table) {
$table->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');
}
}

View File

@ -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'],