Merge develop and fix conflicts in routes/web.php (AI config and Categories)
This commit is contained in:
291
app/Http/Controllers/Api/Admin/CategoryApiController.php
Normal file
291
app/Http/Controllers/Api/Admin/CategoryApiController.php
Normal file
@@ -0,0 +1,291 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Category;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class CategoryApiController extends Controller
|
||||
{
|
||||
/**
|
||||
* 목록 조회 (드롭다운용)
|
||||
*/
|
||||
public function list(Request $request): JsonResponse
|
||||
{
|
||||
$tenantId = session('selected_tenant_id');
|
||||
if (! $tenantId) {
|
||||
return response()->json(['success' => false, 'data' => []]);
|
||||
}
|
||||
|
||||
$codeGroup = $request->input('code_group', 'product');
|
||||
|
||||
$categories = Category::query()
|
||||
->where('tenant_id', $tenantId)
|
||||
->where('code_group', $codeGroup)
|
||||
->whereNull('deleted_at')
|
||||
->orderBy('parent_id')
|
||||
->orderBy('sort_order')
|
||||
->get(['id', 'parent_id', 'code', 'name']);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => $categories,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 트리 데이터 조회 (HTMX)
|
||||
*/
|
||||
public function tree(Request $request): View
|
||||
{
|
||||
$tenantId = session('selected_tenant_id');
|
||||
$codeGroup = $request->input('code_group', 'product');
|
||||
|
||||
$categories = collect();
|
||||
if ($tenantId) {
|
||||
$categories = Category::query()
|
||||
->where('tenant_id', $tenantId)
|
||||
->where('code_group', $codeGroup)
|
||||
->whereNull('parent_id')
|
||||
->with('childrenRecursive')
|
||||
->orderBy('sort_order')
|
||||
->get();
|
||||
}
|
||||
|
||||
return view('categories.partials.tree', [
|
||||
'categories' => $categories,
|
||||
'codeGroup' => $codeGroup,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 단일 조회
|
||||
*/
|
||||
public function show(int $id): JsonResponse
|
||||
{
|
||||
$category = Category::with('children')->findOrFail($id);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => $category,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 생성
|
||||
*/
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$tenantId = session('selected_tenant_id');
|
||||
if (! $tenantId) {
|
||||
return response()->json(['success' => false, 'message' => '테넌트를 선택해주세요.'], 400);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'code_group' => 'required|string|max:50',
|
||||
'parent_id' => 'nullable|integer|exists:categories,id',
|
||||
'code' => 'required|string|max:50',
|
||||
'name' => 'required|string|max:100',
|
||||
'profile_code' => 'nullable|string|max:50',
|
||||
'description' => 'nullable|string|max:500',
|
||||
'is_active' => 'boolean',
|
||||
]);
|
||||
|
||||
// 코드 중복 체크
|
||||
$exists = Category::query()
|
||||
->where('tenant_id', $tenantId)
|
||||
->where('code_group', $validated['code_group'])
|
||||
->where('code', $validated['code'])
|
||||
->exists();
|
||||
|
||||
if ($exists) {
|
||||
return response()->json(['success' => false, 'message' => '이미 존재하는 코드입니다.'], 400);
|
||||
}
|
||||
|
||||
// 최대 sort_order 조회
|
||||
$maxSortOrder = Category::query()
|
||||
->where('tenant_id', $tenantId)
|
||||
->where('code_group', $validated['code_group'])
|
||||
->where('parent_id', $validated['parent_id'] ?? null)
|
||||
->max('sort_order') ?? 0;
|
||||
|
||||
$category = Category::create([
|
||||
'tenant_id' => $tenantId,
|
||||
'parent_id' => $validated['parent_id'] ?? null,
|
||||
'code_group' => $validated['code_group'],
|
||||
'code' => $validated['code'],
|
||||
'name' => $validated['name'],
|
||||
'profile_code' => $validated['profile_code'] ?? null,
|
||||
'description' => $validated['description'] ?? null,
|
||||
'is_active' => $validated['is_active'] ?? true,
|
||||
'sort_order' => $maxSortOrder + 1,
|
||||
'created_by' => Auth::id(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => '카테고리가 생성되었습니다.',
|
||||
'data' => $category,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 수정
|
||||
*/
|
||||
public function update(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$category = Category::findOrFail($id);
|
||||
|
||||
$validated = $request->validate([
|
||||
'code' => 'sometimes|required|string|max:50',
|
||||
'name' => 'sometimes|required|string|max:100',
|
||||
'profile_code' => 'nullable|string|max:50',
|
||||
'description' => 'nullable|string|max:500',
|
||||
'is_active' => 'sometimes|boolean',
|
||||
]);
|
||||
|
||||
// 코드 변경 시 중복 체크
|
||||
if (isset($validated['code']) && $validated['code'] !== $category->code) {
|
||||
$exists = Category::query()
|
||||
->where('tenant_id', $category->tenant_id)
|
||||
->where('code_group', $category->code_group)
|
||||
->where('code', $validated['code'])
|
||||
->where('id', '!=', $id)
|
||||
->exists();
|
||||
|
||||
if ($exists) {
|
||||
return response()->json(['success' => false, 'message' => '이미 존재하는 코드입니다.'], 400);
|
||||
}
|
||||
}
|
||||
|
||||
$category->update(array_merge($validated, ['updated_by' => Auth::id()]));
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => '카테고리가 수정되었습니다.',
|
||||
'data' => $category->fresh(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 삭제
|
||||
*/
|
||||
public function destroy(int $id): JsonResponse
|
||||
{
|
||||
$category = Category::findOrFail($id);
|
||||
|
||||
// 하위 카테고리 존재 여부 확인
|
||||
if ($category->children()->count() > 0) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => '하위 카테고리가 있어 삭제할 수 없습니다.',
|
||||
], 400);
|
||||
}
|
||||
|
||||
$category->update(['deleted_by' => Auth::id()]);
|
||||
$category->delete();
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => '카테고리가 삭제되었습니다.',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 활성 상태 토글
|
||||
*/
|
||||
public function toggle(int $id): JsonResponse
|
||||
{
|
||||
$category = Category::findOrFail($id);
|
||||
$category->update([
|
||||
'is_active' => ! $category->is_active,
|
||||
'updated_by' => Auth::id(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => $category->is_active ? '활성화되었습니다.' : '비활성화되었습니다.',
|
||||
'is_active' => $category->is_active,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 이동 (부모 변경)
|
||||
*/
|
||||
public function move(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$category = Category::findOrFail($id);
|
||||
|
||||
$validated = $request->validate([
|
||||
'parent_id' => 'nullable|integer|exists:categories,id',
|
||||
]);
|
||||
|
||||
$newParentId = $validated['parent_id'] ?? null;
|
||||
|
||||
// 자기 자신이나 자식을 부모로 설정 불가
|
||||
if ($newParentId === $id) {
|
||||
return response()->json(['success' => false, 'message' => '자기 자신을 부모로 설정할 수 없습니다.'], 400);
|
||||
}
|
||||
|
||||
// 자식 카테고리를 부모로 설정하는 것 방지 (순환 참조)
|
||||
if ($newParentId) {
|
||||
$parent = Category::find($newParentId);
|
||||
while ($parent) {
|
||||
if ($parent->id === $id) {
|
||||
return response()->json(['success' => false, 'message' => '하위 카테고리를 부모로 설정할 수 없습니다.'], 400);
|
||||
}
|
||||
$parent = $parent->parent;
|
||||
}
|
||||
}
|
||||
|
||||
$category->update([
|
||||
'parent_id' => $newParentId,
|
||||
'updated_by' => Auth::id(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => '카테고리가 이동되었습니다.',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 순서 변경
|
||||
*/
|
||||
public function reorder(Request $request): JsonResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'items' => 'required|array',
|
||||
'items.*.id' => 'required|integer|exists:categories,id',
|
||||
'items.*.sort_order' => 'required|integer|min:0',
|
||||
]);
|
||||
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
foreach ($validated['items'] as $item) {
|
||||
Category::where('id', $item['id'])->update([
|
||||
'sort_order' => $item['sort_order'],
|
||||
'updated_by' => Auth::id(),
|
||||
]);
|
||||
}
|
||||
DB::commit();
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => '순서가 변경되었습니다.',
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => '순서 변경 중 오류가 발생했습니다.',
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
295
app/Http/Controllers/Api/Admin/GlobalCategoryApiController.php
Normal file
295
app/Http/Controllers/Api/Admin/GlobalCategoryApiController.php
Normal file
@@ -0,0 +1,295 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Category;
|
||||
use App\Models\GlobalCategory;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class GlobalCategoryApiController extends Controller
|
||||
{
|
||||
/**
|
||||
* 목록 조회 (드롭다운용)
|
||||
*/
|
||||
public function list(Request $request): JsonResponse
|
||||
{
|
||||
$codeGroup = $request->input('code_group', 'product');
|
||||
|
||||
$categories = GlobalCategory::query()
|
||||
->where('code_group', $codeGroup)
|
||||
->whereNull('deleted_at')
|
||||
->orderBy('parent_id')
|
||||
->orderBy('sort_order')
|
||||
->get(['id', 'parent_id', 'code', 'name']);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => $categories,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 단일 조회
|
||||
*/
|
||||
public function show(int $id): JsonResponse
|
||||
{
|
||||
$category = GlobalCategory::findOrFail($id);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => $category,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 생성
|
||||
*/
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'code_group' => 'required|string|max:30',
|
||||
'parent_id' => 'nullable|integer|exists:global_categories,id',
|
||||
'code' => 'required|string|max:30',
|
||||
'name' => 'required|string|max:100',
|
||||
'profile_code' => 'nullable|string|max:30',
|
||||
'description' => 'nullable|string|max:255',
|
||||
'sort_order' => 'nullable|integer|min:0',
|
||||
]);
|
||||
|
||||
// 코드 중복 체크
|
||||
$exists = GlobalCategory::query()
|
||||
->where('code_group', $validated['code_group'])
|
||||
->where('code', $validated['code'])
|
||||
->whereNull('deleted_at')
|
||||
->exists();
|
||||
|
||||
if ($exists) {
|
||||
return response()->json(['success' => false, 'message' => '이미 존재하는 코드입니다.'], 400);
|
||||
}
|
||||
|
||||
// 최대 sort_order 조회
|
||||
$maxSortOrder = GlobalCategory::query()
|
||||
->where('code_group', $validated['code_group'])
|
||||
->where('parent_id', $validated['parent_id'] ?? null)
|
||||
->max('sort_order') ?? 0;
|
||||
|
||||
$category = GlobalCategory::create([
|
||||
'parent_id' => $validated['parent_id'] ?? null,
|
||||
'code_group' => $validated['code_group'],
|
||||
'code' => $validated['code'],
|
||||
'name' => $validated['name'],
|
||||
'profile_code' => $validated['profile_code'] ?? null,
|
||||
'description' => $validated['description'] ?? null,
|
||||
'is_active' => true,
|
||||
'sort_order' => $validated['sort_order'] ?? ($maxSortOrder + 1),
|
||||
'created_by' => Auth::id(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => '글로벌 카테고리가 생성되었습니다.',
|
||||
'data' => $category,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 수정
|
||||
*/
|
||||
public function update(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$category = GlobalCategory::findOrFail($id);
|
||||
|
||||
$validated = $request->validate([
|
||||
'name' => 'sometimes|required|string|max:100',
|
||||
'parent_id' => 'nullable|integer|exists:global_categories,id',
|
||||
'profile_code' => 'nullable|string|max:30',
|
||||
'description' => 'nullable|string|max:255',
|
||||
'sort_order' => 'nullable|integer|min:0',
|
||||
]);
|
||||
|
||||
$category->update(array_merge($validated, ['updated_by' => Auth::id()]));
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => '글로벌 카테고리가 수정되었습니다.',
|
||||
'data' => $category->fresh(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 삭제
|
||||
*/
|
||||
public function destroy(int $id): JsonResponse
|
||||
{
|
||||
$category = GlobalCategory::findOrFail($id);
|
||||
|
||||
// 하위 카테고리 존재 여부 확인
|
||||
if (GlobalCategory::where('parent_id', $id)->whereNull('deleted_at')->exists()) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => '하위 카테고리가 있어 삭제할 수 없습니다.',
|
||||
], 400);
|
||||
}
|
||||
|
||||
$category->update(['deleted_by' => Auth::id()]);
|
||||
$category->delete();
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => '글로벌 카테고리가 삭제되었습니다.',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 활성 상태 토글
|
||||
*/
|
||||
public function toggle(int $id): JsonResponse
|
||||
{
|
||||
$category = GlobalCategory::findOrFail($id);
|
||||
$category->update([
|
||||
'is_active' => ! $category->is_active,
|
||||
'updated_by' => Auth::id(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => $category->is_active ? '활성화되었습니다.' : '비활성화되었습니다.',
|
||||
'is_active' => $category->is_active,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 테넌트로 복사
|
||||
*/
|
||||
public function copyToTenant(int $id): JsonResponse
|
||||
{
|
||||
$tenantId = session('selected_tenant_id');
|
||||
if (! $tenantId) {
|
||||
return response()->json(['success' => false, 'message' => '테넌트를 선택해주세요.'], 400);
|
||||
}
|
||||
|
||||
$globalCategory = GlobalCategory::findOrFail($id);
|
||||
|
||||
// 이미 존재하는지 확인
|
||||
$exists = Category::query()
|
||||
->where('tenant_id', $tenantId)
|
||||
->where('code_group', $globalCategory->code_group)
|
||||
->where('code', $globalCategory->code)
|
||||
->whereNull('deleted_at')
|
||||
->exists();
|
||||
|
||||
if ($exists) {
|
||||
return response()->json(['success' => false, 'message' => '이미 존재하는 카테고리입니다.'], 400);
|
||||
}
|
||||
|
||||
// 복사 (parent_id는 복사하지 않음 - 개별 복사이므로)
|
||||
Category::create([
|
||||
'tenant_id' => $tenantId,
|
||||
'parent_id' => null,
|
||||
'code_group' => $globalCategory->code_group,
|
||||
'code' => $globalCategory->code,
|
||||
'name' => $globalCategory->name,
|
||||
'profile_code' => $globalCategory->profile_code,
|
||||
'description' => $globalCategory->description,
|
||||
'is_active' => $globalCategory->is_active,
|
||||
'sort_order' => $globalCategory->sort_order,
|
||||
'created_by' => Auth::id(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => '테넌트로 복사되었습니다.',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 일괄 테넌트로 복사
|
||||
*/
|
||||
public function bulkCopyToTenant(Request $request): JsonResponse
|
||||
{
|
||||
$tenantId = session('selected_tenant_id');
|
||||
if (! $tenantId) {
|
||||
return response()->json(['success' => false, 'message' => '테넌트를 선택해주세요.'], 400);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'ids' => 'required|array',
|
||||
'ids.*' => 'integer|exists:global_categories,id',
|
||||
]);
|
||||
|
||||
$globalCategories = GlobalCategory::whereIn('id', $validated['ids'])
|
||||
->whereNull('deleted_at')
|
||||
->orderBy('parent_id')
|
||||
->orderBy('sort_order')
|
||||
->get();
|
||||
|
||||
$copied = 0;
|
||||
$skipped = 0;
|
||||
$idMap = []; // global_id => tenant_id
|
||||
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
foreach ($globalCategories as $gc) {
|
||||
// 이미 존재하는지 확인
|
||||
$exists = Category::query()
|
||||
->where('tenant_id', $tenantId)
|
||||
->where('code_group', $gc->code_group)
|
||||
->where('code', $gc->code)
|
||||
->whereNull('deleted_at')
|
||||
->exists();
|
||||
|
||||
if ($exists) {
|
||||
$skipped++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// parent_id 매핑
|
||||
$parentId = null;
|
||||
if ($gc->parent_id && isset($idMap[$gc->parent_id])) {
|
||||
$parentId = $idMap[$gc->parent_id];
|
||||
}
|
||||
|
||||
$newCategory = Category::create([
|
||||
'tenant_id' => $tenantId,
|
||||
'parent_id' => $parentId,
|
||||
'code_group' => $gc->code_group,
|
||||
'code' => $gc->code,
|
||||
'name' => $gc->name,
|
||||
'profile_code' => $gc->profile_code,
|
||||
'description' => $gc->description,
|
||||
'is_active' => $gc->is_active,
|
||||
'sort_order' => $gc->sort_order,
|
||||
'created_by' => Auth::id(),
|
||||
]);
|
||||
|
||||
$idMap[$gc->id] = $newCategory->id;
|
||||
$copied++;
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
$message = "{$copied}개 카테고리가 복사되었습니다.";
|
||||
if ($skipped > 0) {
|
||||
$message .= " ({$skipped}개 중복으로 건너뜀)";
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => $message,
|
||||
'copied' => $copied,
|
||||
'skipped' => $skipped,
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => '복사 중 오류가 발생했습니다.',
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
123
app/Http/Controllers/CategoryController.php
Normal file
123
app/Http/Controllers/CategoryController.php
Normal file
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Models\GlobalCategory;
|
||||
use App\Models\Tenants\Tenant;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class CategoryController extends Controller
|
||||
{
|
||||
|
||||
/**
|
||||
* 카테고리 관리 페이지
|
||||
*/
|
||||
public function index(Request $request): View|Response
|
||||
{
|
||||
// HTMX 요청 시 전체 페이지 리로드
|
||||
if ($request->header('HX-Request')) {
|
||||
return response('', 200)->header('HX-Redirect', route('categories.index'));
|
||||
}
|
||||
|
||||
$tenantId = session('selected_tenant_id');
|
||||
$tenant = $tenantId ? Tenant::find($tenantId) : null;
|
||||
$isHQ = $tenantId == 1;
|
||||
|
||||
// 모든 code_group 조회 (글로벌 + 테넌트)
|
||||
$globalGroups = GlobalCategory::whereNull('deleted_at')
|
||||
->select('code_group')
|
||||
->distinct()
|
||||
->pluck('code_group')
|
||||
->toArray();
|
||||
|
||||
$tenantGroups = [];
|
||||
if ($tenantId) {
|
||||
$tenantGroups = Category::where('tenant_id', $tenantId)
|
||||
->whereNull('deleted_at')
|
||||
->select('code_group')
|
||||
->distinct()
|
||||
->pluck('code_group')
|
||||
->toArray();
|
||||
}
|
||||
|
||||
$allGroups = array_unique(array_merge($globalGroups, $tenantGroups));
|
||||
sort($allGroups);
|
||||
|
||||
$codeGroupLabels = config('categories.code_group_labels', []);
|
||||
|
||||
$codeGroups = [];
|
||||
foreach ($allGroups as $group) {
|
||||
$codeGroups[$group] = $codeGroupLabels[$group] ?? $group;
|
||||
}
|
||||
|
||||
if (empty($codeGroups)) {
|
||||
$codeGroups['product'] = $codeGroupLabels['product'] ?? 'product';
|
||||
}
|
||||
|
||||
$selectedGroup = $request->get('group', array_key_first($codeGroups));
|
||||
if (! isset($codeGroups[$selectedGroup])) {
|
||||
$selectedGroup = array_key_first($codeGroups);
|
||||
}
|
||||
|
||||
// 글로벌 카테고리 (트리 구조로 평탄화)
|
||||
$globalCategoriesRaw = GlobalCategory::where('code_group', $selectedGroup)
|
||||
->whereNull('deleted_at')
|
||||
->orderBy('sort_order')
|
||||
->get();
|
||||
$globalCategories = $this->flattenTree($globalCategoriesRaw);
|
||||
|
||||
// 테넌트 카테고리 (트리 구조로 평탄화)
|
||||
$tenantCategories = collect();
|
||||
$tenantCodes = [];
|
||||
if ($tenantId) {
|
||||
$tenantCategoriesRaw = Category::where('tenant_id', $tenantId)
|
||||
->where('code_group', $selectedGroup)
|
||||
->whereNull('deleted_at')
|
||||
->orderBy('sort_order')
|
||||
->get();
|
||||
$tenantCategories = $this->flattenTree($tenantCategoriesRaw);
|
||||
$tenantCodes = $tenantCategoriesRaw->pluck('code')->toArray();
|
||||
}
|
||||
|
||||
return view('categories.index', [
|
||||
'codeGroups' => $codeGroups,
|
||||
'codeGroupLabels' => $codeGroupLabels,
|
||||
'selectedGroup' => $selectedGroup,
|
||||
'globalCategories' => $globalCategories,
|
||||
'tenantCategories' => $tenantCategories,
|
||||
'tenantCodes' => $tenantCodes,
|
||||
'tenant' => $tenant,
|
||||
'isHQ' => $isHQ,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 카테고리 컬렉션을 트리 구조로 평탄화 (부모-자식 순서 유지, depth 추가)
|
||||
*/
|
||||
private function flattenTree($categories): \Illuminate\Support\Collection
|
||||
{
|
||||
$result = collect();
|
||||
$byParent = $categories->groupBy(fn($c) => $c->parent_id ?? 0);
|
||||
|
||||
$this->addChildrenRecursive($result, $byParent, 0, 0);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 재귀적으로 자식 추가
|
||||
*/
|
||||
private function addChildrenRecursive(&$result, $byParent, $parentId, $depth): void
|
||||
{
|
||||
$children = $byParent->get($parentId, collect());
|
||||
|
||||
foreach ($children as $child) {
|
||||
$child->depth = $depth;
|
||||
$result->push($child);
|
||||
$this->addChildrenRecursive($result, $byParent, $child->id, $depth + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,14 @@
|
||||
|
||||
class MenuSyncController extends Controller
|
||||
{
|
||||
protected int $tenantId = 1; // MNG 메뉴는 tenant_id=1
|
||||
/**
|
||||
* 현재 선택된 테넌트 ID
|
||||
*/
|
||||
protected function getTenantId(): int
|
||||
{
|
||||
$tenantId = session('selected_tenant_id');
|
||||
return ($tenantId && $tenantId !== 'all') ? (int) $tenantId : 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 환경 설정 조회
|
||||
@@ -20,7 +27,7 @@ class MenuSyncController extends Controller
|
||||
private function getEnvironments(): array
|
||||
{
|
||||
$setting = TenantSetting::withoutGlobalScopes()
|
||||
->where('tenant_id', $this->tenantId)
|
||||
->where('tenant_id', $this->getTenantId())
|
||||
->where('setting_group', 'menu_sync')
|
||||
->where('setting_key', 'environments')
|
||||
->first();
|
||||
@@ -85,7 +92,7 @@ public function saveSettings(Request $request): JsonResponse
|
||||
|
||||
TenantSetting::withoutGlobalScopes()->updateOrCreate(
|
||||
[
|
||||
'tenant_id' => $this->tenantId,
|
||||
'tenant_id' => $this->getTenantId(),
|
||||
'setting_group' => 'menu_sync',
|
||||
'setting_key' => 'environments',
|
||||
],
|
||||
@@ -178,7 +185,7 @@ public function push(Request $request): JsonResponse
|
||||
|
||||
// 선택된 메뉴 조회
|
||||
$menus = Menu::withoutGlobalScopes()
|
||||
->where('tenant_id', $this->tenantId)
|
||||
->where('tenant_id', $this->getTenantId())
|
||||
->whereIn('id', $validated['menu_ids'])
|
||||
->get();
|
||||
|
||||
@@ -316,7 +323,7 @@ public function testConnection(Request $request): JsonResponse
|
||||
private function getMenuTree(?int $parentId = null): array
|
||||
{
|
||||
$menus = Menu::withoutGlobalScopes()
|
||||
->where('tenant_id', $this->tenantId)
|
||||
->where('tenant_id', $this->getTenantId())
|
||||
->where('parent_id', $parentId)
|
||||
->orderBy('sort_order')
|
||||
->get();
|
||||
@@ -340,7 +347,7 @@ private function getMenuTree(?int $parentId = null): array
|
||||
private function getChildrenData(int $parentId): array
|
||||
{
|
||||
$children = Menu::withoutGlobalScopes()
|
||||
->where('tenant_id', $this->tenantId)
|
||||
->where('tenant_id', $this->getTenantId())
|
||||
->where('parent_id', $parentId)
|
||||
->orderBy('sort_order')
|
||||
->get();
|
||||
@@ -411,17 +418,20 @@ private function flattenMenuNames(array $menus, string $prefix = ''): array
|
||||
}
|
||||
|
||||
/**
|
||||
* 이름으로 메뉴 필터링
|
||||
* 이름으로 메뉴 필터링 (부모 이름 포함)
|
||||
*/
|
||||
private function filterMenusByName(array $menus, array $names): array
|
||||
private function filterMenusByName(array $menus, array $names, ?string $parentName = null): array
|
||||
{
|
||||
$result = [];
|
||||
foreach ($menus as $menu) {
|
||||
if (in_array($menu['name'], $names)) {
|
||||
// 부모 이름 추가
|
||||
$menu['parent_name'] = $parentName;
|
||||
$result[] = $menu;
|
||||
}
|
||||
if (! empty($menu['children'])) {
|
||||
$result = array_merge($result, $this->filterMenusByName($menu['children'], $names));
|
||||
// 현재 메뉴를 부모로 전달
|
||||
$result = array_merge($result, $this->filterMenusByName($menu['children'], $names, $menu['name']));
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
@@ -435,7 +445,7 @@ private function importMenu(array $data, ?int $parentId = null): void
|
||||
// 부모 메뉴 찾기
|
||||
if (! $parentId && ! empty($data['parent_name'])) {
|
||||
$parent = Menu::withoutGlobalScopes()
|
||||
->where('tenant_id', $this->tenantId)
|
||||
->where('tenant_id', $this->getTenantId())
|
||||
->where('name', $data['parent_name'])
|
||||
->first();
|
||||
$parentId = $parent?->id;
|
||||
@@ -444,7 +454,7 @@ private function importMenu(array $data, ?int $parentId = null): void
|
||||
// 기존 메뉴 찾기 또는 생성
|
||||
$menu = Menu::withoutGlobalScopes()->updateOrCreate(
|
||||
[
|
||||
'tenant_id' => $this->tenantId,
|
||||
'tenant_id' => $this->getTenantId(),
|
||||
'name' => $data['name'],
|
||||
],
|
||||
[
|
||||
@@ -464,4 +474,4 @@ private function importMenu(array $data, ?int $parentId = null): void
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user