You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
3.5 KiB
120 lines
3.5 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\ResearchDirection;
|
|
use App\Models\Teacher;
|
|
use App\Support\ApiResponse;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ResearchDirectionController extends Controller
|
|
{
|
|
use ApiResponse;
|
|
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$query = ResearchDirection::query();
|
|
|
|
if ($kw = $request->query('keyword')) {
|
|
$query->where('name', 'like', "%{$kw}%");
|
|
}
|
|
if ($request->filled('status')) {
|
|
$query->where('status', (int) $request->query('status'));
|
|
}
|
|
|
|
$paginator = $query
|
|
->orderBy('sort')
|
|
->orderBy('id')
|
|
->paginate((int) $request->query('page_size', 20))
|
|
->withQueryString();
|
|
|
|
$paginator->getCollection()->transform(fn (ResearchDirection $r) => $this->serialize($r));
|
|
|
|
return $this->paginated($paginator);
|
|
}
|
|
|
|
public function options(): JsonResponse
|
|
{
|
|
$items = ResearchDirection::query()
|
|
->where('status', 1)
|
|
->orderBy('sort')
|
|
->orderBy('name')
|
|
->get()
|
|
->map(fn (ResearchDirection $r) => $this->serialize($r));
|
|
|
|
return $this->ok(['items' => $items]);
|
|
}
|
|
|
|
public function store(Request $request): JsonResponse
|
|
{
|
|
$data = $request->validate([
|
|
'name' => ['required', 'string', 'max:255', 'unique:research_directions,name'],
|
|
'sort' => ['nullable', 'integer', 'min:0'],
|
|
'status' => ['required', 'integer', 'in:0,1'],
|
|
'remark' => ['nullable', 'string', 'max:512'],
|
|
]);
|
|
|
|
$row = ResearchDirection::query()->create([
|
|
'name' => trim($data['name']),
|
|
'sort' => (int) ($data['sort'] ?? 0),
|
|
'status' => (int) $data['status'],
|
|
'remark' => $data['remark'] ?? null,
|
|
]);
|
|
|
|
return $this->ok($this->serialize($row), '已创建');
|
|
}
|
|
|
|
public function update(Request $request, int $researchDirection): JsonResponse
|
|
{
|
|
$row = ResearchDirection::query()->findOrFail($researchDirection);
|
|
$data = $request->validate([
|
|
'name' => ['sometimes', 'string', 'max:255', 'unique:research_directions,name,'.$row->id],
|
|
'sort' => ['nullable', 'integer', 'min:0'],
|
|
'status' => ['sometimes', 'integer', 'in:0,1'],
|
|
'remark' => ['nullable', 'string', 'max:512'],
|
|
]);
|
|
|
|
if (isset($data['name'])) {
|
|
$data['name'] = trim($data['name']);
|
|
}
|
|
|
|
$row->fill($data);
|
|
$row->save();
|
|
|
|
return $this->ok($this->serialize($row->fresh()), '已保存');
|
|
}
|
|
|
|
public function destroy(int $researchDirection): JsonResponse
|
|
{
|
|
$row = ResearchDirection::query()->findOrFail($researchDirection);
|
|
|
|
if ($row->adminUsers()->exists()) {
|
|
return $this->fail('该研究方向已分配给网格员,无法删除', 422);
|
|
}
|
|
|
|
$row->delete();
|
|
|
|
return $this->ok(null, '已删除');
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
protected function serialize(ResearchDirection $r): array
|
|
{
|
|
$teacherCount = $r->teachers()->count();
|
|
|
|
return [
|
|
'id' => $r->id,
|
|
'name' => $r->name,
|
|
'sort' => (int) $r->sort,
|
|
'status' => (int) $r->status,
|
|
'remark' => $r->remark,
|
|
'teacher_count' => $teacherCount,
|
|
'created_at' => $r->created_at?->toIso8601String(),
|
|
];
|
|
}
|
|
}
|