-- liquibase formatted sql
-- changeset 2fi:20260521_01_truck_lane_schedule
-- preconditions onFail:MARK_RAN
-- precondition-sql-check expectedResult:0 SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'truck_lane_schedule'

CREATE TABLE IF NOT EXISTS `truck_lane_schedule`
(
    `id`                 BIGINT       NOT NULL AUTO_INCREMENT,
    `created`            DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `createdBy`          VARCHAR(30)  NULL     DEFAULT NULL,
    `version`            INT          NOT NULL DEFAULT '0',
    `modified`           DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `modifiedBy`         VARCHAR(30)  NULL     DEFAULT NULL,
    `deleted`            TINYINT(1)   NOT NULL DEFAULT '0',
    `executeAt`          DATETIME     NOT NULL,
    `status`             VARCHAR(20)  NOT NULL DEFAULT 'PENDING',
    `source`             VARCHAR(20)  NOT NULL DEFAULT 'MANUAL',
    `note`               VARCHAR(500) NULL     DEFAULT NULL,
    `appliedAt`          DATETIME     NULL     DEFAULT NULL,
    `errorMessage`       VARCHAR(2000) NULL    DEFAULT NULL,
    `snapshotVersionId`  BIGINT       NULL     DEFAULT NULL,
    CONSTRAINT pk_truck_lane_schedule PRIMARY KEY (`id`)
);

CREATE TABLE IF NOT EXISTS `truck_lane_schedule_line`
(
    `id`                  BIGINT       NOT NULL AUTO_INCREMENT,
    `created`             DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `createdBy`           VARCHAR(30)  NULL     DEFAULT NULL,
    `version`             INT          NOT NULL DEFAULT '0',
    `modified`            DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `modifiedBy`          VARCHAR(30)  NULL     DEFAULT NULL,
    `deleted`             TINYINT(1)   NOT NULL DEFAULT '0',
    `scheduleId`          BIGINT       NOT NULL,
    `truckRowId`          BIGINT       NOT NULL,
    `shopCode`            VARCHAR(50)  NULL     DEFAULT NULL,
    `shopName`            VARCHAR(255) NULL     DEFAULT NULL,
    `fromTruckLanceCode`  VARCHAR(100) NOT NULL,
    `fromRemark`          VARCHAR(255) NULL     DEFAULT NULL,
    `fromStoreId`         VARCHAR(10)  NOT NULL,
    `toTruckLanceCode`    VARCHAR(100) NOT NULL,
    `toRemark`            VARCHAR(255) NULL     DEFAULT NULL,
    `toStoreId`           VARCHAR(10)  NOT NULL,
    `lineStatus`          VARCHAR(20)  NOT NULL DEFAULT 'PENDING',
    `errorMessage`        VARCHAR(2000) NULL    DEFAULT NULL,
    `appliedAt`           DATETIME     NULL     DEFAULT NULL,
    CONSTRAINT pk_truck_lane_schedule_line PRIMARY KEY (`id`),
    CONSTRAINT fk_tlsl_schedule FOREIGN KEY (`scheduleId`) REFERENCES `truck_lane_schedule` (`id`)
);

SET @idx_tls_status_exec := (
    SELECT COUNT(*) FROM information_schema.statistics
    WHERE table_schema = DATABASE() AND table_name = 'truck_lane_schedule' AND index_name = 'idx_tls_status_execute'
);
SET @sql_tls := IF(
    @idx_tls_status_exec = 0,
    'CREATE INDEX idx_tls_status_execute ON `truck_lane_schedule` (`status`, `executeAt`)',
    'SELECT 1'
);
PREPARE stmt_tls FROM @sql_tls;
EXECUTE stmt_tls;
DEALLOCATE PREPARE stmt_tls;

SET @idx_tlsl_schedule := (
    SELECT COUNT(*) FROM information_schema.statistics
    WHERE table_schema = DATABASE() AND table_name = 'truck_lane_schedule_line' AND index_name = 'idx_tlsl_schedule'
);
SET @sql_tlsl_s := IF(
    @idx_tlsl_schedule = 0,
    'CREATE INDEX idx_tlsl_schedule ON `truck_lane_schedule_line` (`scheduleId`)',
    'SELECT 1'
);
PREPARE stmt_tlsl_s FROM @sql_tlsl_s;
EXECUTE stmt_tlsl_s;
DEALLOCATE PREPARE stmt_tlsl_s;

SET @idx_tlsl_truck := (
    SELECT COUNT(*) FROM information_schema.statistics
    WHERE table_schema = DATABASE() AND table_name = 'truck_lane_schedule_line' AND index_name = 'idx_tlsl_truck_row'
);
SET @sql_tlsl_t := IF(
    @idx_tlsl_truck = 0,
    'CREATE INDEX idx_tlsl_truck_row ON `truck_lane_schedule_line` (`truckRowId`, `lineStatus`)',
    'SELECT 1'
);
PREPARE stmt_tlsl_t FROM @sql_tlsl_t;
EXECUTE stmt_tlsl_t;
DEALLOCATE PREPARE stmt_tlsl_t;
