Agent.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Http\Controllers\AgentApiController;
  4. use App\Models\Agent\Agent as AgentModel;
  5. use App\Models\Agent\AgentLoginLog;
  6. use App\Models\Agent\AgentWithdrawal;
  7. use App\Models\Agent\AgentRebateRecord;
  8. use App\Models\EgameItem;
  9. use App\Models\User;
  10. use App\Services\Agent\AgentDomainService;
  11. use App\Services\Agent\AgentReportService;
  12. use App\Services\Agent\AgentService;
  13. use App\Services\Agent\AgentWalletService;
  14. use App\Services\Agent\AgentWithdrawalService;
  15. use App\Services\Agent\AgentFlowCommissionProfileService;
  16. use App\Services\Agent\AgentProfitCommissionProfileService;
  17. use Illuminate\Http\Request;
  18. use Illuminate\Validation\Rule;
  19. use Illuminate\Support\Facades\DB;
  20. use App\Rules\DecimalNumber;
  21. use App\Constants\Util;
  22. use App\Services\ThirdGameBalanceService;
  23. class Agent extends AgentApiController
  24. {
  25. public function index(Request $request, AgentDomainService $domains)
  26. {
  27. return $this->execute(function () use ($request, $domains) {
  28. $data = $request->validate([
  29. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  30. 'username' => ['nullable', 'string', 'max:64'], 'real_name' => ['nullable', 'string', 'max:128'],
  31. 'parent_id' => ['nullable', 'integer'], 'status' => ['nullable', 'integer', 'in:0,1'],
  32. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  33. ]);
  34. $page = max(1, (int) ($data['page'] ?? 1));
  35. $limit = min(100, max(1, (int) ($data['limit'] ?? 20)));
  36. $query = AgentModel::query()->with(['parent:id,username', 'token:id,agent_id,expires_at,last_used_at', 'flowCommissionProfile', 'profitCommissionProfile'])->withCount(['children', 'members']);
  37. if (!empty($data['username'])) $query->where('username', 'like', '%' . $data['username'] . '%');
  38. if (!empty($data['real_name'])) $query->where('real_name', 'like', '%' . $data['real_name'] . '%');
  39. if (!empty($data['parent_id'])) $query->where('parent_id', $data['parent_id']);
  40. if (isset($data['status'])) $query->where('status', $data['status']);
  41. if (!empty($data['start_date'])) $query->where('created_at', '>=', $data['start_date'] . ' 00:00:00');
  42. if (!empty($data['end_date'])) $query->where('created_at', '<=', $data['end_date'] . ' 23:59:59');
  43. $total = (clone $query)->count();
  44. $list = $query->orderByDesc('id')->forPage($page, $limit)->get()->map(fn(AgentModel $agent) => $this->formatAgent($agent, $domains));
  45. return compact('total', 'page', 'limit', 'list');
  46. });
  47. }
  48. public function show(Request $request, AgentDomainService $domains)
  49. {
  50. return $this->execute(function () use ($request, $domains) {
  51. $data = $request->validate(['id' => ['required', 'integer', 'exists:agents,id']]);
  52. $agent = AgentModel::query()->with(['parent:id,username', 'token:id,agent_id,expires_at,last_used_at', 'flowCommissionProfile', 'profitCommissionProfile'])->withCount(['children', 'members'])->findOrFail($data['id']);
  53. return $this->formatAgent($agent, $domains);
  54. });
  55. }
  56. public function store(Request $request, AgentService $service)
  57. {
  58. return $this->execute(function () use ($request, $service) {
  59. $data = $request->validate($this->rules());
  60. return DB::transaction(function () use ($data, $service) {
  61. $agent = $service->create($data, (int) request()->user->id);
  62. if (!empty($data['owner_user_id'])) {
  63. $agent = app(\App\Services\Agent\AgentApplicationService::class)->bindMember($agent, (int) $data['owner_user_id']);
  64. }
  65. if (bccomp((string) ($data['initial_balance'] ?? 0), '0', 4) > 0) {
  66. app(AgentWalletService::class)->adjust(
  67. (int) $agent->id,
  68. (string) $data['initial_balance'],
  69. 'credit',
  70. '新增代理初始额度',
  71. 'agent-initial:' . $agent->id,
  72. 'admin',
  73. (int) request()->user->id
  74. );
  75. $agent = $agent->fresh(['flowCommissionProfile', 'profitCommissionProfile']);
  76. }
  77. return $agent;
  78. });
  79. });
  80. }
  81. public function update(Request $request, AgentService $service)
  82. {
  83. return $this->execute(function () use ($request, $service) {
  84. $data = $request->validate(array_merge(['id' => ['required', 'integer', 'exists:agents,id']], $this->rules(true)));
  85. return DB::transaction(function () use ($service, $data) {
  86. $agent = $service->update(
  87. AgentModel::query()->findOrFail($data['id']),
  88. $data,
  89. null,
  90. (int) request()->user->id
  91. );
  92. if (!empty($data['owner_user_id'])) {
  93. $agent = app(\App\Services\Agent\AgentApplicationService::class)->bindMember($agent, (int) $data['owner_user_id']);
  94. }
  95. return $agent;
  96. });
  97. });
  98. }
  99. public function domains(Request $request, AgentDomainService $service)
  100. {
  101. return $this->execute(function () use ($request, $service) {
  102. $data = $request->validate([
  103. 'id' => ['required', 'integer', 'exists:agents,id'],
  104. 'pc' => ['nullable', 'string', 'max:255'], 'h5' => ['nullable', 'string', 'max:255'],
  105. ]);
  106. return $service->save(AgentModel::query()->findOrFail($data['id']), $data);
  107. });
  108. }
  109. public function adjust(Request $request, AgentWalletService $wallets)
  110. {
  111. return $this->execute(function () use ($request, $wallets) {
  112. $data = $request->validate([
  113. 'agent_id' => ['required', 'integer', 'exists:agents,id'], 'type' => ['required', 'in:credit,debit'],
  114. 'amount' => ['required', 'numeric', 'min:0.0001', new DecimalNumber()], 'remark' => ['required', 'string', 'max:500'],
  115. 'request_id' => ['required', 'string', 'max:64'],
  116. ]);
  117. return $wallets->adjust(
  118. (int) $data['agent_id'], (string) $data['amount'], $data['type'], $data['remark'],
  119. 'admin:' . request()->user->id . ':' . $data['request_id'], 'admin', (int) request()->user->id
  120. );
  121. });
  122. }
  123. public function quota(Request $request, ThirdGameBalanceService $thirdGame)
  124. {
  125. return $this->execute(function () use ($request, $thirdGame) {
  126. $data = $request->validate([
  127. 'agent_id' => ['required', 'integer', 'exists:agents,id'],
  128. 'refresh' => ['nullable', 'boolean'],
  129. ]);
  130. $agent = AgentModel::query()->findOrFail($data['agent_id']);
  131. $provider = $thirdGame->agentBalances((int) $agent->id, $request->boolean('refresh'));
  132. if (empty($provider['ok'])) {
  133. throw new \RuntimeException((string) ($provider['msg'] ?? '三方平台余额查询失败'));
  134. }
  135. $balances = is_array($provider['balances'] ?? null) ? $provider['balances'] : [];
  136. $platforms = EgameItem::query()->where('item_type', EgameItem::TYPE_PLATFORM)->where('game_type', 0)
  137. ->where('status', EgameItem::STATUS_ENABLED)->orderByDesc('sort')->get(['plat_type', 'name', 'logo'])
  138. ->map(function (EgameItem $platform) use ($balances) {
  139. $key = strtolower((string) $platform->plat_type);
  140. $platform->logo = Util::ensureUrl($platform->logo);
  141. $platform->setAttribute('balance', $balances[$key] ?? '0.0000000000');
  142. $platform->setAttribute('account_opened', array_key_exists($key, $balances) ? 1 : 0);
  143. return $platform;
  144. });
  145. return [
  146. 'agent_id' => (int) $agent->id,
  147. 'player_id' => (string) $provider['player_id'],
  148. 'currency' => (string) $provider['currency'],
  149. 'balance' => (string) $agent->balance,
  150. 'frozen_balance' => (string) $agent->frozen_balance,
  151. 'quota_mode' => 'provider',
  152. 'provider_wallet_supported' => true,
  153. 'platforms' => $platforms,
  154. ];
  155. });
  156. }
  157. public function bindMember(Request $request)
  158. {
  159. return $this->execute(function () use ($request) {
  160. $data = $request->validate([
  161. 'user_id' => ['required', 'integer', 'exists:users,id'],
  162. 'agent_id' => ['nullable', 'integer', 'exists:agents,id'],
  163. ]);
  164. $user = User::query()->findOrFail($data['user_id']);
  165. $user->agent_id = $data['agent_id'] ?? null;
  166. $user->save();
  167. return ['user_id' => (int) $user->id, 'agent_id' => $user->agent_id === null ? null : (int) $user->agent_id];
  168. });
  169. }
  170. public function loginLogs(Request $request)
  171. {
  172. return $this->execute(function () use ($request) {
  173. $data = $request->validate([
  174. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  175. 'agent_id' => ['nullable', 'integer'], 'username' => ['nullable', 'string', 'max:64'],
  176. 'ip' => ['nullable', 'string', 'max:64'], 'device' => ['nullable', 'in:mobile,pc'],
  177. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  178. ]);
  179. $query = AgentLoginLog::query();
  180. foreach (['agent_id', 'username', 'ip', 'device'] as $field) if (!empty($data[$field])) $query->where($field, $data[$field]);
  181. return $this->paginate($query, $data, 'login_at', true, 'login_at');
  182. });
  183. }
  184. public function withdrawals(Request $request)
  185. {
  186. return $this->execute(function () use ($request) {
  187. $data = $request->validate([
  188. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  189. 'agent_id' => ['nullable', 'integer'], 'order_no' => ['nullable', 'string', 'max:40'],
  190. 'status' => ['nullable', 'in:pending,approved,rejected,paid'],
  191. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  192. ]);
  193. $query = AgentWithdrawal::query()->with('agent:id,username,real_name');
  194. foreach (['agent_id', 'order_no', 'status'] as $field) if (!empty($data[$field])) $query->where($field, $data[$field]);
  195. return $this->paginate($query, $data);
  196. });
  197. }
  198. public function auditWithdrawal(Request $request, AgentWithdrawalService $service)
  199. {
  200. return $this->execute(function () use ($request, $service) {
  201. $data = $request->validate([
  202. 'id' => ['required', 'integer', 'exists:agent_withdrawals,id'],
  203. 'action' => ['required', 'in:approve,reject,paid'], 'remark' => ['nullable', 'string', 'max:500'],
  204. ]);
  205. return $service->audit((int) $data['id'], $data['action'], (int) request()->user->id, (string) ($data['remark'] ?? ''));
  206. });
  207. }
  208. public function reports(Request $request, AgentReportService $reports)
  209. {
  210. return $this->execute(function () use ($request, $reports) {
  211. $data = $request->validate([
  212. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:20'],
  213. 'username' => ['nullable', 'string', 'max:64'], 'start_date' => ['required', 'date_format:Y-m-d'],
  214. 'end_date' => ['required', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  215. ]);
  216. return $reports->reportRows(null, $data);
  217. });
  218. }
  219. public function commissions(Request $request)
  220. {
  221. return $this->execute(function () use ($request) {
  222. $data = $request->validate([
  223. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  224. 'agent_id' => ['nullable', 'integer'], 'order_no' => ['nullable', 'string', 'max:48'],
  225. 'username' => ['nullable', 'string', 'max:64'],
  226. 'platform' => ['nullable', 'string', 'max:32'],
  227. 'flow_status' => ['nullable', 'in:pending,credited'],
  228. 'profit_status' => ['nullable', 'in:pending,credited'],
  229. 'settlement_date' => ['nullable', 'date_format:Y-m-d'],
  230. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  231. ]);
  232. $query = AgentRebateRecord::query()->with('agent:id,username');
  233. foreach (['agent_id', 'order_no', 'platform', 'flow_status', 'profit_status'] as $field) {
  234. if (!empty($data[$field])) $query->where($field, $data[$field]);
  235. }
  236. if (!empty($data['username'])) {
  237. $query->whereHas('agent', fn($query) => $query->where('username', 'like', '%' . $data['username'] . '%'));
  238. }
  239. if (!empty($data['settlement_date'])) $query->where('settlement_date', $data['settlement_date']);
  240. if (!empty($data['start_date'])) $query->where('created_at', '>=', $data['start_date'] . ' 00:00:00');
  241. if (!empty($data['end_date'])) $query->where('created_at', '<=', $data['end_date'] . ' 23:59:59');
  242. return $this->paginate($query, $data, 'settlement_date', false);
  243. });
  244. }
  245. public function profitCommissionProfiles(Request $request, AgentProfitCommissionProfileService $service)
  246. {
  247. return $this->execute(function () use ($request, $service) {
  248. $data = $request->validate([
  249. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  250. 'name' => ['nullable', 'string', 'max:128'],
  251. ]);
  252. return $service->paginate($data);
  253. });
  254. }
  255. public function flowCommissionProfiles(Request $request, AgentFlowCommissionProfileService $service)
  256. {
  257. return $this->execute(function () use ($request, $service) {
  258. $data = $request->validate([
  259. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  260. 'name' => ['nullable', 'string', 'max:128'],
  261. ]);
  262. return $service->paginate($data);
  263. });
  264. }
  265. public function saveFlowCommissionProfile(Request $request, AgentFlowCommissionProfileService $service)
  266. {
  267. return $this->execute(function () use ($request, $service) {
  268. $id = (int)$request->input('id', 0);
  269. $data = $request->validate([
  270. 'id' => ['nullable', 'integer', 'exists:agent_flow_commission_profiles,id'],
  271. 'name' => ['required', 'string', 'max:128', Rule::unique('agent_flow_commission_profiles', 'name')->whereNull('owner_agent_id')->ignore($id)],
  272. 'live_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  273. 'slot_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  274. 'lottery_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  275. 'sport_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  276. 'esports_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  277. 'fishing_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  278. 'chess_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  279. 'is_default' => ['required', 'boolean'],
  280. ]);
  281. return $service->save($data, (int)request()->user->id);
  282. });
  283. }
  284. public function deleteFlowCommissionProfile(Request $request, AgentFlowCommissionProfileService $service)
  285. {
  286. return $this->execute(function () use ($request, $service) {
  287. $data = $request->validate(['id' => ['required', 'integer', 'exists:agent_flow_commission_profiles,id']]);
  288. $service->delete((int)$data['id'], (int)request()->user->id);
  289. return [];
  290. });
  291. }
  292. public function saveProfitCommissionProfile(Request $request, AgentProfitCommissionProfileService $service)
  293. {
  294. return $this->execute(function () use ($request, $service) {
  295. $id = (int) $request->input('id', 0);
  296. $data = $request->validate([
  297. 'id' => ['nullable', 'integer', 'exists:agent_profit_commission_profiles,id'],
  298. 'name' => ['required', 'string', 'max:128', Rule::unique('agent_profit_commission_profiles', 'name')->whereNull('owner_agent_id')->ignore($id)],
  299. 'live_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  300. 'slot_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  301. 'lottery_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  302. 'sport_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  303. 'esports_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  304. 'fishing_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  305. 'chess_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  306. 'is_default' => ['required', 'boolean'],
  307. ]);
  308. return $service->save($data, (int) request()->user->id);
  309. });
  310. }
  311. public function deleteProfitCommissionProfile(Request $request, AgentProfitCommissionProfileService $service)
  312. {
  313. return $this->execute(function () use ($request, $service) {
  314. $data = $request->validate(['id' => ['required', 'integer', 'exists:agent_profit_commission_profiles,id']]);
  315. $service->delete((int) $data['id'], (int) request()->user->id);
  316. return [];
  317. });
  318. }
  319. private function rules(bool $update = false): array
  320. {
  321. return [
  322. 'language' => ['sometimes', 'required', 'in:zh,en,vi'],
  323. 'owner_user_id' => ['nullable', 'integer', 'min:1', 'exists:users,id'],
  324. 'username' => $update
  325. ? ['sometimes', 'string', 'min:4', 'max:64', 'alpha_dash', Rule::unique('agents', 'username')->ignore((int) request()->input('id'))]
  326. : ['required', 'string', 'min:4', 'max:64', 'alpha_dash', 'unique:agents,username'],
  327. 'password' => [$update ? 'nullable' : 'required', 'string', 'min:6', 'max:100'],
  328. 'withdraw_password' => ['nullable', 'string', 'min:6', 'max:100'], 'real_name' => ['nullable', 'string', 'max:128'],
  329. 'parent_id' => $update ? ['nullable', 'integer', 'exists:agents,id'] : ['prohibited'],
  330. 'deposit_fee_rate' => ['nullable', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  331. 'withdraw_fee_rate' => ['nullable', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  332. 'flow_commission_rate' => ['prohibited'],
  333. 'profit_commission_rate' => ['prohibited'],
  334. 'flow_commission_profile_id' => $update
  335. ? ['nullable', 'integer', Rule::exists('agent_flow_commission_profiles', 'id')->whereNull('owner_agent_id')]
  336. : ['required', 'integer', Rule::exists('agent_flow_commission_profiles', 'id')->whereNull('owner_agent_id')],
  337. 'profit_commission_profile_id' => ['nullable', 'integer', Rule::exists('agent_profit_commission_profiles', 'id')->whereNull('owner_agent_id')],
  338. 'status' => ['nullable', 'integer', 'in:0,1'],
  339. 'initial_balance' => $update ? ['prohibited'] : ['nullable', 'numeric', 'min:0', new DecimalNumber()],
  340. ];
  341. }
  342. private function paginate($query, array $data, string $orderBy = 'id', bool $filterCreatedAt = true, string $dateColumn = 'created_at'): array
  343. {
  344. $page = max(1, (int) ($data['page'] ?? 1));
  345. $limit = min(100, max(1, (int) ($data['limit'] ?? 20)));
  346. if ($filterCreatedAt && !empty($data['start_date'])) $query->where($dateColumn, '>=', $data['start_date'] . ' 00:00:00');
  347. if ($filterCreatedAt && !empty($data['end_date'])) $query->where($dateColumn, '<=', $data['end_date'] . ' 23:59:59');
  348. $total = (clone $query)->count();
  349. $list = $query->orderByDesc($orderBy)->orderByDesc('id')->forPage($page, $limit)->get();
  350. return compact('total', 'page', 'limit', 'list');
  351. }
  352. private function formatAgent(AgentModel $agent, AgentDomainService $domains): array
  353. {
  354. $token = $agent->token;
  355. $online = $token
  356. && $token->expires_at->isFuture()
  357. && $token->last_used_at
  358. && $token->last_used_at->gte(now()->subMinutes(config('agent.online_minutes', 5)));
  359. $data = $agent->toArray();
  360. unset($data['token']);
  361. return array_merge($data, $domains->format($agent), [
  362. 'login_status' => $online ? 1 : 0,
  363. 'login_status_text' => $online ? '在线' : '离线',
  364. 'last_active_at' => $token?->last_used_at?->toDateTimeString(),
  365. ]);
  366. }
  367. }