agentSetUp(); Schema::table('users', function (Blueprint $t) { $t->integer('from')->default(1); $t->string('account')->default(''); $t->string('user_code')->nullable(); $t->string('agent_user_code')->nullable(); }); Schema::create('share_members', function (Blueprint $t) { $t->unsignedBigInteger('user_id')->primary(); $t->unsignedBigInteger('parent_user_id')->nullable(); $t->string('invitation_code', 100)->default(''); $t->string('binding_status', 24)->default('root'); $t->unsignedInteger('level')->default(1); $t->dateTime('bound_at'); $t->timestamps(); }); Schema::create('share_tree', function (Blueprint $t) { $t->unsignedBigInteger('ancestor_user_id'); $t->unsignedBigInteger('descendant_user_id'); $t->unsignedInteger('depth'); $t->dateTime('bound_at')->default('1970-01-01 00:00:00'); $t->primary(['ancestor_user_id', 'descendant_user_id']); }); Schema::create('share_accounts', function (Blueprint $t) { $t->unsignedBigInteger('user_id')->primary(); $t->timestamps(); }); DB::table('share_system_state')->insert(['key' => 'relations_lock', 'value' => '1']); } private function member(string $memberId, array $extra = []): int { $id = DB::table('users')->insertGetId($extra + ['member_id' => $memberId, 'username' => $memberId, 'account' => $memberId, 'user_code' => 'C' . $memberId]); if (($extra['from'] ?? 1) !== 2) { DB::table('share_members')->insert(['user_id' => $id, 'invitation_code' => 'C' . $memberId, 'bound_at' => '2026-09-01 00:00:00']); DB::table('share_tree')->insert(['ancestor_user_id' => $id, 'descendant_user_id' => $id, 'depth' => 0]); } return $id; } private function call(array $params): array { $request = Request::create('/admin/user/bindSuperior', 'POST', $params); $request->user = (object) ['id' => 1]; $this->app->instance('request', $request); return (new AdminUser())->bindSuperior(new MemberSuperiorService())->getData(true); } public function test_binds_agent_or_member_only_when_no_superior(): void { $agent = $this->agent(); $this->member('1001'); $b = $this->member('1002'); $c = $this->member('1003'); $this->member('1004', ['from' => 2]); $data = $this->call(['member_id' => '1001', 'type' => 'agent', 'superior_id' => (string) $agent->id]); $this->assertSame(0, $data['code'], (string) $data['msg']); $this->assertSame($agent->id, (int) DB::table('users')->where('member_id', '1001')->value('agent_id')); $this->assertSame('会员已有上级,不能修改', $this->call(['member_id' => '1001', 'type' => 'member', 'superior_id' => '1002'])['msg']); $data = $this->call(['member_id' => '1003', 'type' => 'member', 'superior_id' => '1002']); $this->assertSame(0, $data['code'], (string) $data['msg']); $this->assertSame('C1002', DB::table('users')->where('id', $c)->value('agent_user_code')); $this->assertSame($b, (int) DB::table('share_members')->where('user_id', $c)->value('parent_user_id')); $this->assertTrue(DB::table('share_tree')->where(['ancestor_user_id' => $b, 'descendant_user_id' => $c, 'depth' => 1])->exists()); $this->assertSame('会员已有上级,不能修改', $this->call(['member_id' => '1003', 'type' => 'agent', 'superior_id' => (string) $agent->id])['msg']); $this->assertSame('不能绑定自己或自己的下级', $this->call(['member_id' => '1002', 'type' => 'member', 'superior_id' => '1003'])['msg'], 'no cycle'); $this->assertSame('不能绑定自己', $this->call(['member_id' => '1002', 'type' => 'member', 'superior_id' => '1002'])['msg']); $this->assertSame('上级会员不存在', $this->call(['member_id' => '1002', 'type' => 'member', 'superior_id' => '9999'])['msg']); $this->assertSame('代理不存在', $this->call(['member_id' => '1002', 'type' => 'agent', 'superior_id' => '9999'])['msg']); $this->assertSame('试玩会员不能作为上级', $this->call(['member_id' => '1002', 'type' => 'member', 'superior_id' => '1004'])['msg']); $this->assertSame('试玩会员不能绑定上级', $this->call(['member_id' => '1004', 'type' => 'member', 'superior_id' => '1002'])['msg']); $this->assertNotSame(0, $this->call(['member_id' => '1002', 'type' => 'other', 'superior_id' => '1'])['code']); $equity = $this->agent(Agent::TYPE_EQUITY); $this->assertSame('只能绑定合营代理', $this->call(['member_id' => '1002', 'type' => 'agent', 'superior_id' => (string) $equity->id])['msg']); $this->member('1005', ['agent_id' => $equity->id]); $this->member('1006', ['agent_id' => $equity->id, 'agent_user_code' => 'GONE']); $data = $this->call(['member_id' => '1005', 'type' => 'agent', 'superior_id' => (string) $agent->id]); $this->assertSame(0, $data['code'], (string) $data['msg']); $this->assertSame($agent->id, (int) DB::table('users')->where('member_id', '1005')->value('agent_id')); $data = $this->call(['member_id' => '1006', 'type' => 'member', 'superior_id' => '1002']); $this->assertSame(0, $data['code'], (string) $data['msg']); $this->assertSame('C1002', DB::table('users')->where('member_id', '1006')->value('agent_user_code')); $this->assertNull(DB::table('users')->where('member_id', '1006')->value('agent_id')); Agent::query()->whereKey($agent->id)->update(['status' => Agent::STATUS_DISABLED]); $this->assertSame('代理已禁用', $this->call(['member_id' => '1002', 'type' => 'agent', 'superior_id' => (string) $agent->id])['msg']); $this->assertNull(DB::table('users')->where('id', $b)->value('agent_id')); } }