- 扩大玩家ID随机数位宽降低碰撞概率 - 授信额度变更加 lockForUpdate 防并发下调 - 结算冲正与入账利用唯一索引实现幂等闸门 - 失败登录计数加锁防丢失 - 代理子树查询改用子查询替代全表 pluck - 修复返点默认值百分比单位转换 - 测试中全量 fake 广播事件防 CI 500
This commit is contained in:
@@ -173,6 +173,10 @@ final class AdminAgentScope
|
||||
/**
|
||||
* 玩家必须落在当前代理子树(agent_node_id 必填,由迁移回填根代理)。
|
||||
*
|
||||
* 性能优化:原实现先 pluck('id') 全表扫代理树再 whereIn,N+1 + 中间数组大。
|
||||
* 改用子查询(SELECT id FROM agent_nodes WHERE path LIKE ?),单次 SQL,
|
||||
* 走 idx_agent_nodes_path 索引。
|
||||
*
|
||||
* @param Builder<Player> $query
|
||||
*/
|
||||
public static function applyToPlayerQuery(Builder $query, AdminUser $admin): void
|
||||
@@ -194,18 +198,8 @@ final class AdminAgentScope
|
||||
return;
|
||||
}
|
||||
|
||||
$subtreeIds = AgentNode::query()
|
||||
->where('path', 'like', $actor->path.'%')
|
||||
->pluck('id')
|
||||
->all();
|
||||
|
||||
if ($subtreeIds === []) {
|
||||
$query->whereRaw('0 = 1');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$query->whereIn('agent_node_id', $subtreeIds);
|
||||
// 委托给统一子树过滤:先 count 一次(带 LIMIT 1 优化)确认子树非空,再用子查询展开。
|
||||
self::applySubtreeFilter($query, 'agent_node_id', (string) $actor->path);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -226,17 +220,28 @@ final class AdminAgentScope
|
||||
return;
|
||||
}
|
||||
|
||||
$subtreeIds = AgentNode::query()
|
||||
->where('path', 'like', $node->path.'%')
|
||||
->pluck('id')
|
||||
->all();
|
||||
self::applySubtreeFilter($query, 'agent_node_id', (string) $node->path);
|
||||
}
|
||||
|
||||
if ($subtreeIds === []) {
|
||||
$query->whereRaw('0 = 1');
|
||||
/**
|
||||
* 按 path 前缀对指定列应用代理子树过滤。
|
||||
*
|
||||
* 用子查询 `IN (SELECT id FROM agent_nodes WHERE path LIKE ?)` 替代
|
||||
* 「全表 pluck 再 whereIn」:单次 SQL 走 idx_agent_nodes_path 索引,
|
||||
* 避免 N+1 与中间大数组。子查询天然短路空子树(返回空集 → IN 永不命中)。
|
||||
*
|
||||
* 注意 $path / $column 由调用方控制且为已知安全值,不走用户输入;
|
||||
* LIKE 绑定值仍用参数化。path 中 % / _ / \ 也已做转义,防止 LIKE 元字符注入。
|
||||
*
|
||||
* @param Builder<Player> $query
|
||||
*/
|
||||
private static function applySubtreeFilter(Builder $query, string $column, string $path): void
|
||||
{
|
||||
$escaped = str_replace(['\\', '%', '_'], ['\\\\', '\\%', '\\_'], $path);
|
||||
$prefix = $escaped.'%';
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$query->whereIn('agent_node_id', $subtreeIds);
|
||||
$query->whereIn($column, function ($subQuery) use ($prefix): void {
|
||||
$subQuery->from('agent_nodes')->select('id')->whereRaw('path LIKE ?', [$prefix]);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user