feat:공통코드 글로벌→테넌트 체크박스 선택 및 일괄 복사 기능
This commit is contained in:
@@ -318,6 +318,100 @@ public function copy(Request $request, int $id): RedirectResponse|JsonResponse
|
||||
->with('success', '글로벌 코드가 테넌트용으로 복사되었습니다.');
|
||||
}
|
||||
|
||||
/**
|
||||
* 글로벌 코드를 테넌트용으로 일괄 복사
|
||||
*/
|
||||
public function bulkCopy(Request $request): RedirectResponse|JsonResponse
|
||||
{
|
||||
$tenantId = session('selected_tenant_id');
|
||||
|
||||
if (! $tenantId) {
|
||||
if ($request->ajax()) {
|
||||
return response()->json(['error' => '테넌트를 먼저 선택해주세요.'], 400);
|
||||
}
|
||||
return redirect()->back()->with('error', '테넌트를 먼저 선택해주세요.');
|
||||
}
|
||||
|
||||
// JSON 문자열로 받은 경우 처리
|
||||
$idsJson = $request->input('ids_json');
|
||||
if ($idsJson) {
|
||||
$ids = json_decode($idsJson, true);
|
||||
if (! is_array($ids) || empty($ids)) {
|
||||
if ($request->ajax()) {
|
||||
return response()->json(['error' => '복사할 코드를 선택해주세요.'], 400);
|
||||
}
|
||||
return redirect()->back()->with('error', '복사할 코드를 선택해주세요.');
|
||||
}
|
||||
} else {
|
||||
$validated = $request->validate([
|
||||
'ids' => 'required|array|min:1',
|
||||
'ids.*' => 'integer',
|
||||
]);
|
||||
$ids = $validated['ids'];
|
||||
}
|
||||
$codeGroup = null;
|
||||
$copiedCount = 0;
|
||||
$skippedCount = 0;
|
||||
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
foreach ($ids as $id) {
|
||||
$globalCode = CommonCode::whereNull('tenant_id')->find($id);
|
||||
if (! $globalCode) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$codeGroup = $globalCode->code_group;
|
||||
|
||||
// 이미 복사된 코드가 있는지 확인
|
||||
$exists = CommonCode::query()
|
||||
->where('tenant_id', $tenantId)
|
||||
->where('code_group', $globalCode->code_group)
|
||||
->where('code', $globalCode->code)
|
||||
->exists();
|
||||
|
||||
if ($exists) {
|
||||
$skippedCount++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// 복사
|
||||
CommonCode::create([
|
||||
'tenant_id' => $tenantId,
|
||||
'code_group' => $globalCode->code_group,
|
||||
'code' => $globalCode->code,
|
||||
'name' => $globalCode->name,
|
||||
'sort_order' => $globalCode->sort_order,
|
||||
'attributes' => $globalCode->attributes,
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$copiedCount++;
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
if ($request->ajax()) {
|
||||
return response()->json(['error' => '복사 중 오류가 발생했습니다.'], 500);
|
||||
}
|
||||
return redirect()->back()->with('error', '복사 중 오류가 발생했습니다.');
|
||||
}
|
||||
|
||||
$message = "{$copiedCount}개 코드가 복사되었습니다.";
|
||||
if ($skippedCount > 0) {
|
||||
$message .= " ({$skippedCount}개는 이미 존재하여 건너뜀)";
|
||||
}
|
||||
|
||||
if ($request->ajax()) {
|
||||
return response()->json(['success' => true, 'message' => $message, 'copied' => $copiedCount, 'skipped' => $skippedCount]);
|
||||
}
|
||||
|
||||
return redirect()
|
||||
->route('common-codes.index', ['group' => $codeGroup ?? 'item_type'])
|
||||
->with('success', $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 코드 삭제 (테넌트 코드만)
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user