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.
104 lines
3.2 KiB
104 lines
3.2 KiB
|
2 weeks ago
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Admin;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Models\DictItem;
|
||
|
|
use App\Models\DictType;
|
||
|
|
use App\Support\ApiResponse;
|
||
|
|
use Illuminate\Http\JsonResponse;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
|
||
|
|
class DictItemController extends Controller
|
||
|
|
{
|
||
|
|
use ApiResponse;
|
||
|
|
|
||
|
|
public function index(Request $request, int $dictTypeId): JsonResponse
|
||
|
|
{
|
||
|
|
DictType::query()->findOrFail($dictTypeId);
|
||
|
|
|
||
|
|
$query = DictItem::query()->where('dict_type_id', $dictTypeId);
|
||
|
|
|
||
|
|
if ($request->filled('status')) {
|
||
|
|
$query->where('status', (int) $request->query('status'));
|
||
|
|
}
|
||
|
|
|
||
|
|
$items = $query->orderBy('sort')->orderBy('id')->get()->map(fn (DictItem $i) => [
|
||
|
|
'id' => $i->id,
|
||
|
|
'dict_type_id' => $i->dict_type_id,
|
||
|
|
'label' => $i->label,
|
||
|
|
'value' => $i->value,
|
||
|
|
'sort' => (int) $i->sort,
|
||
|
|
'status' => (int) $i->status,
|
||
|
|
'extra_json' => $i->extra_json,
|
||
|
|
]);
|
||
|
|
|
||
|
|
return $this->ok($items->values()->all());
|
||
|
|
}
|
||
|
|
|
||
|
|
public function store(Request $request, int $dictTypeId): JsonResponse
|
||
|
|
{
|
||
|
|
DictType::query()->findOrFail($dictTypeId);
|
||
|
|
|
||
|
|
$data = $request->validate([
|
||
|
|
'label' => ['required', 'string', 'max:128'],
|
||
|
|
'value' => ['required', 'string', 'max:128'],
|
||
|
|
'sort' => ['nullable', 'integer'],
|
||
|
|
'status' => ['required', 'integer', 'in:0,1'],
|
||
|
|
'extra_json' => ['nullable', 'array'],
|
||
|
|
]);
|
||
|
|
|
||
|
|
$item = DictItem::query()->create([
|
||
|
|
'dict_type_id' => $dictTypeId,
|
||
|
|
'label' => $data['label'],
|
||
|
|
'value' => $data['value'],
|
||
|
|
'sort' => (int) ($data['sort'] ?? 0),
|
||
|
|
'status' => (int) $data['status'],
|
||
|
|
'extra_json' => $data['extra_json'] ?? null,
|
||
|
|
]);
|
||
|
|
|
||
|
|
return $this->ok(['id' => $item->id], '已创建');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function update(Request $request, int $dictTypeId, int $dictItem): JsonResponse
|
||
|
|
{
|
||
|
|
DictType::query()->findOrFail($dictTypeId);
|
||
|
|
$model = DictItem::query()->where('dict_type_id', $dictTypeId)->findOrFail($dictItem);
|
||
|
|
|
||
|
|
$data = $request->validate([
|
||
|
|
'label' => ['sometimes', 'string', 'max:128'],
|
||
|
|
'value' => ['sometimes', 'string', 'max:128'],
|
||
|
|
'sort' => ['nullable', 'integer'],
|
||
|
|
'status' => ['sometimes', 'integer', 'in:0,1'],
|
||
|
|
'extra_json' => ['nullable', 'array'],
|
||
|
|
]);
|
||
|
|
|
||
|
|
if (isset($data['label'])) {
|
||
|
|
$model->label = $data['label'];
|
||
|
|
}
|
||
|
|
if (isset($data['value'])) {
|
||
|
|
$model->value = $data['value'];
|
||
|
|
}
|
||
|
|
if (isset($data['sort'])) {
|
||
|
|
$model->sort = (int) $data['sort'];
|
||
|
|
}
|
||
|
|
if (isset($data['status'])) {
|
||
|
|
$model->status = (int) $data['status'];
|
||
|
|
}
|
||
|
|
if (array_key_exists('extra_json', $data)) {
|
||
|
|
$model->extra_json = $data['extra_json'];
|
||
|
|
}
|
||
|
|
$model->save();
|
||
|
|
|
||
|
|
return $this->ok(null, '已保存');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function destroy(int $dictTypeId, int $dictItem): JsonResponse
|
||
|
|
{
|
||
|
|
$model = DictItem::query()->where('dict_type_id', $dictTypeId)->findOrFail($dictItem);
|
||
|
|
$model->delete();
|
||
|
|
|
||
|
|
return $this->ok(null, '已删除');
|
||
|
|
}
|
||
|
|
}
|