Files
lotteryLaravel/app/Services/AgentSettlement/SettlementPartyEnrichment.php
kang 2d32f006c5 feat: 增强代理结算和账单管理功能
- 在多个控制器中引入 SettlementPartyEnrichment 服务,以优化代理结算和账单的处理逻辑。
- 更新 AgentSettlementBillIndexController 和 AgentSettlementBillShowController,支持根据账单 ID 和关键字进行查询。
- 在 AgentSettlementPeriodCloseController 中添加对站点管理权限的验证,确保只有具备相应权限的管理员能够关闭账期。
- 在 AgentSettlementPeriodIndexController 中更新账期数据的返回格式,提升数据的完整性和可用性。
- 引入对相对占成比例的支持,增强代理资料的管理能力,确保数据一致性。
2026-06-05 18:00:56 +08:00

154 lines
4.8 KiB
PHP

<?php
namespace App\Services\AgentSettlement;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
/** 结算列表:玩家 / 代理 / 注单关联字段 enrichment。 */
final class SettlementPartyEnrichment
{
/**
* @param Collection<int, object> $agents keyed by id
* @return array{
* direct_agent_id: int|null,
* direct_agent_label: string|null,
* parent_agent_id: int|null,
* parent_agent_label: string|null,
* }
*/
public function agentLineLabels(?int $directAgentId, Collection $agents): array
{
if ($directAgentId === null || $directAgentId <= 0) {
return [
'direct_agent_id' => null,
'direct_agent_label' => null,
'parent_agent_id' => null,
'parent_agent_label' => null,
];
}
$direct = $agents->get($directAgentId);
$directLabel = $this->formatAgent($direct, $directAgentId);
$parentId = $direct !== null ? (int) ($direct->parent_id ?? 0) : 0;
$parent = $parentId > 0 ? $agents->get($parentId) : null;
return [
'direct_agent_id' => $directAgentId,
'direct_agent_label' => $directLabel,
'parent_agent_id' => $parentId > 0 ? $parentId : null,
'parent_agent_label' => $parentId > 0 ? $this->formatAgent($parent, $parentId) : null,
];
}
/**
* @param list<int> $agentIds
* @return Collection<int, object>
*/
public function loadAgents(array $agentIds): Collection
{
$ids = array_values(array_unique(array_filter($agentIds, static fn (int $id): bool => $id > 0)));
if ($ids === []) {
return collect();
}
$rows = DB::table('agent_nodes')->whereIn('id', $ids)->get()->keyBy('id');
$parentIds = $rows
->pluck('parent_id')
->map(static fn ($id): int => (int) $id)
->filter(static fn (int $id): bool => $id > 0)
->unique()
->values()
->all();
$missingParents = array_diff($parentIds, $ids);
if ($missingParents !== []) {
$parents = DB::table('agent_nodes')->whereIn('id', $missingParents)->get()->keyBy('id');
foreach ($parents as $id => $row) {
$rows->put((int) $id, $row);
}
}
return $rows;
}
/**
* @param list<int> $ticketItemIds
* @return array<int, array{play_code: string|null, draw_no: string|null, ticket_item_id: int, actual_deduct_amount: int}>
*/
public function loadTicketRefs(array $ticketItemIds): array
{
$ids = array_values(array_unique(array_filter($ticketItemIds, static fn (int $id): bool => $id > 0)));
if ($ids === []) {
return [];
}
$map = [];
foreach (DB::table('ticket_items as ti')
->leftJoin('draws as d', 'd.id', '=', 'ti.draw_id')
->whereIn('ti.id', $ids)
->select(['ti.id', 'ti.play_code', 'ti.actual_deduct_amount', 'd.draw_no'])
->get() as $row) {
$map[(int) $row->id] = [
'ticket_item_id' => (int) $row->id,
'play_code' => $row->play_code !== null ? (string) $row->play_code : null,
'draw_no' => $row->draw_no !== null ? (string) $row->draw_no : null,
'actual_deduct_amount' => (int) ($row->actual_deduct_amount ?? 0),
];
}
return $map;
}
public function formatAgent(?object $agent, int $fallbackId): string
{
if ($agent === null) {
return "agent#{$fallbackId}";
}
$name = trim((string) ($agent->name ?? ''));
$code = trim((string) ($agent->code ?? ''));
if ($name !== '' && $code !== '') {
return "{$name} ({$code})";
}
return $name !== '' ? $name : ($code !== '' ? $code : "agent#{$fallbackId}");
}
public function formatPlayerUsername(?object $player): ?string
{
if ($player === null) {
return null;
}
$username = trim((string) ($player->username ?? ''));
return $username !== '' ? $username : null;
}
public function formatPlayerSiteId(?object $player): ?string
{
if ($player === null) {
return null;
}
$sitePlayerId = trim((string) ($player->site_player_id ?? ''));
return $sitePlayerId !== '' ? $sitePlayerId : null;
}
public function formatCounterpartyLabel(string $type, int $id, Collection $agents): string
{
if ($type === 'platform' || $id <= 0) {
return 'platform';
}
if ($type === 'agent') {
return $this->formatAgent($agents->get($id), $id);
}
return "{$type}#{$id}";
}
}