USE jiaheng_erp;

SET @db_name = DATABASE();

-- 1) 产品版本：允许同一产品编码保留多个版本，因此取消 md_item.item_code 的全局唯一约束。
SET @drop_md_item_code_unique = (
  SELECT IFNULL(
    (
      SELECT CONCAT('ALTER TABLE md_item DROP INDEX `', index_name, '`')
      FROM information_schema.statistics
      WHERE table_schema = @db_name
        AND table_name = 'md_item'
        AND column_name = 'item_code'
        AND non_unique = 0
        AND index_name <> 'PRIMARY'
      GROUP BY index_name
      HAVING COUNT(*) = 1
      LIMIT 1
    ),
    'SELECT 1'
  )
);
PREPARE stmt FROM @drop_md_item_code_unique;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @create_md_item_code_index = (
  SELECT IF(
    EXISTS(
      SELECT 1
      FROM information_schema.statistics
      WHERE table_schema = @db_name
        AND table_name = 'md_item'
        AND index_name = 'idx_md_item_code'
    ),
    'SELECT 1',
    'CREATE INDEX idx_md_item_code ON md_item (item_code)'
  )
);
PREPARE stmt FROM @create_md_item_code_index;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 2) BOM 版本：同一 BOM 编码允许多个版本，唯一性改为 (bom_code, version_no)。
SET @drop_md_bom_code_unique = (
  SELECT IFNULL(
    (
      SELECT CONCAT('ALTER TABLE md_bom DROP INDEX `', index_name, '`')
      FROM information_schema.statistics
      WHERE table_schema = @db_name
        AND table_name = 'md_bom'
        AND column_name = 'bom_code'
        AND non_unique = 0
        AND index_name <> 'PRIMARY'
      GROUP BY index_name
      HAVING COUNT(*) = 1
      LIMIT 1
    ),
    'SELECT 1'
  )
);
PREPARE stmt FROM @drop_md_bom_code_unique;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @create_md_bom_code_version_unique = (
  SELECT IF(
    EXISTS(
      SELECT 1
      FROM information_schema.statistics
      WHERE table_schema = @db_name
        AND table_name = 'md_bom'
        AND index_name = 'uk_bom_code_version'
    ),
    'SELECT 1',
    'ALTER TABLE md_bom ADD UNIQUE KEY uk_bom_code_version (bom_code, version_no)'
  )
);
PREPARE stmt FROM @create_md_bom_code_version_unique;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 3) 工艺路线版本：同一工艺路线编码允许多个版本，唯一性改为 (route_code, version_no)。
SET @drop_md_route_code_unique = (
  SELECT IFNULL(
    (
      SELECT CONCAT('ALTER TABLE md_process_route DROP INDEX `', index_name, '`')
      FROM information_schema.statistics
      WHERE table_schema = @db_name
        AND table_name = 'md_process_route'
        AND column_name = 'route_code'
        AND non_unique = 0
        AND index_name <> 'PRIMARY'
      GROUP BY index_name
      HAVING COUNT(*) = 1
      LIMIT 1
    ),
    'SELECT 1'
  )
);
PREPARE stmt FROM @drop_md_route_code_unique;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @create_md_route_code_version_unique = (
  SELECT IF(
    EXISTS(
      SELECT 1
      FROM information_schema.statistics
      WHERE table_schema = @db_name
        AND table_name = 'md_process_route'
        AND index_name = 'uk_route_code_version'
    ),
    'SELECT 1',
    'ALTER TABLE md_process_route ADD UNIQUE KEY uk_route_code_version (route_code, version_no)'
  )
);
PREPARE stmt FROM @create_md_route_code_version_unique;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
