SET @dbname = DATABASE();
SET @tablename = 'dm_repair_order_entry';
SET @columnname = 'item_id';
 
-- 修改 item_id 字段允许为空
SET @preparedStatement = (SELECT IF(
  (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND COLUMN_NAME = @columnname) > 0,
  CONCAT('ALTER TABLE ', @tablename, ' MODIFY COLUMN ', @columnname, ' BIGINT DEFAULT NULL COMMENT ''项目ID（可选）'''),
  'SELECT 1'
));
PREPARE alterIfExists FROM @preparedStatement;
EXECUTE alterIfExists;
DEALLOCATE PREPARE alterIfExists;

SET @dbname = DATABASE();
SET @tablename = 'dm_repair_order_entry';
SET @columnname = 'item_name';

-- 修改 item_name 字段允许为空（避免保存维修单明细时报 NOT NULL 约束异常）
SET @preparedStatement = (SELECT IF(
  (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND COLUMN_NAME = @columnname) > 0,
  CONCAT('ALTER TABLE ', @tablename, ' MODIFY COLUMN ', @columnname, ' VARCHAR(64) DEFAULT NULL COMMENT ''项目名称（可选）'''),
  'SELECT 1'
));
PREPARE alterIfExists FROM @preparedStatement;
EXECUTE alterIfExists;
DEALLOCATE PREPARE alterIfExists;

