Files
lotteryLaravel/app/Services/AgentSettlement/BetSettlementSnapshotBuilder.php
kang a44679665d feat: 增强代理和玩家管理功能
- 在多个控制器中更新权限检查逻辑,确保管理员能够更灵活地管理代理和玩家。
- 在 AdminPlayerStoreController 中引入对玩家创建能力的验证,确保只有具备相应权限的管理员能够创建玩家。
- 更新请求验证逻辑,新增 credit_limit、rebate_rate 和 extra_rebate_rate 字段,以支持更细粒度的玩家管理。
- 在 AgentNodeProfileController 中添加对父代理能力授予的验证,确保子代理的权限在父代理范围内。
- 引入 AgentProfileFieldRules 以简化代理资料更新请求的规则定义,提升代码复用性。
2026-06-04 18:00:50 +08:00

117 lines
3.6 KiB
PHP

<?php
namespace App\Services\AgentSettlement;
use App\Models\AgentNode;
use App\Models\AgentProfile;
use App\Models\Player;
use Illuminate\Support\Facades\DB;
final class BetSettlementSnapshotBuilder
{
/**
* @return array{
* agent_node_id: int,
* agent_path: list<int>,
* chain_codes: list<string>,
* total_shares: array<string, float>,
* actual_shares: array<string, float>,
* rebate_rate: float,
* extra_rebate_rate: float,
* }
*/
public function buildForPlayer(Player $player, string $gameType = '*'): array
{
$agentNodeId = (int) $player->agent_node_id;
if ($agentNodeId <= 0) {
throw new \InvalidArgumentException('player_missing_agent');
}
$pathIds = [];
$chainCodes = [];
$totalShares = [];
$nodeId = $agentNodeId;
while ($nodeId > 0) {
$node = AgentNode::query()->find($nodeId);
if ($node === null) {
break;
}
array_unshift($pathIds, (int) $node->id);
$profile = AgentProfile::query()->where('agent_node_id', $node->id)->first();
$code = (string) $node->code;
$chainCodes[] = $code;
$totalShares[$code] = (float) ($profile?->total_share_rate ?? 0);
$nodeId = (int) ($node->parent_id ?? 0);
}
$orderedBottomUp = $chainCodes;
$actual = $this->resolveActualShares($totalShares, $orderedBottomUp);
$rebate = $this->resolvePlayerRebateRate((int) $player->id, $agentNodeId, $gameType);
return [
'agent_node_id' => $agentNodeId,
'agent_path' => $pathIds,
'chain_codes' => $orderedBottomUp,
'total_shares' => $totalShares,
'actual_shares' => $actual,
'rebate_rate' => $rebate['rebate_rate'],
'extra_rebate_rate' => $rebate['extra_rebate_rate'],
];
}
/**
* @param array<string, float> $totalShares
* @param list<string> $orderedBottomUp
* @return array<string, float>
*/
private function resolveActualShares(array $totalShares, array $orderedBottomUp): array
{
$actual = [];
$prev = 0.0;
foreach ($orderedBottomUp as $code) {
$total = (float) ($totalShares[$code] ?? 0);
$actual[$code] = max(0, $total - $prev);
$prev = $total;
}
$actual['platform'] = max(0, 100 - $prev);
return $actual;
}
/**
* @return array{rebate_rate: float, extra_rebate_rate: float}
*/
private function resolvePlayerRebateRate(int $playerId, int $agentNodeId, string $gameType = '*'): array
{
$gameType = trim($gameType) !== '' ? trim($gameType) : '*';
$row = DB::table('player_rebate_profiles')
->where('player_id', $playerId)
->where('game_type', $gameType)
->first();
if ($row === null && $gameType !== '*') {
$row = DB::table('player_rebate_profiles')
->where('player_id', $playerId)
->where('game_type', '*')
->first();
}
if ($row !== null && ! (bool) $row->inherit_from_agent) {
return [
'rebate_rate' => (float) $row->rebate_rate,
'extra_rebate_rate' => (float) $row->extra_rebate_rate,
];
}
$profile = AgentProfile::query()->where('agent_node_id', $agentNodeId)->first();
return [
'rebate_rate' => (float) ($profile?->default_player_rebate ?? 0),
'extra_rebate_rate' => 0.0,
];
}
}