execute(function () use ($request, $reports) { $data = $request->validate([ 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'], 'type' => ['nullable', 'in:flow,profit'], 'status' => ['nullable', 'in:pending,credited'], 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'], 'include_children' => ['nullable', 'boolean'], ]); $page = max(1, (int) ($data['page'] ?? 1)); $limit = min(100, max(1, (int) ($data['limit'] ?? 20))); $agent = $this->currentAgent(); $ids = !empty($data['include_children']) ? $reports->visibleAgentIds($agent) : [(int) $agent->id]; $query = AgentCommission::query()->with('agent:id,username')->whereIn('agent_id', $ids); 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('settlement_date', '>=', $data['start_date']); if (!empty($data['end_date'])) $query->where('settlement_date', '<=', $data['end_date']); $total = (clone $query)->count(); $list = $query->orderByDesc('settlement_date')->orderByDesc('id')->forPage($page, $limit)->get(); return compact('total', 'page', 'limit', 'list'); }); } public function rules(Request $request, AgentReportService $reports) { return $this->execute(function () use ($request, $reports) { $data = $request->validate(['include_children' => ['nullable', 'boolean']]); $agent = $this->currentAgent(); $ids = !empty($data['include_children']) ? $reports->visibleAgentIds($agent) : [(int) $agent->id]; return AgentCommissionRule::query()->with('agent:id,username')->whereIn('agent_id', $ids)->orderByDesc('effective_from')->get(); }); } public function saveRule(Request $request, AgentService $agents, AgentCommissionService $commissions) { return $this->execute(function () use ($request, $agents, $commissions) { $root = $this->currentAgent(); $data = $request->validate([ 'agent_id' => ['required', 'integer', 'exists:agents,id'], 'flow_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)], 'profit_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)], 'effective_from' => ['required', 'date_format:Y-m-d', 'date_equals:' . now()->toDateString()], ]); if ((int) $data['agent_id'] === (int) $root->id) throw new \RuntimeException('代理不能修改自己的佣金比例'); $agents->assertVisible($root, (int) $data['agent_id']); return $commissions->saveRule(Agent::query()->findOrFail($data['agent_id']), (string) $data['flow_rate'], (string) $data['profit_rate'], $data['effective_from'], null); }); } }