-- liquibase formatted sql
-- changeset KelvinY:20260611_01_create_do_replenishment
-- preconditions onFail:MARK_RAN
-- precondition-sql-check expectedResult:0 SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'do_replenishment'

CREATE TABLE IF NOT EXISTS `do_replenishment`
(
    `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',
    `code`                 VARCHAR(100)   NOT NULL,
    `deliveryDate`         DATE           NOT NULL,
    `sourceDoId`           BIGINT         NOT NULL,
    `sourceDoCode`         VARCHAR(100)   NULL     DEFAULT NULL,
    `sourceDoLineId`       BIGINT         NOT NULL,
    `sourceM18DataLogId`   BIGINT         NOT NULL,
    `sourceM18Id`          BIGINT         NOT NULL,
    `itemId`               BIGINT         NOT NULL,
    `itemNo`               VARCHAR(100)   NULL     DEFAULT NULL,
    `itemName`             VARCHAR(255)   NULL     DEFAULT NULL,
    `replenishQty`         DECIMAL(14, 2) NOT NULL,
    `uomId`                BIGINT         NULL     DEFAULT NULL,
    `shopId`               BIGINT         NULL     DEFAULT NULL,
    `shopCode`             VARCHAR(50)    NULL     DEFAULT NULL,
    `shopName`             VARCHAR(255)   NULL     DEFAULT NULL,
    `truckLaneCode`        VARCHAR(100)   NULL     DEFAULT NULL,
    `targetDoId`           BIGINT         NULL     DEFAULT NULL,
    `targetDoCode`         VARCHAR(100)   NULL     DEFAULT NULL,
    `pickOrderLineId`      BIGINT         NULL     DEFAULT NULL,
    `status`               VARCHAR(20)    NOT NULL DEFAULT 'pending',
    CONSTRAINT pk_do_replenishment PRIMARY KEY (`id`)
);

SET @idx_dr_code := (
    SELECT COUNT(*) FROM information_schema.statistics
    WHERE table_schema = DATABASE() AND table_name = 'do_replenishment' AND index_name = 'uk_dr_code'
);
SET @sql_dr_code := IF(
    @idx_dr_code = 0,
    'CREATE UNIQUE INDEX uk_dr_code ON `do_replenishment` (`code`)',
    'SELECT 1'
);
PREPARE stmt_dr_code FROM @sql_dr_code;
EXECUTE stmt_dr_code;
DEALLOCATE PREPARE stmt_dr_code;

SET @idx_dr_status_date := (
    SELECT COUNT(*) FROM information_schema.statistics
    WHERE table_schema = DATABASE() AND table_name = 'do_replenishment' AND index_name = 'idx_dr_status_delivery_date'
);
SET @sql_dr_sd := IF(
    @idx_dr_status_date = 0,
    'CREATE INDEX idx_dr_status_delivery_date ON `do_replenishment` (`status`, `deliveryDate`)',
    'SELECT 1'
);
PREPARE stmt_dr_sd FROM @sql_dr_sd;
EXECUTE stmt_dr_sd;
DEALLOCATE PREPARE stmt_dr_sd;

SET @idx_dr_lane_shop := (
    SELECT COUNT(*) FROM information_schema.statistics
    WHERE table_schema = DATABASE() AND table_name = 'do_replenishment' AND index_name = 'idx_dr_lane_shop_status'
);
SET @sql_dr_ls := IF(
    @idx_dr_lane_shop = 0,
    'CREATE INDEX idx_dr_lane_shop_status ON `do_replenishment` (`truckLaneCode`, `shopId`, `status`)',
    'SELECT 1'
);
PREPARE stmt_dr_ls FROM @sql_dr_ls;
EXECUTE stmt_dr_ls;
DEALLOCATE PREPARE stmt_dr_ls;

SET @idx_dr_source_line := (
    SELECT COUNT(*) FROM information_schema.statistics
    WHERE table_schema = DATABASE() AND table_name = 'do_replenishment' AND index_name = 'idx_dr_source_line'
);
SET @sql_dr_sl := IF(
    @idx_dr_source_line = 0,
    'CREATE INDEX idx_dr_source_line ON `do_replenishment` (`sourceDoId`, `sourceDoLineId`)',
    'SELECT 1'
);
PREPARE stmt_dr_sl FROM @sql_dr_sl;
EXECUTE stmt_dr_sl;
DEALLOCATE PREPARE stmt_dr_sl;
