|
|
@@ -15,8 +15,15 @@ class AgentService
|
|
|
return DB::transaction(function () use ($data, $createdBy, $scopeAgent): Agent {
|
|
|
$parentId = isset($data['parent_id']) ? (int) $data['parent_id'] : null;
|
|
|
if ($scopeAgent) {
|
|
|
- $parentId = $parentId ?: (int) $scopeAgent->id;
|
|
|
- $this->assertVisible($scopeAgent, $parentId);
|
|
|
+ if ((int) $scopeAgent->level !== Agent::LEVEL_ONE) {
|
|
|
+ throw new \RuntimeException('二级代理不能继续新增下级代理');
|
|
|
+ }
|
|
|
+ if ($parentId && $parentId !== (int) $scopeAgent->id) {
|
|
|
+ throw new \RuntimeException('一级代理只能新增自己的直属二级代理');
|
|
|
+ }
|
|
|
+ $parentId = (int) $scopeAgent->id;
|
|
|
+ } elseif ($parentId) {
|
|
|
+ throw new \RuntimeException('总后台只能新增一级代理');
|
|
|
}
|
|
|
$parent = $parentId ? Agent::query()->lockForUpdate()->findOrFail($parentId) : null;
|
|
|
if ($scopeAgent && $parent && (int) $parent->status !== Agent::STATUS_ENABLED) {
|
|
|
@@ -25,11 +32,15 @@ class AgentService
|
|
|
if ($parent) {
|
|
|
$this->assertRatesWithinParent($parent, $data);
|
|
|
}
|
|
|
+ $level = $parent ? ((int) $parent->level + 1) : Agent::LEVEL_ONE;
|
|
|
+ if ($level > Agent::MAX_LEVEL) {
|
|
|
+ throw new \RuntimeException('代理层级最多为二级');
|
|
|
+ }
|
|
|
|
|
|
$agent = Agent::query()->create([
|
|
|
'parent_id' => $parent?->id,
|
|
|
'path' => '/',
|
|
|
- 'level' => $parent ? ((int) $parent->level + 1) : 1,
|
|
|
+ 'level' => $level,
|
|
|
'username' => trim((string) $data['username']),
|
|
|
'password' => Hash::make((string) $data['password']),
|
|
|
'withdraw_password' => empty($data['withdraw_password']) ? '' : Hash::make((string) $data['withdraw_password']),
|
|
|
@@ -142,6 +153,9 @@ class AgentService
|
|
|
if ($parent && str_starts_with((string) $parent->path, (string) $agent->path)) {
|
|
|
throw new \RuntimeException('不能把代理移动到自己的下级');
|
|
|
}
|
|
|
+ if ($parent && (int) $parent->level !== Agent::LEVEL_ONE) {
|
|
|
+ throw new \RuntimeException('二级代理不能作为上级代理');
|
|
|
+ }
|
|
|
if ($parent) {
|
|
|
$this->assertRatesWithinParent($parent, [
|
|
|
'flow_commission_rate' => $agent->flow_commission_rate,
|
|
|
@@ -153,6 +167,12 @@ class AgentService
|
|
|
$newLevel = $parent ? ((int) $parent->level + 1) : 1;
|
|
|
$newPath = ($parent ? rtrim((string) $parent->path, '/') . '/' : '/') . $agent->id . '/';
|
|
|
$descendants = Agent::query()->where('path', 'like', rtrim($oldPath, '/') . '/%')->lockForUpdate()->get();
|
|
|
+ $newMaxLevel = $descendants->max(fn(Agent $descendant) =>
|
|
|
+ (int) $descendant->level + $newLevel - $oldLevel
|
|
|
+ ) ?? $newLevel;
|
|
|
+ if ($newLevel > Agent::MAX_LEVEL || $newMaxLevel > Agent::MAX_LEVEL) {
|
|
|
+ throw new \RuntimeException('移动后会产生三级代理,操作已拒绝');
|
|
|
+ }
|
|
|
|
|
|
$agent->parent_id = $parent?->id;
|
|
|
$agent->path = $newPath;
|