生成管理者的商品销售业绩
parent
65997b6956
commit
d95d529234
|
|
@ -0,0 +1,125 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Commands\Dealer;
|
||||
|
||||
use App\Models\Dealer;
|
||||
use App\Models\DealerManagerSalesLog;
|
||||
use App\Models\DealerOrder;
|
||||
use App\Models\UserInfo;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Throwable;
|
||||
|
||||
class OrderProcessCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'dealer:order-process';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = '处理结算状态是待处理的已付款经销商订单';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
DealerOrder::chunkById(200, function ($orders) {
|
||||
$orders->load(['userInfo.dealer', 'products']);
|
||||
|
||||
foreach ($orders as $order) {
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
$this->processOrder($order);
|
||||
|
||||
DB::commit();
|
||||
} catch (Throwable $e) {
|
||||
DB::rollBack();
|
||||
|
||||
report($e);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理经销商订单
|
||||
*
|
||||
* @param \App\Models\DealerOrder $order
|
||||
* @return void
|
||||
*/
|
||||
protected function processOrder(DealerOrder $order)
|
||||
{
|
||||
$tz = now();
|
||||
|
||||
$dealers = $this->getDealers($userInfo = $order->userInfo);
|
||||
|
||||
// 如果当前用户的上级有管理者,则需要增加管理者的商品销售业绩
|
||||
if ($manager = $this->firstManager([$userInfo->dealer, ...$dealers])) {
|
||||
DealerManagerSalesLog::insert(
|
||||
$order->products->map(function ($product) use ($manager, $tz) {
|
||||
return [
|
||||
'user_id' => $manager->user_id,
|
||||
'order_id' => $product->order_id,
|
||||
'product_id' => $product->product_id,
|
||||
'lvl' => $manager->lvl,
|
||||
'sales_volume' => $product->qty,
|
||||
'created_at' => $tz,
|
||||
'updated_at' => $tz,
|
||||
];
|
||||
})->all()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从给定的经销商中获取第一个管理者
|
||||
*
|
||||
* @param array $dealers
|
||||
* @return \App\Models\Dealer|null
|
||||
*/
|
||||
protected function firstManager(array $dealers): ?Dealer
|
||||
{
|
||||
foreach ($dealers as $dealer) {
|
||||
if ($dealer->is_manager) {
|
||||
return $dealer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取给定用户的上级经销商
|
||||
*
|
||||
* @param \App\Models\UserInfo $userInfo
|
||||
* @return array
|
||||
*/
|
||||
protected function getDealers(UserInfo $userInfo)
|
||||
{
|
||||
$ancestors = [];
|
||||
|
||||
if (empty($pids = $userInfo->parent_ids)) {
|
||||
return $ancestors;
|
||||
}
|
||||
|
||||
$ancestors = UserInfo::with(['dealer'])
|
||||
->whereIn('user_id', $pids)
|
||||
->latest('depth')
|
||||
->get(['user_id', 'depth']);
|
||||
|
||||
return $ancestors->map(function ($item) {
|
||||
return $item->dealer;
|
||||
})->all();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
enum DealerLvl: int {
|
||||
case None = 0;
|
||||
case Gold = 2;
|
||||
case Special = 3;
|
||||
case Contracted = 4;
|
||||
case Secondary = 5;
|
||||
case Top = 6;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function text(): string
|
||||
{
|
||||
return match ($this) {
|
||||
static::None => '普通用户',
|
||||
static::Gold => '金牌经销商',
|
||||
static::Special => '特邀经销商',
|
||||
static::Contracted => '签约经销商',
|
||||
static::Secondary => '二级经销商',
|
||||
static::Top => '一级经销商',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\DealerLvl;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Dealer extends Model
|
||||
{
|
||||
protected $attributes = [
|
||||
'lvl' => DealerLvl::None,
|
||||
'is_sale' => false,
|
||||
'is_manager' => false,
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'lvl' => DealerLvl::class,
|
||||
'is_sale' => 'bool',
|
||||
'is_manager' => 'bool',
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'lvl',
|
||||
'is_sale',
|
||||
'is_manager',
|
||||
];
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\DealerLvl;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* 管理者的团队的商品销售业绩
|
||||
*/
|
||||
class DealerManagerSalesLog extends Model
|
||||
{
|
||||
protected $casts = [
|
||||
'lvl' => DealerLvl::class,
|
||||
'order_completed_at' => 'datetime',
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'order_id',
|
||||
'product_id',
|
||||
'lvl',
|
||||
'sales_volume',
|
||||
'order_completed_at',
|
||||
];
|
||||
}
|
||||
|
|
@ -17,4 +17,20 @@ class DealerOrder extends Model
|
|||
'status' => DealerOrderStatus::class,
|
||||
'settle_state' => DealerOrderSettleState::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* 此订单所属的用户的信息
|
||||
*/
|
||||
public function userInfo()
|
||||
{
|
||||
return $this->belongsTo(UserInfo::class, 'user_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 属于此订单的商品
|
||||
*/
|
||||
public function products()
|
||||
{
|
||||
return $this->hasMany(DealerOrderProduct::class, 'order_id');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -175,6 +175,14 @@ class UserInfo extends Model
|
|||
return $this->hasMany(AgentUpgradeLog::class, 'user_id', 'user_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 属于此用户的经销商信息
|
||||
*/
|
||||
public function dealer()
|
||||
{
|
||||
return $this->hasOne(Dealer::class, 'user_id', 'user_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取此用户的直推店铺总数
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateDealersTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('dealers', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('user_id')->unique()->comment('用户ID');
|
||||
$table->tinyInteger('lvl')->default(0)->comment('经销商等级');
|
||||
$table->boolean('is_sale')->default(false)->comment('是否可销售');
|
||||
$table->boolean('is_manager')->default(false)->comment('是否是管理者');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('dealers');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateDealerManagerSalesLogsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('dealer_manager_sales_logs', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('user_id')->comment('经销商的用户ID');
|
||||
$table->unsignedBigInteger('order_id')->comment('订单ID');
|
||||
$table->unsignedBigInteger('product_id')->comment('商品ID');
|
||||
$table->tinyInteger('lvl')->comment('经销商等级');
|
||||
$table->unsignedBigInteger('sales_volume')->default(0)->comment('商品销量');
|
||||
$table->timestamp('order_completed_at')->nullable()->comment('订单完成时间');
|
||||
$table->timestamps();
|
||||
|
||||
$table->index('user_id');
|
||||
$table->index('order_id');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('dealer_manager_sales_logs');
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue