-- ============================================
-- 봉투 단가 관리 테이블 (P2-03, P2-04)
-- ============================================

CREATE TABLE IF NOT EXISTS `bag_price` (
  `bp_idx`        INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `bp_lg_idx`     INT UNSIGNED NOT NULL COMMENT '지자체 FK',
  `bp_bag_code`   VARCHAR(50)  NOT NULL COMMENT '봉투코드(code_detail cd_code, ck=O)',
  `bp_bag_name`   VARCHAR(100) NOT NULL DEFAULT '' COMMENT '봉투명(등록시점 스냅샷)',
  `bp_order_price`  DECIMAL(12,2) NOT NULL DEFAULT 0 COMMENT '발주단가',
  `bp_wholesale`    DECIMAL(12,2) NOT NULL DEFAULT 0 COMMENT '도매가',
  `bp_consumer`     DECIMAL(12,2) NOT NULL DEFAULT 0 COMMENT '소비자가',
  `bp_start_date` DATE NOT NULL COMMENT '적용 시작일',
  `bp_end_date`   DATE NULL DEFAULT NULL COMMENT '적용 종료일(NULL=현재 적용중)',
  `bp_state`      TINYINT UNSIGNED NOT NULL DEFAULT 1 COMMENT '1=사용, 0=미사용',
  `bp_regdate`    DATETIME NOT NULL,
  `bp_moddate`    DATETIME NULL DEFAULT NULL,
  `bp_reg_mb_idx` INT UNSIGNED NULL COMMENT '등록자',
  PRIMARY KEY (`bp_idx`),
  KEY `idx_bp_lg_bag` (`bp_lg_idx`, `bp_bag_code`),
  KEY `idx_bp_dates` (`bp_start_date`, `bp_end_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='지자체별 봉투 단가';

CREATE TABLE IF NOT EXISTS `bag_price_history` (
  `bph_idx`          INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `bph_bp_idx`       INT UNSIGNED NOT NULL COMMENT 'bag_price FK',
  `bph_field`        VARCHAR(30) NOT NULL COMMENT '변경 필드명',
  `bph_old_value`    VARCHAR(100) NOT NULL DEFAULT '',
  `bph_new_value`    VARCHAR(100) NOT NULL DEFAULT '',
  `bph_changed_at`   DATETIME NOT NULL,
  `bph_changed_by`   INT UNSIGNED NULL COMMENT '변경자 mb_idx',
  PRIMARY KEY (`bph_idx`),
  KEY `idx_bph_bp_idx` (`bph_bp_idx`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='봉투 단가 변경 이력';
