-- 轧辊库
CREATE TABLE mes_roll_info (
                               roll_id              BIGINT NOT NULL AUTO_INCREMENT,
                               roll_no              VARCHAR(50) NOT NULL COMMENT '轧辊编号',
                               roll_type            VARCHAR(10) NOT NULL COMMENT '辊型: WR=工作辊 / BR=支撑辊',
                               status               VARCHAR(20) NOT NULL DEFAULT 'Offline' COMMENT 'Online/Standby/Offline/Scrapped',
                               initial_dia          DECIMAL(10,2)  COMMENT '初始直径(mm)',
                               current_dia          DECIMAL(10,2)  COMMENT '当前直径(mm)',
                               min_dia              DECIMAL(10,2)  COMMENT '最小直径(mm)',
                               roughness            DECIMAL(10,4),
                               crown                DECIMAL(10,4),
                               material             VARCHAR(50),
                               grind_count          INT NOT NULL DEFAULT 0,
                               total_rolled_weight  DECIMAL(15,2) NOT NULL DEFAULT 0.00,
                               total_rolled_length  DECIMAL(15,2) NOT NULL DEFAULT 0.00,
                               total_rolled_count   INT NOT NULL DEFAULT 0,
                               manufacture_date     DATE,
                               del_flag             INT NOT NULL DEFAULT 0,
                               create_by VARCHAR(64), create_time DATETIME,
                               update_by VARCHAR(64), update_time DATETIME,
                               remark VARCHAR(500),
                               PRIMARY KEY (roll_id),
                               UNIQUE KEY uk_roll_no (roll_no)
) ENGINE=InnoDB COMMENT='轧辊库';

-- 换辊记录（四辊轧机，双机架）
CREATE TABLE mes_roll_change (
                                 change_id    BIGINT NOT NULL AUTO_INCREMENT,
                                 change_no    VARCHAR(50)  COMMENT '换辊编号',
                                 change_time  DATETIME     COMMENT '换辊时间',
                                 stand_no     VARCHAR(10)  COMMENT '机架号: 1# / 2#',
                                 change_type  VARCHAR(20)  COMMENT '换辊类型: 计划换辊/紧急换辊',
                                 change_status VARCHAR(20),
                                 operator     VARCHAR(50),
    -- 四支辊
                                 upper_wr_no  VARCHAR(50) COMMENT '上工作辊编号',
                                 upper_wr_dia DECIMAL(10,2),
                                 lower_wr_no  VARCHAR(50) COMMENT '下工作辊编号',
                                 lower_wr_dia DECIMAL(10,2),
                                 upper_br_no  VARCHAR(50) COMMENT '上支撑辊编号',
                                 upper_br_dia DECIMAL(10,2),
                                 lower_br_no  VARCHAR(50) COMMENT '下支撑辊编号',
                                 lower_br_dia DECIMAL(10,2),
                                 del_flag     INT NOT NULL DEFAULT 0,
                                 create_by VARCHAR(64), create_time DATETIME,
                                 update_by VARCHAR(64), update_time DATETIME,
                                 remark VARCHAR(500),
                                 PRIMARY KEY (change_id),
                                 KEY idx_stand_time (stand_no, change_time)
) ENGINE=InnoDB COMMENT='换辊记录';

-- 下批轧辊（每条=一个辊位的备用辊）
CREATE TABLE mes_roll_standby (
                                  standby_id  BIGINT NOT NULL AUTO_INCREMENT,
                                  stand_no    VARCHAR(10)   COMMENT '机架号: 1# / 2#',
                                  roll_no     VARCHAR(50)   COMMENT '轧辊编号',
                                  roll_type   VARCHAR(10)   COMMENT 'WR=工作辊 / BR=支撑辊',
                                  position    VARCHAR(10)   COMMENT 'UP=上 / DOWN=下',
                                  diameter    DECIMAL(10,2),
                                  roughness   DECIMAL(10,4),
                                  crown       DECIMAL(10,4),
                                  ready_time  DATETIME,
                                  del_flag    INT NOT NULL DEFAULT 0,
                                  create_by VARCHAR(64), create_time DATETIME,
                                  update_by VARCHAR(64), update_time DATETIME,
                                  remark VARCHAR(500),
                                  PRIMARY KEY (standby_id)
) ENGINE=InnoDB COMMENT='下批轧辊（待换上）';
