Agent.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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,processing,rejected,paid,failed'], 'channel' => ['nullable', 'string', 'max:32'],
  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. if (!empty($data['channel'])) $query->where('account_type', $data['channel']);
  175. $result = $this->paginate($query, $data);
  176. $result['list']->each->makeVisible(['pay_data', 'callback_data']);
  177. return $result;
  178. });
  179. }
  180. public function auditWithdrawal(Request $request, AgentWithdrawalService $service)
  181. {
  182. return $this->execute(function () use ($request, $service) {
  183. $data = $request->validate([
  184. 'id' => ['required', 'integer', 'exists:agent_withdrawals,id'],
  185. 'action' => ['required', 'in:approve,reject,paid'], 'remark' => ['nullable', 'string', 'max:500'],
  186. ]);
  187. return $service->audit((int) $data['id'], $data['action'], (int) request()->user->id, (string) ($data['remark'] ?? ''));
  188. });
  189. }
  190. public function reports(Request $request, AgentReportService $reports)
  191. {
  192. return $this->execute(function () use ($request, $reports) {
  193. $data = $request->validate([
  194. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:20'],
  195. 'username' => ['nullable', 'string', 'max:64'], 'start_date' => ['required', 'date_format:Y-m-d'],
  196. 'end_date' => ['required', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  197. ]);
  198. return $reports->reportRows(null, $data);
  199. });
  200. }
  201. public function commissions(Request $request)
  202. {
  203. return $this->execute(function () use ($request) {
  204. $data = $request->validate([
  205. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  206. 'agent_id' => ['nullable', 'integer'], 'order_no' => ['nullable', 'string', 'max:48'],
  207. 'username' => ['nullable', 'string', 'max:64'],
  208. 'platform' => ['nullable', 'string', 'max:32'],
  209. 'category' => ['nullable', 'string', 'max:32'],
  210. 'profit_status' => ['nullable', 'in:pending,credited'],
  211. 'settlement_date' => ['nullable', 'date_format:Y-m-d'],
  212. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  213. ]);
  214. $query = AgentRebateRecord::query()->with('agent:id,username');
  215. if (!empty($data['category'])) $query->where('platform', $data['category']);
  216. foreach (['agent_id', 'order_no', 'platform', 'profit_status'] as $field) {
  217. if (!empty($data[$field])) $query->where($field, $data[$field]);
  218. }
  219. if (!empty($data['username'])) {
  220. $query->whereHas('agent', fn($query) => $query->where('username', 'like', '%' . $data['username'] . '%'));
  221. }
  222. if (!empty($data['settlement_date'])) $query->where('settlement_date', $data['settlement_date']);
  223. if (!empty($data['start_date'])) $query->where('created_at', '>=', $data['start_date'] . ' 00:00:00');
  224. if (!empty($data['end_date'])) $query->where('created_at', '<=', $data['end_date'] . ' 23:59:59');
  225. return $this->paginate($query, $data, 'settlement_date', false);
  226. });
  227. }
  228. public function profitCommissionProfiles(Request $request, AgentProfitCommissionProfileService $service)
  229. {
  230. return $this->execute(function () use ($request, $service) {
  231. $data = $request->validate([
  232. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  233. 'name' => ['nullable', 'string', 'max:128'],
  234. ]);
  235. return $service->paginate($data);
  236. });
  237. }
  238. public function saveProfitCommissionProfile(Request $request, AgentProfitCommissionProfileService $service)
  239. {
  240. return $this->execute(function () use ($request, $service) {
  241. $data = $request->validate([
  242. 'id' => ['nullable', 'integer', 'exists:agent_profit_commission_profiles,id'],
  243. 'name' => ['required', 'string', 'max:128'],
  244. 'margin_amount' => ['required', 'numeric', 'min:0', new DecimalNumber()],
  245. 'f1_lottery_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  246. 'f1_sport_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  247. 'live_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  248. 'slot_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  249. 'sport_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  250. 'esports_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  251. 'fishing_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  252. 'chess_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  253. ]);
  254. return $service->save($data, (int) request()->user->id);
  255. });
  256. }
  257. public function deleteProfitCommissionProfile(Request $request, AgentProfitCommissionProfileService $service)
  258. {
  259. return $this->execute(function () use ($request, $service) {
  260. $data = $request->validate(['id' => ['required', 'integer', 'exists:agent_profit_commission_profiles,id']]);
  261. $service->delete((int) $data['id']);
  262. return [];
  263. });
  264. }
  265. /** 代理全局配置:停止分成阈值(方案保证金的百分比)、入股价格与可购买区间。 */
  266. public function settings()
  267. {
  268. return $this->execute(fn () => AgentSetting::current());
  269. }
  270. public function saveSettings(Request $request)
  271. {
  272. return $this->execute(function () use ($request) {
  273. $data = $request->validate([
  274. 'margin_stop_percent' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  275. 'equity_enabled' => ['required', 'boolean'],
  276. 'equity_price_per_percent' => ['required', 'numeric', 'min:0', new DecimalNumber()],
  277. 'equity_min_percent' => ['required', 'numeric', 'gt:0', 'max:100', new DecimalNumber(3, 4)],
  278. 'equity_max_percent' => ['required', 'numeric', 'gte:equity_min_percent', 'max:100', new DecimalNumber(3, 4)],
  279. 'recharge_channel_group_id' => ['nullable', 'integer', \Illuminate\Validation\Rule::exists('recharge_channel_group', 'id')->whereNull('deleted_at')],
  280. ]);
  281. if (array_key_exists('recharge_channel_group_id', $data)) $data['recharge_channel_group_id'] = (int) ($data['recharge_channel_group_id'] ?: 1);
  282. $setting = AgentSetting::current();
  283. $setting->fill($data + ['updated_by' => (int) request()->user->id]);
  284. $setting->equity_enabled = $request->boolean('equity_enabled') ? 1 : 0;
  285. $setting->save();
  286. return $setting->fresh();
  287. });
  288. }
  289. public function planApplications(Request $request, AgentPlanApplicationService $service)
  290. {
  291. return $this->execute(function () use ($request, $service) {
  292. $data = $request->validate([
  293. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  294. 'agent_id' => ['nullable', 'integer'], 'username' => ['nullable', 'string', 'max:64'],
  295. 'status' => ['nullable', 'in:pending,approved,rejected'],
  296. ]);
  297. return $service->paginate($data);
  298. });
  299. }
  300. public function auditPlanApplication(Request $request, AgentPlanApplicationService $service)
  301. {
  302. return $this->execute(function () use ($request, $service) {
  303. $data = $request->validate([
  304. 'id' => ['required', 'integer'], 'status' => ['required', 'in:approved,rejected'],
  305. 'reject_reason' => ['required_if:status,rejected', 'nullable', 'string', 'max:500'],
  306. ]);
  307. return $service->audit((int) $data['id'], $data['status'], (int) request()->user->id, (string) ($data['reject_reason'] ?? ''));
  308. });
  309. }
  310. public function recharges(Request $request)
  311. {
  312. return $this->execute(function () use ($request) {
  313. $data = $request->validate([
  314. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  315. 'agent_id' => ['nullable', 'integer'], 'order_no' => ['nullable', 'string', 'max:40'],
  316. 'txid' => ['nullable', 'string', 'max:100'], 'type' => ['nullable', 'integer', 'in:1,2'],
  317. 'status' => ['nullable', 'integer', 'in:0,1,2,3'],
  318. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  319. ]);
  320. $query = AgentRecharge::query()->with('agent:id,username,real_name,type');
  321. foreach (['agent_id', 'order_no', 'txid', 'type'] as $field) if (!empty($data[$field])) $query->where($field, $data[$field]);
  322. if (isset($data['status'])) $query->where('status', (int) $data['status']);
  323. return $this->paginate($query, $data);
  324. });
  325. }
  326. public function auditRecharge(Request $request, AgentRechargeService $service)
  327. {
  328. return $this->execute(function () use ($request, $service) {
  329. $data = $request->validate([
  330. 'id' => ['required', 'integer'], 'status' => ['required', 'integer', 'in:1,2'],
  331. 'remark' => ['required_if:status,2', 'nullable', 'string', 'max:500'],
  332. ]);
  333. return $service->audit((int) $data['id'], (int) $data['status'], (int) request()->user->id, (string) ($data['remark'] ?? ''));
  334. });
  335. }
  336. /** 代理人民币充值(在线代收 + 人工充值)。 */
  337. public function paymentOrders(Request $request, AgentPaymentService $service)
  338. {
  339. return $this->execute(function () use ($request, $service) {
  340. $data = $request->validate([
  341. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  342. 'agent_id' => ['nullable', 'integer'], 'order_no' => ['nullable', 'string', 'max:64'],
  343. 'type' => ['nullable', 'integer', 'in:1,3'], 'channel' => ['nullable', 'string', 'max:50'],
  344. 'payment_type' => ['nullable', 'integer'], 'status' => ['nullable', 'integer', 'between:0,7'],
  345. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  346. ]);
  347. return $service->paginate($data);
  348. });
  349. }
  350. public function paymentOrderPayData(Request $request, AgentPaymentService $service)
  351. {
  352. return $this->execute(function () use ($request, $service) {
  353. $data = $request->validate([
  354. 'ids' => ['required', 'array', 'min:1'], 'ids.*' => ['required', 'integer', 'min:1'],
  355. 'payment_type' => ['required', 'integer'], 'pay_data' => ['required', 'string'],
  356. ]);
  357. return ['updated' => $service->setPayData($data['ids'], (int) $data['payment_type'], $data['pay_data'])];
  358. });
  359. }
  360. public function auditPaymentOrder(Request $request, AgentPaymentService $service)
  361. {
  362. return $this->execute(function () use ($request, $service) {
  363. $data = $request->validate([
  364. 'id' => ['required', 'integer'], 'status' => ['required', 'integer', 'in:2,3'],
  365. 'remark' => ['required_if:status,3', 'nullable', 'string', 'max:255'],
  366. ]);
  367. return $service->manualAudit((int) $data['id'], (int) $data['status'], (string) ($data['remark'] ?? ''), (int) request()->user->id);
  368. });
  369. }
  370. public function rejectPaymentOrder(Request $request, AgentPaymentService $service)
  371. {
  372. return $this->execute(function () use ($request, $service) {
  373. $data = $request->validate(['id' => ['required', 'integer'], 'remark' => ['nullable', 'string', 'max:120']]);
  374. return $service->reject((int) $data['id'], (string) ($data['remark'] ?? ''), (int) request()->user->id);
  375. });
  376. }
  377. public function queryPaymentOrder(Request $request, AgentPaymentService $service)
  378. {
  379. return $this->execute(function () use ($request, $service) {
  380. $data = $request->validate(['id' => ['required', 'integer']]);
  381. $result = $service->query((int) $data['id']);
  382. return ['paid' => (int) ($result['code'] ?? 0) === 1, 'msg' => (string) ($result['msg'] ?? '')];
  383. });
  384. }
  385. public function profitEntries(Request $request)
  386. {
  387. return $this->execute(function () use ($request) {
  388. $data = $request->validate([
  389. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  390. 'agent_id' => ['nullable', 'integer'], 'user_id' => ['nullable', 'integer'], 'category' => ['nullable', 'string', 'max:32'],
  391. 'status' => ['nullable', 'in:counted,skipped_inactive,skipped_no_plan,skipped_category'],
  392. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  393. ]);
  394. $query = AgentProfitEntry::query();
  395. foreach (['agent_id', 'user_id', 'category', 'status'] as $field) if (!empty($data[$field])) $query->where($field, $data[$field]);
  396. if (!empty($data['start_date'])) $query->where('settle_date', '>=', $data['start_date']);
  397. if (!empty($data['end_date'])) $query->where('settle_date', '<=', $data['end_date']);
  398. $page = max(1, (int) ($data['page'] ?? 1));
  399. $limit = min(100, max(1, (int) ($data['limit'] ?? 20)));
  400. $total = (clone $query)->count();
  401. $list = $query->orderByDesc('id')->forPage($page, $limit)->get()->each->makeVisible('realtime_balance_after');
  402. return compact('total', 'page', 'limit', 'list');
  403. });
  404. }
  405. private function rules(bool $update = false): array
  406. {
  407. return [
  408. 'language' => ['sometimes', 'required', 'in:zh,en,vi'],
  409. 'type' => [$update ? 'nullable' : 'required', 'integer', 'in:1,2'],
  410. 'username' => $update
  411. ? ['sometimes', 'string', 'min:4', 'max:64', 'alpha_dash', Rule::unique('agents', 'username')->ignore((int) request()->input('id'))]
  412. : ['required', 'string', 'min:4', 'max:64', 'alpha_dash', 'unique:agents,username'],
  413. 'password' => [$update ? 'nullable' : 'required', 'string', 'min:6', 'max:100'],
  414. 'withdraw_password' => ['nullable', 'string', 'min:6', 'max:100'], 'real_name' => ['nullable', 'string', 'max:128'],
  415. 'deposit_fee_rate' => ['nullable', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  416. 'withdraw_fee_rate' => ['nullable', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  417. 'status' => ['nullable', 'integer', 'in:0,1'],
  418. 'initial_balance' => $update ? ['prohibited'] : ['nullable', 'numeric', 'min:0', new DecimalNumber()],
  419. ];
  420. }
  421. private function paginate($query, array $data, string $orderBy = 'id', bool $filterCreatedAt = true, string $dateColumn = 'created_at'): array
  422. {
  423. $page = max(1, (int) ($data['page'] ?? 1));
  424. $limit = min(100, max(1, (int) ($data['limit'] ?? 20)));
  425. if ($filterCreatedAt && !empty($data['start_date'])) $query->where($dateColumn, '>=', $data['start_date'] . ' 00:00:00');
  426. if ($filterCreatedAt && !empty($data['end_date'])) $query->where($dateColumn, '<=', $data['end_date'] . ' 23:59:59');
  427. $total = (clone $query)->count();
  428. $list = $query->orderByDesc($orderBy)->orderByDesc('id')->forPage($page, $limit)->get();
  429. return compact('total', 'page', 'limit', 'list');
  430. }
  431. private function formatAgent(AgentModel $agent, AgentDomainService $domains): array
  432. {
  433. $token = $agent->token;
  434. $online = $token
  435. && $token->expires_at->isFuture()
  436. && $token->last_used_at
  437. && $token->last_used_at->gte(now()->subMinutes(config('agent.online_minutes', 5)));
  438. $data = $agent->toArray();
  439. unset($data['token']);
  440. $wallets = app(AgentWalletService::class);
  441. return array_merge($data, $domains->format($agent), $wallets->funds($agent), [
  442. // 总后台可以看到实时保证金;代理端不返回。
  443. 'realtime_balance' => (string) $agent->realtime_balance,
  444. 'realtime_margin' => $agent->isJoint() ? $wallets->effectiveMargin($agent) : null,
  445. 'login_status' => $online ? 1 : 0,
  446. 'login_status_text' => $online ? '在线' : '离线',
  447. 'last_active_at' => $token?->last_used_at?->toDateTimeString(),
  448. ]);
  449. }
  450. }