-- ============================================
-- 포장 단위 관리 테이블 (P2-05, P2-06)
-- ============================================

CREATE TABLE IF NOT EXISTS `packaging_unit` (
  `pu_idx`        INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `pu_lg_idx`     INT UNSIGNED NOT NULL COMMENT '지자체 FK',
  `pu_bag_code`   VARCHAR(50) NOT NULL COMMENT '봉투코드(code_detail cd_code, ck=O)',
  `pu_bag_name`   VARCHAR(100) NOT NULL DEFAULT '' COMMENT '봉투명(스냅샷)',
  `pu_box_per_pack` INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '박스당 팩 수',
  `pu_pack_per_sheet` INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '팩당 낱장 수',
  `pu_total_per_box` INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '1박스당 총 낱장 수',
  `pu_start_date` DATE NOT NULL,
  `pu_end_date`   DATE NULL DEFAULT NULL,
  `pu_state`      TINYINT UNSIGNED NOT NULL DEFAULT 1,
  `pu_regdate`    DATETIME NOT NULL,
  `pu_moddate`    DATETIME NULL DEFAULT NULL,
  `pu_reg_mb_idx` INT UNSIGNED NULL,
  PRIMARY KEY (`pu_idx`),
  KEY `idx_pu_lg_bag` (`pu_lg_idx`, `pu_bag_code`),
  KEY `idx_pu_dates` (`pu_start_date`, `pu_end_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='포장 단위';

CREATE TABLE IF NOT EXISTS `packaging_unit_history` (
  `puh_idx`        INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `puh_pu_idx`     INT UNSIGNED NOT NULL,
  `puh_field`      VARCHAR(30) NOT NULL,
  `puh_old_value`  VARCHAR(100) NOT NULL DEFAULT '',
  `puh_new_value`  VARCHAR(100) NOT NULL DEFAULT '',
  `puh_changed_at` DATETIME NOT NULL,
  `puh_changed_by` INT UNSIGNED NULL,
  PRIMARY KEY (`puh_idx`),
  KEY `idx_puh_pu_idx` (`puh_pu_idx`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='포장 단위 변경 이력';
