#!/bin/bash

# SpringDoc OpenAPI 注解迁移脚本
# 将 Springfox 注解替换为 SpringDoc OpenAPI 注解

echo "开始迁移 Swagger 注解..."

# 查找所有 Java 文件
find src/main/java -name "*.java" -type f | while read file; do
    echo "处理文件: $file"
    
    # 备份原文件
    cp "$file" "$file.bak"
    
    # 替换 import 语句
    sed -i '' 's/import io\.swagger\.annotations\.Api;/import io.swagger.v3.oas.annotations.tags.Tag;/g' "$file"
    sed -i '' 's/import io\.swagger\.annotations\.ApiOperation;/import io.swagger.v3.oas.annotations.Operation;/g' "$file"
    sed -i '' 's/import io\.swagger\.annotations\.ApiParam;/import io.swagger.v3.oas.annotations.Parameter;/g' "$file"
    sed -i '' 's/import io\.swagger\.annotations\.ApiModel;/import io.swagger.v3.oas.annotations.media.Schema;/g' "$file"
    sed -i '' 's/import io\.swagger\.annotations\.ApiModelProperty;/import io.swagger.v3.oas.annotations.media.Schema;/g' "$file"
    
    # 替换注解使用
    sed -i '' 's/@Api(tags = "\([^"]*\)")/@Tag(name = "\1")/g' "$file"
    sed -i '' 's/@ApiOperation("\([^"]*\)")/@Operation(summary = "\1")/g' "$file"
    sed -i '' 's/@ApiOperation(value = "\([^"]*\)")/@Operation(summary = "\1")/g' "$file"
    sed -i '' 's/@ApiOperation(value = "\([^"]*\)", notes = "\([^"]*\)")/@Operation(summary = "\1", description = "\2")/g' "$file"
    
    # 替换实体类注解
    sed -i '' 's/@ApiModel(value = "\([^"]*\)", description = "\([^"]*\)")/@Schema(name = "\1", description = "\2")/g' "$file"
    sed -i '' 's/@ApiModel(value = "\([^"]*\)")/@Schema(name = "\1")/g' "$file"
    sed -i '' 's/@ApiModel("\([^"]*\)")/@Schema(description = "\1")/g' "$file"
    
    # 替换属性注解
    sed -i '' 's/@ApiModelProperty(value = "\([^"]*\)")/@Schema(description = "\1")/g' "$file"
    sed -i '' 's/@ApiModelProperty("\([^"]*\)")/@Schema(description = "\1")/g' "$file"
    
    # 替换参数注解
    sed -i '' 's/@ApiParam(name = "\([^"]*\)", value = "\([^"]*\)", required = \([^)]*\))/@Parameter(name = "\1", description = "\2", required = \3)/g' "$file"
    sed -i '' 's/@ApiParam(value = "\([^"]*\)")/@Parameter(description = "\1")/g' "$file"
    sed -i '' 's/@ApiParam("\([^"]*\)")/@Parameter(description = "\1")/g' "$file"
    
    echo "完成处理: $file"
done

echo "注解迁移完成！"
echo "请检查修改后的文件，如有问题可以从 .bak 文件恢复"
