-- ============================================
-- 在库存使用申请表中添加总价字段
-- ============================================
-- 说明：用于保存该批次所有商品的总价
-- ============================================

-- 检查并添加总价字段
SET @dbname = DATABASE();
SET @tablename = 'lq_inventory_usage_application';
SET @columnname = 'F_TotalAmount';
SET @preparedStatement = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE
      (TABLE_SCHEMA = @dbname)
      AND (TABLE_NAME = @tablename)
      AND (COLUMN_NAME = @columnname)
  ) > 0,
  'SELECT 1',
  CONCAT('ALTER TABLE ', @tablename, ' ADD COLUMN `', @columnname, '` decimal(18,2) DEFAULT 0.00 COMMENT ''申请总金额（该批次所有商品的总价）'' AFTER `F_Remarks`')
));
PREPARE alterIfNotExists FROM @preparedStatement;
EXECUTE alterIfNotExists;
DEALLOCATE PREPARE alterIfNotExists;

-- 添加索引（如果不存在）
ALTER TABLE `lq_inventory_usage_application`
  ADD INDEX `idx_total_amount` (`F_TotalAmount`) COMMENT '总价索引';

