summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/items-ship/service.ts416
-rw-r--r--lib/items-ship/table/add-items-dialog.tsx211
-rw-r--r--lib/items-ship/table/update-items-sheet.tsx202
-rw-r--r--lib/items-ship/validations.ts88
-rw-r--r--lib/items-tech/repository.ts (renamed from lib/items-ship/repository.ts)0
-rw-r--r--lib/items-tech/service.ts1139
-rw-r--r--lib/items-tech/table/add-items-dialog.tsx400
-rw-r--r--lib/items-tech/table/delete-items-dialog.tsx (renamed from lib/items-ship/table/delete-items-dialog.tsx)56
-rw-r--r--lib/items-tech/table/feature-flags.tsx (renamed from lib/items-ship/table/feature-flags.tsx)0
-rw-r--r--lib/items-tech/table/hull/import-item-handler.tsx143
-rw-r--r--lib/items-tech/table/hull/item-excel-template.tsx125
-rw-r--r--lib/items-tech/table/hull/offshore-hull-table-columns.tsx282
-rw-r--r--lib/items-tech/table/hull/offshore-hull-table-toolbar-actions.tsx184
-rw-r--r--lib/items-tech/table/hull/offshore-hull-table.tsx152
-rw-r--r--lib/items-tech/table/import-excel-button.tsx (renamed from lib/items-ship/table/import-excel-button.tsx)105
-rw-r--r--lib/items-tech/table/ship/Items-ship-table.tsx (renamed from lib/items-ship/table/Items-ship-table.tsx)23
-rw-r--r--lib/items-tech/table/ship/import-item-handler.tsx (renamed from lib/items-ship/table/import-item-handler.tsx)2
-rw-r--r--lib/items-tech/table/ship/item-excel-template.tsx (renamed from lib/items-ship/table/item-excel-template.tsx)0
-rw-r--r--lib/items-tech/table/ship/items-ship-table-columns.tsx (renamed from lib/items-ship/table/items-ship-table-columns.tsx)0
-rw-r--r--lib/items-tech/table/ship/items-table-toolbar-actions.tsx (renamed from lib/items-ship/table/items-table-toolbar-actions.tsx)11
-rw-r--r--lib/items-tech/table/top/import-item-handler.tsx143
-rw-r--r--lib/items-tech/table/top/item-excel-template.tsx125
-rw-r--r--lib/items-tech/table/top/offshore-top-table-columns.tsx282
-rw-r--r--lib/items-tech/table/top/offshore-top-table-toolbar-actions.tsx184
-rw-r--r--lib/items-tech/table/top/offshore-top-table.tsx153
-rw-r--r--lib/items-tech/table/update-items-sheet.tsx390
-rw-r--r--lib/items-tech/validations.ts156
27 files changed, 3997 insertions, 975 deletions
diff --git a/lib/items-ship/service.ts b/lib/items-ship/service.ts
deleted file mode 100644
index 37b623c1..00000000
--- a/lib/items-ship/service.ts
+++ /dev/null
@@ -1,416 +0,0 @@
-// src/lib/items-ship/service.ts
-"use server"; // Next.js 서버 액션에서 직접 import하려면 (선택)
-
-import { revalidateTag, unstable_noStore } from "next/cache";
-import db from "@/db/db";
-
-import { filterColumns } from "@/lib/filter-columns";
-import { unstable_cache } from "@/lib/unstable-cache";
-import { getErrorMessage } from "@/lib/handle-error";
-
-import { asc, desc, ilike, and, or, eq, count, inArray, sql } from "drizzle-orm";
-import { GetItemsSchema, UpdateItemSchema, ShipbuildingItemCreateData, TypedItemCreateData } from "./validations";
-import { Item, items, itemShipbuilding } from "@/db/schema/items";
-import { deleteItemById, deleteItemsByIds, findAllItems, insertItem, updateItem } from "./repository";
-
-/* -----------------------------------------------------
- 1) 조회 관련
------------------------------------------------------ */
-
-/**
- * 복잡한 조건으로 Item 목록을 조회 (+ pagination) 하고,
- * 총 개수에 따라 pageCount를 계산해서 리턴.
- * Next.js의 unstable_cache를 사용해 일정 시간 캐시.
- */
-export async function getShipbuildingItems(input: GetItemsSchema) {
- return unstable_cache(
- async () => {
- try {
- const offset = (input.page - 1) * input.perPage;
-
- // advancedTable 모드면 filterColumns()로 where 절 구성
- const advancedWhere = filterColumns({
- table: items,
- filters: input.filters,
- joinOperator: input.joinOperator,
- });
-
- let globalWhere;
- if (input.search) {
- const s = `%${input.search}%`;
- globalWhere = or(
- ilike(items.itemCode, s),
- ilike(items.itemName, s),
- ilike(items.description, s)
- );
- }
-
- const finalWhere = and(
- advancedWhere,
- globalWhere
- );
-
- const where = finalWhere;
-
- const orderBy =
- input.sort.length > 0
- ? input.sort.map((item) =>
- item.desc ? desc(items[item.id]) : asc(items[item.id])
- )
- : [asc(items.createdAt)];
-
- // 조선 아이템 테이블과 기본 아이템 테이블 조인하여 조회
- const result = await db.select({
- id: itemShipbuilding.id,
- itemId: itemShipbuilding.itemId,
- workType: itemShipbuilding.workType,
- shipTypes: itemShipbuilding.shipTypes,
- itemCode: items.itemCode,
- itemName: items.itemName,
- description: items.description,
- createdAt: items.createdAt,
- updatedAt: items.updatedAt,
- })
- .from(itemShipbuilding)
- .innerJoin(items, eq(itemShipbuilding.itemId, items.id))
- .where(where)
- .orderBy(...orderBy)
- .offset(offset)
- .limit(input.perPage);
-
- // 전체 데이터 개수 조회
- const [{ count: total }] = await db.select({
- count: count()
- })
- .from(itemShipbuilding)
- .innerJoin(items, eq(itemShipbuilding.itemId, items.id))
- .where(where);
-
- const pageCount = Math.ceil(Number(total) / input.perPage);
-
- return { data: result, pageCount };
- } catch (err) {
- console.error("Error fetching shipbuilding items:", err);
- return { data: [], pageCount: 0 };
- }
- },
- [JSON.stringify(input)],
- {
- revalidate: 3600,
- tags: ["items"],
- }
- )();
-}
-
-/* -----------------------------------------------------
- 2) 생성(Create)
------------------------------------------------------ */
-
-/**
- * Item 생성 - 아이템 타입에 따라 해당 테이블에 데이터 삽입
- */
-export async function createShipbuildingItem(input: TypedItemCreateData) {
- unstable_noStore()
-
- try {
- if (!input.itemCode || !input.itemName) {
- return {
- success: false,
- message: "아이템 코드와 아이템 명은 필수입니다",
- data: null,
- error: "필수 필드 누락"
- }
- }
-
- let result: unknown[] = []
-
- result = await db.transaction(async (tx) => {
- const existingItem = await tx.query.items.findFirst({
- where: eq(items.itemCode, input.itemCode),
- })
-
- let itemId: number
- let itemResult
-
- if (existingItem) {
- itemResult = await updateItem(tx, existingItem.id, {
- itemName: input.itemName,
- description: input.description,
- })
- itemId = existingItem.id
- } else {
- itemResult = await insertItem(tx, {
- itemCode: input.itemCode,
- itemName: input.itemName,
- description: input.description,
- })
- itemId = itemResult[0].id
- }
-
- const shipData = input as ShipbuildingItemCreateData;
- const typeResult = await tx.insert(itemShipbuilding).values({
- itemId: itemId,
- workType: shipData.workType ? (shipData.workType as '기장' | '전장' | '선실' | '배관' | '철의') : '기장',
- shipTypes: shipData.shipTypes || ''
- }).returning();
-
- return [...itemResult, ...typeResult]
- })
-
- revalidateTag("items")
-
- return {
- success: true,
- data: result[0] || null,
- error: null
- }
- } catch (err) {
- console.error("아이템 생성/업데이트 오류:", err)
-
- if (err instanceof Error && err.message.includes("unique constraint")) {
- return {
- success: false,
- message: "이미 존재하는 아이템 코드입니다",
- data: null,
- error: "중복 키 오류"
- }
- }
-
- return {
- success: false,
- message: getErrorMessage(err),
- data: null,
- error: getErrorMessage(err)
- }
- }
-}
-
-/**
- * Excel import를 위한 조선 아이템 생성 함수
- * 하나의 아이템 코드에 대해 여러 선종을 처리 (1:N 관계)
- */
-export async function createShipbuildingImportItem(input: {
- itemCode: string;
- itemName: string;
- workType: '기장' | '전장' | '선실' | '배관' | '철의';
- description?: string | null;
- shipTypes: Record<string, boolean>;
-}) {
- unstable_noStore();
-
- try {
-
- if (!input.itemCode || !input.itemName) {
- return {
- success: false,
- message: "아이템 코드와 아이템 명은 필수입니다",
- data: null,
- error: "필수 필드 누락"
- }
- }
- let results: any[] = []
- results = await db.transaction(async (tx) => {
- // 1. itemCode 정규화해서 직접 쿼리
- const existRows = await tx.select().from(items)
- .where(eq(items.itemCode, input.itemCode));
- const existingItem = existRows[0];
-
- console.log('DB에서 직접 조회한 기존 아이템:', existingItem);
-
- let itemId: number;
-
- if (existingItem) {
- // 이미 있으면 업데이트
- await updateItem(tx, existingItem.id, {
- itemName: input.itemName,
- description: input.description,
- });
- itemId = existingItem.id;
- console.log('기존 아이템 업데이트, id:', itemId);
- } else {
- // 없으면 새로 생성
- // 현재 가장 큰 ID 값 가져오기
- const maxIdResult = await tx.select({ maxId: sql`MAX(id)` }).from(items);
- const maxId = maxIdResult[0]?.maxId || 0;
- const newId = Number(maxId) + 1;
- console.log('새 아이템 생성을 위한 ID 계산:', { maxId, newId });
-
- // 새 ID로 아이템 생성
- const insertResult = await tx.insert(items).values({
- id: newId,
- itemCode: input.itemCode,
- itemName: input.itemName,
- description: input.description,
- }).returning();
-
- itemId = insertResult[0].id;
- console.log('새 아이템 생성 완료, id:', itemId);
- }
-
- const createdItems = [];
- for (const shipType of Object.keys(input.shipTypes)) {
- // 그대로 선종명 string으로 저장
- const existShip = await tx.select().from(itemShipbuilding)
- .where(
- and(
- eq(itemShipbuilding.itemId, itemId),
- eq(itemShipbuilding.shipTypes, shipType)
- )
- );
- if (!existShip[0]) {
- const shipbuildingResult = await tx.insert(itemShipbuilding).values({
- itemId: itemId,
- workType: input.workType,
- shipTypes: shipType
- }).returning();
- createdItems.push({
- ...shipbuildingResult[0]
- });
- console.log('조선아이템 생성:', shipType, shipbuildingResult[0]);
- } else {
- console.log('이미 존재하는 조선아이템:', shipType);
- }
- }
- return createdItems;
- });
-
- revalidateTag("items");
-
- return {
- success: true,
- data: results,
- error: null
- }
- } catch (err) {
- // DB에 실제로 존재하는 itemCode 목록도 함께 출력
- const allCodes = await db.select({ code: items.itemCode }).from(items);
- console.error("아이템 import 오류:", err);
- console.error("DB에 존재하는 모든 itemCode:", allCodes.map(x => x.code));
- return {
- success: false,
- message: getErrorMessage(err),
- data: null,
- error: getErrorMessage(err)
- }
- }
-}
-
-/* -----------------------------------------------------
- 3) 업데이트
------------------------------------------------------ */
-
-// 업데이트 타입 정의 인터페이스
-interface UpdateShipbuildingItemInput extends UpdateItemSchema {
- id: number;
- workType?: string;
- shipTypes?: string;
- itemCode?: string;
- itemName?: string;
- description?: string;
-}
-
-/** 단건 업데이트 */
-export async function modifyShipbuildingItem(input: UpdateShipbuildingItemInput) {
- unstable_noStore();
- try {
- await db.transaction(async (tx) => {
- // 기본 아이템 테이블 업데이트
- const [item] = await updateItem(tx, input.id, {
- itemCode: input.itemCode,
- itemName: input.itemName,
- description: input.description,
- });
-
- // 조선 아이템 테이블 업데이트
- if (input.workType || input.shipTypes) {
- await tx.update(itemShipbuilding)
- .set({
- workType: input.workType as '기장' | '전장' | '선실' | '배관' | '철의',
- shipTypes: input.shipTypes
- })
- .where(eq(itemShipbuilding.itemId, item.id));
- }
-
- return item;
- });
-
- revalidateTag("items");
- return { data: null, error: null };
- } catch (err) {
- return { data: null, error: getErrorMessage(err) };
- }
-}
-
-/* -----------------------------------------------------
- 4) 삭제
------------------------------------------------------ */
-
-// 삭제 타입 정의 인터페이스
-interface DeleteItemInput {
- id: number;
-}
-
-interface DeleteItemsInput {
- ids: number[];
-}
-
-/** 단건 삭제 */
-export async function removeShipbuildingItem(input: DeleteItemInput) {
- unstable_noStore();
- try {
- await db.transaction(async (tx) => {
- const item = await tx.query.items.findFirst({
- where: eq(items.id, input.id),
- });
-
- if (!item) {
- throw new Error("아이템을 찾을 수 없습니다.");
- }
-
- // 조선 아이템 테이블에서 먼저 삭제
- await tx.delete(itemShipbuilding)
- .where(eq(itemShipbuilding.itemId, input.id));
-
- // 기본 아이템 테이블에서 삭제
- await deleteItemById(tx, input.id);
- });
-
- revalidateTag("items");
-
- return { data: null, error: null };
- } catch (err) {
- return { data: null, error: getErrorMessage(err) };
- }
-}
-
-/** 복수 삭제 */
-export async function removeShipbuildingItems(input: DeleteItemsInput) {
- unstable_noStore();
- try {
- await db.transaction(async (tx) => {
- if (input.ids.length > 0) {
- // 조선 아이템 테이블에서 먼저 삭제
- await tx.delete(itemShipbuilding)
- .where(inArray(itemShipbuilding.itemId, input.ids));
-
- // 기본 아이템 테이블에서 삭제
- await deleteItemsByIds(tx, input.ids);
- }
- });
-
- revalidateTag("items");
-
- return { data: null, error: null, success: true, message: "아이템이 성공적으로 삭제되었습니다." };
- } catch (err) {
- return { data: null, error: getErrorMessage(err), success: false, message: "아이템 삭제 중 오류가 발생했습니다." };
- }
-}
-
-export async function getAllShipbuildingItems(): Promise<Item[]> {
- try {
- return await findAllItems();
- } catch (error) {
- console.error("Failed to get items:", error);
- throw new Error("Failed to get items");
- }
-}
diff --git a/lib/items-ship/table/add-items-dialog.tsx b/lib/items-ship/table/add-items-dialog.tsx
deleted file mode 100644
index fdfe54cc..00000000
--- a/lib/items-ship/table/add-items-dialog.tsx
+++ /dev/null
@@ -1,211 +0,0 @@
-"use client"
-
-import * as React from "react"
-import { useForm } from "react-hook-form"
-import { zodResolver } from "@hookform/resolvers/zod"
-import { z } from "zod"
-
-import { Dialog, DialogTrigger, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter } from "@/components/ui/dialog"
-import { Button } from "@/components/ui/button"
-import { Input } from "@/components/ui/input"
-import { Textarea } from "@/components/ui/textarea"
-// react-hook-form + shadcn/ui Form
-import {
- Form,
- FormControl,
- FormField,
- FormItem,
- FormLabel,
- FormMessage,
-} from "@/components/ui/form"
-
-import {
- createShipbuildingItemSchema,
-
-} from "../validations"
-import { createShipbuildingItem } from "../service"
-import { Plus } from "lucide-react"
-import { toast } from "sonner"
-import {
- Select,
- SelectContent,
- SelectItem,
- SelectTrigger,
- SelectValue,
-} from "@/components/ui/select"
-
-const workTypes = [
- { value: "기장", label: "기장" },
- { value: "전장", label: "전장" },
- { value: "선실", label: "선실" },
- { value: "배관", label: "배관" },
- { value: "철의", label: "철의" },
-] as const
-
-export function AddItemDialog() {
- const [open, setOpen] = React.useState(false)
- const [isSubmitting, setIsSubmitting] = React.useState(false)
-
- // 아이템 타입에 따라 다른 폼과 스키마 사용
- const getFormAndSchema = () => {
- return {
- schema: createShipbuildingItemSchema,
- defaultValues: {
- itemCode: "",
- itemName: "",
- description: "",
- workType: "",
- shipTypes: "",
- }
- };
- };
-
- const { schema, defaultValues } = getFormAndSchema();
-
- // 타입 안전성을 위해 구체적인 타입 사용
- type ItemFormSchema =
- | z.infer<typeof createShipbuildingItemSchema>
-
-
- const form = useForm<ItemFormSchema>({
- resolver: zodResolver(schema),
- defaultValues,
- });
-
- async function onSubmit(values: ItemFormSchema) {
- try {
- setIsSubmitting(true)
-
- // 타입에 따라 다른 로직 추가 가능
- const result = await createShipbuildingItem(values)
-
- if (result.success) {
- toast.success("아이템이 추가되었습니다.")
- form.reset()
- setOpen(false)
- } else {
- toast.error(result.message || "아이템 추가 실패")
- }
- } catch (error) {
- toast.error("오류가 발생했습니다.")
- console.error(error)
- } finally {
- setIsSubmitting(false)
- }
- }
-
-
-
- return (
- <Dialog open={open} onOpenChange={setOpen}>
- <DialogTrigger asChild>
- <Button variant="outline" size="sm" className="ml-auto gap-2">
- <Plus className="h-4 w-4" />
- <span className="hidden sm:inline">Add Item</span>
- </Button>
- </DialogTrigger>
- <DialogContent className="sm:max-w-[425px] max-h-[80vh] flex flex-col">
- <DialogHeader>
- <DialogTitle>아이템 추가</DialogTitle>
- <DialogDescription>
- {'조선 아이템을 추가합니다. 아이템 코드, 이름, 설명을 입력하세요.'}
- </DialogDescription>
- </DialogHeader>
- <div className="overflow-y-auto pr-2">
- <Form {...form}>
- <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
- <FormField
- control={form.control}
- name="itemCode"
- render={({ field }) => (
- <FormItem>
- <FormLabel>아이템 코드</FormLabel>
- <FormControl>
- <Input placeholder={'예: SB001'} {...field} />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name="itemName"
- render={({ field }) => (
- <FormItem>
- <FormLabel>아이템 이름</FormLabel>
- <FormControl>
- <Input placeholder="아이템 이름 입력" {...field} />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name="description"
- render={({ field }) => (
- <FormItem>
- <FormLabel>설명</FormLabel>
- <FormControl>
- <Textarea placeholder="설명 입력 (선택사항)" {...field} />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name="workType"
- render={({ field }) => (
- <FormItem>
- <FormLabel>기능(공종)</FormLabel>
- <FormControl>
- <Select
- onValueChange={field.onChange}
- defaultValue={field.value}
- >
- <FormControl>
- <SelectTrigger>
- <SelectValue placeholder={'기능(공종)을 선택하세요'} />
- </SelectTrigger>
- </FormControl>
- <SelectContent>
- {workTypes.map((type) => (
- <SelectItem key={type.value} value={type.value}>
- {type.label}
- </SelectItem>
- ))}
- </SelectContent>
- </Select>
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
-
- {/* 조선 아이템인 경우 선종 필드 표시 */}
- <FormField
- control={form.control}
- name="shipTypes"
- render={({ field }) => (
- <FormItem>
- <FormLabel>선종</FormLabel>
- <FormControl>
- <Input placeholder="예: A-MAX, VLCC, S-MAX" {...field} />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <DialogFooter className="mt-4">
- <Button type="submit" disabled={isSubmitting}>
- {isSubmitting ? "추가 중..." : "추가"}
- </Button>
- </DialogFooter>
- </form>
- </Form>
- </div>
- </DialogContent>
- </Dialog>
- )
-} \ No newline at end of file
diff --git a/lib/items-ship/table/update-items-sheet.tsx b/lib/items-ship/table/update-items-sheet.tsx
deleted file mode 100644
index e021e145..00000000
--- a/lib/items-ship/table/update-items-sheet.tsx
+++ /dev/null
@@ -1,202 +0,0 @@
-"use client"
-
-import * as React from "react"
-import { zodResolver } from "@hookform/resolvers/zod"
-import { useForm } from "react-hook-form"
-
-import { Button } from "@/components/ui/button"
-import {
- Sheet,
- SheetClose,
- SheetContent,
- SheetDescription,
- SheetFooter,
- SheetHeader,
- SheetTitle,
-} from "@/components/ui/sheet"
-import {
- Form,
- FormControl,
- FormField,
- FormItem,
- FormLabel,
- FormMessage,
-} from "@/components/ui/form"
-import { Input } from "@/components/ui/input"
-import {
- Select,
- SelectContent,
- SelectItem,
- SelectTrigger,
- SelectValue,
-} from "@/components/ui/select"
-import { Textarea } from "@/components/ui/textarea"
-import { toast } from "sonner"
-
-import { modifyShipbuildingItem } from "../service"
-import { updateShipbuildingItemSchema, type UpdateShipbuildingItemSchema } from "../validations"
-
-const workTypes = [
- { value: "기장", label: "기장" },
- { value: "전장", label: "전장" },
- { value: "선실", label: "선실" },
- { value: "배관", label: "배관" },
- { value: "철의", label: "철의" },
-] as const
-
-interface UpdateItemSheetProps {
- item: {
- id: number;
- itemId: number;
- workType: "기장" | "전장" | "선실" | "배관" | "철의";
- shipTypes: string;
- itemCode: string;
- itemName: string;
- description: string | null;
- createdAt: Date;
- updatedAt: Date;
- }
- open: boolean
- onOpenChange: (open: boolean) => void
-}
-
-export function UpdateItemSheet({ item, open, onOpenChange }: UpdateItemSheetProps) {
- const [isSubmitting, setIsSubmitting] = React.useState(false)
- // 초기값 설정
- const form = useForm<UpdateShipbuildingItemSchema>({
- resolver: zodResolver(updateShipbuildingItemSchema),
- defaultValues: {
- itemCode: item.itemCode,
- itemName: item.itemName,
- description: item.description || "",
- workType: item.workType,
- shipTypes: item.shipTypes,
- },
- })
-
- async function onSubmit(data: UpdateShipbuildingItemSchema) {
- try {
- setIsSubmitting(true)
- const result = await modifyShipbuildingItem({ ...data, id: item.id })
-
- if (result.data) {
- toast.success("아이템이 수정되었습니다.")
- onOpenChange(false)
- } else {
- toast.error(result.error || "아이템 수정 중 오류가 발생했습니다. 다시 시도해주세요.")
- }
- } catch (error) {
- toast.error("오류가 발생했습니다.")
- console.error(error)
- } finally {
- setIsSubmitting(false)
- }
- }
-
- return (
- <Sheet open={open} onOpenChange={onOpenChange}>
- <SheetContent>
- <SheetHeader>
- <SheetTitle>조선 아이템 수정</SheetTitle>
- <SheetDescription>
- 조선 아이템 정보를 수정합니다. 수정할 필드를 입력해주세요.
- </SheetDescription>
- </SheetHeader>
- <Form {...form}>
- <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
- <FormField
- control={form.control}
- name="itemCode"
- render={({ field }) => (
- <FormItem>
- <FormLabel>Material Group</FormLabel>
- <FormControl>
- <Input placeholder="Material Group을 입력하세요" {...field} />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name="itemName"
- render={({ field }) => (
- <FormItem>
- <FormLabel>Description</FormLabel>
- <FormControl>
- <Input placeholder="Description을 입력하세요" {...field} />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name="workType"
- render={({ field }) => (
- <FormItem>
- <FormLabel>기능(공종)</FormLabel>
- <Select
- onValueChange={field.onChange}
- defaultValue={field.value}
- >
- <FormControl>
- <SelectTrigger>
- <SelectValue placeholder="기능(공종)을 선택하세요" />
- </SelectTrigger>
- </FormControl>
- <SelectContent>
- {workTypes.map((type) => (
- <SelectItem key={type.value} value={type.value}>
- {type.label}
- </SelectItem>
- ))}
- </SelectContent>
- </Select>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name="shipTypes"
- render={({ field }) => (
- <FormItem>
- <FormLabel>선종</FormLabel>
- <FormControl>
- <Input placeholder="선종을 입력하세요" {...field} />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name="description"
- render={({ field }) => (
- <FormItem>
- <FormLabel>Size/Dimension</FormLabel>
- <FormControl>
- <Textarea
- placeholder="Size/Dimension을 입력하세요"
- {...field}
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <SheetFooter>
- <SheetClose asChild>
- <Button variant="outline">취소</Button>
- </SheetClose>
- <Button type="submit" disabled={isSubmitting}>
- {isSubmitting ? "수정 중..." : "수정"}
- </Button>
- </SheetFooter>
- </form>
- </Form>
- </SheetContent>
- </Sheet>
- )
-} \ No newline at end of file
diff --git a/lib/items-ship/validations.ts b/lib/items-ship/validations.ts
deleted file mode 100644
index cb297f99..00000000
--- a/lib/items-ship/validations.ts
+++ /dev/null
@@ -1,88 +0,0 @@
-import {
- createSearchParamsCache,
- parseAsArrayOf,
- parseAsInteger,
- parseAsString,
- parseAsStringEnum,
-} from "nuqs/server"
-import * as z from "zod"
-
-import { getFiltersStateParser, getSortingStateParser } from "@/lib/parsers"
-import { Item } from "@/db/schema/items";
-
-export const searchParamsCache = createSearchParamsCache({
- flags: parseAsArrayOf(z.enum(["advancedTable", "floatingBar"])).withDefault(
- []
- ),
- page: parseAsInteger.withDefault(1),
- perPage: parseAsInteger.withDefault(10),
- sort: getSortingStateParser<Item>().withDefault([
- { id: "createdAt", desc: true },
- ]),
- itemCode: parseAsString.withDefault(""),
- itemName: parseAsString.withDefault(""),
- description: parseAsString.withDefault(""),
-
- // advanced filter
- filters: getFiltersStateParser().withDefault([]),
- joinOperator: parseAsStringEnum(["and", "or"]).withDefault("and"),
- search: parseAsString.withDefault(""),
-
-})
-
-export const createItemSchema = z.object({
- itemCode: z.string(),
- itemName: z.string(),
- description: z.string(),
-})
-
-export const updateItemSchema = z.object({
- itemCode: z.string(),
- itemName: z.string().optional(),
- description: z.string().optional(),
-})
-
-// 조선 아이템 업데이트 스키마
-export const updateShipbuildingItemSchema = z.object({
- itemCode: z.string(),
- itemName: z.string().optional(),
- description: z.string().optional(),
- workType: z.string().optional(),
- shipTypes: z.string().optional(),
-})
-
-
-export type GetItemsSchema = Awaited<ReturnType<typeof searchParamsCache.parse>>
-export type CreateItemSchema = z.infer<typeof createItemSchema>
-export type UpdateItemSchema = z.infer<typeof updateItemSchema>
-export type UpdateShipbuildingItemSchema = z.infer<typeof updateShipbuildingItemSchema>
-
-// 조선 아이템 스키마
-export const createShipbuildingItemSchema = z.object({
- itemCode: z.string(),
- itemName: z.string(),
- description: z.string(),
- workType: z.string(),
- shipTypes: z.string(),
-})
-
-
-export type CreateShipbuildingItemSchema = z.infer<typeof createShipbuildingItemSchema>
-
-// 기본 아이템 생성 데이터 타입
-export interface ItemCreateData {
- itemCode: string
- itemName: string
- description: string | null
-}
-
-
-// 조선 아이템 생성 데이터 타입
-export interface ShipbuildingItemCreateData extends ItemCreateData {
- workType: string | null
- shipTypes: string | null
-}
-
-// 아이템 타입에 따른 생성 데이터 타입
-export type TypedItemCreateData = ShipbuildingItemCreateData
-
diff --git a/lib/items-ship/repository.ts b/lib/items-tech/repository.ts
index 550e6b1d..550e6b1d 100644
--- a/lib/items-ship/repository.ts
+++ b/lib/items-tech/repository.ts
diff --git a/lib/items-tech/service.ts b/lib/items-tech/service.ts
new file mode 100644
index 00000000..97aacfba
--- /dev/null
+++ b/lib/items-tech/service.ts
@@ -0,0 +1,1139 @@
+// src/lib/items-ship/service.ts
+"use server"; // Next.js 서버 액션에서 직접 import하려면 (선택)
+
+import { revalidateTag, unstable_noStore } from "next/cache";
+import db from "@/db/db";
+
+import { filterColumns } from "@/lib/filter-columns";
+import { unstable_cache } from "@/lib/unstable-cache";
+import { getErrorMessage } from "@/lib/handle-error";
+
+import { asc, desc, ilike, and, or, eq, count, inArray, sql } from "drizzle-orm";
+import { GetItemsSchema, UpdateItemSchema, ShipbuildingItemCreateData, TypedItemCreateData, OffshoreTopItemCreateData, OffshoreHullItemCreateData } from "./validations";
+import { Item, items, itemShipbuilding, itemOffshoreTop, itemOffshoreHull } from "@/db/schema/items";
+import { findAllItems, insertItem, updateItem } from "./repository";
+
+/* -----------------------------------------------------
+ 1) 조회 관련
+----------------------------------------------------- */
+
+/**
+ * 복잡한 조건으로 Item 목록을 조회 (+ pagination) 하고,
+ * 총 개수에 따라 pageCount를 계산해서 리턴.
+ * Next.js의 unstable_cache를 사용해 일정 시간 캐시.
+ */
+export async function getShipbuildingItems(input: GetItemsSchema) {
+ return unstable_cache(
+ async () => {
+ try {
+ const offset = (input.page - 1) * input.perPage;
+
+ // advancedTable 모드면 filterColumns()로 where 절 구성
+ const advancedWhere = filterColumns({
+ table: items,
+ filters: input.filters,
+ joinOperator: input.joinOperator,
+ });
+
+ let globalWhere;
+ if (input.search) {
+ const s = `%${input.search}%`;
+ globalWhere = or(
+ ilike(items.itemCode, s),
+ ilike(items.itemName, s),
+ ilike(items.description, s)
+ );
+ }
+
+ const finalWhere = and(
+ advancedWhere,
+ globalWhere
+ );
+
+ const where = finalWhere;
+
+ const orderBy =
+ input.sort.length > 0
+ ? input.sort.map((item) =>
+ item.desc ? desc(items[item.id]) : asc(items[item.id])
+ )
+ : [asc(items.createdAt)];
+
+ // 조선 아이템 테이블과 기본 아이템 테이블 조인하여 조회
+ const result = await db.select({
+ id: itemShipbuilding.id,
+ itemId: itemShipbuilding.itemId,
+ workType: itemShipbuilding.workType,
+ shipTypes: itemShipbuilding.shipTypes,
+ itemCode: items.itemCode,
+ itemName: items.itemName,
+ description: items.description,
+ createdAt: itemShipbuilding.createdAt,
+ updatedAt: itemShipbuilding.updatedAt,
+ })
+ .from(itemShipbuilding)
+ .innerJoin(items, eq(itemShipbuilding.itemId, items.id))
+ .where(where)
+ .orderBy(...orderBy)
+ .offset(offset)
+ .limit(input.perPage);
+
+ // 전체 데이터 개수 조회
+ const [{ count: total }] = await db.select({
+ count: count()
+ })
+ .from(itemShipbuilding)
+ .innerJoin(items, eq(itemShipbuilding.itemId, items.id))
+ .where(where);
+
+ const pageCount = Math.ceil(Number(total) / input.perPage);
+
+ return { data: result, pageCount };
+ } catch (err) {
+ console.error("Error fetching shipbuilding items:", err);
+ return { data: [], pageCount: 0 };
+ }
+ },
+ [JSON.stringify(input)],
+ {
+ revalidate: 3600,
+ tags: ["items"],
+ }
+ )();
+}
+
+export async function getOffshoreTopItems(input: GetItemsSchema) {
+ return unstable_cache(
+ async () => {
+ try {
+ const offset = (input.page - 1) * input.perPage;
+
+ // advancedTable 모드면 filterColumns()로 where 절 구성
+ const advancedWhere = filterColumns({
+ table: items,
+ filters: input.filters,
+ joinOperator: input.joinOperator,
+ });
+
+ let globalWhere;
+ if (input.search) {
+ const s = `%${input.search}%`;
+ globalWhere = or(
+ ilike(items.itemCode, s),
+ ilike(items.itemName, s),
+ ilike(items.description, s)
+ );
+ }
+
+ const finalWhere = and(
+ advancedWhere,
+ globalWhere
+ );
+
+ const where = finalWhere;
+
+ const orderBy =
+ input.sort.length > 0
+ ? input.sort.map((item) =>
+ item.desc ? desc(items[item.id]) : asc(items[item.id])
+ )
+ : [asc(items.createdAt)];
+
+ // 해양 TOP 아이템 테이블과 기본 아이템 테이블 조인하여 조회
+ const result = await db.select({
+ id: itemOffshoreTop.id,
+ itemId: itemOffshoreTop.itemId,
+ workType: itemOffshoreTop.workType,
+ itemList1: itemOffshoreTop.itemList1,
+ itemList2: itemOffshoreTop.itemList2,
+ itemList3: itemOffshoreTop.itemList3,
+ itemList4: itemOffshoreTop.itemList4,
+ itemCode: items.itemCode,
+ itemName: items.itemName,
+ description: items.description,
+ createdAt: itemOffshoreTop.createdAt,
+ updatedAt: itemOffshoreTop.updatedAt,
+ })
+ .from(itemOffshoreTop)
+ .innerJoin(items, eq(itemOffshoreTop.itemId, items.id))
+ .where(where)
+ .orderBy(...orderBy)
+ .offset(offset)
+ .limit(input.perPage);
+
+ // 전체 데이터 개수 조회
+ const [{ count: total }] = await db.select({
+ count: count()
+ })
+ .from(itemOffshoreTop)
+ .innerJoin(items, eq(itemOffshoreTop.itemId, items.id))
+ .where(where);
+
+ const pageCount = Math.ceil(Number(total) / input.perPage);
+
+ return { data: result, pageCount };
+ } catch (err) {
+ console.error("Error fetching offshore top items:", err);
+ return { data: [], pageCount: 0 };
+ }
+ },
+ [JSON.stringify(input)],
+ {
+ revalidate: 3600,
+ tags: ["items"],
+ }
+ )();
+}
+
+export async function getOffshoreHullItems(input: GetItemsSchema) {
+ return unstable_cache(
+ async () => {
+ try {
+ const offset = (input.page - 1) * input.perPage;
+
+ // advancedTable 모드면 filterColumns()로 where 절 구성
+ const advancedWhere = filterColumns({
+ table: items,
+ filters: input.filters,
+ joinOperator: input.joinOperator,
+ });
+
+ let globalWhere;
+ if (input.search) {
+ const s = `%${input.search}%`;
+ globalWhere = or(
+ ilike(items.itemCode, s),
+ ilike(items.itemName, s),
+ ilike(items.description, s)
+ );
+ }
+
+ const finalWhere = and(
+ advancedWhere,
+ globalWhere
+ );
+
+ const where = finalWhere;
+
+ const orderBy =
+ input.sort.length > 0
+ ? input.sort.map((item) =>
+ item.desc ? desc(items[item.id]) : asc(items[item.id])
+ )
+ : [asc(items.createdAt)];
+
+ // 해양 HULL 아이템 테이블과 기본 아이템 테이블 조인하여 조회
+ const result = await db.select({
+ id: itemOffshoreHull.id,
+ itemId: itemOffshoreHull.itemId,
+ workType: itemOffshoreHull.workType,
+ itemList1: itemOffshoreHull.itemList1,
+ itemList2: itemOffshoreHull.itemList2,
+ itemList3: itemOffshoreHull.itemList3,
+ itemList4: itemOffshoreHull.itemList4,
+ itemCode: items.itemCode,
+ itemName: items.itemName,
+ description: items.description,
+ createdAt: itemOffshoreHull.createdAt,
+ updatedAt: itemOffshoreHull.updatedAt,
+ })
+ .from(itemOffshoreHull)
+ .innerJoin(items, eq(itemOffshoreHull.itemId, items.id))
+ .where(where)
+ .orderBy(...orderBy)
+ .offset(offset)
+ .limit(input.perPage);
+
+ // 전체 데이터 개수 조회
+ const [{ count: total }] = await db.select({
+ count: count()
+ })
+ .from(itemOffshoreHull)
+ .innerJoin(items, eq(itemOffshoreHull.itemId, items.id))
+ .where(where);
+
+ const pageCount = Math.ceil(Number(total) / input.perPage);
+
+ return { data: result, pageCount };
+ } catch (err) {
+ console.error("Error fetching offshore hull items:", err);
+ return { data: [], pageCount: 0 };
+ }
+ },
+ [JSON.stringify(input)],
+ {
+ revalidate: 3600,
+ tags: ["items"],
+ }
+ )();
+}
+
+/* -----------------------------------------------------
+ 2) 생성(Create)
+----------------------------------------------------- */
+
+/**
+ * Item 생성 - 아이템 타입에 따라 해당 테이블에 데이터 삽입
+ */
+export async function createShipbuildingItem(input: TypedItemCreateData) {
+ unstable_noStore()
+
+ try {
+ if (!input.itemCode || !input.itemName) {
+ return {
+ success: false,
+ message: "아이템 코드와 아이템 명은 필수입니다",
+ data: null,
+ error: "필수 필드 누락"
+ }
+ }
+ const result = await db.transaction(async (tx) => {
+ // 1. itemCode 정규화해서 직접 쿼리
+ const existRows = await tx.select().from(items)
+ .where(eq(items.itemCode, input.itemCode));
+ const existingItem = existRows[0];
+
+ let itemId: number;
+ let itemResult;
+
+ if (existingItem) {
+ // 이미 있으면 업데이트
+ itemResult = await updateItem(tx, existingItem.id, {
+ itemName: input.itemName,
+ description: input.description,
+ });
+ itemId = existingItem.id;
+ } else {
+ // 없으면 새로 생성
+ // 현재 가장 큰 ID 값 가져오기
+ const maxIdResult = await tx.select({ maxId: sql`MAX(id)` }).from(items);
+ const maxId = maxIdResult[0]?.maxId || 0;
+ const newId = Number(maxId) + 1;
+
+ // 새 ID로 아이템 생성
+ itemResult = await tx.insert(items).values({
+ id: newId,
+ itemCode: input.itemCode,
+ itemName: input.itemName,
+ description: input.description,
+ }).returning();
+
+ itemId = itemResult[0].id;
+ }
+
+ const shipData = input as ShipbuildingItemCreateData;
+ const typeResult = await tx.insert(itemShipbuilding).values({
+ itemId: itemId,
+ workType: shipData.workType ? (shipData.workType as '기장' | '전장' | '선실' | '배관' | '철의') : '기장',
+ shipTypes: shipData.shipTypes || '',
+ createdAt: new Date(),
+ updatedAt: new Date()
+ }).returning();
+
+ return { itemData: itemResult[0], shipbuildingData: typeResult[0] };
+ })
+
+ revalidateTag("items")
+
+ return {
+ success: true,
+ data: result || null,
+ error: null
+ }
+ } catch (err) {
+ console.error("아이템 생성/업데이트 오류:", err)
+
+ if (err instanceof Error && err.message.includes("unique constraint")) {
+ return {
+ success: false,
+ message: "이미 존재하는 아이템 코드입니다",
+ data: null,
+ error: "중복 키 오류"
+ }
+ }
+
+ return {
+ success: false,
+ message: getErrorMessage(err),
+ data: null,
+ error: getErrorMessage(err)
+ }
+ }
+}
+
+/**
+ * Excel import를 위한 조선 아이템 생성 함수
+ * 하나의 아이템 코드에 대해 여러 선종을 처리 (1:N 관계)
+ */
+export async function createShipbuildingImportItem(input: {
+ itemCode: string;
+ itemName: string;
+ workType: '기장' | '전장' | '선실' | '배관' | '철의';
+ description?: string | null;
+ shipTypes: Record<string, boolean>;
+}) {
+ unstable_noStore();
+
+ try {
+
+ if (!input.itemCode || !input.itemName) {
+ return {
+ success: false,
+ message: "아이템 코드와 아이템 명은 필수입니다",
+ data: null,
+ error: "필수 필드 누락"
+ }
+ }
+ const results = await db.transaction(async (tx) => {
+ // 1. itemCode 정규화해서 직접 쿼리
+ const existRows = await tx.select().from(items)
+ .where(eq(items.itemCode, input.itemCode));
+ const existingItem = existRows[0];
+
+ console.log('DB에서 직접 조회한 기존 아이템:', existingItem);
+
+ let itemId: number;
+
+ if (existingItem) {
+ // 이미 있으면 업데이트
+ await updateItem(tx, existingItem.id, {
+ itemName: input.itemName,
+ description: input.description,
+ });
+ itemId = existingItem.id;
+ console.log('기존 아이템 업데이트, id:', itemId);
+ } else {
+ // 없으면 새로 생성
+ // 현재 가장 큰 ID 값 가져오기
+ const maxIdResult = await tx.select({ maxId: sql`MAX(id)` }).from(items);
+ const maxId = maxIdResult[0]?.maxId || 0;
+ const newId = Number(maxId) + 1;
+ console.log('새 아이템 생성을 위한 ID 계산:', { maxId, newId });
+
+ // 새 ID로 아이템 생성
+ const insertResult = await tx.insert(items).values({
+ id: newId,
+ itemCode: input.itemCode,
+ itemName: input.itemName,
+ description: input.description,
+ }).returning();
+
+ itemId = insertResult[0].id;
+ console.log('새 아이템 생성 완료, id:', itemId);
+ }
+
+ const createdItems = [];
+ for (const shipType of Object.keys(input.shipTypes)) {
+ // 그대로 선종명 string으로 저장
+ const existShip = await tx.select().from(itemShipbuilding)
+ .where(
+ and(
+ eq(itemShipbuilding.itemId, itemId),
+ eq(itemShipbuilding.shipTypes, shipType)
+ )
+ );
+ if (!existShip[0]) {
+ const shipbuildingResult = await tx.insert(itemShipbuilding).values({
+ itemId: itemId,
+ workType: input.workType,
+ shipTypes: shipType,
+ createdAt: new Date(),
+ updatedAt: new Date()
+ }).returning();
+ createdItems.push({
+ ...shipbuildingResult[0]
+ });
+ console.log('조선아이템 생성:', shipType, shipbuildingResult[0]);
+ } else {
+ console.log('이미 존재하는 조선아이템:', shipType);
+ }
+ }
+ return createdItems;
+ });
+
+ revalidateTag("items");
+
+ return {
+ success: true,
+ data: results,
+ error: null
+ }
+ } catch (err) {
+
+ return {
+ success: false,
+ message: getErrorMessage(err),
+ data: null,
+ error: getErrorMessage(err)
+ }
+ }
+}
+
+export async function createOffshoreTopItem(data: OffshoreTopItemCreateData) {
+ unstable_noStore()
+
+ try {
+ if (!data.itemCode || !data.itemName) {
+ return {
+ success: false,
+ message: "아이템 코드와 아이템 명은 필수입니다",
+ data: null,
+ error: "필수 필드 누락"
+ }
+ }
+
+ // 트랜잭션 내에서 처리
+ const result = await db.transaction(async (tx) => {
+ // 1. itemCode 정규화해서 직접 쿼리
+ const existRows = await tx.select().from(items)
+ .where(eq(items.itemCode, data.itemCode));
+ const existingItem = existRows[0];
+
+ let itemId: number;
+ let itemResult;
+
+ if (existingItem) {
+ // 이미 있으면 업데이트
+ itemResult = await updateItem(tx, existingItem.id, {
+ itemName: data.itemName,
+ description: data.description,
+ });
+ itemId = existingItem.id;
+ } else {
+ // 없으면 새로 생성
+ // 현재 가장 큰 ID 값 가져오기
+ const maxIdResult = await tx.select({ maxId: sql`MAX(id)` }).from(items);
+ const maxId = maxIdResult[0]?.maxId || 0;
+ const newId = Number(maxId) + 1;
+
+ // 새 ID로 아이템 생성
+ itemResult = await tx.insert(items).values({
+ id: newId,
+ itemCode: data.itemCode,
+ itemName: data.itemName,
+ description: data.description,
+ }).returning();
+
+ itemId = itemResult[0].id;
+ }
+
+ const [offshoreTop] = await tx
+ .insert(itemOffshoreTop)
+ .values({
+ itemId: itemId,
+ workType: data.workType,
+ itemList1: data.itemList1 || null,
+ itemList2: data.itemList2 || null,
+ itemList3: data.itemList3 || null,
+ itemList4: data.itemList4 || null,
+ createdAt: new Date(),
+ updatedAt: new Date()
+ })
+ .returning();
+
+ return { itemData: itemResult[0], offshoreTopData: offshoreTop };
+ })
+
+ revalidateTag("items")
+
+ return {
+ success: true,
+ data: result,
+ error: null
+ }
+ } catch (err) {
+ console.error("아이템 생성/업데이트 오류:", err)
+
+ if (err instanceof Error && err.message.includes("unique constraint")) {
+ return {
+ success: false,
+ message: "이미 존재하는 아이템 코드입니다",
+ data: null,
+ error: "중복 키 오류"
+ }
+ }
+
+ return {
+ success: false,
+ message: getErrorMessage(err),
+ data: null,
+ error: getErrorMessage(err)
+ }
+ }
+}
+
+export async function createOffshoreHullItem(data: OffshoreHullItemCreateData) {
+ unstable_noStore()
+
+ try {
+ if (!data.itemCode || !data.itemName) {
+ return {
+ success: false,
+ message: "아이템 코드와 아이템 명은 필수입니다",
+ data: null,
+ error: "필수 필드 누락"
+ }
+ }
+
+ // 트랜잭션 내에서 처리
+ const result = await db.transaction(async (tx) => {
+ // 1. itemCode 정규화해서 직접 쿼리
+ const existRows = await tx.select().from(items)
+ .where(eq(items.itemCode, data.itemCode));
+ const existingItem = existRows[0];
+
+ let itemId: number;
+ let itemResult;
+
+ if (existingItem) {
+ // 이미 있으면 업데이트
+ itemResult = await updateItem(tx, existingItem.id, {
+ itemName: data.itemName,
+ description: data.description,
+ });
+ itemId = existingItem.id;
+ } else {
+ // 없으면 새로 생성
+ // 현재 가장 큰 ID 값 가져오기
+ const maxIdResult = await tx.select({ maxId: sql`MAX(id)` }).from(items);
+ const maxId = maxIdResult[0]?.maxId || 0;
+ const newId = Number(maxId) + 1;
+
+ // 새 ID로 아이템 생성
+ itemResult = await tx.insert(items).values({
+ id: newId,
+ itemCode: data.itemCode,
+ itemName: data.itemName,
+ description: data.description,
+ }).returning();
+
+ itemId = itemResult[0].id;
+ }
+
+ const [offshoreHull] = await tx
+ .insert(itemOffshoreHull)
+ .values({
+ itemId: itemId,
+ workType: data.workType,
+ itemList1: data.itemList1 || null,
+ itemList2: data.itemList2 || null,
+ itemList3: data.itemList3 || null,
+ itemList4: data.itemList4 || null,
+ createdAt: new Date(),
+ updatedAt: new Date()
+ })
+ .returning();
+
+ return { itemData: itemResult[0], offshoreHullData: offshoreHull };
+ })
+
+ revalidateTag("items")
+
+ return {
+ success: true,
+ data: result,
+ error: null
+ }
+ } catch (err) {
+ console.error("아이템 생성/업데이트 오류:", err)
+
+ if (err instanceof Error && err.message.includes("unique constraint")) {
+ return {
+ success: false,
+ message: "이미 존재하는 아이템 코드입니다",
+ data: null,
+ error: "중복 키 오류"
+ }
+ }
+
+ return {
+ success: false,
+ message: getErrorMessage(err),
+ data: null,
+ error: getErrorMessage(err)
+ }
+ }
+}
+
+/* -----------------------------------------------------
+ 3) 업데이트
+----------------------------------------------------- */
+
+// 업데이트 타입 정의 인터페이스
+interface UpdateShipbuildingItemInput extends UpdateItemSchema {
+ id: number;
+ workType?: string;
+ shipTypes?: string;
+ itemCode?: string;
+ itemName?: string;
+ description?: string;
+}
+
+/** 단건 업데이트 */
+export async function modifyShipbuildingItem(input: UpdateShipbuildingItemInput) {
+ unstable_noStore();
+ try {
+ const result = await db.transaction(async (tx) => {
+ // 기존 아이템 조회
+ const existingShipbuilding = await tx.query.itemShipbuilding.findFirst({
+ where: eq(itemShipbuilding.id, input.id),
+ with: {
+ item: true
+ }
+ });
+
+ if (!existingShipbuilding) {
+ throw new Error("아이템을 찾을 수 없습니다.");
+ }
+
+ const existingItem = existingShipbuilding.item;
+
+ // 아이템 테이블 정보가 변경되었는지 확인
+ const isItemChanged =
+ (input.itemCode && input.itemCode !== existingItem.itemCode) ||
+ (input.itemName && input.itemName !== existingItem.itemName) ||
+ (input.description !== undefined && input.description !== existingItem.description);
+
+ // 세부 아이템 정보만 변경된 경우
+ if (!isItemChanged) {
+ // 조선 아이템 테이블 업데이트
+ if (input.workType || input.shipTypes) {
+ await tx.update(itemShipbuilding)
+ .set({
+ workType: input.workType as '기장' | '전장' | '선실' | '배관' | '철의',
+ shipTypes: input.shipTypes
+ })
+ .where(eq(itemShipbuilding.id, input.id));
+ }
+
+ return {
+ data: { id: input.id },
+ error: null,
+ success: true,
+ message: "아이템이 성공적으로 업데이트되었습니다."
+ };
+ }
+ // 아이템 테이블 정보가 변경된 경우 - 새 아이템 생성하고 세부 아이템 연결
+ else {
+ // 새 아이템 생성
+ const [newItem] = await insertItem(tx, {
+ itemCode: input.itemCode || existingItem.itemCode,
+ itemName: input.itemName || existingItem.itemName,
+ description: input.description !== undefined ? input.description : existingItem.description,
+ });
+
+ // 세부 아이템 테이블 정보를 새 아이템에 연결
+ await tx.update(itemShipbuilding)
+ .set({
+ itemId: newItem.id,
+ workType: input.workType ? (input.workType as '기장' | '전장' | '선실' | '배관' | '철의') : existingShipbuilding.workType,
+ shipTypes: input.shipTypes || existingShipbuilding.shipTypes
+ })
+ .where(eq(itemShipbuilding.id, input.id));
+
+ return {
+ data: { id: input.id },
+ error: null,
+ success: true,
+ message: "새 아이템이 생성되고 세부 정보가 업데이트되었습니다."
+ };
+ }
+ });
+
+ // 캐시 무효화
+ revalidateTag("items");
+
+ return result;
+ } catch (err) {
+ return {
+ data: null,
+ error: getErrorMessage(err),
+ success: false,
+ message: "아이템 업데이트 중 오류가 발생했습니다."
+ };
+ }
+}
+
+// Offshore TOP 업데이트 타입 정의 인터페이스
+interface UpdateOffshoreTopItemInput extends UpdateItemSchema {
+ id: number;
+ workType?: string;
+ itemList1?: string;
+ itemList2?: string;
+ itemList3?: string;
+ itemList4?: string;
+ itemCode?: string;
+ itemName?: string;
+ description?: string;
+}
+
+/** Offshore TOP 단건 업데이트 */
+export async function modifyOffshoreTopItem(input: UpdateOffshoreTopItemInput) {
+ unstable_noStore();
+ try {
+ const result = await db.transaction(async (tx) => {
+ // 기존 아이템 조회
+ const existingOffshoreTop = await tx.query.itemOffshoreTop.findFirst({
+ where: eq(itemOffshoreTop.id, input.id),
+ with: {
+ item: true
+ }
+ });
+
+ if (!existingOffshoreTop) {
+ throw new Error("아이템을 찾을 수 없습니다.");
+ }
+
+ const existingItem = existingOffshoreTop.item;
+
+ // 아이템 테이블 정보가 변경되었는지 확인
+ const isItemChanged =
+ (input.itemCode && input.itemCode !== existingItem.itemCode) ||
+ (input.itemName && input.itemName !== existingItem.itemName) ||
+ (input.description !== undefined && input.description !== existingItem.description);
+
+ // 세부 아이템 정보만 변경된 경우
+ if (!isItemChanged) {
+ // Offshore TOP 아이템 테이블 업데이트
+ const updateData: Record<string, unknown> = {};
+
+ if (input.workType) updateData.workType = input.workType as 'TM' | 'TS' | 'TE' | 'TP';
+ if (input.itemList1 !== undefined) updateData.itemList1 = input.itemList1;
+ if (input.itemList2 !== undefined) updateData.itemList2 = input.itemList2;
+ if (input.itemList3 !== undefined) updateData.itemList3 = input.itemList3;
+ if (input.itemList4 !== undefined) updateData.itemList4 = input.itemList4;
+
+ if (Object.keys(updateData).length > 0) {
+ await tx.update(itemOffshoreTop)
+ .set(updateData)
+ .where(eq(itemOffshoreTop.id, input.id));
+ }
+
+ return {
+ data: { id: input.id },
+ error: null,
+ success: true,
+ message: "아이템이 성공적으로 업데이트되었습니다."
+ };
+ }
+ // 아이템 테이블 정보가 변경된 경우 - 새 아이템 생성하고 세부 아이템 연결
+ else {
+ // 새 아이템 생성
+ const [newItem] = await insertItem(tx, {
+ itemCode: input.itemCode || existingItem.itemCode,
+ itemName: input.itemName || existingItem.itemName,
+ description: input.description !== undefined ? input.description : existingItem.description,
+ });
+
+ // 세부 아이템 테이블 정보를 새 아이템에 연결
+ const updateData: Record<string, unknown> = {
+ itemId: newItem.id
+ };
+
+ if (input.workType) updateData.workType = input.workType as 'TM' | 'TS' | 'TE' | 'TP';
+ if (input.itemList1 !== undefined) updateData.itemList1 = input.itemList1;
+ if (input.itemList2 !== undefined) updateData.itemList2 = input.itemList2;
+ if (input.itemList3 !== undefined) updateData.itemList3 = input.itemList3;
+ if (input.itemList4 !== undefined) updateData.itemList4 = input.itemList4;
+
+ await tx.update(itemOffshoreTop)
+ .set(updateData)
+ .where(eq(itemOffshoreTop.id, input.id));
+
+ return {
+ data: { id: input.id },
+ error: null,
+ success: true,
+ message: "새 아이템이 생성되고 세부 정보가 업데이트되었습니다."
+ };
+ }
+ });
+
+ // 캐시 무효화
+ revalidateTag("items");
+
+ return result;
+ } catch (err) {
+ return {
+ data: null,
+ error: getErrorMessage(err),
+ success: false,
+ message: "아이템 업데이트 중 오류가 발생했습니다."
+ };
+ }
+}
+
+// Offshore HULL 업데이트 타입 정의 인터페이스
+interface UpdateOffshoreHullItemInput extends UpdateItemSchema {
+ id: number;
+ workType?: string;
+ itemList1?: string;
+ itemList2?: string;
+ itemList3?: string;
+ itemList4?: string;
+ itemCode?: string;
+ itemName?: string;
+ description?: string;
+}
+
+/** Offshore HULL 단건 업데이트 */
+export async function modifyOffshoreHullItem(input: UpdateOffshoreHullItemInput) {
+ unstable_noStore();
+ try {
+ const result = await db.transaction(async (tx) => {
+ // 기존 아이템 조회
+ const existingOffshoreHull = await tx.query.itemOffshoreHull.findFirst({
+ where: eq(itemOffshoreHull.id, input.id),
+ with: {
+ item: true
+ }
+ });
+
+ if (!existingOffshoreHull) {
+ throw new Error("아이템을 찾을 수 없습니다.");
+ }
+
+ const existingItem = existingOffshoreHull.item;
+
+ // 아이템 테이블 정보가 변경되었는지 확인
+ const isItemChanged =
+ (input.itemCode && input.itemCode !== existingItem.itemCode) ||
+ (input.itemName && input.itemName !== existingItem.itemName) ||
+ (input.description !== undefined && input.description !== existingItem.description);
+
+ // 세부 아이템 정보만 변경된 경우
+ if (!isItemChanged) {
+ // Offshore HULL 아이템 테이블 업데이트
+ const updateData: Record<string, unknown> = {};
+
+ if (input.workType) updateData.workType = input.workType as 'HA' | 'HE' | 'HH' | 'HM' | 'NC';
+ if (input.itemList1 !== undefined) updateData.itemList1 = input.itemList1;
+ if (input.itemList2 !== undefined) updateData.itemList2 = input.itemList2;
+ if (input.itemList3 !== undefined) updateData.itemList3 = input.itemList3;
+ if (input.itemList4 !== undefined) updateData.itemList4 = input.itemList4;
+
+ if (Object.keys(updateData).length > 0) {
+ await tx.update(itemOffshoreHull)
+ .set(updateData)
+ .where(eq(itemOffshoreHull.id, input.id));
+ }
+
+ return {
+ data: { id: input.id },
+ error: null,
+ success: true,
+ message: "아이템이 성공적으로 업데이트되었습니다."
+ };
+ }
+ // 아이템 테이블 정보가 변경된 경우 - 새 아이템 생성하고 세부 아이템 연결
+ else {
+ // 새 아이템 생성
+ const [newItem] = await insertItem(tx, {
+ itemCode: input.itemCode || existingItem.itemCode,
+ itemName: input.itemName || existingItem.itemName,
+ description: input.description !== undefined ? input.description : existingItem.description,
+ });
+
+ // 세부 아이템 테이블 정보를 새 아이템에 연결
+ const updateData: Record<string, unknown> = {
+ itemId: newItem.id
+ };
+
+ if (input.workType) updateData.workType = input.workType as 'HA' | 'HE' | 'HH' | 'HM' | 'NC';
+ if (input.itemList1 !== undefined) updateData.itemList1 = input.itemList1;
+ if (input.itemList2 !== undefined) updateData.itemList2 = input.itemList2;
+ if (input.itemList3 !== undefined) updateData.itemList3 = input.itemList3;
+ if (input.itemList4 !== undefined) updateData.itemList4 = input.itemList4;
+
+ await tx.update(itemOffshoreHull)
+ .set(updateData)
+ .where(eq(itemOffshoreHull.id, input.id));
+
+ return {
+ data: { id: input.id },
+ error: null,
+ success: true,
+ message: "새 아이템이 생성되고 세부 정보가 업데이트되었습니다."
+ };
+ }
+ });
+
+ // 캐시 무효화
+ revalidateTag("items");
+
+ return result;
+ } catch (err) {
+ return {
+ data: null,
+ error: getErrorMessage(err),
+ success: false,
+ message: "아이템 업데이트 중 오류가 발생했습니다."
+ };
+ }
+}
+
+/* -----------------------------------------------------
+ 4) 삭제
+----------------------------------------------------- */
+
+// 삭제 타입 정의 인터페이스
+interface DeleteItemInput {
+ id: number;
+}
+
+interface DeleteItemsInput {
+ ids: number[];
+}
+
+/** 단건 삭제 */
+export async function removeShipbuildingItem(input: DeleteItemInput) {
+ unstable_noStore();
+ try {
+ await db.transaction(async (tx) => {
+ // 세부 아이템만 삭제 (아이템 테이블은 유지)
+ await tx.delete(itemShipbuilding)
+ .where(eq(itemShipbuilding.id, input.id));
+ });
+
+ revalidateTag("items");
+
+ return {
+ data: null,
+ error: null,
+ success: true,
+ message: "아이템이 성공적으로 삭제되었습니다."
+ };
+ } catch (err) {
+ return {
+ data: null,
+ error: getErrorMessage(err),
+ success: false,
+ message: "아이템 삭제 중 오류가 발생했습니다."
+ };
+ }
+}
+
+/** 복수 삭제 */
+export async function removeShipbuildingItems(input: DeleteItemsInput) {
+ unstable_noStore();
+ try {
+ await db.transaction(async (tx) => {
+ if (input.ids.length > 0) {
+ // 세부 아이템만 삭제 (아이템 테이블은 유지)
+ await tx.delete(itemShipbuilding)
+ .where(inArray(itemShipbuilding.id, input.ids));
+ }
+ });
+
+ revalidateTag("items");
+
+ return { data: null, error: null, success: true, message: "아이템이 성공적으로 삭제되었습니다." };
+ } catch (err) {
+ return { data: null, error: getErrorMessage(err), success: false, message: "아이템 삭제 중 오류가 발생했습니다." };
+ }
+}
+
+/** Offshore TOP 단건 삭제 */
+export async function removeOffshoreTopItem(input: DeleteItemInput) {
+ unstable_noStore();
+ try {
+ await db.transaction(async (tx) => {
+ // 세부 아이템만 삭제 (아이템 테이블은 유지)
+ await tx.delete(itemOffshoreTop)
+ .where(eq(itemOffshoreTop.id, input.id));
+ });
+
+ revalidateTag("items");
+
+ return {
+ data: null,
+ error: null,
+ success: true,
+ message: "아이템이 성공적으로 삭제되었습니다."
+ };
+ } catch (err) {
+ return {
+ data: null,
+ error: getErrorMessage(err),
+ success: false,
+ message: "아이템 삭제 중 오류가 발생했습니다."
+ };
+ }
+}
+
+/** Offshore TOP 복수 삭제 */
+export async function removeOffshoreTopItems(input: DeleteItemsInput) {
+ unstable_noStore();
+ try {
+ await db.transaction(async (tx) => {
+ if (input.ids.length > 0) {
+ // 세부 아이템만 삭제 (아이템 테이블은 유지)
+ await tx.delete(itemOffshoreTop)
+ .where(inArray(itemOffshoreTop.id, input.ids));
+ }
+ });
+
+ revalidateTag("items");
+
+ return { data: null, error: null, success: true, message: "아이템이 성공적으로 삭제되었습니다." };
+ } catch (err) {
+ return { data: null, error: getErrorMessage(err), success: false, message: "아이템 삭제 중 오류가 발생했습니다." };
+ }
+}
+
+/** Offshore HULL 단건 삭제 */
+export async function removeOffshoreHullItem(input: DeleteItemInput) {
+ unstable_noStore();
+ try {
+ await db.transaction(async (tx) => {
+ // 세부 아이템만 삭제 (아이템 테이블은 유지)
+ await tx.delete(itemOffshoreHull)
+ .where(eq(itemOffshoreHull.id, input.id));
+ });
+
+ revalidateTag("items");
+
+ return {
+ data: null,
+ error: null,
+ success: true,
+ message: "아이템이 성공적으로 삭제되었습니다."
+ };
+ } catch (err) {
+ return {
+ data: null,
+ error: getErrorMessage(err),
+ success: false,
+ message: "아이템 삭제 중 오류가 발생했습니다."
+ };
+ }
+}
+
+/** Offshore HULL 복수 삭제 */
+export async function removeOffshoreHullItems(input: DeleteItemsInput) {
+ unstable_noStore();
+ try {
+ await db.transaction(async (tx) => {
+ if (input.ids.length > 0) {
+ // 세부 아이템만 삭제 (아이템 테이블은 유지)
+ await tx.delete(itemOffshoreHull)
+ .where(inArray(itemOffshoreHull.id, input.ids));
+ }
+ });
+
+ revalidateTag("items");
+
+ return { data: null, error: null, success: true, message: "아이템이 성공적으로 삭제되었습니다." };
+ } catch (err) {
+ return { data: null, error: getErrorMessage(err), success: false, message: "아이템 삭제 중 오류가 발생했습니다." };
+ }
+}
+
+export async function getAllShipbuildingItems(): Promise<Item[]> {
+ try {
+ return await findAllItems();
+ } catch (error) {
+ console.error("Failed to get items:", error);
+ throw new Error("Failed to get items");
+ }
+}
diff --git a/lib/items-tech/table/add-items-dialog.tsx b/lib/items-tech/table/add-items-dialog.tsx
new file mode 100644
index 00000000..86333189
--- /dev/null
+++ b/lib/items-tech/table/add-items-dialog.tsx
@@ -0,0 +1,400 @@
+"use client"
+
+import * as React from "react"
+import { useRouter } from "next/navigation"
+import { zodResolver } from "@hookform/resolvers/zod"
+import { useForm } from "react-hook-form"
+import { Plus } from "lucide-react"
+import * as z from "zod"
+
+import { Button } from "@/components/ui/button"
+import {
+ Dialog,
+ DialogContent,
+ DialogDescription,
+ DialogHeader,
+ DialogTitle,
+ DialogTrigger,
+} from "@/components/ui/dialog"
+import {
+ Form,
+ FormControl,
+ FormField,
+ FormItem,
+ FormLabel,
+ FormMessage,
+} from "@/components/ui/form"
+import { Input } from "@/components/ui/input"
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from "@/components/ui/select"
+import { Textarea } from "@/components/ui/textarea"
+import { toast } from "sonner"
+
+import { createShipbuildingItem, createOffshoreTopItem, createOffshoreHullItem } from "../service"
+import { ItemType } from "./delete-items-dialog"
+
+// 조선 공종 유형 정의
+const shipbuildingWorkTypes = [
+ { label: "기장", value: "기장" },
+ { label: "전장", value: "전장" },
+ { label: "선실", value: "선실" },
+ { label: "배관", value: "배관" },
+ { label: "철의", value: "철의" },
+] as const
+
+// 선종 유형 정의
+const shipTypes = [
+ { label: "A-MAX", value: "A-MAX" },
+ { label: "S-MAX", value: "S-MAX" },
+ { label: "LNGC", value: "LNGC" },
+ { label: "VLCC", value: "VLCC" },
+ { label: "CONT", value: "CONT" },
+] as const
+
+// 해양 TOP 공종 유형 정의
+const offshoreTopWorkTypes = [
+ { label: "TM", value: "TM" },
+ { label: "TS", value: "TS" },
+ { label: "TE", value: "TE" },
+ { label: "TP", value: "TP" },
+] as const
+
+// 해양 HULL 공종 유형 정의
+const offshoreHullWorkTypes = [
+ { label: "HA", value: "HA" },
+ { label: "HE", value: "HE" },
+ { label: "HH", value: "HH" },
+ { label: "HM", value: "HM" },
+ { label: "NC", value: "NC" },
+] as const
+
+// 기본 아이템 스키마
+const itemFormSchema = z.object({
+ itemCode: z.string().min(1, "아이템 코드는 필수입니다"),
+ itemName: z.string().min(1, "아이템 명은 필수입니다"),
+ description: z.string().optional(),
+ workType: z.string().min(1, "공종은 필수입니다"),
+ // 조선 아이템 전용 필드
+ shipTypes: z.string().optional(),
+ // 해양 아이템 전용 필드
+ itemList1: z.string().optional(),
+ itemList2: z.string().optional(),
+ itemList3: z.string().optional(),
+ itemList4: z.string().optional(),
+})
+
+type ItemFormValues = z.infer<typeof itemFormSchema>
+
+interface AddItemDialogProps {
+ itemType: ItemType
+}
+
+export function AddItemDialog({ itemType }: AddItemDialogProps) {
+ const router = useRouter()
+ const [open, setOpen] = React.useState(false)
+
+ // 기본값 설정
+ const getDefaultValues = () => {
+ const defaults: ItemFormValues = {
+ itemCode: "",
+ itemName: "",
+ description: "",
+ workType: getDefaultWorkType(),
+ }
+
+ if (itemType === 'shipbuilding') {
+ defaults.shipTypes = "A-MAX"
+ } else {
+ defaults.itemList1 = ""
+ defaults.itemList2 = ""
+ defaults.itemList3 = ""
+ defaults.itemList4 = ""
+ }
+
+ return defaults
+ }
+
+ const getDefaultWorkType = () => {
+ switch (itemType) {
+ case 'shipbuilding':
+ return "기장"
+ case 'offshoreTop':
+ return "TM"
+ case 'offshoreHull':
+ return "HA"
+ default:
+ return ""
+ }
+ }
+
+ const form = useForm<ItemFormValues>({
+ resolver: zodResolver(itemFormSchema),
+ defaultValues: getDefaultValues(),
+ })
+
+ const onSubmit = async (data: ItemFormValues) => {
+ try {
+ switch (itemType) {
+ case 'shipbuilding':
+ if (!data.shipTypes) {
+ toast.error("선종은 필수입니다")
+ return
+ }
+
+ await createShipbuildingItem({
+ itemCode: data.itemCode,
+ itemName: data.itemName,
+ workType: data.workType,
+ shipTypes: data.shipTypes,
+ description: data.description || null
+ });
+ break;
+
+ case 'offshoreTop':
+ await createOffshoreTopItem({
+ itemCode: data.itemCode,
+ itemName: data.itemName,
+ workType: data.workType as "TM" | "TS" | "TE" | "TP",
+ description: data.description || null,
+ itemList1: data.itemList1 || null,
+ itemList2: data.itemList2 || null,
+ itemList3: data.itemList3 || null,
+ itemList4: data.itemList4 || null
+ });
+ break;
+
+ case 'offshoreHull':
+ await createOffshoreHullItem({
+ itemCode: data.itemCode,
+ itemName: data.itemName,
+ workType: data.workType as "HA" | "HE" | "HH" | "HM" | "NC",
+ description: data.description || null,
+ itemList1: data.itemList1 || null,
+ itemList2: data.itemList2 || null,
+ itemList3: data.itemList3 || null,
+ itemList4: data.itemList4 || null
+ });
+ break;
+
+ default:
+ toast.error("지원하지 않는 아이템 타입입니다");
+ return;
+ }
+
+ toast.success("아이템이 성공적으로 추가되었습니다")
+ setOpen(false)
+ form.reset(getDefaultValues())
+ router.refresh()
+ } catch (error) {
+ toast.error("아이템 추가 중 오류가 발생했습니다")
+ console.error(error)
+ }
+ }
+
+ const getItemTypeLabel = () => {
+ switch (itemType) {
+ case 'shipbuilding':
+ return '조선 아이템';
+ case 'offshoreTop':
+ return '해양 TOP 아이템';
+ case 'offshoreHull':
+ return '해양 HULL 아이템';
+ default:
+ return '아이템';
+ }
+ }
+
+ const getWorkTypeOptions = () => {
+ switch (itemType) {
+ case 'shipbuilding':
+ return shipbuildingWorkTypes;
+ case 'offshoreTop':
+ return offshoreTopWorkTypes;
+ case 'offshoreHull':
+ return offshoreHullWorkTypes;
+ default:
+ return [];
+ }
+ }
+
+ return (
+ <Dialog open={open} onOpenChange={setOpen}>
+ <DialogTrigger asChild>
+ <Button>
+ <Plus className="mr-2 h-4 w-4" />
+ {getItemTypeLabel()} 추가
+ </Button>
+ </DialogTrigger>
+ <DialogContent>
+ <DialogHeader>
+ <DialogTitle>
+ {getItemTypeLabel()} 추가
+ </DialogTitle>
+ <DialogDescription>
+ 새로운 {getItemTypeLabel()}을 추가합니다.
+ </DialogDescription>
+ </DialogHeader>
+ <Form {...form}>
+ <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
+ <FormField
+ control={form.control}
+ name="itemCode"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>아이템 코드</FormLabel>
+ <FormControl>
+ <Input {...field} />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="itemName"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>아이템 명</FormLabel>
+ <FormControl>
+ <Input {...field} />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="workType"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>공종</FormLabel>
+ <Select onValueChange={field.onChange} defaultValue={field.value}>
+ <FormControl>
+ <SelectTrigger>
+ <SelectValue placeholder="공종을 선택하세요" />
+ </SelectTrigger>
+ </FormControl>
+ <SelectContent>
+ {getWorkTypeOptions().map((type) => (
+ <SelectItem key={type.value} value={type.value}>
+ {type.label}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ {itemType === 'shipbuilding' && (
+ <FormField
+ control={form.control}
+ name="shipTypes"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>선종</FormLabel>
+ <Select onValueChange={field.onChange} defaultValue={field.value}>
+ <FormControl>
+ <SelectTrigger>
+ <SelectValue placeholder="선종을 선택하세요" />
+ </SelectTrigger>
+ </FormControl>
+ <SelectContent>
+ {shipTypes.map((type) => (
+ <SelectItem key={type.value} value={type.value}>
+ {type.label}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ )}
+ {(itemType === 'offshoreTop' || itemType === 'offshoreHull') && (
+ <>
+ <FormField
+ control={form.control}
+ name="itemList1"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>아이템 리스트 1</FormLabel>
+ <FormControl>
+ <Input {...field} />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="itemList2"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>아이템 리스트 2</FormLabel>
+ <FormControl>
+ <Input {...field} />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="itemList3"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>아이템 리스트 3</FormLabel>
+ <FormControl>
+ <Input {...field} />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="itemList4"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>아이템 리스트 4</FormLabel>
+ <FormControl>
+ <Input {...field} />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ </>
+ )}
+ <FormField
+ control={form.control}
+ name="description"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>설명</FormLabel>
+ <FormControl>
+ <Textarea {...field} />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <div className="flex justify-end space-x-2">
+ <Button type="button" variant="outline" onClick={() => setOpen(false)}>
+ 취소
+ </Button>
+ <Button type="submit">추가</Button>
+ </div>
+ </form>
+ </Form>
+ </DialogContent>
+ </Dialog>
+ )
+} \ No newline at end of file
diff --git a/lib/items-ship/table/delete-items-dialog.tsx b/lib/items-tech/table/delete-items-dialog.tsx
index 53e047fb..b94a2333 100644
--- a/lib/items-ship/table/delete-items-dialog.tsx
+++ b/lib/items-tech/table/delete-items-dialog.tsx
@@ -29,33 +29,73 @@ import {
} from "@/components/ui/drawer"
import { Item } from "@/db/schema/items"
-import { removeShipbuildingItems } from "../service"
+import {
+ removeShipbuildingItems,
+ removeOffshoreTopItems,
+ removeOffshoreHullItems
+} from "../service"
+
+export type ItemType = 'shipbuilding' | 'offshoreTop' | 'offshoreHull';
interface DeleteItemsDialogProps
extends React.ComponentPropsWithoutRef<typeof Dialog> {
items: Row<Item>["original"][]
showTrigger?: boolean
onSuccess?: () => void
+ itemType: ItemType
}
export function DeleteItemsDialog({
items,
showTrigger = true,
onSuccess,
+ itemType,
...props
}: DeleteItemsDialogProps) {
const [isDeletePending, startDeleteTransition] = React.useTransition()
const isDesktop = useMediaQuery("(min-width: 640px)")
+ const getItemTypeLabel = () => {
+ switch (itemType) {
+ case 'shipbuilding':
+ return '조선 아이템';
+ case 'offshoreTop':
+ return '해양 TOP 아이템';
+ case 'offshoreHull':
+ return '해양 HULL 아이템';
+ default:
+ return '아이템';
+ }
+ }
+
async function onDelete() {
try {
startDeleteTransition(async () => {
- const { error } = await removeShipbuildingItems({
- ids: items.map((item) => item.id),
- })
+ let result;
+
+ switch (itemType) {
+ case 'shipbuilding':
+ result = await removeShipbuildingItems({
+ ids: items.map((item) => item.id),
+ });
+ break;
+ case 'offshoreTop':
+ result = await removeOffshoreTopItems({
+ ids: items.map((item) => item.id),
+ });
+ break;
+ case 'offshoreHull':
+ result = await removeOffshoreHullItems({
+ ids: items.map((item) => item.id),
+ });
+ break;
+ default:
+ toast.error("지원하지 않는 아이템 타입입니다");
+ return;
+ }
- if (error) {
- toast.error(error)
+ if (result.error) {
+ toast.error(result.error)
return
}
@@ -86,7 +126,7 @@ export function DeleteItemsDialog({
<DialogDescription>
이 작업은 되돌릴 수 없습니다. 선택한{" "}
<span className="font-medium">{items.length}</span>
- 개의 조선 아이템이(가) 영구적으로 삭제됩니다.
+ 개의 {getItemTypeLabel()}이(가) 영구적으로 삭제됩니다.
</DialogDescription>
</DialogHeader>
<DialogFooter className="gap-2 sm:space-x-0">
@@ -129,7 +169,7 @@ export function DeleteItemsDialog({
<DrawerDescription>
이 작업은 되돌릴 수 없습니다. 선택한{" "}
<span className="font-medium">{items.length}</span>
- 개의 조선 아이템이(가) 영구적으로 삭제됩니다.
+ 개의 {getItemTypeLabel()}이(가) 영구적으로 삭제됩니다.
</DrawerDescription>
</DrawerHeader>
<DrawerFooter className="gap-2 sm:space-x-0">
diff --git a/lib/items-ship/table/feature-flags.tsx b/lib/items-tech/table/feature-flags.tsx
index aaae6af2..aaae6af2 100644
--- a/lib/items-ship/table/feature-flags.tsx
+++ b/lib/items-tech/table/feature-flags.tsx
diff --git a/lib/items-tech/table/hull/import-item-handler.tsx b/lib/items-tech/table/hull/import-item-handler.tsx
new file mode 100644
index 00000000..40070769
--- /dev/null
+++ b/lib/items-tech/table/hull/import-item-handler.tsx
@@ -0,0 +1,143 @@
+"use client"
+
+import { z } from "zod"
+import { createOffshoreHullItem } from "../../service"
+
+// 해양 HULL 기능(공종) 유형 enum
+const HULL_WORK_TYPES = ["HA", "HE", "HH", "HM", "NC"] as const;
+
+// 아이템 데이터 검증을 위한 Zod 스키마
+const itemSchema = z.object({
+ itemCode: z.string().min(1, "아이템 코드는 필수입니다"),
+ itemName: z.string().min(1, "아이템 명은 필수입니다"),
+ workType: z.enum(HULL_WORK_TYPES, {
+ required_error: "기능(공종)은 필수입니다",
+ }),
+ description: z.string().nullable().optional(),
+ itemList1: z.string().nullable().optional(),
+ itemList2: z.string().nullable().optional(),
+ itemList3: z.string().nullable().optional(),
+ itemList4: z.string().nullable().optional(),
+});
+
+interface ProcessResult {
+ successCount: number;
+ errorCount: number;
+ errors?: Array<{ row: number; message: string }>;
+}
+
+/**
+ * Excel 파일에서 가져온 해양 HULL 아이템 데이터 처리하는 함수
+ */
+export async function processHullFileImport(
+ jsonData: any[],
+ progressCallback?: (current: number, total: number) => void
+): Promise<ProcessResult> {
+ // 결과 카운터 초기화
+ let successCount = 0;
+ let errorCount = 0;
+ const errors: Array<{ row: number; message: string }> = [];
+
+ // 빈 행 등 필터링
+ const dataRows = jsonData.filter(row => {
+ // 빈 행 건너뛰기
+ if (Object.values(row).every(val => !val)) {
+ return false;
+ }
+ return true;
+ });
+
+ // 데이터 행이 없으면 빈 결과 반환
+ if (dataRows.length === 0) {
+ return { successCount: 0, errorCount: 0 };
+ }
+
+ // 각 행에 대해 처리
+ for (let i = 0; i < dataRows.length; i++) {
+ const row = dataRows[i];
+ const rowIndex = i + 1; // 사용자에게 표시할 행 번호는 1부터 시작
+
+ // 진행 상황 콜백 호출
+ if (progressCallback) {
+ progressCallback(i + 1, dataRows.length);
+ }
+
+ try {
+ // 필드 매핑 (한글/영문 필드명 모두 지원)
+ const itemCode = row["아이템 코드"] || row["itemCode"] || "";
+ const itemName = row["아이템 명"] || row["itemName"] || "";
+ const workType = row["기능(공종)"] || row["workType"] || "";
+ const description = row["설명"] || row["description"] || null;
+ const itemList1 = row["항목1"] || row["itemList1"] || null;
+ const itemList2 = row["항목2"] || row["itemList2"] || null;
+ const itemList3 = row["항목3"] || row["itemList3"] || null;
+ const itemList4 = row["항목4"] || row["itemList4"] || null;
+
+ // 데이터 정제
+ const cleanedRow = {
+ itemCode: typeof itemCode === 'string' ? itemCode.trim() : String(itemCode).trim(),
+ itemName: typeof itemName === 'string' ? itemName.trim() : String(itemName).trim(),
+ workType: typeof workType === 'string' ? workType.trim() : String(workType).trim(),
+ description: description ? (typeof description === 'string' ? description : String(description)) : null,
+ itemList1: itemList1 ? (typeof itemList1 === 'string' ? itemList1 : String(itemList1)) : null,
+ itemList2: itemList2 ? (typeof itemList2 === 'string' ? itemList2 : String(itemList2)) : null,
+ itemList3: itemList3 ? (typeof itemList3 === 'string' ? itemList3 : String(itemList3)) : null,
+ itemList4: itemList4 ? (typeof itemList4 === 'string' ? itemList4 : String(itemList4)) : null,
+ };
+
+ // 데이터 유효성 검사
+ const validationResult = itemSchema.safeParse(cleanedRow);
+
+ if (!validationResult.success) {
+ const errorMessage = validationResult.error.errors.map(
+ err => `${err.path.join('.')}: ${err.message}`
+ ).join(', ');
+
+ errors.push({ row: rowIndex, message: errorMessage });
+ errorCount++;
+ continue;
+ }
+
+ // 해양 HULL 아이템 생성
+ const result = await createOffshoreHullItem({
+ itemCode: cleanedRow.itemCode,
+ itemName: cleanedRow.itemName,
+ workType: cleanedRow.workType as "HA" | "HE" | "HH" | "HM" | "NC",
+ description: cleanedRow.description,
+ itemList1: cleanedRow.itemList1,
+ itemList2: cleanedRow.itemList2,
+ itemList3: cleanedRow.itemList3,
+ itemList4: cleanedRow.itemList4,
+ });
+
+ if (result.success) {
+ successCount++;
+ } else {
+ errors.push({
+ row: rowIndex,
+ message: result.message || result.error || "알 수 없는 오류"
+ });
+ errorCount++;
+ }
+ } catch (error) {
+ console.error(`${rowIndex}행 처리 오류:`, error);
+ errors.push({
+ row: rowIndex,
+ message: error instanceof Error ? error.message : "알 수 없는 오류"
+ });
+ errorCount++;
+ }
+
+ // 비동기 작업 쓰로틀링
+ if (i % 5 === 0) {
+ await new Promise(resolve => setTimeout(resolve, 10));
+ }
+ }
+
+ // 처리 결과 반환
+ return {
+ successCount,
+ errorCount,
+ errors: errors.length > 0 ? errors : undefined
+ };
+}
diff --git a/lib/items-tech/table/hull/item-excel-template.tsx b/lib/items-tech/table/hull/item-excel-template.tsx
new file mode 100644
index 00000000..f38bb9dc
--- /dev/null
+++ b/lib/items-tech/table/hull/item-excel-template.tsx
@@ -0,0 +1,125 @@
+import * as ExcelJS from 'exceljs';
+import { saveAs } from "file-saver";
+
+// 해양 HULL 기능(공종) 유형
+const HULL_WORK_TYPES = ["HA", "HE", "HH", "HM", "NC"] as const;
+
+/**
+ * 해양 HULL 아이템 데이터 가져오기를 위한 Excel 템플릿 파일 생성 및 다운로드
+ */
+export async function exportHullItemTemplate() {
+ // 워크북 생성
+ const workbook = new ExcelJS.Workbook();
+ workbook.creator = 'Offshore HULL Item Management System';
+ workbook.created = new Date();
+
+ // 워크시트 생성
+ const worksheet = workbook.addWorksheet('해양 HULL 아이템');
+
+ // 컬럼 헤더 정의 및 스타일 적용
+ worksheet.columns = [
+ { header: '아이템 코드', key: 'itemCode', width: 15 },
+ { header: '아이템 명', key: 'itemName', width: 30 },
+ { header: '기능(공종)', key: 'workType', width: 15 },
+ { header: '설명', key: 'description', width: 50 },
+ { header: '항목1', key: 'itemList1', width: 20 },
+ { header: '항목2', key: 'itemList2', width: 20 },
+ { header: '항목3', key: 'itemList3', width: 20 },
+ { header: '항목4', key: 'itemList4', width: 20 },
+ ];
+
+ // 헤더 스타일 적용
+ const headerRow = worksheet.getRow(1);
+ headerRow.font = { bold: true };
+ headerRow.fill = {
+ type: 'pattern',
+ pattern: 'solid',
+ fgColor: { argb: 'FFE0E0E0' }
+ };
+ headerRow.alignment = { vertical: 'middle', horizontal: 'center' };
+
+ // 테두리 스타일 적용
+ headerRow.eachCell((cell) => {
+ cell.border = {
+ top: { style: 'thin' },
+ left: { style: 'thin' },
+ bottom: { style: 'thin' },
+ right: { style: 'thin' }
+ };
+ });
+
+ // 샘플 데이터 추가
+ const sampleData = [
+ {
+ itemCode: 'HULL001',
+ itemName: 'HULL 샘플 아이템 1',
+ workType: 'HA',
+ description: '이것은 해양 HULL 샘플 아이템 1의 설명입니다.',
+ itemList1: '항목1 샘플 데이터',
+ itemList2: '항목2 샘플 데이터',
+ itemList3: '항목3 샘플 데이터',
+ itemList4: '항목4 샘플 데이터'
+ },
+ {
+ itemCode: 'HULL002',
+ itemName: 'HULL 샘플 아이템 2',
+ workType: 'HE',
+ description: '이것은 해양 HULL 샘플 아이템 2의 설명입니다.',
+ itemList1: '항목1 샘플 데이터',
+ itemList2: '항목2 샘플 데이터',
+ itemList3: '',
+ itemList4: ''
+ }
+ ];
+
+ // 데이터 행 추가
+ sampleData.forEach(item => {
+ worksheet.addRow(item);
+ });
+
+ // 데이터 행 스타일 적용
+ worksheet.eachRow((row, rowNumber) => {
+ if (rowNumber > 1) { // 헤더를 제외한 데이터 행
+ row.eachCell((cell) => {
+ cell.border = {
+ top: { style: 'thin' },
+ left: { style: 'thin' },
+ bottom: { style: 'thin' },
+ right: { style: 'thin' }
+ };
+ });
+ }
+ });
+
+ // 워크시트에 공종 유형 관련 메모 추가
+ const infoRow = worksheet.addRow(['공종 유형 안내: ' + HULL_WORK_TYPES.join(', ')]);
+ infoRow.font = { bold: true, color: { argb: 'FF0000FF' } };
+ worksheet.mergeCells(`A${infoRow.number}:H${infoRow.number}`);
+
+ // 워크시트 보호 (선택적)
+ worksheet.protect('', {
+ selectLockedCells: true,
+ selectUnlockedCells: true,
+ formatColumns: true,
+ formatRows: true,
+ insertColumns: false,
+ insertRows: true,
+ insertHyperlinks: false,
+ deleteColumns: false,
+ deleteRows: true,
+ sort: true,
+ autoFilter: true,
+ pivotTables: false
+ });
+
+ try {
+ // 워크북을 Blob으로 변환
+ const buffer = await workbook.xlsx.writeBuffer();
+ const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
+ saveAs(blob, 'offshore-hull-item-template.xlsx');
+ return true;
+ } catch (error) {
+ console.error('Excel 템플릿 생성 오류:', error);
+ throw error;
+ }
+}
diff --git a/lib/items-tech/table/hull/offshore-hull-table-columns.tsx b/lib/items-tech/table/hull/offshore-hull-table-columns.tsx
new file mode 100644
index 00000000..f5db40d8
--- /dev/null
+++ b/lib/items-tech/table/hull/offshore-hull-table-columns.tsx
@@ -0,0 +1,282 @@
+"use client"
+
+import * as React from "react"
+import { type DataTableRowAction } from "@/types/table"
+import { type ColumnDef } from "@tanstack/react-table"
+import { Ellipsis } from "lucide-react"
+
+import { formatDate } from "@/lib/utils"
+import { Button } from "@/components/ui/button"
+import { Checkbox } from "@/components/ui/checkbox"
+import {
+ DropdownMenu,
+ DropdownMenuContent,
+ DropdownMenuItem,
+ DropdownMenuSeparator,
+ DropdownMenuShortcut,
+ DropdownMenuTrigger,
+} from "@/components/ui/dropdown-menu"
+import { DataTableColumnHeaderSimple } from "@/components/data-table/data-table-column-simple-header"
+
+// 테이블 표시에 필요한 데이터 타입 정의
+interface OffshoreHullTableItem {
+ id: number;
+ itemId: number;
+ workType: "HA" | "HE" | "HH" | "HM" | "NC";
+ itemList1: string | null;
+ itemList2: string | null;
+ itemList3: string | null;
+ itemList4: string | null;
+ itemCode: string;
+ itemName: string;
+ description: string | null;
+ createdAt: Date;
+ updatedAt: Date;
+}
+
+interface GetColumnsProps {
+ setRowAction: React.Dispatch<React.SetStateAction<DataTableRowAction<OffshoreHullTableItem> | null>>
+}
+
+export function getOffshoreHullColumns({ setRowAction }: GetColumnsProps): ColumnDef<OffshoreHullTableItem>[] {
+ // ----------------------------------------------------------------
+ // 1) select 컬럼 (체크박스)
+ // ----------------------------------------------------------------
+ const selectColumn: ColumnDef<OffshoreHullTableItem> = {
+ id: "select",
+ header: ({ table }) => (
+ <Checkbox
+ checked={
+ table.getIsAllPageRowsSelected() ||
+ (table.getIsSomePageRowsSelected() && "indeterminate")
+ }
+ onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}
+ aria-label="Select all"
+ className="translate-y-0.5"
+ />
+ ),
+ cell: ({ row }) => (
+ <Checkbox
+ checked={row.getIsSelected()}
+ onCheckedChange={(value) => row.toggleSelected(!!value)}
+ aria-label="Select row"
+ className="translate-y-0.5"
+ />
+ ),
+ size: 40,
+ enableSorting: false,
+ enableHiding: false,
+ }
+
+ // ----------------------------------------------------------------
+ // 2) actions 컬럼 (Dropdown 메뉴)
+ // ----------------------------------------------------------------
+ const actionsColumn: ColumnDef<OffshoreHullTableItem> = {
+ id: "actions",
+ cell: ({ row }) => (
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <Button
+ aria-label="Open menu"
+ variant="ghost"
+ className="flex size-8 p-0 data-[state=open]:bg-muted"
+ >
+ <Ellipsis className="size-4" aria-hidden="true" />
+ </Button>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent align="end" className="w-40">
+ <DropdownMenuItem
+ onSelect={() => setRowAction({ row, type: "update" })}
+ >
+ 수정
+ </DropdownMenuItem>
+ <DropdownMenuSeparator />
+ <DropdownMenuItem
+ onSelect={() => setRowAction({ row, type: "delete" })}
+ className="text-destructive"
+ >
+ 삭제
+ <DropdownMenuShortcut>⌘⌫</DropdownMenuShortcut>
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ ),
+ size: 40,
+ enableSorting: false,
+ enableHiding: false,
+ }
+
+ // ----------------------------------------------------------------
+ // 3) 데이터 컬럼들을 그룹별로 구성
+ // ----------------------------------------------------------------
+
+ // 3-1) 기본 정보 그룹 컬럼
+ const basicInfoColumns: ColumnDef<OffshoreHullTableItem>[] = [
+ {
+ accessorKey: "itemCode",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="Material Group" />
+ ),
+ cell: ({ row }) => <div>{row.original.itemCode}</div>,
+ enableSorting: true,
+ enableHiding: true,
+ meta: {
+ excelHeader: "Material Group",
+ group: "기본 정보",
+ },
+ },
+ {
+ accessorKey: "itemName",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="Description" />
+ ),
+ cell: ({ row }) => <div>{row.original.itemName}</div>,
+ enableSorting: true,
+ enableHiding: true,
+ meta: {
+ excelHeader: "Description",
+ group: "기본 정보",
+ },
+ },
+ {
+ accessorKey: "workType",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="기능(공종)" />
+ ),
+ cell: ({ row }) => <div>{row.original.workType}</div>,
+ enableSorting: true,
+ enableHiding: true,
+ meta: {
+ excelHeader: "기능(공종)",
+ group: "기본 정보",
+ },
+ },
+ {
+ accessorKey: "description",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="Size/Dimension" />
+ ),
+ cell: ({ row }) => <div>{row.original.description || "-"}</div>,
+ enableSorting: true,
+ enableHiding: true,
+ meta: {
+ excelHeader: "Size/Dimension",
+ group: "기본 정보",
+ },
+ },
+ ]
+
+ // 3-2) 아이템 리스트 그룹 컬럼
+ const itemListColumns: ColumnDef<OffshoreHullTableItem>[] = [
+ {
+ accessorKey: "itemList1",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="아이템 리스트 1" />
+ ),
+ cell: ({ row }) => <div>{row.original.itemList1 || "-"}</div>,
+ enableSorting: true,
+ enableHiding: true,
+ meta: {
+ excelHeader: "아이템 리스트 1",
+ group: "아이템 리스트",
+ },
+ },
+ {
+ accessorKey: "itemList2",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="아이템 리스트 2" />
+ ),
+ cell: ({ row }) => <div>{row.original.itemList2 || "-"}</div>,
+ enableSorting: true,
+ enableHiding: true,
+ meta: {
+ excelHeader: "아이템 리스트 2",
+ group: "아이템 리스트",
+ },
+ },
+ {
+ accessorKey: "itemList3",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="아이템 리스트 3" />
+ ),
+ cell: ({ row }) => <div>{row.original.itemList3 || "-"}</div>,
+ enableSorting: true,
+ enableHiding: true,
+ meta: {
+ excelHeader: "아이템 리스트 3",
+ group: "아이템 리스트",
+ },
+ },
+ {
+ accessorKey: "itemList4",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="아이템 리스트 4" />
+ ),
+ cell: ({ row }) => <div>{row.original.itemList4 || "-"}</div>,
+ enableSorting: true,
+ enableHiding: true,
+ meta: {
+ excelHeader: "아이템 리스트 4",
+ group: "아이템 리스트",
+ },
+ },
+ ]
+
+ // 3-3) 메타데이터 그룹 컬럼
+ const metadataColumns: ColumnDef<OffshoreHullTableItem>[] = [
+ {
+ accessorKey: "createdAt",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="생성일" />
+ ),
+ cell: ({ row }) => formatDate(row.original.createdAt),
+ enableSorting: true,
+ enableHiding: true,
+ meta: {
+ excelHeader: "생성일",
+ group: "Metadata",
+ },
+ },
+ {
+ accessorKey: "updatedAt",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="수정일" />
+ ),
+ cell: ({ row }) => formatDate(row.original.updatedAt),
+ enableSorting: true,
+ enableHiding: true,
+ meta: {
+ excelHeader: "수정일",
+ group: "Metadata",
+ },
+ }
+ ]
+
+ // 3-4) 그룹별 컬럼 구성
+ const groupedColumns: ColumnDef<OffshoreHullTableItem>[] = [
+ {
+ id: "기본 정보",
+ header: "기본 정보",
+ columns: basicInfoColumns,
+ },
+ {
+ id: "아이템 리스트",
+ header: "아이템 리스트",
+ columns: itemListColumns,
+ },
+ {
+ id: "Metadata",
+ header: "Metadata",
+ columns: metadataColumns,
+ }
+ ]
+
+ // ----------------------------------------------------------------
+ // 4) 최종 컬럼 배열: select, groupedColumns, actions
+ // ----------------------------------------------------------------
+ return [
+ selectColumn,
+ ...groupedColumns,
+ actionsColumn,
+ ]
+} \ No newline at end of file
diff --git a/lib/items-tech/table/hull/offshore-hull-table-toolbar-actions.tsx b/lib/items-tech/table/hull/offshore-hull-table-toolbar-actions.tsx
new file mode 100644
index 00000000..f5324cb1
--- /dev/null
+++ b/lib/items-tech/table/hull/offshore-hull-table-toolbar-actions.tsx
@@ -0,0 +1,184 @@
+"use client"
+
+import * as React from "react"
+import { type Table } from "@tanstack/react-table"
+import { Download, FileDown } from "lucide-react"
+import * as ExcelJS from 'exceljs'
+import { saveAs } from "file-saver"
+
+import { Button } from "@/components/ui/button"
+import {
+ DropdownMenu,
+ DropdownMenuContent,
+ DropdownMenuItem,
+ DropdownMenuTrigger,
+} from "@/components/ui/dropdown-menu"
+
+import { DeleteItemsDialog } from "../delete-items-dialog"
+import { AddItemDialog } from "../add-items-dialog"
+import { exportHullItemTemplate } from "./item-excel-template"
+import { ImportItemButton } from "../import-excel-button"
+
+// 해양 HULL 아이템 타입 정의
+interface OffshoreHullItem {
+ id: number;
+ itemId: number;
+ workType: "HA" | "HE" | "HH" | "HM" | "NC";
+ itemList1: string | null;
+ itemList2: string | null;
+ itemList3: string | null;
+ itemList4: string | null;
+ itemCode: string;
+ itemName: string;
+ description: string | null;
+ createdAt: Date;
+ updatedAt: Date;
+}
+
+interface OffshoreHullTableToolbarActionsProps {
+ table: Table<OffshoreHullItem>
+}
+
+export function OffshoreHullTableToolbarActions({ table }: OffshoreHullTableToolbarActionsProps) {
+ const [refreshKey, setRefreshKey] = React.useState(0)
+
+ // 가져오기 성공 후 테이블 갱신
+ const handleImportSuccess = () => {
+ setRefreshKey(prev => prev + 1)
+ }
+
+ // Excel 내보내기 함수
+ const exportTableToExcel = async (
+ table: Table<OffshoreHullItem>,
+ options: {
+ filename: string;
+ excludeColumns?: string[];
+ sheetName?: string;
+ }
+ ) => {
+ const { filename, excludeColumns = [], sheetName = "해양 HULL 아이템 목록" } = options;
+
+ // 워크북 생성
+ const workbook = new ExcelJS.Workbook();
+ workbook.creator = 'Offshore Item Management System';
+ workbook.created = new Date();
+
+ // 워크시트 생성
+ const worksheet = workbook.addWorksheet(sheetName);
+
+ // 테이블 데이터 가져오기
+ const data = table.getFilteredRowModel().rows.map(row => row.original);
+ console.log("내보내기 데이터:", data);
+
+ // 필요한 헤더 직접 정의 (필터링 문제 해결)
+ const headers = [
+ { key: 'itemCode', header: '아이템 코드' },
+ { key: 'itemName', header: '아이템 명' },
+ { key: 'description', header: '설명' },
+ { key: 'workType', header: '기능(공종)' },
+ { key: 'itemList1', header: '아이템 리스트 1' },
+ { key: 'itemList2', header: '아이템 리스트 2' },
+ { key: 'itemList3', header: '아이템 리스트 3' },
+ { key: 'itemList4', header: '아이템 리스트 4' }
+ ].filter(header => !excludeColumns.includes(header.key));
+
+ console.log("내보내기 헤더:", headers);
+ // 컬럼 정의
+ worksheet.columns = headers.map(header => ({
+ header: header.header,
+ key: header.key,
+ width: 20 // 기본 너비
+ }));
+
+ // 스타일 적용
+ const headerRow = worksheet.getRow(1);
+ headerRow.font = { bold: true };
+ headerRow.fill = {
+ type: 'pattern',
+ pattern: 'solid',
+ fgColor: { argb: 'FFE0E0E0' }
+ };
+ headerRow.alignment = { vertical: 'middle', horizontal: 'center' };
+
+ // 데이터 행 추가
+ data.forEach(item => {
+ const row: Record<string, any> = {};
+ headers.forEach(header => {
+ row[header.key] = item[header.key as keyof OffshoreHullItem];
+ });
+ worksheet.addRow(row);
+ });
+
+ // 전체 셀에 테두리 추가
+ worksheet.eachRow((row) => {
+ row.eachCell((cell) => {
+ cell.border = {
+ top: { style: 'thin' },
+ left: { style: 'thin' },
+ bottom: { style: 'thin' },
+ right: { style: 'thin' }
+ };
+ });
+ });
+
+ try {
+ // 워크북을 Blob으로 변환
+ const buffer = await workbook.xlsx.writeBuffer();
+ const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
+ saveAs(blob, `${filename}.xlsx`);
+ return true;
+ } catch (error) {
+ console.error("Excel 내보내기 오류:", error);
+ return false;
+ }
+ }
+
+ return (
+ <div className="flex items-center gap-2">
+ {/* 선택된 로우가 있으면 삭제 다이얼로그 */}
+ {table.getFilteredSelectedRowModel().rows.length > 0 ? (
+ <DeleteItemsDialog
+ items={table
+ .getFilteredSelectedRowModel()
+ .rows.map((row) => row.original)}
+ onSuccess={() => table.toggleAllRowsSelected(false)}
+ itemType="offshoreHull"
+ />
+ ) : null}
+
+ {/* 새 아이템 추가 다이얼로그 */}
+ <AddItemDialog itemType="offshoreHull" />
+
+ {/* Import 버튼 */}
+ <ImportItemButton itemType="hull" onSuccess={handleImportSuccess} />
+
+ {/* Export 드롭다운 메뉴 */}
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <Button variant="outline" size="sm" className="gap-2">
+ <Download className="size-4" aria-hidden="true" />
+ <span className="hidden sm:inline">Export</span>
+ </Button>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent align="end">
+ <DropdownMenuItem
+ onClick={() =>
+ exportTableToExcel(table, {
+ filename: "offshore_hull_items",
+ excludeColumns: ["select", "actions"],
+ sheetName: "해양 HULL 아이템 목록"
+ })
+ }
+ >
+ <FileDown className="mr-2 h-4 w-4" />
+ <span>현재 데이터 내보내기</span>
+ </DropdownMenuItem>
+ <DropdownMenuItem onClick={() => exportHullItemTemplate()}>
+ <FileDown className="mr-2 h-4 w-4" />
+ <span>템플릿 다운로드</span>
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ </div>
+ )
+} \ No newline at end of file
diff --git a/lib/items-tech/table/hull/offshore-hull-table.tsx b/lib/items-tech/table/hull/offshore-hull-table.tsx
new file mode 100644
index 00000000..8efa9c81
--- /dev/null
+++ b/lib/items-tech/table/hull/offshore-hull-table.tsx
@@ -0,0 +1,152 @@
+"use client"
+
+import * as React from "react"
+import type {
+ DataTableFilterField,
+ DataTableAdvancedFilterField,
+ DataTableRowAction,
+} from "@/types/table"
+
+import { useDataTable } from "@/hooks/use-data-table"
+import { DataTable } from "@/components/data-table/data-table"
+import { DataTableAdvancedToolbar } from "@/components/data-table/data-table-advanced-toolbar"
+import { getOffshoreHullItems } from "../../service"
+import { getOffshoreHullColumns } from "./offshore-hull-table-columns"
+import { OffshoreHullTableToolbarActions } from "./offshore-hull-table-toolbar-actions"
+import { DeleteItemsDialog } from "../delete-items-dialog"
+import { UpdateItemSheet } from "../update-items-sheet"
+
+// 서비스에서 반환하는 데이터 타입 정의
+type OffshoreHullItem = {
+ id: number;
+ itemId: number;
+ workType: "HA" | "HE" | "HH" | "HM" | "NC";
+ itemList1: string | null;
+ itemList2: string | null;
+ itemList3: string | null;
+ itemList4: string | null;
+ itemCode: string;
+ itemName: string;
+ description: string | null;
+ createdAt: Date;
+ updatedAt: Date;
+}
+
+interface OffshoreHullTableProps {
+ promises: Promise<Awaited<ReturnType<typeof getOffshoreHullItems>>>
+}
+
+export function OffshoreHullTable({ promises }: OffshoreHullTableProps) {
+ const { data, pageCount } = React.use(promises)
+
+ // 아이템 타입에 따른 행 액션 상태 관리
+ const [rowAction, setRowAction] = React.useState<DataTableRowAction<OffshoreHullItem> | null>(null)
+ const columns = getOffshoreHullColumns({ setRowAction })
+ const filterFields: DataTableFilterField<OffshoreHullItem>[] = [
+ {
+ id: "itemCode",
+ label: "Item Code",
+ },
+ {
+ id: "itemName",
+ label: "Item Name",
+ },
+ {
+ id: "workType",
+ label: "기능(공종)",
+ },
+ {
+ id: "itemList1",
+ label: "아이템 리스트 1",
+ },
+ ]
+
+ const advancedFilterFields: DataTableAdvancedFilterField<OffshoreHullItem>[] = [
+ {
+ id: "itemCode",
+ label: "Item Code",
+ type: "text",
+ },
+ {
+ id: "itemName",
+ label: "Item Name",
+ type: "text",
+ },
+ {
+ id: "description",
+ label: "Description",
+ type: "text",
+ },
+ {
+ id: "workType",
+ label: "기능(공종)",
+ type: "text",
+ },
+ {
+ id: "itemList1",
+ label: "아이템 리스트 1",
+ type: "text",
+ },
+ {
+ id: "itemList2",
+ label: "아이템 리스트 2",
+ type: "text",
+ },
+ {
+ id: "itemList3",
+ label: "아이템 리스트 3",
+ type: "text",
+ },
+ {
+ id: "itemList4",
+ label: "아이템 리스트 4",
+ type: "text",
+ },
+ ]
+
+ const { table } = useDataTable({
+ data: data as OffshoreHullItem[],
+ columns,
+ pageCount,
+ filterFields,
+ enablePinning: true,
+ enableAdvancedFilter: true,
+ initialState: {
+ sorting: [{ id: "createdAt", desc: true }],
+ columnPinning: { right: ["actions"] },
+ },
+ getRowId: (originalRow) => String(originalRow.id),
+ shallow: false,
+ clearOnDefault: true,
+ })
+
+ return (
+ <>
+ <DataTable table={table}>
+ <DataTableAdvancedToolbar
+ table={table}
+ filterFields={advancedFilterFields}
+ shallow={false}
+ >
+ <OffshoreHullTableToolbarActions table={table} />
+ </DataTableAdvancedToolbar>
+ </DataTable>
+ <DeleteItemsDialog
+ open={rowAction?.type === "delete"}
+ onOpenChange={() => setRowAction(null)}
+ items={rowAction?.row.original ? [rowAction?.row.original] : []}
+ showTrigger={false}
+ onSuccess={() => rowAction?.row.toggleSelected(false)}
+ itemType="offshoreHull"
+ />
+ {rowAction?.type === "update" && rowAction.row.original && (
+ <UpdateItemSheet
+ item={rowAction.row.original}
+ itemType="offshoreHull"
+ open={true}
+ onOpenChange={() => setRowAction(null)}
+ />
+ )}
+ </>
+ )
+} \ No newline at end of file
diff --git a/lib/items-ship/table/import-excel-button.tsx b/lib/items-tech/table/import-excel-button.tsx
index cc6c9b73..8c4a9e16 100644
--- a/lib/items-ship/table/import-excel-button.tsx
+++ b/lib/items-tech/table/import-excel-button.tsx
@@ -13,49 +13,63 @@ import {
DialogFooter,
DialogHeader,
DialogTitle,
+ DialogTrigger,
} from "@/components/ui/dialog"
import { Progress } from "@/components/ui/progress"
-import { processFileImport } from "./import-item-handler"
+import { processFileImport } from "./ship/import-item-handler"
+import { processTopFileImport } from "./top/import-item-handler"
+import { processHullFileImport } from "./hull/import-item-handler"
-const SHIP_TYPES = ['A-MAX', 'S-MAX', 'LNGC', 'VLCC', 'CONT'] as const;
+
+// 선박 아이템 타입
+type ItemType = "ship" | "top" | "hull";
+
+const ITEM_TYPE_NAMES = {
+ ship: "조선 아이템",
+ top: "해양 TOP 아이템",
+ hull: "해양 HULL 아이템",
+};
interface ImportItemButtonProps {
- onSuccess?: () => void
+ itemType: ItemType;
+ onSuccess?: () => void;
}
-export function ImportItemButton({ onSuccess }: ImportItemButtonProps) {
- const [open, setOpen] = React.useState(false)
- const [file, setFile] = React.useState<File | null>(null)
- const [isUploading, setIsUploading] = React.useState(false)
- const [progress, setProgress] = React.useState(0)
- const [error, setError] = React.useState<string | null>(null)
- const fileInputRef = React.useRef<HTMLInputElement>(null)
+export function ImportItemButton({ itemType, onSuccess }: ImportItemButtonProps) {
+ const [open, setOpen] = React.useState(false);
+ const [file, setFile] = React.useState<File | null>(null);
+ const [isUploading, setIsUploading] = React.useState(false);
+ const [progress, setProgress] = React.useState(0);
+ const [error, setError] = React.useState<string | null>(null);
+
+ const fileInputRef = React.useRef<HTMLInputElement>(null);
// 파일 선택 처리
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
- const selectedFile = e.target.files?.[0]
- if (!selectedFile) return
+ const selectedFile = e.target.files?.[0];
+ if (!selectedFile) return;
if (!selectedFile.name.endsWith('.xlsx') && !selectedFile.name.endsWith('.xls')) {
- setError("Excel 파일(.xlsx 또는 .xls)만 가능합니다.")
- return
+ setError("Excel 파일(.xlsx 또는 .xls)만 가능합니다.");
+ return;
}
- setFile(selectedFile)
- setError(null)
- }
+ setFile(selectedFile);
+ setError(null);
+ };
+
// 데이터 가져오기 처리
const handleImport = async () => {
if (!file) {
- setError("가져올 파일을 선택해주세요.")
- return
+ setError("가져올 파일을 선택해주세요.");
+ return;
}
try {
- setIsUploading(true)
- setProgress(0)
- setError(null)
+ setIsUploading(true);
+ setProgress(0);
+ setError(null);
// 파일을 ArrayBuffer로 읽기
const arrayBuffer = await file.arrayBuffer();
@@ -96,12 +110,23 @@ export function ImportItemButton({ onSuccess }: ImportItemButtonProps) {
}
});
- const requiredHeaders = ["아이템 코드", "아이템 명", "기능(공종)", ...SHIP_TYPES];
+ // 필수 헤더 확인 (타입별 구분)
+ let requiredHeaders: string[] = ["아이템 코드", "아이템 명", "기능(공종)"];
+
+ // 해양 TOP 및 HULL의 경우 선종 헤더는 필요 없음
+ if (itemType === "ship") {
+ requiredHeaders = [...requiredHeaders, "A-MAX", "S-MAX", "LNGC", "VLCC", "CONT"];
+ }
+
const alternativeHeaders = {
"아이템 코드": ["itemCode", "item_code"],
"아이템 명": ["itemName", "item_name"],
"기능(공종)": ["workType"],
- "설명": ["description"]
+ "설명": ["description"],
+ "항목1": ["itemList1"],
+ "항목2": ["itemList2"],
+ "항목3": ["itemList3"],
+ "항목4": ["itemList4"]
};
// 헤더 매핑 확인 (대체 이름 포함)
@@ -145,9 +170,17 @@ export function ImportItemButton({ onSuccess }: ImportItemButtonProps) {
setProgress(percentage);
};
- const result = await processFileImport(dataRows, updateProgress);
+ // 선택된 타입에 따라 적절한 프로세스 함수 호출
+ let result;
+ if (itemType === "top") {
+ result = await processTopFileImport(dataRows, updateProgress);
+ } else if (itemType === "hull") {
+ result = await processHullFileImport(dataRows, updateProgress);
+ } else {
+ result = await processFileImport(dataRows, updateProgress);
+ }
- toast.success(`${result.successCount}개의 조선 아이템이 성공적으로 가져와졌습니다.`);
+ toast.success(`${result.successCount}개의 ${ITEM_TYPE_NAMES[itemType]}이(가) 성공적으로 가져와졌습니다.`);
if (result.errorCount > 0) {
toast.warning(`${result.errorCount}개의 항목은 처리할 수 없었습니다.`);
@@ -169,19 +202,21 @@ export function ImportItemButton({ onSuccess }: ImportItemButtonProps) {
}
};
+
+
// 다이얼로그 열기/닫기 핸들러
const handleOpenChange = (newOpen: boolean) => {
if (!newOpen) {
// 닫을 때 상태 초기화
- setFile(null)
- setError(null)
- setProgress(0)
+ setFile(null);
+ setError(null);
+ setProgress(0);
if (fileInputRef.current) {
- fileInputRef.current.value = ""
+ fileInputRef.current.value = "";
}
}
- setOpen(newOpen)
- }
+ setOpen(newOpen);
+ };
return (
<>
@@ -199,9 +234,9 @@ export function ImportItemButton({ onSuccess }: ImportItemButtonProps) {
<Dialog open={open} onOpenChange={handleOpenChange}>
<DialogContent className="sm:max-w-[500px]">
<DialogHeader>
- <DialogTitle>조선 아이템 가져오기</DialogTitle>
+ <DialogTitle>{ITEM_TYPE_NAMES[itemType]} 가져오기</DialogTitle>
<DialogDescription>
- 조선 아이템을 Excel 파일에서 가져옵니다.
+ {ITEM_TYPE_NAMES[itemType]}을 Excel 파일에서 가져옵니다.
<br />
올바른 형식의 Excel 파일(.xlsx)을 업로드하세요.
</DialogDescription>
@@ -259,5 +294,5 @@ export function ImportItemButton({ onSuccess }: ImportItemButtonProps) {
</DialogContent>
</Dialog>
</>
- )
+ );
} \ No newline at end of file
diff --git a/lib/items-ship/table/Items-ship-table.tsx b/lib/items-tech/table/ship/Items-ship-table.tsx
index 6fec9ac0..aee90cfc 100644
--- a/lib/items-ship/table/Items-ship-table.tsx
+++ b/lib/items-tech/table/ship/Items-ship-table.tsx
@@ -10,11 +10,11 @@ import type {
import { useDataTable } from "@/hooks/use-data-table"
import { DataTable } from "@/components/data-table/data-table"
import { DataTableAdvancedToolbar } from "@/components/data-table/data-table-advanced-toolbar"
-import { getShipbuildingItems } from "../service"
+import { getShipbuildingItems } from "../../service"
import { getShipbuildingColumns } from "./items-ship-table-columns"
import { ItemsTableToolbarActions } from "./items-table-toolbar-actions"
-import { DeleteItemsDialog } from "./delete-items-dialog"
-import { UpdateItemSheet } from "./update-items-sheet"
+import { DeleteItemsDialog } from "../delete-items-dialog"
+import { UpdateItemSheet } from "../update-items-sheet"
// 서비스에서 반환하는 데이터 타입 정의
type ShipbuildingItem = {
@@ -98,7 +98,7 @@ export function ItemsShipTable({ promises }: ItemsTableProps) {
]
const { table } = useDataTable({
- data,
+ data: data as ShipbuildingItem[],
columns,
pageCount,
filterFields,
@@ -124,18 +124,23 @@ export function ItemsShipTable({ promises }: ItemsTableProps) {
<ItemsTableToolbarActions table={table} />
</DataTableAdvancedToolbar>
</DataTable>
- {/* <UpdateItemSheet
- open={rowAction?.type === "update"}
- onOpenChange={() => setRowAction(null)}
- item={rowAction?.row.original as ShipbuildingItem}
- /> */}
<DeleteItemsDialog
open={rowAction?.type === "delete"}
onOpenChange={() => setRowAction(null)}
items={rowAction?.row.original ? [rowAction?.row.original] : []}
showTrigger={false}
onSuccess={() => rowAction?.row.toggleSelected(false)}
+ itemType="shipbuilding"
/>
+
+ {rowAction?.type === "update" && rowAction.row.original && (
+ <UpdateItemSheet
+ item={rowAction.row.original}
+ itemType="shipbuilding"
+ open={true}
+ onOpenChange={() => setRowAction(null)}
+ />
+ )}
</>
)
}
diff --git a/lib/items-ship/table/import-item-handler.tsx b/lib/items-tech/table/ship/import-item-handler.tsx
index 07086c94..6ad24398 100644
--- a/lib/items-ship/table/import-item-handler.tsx
+++ b/lib/items-tech/table/ship/import-item-handler.tsx
@@ -1,7 +1,7 @@
"use client"
import { z } from "zod"
-import { createShipbuildingImportItem, createShipbuildingItem } from "../service" // 아이템 생성 서버 액션
+import { createShipbuildingImportItem } from "../../service" // 아이템 생성 서버 액션
const SHIP_TYPES = ['A-MAX', 'S-MAX', 'LNGC', 'VLCC', 'CONT'] as const;
diff --git a/lib/items-ship/table/item-excel-template.tsx b/lib/items-tech/table/ship/item-excel-template.tsx
index 127a1dea..127a1dea 100644
--- a/lib/items-ship/table/item-excel-template.tsx
+++ b/lib/items-tech/table/ship/item-excel-template.tsx
diff --git a/lib/items-ship/table/items-ship-table-columns.tsx b/lib/items-tech/table/ship/items-ship-table-columns.tsx
index 2b46db92..2b46db92 100644
--- a/lib/items-ship/table/items-ship-table-columns.tsx
+++ b/lib/items-tech/table/ship/items-ship-table-columns.tsx
diff --git a/lib/items-ship/table/items-table-toolbar-actions.tsx b/lib/items-tech/table/ship/items-table-toolbar-actions.tsx
index ba3a6cee..6cae61af 100644
--- a/lib/items-ship/table/items-table-toolbar-actions.tsx
+++ b/lib/items-tech/table/ship/items-table-toolbar-actions.tsx
@@ -14,10 +14,10 @@ import {
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
-import { DeleteItemsDialog } from "./delete-items-dialog"
-import { AddItemDialog } from "./add-items-dialog"
+import { DeleteItemsDialog } from "../delete-items-dialog"
+import { AddItemDialog } from "../add-items-dialog"
import { exportItemTemplate } from "./item-excel-template"
-import { ImportItemButton } from "./import-excel-button"
+import { ImportItemButton } from "../import-excel-button"
// 조선 아이템 타입 정의
interface ShipbuildingItem {
@@ -136,14 +136,15 @@ export function ItemsTableToolbarActions({ table }: ItemsTableToolbarActionsProp
.getFilteredSelectedRowModel()
.rows.map((row) => row.original)}
onSuccess={() => table.toggleAllRowsSelected(false)}
+ itemType="shipbuilding"
/>
) : null}
{/* 새 아이템 추가 다이얼로그 */}
- <AddItemDialog />
+ <AddItemDialog itemType="shipbuilding" />
{/* Import 버튼 */}
- <ImportItemButton onSuccess={handleImportSuccess} />
+ <ImportItemButton itemType="ship" onSuccess={handleImportSuccess} />
{/* Export 드롭다운 메뉴 */}
<DropdownMenu>
diff --git a/lib/items-tech/table/top/import-item-handler.tsx b/lib/items-tech/table/top/import-item-handler.tsx
new file mode 100644
index 00000000..de1638a8
--- /dev/null
+++ b/lib/items-tech/table/top/import-item-handler.tsx
@@ -0,0 +1,143 @@
+"use client"
+
+import { z } from "zod"
+import { createOffshoreTopItem } from "../../service"
+
+// 해양 TOP 기능(공종) 유형 enum
+const TOP_WORK_TYPES = ["TM", "TS", "TE", "TP"] as const;
+
+// 아이템 데이터 검증을 위한 Zod 스키마
+const itemSchema = z.object({
+ itemCode: z.string().min(1, "아이템 코드는 필수입니다"),
+ itemName: z.string().min(1, "아이템 명은 필수입니다"),
+ workType: z.enum(TOP_WORK_TYPES, {
+ required_error: "기능(공종)은 필수입니다",
+ }),
+ description: z.string().nullable().optional(),
+ itemList1: z.string().nullable().optional(),
+ itemList2: z.string().nullable().optional(),
+ itemList3: z.string().nullable().optional(),
+ itemList4: z.string().nullable().optional(),
+});
+
+interface ProcessResult {
+ successCount: number;
+ errorCount: number;
+ errors?: Array<{ row: number; message: string }>;
+}
+
+/**
+ * Excel 파일에서 가져온 해양 TOP 아이템 데이터 처리하는 함수
+ */
+export async function processTopFileImport(
+ jsonData: any[],
+ progressCallback?: (current: number, total: number) => void
+): Promise<ProcessResult> {
+ // 결과 카운터 초기화
+ let successCount = 0;
+ let errorCount = 0;
+ const errors: Array<{ row: number; message: string }> = [];
+
+ // 빈 행 등 필터링
+ const dataRows = jsonData.filter(row => {
+ // 빈 행 건너뛰기
+ if (Object.values(row).every(val => !val)) {
+ return false;
+ }
+ return true;
+ });
+
+ // 데이터 행이 없으면 빈 결과 반환
+ if (dataRows.length === 0) {
+ return { successCount: 0, errorCount: 0 };
+ }
+
+ // 각 행에 대해 처리
+ for (let i = 0; i < dataRows.length; i++) {
+ const row = dataRows[i];
+ const rowIndex = i + 1; // 사용자에게 표시할 행 번호는 1부터 시작
+
+ // 진행 상황 콜백 호출
+ if (progressCallback) {
+ progressCallback(i + 1, dataRows.length);
+ }
+
+ try {
+ // 필드 매핑 (한글/영문 필드명 모두 지원)
+ const itemCode = row["아이템 코드"] || row["itemCode"] || "";
+ const itemName = row["아이템 명"] || row["itemName"] || "";
+ const workType = row["기능(공종)"] || row["workType"] || "";
+ const description = row["설명"] || row["description"] || null;
+ const itemList1 = row["항목1"] || row["itemList1"] || null;
+ const itemList2 = row["항목2"] || row["itemList2"] || null;
+ const itemList3 = row["항목3"] || row["itemList3"] || null;
+ const itemList4 = row["항목4"] || row["itemList4"] || null;
+
+ // 데이터 정제
+ const cleanedRow = {
+ itemCode: typeof itemCode === 'string' ? itemCode.trim() : String(itemCode).trim(),
+ itemName: typeof itemName === 'string' ? itemName.trim() : String(itemName).trim(),
+ workType: typeof workType === 'string' ? workType.trim() : String(workType).trim(),
+ description: description ? (typeof description === 'string' ? description : String(description)) : null,
+ itemList1: itemList1 ? (typeof itemList1 === 'string' ? itemList1 : String(itemList1)) : null,
+ itemList2: itemList2 ? (typeof itemList2 === 'string' ? itemList2 : String(itemList2)) : null,
+ itemList3: itemList3 ? (typeof itemList3 === 'string' ? itemList3 : String(itemList3)) : null,
+ itemList4: itemList4 ? (typeof itemList4 === 'string' ? itemList4 : String(itemList4)) : null,
+ };
+
+ // 데이터 유효성 검사
+ const validationResult = itemSchema.safeParse(cleanedRow);
+
+ if (!validationResult.success) {
+ const errorMessage = validationResult.error.errors.map(
+ err => `${err.path.join('.')}: ${err.message}`
+ ).join(', ');
+
+ errors.push({ row: rowIndex, message: errorMessage });
+ errorCount++;
+ continue;
+ }
+
+ // 해양 TOP 아이템 생성
+ const result = await createOffshoreTopItem({
+ itemCode: cleanedRow.itemCode,
+ itemName: cleanedRow.itemName,
+ workType: cleanedRow.workType as "TM" | "TS" | "TE" | "TP",
+ description: cleanedRow.description,
+ itemList1: cleanedRow.itemList1,
+ itemList2: cleanedRow.itemList2,
+ itemList3: cleanedRow.itemList3,
+ itemList4: cleanedRow.itemList4,
+ });
+
+ if (result.success) {
+ successCount++;
+ } else {
+ errors.push({
+ row: rowIndex,
+ message: result.message || result.error || "알 수 없는 오류"
+ });
+ errorCount++;
+ }
+ } catch (error) {
+ console.error(`${rowIndex}행 처리 오류:`, error);
+ errors.push({
+ row: rowIndex,
+ message: error instanceof Error ? error.message : "알 수 없는 오류"
+ });
+ errorCount++;
+ }
+
+ // 비동기 작업 쓰로틀링
+ if (i % 5 === 0) {
+ await new Promise(resolve => setTimeout(resolve, 10));
+ }
+ }
+
+ // 처리 결과 반환
+ return {
+ successCount,
+ errorCount,
+ errors: errors.length > 0 ? errors : undefined
+ };
+}
diff --git a/lib/items-tech/table/top/item-excel-template.tsx b/lib/items-tech/table/top/item-excel-template.tsx
new file mode 100644
index 00000000..4514af59
--- /dev/null
+++ b/lib/items-tech/table/top/item-excel-template.tsx
@@ -0,0 +1,125 @@
+import * as ExcelJS from 'exceljs';
+import { saveAs } from "file-saver";
+
+// 해양 TOP 기능(공종) 유형
+const TOP_WORK_TYPES = ["TM", "TS", "TE", "TP"] as const;
+
+/**
+ * 해양 TOP 아이템 데이터 가져오기를 위한 Excel 템플릿 파일 생성 및 다운로드
+ */
+export async function exportTopItemTemplate() {
+ // 워크북 생성
+ const workbook = new ExcelJS.Workbook();
+ workbook.creator = 'Offshore TOP Item Management System';
+ workbook.created = new Date();
+
+ // 워크시트 생성
+ const worksheet = workbook.addWorksheet('해양 TOP 아이템');
+
+ // 컬럼 헤더 정의 및 스타일 적용
+ worksheet.columns = [
+ { header: '아이템 코드', key: 'itemCode', width: 15 },
+ { header: '아이템 명', key: 'itemName', width: 30 },
+ { header: '기능(공종)', key: 'workType', width: 15 },
+ { header: '설명', key: 'description', width: 50 },
+ { header: '항목1', key: 'itemList1', width: 20 },
+ { header: '항목2', key: 'itemList2', width: 20 },
+ { header: '항목3', key: 'itemList3', width: 20 },
+ { header: '항목4', key: 'itemList4', width: 20 },
+ ];
+
+ // 헤더 스타일 적용
+ const headerRow = worksheet.getRow(1);
+ headerRow.font = { bold: true };
+ headerRow.fill = {
+ type: 'pattern',
+ pattern: 'solid',
+ fgColor: { argb: 'FFE0E0E0' }
+ };
+ headerRow.alignment = { vertical: 'middle', horizontal: 'center' };
+
+ // 테두리 스타일 적용
+ headerRow.eachCell((cell) => {
+ cell.border = {
+ top: { style: 'thin' },
+ left: { style: 'thin' },
+ bottom: { style: 'thin' },
+ right: { style: 'thin' }
+ };
+ });
+
+ // 샘플 데이터 추가
+ const sampleData = [
+ {
+ itemCode: 'TOP001',
+ itemName: 'TOP 샘플 아이템 1',
+ workType: 'TM',
+ description: '이것은 해양 TOP 샘플 아이템 1의 설명입니다.',
+ itemList1: '항목1 샘플 데이터',
+ itemList2: '항목2 샘플 데이터',
+ itemList3: '항목3 샘플 데이터',
+ itemList4: '항목4 샘플 데이터'
+ },
+ {
+ itemCode: 'TOP002',
+ itemName: 'TOP 샘플 아이템 2',
+ workType: 'TS',
+ description: '이것은 해양 TOP 샘플 아이템 2의 설명입니다.',
+ itemList1: '항목1 샘플 데이터',
+ itemList2: '항목2 샘플 데이터',
+ itemList3: '',
+ itemList4: ''
+ }
+ ];
+
+ // 데이터 행 추가
+ sampleData.forEach(item => {
+ worksheet.addRow(item);
+ });
+
+ // 데이터 행 스타일 적용
+ worksheet.eachRow((row, rowNumber) => {
+ if (rowNumber > 1) { // 헤더를 제외한 데이터 행
+ row.eachCell((cell) => {
+ cell.border = {
+ top: { style: 'thin' },
+ left: { style: 'thin' },
+ bottom: { style: 'thin' },
+ right: { style: 'thin' }
+ };
+ });
+ }
+ });
+
+ // 워크시트에 공종 유형 관련 메모 추가
+ const infoRow = worksheet.addRow(['공종 유형 안내: ' + TOP_WORK_TYPES.join(', ')]);
+ infoRow.font = { bold: true, color: { argb: 'FF0000FF' } };
+ worksheet.mergeCells(`A${infoRow.number}:H${infoRow.number}`);
+
+ // 워크시트 보호 (선택적)
+ worksheet.protect('', {
+ selectLockedCells: true,
+ selectUnlockedCells: true,
+ formatColumns: true,
+ formatRows: true,
+ insertColumns: false,
+ insertRows: true,
+ insertHyperlinks: false,
+ deleteColumns: false,
+ deleteRows: true,
+ sort: true,
+ autoFilter: true,
+ pivotTables: false
+ });
+
+ try {
+ // 워크북을 Blob으로 변환
+ const buffer = await workbook.xlsx.writeBuffer();
+ const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
+ saveAs(blob, 'offshore-top-item-template.xlsx');
+ return true;
+ } catch (error) {
+ console.error('Excel 템플릿 생성 오류:', error);
+ throw error;
+ }
+}
diff --git a/lib/items-tech/table/top/offshore-top-table-columns.tsx b/lib/items-tech/table/top/offshore-top-table-columns.tsx
new file mode 100644
index 00000000..4ccb2003
--- /dev/null
+++ b/lib/items-tech/table/top/offshore-top-table-columns.tsx
@@ -0,0 +1,282 @@
+"use client"
+
+import * as React from "react"
+import { type DataTableRowAction } from "@/types/table"
+import { type ColumnDef } from "@tanstack/react-table"
+import { Ellipsis } from "lucide-react"
+
+import { formatDate } from "@/lib/utils"
+import { Button } from "@/components/ui/button"
+import { Checkbox } from "@/components/ui/checkbox"
+import {
+ DropdownMenu,
+ DropdownMenuContent,
+ DropdownMenuItem,
+ DropdownMenuSeparator,
+ DropdownMenuShortcut,
+ DropdownMenuTrigger,
+} from "@/components/ui/dropdown-menu"
+import { DataTableColumnHeaderSimple } from "@/components/data-table/data-table-column-simple-header"
+
+// 테이블 표시에 필요한 데이터 타입 정의
+interface OffshoreTopTableItem {
+ id: number;
+ itemId: number;
+ workType: "TM" | "TS" | "TE" | "TP";
+ itemList1: string | null;
+ itemList2: string | null;
+ itemList3: string | null;
+ itemList4: string | null;
+ itemCode: string;
+ itemName: string;
+ description: string | null;
+ createdAt: Date;
+ updatedAt: Date;
+}
+
+interface GetColumnsProps {
+ setRowAction: React.Dispatch<React.SetStateAction<DataTableRowAction<OffshoreTopTableItem> | null>>
+}
+
+export function getOffshoreTopColumns({ setRowAction }: GetColumnsProps): ColumnDef<OffshoreTopTableItem>[] {
+ // ----------------------------------------------------------------
+ // 1) select 컬럼 (체크박스)
+ // ----------------------------------------------------------------
+ const selectColumn: ColumnDef<OffshoreTopTableItem> = {
+ id: "select",
+ header: ({ table }) => (
+ <Checkbox
+ checked={
+ table.getIsAllPageRowsSelected() ||
+ (table.getIsSomePageRowsSelected() && "indeterminate")
+ }
+ onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}
+ aria-label="Select all"
+ className="translate-y-0.5"
+ />
+ ),
+ cell: ({ row }) => (
+ <Checkbox
+ checked={row.getIsSelected()}
+ onCheckedChange={(value) => row.toggleSelected(!!value)}
+ aria-label="Select row"
+ className="translate-y-0.5"
+ />
+ ),
+ size: 40,
+ enableSorting: false,
+ enableHiding: false,
+ }
+
+ // ----------------------------------------------------------------
+ // 2) actions 컬럼 (Dropdown 메뉴)
+ // ----------------------------------------------------------------
+ const actionsColumn: ColumnDef<OffshoreTopTableItem> = {
+ id: "actions",
+ cell: ({ row }) => (
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <Button
+ aria-label="Open menu"
+ variant="ghost"
+ className="flex size-8 p-0 data-[state=open]:bg-muted"
+ >
+ <Ellipsis className="size-4" aria-hidden="true" />
+ </Button>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent align="end" className="w-40">
+ <DropdownMenuItem
+ onSelect={() => setRowAction({ row, type: "update" })}
+ >
+ 수정
+ </DropdownMenuItem>
+ <DropdownMenuSeparator />
+ <DropdownMenuItem
+ onSelect={() => setRowAction({ row, type: "delete" })}
+ className="text-destructive"
+ >
+ 삭제
+ <DropdownMenuShortcut>⌘⌫</DropdownMenuShortcut>
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ ),
+ size: 40,
+ enableSorting: false,
+ enableHiding: false,
+ }
+
+ // ----------------------------------------------------------------
+ // 3) 데이터 컬럼들을 그룹별로 구성
+ // ----------------------------------------------------------------
+
+ // 3-1) 기본 정보 그룹 컬럼
+ const basicInfoColumns: ColumnDef<OffshoreTopTableItem>[] = [
+ {
+ accessorKey: "itemCode",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="Material Group" />
+ ),
+ cell: ({ row }) => <div>{row.original.itemCode}</div>,
+ enableSorting: true,
+ enableHiding: true,
+ meta: {
+ excelHeader: "Material Group",
+ group: "기본 정보",
+ },
+ },
+ {
+ accessorKey: "itemName",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="Description" />
+ ),
+ cell: ({ row }) => <div>{row.original.itemName}</div>,
+ enableSorting: true,
+ enableHiding: true,
+ meta: {
+ excelHeader: "Description",
+ group: "기본 정보",
+ },
+ },
+ {
+ accessorKey: "workType",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="기능(공종)" />
+ ),
+ cell: ({ row }) => <div>{row.original.workType}</div>,
+ enableSorting: true,
+ enableHiding: true,
+ meta: {
+ excelHeader: "기능(공종)",
+ group: "기본 정보",
+ },
+ },
+ {
+ accessorKey: "description",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="Size/Dimension" />
+ ),
+ cell: ({ row }) => <div>{row.original.description || "-"}</div>,
+ enableSorting: true,
+ enableHiding: true,
+ meta: {
+ excelHeader: "Size/Dimension",
+ group: "기본 정보",
+ },
+ },
+ ]
+
+ // 3-2) 아이템 리스트 그룹 컬럼
+ const itemListColumns: ColumnDef<OffshoreTopTableItem>[] = [
+ {
+ accessorKey: "itemList1",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="아이템 리스트 1" />
+ ),
+ cell: ({ row }) => <div>{row.original.itemList1 || "-"}</div>,
+ enableSorting: true,
+ enableHiding: true,
+ meta: {
+ excelHeader: "아이템 리스트 1",
+ group: "아이템 리스트",
+ },
+ },
+ {
+ accessorKey: "itemList2",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="아이템 리스트 2" />
+ ),
+ cell: ({ row }) => <div>{row.original.itemList2 || "-"}</div>,
+ enableSorting: true,
+ enableHiding: true,
+ meta: {
+ excelHeader: "아이템 리스트 2",
+ group: "아이템 리스트",
+ },
+ },
+ {
+ accessorKey: "itemList3",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="아이템 리스트 3" />
+ ),
+ cell: ({ row }) => <div>{row.original.itemList3 || "-"}</div>,
+ enableSorting: true,
+ enableHiding: true,
+ meta: {
+ excelHeader: "아이템 리스트 3",
+ group: "아이템 리스트",
+ },
+ },
+ {
+ accessorKey: "itemList4",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="아이템 리스트 4" />
+ ),
+ cell: ({ row }) => <div>{row.original.itemList4 || "-"}</div>,
+ enableSorting: true,
+ enableHiding: true,
+ meta: {
+ excelHeader: "아이템 리스트 4",
+ group: "아이템 리스트",
+ },
+ },
+ ]
+
+ // 3-3) 메타데이터 그룹 컬럼
+ const metadataColumns: ColumnDef<OffshoreTopTableItem>[] = [
+ {
+ accessorKey: "createdAt",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="생성일" />
+ ),
+ cell: ({ row }) => formatDate(row.original.createdAt),
+ enableSorting: true,
+ enableHiding: true,
+ meta: {
+ excelHeader: "생성일",
+ group: "Metadata",
+ },
+ },
+ {
+ accessorKey: "updatedAt",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="수정일" />
+ ),
+ cell: ({ row }) => formatDate(row.original.updatedAt),
+ enableSorting: true,
+ enableHiding: true,
+ meta: {
+ excelHeader: "수정일",
+ group: "Metadata",
+ },
+ }
+ ]
+
+ // 3-4) 그룹별 컬럼 구성
+ const groupedColumns: ColumnDef<OffshoreTopTableItem>[] = [
+ {
+ id: "기본 정보",
+ header: "기본 정보",
+ columns: basicInfoColumns,
+ },
+ {
+ id: "아이템 리스트",
+ header: "아이템 리스트",
+ columns: itemListColumns,
+ },
+ {
+ id: "Metadata",
+ header: "Metadata",
+ columns: metadataColumns,
+ }
+ ]
+
+ // ----------------------------------------------------------------
+ // 4) 최종 컬럼 배열: select, groupedColumns, actions
+ // ----------------------------------------------------------------
+ return [
+ selectColumn,
+ ...groupedColumns,
+ actionsColumn,
+ ]
+} \ No newline at end of file
diff --git a/lib/items-tech/table/top/offshore-top-table-toolbar-actions.tsx b/lib/items-tech/table/top/offshore-top-table-toolbar-actions.tsx
new file mode 100644
index 00000000..324312aa
--- /dev/null
+++ b/lib/items-tech/table/top/offshore-top-table-toolbar-actions.tsx
@@ -0,0 +1,184 @@
+"use client"
+
+import * as React from "react"
+import { type Table } from "@tanstack/react-table"
+import { Download, FileDown } from "lucide-react"
+import * as ExcelJS from 'exceljs'
+import { saveAs } from "file-saver"
+
+import { Button } from "@/components/ui/button"
+import {
+ DropdownMenu,
+ DropdownMenuContent,
+ DropdownMenuItem,
+ DropdownMenuTrigger,
+} from "@/components/ui/dropdown-menu"
+
+import { DeleteItemsDialog } from "../delete-items-dialog"
+import { AddItemDialog } from "../add-items-dialog"
+import { exportTopItemTemplate } from "./item-excel-template"
+import { ImportItemButton } from "../import-excel-button"
+
+// 해양 TOP 아이템 타입 정의
+interface OffshoreTopItem {
+ id: number;
+ itemId: number;
+ workType: "TM" | "TS" | "TE" | "TP";
+ itemList1: string | null;
+ itemList2: string | null;
+ itemList3: string | null;
+ itemList4: string | null;
+ itemCode: string;
+ itemName: string;
+ description: string | null;
+ createdAt: Date;
+ updatedAt: Date;
+}
+
+interface OffshoreTopTableToolbarActionsProps {
+ table: Table<OffshoreTopItem>
+}
+
+export function OffshoreTopTableToolbarActions({ table }: OffshoreTopTableToolbarActionsProps) {
+ const [refreshKey, setRefreshKey] = React.useState(0)
+
+ // 가져오기 성공 후 테이블 갱신
+ const handleImportSuccess = () => {
+ setRefreshKey(prev => prev + 1)
+ }
+
+ // Excel 내보내기 함수
+ const exportTableToExcel = async (
+ table: Table<OffshoreTopItem>,
+ options: {
+ filename: string;
+ excludeColumns?: string[];
+ sheetName?: string;
+ }
+ ) => {
+ const { filename, excludeColumns = [], sheetName = "해양 TOP 아이템 목록" } = options;
+
+ // 워크북 생성
+ const workbook = new ExcelJS.Workbook();
+ workbook.creator = 'Offshore Item Management System';
+ workbook.created = new Date();
+
+ // 워크시트 생성
+ const worksheet = workbook.addWorksheet(sheetName);
+
+ // 테이블 데이터 가져오기
+ const data = table.getFilteredRowModel().rows.map(row => row.original);
+ console.log("내보내기 데이터:", data);
+
+ // 필요한 헤더 직접 정의 (필터링 문제 해결)
+ const headers = [
+ { key: 'itemCode', header: '아이템 코드' },
+ { key: 'itemName', header: '아이템 명' },
+ { key: 'description', header: '설명' },
+ { key: 'workType', header: '기능(공종)' },
+ { key: 'itemList1', header: '아이템 리스트 1' },
+ { key: 'itemList2', header: '아이템 리스트 2' },
+ { key: 'itemList3', header: '아이템 리스트 3' },
+ { key: 'itemList4', header: '아이템 리스트 4' }
+ ].filter(header => !excludeColumns.includes(header.key));
+
+ console.log("내보내기 헤더:", headers);
+ // 컬럼 정의
+ worksheet.columns = headers.map(header => ({
+ header: header.header,
+ key: header.key,
+ width: 20 // 기본 너비
+ }));
+
+ // 스타일 적용
+ const headerRow = worksheet.getRow(1);
+ headerRow.font = { bold: true };
+ headerRow.fill = {
+ type: 'pattern',
+ pattern: 'solid',
+ fgColor: { argb: 'FFE0E0E0' }
+ };
+ headerRow.alignment = { vertical: 'middle', horizontal: 'center' };
+
+ // 데이터 행 추가
+ data.forEach(item => {
+ const row: Record<string, any> = {};
+ headers.forEach(header => {
+ row[header.key] = item[header.key as keyof OffshoreTopItem];
+ });
+ worksheet.addRow(row);
+ });
+
+ // 전체 셀에 테두리 추가
+ worksheet.eachRow((row) => {
+ row.eachCell((cell) => {
+ cell.border = {
+ top: { style: 'thin' },
+ left: { style: 'thin' },
+ bottom: { style: 'thin' },
+ right: { style: 'thin' }
+ };
+ });
+ });
+
+ try {
+ // 워크북을 Blob으로 변환
+ const buffer = await workbook.xlsx.writeBuffer();
+ const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
+ saveAs(blob, `${filename}.xlsx`);
+ return true;
+ } catch (error) {
+ console.error("Excel 내보내기 오류:", error);
+ return false;
+ }
+ }
+
+ return (
+ <div className="flex items-center gap-2">
+ {/* 선택된 로우가 있으면 삭제 다이얼로그 */}
+ {table.getFilteredSelectedRowModel().rows.length > 0 ? (
+ <DeleteItemsDialog
+ items={table
+ .getFilteredSelectedRowModel()
+ .rows.map((row) => row.original)}
+ onSuccess={() => table.toggleAllRowsSelected(false)}
+ itemType="offshoreTop"
+ />
+ ) : null}
+
+ {/* 새 아이템 추가 다이얼로그 */}
+ <AddItemDialog itemType="offshoreTop" />
+
+ {/* Import 버튼 */}
+ <ImportItemButton itemType="top" onSuccess={handleImportSuccess} />
+
+ {/* Export 드롭다운 메뉴 */}
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <Button variant="outline" size="sm" className="gap-2">
+ <Download className="size-4" aria-hidden="true" />
+ <span className="hidden sm:inline">Export</span>
+ </Button>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent align="end">
+ <DropdownMenuItem
+ onClick={() =>
+ exportTableToExcel(table, {
+ filename: "offshore_top_items",
+ excludeColumns: ["select", "actions"],
+ sheetName: "해양 TOP 아이템 목록"
+ })
+ }
+ >
+ <FileDown className="mr-2 h-4 w-4" />
+ <span>현재 데이터 내보내기</span>
+ </DropdownMenuItem>
+ <DropdownMenuItem onClick={() => exportTopItemTemplate()}>
+ <FileDown className="mr-2 h-4 w-4" />
+ <span>템플릿 다운로드</span>
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ </div>
+ )
+} \ No newline at end of file
diff --git a/lib/items-tech/table/top/offshore-top-table.tsx b/lib/items-tech/table/top/offshore-top-table.tsx
new file mode 100644
index 00000000..dedf766a
--- /dev/null
+++ b/lib/items-tech/table/top/offshore-top-table.tsx
@@ -0,0 +1,153 @@
+"use client"
+
+import * as React from "react"
+import type {
+ DataTableFilterField,
+ DataTableAdvancedFilterField,
+ DataTableRowAction,
+} from "@/types/table"
+
+import { useDataTable } from "@/hooks/use-data-table"
+import { DataTable } from "@/components/data-table/data-table"
+import { DataTableAdvancedToolbar } from "@/components/data-table/data-table-advanced-toolbar"
+import { getOffshoreTopItems } from "../../service"
+import { getOffshoreTopColumns } from "./offshore-top-table-columns"
+import { OffshoreTopTableToolbarActions } from "./offshore-top-table-toolbar-actions"
+import { DeleteItemsDialog } from "../delete-items-dialog"
+import { UpdateItemSheet } from "../update-items-sheet"
+
+// 서비스에서 반환하는 데이터 타입 정의
+type OffshoreTopItem = {
+ id: number;
+ itemId: number;
+ workType: "TM" | "TS" | "TE" | "TP";
+ itemList1: string | null;
+ itemList2: string | null;
+ itemList3: string | null;
+ itemList4: string | null;
+ itemCode: string;
+ itemName: string;
+ description: string | null;
+ createdAt: Date;
+ updatedAt: Date;
+}
+
+interface OffshoreTopTableProps {
+ promises: Promise<Awaited<ReturnType<typeof getOffshoreTopItems>>>
+}
+
+export function OffshoreTopTable({ promises }: OffshoreTopTableProps) {
+ const { data, pageCount } = React.use(promises)
+
+ // 아이템 타입에 따른 행 액션 상태 관리
+ const [rowAction, setRowAction] = React.useState<DataTableRowAction<OffshoreTopItem> | null>(null)
+ const columns = getOffshoreTopColumns({ setRowAction })
+ const filterFields: DataTableFilterField<OffshoreTopItem>[] = [
+ {
+ id: "itemCode",
+ label: "Item Code",
+ },
+ {
+ id: "itemName",
+ label: "Item Name",
+ },
+ {
+ id: "workType",
+ label: "기능(공종)",
+ },
+ {
+ id: "itemList1",
+ label: "아이템 리스트 1",
+ },
+ ]
+
+ const advancedFilterFields: DataTableAdvancedFilterField<OffshoreTopItem>[] = [
+ {
+ id: "itemCode",
+ label: "Item Code",
+ type: "text",
+ },
+ {
+ id: "itemName",
+ label: "Item Name",
+ type: "text",
+ },
+ {
+ id: "description",
+ label: "Description",
+ type: "text",
+ },
+ {
+ id: "workType",
+ label: "기능(공종)",
+ type: "text",
+ },
+ {
+ id: "itemList1",
+ label: "아이템 리스트 1",
+ type: "text",
+ },
+ {
+ id: "itemList2",
+ label: "아이템 리스트 2",
+ type: "text",
+ },
+ {
+ id: "itemList3",
+ label: "아이템 리스트 3",
+ type: "text",
+ },
+ {
+ id: "itemList4",
+ label: "아이템 리스트 4",
+ type: "text",
+ },
+ ]
+
+ const { table } = useDataTable({
+ data: data as OffshoreTopItem[],
+ columns,
+ pageCount,
+ filterFields,
+ enablePinning: true,
+ enableAdvancedFilter: true,
+ initialState: {
+ sorting: [{ id: "createdAt", desc: true }],
+ columnPinning: { right: ["actions"] },
+ },
+ getRowId: (originalRow) => String(originalRow.id),
+ shallow: false,
+ clearOnDefault: true,
+ })
+
+ return (
+ <>
+ <DataTable table={table}>
+ <DataTableAdvancedToolbar
+ table={table}
+ filterFields={advancedFilterFields}
+ shallow={false}
+ >
+ <OffshoreTopTableToolbarActions table={table} />
+ </DataTableAdvancedToolbar>
+ </DataTable>
+ <DeleteItemsDialog
+ open={rowAction?.type === "delete"}
+ onOpenChange={() => setRowAction(null)}
+ items={rowAction?.row.original ? [rowAction?.row.original] : []}
+ showTrigger={false}
+ onSuccess={() => rowAction?.row.toggleSelected(false)}
+ itemType="offshoreTop"
+ />
+
+ {rowAction?.type === "update" && rowAction.row.original && (
+ <UpdateItemSheet
+ item={rowAction.row.original}
+ itemType="offshoreTop"
+ open={true}
+ onOpenChange={() => setRowAction(null)}
+ />
+ )}
+ </>
+ )
+} \ No newline at end of file
diff --git a/lib/items-tech/table/update-items-sheet.tsx b/lib/items-tech/table/update-items-sheet.tsx
new file mode 100644
index 00000000..2db3b193
--- /dev/null
+++ b/lib/items-tech/table/update-items-sheet.tsx
@@ -0,0 +1,390 @@
+"use client"
+
+import * as React from "react"
+import { useForm } from "react-hook-form"
+
+import { Button } from "@/components/ui/button"
+import {
+ Sheet,
+ SheetClose,
+ SheetContent,
+ SheetDescription,
+ SheetFooter,
+ SheetHeader,
+ SheetTitle,
+} from "@/components/ui/sheet"
+import {
+ Form,
+ FormControl,
+ FormField,
+ FormItem,
+ FormLabel,
+ FormMessage,
+} from "@/components/ui/form"
+import { Input } from "@/components/ui/input"
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from "@/components/ui/select"
+import { Textarea } from "@/components/ui/textarea"
+import { toast } from "sonner"
+
+import {
+ modifyShipbuildingItem,
+ modifyOffshoreTopItem,
+ modifyOffshoreHullItem
+} from "../service"
+import { ItemType } from "./delete-items-dialog"
+
+const shipbuildingWorkTypes = [
+ { value: "기장", label: "기장" },
+ { value: "전장", label: "전장" },
+ { value: "선실", label: "선실" },
+ { value: "배관", label: "배관" },
+ { value: "철의", label: "철의" },
+] as const
+
+const offshoreTopWorkTypes = [
+ { value: "TM", label: "TM" },
+ { value: "TS", label: "TS" },
+ { value: "TE", label: "TE" },
+ { value: "TP", label: "TP" },
+] as const
+
+const offshoreHullWorkTypes = [
+ { value: "HA", label: "HA" },
+ { value: "HE", label: "HE" },
+ { value: "HH", label: "HH" },
+ { value: "HM", label: "HM" },
+ { value: "NC", label: "NC" },
+] as const
+
+interface CommonItemFields {
+ id: number
+ itemId: number
+ itemCode: string
+ itemName: string
+ description: string | null
+ createdAt: Date
+ updatedAt: Date
+}
+
+type ShipbuildingItem = CommonItemFields & {
+ workType: "기장" | "전장" | "선실" | "배관" | "철의"
+ shipTypes: string
+}
+
+type OffshoreTopItem = CommonItemFields & {
+ workType: "TM" | "TS" | "TE" | "TP"
+ itemList1: string | null
+ itemList2: string | null
+ itemList3: string | null
+ itemList4: string | null
+}
+
+type OffshoreHullItem = CommonItemFields & {
+ workType: "HA" | "HE" | "HH" | "HM" | "NC"
+ itemList1: string | null
+ itemList2: string | null
+ itemList3: string | null
+ itemList4: string | null
+}
+
+type UpdateItemSchema = {
+ itemCode?: string
+ itemName?: string
+ description?: string
+ workType?: string
+ shipTypes?: string
+ itemList1?: string
+ itemList2?: string
+ itemList3?: string
+ itemList4?: string
+}
+
+interface UpdateItemSheetProps {
+ item: ShipbuildingItem | OffshoreTopItem | OffshoreHullItem
+ itemType: ItemType
+ open: boolean
+ onOpenChange: (open: boolean) => void
+}
+
+export function UpdateItemSheet({ item, itemType, open, onOpenChange }: UpdateItemSheetProps) {
+ const [isSubmitting, setIsSubmitting] = React.useState(false)
+
+ // 초기값 설정
+ const form = useForm<UpdateItemSchema>({
+ defaultValues: {
+ itemCode: item.itemCode,
+ itemName: item.itemName,
+ description: item.description || "",
+ workType: item.workType,
+ ...getItemTypeSpecificDefaults(item, itemType),
+ },
+ })
+
+ function getItemTypeSpecificDefaults(
+ item: ShipbuildingItem | OffshoreTopItem | OffshoreHullItem,
+ itemType: ItemType
+ ) {
+ switch (itemType) {
+ case 'shipbuilding':
+ return {
+ shipTypes: (item as ShipbuildingItem).shipTypes
+ };
+ case 'offshoreTop':
+ case 'offshoreHull':
+ const offshoreItem = item as OffshoreTopItem | OffshoreHullItem;
+ return {
+ itemList1: offshoreItem.itemList1 || "",
+ itemList2: offshoreItem.itemList2 || "",
+ itemList3: offshoreItem.itemList3 || "",
+ itemList4: offshoreItem.itemList4 || "",
+ };
+ default:
+ return {};
+ }
+ }
+
+ async function onSubmit(data: UpdateItemSchema) {
+ try {
+ setIsSubmitting(true)
+ let result;
+
+ switch (itemType) {
+ case 'shipbuilding':
+ result = await modifyShipbuildingItem({
+ ...data,
+ id: item.id
+ });
+ break;
+ case 'offshoreTop':
+ result = await modifyOffshoreTopItem({
+ ...data,
+ id: item.id
+ });
+ break;
+ case 'offshoreHull':
+ result = await modifyOffshoreHullItem({
+ ...data,
+ id: item.id
+ });
+ break;
+ default:
+ toast.error("지원하지 않는 아이템 타입입니다");
+ return;
+ }
+
+ if (result.success) {
+ toast.success(result.message || "아이템이 수정되었습니다.")
+ onOpenChange(false)
+ } else {
+ toast.error(result.error || "아이템 수정 중 오류가 발생했습니다. 다시 시도해주세요.")
+ }
+ } catch (error) {
+ toast.error("오류가 발생했습니다.")
+ console.error(error)
+ } finally {
+ setIsSubmitting(false)
+ }
+ }
+
+ const getItemTypeLabel = () => {
+ switch (itemType) {
+ case 'shipbuilding':
+ return '조선 아이템';
+ case 'offshoreTop':
+ return '해양 TOP 아이템';
+ case 'offshoreHull':
+ return '해양 HULL 아이템';
+ default:
+ return '아이템';
+ }
+ }
+
+ const getWorkTypeOptions = () => {
+ switch (itemType) {
+ case 'shipbuilding':
+ return shipbuildingWorkTypes;
+ case 'offshoreTop':
+ return offshoreTopWorkTypes;
+ case 'offshoreHull':
+ return offshoreHullWorkTypes;
+ default:
+ return [];
+ }
+ }
+
+ return (
+ <Sheet open={open} onOpenChange={onOpenChange}>
+ <SheetContent>
+ <SheetHeader>
+ <SheetTitle>{getItemTypeLabel()} 수정</SheetTitle>
+ <SheetDescription>
+ {getItemTypeLabel()} 정보를 수정합니다. 수정할 필드를 입력해주세요.
+ </SheetDescription>
+ </SheetHeader>
+ <Form {...form}>
+ <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
+ <FormField
+ control={form.control}
+ name="itemCode"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Material Group</FormLabel>
+ <FormControl>
+ <Input placeholder="Material Group을 입력하세요" {...field} />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="itemName"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Description</FormLabel>
+ <FormControl>
+ <Input placeholder="Description을 입력하세요" {...field} />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="workType"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>기능(공종)</FormLabel>
+ <Select
+ onValueChange={field.onChange}
+ defaultValue={field.value}
+ >
+ <FormControl>
+ <SelectTrigger>
+ <SelectValue placeholder="기능(공종)을 선택하세요" />
+ </SelectTrigger>
+ </FormControl>
+ <SelectContent>
+ {getWorkTypeOptions().map((type) => (
+ <SelectItem key={type.value} value={type.value}>
+ {type.label}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+
+ {/* 조선 아이템 전용 필드 */}
+ {itemType === 'shipbuilding' && (
+ <FormField
+ control={form.control}
+ name="shipTypes"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>선종</FormLabel>
+ <FormControl>
+ <Input placeholder="선종을 입력하세요" {...field} />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ )}
+
+ {/* 해양 TOP 또는 HULL 아이템 전용 필드 */}
+ {(itemType === 'offshoreTop' || itemType === 'offshoreHull') && (
+ <>
+ <FormField
+ control={form.control}
+ name="itemList1"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Item List 1</FormLabel>
+ <FormControl>
+ <Input placeholder="Item List 1을 입력하세요" {...field} />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="itemList2"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Item List 2</FormLabel>
+ <FormControl>
+ <Input placeholder="Item List 2를 입력하세요" {...field} />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="itemList3"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Item List 3</FormLabel>
+ <FormControl>
+ <Input placeholder="Item List 3을 입력하세요" {...field} />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="itemList4"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Item List 4</FormLabel>
+ <FormControl>
+ <Input placeholder="Item List 4를 입력하세요" {...field} />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ </>
+ )}
+
+ <FormField
+ control={form.control}
+ name="description"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Size/Dimension</FormLabel>
+ <FormControl>
+ <Textarea
+ placeholder="Size/Dimension을 입력하세요"
+ {...field}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <SheetFooter>
+ <SheetClose asChild>
+ <Button variant="outline">취소</Button>
+ </SheetClose>
+ <Button type="submit" disabled={isSubmitting}>
+ {isSubmitting ? "수정 중..." : "수정"}
+ </Button>
+ </SheetFooter>
+ </form>
+ </Form>
+ </SheetContent>
+ </Sheet>
+ )
+} \ No newline at end of file
diff --git a/lib/items-tech/validations.ts b/lib/items-tech/validations.ts
new file mode 100644
index 00000000..7c8a58f9
--- /dev/null
+++ b/lib/items-tech/validations.ts
@@ -0,0 +1,156 @@
+import {
+ createSearchParamsCache,
+ parseAsArrayOf,
+ parseAsInteger,
+ parseAsString,
+ parseAsStringEnum,
+} from "nuqs/server"
+import * as z from "zod"
+
+import { getFiltersStateParser, getSortingStateParser } from "@/lib/parsers"
+import { Item } from "@/db/schema/items";
+
+export const searchParamsCache = createSearchParamsCache({
+ flags: parseAsArrayOf(z.enum(["advancedTable", "floatingBar"])).withDefault(
+ []
+ ),
+ page: parseAsInteger.withDefault(1),
+ perPage: parseAsInteger.withDefault(10),
+ sort: getSortingStateParser<Item>().withDefault([
+ { id: "createdAt", desc: true },
+ ]),
+ itemCode: parseAsString.withDefault(""),
+ itemName: parseAsString.withDefault(""),
+ description: parseAsString.withDefault(""),
+
+ // advanced filter
+ filters: getFiltersStateParser().withDefault([]),
+ joinOperator: parseAsStringEnum(["and", "or"]).withDefault("and"),
+ search: parseAsString.withDefault(""),
+})
+
+export const createItemSchema = z.object({
+ itemCode: z.string(),
+ itemName: z.string(),
+ description: z.string(),
+})
+
+export const updateItemSchema = z.object({
+ itemCode: z.string().optional(),
+ itemName: z.string().optional(),
+ description: z.string().nullish(),
+})
+
+// 조선 아이템 업데이트 스키마
+export const updateShipbuildingItemSchema = z.object({
+ itemCode: z.string(),
+ itemName: z.string().optional(),
+ description: z.string().optional(),
+ workType: z.string().optional(),
+ shipTypes: z.string().optional(),
+})
+
+export type GetItemsSchema = Awaited<ReturnType<typeof searchParamsCache.parse>>
+export type CreateItemSchema = z.infer<typeof createItemSchema>
+export type UpdateItemSchema = z.infer<typeof updateItemSchema>
+export type UpdateShipbuildingItemSchema = z.infer<typeof updateShipbuildingItemSchema>
+
+// 조선 아이템 스키마
+export const createShipbuildingItemSchema = z.object({
+ itemCode: z.string(),
+ itemName: z.string(),
+ description: z.string(),
+ workType: z.string(),
+ shipTypes: z.string(),
+})
+
+export type CreateShipbuildingItemSchema = z.infer<typeof createShipbuildingItemSchema>
+
+// 기본 아이템 생성 데이터 타입
+export interface ItemCreateData {
+ itemCode: string
+ itemName: string
+ description: string | null
+}
+
+// 조선 아이템 생성 데이터 타입
+export interface ShipbuildingItemCreateData extends ItemCreateData {
+ workType: string | null
+ shipTypes: string | null
+}
+
+// 아이템 타입에 따른 생성 데이터 타입
+export type TypedItemCreateData = ShipbuildingItemCreateData
+
+// 해양 TOP 아이템 스키마
+export const createOffshoreTopItemSchema = z.object({
+ itemCode: z.string(),
+ itemName: z.string(),
+ description: z.string(),
+ workType: z.enum(["TM", "TS", "TE", "TP"]),
+ itemList1: z.string().optional(),
+ itemList2: z.string().optional(),
+ itemList3: z.string().optional(),
+ itemList4: z.string().optional(),
+})
+
+// 해양 HULL 아이템 스키마
+export const createOffshoreHullItemSchema = z.object({
+ itemCode: z.string(),
+ itemName: z.string(),
+ description: z.string(),
+ workType: z.enum(["HA", "HE", "HH", "HM", "NC"]),
+ itemList1: z.string().optional(),
+ itemList2: z.string().optional(),
+ itemList3: z.string().optional(),
+ itemList4: z.string().optional(),
+})
+
+export type CreateOffshoreTopItemSchema = z.infer<typeof createOffshoreTopItemSchema>
+export type CreateOffshoreHullItemSchema = z.infer<typeof createOffshoreHullItemSchema>
+
+// 해양 TOP 아이템 업데이트 스키마
+export const updateOffshoreTopItemSchema = z.object({
+ itemCode: z.string(),
+ itemName: z.string().optional(),
+ description: z.string().optional(),
+ workType: z.enum(["TM", "TS", "TE", "TP"]).optional(),
+ itemList1: z.string().optional(),
+ itemList2: z.string().optional(),
+ itemList3: z.string().optional(),
+ itemList4: z.string().optional(),
+})
+
+// 해양 HULL 아이템 업데이트 스키마
+export const updateOffshoreHullItemSchema = z.object({
+ itemCode: z.string(),
+ itemName: z.string().optional(),
+ description: z.string().optional(),
+ workType: z.enum(["HA", "HE", "HH", "HM", "NC"]).optional(),
+ itemList1: z.string().optional(),
+ itemList2: z.string().optional(),
+ itemList3: z.string().optional(),
+ itemList4: z.string().optional(),
+})
+
+export type UpdateOffshoreTopItemSchema = z.infer<typeof updateOffshoreTopItemSchema>
+export type UpdateOffshoreHullItemSchema = z.infer<typeof updateOffshoreHullItemSchema>
+
+// 해양 TOP 아이템 생성 데이터 타입
+export interface OffshoreTopItemCreateData extends ItemCreateData {
+ workType: "TM" | "TS" | "TE" | "TP"
+ itemList1?: string | null
+ itemList2?: string | null
+ itemList3?: string | null
+ itemList4?: string | null
+}
+
+// 해양 HULL 아이템 생성 데이터 타입
+export interface OffshoreHullItemCreateData extends ItemCreateData {
+ workType: "HA" | "HE" | "HH" | "HM" | "NC"
+ itemList1?: string | null
+ itemList2?: string | null
+ itemList3?: string | null
+ itemList4?: string | null
+}
+