| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php
- namespace App\Http\Controllers\agent;
- use App\Http\Controllers\AgentApiController;
- use App\Models\Agent\Agent;
- use App\Models\Agent\AgentRebateRecord;
- use App\Models\Agent\AgentCommissionRule;
- use App\Services\Agent\AgentCommissionService;
- use App\Services\Agent\AgentReportService;
- use App\Services\Agent\AgentService;
- use Illuminate\Http\Request;
- use App\Rules\DecimalNumber;
- class CommissionController extends AgentApiController
- {
- public function records(Request $request, AgentReportService $reports)
- {
- return $this->execute(function () use ($request, $reports) {
- $data = $request->validate([
- 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
- 'order_no' => ['nullable', 'string', 'max:48'], 'username' => ['nullable', 'string', 'max:64'],
- 'platform' => ['nullable', 'string', 'max:32'],
- 'flow_status' => ['nullable', 'in:pending,credited'],
- 'profit_status' => ['nullable', 'in:pending,credited'],
- 'settlement_date' => ['nullable', 'date_format:Y-m-d'],
- '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 = AgentRebateRecord::query()->with('agent:id,username')->whereIn('agent_id', $ids);
- foreach (['order_no', 'platform', 'flow_status', 'profit_status'] as $field) {
- if (!empty($data[$field])) $query->where($field, $data[$field]);
- }
- if (!empty($data['username'])) {
- $query->whereHas('agent', fn($query) => $query->where('username', 'like', '%' . $data['username'] . '%'));
- }
- if (!empty($data['settlement_date'])) $query->where('settlement_date', $data['settlement_date']);
- 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('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)],
- '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->saveFlowRate(Agent::query()->findOrFail($data['agent_id']), (string) $data['flow_rate'], $data['effective_from'], null);
- });
- }
- }
|