2025-08-22 19:30:05 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
|
|
use App\Models\Commons\Classification;
|
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
|
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
|
|
|
|
|
|
class ClassificationService extends Service
|
|
|
|
|
{
|
|
|
|
|
/** 목록(검색/페이징) */
|
|
|
|
|
public function index(array $params)
|
|
|
|
|
{
|
|
|
|
|
$tenantId = $this->tenantId();
|
|
|
|
|
|
2025-11-06 17:45:49 +09:00
|
|
|
$page = (int) ($params['page'] ?? 1);
|
|
|
|
|
$size = (int) ($params['size'] ?? 20);
|
|
|
|
|
$q = trim((string) ($params['q'] ?? ''));
|
2025-08-22 19:30:05 +09:00
|
|
|
$group = $params['group'] ?? null;
|
2025-11-06 17:45:49 +09:00
|
|
|
$only = $params['only_active'] ?? null;
|
2025-08-22 19:30:05 +09:00
|
|
|
|
|
|
|
|
$query = Classification::query()->where('tenant_id', $tenantId);
|
|
|
|
|
|
|
|
|
|
if ($q !== '') {
|
|
|
|
|
$query->where(function ($w) use ($q) {
|
|
|
|
|
$w->where('name', 'like', "%{$q}%")
|
|
|
|
|
->orWhere('code', 'like', "%{$q}%");
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-11-06 17:45:49 +09:00
|
|
|
if (! is_null($group) && $group !== '') {
|
2025-08-22 19:30:05 +09:00
|
|
|
$query->where('group', $group);
|
|
|
|
|
}
|
2025-11-06 17:45:49 +09:00
|
|
|
if (! is_null($only)) {
|
|
|
|
|
$query->where('is_active', (int) (bool) $only);
|
2025-08-22 19:30:05 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$query->orderBy('group')->orderBy('code')->orderBy('id');
|
|
|
|
|
|
|
|
|
|
return $query->paginate($size, ['*'], 'page', $page);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 단건 */
|
|
|
|
|
public function show(int $id)
|
|
|
|
|
{
|
|
|
|
|
$tenantId = $this->tenantId();
|
|
|
|
|
$row = Classification::where('tenant_id', $tenantId)->find($id);
|
2025-11-06 17:45:49 +09:00
|
|
|
if (! $row) {
|
|
|
|
|
throw new NotFoundHttpException(__('error.not_found'));
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-22 19:30:05 +09:00
|
|
|
return $row;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 생성 */
|
|
|
|
|
public function store(array $params)
|
|
|
|
|
{
|
|
|
|
|
$tenantId = $this->tenantId();
|
2025-11-06 17:45:49 +09:00
|
|
|
$uid = $this->apiUserId();
|
2025-08-22 19:30:05 +09:00
|
|
|
|
|
|
|
|
$v = Validator::make($params, [
|
2025-11-06 17:45:49 +09:00
|
|
|
'group' => 'required|string|max:50',
|
|
|
|
|
'code' => 'required|string|max:50',
|
|
|
|
|
'name' => 'required|string|max:100',
|
|
|
|
|
'is_active' => 'nullable|boolean',
|
2025-08-22 19:30:05 +09:00
|
|
|
]);
|
2025-11-06 17:45:49 +09:00
|
|
|
if ($v->fails()) {
|
|
|
|
|
throw new BadRequestHttpException($v->errors()->first());
|
|
|
|
|
}
|
2025-08-22 19:30:05 +09:00
|
|
|
|
|
|
|
|
// 그룹 내 코드 중복 체크(tenant 기준)
|
|
|
|
|
$data = $v->validated();
|
|
|
|
|
$exists = Classification::where('tenant_id', $tenantId)
|
|
|
|
|
->where('group', $data['group'])
|
2025-11-06 17:45:49 +09:00
|
|
|
->where('code', $data['code'])
|
2025-08-22 19:30:05 +09:00
|
|
|
->exists();
|
2025-11-06 17:45:49 +09:00
|
|
|
if ($exists) {
|
|
|
|
|
throw new BadRequestHttpException(__('validation.unique', ['attribute' => 'code']));
|
|
|
|
|
}
|
2025-08-22 19:30:05 +09:00
|
|
|
|
|
|
|
|
$data['tenant_id'] = $tenantId;
|
2025-11-06 17:45:49 +09:00
|
|
|
$data['is_active'] = (int) ($data['is_active'] ?? 1);
|
2025-08-22 19:30:05 +09:00
|
|
|
|
|
|
|
|
return Classification::create($data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 수정 */
|
|
|
|
|
public function update(int $id, array $params)
|
|
|
|
|
{
|
|
|
|
|
$tenantId = $this->tenantId();
|
|
|
|
|
|
|
|
|
|
$row = Classification::where('tenant_id', $tenantId)->find($id);
|
2025-11-06 17:45:49 +09:00
|
|
|
if (! $row) {
|
|
|
|
|
throw new NotFoundHttpException(__('error.not_found'));
|
|
|
|
|
}
|
2025-08-22 19:30:05 +09:00
|
|
|
|
|
|
|
|
$v = Validator::make($params, [
|
2025-11-06 17:45:49 +09:00
|
|
|
'group' => 'sometimes|required|string|max:50',
|
|
|
|
|
'code' => 'sometimes|required|string|max:50',
|
|
|
|
|
'name' => 'sometimes|required|string|max:100',
|
|
|
|
|
'is_active' => 'nullable|boolean',
|
2025-08-22 19:30:05 +09:00
|
|
|
]);
|
2025-11-06 17:45:49 +09:00
|
|
|
if ($v->fails()) {
|
|
|
|
|
throw new BadRequestHttpException($v->errors()->first());
|
|
|
|
|
}
|
2025-08-22 19:30:05 +09:00
|
|
|
|
|
|
|
|
$payload = $v->validated();
|
|
|
|
|
|
|
|
|
|
// group 또는 code 변경 시 유니크 검사
|
|
|
|
|
$newGroup = $payload['group'] ?? $row->group;
|
2025-11-06 17:45:49 +09:00
|
|
|
$newCode = $payload['code'] ?? $row->code;
|
2025-08-22 19:30:05 +09:00
|
|
|
$dupe = Classification::where('tenant_id', $tenantId)
|
|
|
|
|
->where('group', $newGroup)
|
2025-11-06 17:45:49 +09:00
|
|
|
->where('code', $newCode)
|
2025-08-22 19:30:05 +09:00
|
|
|
->where('id', '!=', $row->id)
|
|
|
|
|
->exists();
|
2025-11-06 17:45:49 +09:00
|
|
|
if ($dupe) {
|
|
|
|
|
throw new BadRequestHttpException(__('validation.unique', ['attribute' => 'code']));
|
|
|
|
|
}
|
2025-08-22 19:30:05 +09:00
|
|
|
|
|
|
|
|
$row->update($payload);
|
2025-11-06 17:45:49 +09:00
|
|
|
|
2025-08-22 19:30:05 +09:00
|
|
|
return $row->refresh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 삭제(soft) */
|
|
|
|
|
public function destroy(int $id)
|
|
|
|
|
{
|
|
|
|
|
$tenantId = $this->tenantId();
|
|
|
|
|
|
|
|
|
|
$row = Classification::where('tenant_id', $tenantId)->find($id);
|
2025-11-06 17:45:49 +09:00
|
|
|
if (! $row) {
|
|
|
|
|
throw new NotFoundHttpException(__('error.not_found'));
|
|
|
|
|
}
|
2025-08-22 19:30:05 +09:00
|
|
|
|
|
|
|
|
$row->delete();
|
2025-11-06 17:45:49 +09:00
|
|
|
|
2025-08-22 19:30:05 +09:00
|
|
|
return 'success';
|
|
|
|
|
}
|
|
|
|
|
}
|