Agent.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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\Services\Agent\AgentDomainService;
  10. use App\Services\Agent\AgentReportService;
  11. use App\Services\Agent\AgentService;
  12. use App\Services\Agent\AgentWalletService;
  13. use App\Services\Agent\AgentWithdrawalService;
  14. use App\Services\Agent\AgentPlanApplicationService;
  15. use App\Services\Agent\AgentRechargeService;
  16. use App\Services\Agent\AgentPaymentService;
  17. use App\Models\Agent\AgentSetting;
  18. use App\Models\Agent\AgentRecharge;
  19. use App\Models\Agent\AgentProfitEntry;
  20. use App\Services\Agent\AgentProfitCommissionProfileService;
  21. use Illuminate\Http\Request;
  22. use Illuminate\Validation\Rule;
  23. use Illuminate\Support\Facades\DB;
  24. use App\Rules\DecimalNumber;
  25. use App\Constants\Util;
  26. use App\Services\ThirdGameBalanceService;
  27. class Agent extends AgentApiController
  28. {
  29. public function index(Request $request, AgentDomainService $domains)
  30. {
  31. return $this->execute(function () use ($request, $domains) {
  32. $data = $request->validate([
  33. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  34. 'username' => ['nullable', 'string', 'max:64'], 'real_name' => ['nullable', 'string', 'max:128'],
  35. 'type' => ['nullable', 'integer', 'in:1,2'], 'status' => ['nullable', 'integer', 'in:0,1'],
  36. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  37. ]);
  38. $page = max(1, (int) ($data['page'] ?? 1));
  39. $limit = min(100, max(1, (int) ($data['limit'] ?? 20)));
  40. $query = AgentModel::query()->with(['token:id,agent_id,expires_at,last_used_at', 'profitCommissionProfile'])->withCount(['members']);
  41. if (!empty($data['username'])) $query->where('username', 'like', '%' . $data['username'] . '%');
  42. if (!empty($data['real_name'])) $query->where('real_name', 'like', '%' . $data['real_name'] . '%');
  43. if (!empty($data['type'])) $query->where('type', $data['type']);
  44. if (isset($data['status'])) $query->where('status', $data['status']);
  45. if (!empty($data['start_date'])) $query->where('created_at', '>=', $data['start_date'] . ' 00:00:00');
  46. if (!empty($data['end_date'])) $query->where('created_at', '<=', $data['end_date'] . ' 23:59:59');
  47. $total = (clone $query)->count();
  48. $list = $query->orderByDesc('id')->forPage($page, $limit)->get()->map(fn(AgentModel $agent) => $this->formatAgent($agent, $domains));
  49. return compact('total', 'page', 'limit', 'list');
  50. });
  51. }
  52. public function show(Request $request, AgentDomainService $domains)
  53. {
  54. return $this->execute(function () use ($request, $domains) {
  55. $data = $request->validate(['id' => ['required', 'integer', 'exists:agents,id']]);
  56. $agent = AgentModel::query()->with(['token:id,agent_id,expires_at,last_used_at', 'profitCommissionProfile'])->withCount(['members'])->findOrFail($data['id']);
  57. return $this->formatAgent($agent, $domains);
  58. });
  59. }
  60. public function store(Request $request, AgentService $service)
  61. {
  62. return $this->execute(function () use ($request, $service) {
  63. $data = $request->validate($this->rules());
  64. return DB::transaction(function () use ($data, $service) {
  65. $agent = $service->create($data, (int) request()->user->id);
  66. if (bccomp((string) ($data['initial_balance'] ?? 0), '0', 4) > 0) {
  67. app(AgentWalletService::class)->adjust(
  68. (int) $agent->id,
  69. (string) $data['initial_balance'],
  70. 'credit',
  71. '新增代理初始额度',
  72. 'agent-initial:' . $agent->id,
  73. 'admin',
  74. (int) request()->user->id
  75. );
  76. $agent = $agent->fresh(['profitCommissionProfile']);
  77. }
  78. return $agent;
  79. });
  80. });
  81. }
  82. public function update(Request $request, AgentService $service)
  83. {
  84. return $this->execute(function () use ($request, $service) {
  85. $data = $request->validate(array_merge(['id' => ['required', 'integer', 'exists:agents,id']], $this->rules(true)));
  86. return DB::transaction(function () use ($service, $data) {
  87. return $service->update(AgentModel::query()->findOrFail($data['id']), $data);
  88. });
  89. });
  90. }
  91. public function domains(Request $request, AgentDomainService $service)
  92. {
  93. return $this->execute(function () use ($request, $service) {
  94. $data = $request->validate([
  95. 'id' => ['required', 'integer', 'exists:agents,id'],
  96. 'pc' => ['nullable', 'string', 'max:255'], 'h5' => ['nullable', 'string', 'max:255'],
  97. ]);
  98. return $service->save(AgentModel::query()->findOrFail($data['id']), $data);
  99. });
  100. }
  101. public function adjust(Request $request, AgentWalletService $wallets)
  102. {
  103. return $this->execute(function () use ($request, $wallets) {
  104. $data = $request->validate([
  105. 'agent_id' => ['required', 'integer', 'exists:agents,id'], 'type' => ['required', 'in:credit,debit,margin_credit,margin_refund'],
  106. 'amount' => ['required', 'numeric', 'min:0.0001', new DecimalNumber()], 'remark' => ['required', 'string', 'max:500'],
  107. 'request_id' => ['required', 'string', 'max:64'],
  108. ]);
  109. return $wallets->adjust(
  110. (int) $data['agent_id'], (string) $data['amount'], $data['type'], $data['remark'],
  111. 'admin:' . request()->user->id . ':' . $data['request_id'], 'admin', (int) request()->user->id
  112. );
  113. });
  114. }
  115. public function quota(Request $request, ThirdGameBalanceService $thirdGame)
  116. {
  117. return $this->execute(function () use ($request, $thirdGame) {
  118. $data = $request->validate([
  119. 'agent_id' => ['required', 'integer', 'exists:agents,id'],
  120. 'refresh' => ['nullable', 'boolean'],
  121. ]);
  122. $agent = AgentModel::query()->findOrFail($data['agent_id']);
  123. $provider = $thirdGame->agentBalances((int) $agent->id, $request->boolean('refresh'));
  124. if (empty($provider['ok'])) {
  125. throw new \RuntimeException((string) ($provider['msg'] ?? '三方平台余额查询失败'));
  126. }
  127. $balances = is_array($provider['balances'] ?? null) ? $provider['balances'] : [];
  128. $platforms = EgameItem::query()->where('item_type', EgameItem::TYPE_PLATFORM)->where('game_type', 0)
  129. ->where('status', EgameItem::STATUS_ENABLED)->orderByDesc('sort')->get(['plat_type', 'name', 'logo'])
  130. ->map(function (EgameItem $platform) use ($balances) {
  131. $key = strtolower((string) $platform->plat_type);
  132. $platform->logo = Util::ensureUrl($platform->logo);
  133. $platform->setAttribute('balance', $balances[$key] ?? '0.0000000000');
  134. $platform->setAttribute('account_opened', array_key_exists($key, $balances) ? 1 : 0);
  135. return $platform;
  136. });
  137. return [
  138. 'agent_id' => (int) $agent->id,
  139. 'player_id' => (string) $provider['player_id'],
  140. 'currency' => (string) $provider['currency'],
  141. 'balance' => (string) $agent->balance,
  142. 'frozen_balance' => (string) $agent->frozen_balance,
  143. 'quota_mode' => 'provider',
  144. 'provider_wallet_supported' => true,
  145. 'platforms' => $platforms,
  146. ];
  147. });
  148. }
  149. public function loginLogs(Request $request)
  150. {
  151. return $this->execute(function () use ($request) {
  152. $data = $request->validate([
  153. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  154. 'agent_id' => ['nullable', 'integer'], 'username' => ['nullable', 'string', 'max:64'],
  155. 'ip' => ['nullable', 'string', 'max:64'], 'device' => ['nullable', 'in:mobile,pc'],
  156. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  157. ]);
  158. $query = AgentLoginLog::query();
  159. foreach (['agent_id', 'username', 'ip', 'device'] as $field) if (!empty($data[$field])) $query->where($field, $data[$field]);
  160. return $this->paginate($query, $data, 'login_at', true, 'login_at');
  161. });
  162. }
  163. public function withdrawals(Request $request)
  164. {
  165. return $this->execute(function () use ($request) {
  166. $data = $request->validate([
  167. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  168. 'agent_id' => ['nullable', 'integer'], 'order_no' => ['nullable', 'string', 'max:40'],
  169. 'status' => ['nullable', 'in:pending,approved,rejected,paid'],
  170. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  171. ]);
  172. $query = AgentWithdrawal::query()->with('agent:id,username,real_name');
  173. foreach (['agent_id', 'order_no', 'status'] as $field) if (!empty($data[$field])) $query->where($field, $data[$field]);
  174. return $this->paginate($query, $data);
  175. });
  176. }
  177. public function auditWithdrawal(Request $request, AgentWithdrawalService $service)
  178. {
  179. return $this->execute(function () use ($request, $service) {
  180. $data = $request->validate([
  181. 'id' => ['required', 'integer', 'exists:agent_withdrawals,id'],
  182. 'action' => ['required', 'in:approve,reject,paid'], 'remark' => ['nullable', 'string', 'max:500'],
  183. ]);
  184. return $service->audit((int) $data['id'], $data['action'], (int) request()->user->id, (string) ($data['remark'] ?? ''));
  185. });
  186. }
  187. public function reports(Request $request, AgentReportService $reports)
  188. {
  189. return $this->execute(function () use ($request, $reports) {
  190. $data = $request->validate([
  191. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:20'],
  192. 'username' => ['nullable', 'string', 'max:64'], 'start_date' => ['required', 'date_format:Y-m-d'],
  193. 'end_date' => ['required', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  194. ]);
  195. return $reports->reportRows(null, $data);
  196. });
  197. }
  198. public function commissions(Request $request)
  199. {
  200. return $this->execute(function () use ($request) {
  201. $data = $request->validate([
  202. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  203. 'agent_id' => ['nullable', 'integer'], 'order_no' => ['nullable', 'string', 'max:48'],
  204. 'username' => ['nullable', 'string', 'max:64'],
  205. 'platform' => ['nullable', 'string', 'max:32'],
  206. 'category' => ['nullable', 'string', 'max:32'],
  207. 'profit_status' => ['nullable', 'in:pending,credited'],
  208. 'settlement_date' => ['nullable', 'date_format:Y-m-d'],
  209. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  210. ]);
  211. $query = AgentRebateRecord::query()->with('agent:id,username');
  212. if (!empty($data['category'])) $query->where('platform', $data['category']);
  213. foreach (['agent_id', 'order_no', 'platform', 'profit_status'] as $field) {
  214. if (!empty($data[$field])) $query->where($field, $data[$field]);
  215. }
  216. if (!empty($data['username'])) {
  217. $query->whereHas('agent', fn($query) => $query->where('username', 'like', '%' . $data['username'] . '%'));
  218. }
  219. if (!empty($data['settlement_date'])) $query->where('settlement_date', $data['settlement_date']);
  220. if (!empty($data['start_date'])) $query->where('created_at', '>=', $data['start_date'] . ' 00:00:00');
  221. if (!empty($data['end_date'])) $query->where('created_at', '<=', $data['end_date'] . ' 23:59:59');
  222. return $this->paginate($query, $data, 'settlement_date', false);
  223. });
  224. }
  225. public function profitCommissionProfiles(Request $request, AgentProfitCommissionProfileService $service)
  226. {
  227. return $this->execute(function () use ($request, $service) {
  228. $data = $request->validate([
  229. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  230. 'name' => ['nullable', 'string', 'max:128'],
  231. ]);
  232. return $service->paginate($data);
  233. });
  234. }
  235. public function saveProfitCommissionProfile(Request $request, AgentProfitCommissionProfileService $service)
  236. {
  237. return $this->execute(function () use ($request, $service) {
  238. $data = $request->validate([
  239. 'id' => ['nullable', 'integer', 'exists:agent_profit_commission_profiles,id'],
  240. 'name' => ['required', 'string', 'max:128'],
  241. 'margin_amount' => ['required', 'numeric', 'min:0', new DecimalNumber()],
  242. 'f1_lottery_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  243. 'f1_sport_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  244. 'live_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  245. 'slot_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  246. 'sport_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  247. 'esports_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  248. 'fishing_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  249. 'chess_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  250. ]);
  251. return $service->save($data, (int) request()->user->id);
  252. });
  253. }
  254. public function deleteProfitCommissionProfile(Request $request, AgentProfitCommissionProfileService $service)
  255. {
  256. return $this->execute(function () use ($request, $service) {
  257. $data = $request->validate(['id' => ['required', 'integer', 'exists:agent_profit_commission_profiles,id']]);
  258. $service->delete((int) $data['id']);
  259. return [];
  260. });
  261. }
  262. /** 代理全局配置:停止分成阈值(方案保证金的百分比)、入股价格与可购买区间。 */
  263. public function settings()
  264. {
  265. return $this->execute(fn () => AgentSetting::current());
  266. }
  267. public function saveSettings(Request $request)
  268. {
  269. return $this->execute(function () use ($request) {
  270. $data = $request->validate([
  271. 'margin_stop_percent' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  272. 'equity_enabled' => ['required', 'boolean'],
  273. 'equity_price_per_percent' => ['required', 'numeric', 'min:0', new DecimalNumber()],
  274. 'equity_min_percent' => ['required', 'numeric', 'gt:0', 'max:100', new DecimalNumber(3, 4)],
  275. 'equity_max_percent' => ['required', 'numeric', 'gte:equity_min_percent', 'max:100', new DecimalNumber(3, 4)],
  276. 'recharge_channel_group_id' => ['nullable', 'integer', \Illuminate\Validation\Rule::exists('recharge_channel_group', 'id')->whereNull('deleted_at')],
  277. ]);
  278. if (array_key_exists('recharge_channel_group_id', $data)) $data['recharge_channel_group_id'] = (int) ($data['recharge_channel_group_id'] ?: 1);
  279. $setting = AgentSetting::current();
  280. $setting->fill($data + ['updated_by' => (int) request()->user->id]);
  281. $setting->equity_enabled = $request->boolean('equity_enabled') ? 1 : 0;
  282. $setting->save();
  283. return $setting->fresh();
  284. });
  285. }
  286. public function planApplications(Request $request, AgentPlanApplicationService $service)
  287. {
  288. return $this->execute(function () use ($request, $service) {
  289. $data = $request->validate([
  290. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  291. 'agent_id' => ['nullable', 'integer'], 'username' => ['nullable', 'string', 'max:64'],
  292. 'status' => ['nullable', 'in:pending,approved,rejected'],
  293. ]);
  294. return $service->paginate($data);
  295. });
  296. }
  297. public function auditPlanApplication(Request $request, AgentPlanApplicationService $service)
  298. {
  299. return $this->execute(function () use ($request, $service) {
  300. $data = $request->validate([
  301. 'id' => ['required', 'integer'], 'status' => ['required', 'in:approved,rejected'],
  302. 'reject_reason' => ['required_if:status,rejected', 'nullable', 'string', 'max:500'],
  303. ]);
  304. return $service->audit((int) $data['id'], $data['status'], (int) request()->user->id, (string) ($data['reject_reason'] ?? ''));
  305. });
  306. }
  307. public function recharges(Request $request)
  308. {
  309. return $this->execute(function () use ($request) {
  310. $data = $request->validate([
  311. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  312. 'agent_id' => ['nullable', 'integer'], 'order_no' => ['nullable', 'string', 'max:40'],
  313. 'txid' => ['nullable', 'string', 'max:100'], 'type' => ['nullable', 'integer', 'in:1,2'],
  314. 'status' => ['nullable', 'integer', 'in:0,1,2,3'],
  315. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  316. ]);
  317. $query = AgentRecharge::query()->with('agent:id,username,real_name,type');
  318. foreach (['agent_id', 'order_no', 'txid', 'type'] as $field) if (!empty($data[$field])) $query->where($field, $data[$field]);
  319. if (isset($data['status'])) $query->where('status', (int) $data['status']);
  320. return $this->paginate($query, $data);
  321. });
  322. }
  323. public function auditRecharge(Request $request, AgentRechargeService $service)
  324. {
  325. return $this->execute(function () use ($request, $service) {
  326. $data = $request->validate([
  327. 'id' => ['required', 'integer'], 'status' => ['required', 'integer', 'in:1,2'],
  328. 'remark' => ['required_if:status,2', 'nullable', 'string', 'max:500'],
  329. ]);
  330. return $service->audit((int) $data['id'], (int) $data['status'], (int) request()->user->id, (string) ($data['remark'] ?? ''));
  331. });
  332. }
  333. /** 代理人民币充值(在线代收 + 人工充值)。 */
  334. public function paymentOrders(Request $request, AgentPaymentService $service)
  335. {
  336. return $this->execute(function () use ($request, $service) {
  337. $data = $request->validate([
  338. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  339. 'agent_id' => ['nullable', 'integer'], 'order_no' => ['nullable', 'string', 'max:64'],
  340. 'type' => ['nullable', 'integer', 'in:1,3'], 'channel' => ['nullable', 'string', 'max:50'],
  341. 'payment_type' => ['nullable', 'integer'], 'status' => ['nullable', 'integer', 'between:0,7'],
  342. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  343. ]);
  344. return $service->paginate($data);
  345. });
  346. }
  347. public function paymentOrderPayData(Request $request, AgentPaymentService $service)
  348. {
  349. return $this->execute(function () use ($request, $service) {
  350. $data = $request->validate([
  351. 'ids' => ['required', 'array', 'min:1'], 'ids.*' => ['required', 'integer', 'min:1'],
  352. 'payment_type' => ['required', 'integer'], 'pay_data' => ['required', 'string'],
  353. ]);
  354. return ['updated' => $service->setPayData($data['ids'], (int) $data['payment_type'], $data['pay_data'])];
  355. });
  356. }
  357. public function auditPaymentOrder(Request $request, AgentPaymentService $service)
  358. {
  359. return $this->execute(function () use ($request, $service) {
  360. $data = $request->validate([
  361. 'id' => ['required', 'integer'], 'status' => ['required', 'integer', 'in:2,3'],
  362. 'remark' => ['required_if:status,3', 'nullable', 'string', 'max:255'],
  363. ]);
  364. return $service->manualAudit((int) $data['id'], (int) $data['status'], (string) ($data['remark'] ?? ''), (int) request()->user->id);
  365. });
  366. }
  367. public function rejectPaymentOrder(Request $request, AgentPaymentService $service)
  368. {
  369. return $this->execute(function () use ($request, $service) {
  370. $data = $request->validate(['id' => ['required', 'integer'], 'remark' => ['nullable', 'string', 'max:120']]);
  371. return $service->reject((int) $data['id'], (string) ($data['remark'] ?? ''), (int) request()->user->id);
  372. });
  373. }
  374. public function queryPaymentOrder(Request $request, AgentPaymentService $service)
  375. {
  376. return $this->execute(function () use ($request, $service) {
  377. $data = $request->validate(['id' => ['required', 'integer']]);
  378. $result = $service->query((int) $data['id']);
  379. return ['paid' => (int) ($result['code'] ?? 0) === 1, 'msg' => (string) ($result['msg'] ?? '')];
  380. });
  381. }
  382. public function profitEntries(Request $request)
  383. {
  384. return $this->execute(function () use ($request) {
  385. $data = $request->validate([
  386. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  387. 'agent_id' => ['nullable', 'integer'], 'user_id' => ['nullable', 'integer'], 'category' => ['nullable', 'string', 'max:32'],
  388. 'status' => ['nullable', 'in:counted,skipped_inactive,skipped_no_plan,skipped_category'],
  389. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  390. ]);
  391. $query = AgentProfitEntry::query();
  392. foreach (['agent_id', 'user_id', 'category', 'status'] as $field) if (!empty($data[$field])) $query->where($field, $data[$field]);
  393. if (!empty($data['start_date'])) $query->where('settle_date', '>=', $data['start_date']);
  394. if (!empty($data['end_date'])) $query->where('settle_date', '<=', $data['end_date']);
  395. $page = max(1, (int) ($data['page'] ?? 1));
  396. $limit = min(100, max(1, (int) ($data['limit'] ?? 20)));
  397. $total = (clone $query)->count();
  398. $list = $query->orderByDesc('id')->forPage($page, $limit)->get()->each->makeVisible('realtime_balance_after');
  399. return compact('total', 'page', 'limit', 'list');
  400. });
  401. }
  402. private function rules(bool $update = false): array
  403. {
  404. return [
  405. 'language' => ['sometimes', 'required', 'in:zh,en,vi'],
  406. 'type' => [$update ? 'nullable' : 'required', 'integer', 'in:1,2'],
  407. 'username' => $update
  408. ? ['sometimes', 'string', 'min:4', 'max:64', 'alpha_dash', Rule::unique('agents', 'username')->ignore((int) request()->input('id'))]
  409. : ['required', 'string', 'min:4', 'max:64', 'alpha_dash', 'unique:agents,username'],
  410. 'password' => [$update ? 'nullable' : 'required', 'string', 'min:6', 'max:100'],
  411. 'withdraw_password' => ['nullable', 'string', 'min:6', 'max:100'], 'real_name' => ['nullable', 'string', 'max:128'],
  412. 'deposit_fee_rate' => ['nullable', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  413. 'withdraw_fee_rate' => ['nullable', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  414. 'status' => ['nullable', 'integer', 'in:0,1'],
  415. 'initial_balance' => $update ? ['prohibited'] : ['nullable', 'numeric', 'min:0', new DecimalNumber()],
  416. ];
  417. }
  418. private function paginate($query, array $data, string $orderBy = 'id', bool $filterCreatedAt = true, string $dateColumn = 'created_at'): array
  419. {
  420. $page = max(1, (int) ($data['page'] ?? 1));
  421. $limit = min(100, max(1, (int) ($data['limit'] ?? 20)));
  422. if ($filterCreatedAt && !empty($data['start_date'])) $query->where($dateColumn, '>=', $data['start_date'] . ' 00:00:00');
  423. if ($filterCreatedAt && !empty($data['end_date'])) $query->where($dateColumn, '<=', $data['end_date'] . ' 23:59:59');
  424. $total = (clone $query)->count();
  425. $list = $query->orderByDesc($orderBy)->orderByDesc('id')->forPage($page, $limit)->get();
  426. return compact('total', 'page', 'limit', 'list');
  427. }
  428. private function formatAgent(AgentModel $agent, AgentDomainService $domains): array
  429. {
  430. $token = $agent->token;
  431. $online = $token
  432. && $token->expires_at->isFuture()
  433. && $token->last_used_at
  434. && $token->last_used_at->gte(now()->subMinutes(config('agent.online_minutes', 5)));
  435. $data = $agent->toArray();
  436. unset($data['token']);
  437. $wallets = app(AgentWalletService::class);
  438. return array_merge($data, $domains->format($agent), $wallets->funds($agent), [
  439. // 总后台可以看到实时保证金;代理端不返回。
  440. 'realtime_balance' => (string) $agent->realtime_balance,
  441. 'realtime_margin' => $agent->isJoint() ? $wallets->effectiveMargin($agent) : null,
  442. 'login_status' => $online ? 1 : 0,
  443. 'login_status_text' => $online ? '在线' : '离线',
  444. 'last_active_at' => $token?->last_used_at?->toDateTimeString(),
  445. ]);
  446. }
  447. }