From 23fd59dc88661626ff5826028fa2dfc45281a846 Mon Sep 17 00:00:00 2001 From: hskwon Date: Mon, 15 Dec 2025 14:59:07 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20group=5Fid=20=EB=84=A4=EC=9D=B4?= =?UTF-8?q?=EB=B0=8D=20=ED=86=B5=EC=9D=BC=20(code=5Fgroup=3D'group',=20cod?= =?UTF-8?q?e=3D'1')?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 마이그레이션: code_group='item_group' → 'group', code='ITEM' → '1' - ItemService: group_id로 code 조회 후 parent_id 매칭 - API: /api/v1/items?group_id=1 → 품목 그룹 전체 조회 --- app/Services/ItemService.php | 16 +++++++++-- ..._143850_add_item_group_to_common_codes.php | 27 ++++++++++--------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/app/Services/ItemService.php b/app/Services/ItemService.php index 552d0ce..02a590b 100644 --- a/app/Services/ItemService.php +++ b/app/Services/ItemService.php @@ -82,14 +82,26 @@ private function newQuery(string $itemType) /** * group_id로 해당 그룹의 item_type 목록 조회 * - * @param int $groupId common_codes의 item_group id + * @param int $groupId 그룹 코드 (code_group='group', code='1' → group_id=1) * @return array item_type 코드 배열 ['FG', 'PT', 'SM', 'RM', 'CS'] */ private function getItemTypesByGroupId(int $groupId): array { + // 1. group_id로 그룹 레코드 찾기 (code_group='group', code=group_id) + $group = \DB::table('common_codes') + ->where('code_group', 'group') + ->where('code', (string) $groupId) + ->where('is_active', true) + ->first(); + + if (! $group) { + return []; + } + + // 2. 해당 그룹의 id를 parent_id로 가진 item_type 조회 return \DB::table('common_codes') ->where('code_group', 'item_type') - ->where('parent_id', $groupId) + ->where('parent_id', $group->id) ->where('is_active', true) ->pluck('code') ->toArray(); diff --git a/database/migrations/2025_12_15_143850_add_item_group_to_common_codes.php b/database/migrations/2025_12_15_143850_add_item_group_to_common_codes.php index 460ec8f..f5d9f1e 100644 --- a/database/migrations/2025_12_15_143850_add_item_group_to_common_codes.php +++ b/database/migrations/2025_12_15_143850_add_item_group_to_common_codes.php @@ -4,21 +4,24 @@ use Illuminate\Support\Facades\DB; /** - * common_codes에 item_group 추가 및 item_type과 연결 + * common_codes에 그룹 추가 및 item_type과 연결 * * 구조: - * - item_group (부모): code_group='item_group', code='ITEM', name='품목' - * - item_type (자식): parent_id로 item_group 연결 + * - group (부모): code_group='group', code='1', name='품목' + * - item_type (자식): parent_id로 group 연결 + * + * API 사용: + * - /api/v1/items?group_id=1 → 품목 그룹 전체 (FG, PT, SM, RM, CS) */ return new class extends Migration { public function up(): void { - // 1. item_group 추가 (품목 그룹) - $itemGroupId = DB::table('common_codes')->insertGetId([ + // 1. 품목 그룹 추가 (code_group='group', code='1') + $groupId = DB::table('common_codes')->insertGetId([ 'tenant_id' => 1, - 'code_group' => 'item_group', - 'code' => 'ITEM', + 'code_group' => 'group', + 'code' => '1', 'name' => '품목', 'description' => '품목 관리 그룹 (FG, PT, SM, RM, CS)', 'is_active' => true, @@ -27,11 +30,11 @@ public function up(): void 'updated_at' => now(), ]); - // 2. 기존 item_type의 parent_id를 item_group으로 연결 + // 2. 기존 item_type의 parent_id를 group으로 연결 DB::table('common_codes') ->where('code_group', 'item_type') ->whereIn('code', ['FG', 'PT', 'SM', 'RM', 'CS']) - ->update(['parent_id' => $itemGroupId]); + ->update(['parent_id' => $groupId]); // 3. item_type의 attributes에서 source_table을 items로 통일 $itemTypes = DB::table('common_codes') @@ -57,10 +60,10 @@ public function down(): void ->whereIn('code', ['FG', 'PT', 'SM', 'RM', 'CS']) ->update(['parent_id' => null]); - // 2. item_group 삭제 + // 2. group 삭제 DB::table('common_codes') - ->where('code_group', 'item_group') - ->where('code', 'ITEM') + ->where('code_group', 'group') + ->where('code', '1') ->delete(); } };