/*M!999999\- enable the sandbox mode */
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
DROP TABLE IF EXISTS `api_keys`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `api_keys` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `key` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `description` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `is_active` tinyint(1) NOT NULL DEFAULT '1',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `api_keys_key_unique` (`key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `board_comments`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `board_comments` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '댓글 고유번호',
  `post_id` bigint unsigned NOT NULL COMMENT '게시글 고유번호',
  `tenant_id` bigint unsigned NOT NULL COMMENT '테넌트 고유번호',
  `user_id` bigint unsigned DEFAULT NULL COMMENT '작성자 고유번호',
  `parent_id` bigint unsigned DEFAULT NULL COMMENT '상위 댓글ID(대댓글)',
  `content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '댓글 내용',
  `ip_address` varchar(45) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '작성자 IP',
  `status` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'active' COMMENT '상태',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL COMMENT '삭제일시(Soft Delete)',
  PRIMARY KEY (`id`),
  KEY `board_comments_post_id_foreign` (`post_id`),
  KEY `board_comments_tenant_id_foreign` (`tenant_id`),
  KEY `board_comments_user_id_foreign` (`user_id`),
  KEY `board_comments_parent_id_foreign` (`parent_id`),
  CONSTRAINT `board_comments_parent_id_foreign` FOREIGN KEY (`parent_id`) REFERENCES `board_comments` (`id`),
  CONSTRAINT `board_comments_post_id_foreign` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`) ON DELETE CASCADE,
  CONSTRAINT `board_comments_tenant_id_foreign` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`id`),
  CONSTRAINT `board_comments_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `board_files`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `board_files` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '첨부파일 고유번호',
  `post_id` bigint unsigned NOT NULL COMMENT '게시글 고유번호',
  `file_path` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '저장경로',
  `file_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '원본 파일명',
  `file_size` int NOT NULL COMMENT '파일 크기(Byte)',
  `file_type` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '파일 MIME 타입',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `board_files_post_id_foreign` (`post_id`),
  CONSTRAINT `board_files_post_id_foreign` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `board_settings`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `board_settings` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '커스텀필드 고유번호',
  `board_id` bigint unsigned NOT NULL COMMENT '게시판 고유번호',
  `name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '필드명',
  `field_key` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '필드키',
  `field_type` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '필드유형',
  `field_meta` json DEFAULT NULL COMMENT '필드메타',
  `is_required` tinyint(1) NOT NULL DEFAULT '0' COMMENT '필수여부',
  `sort_order` int NOT NULL DEFAULT '0' COMMENT '정렬순서',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `board_settings_board_id_field_key_unique` (`board_id`,`field_key`),
  CONSTRAINT `board_settings_board_id_foreign` FOREIGN KEY (`board_id`) REFERENCES `boards` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `boards`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `boards` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '게시판 고유번호',
  `tenant_id` bigint unsigned NOT NULL COMMENT '테넌트 고유번호',
  `board_code` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '게시판 코드',
  `name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '게시판 이름',
  `description` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '게시판 설명',
  `editor_type` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'wysiwyg' COMMENT '에디터 유형',
  `allow_files` tinyint(1) NOT NULL DEFAULT '1' COMMENT '파일첨부 허용 여부',
  `max_file_count` int NOT NULL DEFAULT '5' COMMENT '최대 첨부파일 수',
  `max_file_size` int NOT NULL DEFAULT '20480' COMMENT '최대 첨부파일 크기(KB)',
  `extra_settings` json DEFAULT NULL COMMENT '추가 설정(JSON)',
  `is_active` tinyint(1) NOT NULL DEFAULT '1' COMMENT '활성화 여부',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `boards_tenant_id_board_code_unique` (`tenant_id`,`board_code`),
  CONSTRAINT `boards_tenant_id_foreign` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `bom_items`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `bom_items` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned NOT NULL COMMENT '멀티테넌시',
  `bom_id` bigint unsigned NOT NULL COMMENT 'BOM ID',
  `parent_id` bigint unsigned DEFAULT NULL COMMENT '상위 BOM Item',
  `item_type` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'item 종류: part, product, bom',
  `ref_id` bigint unsigned NOT NULL COMMENT '참조 ID',
  `quantity` decimal(18,4) NOT NULL DEFAULT '1.0000',
  `attributes` json DEFAULT NULL COMMENT '동적 속성',
  `sort_order` int NOT NULL DEFAULT '0',
  `created_by` bigint unsigned NOT NULL COMMENT '생성자(회원PK)',
  `updated_by` bigint unsigned DEFAULT NULL COMMENT '수정자(회원PK)',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `bom_items_bom_id_foreign` (`bom_id`),
  KEY `bom_items_parent_id_foreign` (`parent_id`),
  CONSTRAINT `bom_items_bom_id_foreign` FOREIGN KEY (`bom_id`) REFERENCES `boms` (`id`),
  CONSTRAINT `bom_items_parent_id_foreign` FOREIGN KEY (`parent_id`) REFERENCES `bom_items` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `boms`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `boms` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned NOT NULL COMMENT '멀티테넌시',
  `product_id` bigint unsigned NOT NULL COMMENT '제품ID',
  `code` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'BOM코드',
  `name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'BOM명',
  `category_id` bigint unsigned NOT NULL COMMENT '카테고리ID(common_codes)',
  `attributes` json DEFAULT NULL COMMENT '동적 속성',
  `description` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '설명',
  `is_default` tinyint(1) NOT NULL DEFAULT '0' COMMENT '기본BOM여부',
  `is_active` tinyint(1) NOT NULL DEFAULT '1' COMMENT '사용여부',
  `image_file_id` bigint unsigned DEFAULT NULL COMMENT '첨부파일ID',
  `created_by` bigint unsigned NOT NULL COMMENT '생성자(회원PK)',
  `updated_by` bigint unsigned DEFAULT NULL COMMENT '수정자(회원PK)',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `boms_tenant_id_product_id_code_unique` (`tenant_id`,`product_id`,`code`),
  KEY `boms_product_id_foreign` (`product_id`),
  KEY `boms_category_id_foreign` (`category_id`),
  KEY `boms_image_file_id_foreign` (`image_file_id`),
  CONSTRAINT `boms_category_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `common_codes` (`id`),
  CONSTRAINT `boms_image_file_id_foreign` FOREIGN KEY (`image_file_id`) REFERENCES `files` (`id`),
  CONSTRAINT `boms_product_id_foreign` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `cache`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `cache` (
  `key` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `value` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `expiration` int NOT NULL,
  PRIMARY KEY (`key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `cache_locks`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `cache_locks` (
  `key` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `owner` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `expiration` int NOT NULL,
  PRIMARY KEY (`key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `categories`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `categories` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '카테고리 PK',
  `tenant_id` bigint unsigned NOT NULL COMMENT '테넌트(고객사) ID',
  `parent_id` bigint unsigned DEFAULT NULL COMMENT '상위 카테고리ID(NULL=최상위)',
  `code` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '카테고리 코드(영문, 고유)',
  `name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '카테고리명',
  `is_active` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'Y' COMMENT '사용여부(Y/N)',
  `sort_order` int NOT NULL DEFAULT '1' COMMENT '정렬순서',
  `description` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '비고',
  `created_by` bigint unsigned DEFAULT NULL COMMENT '등록자(관리자ID)',
  `updated_by` bigint unsigned DEFAULT NULL COMMENT '수정자(관리자ID)',
  `deleted_by` bigint unsigned DEFAULT NULL COMMENT '삭제자(관리자ID)',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_tenant_code` (`tenant_id`,`code`),
  KEY `idx_tenant` (`tenant_id`),
  KEY `idx_parent_id` (`parent_id`),
  CONSTRAINT `fk_category_parent` FOREIGN KEY (`parent_id`) REFERENCES `categories` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='제품 카테고리(계층형, 멀티테넌시)';
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `category_fields`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `category_fields` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '항목 PK',
  `tenant_id` bigint unsigned NOT NULL COMMENT '테넌트(고객사) ID',
  `category_id` bigint unsigned NOT NULL COMMENT '카테고리ID',
  `field_key` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '항목키(영문, 고유)',
  `field_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '항목명',
  `field_type` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '입력유형(text, number, select 등)',
  `is_required` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'N' COMMENT '필수여부(Y/N)',
  `sort_order` int NOT NULL DEFAULT '1' COMMENT '정렬순서',
  `default_value` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '기본값',
  `options` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '선택지(콤보박스, JSON)',
  `description` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '비고',
  `created_by` bigint unsigned DEFAULT NULL COMMENT '등록자(관리자ID)',
  `updated_by` bigint unsigned DEFAULT NULL COMMENT '수정자(관리자ID)',
  `deleted_by` bigint unsigned DEFAULT NULL COMMENT '삭제자(관리자ID)',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_tenant` (`tenant_id`),
  KEY `idx_category_id` (`category_id`),
  CONSTRAINT `fk_category_fields_category` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='카테고리별 항목(필드) 정의(멀티테넌시)';
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `category_logs`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `category_logs` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `category_id` bigint unsigned NOT NULL COMMENT '카테고리ID',
  `tenant_id` bigint unsigned NOT NULL,
  `action` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '동작(등록,수정,삭제: insert, update, delete)',
  `changed_by` bigint unsigned DEFAULT NULL COMMENT '작업자ID',
  `changed_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '작업시각',
  `before_json` json DEFAULT NULL COMMENT '변경 전 데이터(스냅샷)',
  `after_json` json DEFAULT NULL COMMENT '변경 후 데이터(스냅샷)',
  `remarks` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '비고',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='카테고리 변경 이력 로그';
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `category_templates`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `category_templates` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '템플릿 PK',
  `tenant_id` bigint unsigned NOT NULL COMMENT '테넌트(고객사) ID',
  `category_id` bigint unsigned NOT NULL COMMENT '카테고리ID',
  `version_no` int NOT NULL COMMENT '버전번호',
  `template_json` json NOT NULL COMMENT '항목구조 스냅샷',
  `applied_at` timestamp NOT NULL COMMENT '적용일시',
  `created_by` bigint unsigned DEFAULT NULL COMMENT '등록자(관리자ID)',
  `updated_by` bigint unsigned DEFAULT NULL COMMENT '수정자(관리자ID)',
  `deleted_by` bigint unsigned DEFAULT NULL COMMENT '삭제자(관리자ID)',
  `remarks` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '비고',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_tenant_category_version` (`tenant_id`,`category_id`,`version_no`),
  KEY `idx_tenant` (`tenant_id`),
  KEY `idx_category_id` (`category_id`),
  CONSTRAINT `fk_category_templates_category` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='카테고리 템플릿 버전관리(멀티테넌시)';
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `clients`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `clients` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'PK',
  `tenant_id` bigint unsigned NOT NULL COMMENT '테넌트ID',
  `client_code` varchar(50) NOT NULL COMMENT '거래처코드',
  `name` varchar(100) NOT NULL COMMENT '거래처명',
  `contact_person` varchar(50) DEFAULT NULL COMMENT '담당자',
  `phone` varchar(30) DEFAULT NULL COMMENT '연락처',
  `email` varchar(80) DEFAULT NULL COMMENT '이메일',
  `address` varchar(255) DEFAULT NULL COMMENT '주소',
  `is_active` char(1) DEFAULT 'Y' COMMENT '사용여부',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_client_code` (`client_code`),
  KEY `idx_tenant_id` (`tenant_id`),
  CONSTRAINT `fk_clients_tenant` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='거래처/발주처 마스터';
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `common_codes`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `common_codes` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned DEFAULT NULL COMMENT '멀티테넌시',
  `code_group` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'category' COMMENT '코드 그룹',
  `code` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '코드값',
  `name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '이름',
  `parent_id` bigint unsigned DEFAULT NULL COMMENT '상위코드ID(트리)',
  `attributes` json DEFAULT NULL COMMENT '동적 속성',
  `description` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '설명',
  `is_active` tinyint NOT NULL DEFAULT '1' COMMENT '사용여부',
  `sort_order` int NOT NULL DEFAULT '0' COMMENT '정렬순서',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `common_codes_tenant_id_code_group_code_unique` (`tenant_id`,`code_group`,`code`),
  KEY `common_codes_parent_id_foreign` (`parent_id`),
  CONSTRAINT `common_codes_parent_id_foreign` FOREIGN KEY (`parent_id`) REFERENCES `common_codes` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='공통코드 트리';
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `failed_jobs`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `failed_jobs` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `uuid` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `connection` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `queue` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `payload` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `exception` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `failed_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `failed_jobs_uuid_unique` (`uuid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `files`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `files` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned NOT NULL COMMENT '멀티테넌시',
  `file_path` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '저장경로',
  `original_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '원본 파일명',
  `file_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '저장 파일명 (난수)',
  `file_name_old` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `file_size` int DEFAULT NULL,
  `mime_type` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `description` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `fileable_id` bigint unsigned NOT NULL COMMENT 'Polymorphic - 연결된 모델의 PK',
  `fileable_type` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'Polymorphic - 연결된 모델 클래스명',
  `uploaded_by` bigint unsigned DEFAULT NULL COMMENT '업로더 사용자 ID',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `job_batches`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `job_batches` (
  `id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `total_jobs` int NOT NULL,
  `pending_jobs` int NOT NULL,
  `failed_jobs` int NOT NULL,
  `failed_job_ids` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `options` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
  `cancelled_at` int DEFAULT NULL,
  `created_at` int NOT NULL,
  `finished_at` int DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `jobs`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `jobs` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `queue` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `payload` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `attempts` tinyint unsigned NOT NULL,
  `reserved_at` int unsigned DEFAULT NULL,
  `available_at` int unsigned NOT NULL,
  `created_at` int unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `jobs_queue_index` (`queue`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `log_session`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `log_session` (
  `id` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `ip_address` varchar(45) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0' COMMENT 'IP 주소',
  `user_agent` varchar(120) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'User Agent',
  `last_activity` int NOT NULL DEFAULT '0' COMMENT '마지막 활동 시간',
  `timestamp` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '타임스탬프',
  `data` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '세션 데이터',
  PRIMARY KEY (`id`),
  KEY `last_activity_idx` (`last_activity`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='세션정보 저장 테이블';
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `lot_sales`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `lot_sales` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '로트별 판매기록 PK',
  `tenant_id` bigint unsigned NOT NULL COMMENT '테넌트ID',
  `lot_id` bigint unsigned NOT NULL COMMENT '로트ID',
  `sale_date` date NOT NULL COMMENT '판매일',
  `author` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '등록자',
  `workplace_name` varchar(60) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '작업장명',
  `remarks` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '비고',
  `created_by` bigint unsigned NOT NULL COMMENT '생성자(회원PK)',
  `updated_by` bigint unsigned DEFAULT NULL COMMENT '수정자(회원PK)',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_sales_lot_id` (`lot_id`),
  CONSTRAINT `fk_sales_lot_id` FOREIGN KEY (`lot_id`) REFERENCES `lots` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `lots`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `lots` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '로트관리 PK',
  `tenant_id` bigint unsigned NOT NULL COMMENT '테넌트ID',
  `lot_number` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '로트번호',
  `material_id` bigint unsigned NOT NULL COMMENT '자재ID',
  `specification` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '규격',
  `length` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '길이',
  `quantity` int NOT NULL COMMENT '수량',
  `raw_lot_number` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '원자재 로트번호',
  `fabric_lot` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '원단 로트',
  `author` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '작성자',
  `remarks` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '비고',
  `created_by` bigint unsigned NOT NULL COMMENT '생성자(회원PK)',
  `updated_by` bigint unsigned DEFAULT NULL COMMENT '수정자(회원PK)',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `lots_lot_number_unique` (`lot_number`),
  KEY `idx_lots_material_id` (`material_id`),
  CONSTRAINT `fk_lots_material_id` FOREIGN KEY (`material_id`) REFERENCES `materials` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `main_request_flows`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `main_request_flows` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `main_request_id` bigint unsigned NOT NULL COMMENT '메인 업무 ID',
  `flowable_type` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '업무타입(estimate,order,purchase,work 등)',
  `flowable_id` bigint unsigned NOT NULL COMMENT '해당 업무 PK',
  `status_code` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '상태코드',
  `action` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '액션(등록/수정/승인 등)',
  `content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '변경내역/메모',
  `actor_id` bigint unsigned DEFAULT NULL COMMENT '처리자(회원PK)',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_mrf_main_request` (`main_request_id`),
  CONSTRAINT `fk_mrf_main_request` FOREIGN KEY (`main_request_id`) REFERENCES `main_requests` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='업무 전체/상세 흐름 이력(폴리모픽)';
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `main_requests`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `main_requests` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned NOT NULL COMMENT '테넌트ID',
  `status_code` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '업무진행상태(견적/수주/발주/작업 등)',
  `description` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '요약/비고',
  `created_by` bigint unsigned NOT NULL,
  `updated_by` bigint unsigned DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='업무 마스터(관계/흐름 중심)';
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `material_inspection_items`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `material_inspection_items` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '검사항목 PK',
  `inspection_id` bigint unsigned NOT NULL COMMENT '수입검사ID',
  `item_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '검사항목명',
  `is_checked` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'N' COMMENT '체크여부(Y/N)',
  `created_by` bigint unsigned NOT NULL COMMENT '생성자(회원PK)',
  `updated_by` bigint unsigned DEFAULT NULL COMMENT '수정자(회원PK)',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_items_inspection_id` (`inspection_id`),
  CONSTRAINT `fk_items_inspection_id` FOREIGN KEY (`inspection_id`) REFERENCES `material_inspections` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `material_inspections`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `material_inspections` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '수입검사 PK',
  `receipt_id` bigint unsigned NOT NULL COMMENT '자재입고ID',
  `tenant_id` bigint unsigned NOT NULL COMMENT '테넌트ID',
  `inspection_date` date NOT NULL COMMENT '검사일자',
  `inspector_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '검사자',
  `approver_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '결재자',
  `judgment_code` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '종합판정(common_codes)',
  `status_code` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '상태코드(common_codes)',
  `result_file_path` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '성적서 PDF 경로',
  `remarks` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '비고',
  `created_by` bigint unsigned NOT NULL COMMENT '생성자(회원PK)',
  `updated_by` bigint unsigned DEFAULT NULL COMMENT '수정자(회원PK)',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_inspections_receipt_id` (`receipt_id`),
  CONSTRAINT `fk_inspections_receipt_id` FOREIGN KEY (`receipt_id`) REFERENCES `material_receipts` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `material_receipts`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `material_receipts` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '자재입고 PK',
  `material_id` bigint unsigned NOT NULL COMMENT '자재ID',
  `tenant_id` bigint unsigned NOT NULL COMMENT '테넌트ID',
  `receipt_date` date NOT NULL COMMENT '입고일',
  `lot_number` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '로트번호',
  `received_qty` decimal(12,2) NOT NULL COMMENT '입고수량',
  `unit` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '단위',
  `supplier_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '납품업체',
  `manufacturer_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '제조사',
  `purchase_price_excl_vat` decimal(12,2) DEFAULT NULL COMMENT '구매단가(부가세 제외)',
  `weight_kg` decimal(12,2) DEFAULT NULL COMMENT '총 무게(kg)',
  `status_code` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '상태코드(common_codes)',
  `is_inspection` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'N' COMMENT '검사대상 여부(Y/N)',
  `inspection_date` date DEFAULT NULL COMMENT '검사일자',
  `remarks` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '비고',
  `created_by` bigint unsigned NOT NULL COMMENT '생성자(회원PK)',
  `updated_by` bigint unsigned DEFAULT NULL COMMENT '수정자(회원PK)',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_receipts_material_id` (`material_id`),
  CONSTRAINT `fk_receipts_material_id` FOREIGN KEY (`material_id`) REFERENCES `materials` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `materials`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `materials` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '자재 PK',
  `tenant_id` bigint unsigned NOT NULL COMMENT '테넌트ID',
  `name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '자재명(품명)',
  `specification` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '규격',
  `material_code` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '자재코드',
  `unit` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '단위',
  `is_inspection` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'N' COMMENT '검사대상 여부(Y/N)',
  `search_tag` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '검색 태그',
  `remarks` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '비고',
  `created_by` bigint unsigned NOT NULL COMMENT '생성자(회원PK)',
  `updated_by` bigint unsigned DEFAULT NULL COMMENT '수정자(회원PK)',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `materials_material_code_unique` (`material_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `menus`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `menus` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'PK: 메뉴 ID',
  `tenant_id` bigint unsigned DEFAULT NULL COMMENT 'FK: 테넌트 ID(null=공용메뉴)',
  `parent_id` bigint unsigned DEFAULT NULL COMMENT '상위 메뉴 ID',
  `name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '메뉴명',
  `url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '메뉴 URL',
  `is_active` tinyint(1) NOT NULL DEFAULT '1' COMMENT '활성여부(1=활성,0=비활성)',
  `sort_order` int NOT NULL DEFAULT '0' COMMENT '정렬순서',
  `hidden` tinyint(1) NOT NULL DEFAULT '0' COMMENT '숨김여부',
  `is_external` tinyint(1) NOT NULL DEFAULT '0' COMMENT '외부링크여부',
  `external_url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '외부링크 URL',
  `icon` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '아이콘명',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `menus_tenant_id_index` (`tenant_id`),
  KEY `menus_parent_id_index` (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `migrations`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `migrations` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `migration` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `batch` int NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `model_has_permissions`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `model_has_permissions` (
  `permission_id` bigint unsigned NOT NULL,
  `model_type` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `model_id` bigint unsigned NOT NULL,
  PRIMARY KEY (`permission_id`,`model_id`,`model_type`),
  KEY `model_has_permissions_model_id_model_type_index` (`model_id`,`model_type`),
  CONSTRAINT `model_has_permissions_permission_id_foreign` FOREIGN KEY (`permission_id`) REFERENCES `permissions` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `model_has_roles`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `model_has_roles` (
  `role_id` bigint unsigned NOT NULL,
  `model_type` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `model_id` bigint unsigned NOT NULL,
  PRIMARY KEY (`role_id`,`model_id`,`model_type`),
  KEY `model_has_roles_model_id_model_type_index` (`model_id`,`model_type`),
  CONSTRAINT `model_has_roles_role_id_foreign` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `order_histories`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `order_histories` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'PK',
  `tenant_id` bigint unsigned NOT NULL COMMENT '테넌트ID',
  `order_id` bigint unsigned NOT NULL COMMENT 'orders.id',
  `history_type` varchar(20) NOT NULL COMMENT '이력구분(수정/견적/발주/메모)',
  `content` text NOT NULL COMMENT '내용',
  `created_by` bigint unsigned DEFAULT NULL COMMENT '작성자',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_tenant_id` (`tenant_id`),
  KEY `idx_order_id` (`order_id`),
  KEY `fk_order_histories_created_by` (`created_by`),
  CONSTRAINT `fk_order_histories_created_by` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`),
  CONSTRAINT `fk_order_histories_order` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_order_histories_tenant` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='주문 이력/메모';
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `order_item_components`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `order_item_components` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'PK',
  `tenant_id` bigint unsigned NOT NULL COMMENT '테넌트ID',
  `order_item_id` bigint unsigned NOT NULL COMMENT 'order_items.id',
  `component_type` varchar(20) NOT NULL COMMENT '타입(material/product/part/bom)',
  `component_id` bigint unsigned NOT NULL COMMENT '타입별 PK',
  `quantity` decimal(12,2) NOT NULL DEFAULT '1.00' COMMENT '필요수량',
  `unit` varchar(10) DEFAULT NULL,
  `price` decimal(12,2) DEFAULT NULL,
  `remarks` varchar(100) DEFAULT NULL,
  `created_by` bigint unsigned NOT NULL COMMENT '생성자',
  `updated_by` bigint unsigned DEFAULT NULL COMMENT '수정자',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_tenant_id` (`tenant_id`),
  KEY `idx_order_item_id` (`order_item_id`),
  KEY `idx_component` (`component_type`,`component_id`),
  CONSTRAINT `fk_oic_order_item` FOREIGN KEY (`order_item_id`) REFERENCES `order_items` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_oic_tenant` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='주문항목 투입구성(자재/제품/부품 등)';
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `order_items`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `order_items` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'PK',
  `tenant_id` bigint unsigned NOT NULL COMMENT '테넌트ID',
  `order_id` bigint unsigned NOT NULL COMMENT 'orders.id',
  `serial_no` int NOT NULL COMMENT '일련번호',
  `product_id` bigint unsigned DEFAULT NULL COMMENT '제품ID',
  `quantity` int NOT NULL DEFAULT '1' COMMENT '수량',
  `status_code` varchar(20) DEFAULT NULL COMMENT '상세상태 코드',
  `design_code` varchar(100) DEFAULT NULL COMMENT '설계도면/부호',
  `remarks` text COMMENT '비고',
  `attributes` json DEFAULT NULL COMMENT '확장필드',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_tenant_id` (`tenant_id`),
  KEY `idx_order_id` (`order_id`),
  KEY `idx_product_id` (`product_id`),
  CONSTRAINT `fk_order_items_order` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_order_items_product` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`),
  CONSTRAINT `fk_order_items_tenant` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='주문 상세(제품/오픈사이즈 등)';
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `order_versions`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `order_versions` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned NOT NULL,
  `order_id` bigint unsigned NOT NULL,
  `version_no` int NOT NULL,
  `version_type` varchar(20) NOT NULL,
  `status_code` varchar(20) NOT NULL,
  `changed_fields` json DEFAULT NULL COMMENT '변경된 필드만 저장(전/후값)',
  `changed_by` bigint unsigned DEFAULT NULL,
  `changed_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `change_note` varchar(255) DEFAULT NULL COMMENT '변경사유/메모',
  PRIMARY KEY (`id`),
  KEY `idx_order_id` (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='주문상태/중요필드 버전 히스토리';
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `orders`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `orders` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'PK',
  `tenant_id` bigint unsigned NOT NULL COMMENT '테넌트ID',
  `order_no` varchar(30) NOT NULL COMMENT '주문/견적번호',
  `order_type_code` varchar(20) NOT NULL COMMENT '업무구분(견적/수주/발주 등)',
  `status_code` varchar(20) NOT NULL COMMENT '진행상태 코드',
  `category_code` varchar(20) DEFAULT NULL COMMENT '대분류(스크린/철재 등)',
  `product_id` bigint unsigned DEFAULT NULL COMMENT '대표 제품ID',
  `received_at` datetime DEFAULT NULL COMMENT '접수일',
  `writer_id` bigint unsigned DEFAULT NULL COMMENT '작성자',
  `client_id` bigint unsigned DEFAULT NULL COMMENT '거래처ID',
  `client_contact` varchar(50) DEFAULT NULL COMMENT '담당자 연락처',
  `site_name` varchar(100) DEFAULT NULL COMMENT '현장명',
  `quantity` int DEFAULT NULL COMMENT '전체 수량',
  `delivery_date` date DEFAULT NULL COMMENT '출고일',
  `delivery_method_code` varchar(20) DEFAULT NULL COMMENT '운송방법',
  `memo` text COMMENT '비고',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_tenant_id` (`tenant_id`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_product_id` (`product_id`),
  KEY `fk_orders_writer` (`writer_id`),
  CONSTRAINT `fk_orders_client` FOREIGN KEY (`client_id`) REFERENCES `clients` (`id`),
  CONSTRAINT `fk_orders_product` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`),
  CONSTRAINT `fk_orders_tenant` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`id`),
  CONSTRAINT `fk_orders_writer` FOREIGN KEY (`writer_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='수주/견적/발주 메인';
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `parts`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `parts` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned NOT NULL COMMENT '멀티테넌시',
  `code` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '부품코드',
  `name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '부품명',
  `category_id` bigint unsigned NOT NULL COMMENT '카테고리ID(common_codes)',
  `part_type_id` bigint unsigned DEFAULT NULL COMMENT '부품타입ID(common_codes)',
  `unit` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '단위',
  `attributes` json DEFAULT NULL COMMENT '동적 속성',
  `description` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '설명',
  `is_active` tinyint NOT NULL DEFAULT '1' COMMENT '사용여부',
  `created_by` bigint unsigned NOT NULL COMMENT '생성자(회원PK)',
  `updated_by` bigint unsigned DEFAULT NULL COMMENT '수정자(회원PK)',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `parts_tenant_id_code_unique` (`tenant_id`,`code`),
  KEY `parts_category_id_foreign` (`category_id`),
  KEY `parts_part_type_id_foreign` (`part_type_id`),
  CONSTRAINT `parts_category_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `common_codes` (`id`),
  CONSTRAINT `parts_part_type_id_foreign` FOREIGN KEY (`part_type_id`) REFERENCES `common_codes` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `password_reset_tokens`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `password_reset_tokens` (
  `email` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `token` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `payments`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `payments` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'PK',
  `subscription_id` bigint unsigned NOT NULL,
  `amount` decimal(12,2) NOT NULL COMMENT '결제금액',
  `payment_method` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '결제수단',
  `transaction_id` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'PG 거래ID',
  `paid_at` datetime NOT NULL COMMENT '결제일시',
  `status` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'paid' COMMENT '결제상태(paid, failed 등)',
  `memo` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '비고',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `payments_subscription_id_foreign` (`subscription_id`),
  CONSTRAINT `payments_subscription_id_foreign` FOREIGN KEY (`subscription_id`) REFERENCES `subscriptions` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `permissions`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `permissions` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned DEFAULT NULL COMMENT '테넌트(고객사) ID',
  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `guard_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `permissions_name_guard_name_unique` (`name`,`guard_name`),
  KEY `permissions_tenant_id_index` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `personal_access_tokens`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `personal_access_tokens` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tokenable_type` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `tokenable_id` bigint unsigned NOT NULL,
  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `token` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `abilities` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
  `last_used_at` timestamp NULL DEFAULT NULL,
  `expires_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `personal_access_tokens_token_unique` (`token`),
  KEY `personal_access_tokens_tokenable_type_tokenable_id_index` (`tokenable_type`,`tokenable_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `plans`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `plans` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'PK',
  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '플랜명',
  `code` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '플랜 코드',
  `description` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '설명',
  `price` decimal(12,2) NOT NULL COMMENT '월 요금',
  `billing_cycle` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'monthly' COMMENT '청구 주기',
  `features` json DEFAULT NULL COMMENT '제공 기능/제한사항',
  `is_active` tinyint(1) NOT NULL DEFAULT '1' COMMENT '사용여부',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `plans_code_unique` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `post_custom_field_values`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `post_custom_field_values` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '필드값 고유번호',
  `post_id` bigint unsigned NOT NULL COMMENT '게시글 고유번호',
  `field_id` bigint unsigned NOT NULL COMMENT '커스텀필드 고유번호',
  `value` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '필드값',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `post_custom_field_values_post_id_field_id_unique` (`post_id`,`field_id`),
  KEY `post_custom_field_values_field_id_foreign` (`field_id`),
  CONSTRAINT `post_custom_field_values_field_id_foreign` FOREIGN KEY (`field_id`) REFERENCES `board_settings` (`id`),
  CONSTRAINT `post_custom_field_values_post_id_foreign` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `posts`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `posts` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '게시글 고유번호',
  `tenant_id` bigint unsigned NOT NULL COMMENT '테넌트 고유번호',
  `board_id` bigint unsigned NOT NULL COMMENT '게시판 고유번호',
  `user_id` bigint unsigned DEFAULT NULL COMMENT '작성자 고유번호',
  `title` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '제목',
  `content` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '내용',
  `editor_type` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'wysiwyg' COMMENT '에디터 유형',
  `ip_address` varchar(45) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '작성자 IP',
  `is_notice` tinyint(1) NOT NULL DEFAULT '0' COMMENT '공지글 여부',
  `is_secret` tinyint(1) NOT NULL DEFAULT '0' COMMENT '비밀글 여부',
  `views` int NOT NULL DEFAULT '0' COMMENT '조회수',
  `status` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'active' COMMENT '상태',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL COMMENT '삭제일시(Soft Delete)',
  PRIMARY KEY (`id`),
  KEY `posts_tenant_id_board_id_index` (`tenant_id`,`board_id`),
  KEY `posts_board_id_foreign` (`board_id`),
  KEY `posts_status_index` (`status`),
  CONSTRAINT `posts_board_id_foreign` FOREIGN KEY (`board_id`) REFERENCES `boards` (`id`),
  CONSTRAINT `posts_tenant_id_foreign` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `price_histories`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `price_histories` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned NOT NULL COMMENT '멀티테넌시',
  `item_type_code` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '대상구분(product/part/bom 등, code_group=price_item_type)',
  `item_id` bigint unsigned NOT NULL COMMENT '대상ID',
  `price_type_code` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '단가구분(default/20%up/10%discount 등, code_group=price_type)',
  `price` decimal(18,4) NOT NULL COMMENT '단가',
  `started_at` date NOT NULL,
  `ended_at` date DEFAULT NULL,
  `created_by` bigint unsigned NOT NULL COMMENT '생성자(회원PK)',
  `updated_by` bigint unsigned DEFAULT NULL COMMENT '수정자(회원PK)',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_price_histories_main` (`tenant_id`,`item_type_code`,`item_id`,`started_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `products`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `products` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned NOT NULL COMMENT '멀티테넌시',
  `code` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '제품코드',
  `name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '제품명',
  `category_id` bigint unsigned NOT NULL COMMENT '카테고리ID(common_codes)',
  `attributes` json DEFAULT NULL COMMENT '동적 속성',
  `description` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '설명',
  `is_active` tinyint NOT NULL DEFAULT '1' COMMENT '사용여부',
  `created_by` bigint unsigned NOT NULL COMMENT '생성자(회원PK)',
  `updated_by` bigint unsigned DEFAULT NULL COMMENT '수정자(회원PK)',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `products_tenant_id_code_unique` (`tenant_id`,`code`),
  KEY `products_category_id_foreign` (`category_id`),
  CONSTRAINT `products_category_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `common_codes` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `role_has_permissions`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `role_has_permissions` (
  `permission_id` bigint unsigned NOT NULL,
  `role_id` bigint unsigned NOT NULL,
  PRIMARY KEY (`permission_id`,`role_id`),
  KEY `role_has_permissions_role_id_foreign` (`role_id`),
  CONSTRAINT `role_has_permissions_permission_id_foreign` FOREIGN KEY (`permission_id`) REFERENCES `permissions` (`id`) ON DELETE CASCADE,
  CONSTRAINT `role_has_permissions_role_id_foreign` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `role_menu_permissions`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `role_menu_permissions` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'PK: 역할-메뉴 권한 ID',
  `role_id` bigint unsigned NOT NULL COMMENT 'FK: 역할 ID',
  `menu_id` bigint unsigned NOT NULL COMMENT 'FK: 메뉴 ID',
  `access` tinyint(1) NOT NULL DEFAULT '0' COMMENT '메뉴 접근 권한',
  `read` tinyint(1) NOT NULL DEFAULT '0' COMMENT '조회 권한',
  `write` tinyint(1) NOT NULL DEFAULT '0' COMMENT '등록/수정/삭제 권한',
  `export` tinyint(1) NOT NULL DEFAULT '0' COMMENT '다운로드 권한',
  `approve` tinyint(1) NOT NULL DEFAULT '0' COMMENT '승인/반려 권한',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `role_menu_permissions_role_id_menu_id_unique` (`role_id`,`menu_id`),
  KEY `role_menu_permissions_menu_id_foreign` (`menu_id`),
  CONSTRAINT `role_menu_permissions_menu_id_foreign` FOREIGN KEY (`menu_id`) REFERENCES `menus` (`id`) ON DELETE CASCADE,
  CONSTRAINT `role_menu_permissions_role_id_foreign` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `roles`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `roles` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'PK: 역할 ID',
  `tenant_id` bigint unsigned NOT NULL,
  `name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '역할명',
  `description` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '설명',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL COMMENT '삭제일시(소프트삭제)',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_roles_tenant_name` (`tenant_id`,`name`),
  KEY `roles_tenant_id_index` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `sessions`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `sessions` (
  `id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `user_id` bigint unsigned DEFAULT NULL,
  `ip_address` varchar(45) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `user_agent` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
  `payload` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `last_activity` int NOT NULL,
  PRIMARY KEY (`id`),
  KEY `sessions_user_id_index` (`user_id`),
  KEY `sessions_last_activity_index` (`last_activity`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `setting_field_defs`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `setting_field_defs` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `field_key` varchar(64) NOT NULL COMMENT '예: department, position, job_title',
  `label` varchar(100) NOT NULL COMMENT '표시 라벨',
  `data_type` varchar(20) NOT NULL COMMENT 'string|number|date|file|bigint 등',
  `input_type` varchar(30) NOT NULL COMMENT 'text|select|textarea|date|file|select_user 등',
  `option_source` varchar(20) DEFAULT NULL COMMENT 'static|tenant_list|table_ref',
  `option_payload` json DEFAULT NULL COMMENT 'option_source에 따른 설정(JSON)',
  `comment` text,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `storage_area` varchar(50) NOT NULL DEFAULT 'tenant_profile' COMMENT 'users: users 컬럼, tenant_profile: tenant_user_profiles 컬럼, tenant_profile_json: json_extra에 key로 저장',
  `storage_key` varchar(64) DEFAULT NULL COMMENT '저장할 컬럼명(users/tenant_user_profiles) 또는 json key(tenant_profile_json)',
  `is_core` tinyint(1) NOT NULL DEFAULT '0' COMMENT '1=코어 필드(이름/이메일 등), 테넌트가 비활성 불가',
  PRIMARY KEY (`id`),
  UNIQUE KEY `field_key` (`field_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='전역 필드 사전';
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `subscriptions`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `subscriptions` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'PK',
  `tenant_id` bigint unsigned NOT NULL,
  `plan_id` bigint unsigned NOT NULL,
  `started_at` date NOT NULL COMMENT '구독 시작일',
  `ended_at` date DEFAULT NULL COMMENT '구독 종료일(해지시)',
  `status` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'active' COMMENT '상태(active, canceled, expired 등)',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `subscriptions_tenant_id_foreign` (`tenant_id`),
  KEY `subscriptions_plan_id_foreign` (`plan_id`),
  CONSTRAINT `subscriptions_plan_id_foreign` FOREIGN KEY (`plan_id`) REFERENCES `plans` (`id`),
  CONSTRAINT `subscriptions_tenant_id_foreign` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `taggables`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `taggables` (
  `tag_id` bigint unsigned NOT NULL COMMENT '태그 ID (tags.id FK)',
  `taggable_id` bigint unsigned NOT NULL COMMENT '태그가 연결된 엔티티의 PK',
  `taggable_type` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '엔티티 타입(모델명)',
  `created_at` timestamp NULL DEFAULT NULL COMMENT '연결일시',
  PRIMARY KEY (`tag_id`,`taggable_id`,`taggable_type`),
  CONSTRAINT `taggables_ibfk_1` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='태그-대상(제품, 부품, 자재, BOM 등) 다형성 연결';
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `tags`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `tags` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'PK',
  `tenant_id` bigint unsigned NOT NULL COMMENT '테넌트 ID',
  `name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '태그명',
  `created_at` timestamp NULL DEFAULT NULL COMMENT '생성일시',
  `updated_at` timestamp NULL DEFAULT NULL COMMENT '수정일시',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_tenant_tag_name` (`tenant_id`,`name`),
  CONSTRAINT `tags_ibfk_1` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='테넌트별 태그 목록';
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `tenant_field_settings`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `tenant_field_settings` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned NOT NULL,
  `field_key` varchar(64) NOT NULL,
  `enabled` tinyint(1) NOT NULL DEFAULT '1' COMMENT '노출 여부',
  `required` tinyint(1) NOT NULL DEFAULT '0' COMMENT '필수 여부',
  `sort_order` int NOT NULL DEFAULT '0' COMMENT '정렬',
  `option_group_id` bigint unsigned DEFAULT NULL COMMENT 'tenant_option_groups.id (option_source=tenant_list일 경우)',
  `code_group` varchar(50) DEFAULT NULL COMMENT 'common_codes 참조 그룹',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_tenant_field` (`tenant_id`,`field_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='테넌트별 회원필드 구동 설정';
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `tenant_option_groups`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `tenant_option_groups` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned NOT NULL,
  `group_key` varchar(64) NOT NULL COMMENT '예: position, job_title, work_location 등',
  `name` varchar(100) NOT NULL COMMENT '테넌트 화면 표기용 이름',
  `description` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_tenant_group` (`tenant_id`,`group_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='테넌트 관리형 옵션 그룹';
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `tenant_option_values`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `tenant_option_values` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `group_id` bigint unsigned NOT NULL,
  `value_key` varchar(64) NOT NULL COMMENT '내부키 (영문/코드)',
  `value_label` varchar(100) NOT NULL COMMENT '화면 라벨',
  `sort_order` int NOT NULL DEFAULT '0',
  `is_active` tinyint(1) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_group_value` (`group_id`,`value_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='옵션 그룹의 항목 값';
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `tenant_user_profiles`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `tenant_user_profiles` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned NOT NULL COMMENT '테넌트',
  `user_id` bigint unsigned NOT NULL COMMENT 'users.id',
  `department_id` bigint unsigned DEFAULT NULL COMMENT '부서(조직 트리 또는 common_codes)',
  `position_key` varchar(64) DEFAULT NULL COMMENT '직급 key (tenant_option_values.value_key or common_codes.code)',
  `job_title_key` varchar(64) DEFAULT NULL COMMENT '직책 key',
  `work_location_key` varchar(64) DEFAULT NULL COMMENT '근무지 key',
  `employment_type_key` varchar(64) DEFAULT NULL COMMENT '고용형태 key',
  `manager_user_id` bigint unsigned DEFAULT NULL COMMENT '직속상급자 users.id',
  `json_extra` json DEFAULT NULL COMMENT '은행/계좌/주소/개인옵션 등 유연필드',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `profile_photo_path` varchar(2048) DEFAULT NULL COMMENT '테넌트 전용 아바타(없으면 전역 사용)',
  `display_name` varchar(255) DEFAULT NULL COMMENT '테넌트 전용 표기명(없으면 users.name)',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_tenant_user` (`tenant_id`,`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='테넌트별 회원 프로필';
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `tenants`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `tenants` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'PK',
  `company_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '회사/조직명',
  `code` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '테넌트 코드',
  `email` varchar(80) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '대표 이메일',
  `phone` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '대표 전화번호',
  `address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '주소',
  `business_num` varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '사업자등록번호(10자리)',
  `corp_reg_no` varchar(13) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '법인등록번호(13자리, 법인만)',
  `ceo_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '대표자명',
  `homepage` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '홈페이지 주소',
  `fax` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '팩스번호',
  `logo` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '회사 로고 이미지 경로',
  `admin_memo` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '관리자 메모/비고',
  `options` json DEFAULT NULL COMMENT '회사별 옵션 정보(확장용 JSON)',
  `tenant_st_code` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'trial' COMMENT '테넌트 상태(trial,active,suspended,cancelled)',
  `plan_id` bigint unsigned DEFAULT NULL COMMENT '현재 요금제(플랜) ID',
  `subscription_id` bigint unsigned DEFAULT NULL COMMENT '현재 구독 정보 ID',
  `max_users` int NOT NULL DEFAULT '10' COMMENT '최대 사용자 수',
  `trial_ends_at` datetime DEFAULT NULL COMMENT '트라이얼 종료일',
  `expires_at` datetime DEFAULT NULL COMMENT '계약 만료일',
  `last_paid_at` datetime DEFAULT NULL COMMENT '마지막 결제일',
  `billing_tp_code` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'monthly' COMMENT '결제 주기(monthly,yearly)',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `tenants_code_unique` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `user_menu_permissions`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `user_menu_permissions` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'PK: 사용자-메뉴 권한 ID',
  `user_id` bigint unsigned NOT NULL COMMENT 'FK: 사용자 ID',
  `menu_id` bigint unsigned NOT NULL COMMENT 'FK: 메뉴 ID',
  `access` tinyint(1) NOT NULL DEFAULT '0' COMMENT '메뉴 접근 권한',
  `read` tinyint(1) NOT NULL DEFAULT '0' COMMENT '조회 권한',
  `write` tinyint(1) NOT NULL DEFAULT '0' COMMENT '등록/수정/삭제 권한',
  `export` tinyint(1) NOT NULL DEFAULT '0' COMMENT '다운로드 권한',
  `approve` tinyint(1) NOT NULL DEFAULT '0' COMMENT '승인/반려 권한',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_menu_permissions_user_id_menu_id_unique` (`user_id`,`menu_id`),
  KEY `user_menu_permissions_menu_id_foreign` (`menu_id`),
  CONSTRAINT `user_menu_permissions_menu_id_foreign` FOREIGN KEY (`menu_id`) REFERENCES `menus` (`id`) ON DELETE CASCADE,
  CONSTRAINT `user_menu_permissions_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `user_roles`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `user_roles` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'PK: 회원별 역할 매핑 ID',
  `user_id` bigint unsigned NOT NULL COMMENT 'FK: 회원ID',
  `tenant_id` bigint unsigned NOT NULL COMMENT 'FK: 테넌트ID',
  `role_id` bigint unsigned NOT NULL COMMENT 'FK: 역할ID',
  `assigned_at` timestamp NULL DEFAULT NULL COMMENT '역할 할당일',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL COMMENT '삭제일시(소프트삭제)',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_user_tenant_role` (`user_id`,`tenant_id`,`role_id`),
  KEY `user_roles_tenant_id_foreign` (`tenant_id`),
  KEY `user_roles_role_id_foreign` (`role_id`),
  CONSTRAINT `user_roles_role_id_foreign` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`),
  CONSTRAINT `user_roles_tenant_id_foreign` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`id`),
  CONSTRAINT `user_roles_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `user_tenants`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `user_tenants` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'PK: 회원-테넌트 소속 ID',
  `user_id` bigint unsigned NOT NULL COMMENT 'FK: 회원ID',
  `tenant_id` bigint unsigned NOT NULL COMMENT 'FK: 테넌트ID',
  `is_active` tinyint(1) NOT NULL DEFAULT '1' COMMENT '활성화 여부',
  `joined_at` timestamp NULL DEFAULT NULL COMMENT '소속일시',
  `left_at` timestamp NULL DEFAULT NULL COMMENT '탈퇴일시',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL COMMENT '삭제일시(소프트삭제)',
  `is_default` tinyint(1) NOT NULL DEFAULT '0' COMMENT '기본 설정 여부',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_user_tenant` (`user_id`,`tenant_id`),
  KEY `user_tenants_tenant_id_foreign` (`tenant_id`),
  CONSTRAINT `user_tenants_tenant_id_foreign` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`id`),
  CONSTRAINT `user_tenants_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `users`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `users` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `user_id` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '회원 아이디',
  `phone` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '회원 전화번호',
  `options` json DEFAULT NULL COMMENT '회사별 옵션 정보(계좌, 사번 등)',
  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '회원 이름',
  `email` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '이메일',
  `email_verified_at` timestamp NULL DEFAULT NULL,
  `password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '비밀번호',
  `last_login_at` timestamp NULL DEFAULT NULL COMMENT '마지막 로그인 일시',
  `two_factor_secret` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
  `two_factor_recovery_codes` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
  `two_factor_confirmed_at` timestamp NULL DEFAULT NULL,
  `remember_token` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '자동로그인 토큰',
  `current_team_id` bigint unsigned DEFAULT NULL,
  `profile_photo_path` varchar(2048) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '프로필 사진 경로',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL COMMENT '삭제일시(소프트삭제)',
  PRIMARY KEY (`id`),
  UNIQUE KEY `users_user_id_unique` (`user_id`),
  UNIQUE KEY `uk_users_tenant_email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
