SET @exists_bag_code_id = (SELECT COUNT(*) FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = 'mes_wm_transaction' AND column_name = 'bag_code_id');
SET @sql_bag_code_id = IF(@exists_bag_code_id = 0, 'ALTER TABLE mes_wm_transaction ADD COLUMN bag_code_id BIGINT NULL COMMENT ''生产袋码ID'' AFTER material_stock_id', 'SELECT 1');
PREPARE stmt FROM @sql_bag_code_id;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @exists_bag_code = (SELECT COUNT(*) FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = 'mes_wm_transaction' AND column_name = 'bag_code');
SET @sql_bag_code = IF(@exists_bag_code = 0, 'ALTER TABLE mes_wm_transaction ADD COLUMN bag_code VARCHAR(64) NULL COMMENT ''袋码'' AFTER bag_code_id', 'SELECT 1');
PREPARE stmt FROM @sql_bag_code;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @exists_bag_code_index = (SELECT COUNT(*) FROM information_schema.statistics WHERE table_schema = DATABASE() AND table_name = 'mes_wm_transaction' AND index_name = 'idx_bag_code');
SET @sql_bag_code_index = IF(@exists_bag_code_index = 0, 'ALTER TABLE mes_wm_transaction ADD KEY idx_bag_code (bag_code)', 'SELECT 1');
PREPARE stmt FROM @sql_bag_code_index;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
