-- ============================================================
-- erp_mp_mbom 表补充缺失字段
-- 修复: Unknown column 'business_type' in 'field list'
-- MpMbomMapper 与 MpMbom 实体需要的字段需与表结构一致
-- 生成日期: 2026-02-06
-- ============================================================

SET NAMES utf8mb4;

-- 辅助存储过程 (若已存在可跳过)
DROP PROCEDURE IF EXISTS `proc_add_column_if_not_exists`;

DELIMITER //
CREATE PROCEDURE `proc_add_column_if_not_exists`(
    IN p_table_name VARCHAR(64),
    IN p_column_name VARCHAR(64),
    IN p_column_definition VARCHAR(500)
)
BEGIN
    DECLARE v_count INT DEFAULT 0;
    SELECT COUNT(*) INTO v_count
    FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = p_table_name
      AND COLUMN_NAME = p_column_name;
    IF v_count = 0 THEN
        SET @sql = CONCAT('ALTER TABLE `', p_table_name, '` ADD COLUMN `', p_column_name, '` ', p_column_definition);
        PREPARE stmt FROM @sql;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;
    END IF;
END //
DELIMITER ;

-- erp_mp_mbom 补充 MpMbomMapper 所需的缺失字段
CALL proc_add_column_if_not_exists('erp_mp_mbom', 'business_type', "varchar(20) DEFAULT NULL COMMENT '业务类型(BOM运算/生产补料)'");
CALL proc_add_column_if_not_exists('erp_mp_mbom', 'sales_order_id', "bigint(20) DEFAULT NULL COMMENT '销售订单ID'");
CALL proc_add_column_if_not_exists('erp_mp_mbom', 'operator_id', "bigint(20) DEFAULT NULL COMMENT '操作员ID'");
CALL proc_add_column_if_not_exists('erp_mp_mbom', 'operator_name', "varchar(50) DEFAULT NULL COMMENT '操作员名称'");
CALL proc_add_column_if_not_exists('erp_mp_mbom', 'approver_id', "bigint(20) DEFAULT NULL COMMENT '审核员ID'");
CALL proc_add_column_if_not_exists('erp_mp_mbom', 'approver_name', "varchar(50) DEFAULT NULL COMMENT '审核员名称'");
CALL proc_add_column_if_not_exists('erp_mp_mbom', 'approve_date', "datetime DEFAULT NULL COMMENT '审核日期'");

DROP PROCEDURE IF EXISTS `proc_add_column_if_not_exists`;
