diff --git a/app/Console/Commands/OrderSettleCommand.php b/app/Console/Commands/OrderSettleCommand.php index d2ebb4f0..ebf08f26 100644 --- a/app/Console/Commands/OrderSettleCommand.php +++ b/app/Console/Commands/OrderSettleCommand.php @@ -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); } // 将订单标记未已结算 diff --git a/app/Models/SalesValueLog.php b/app/Models/SalesValueLog.php new file mode 100644 index 00000000..630a7ade --- /dev/null +++ b/app/Models/SalesValueLog.php @@ -0,0 +1,30 @@ + self::TYPE_INDIVIDUAL, + ]; + + /** + * @var array + */ + protected $fillable = [ + 'user_id', + 'order_id', + 'order_user_id', + 'type', + 'change_sales_value', + 'remarks', + ]; +} diff --git a/database/migrations/2021_12_30_113323_create_sales_value_logs_table.php b/database/migrations/2021_12_30_113323_create_sales_value_logs_table.php new file mode 100644 index 00000000..baf9eb83 --- /dev/null +++ b/database/migrations/2021_12_30_113323_create_sales_value_logs_table.php @@ -0,0 +1,39 @@ +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'); + } +}