6
0
Fork 0

销售值日志

release
李静 2021-12-30 13:53:32 +08:00
parent a4a321adfc
commit 6d1c71c209
3 changed files with 116 additions and 17 deletions

View File

@ -4,6 +4,7 @@ namespace App\Console\Commands;
use App\Models\DistributionPreIncome;
use App\Models\Order;
use App\Models\SalesValueLog;
use App\Models\UserInfo;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
@ -75,28 +76,57 @@ class OrderSettleCommand extends Command
$salesValue = bcsub($salesValue, $afterSale->sales_value, 2);
}
$user = $order->user;
if (bccomp($salesValue, '0', 2) === 1) {
$salesValueLogs = [];
$ts = now()->toDateTimeString();
// 结算下单用户的成长值
$user->userInfo()->update([
'growth_value' => DB::raw("growth_value+{$salesValue}"),
'pre_growth_value' => DB::raw("pre_growth_value-{$salesValue}"),
]);
// 尝试提升用户的代理等级
$user->userInfo->attemptUpgradeAgentLevel();
if (count($pids = $user->userInfo->parent_ids) > 0) {
// 更新上级的团队销售值
UserInfo::whereIn('user_id', $pids)->update([
'group_sales_value' => DB::raw("group_sales_value + {$salesValue}"),
// 更新下单用户的成长值和预成长值
$order->user->userInfo()->update([
'growth_value' => DB::raw("growth_value+{$salesValue}"),
'pre_growth_value' => DB::raw("pre_growth_value-{$salesValue}"),
]);
$ancestors = UserInfo::whereIn('user_id', $pids)->latest('depth')->get();
// 下单用户的销售值日志
$salesValueLogs[] = [
'user_id' => $order->user->id,
'order_id' => $order->id,
'order_user_id' => $order->user->id,
'type' => SalesValueLog::TYPE_INDIVIDUAL,
'change_sales_value' => $salesValue,
'remarks' => '个人消费',
'created_at' => $ts,
'updated_at' => $ts,
];
foreach ($ancestors as $ancestor) {
$ancestor->attemptUpgradeAgentLevel();
// 提升下单用户的代理等级
$order->user->userInfo->attemptUpgradeAgentLevel();
if (count($pids = $order->user->userInfo->parent_ids) > 0) {
$ancestors = UserInfo::whereIn('user_id', $pids)->latest('depth')->get();
foreach ($ancestors as $ancestor) {
$ancestor->attemptUpgradeAgentLevel();
$salesValueLogs[] = [
'user_id' => $ancestor->user_id,
'order_id' => $order->id,
'order_user_id' => $order->user_id,
'type' => SalesValueLog::TYPE_TEAM,
'change_sales_value' => $salesValue,
'remarks' => '团队成员消费',
'created_at' => $ts,
'updated_at' => $ts,
];
}
// 更新上级的团队销售值
UserInfo::whereIn('user_id', $pids)->update([
'group_sales_value' => DB::raw("group_sales_value + {$salesValue}"),
]);
}
// 保存销售值日志
SalesValueLog::insert($salesValueLogs);
}
// 将订单标记未已结算

View File

@ -0,0 +1,30 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class SalesValueLog extends Model
{
public const TYPE_INDIVIDUAL = 1;
public const TYPE_TEAM = 2;
/**
* @var array
*/
protected $attributes = [
'type' => self::TYPE_INDIVIDUAL,
];
/**
* @var array
*/
protected $fillable = [
'user_id',
'order_id',
'order_user_id',
'type',
'change_sales_value',
'remarks',
];
}

View File

@ -0,0 +1,39 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateSalesValueLogsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('sales_value_logs', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id')->comment('用户ID');
$table->unsignedBigInteger('order_id')->nullable()->comment('订单ID');
$table->unsignedBigInteger('order_user_id')->nullable()->comment('下单用户ID');
$table->tinyInteger('type')->default(1)->comment('类型: 1 个人, 2 团队');
$table->decimal('change_sales_value', 18, 2)->default(0.00)->comment('变更销售值');
$table->string('remarks')->nullable()->comment('备注');
$table->timestamps();
$table->index(['user_id', 'type']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('sales_value_logs');
}
}