execute(function () use ($request) { $data = $request->validate([ 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'], 'type' => ['nullable', 'string', 'max:32'], 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'], ]); return $this->pageQuery(AgentBalanceLog::query()->where('agent_id', $this->currentAgent()->id), $data); }); } public function accounts(Request $request) { return $this->execute(function () use ($request) { $data = $request->validate(['type' => ['nullable', 'in:bank,alipay,wechat,usdt,other']]); return AgentWithdrawAccount::query()->where('agent_id', $this->currentAgent()->id) ->when(!empty($data['type']), fn($query) => $query->where('type', $data['type'])) ->orderByDesc('id')->get(); }); } public function saveAccount(Request $request) { return $this->execute(function () use ($request) { $data = $request->validate([ 'id' => ['nullable', 'integer'], 'type' => ['required', 'in:bank,alipay,wechat,usdt,other'], 'name' => ['nullable', 'string', 'max:128'], 'account' => ['required', 'string', 'max:255'], 'bank_name' => ['nullable', 'string', 'max:128'], 'network' => ['nullable', 'string', 'max:32'], 'details' => ['nullable', 'array'], 'status' => ['nullable', 'integer', 'in:0,1'], ]); $agent = $this->currentAgent(); $account = empty($data['id']) ? new AgentWithdrawAccount() : AgentWithdrawAccount::query()->where('agent_id', $agent->id)->findOrFail($data['id']); $account->fill($data); $account->agent_id = $agent->id; $account->save(); return $account; }); } public function deleteAccount(Request $request) { return $this->execute(function () use ($request) { $data = $request->validate(['id' => ['required', 'integer']]); $account = AgentWithdrawAccount::query()->where('agent_id', $this->currentAgent()->id)->findOrFail($data['id']); if (AgentWithdrawal::query()->where('account_id', $account->id)->whereIn('status', ['pending', 'approved'])->exists()) { throw new \RuntimeException('该账户有处理中的提现,不能删除'); } $account->delete(); return []; }); } public function withdrawals(Request $request) { return $this->execute(function () use ($request) { $data = $request->validate([ 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'], 'status' => ['nullable', 'in:pending,approved,rejected,paid'], 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'], ]); return $this->pageQuery(AgentWithdrawal::query()->where('agent_id', $this->currentAgent()->id), $data); }); } public function requestWithdrawal(Request $request, AgentWithdrawalService $service) { return $this->execute(function () use ($request, $service) { $data = $request->validate([ 'account_id' => ['required', 'integer'], 'amount' => ['required', 'numeric', 'min:0.0001', new DecimalNumber()], 'withdraw_password' => ['required', 'string'], 'remark' => ['nullable', 'string', 'max:500'], 'request_id' => ['required', 'string', 'max:64'], ]); return $service->request($this->currentAgent(), (int) $data['account_id'], (string) $data['amount'], $data['withdraw_password'], $data['request_id'], (string) ($data['remark'] ?? '')); }); } private function pageQuery($query, array $data): array { $page = max(1, (int) ($data['page'] ?? 1)); $limit = min(100, max(1, (int) ($data['limit'] ?? 20))); if (!empty($data['type'])) $query->where('type', $data['type']); if (!empty($data['status'])) $query->where('status', $data['status']); if (!empty($data['start_date'])) $query->where('created_at', '>=', $data['start_date'] . ' 00:00:00'); if (!empty($data['end_date'])) $query->where('created_at', '<=', $data['end_date'] . ' 23:59:59'); $total = (clone $query)->count(); $list = $query->orderByDesc('id')->forPage($page, $limit)->get(); return compact('total', 'page', 'limit', 'list'); } }