#!/bin/bash
# 批量修复所有handler，统一使用response包
set -e

APP_DIR="/Users/zhangjianmin/sourceCode/GolandProjects/src/sundynix-micro-go/app"

find "$APP_DIR" -name "*Handler.go" -path "*/handler/*" | while read -r file; do
    # 跳过已经修复的文件（已导入response包）
    if grep -q "sundynix-micro-go/common/response" "$file"; then
        continue
    fi
    
    # 检查是否有需要替换的内容
    if ! grep -q 'httpx.Ok(w)\|httpx.ErrorCtx' "$file"; then
        continue
    fi
    
    echo "修复: $file"
    
    # 1. 添加 response 导入
    sed -i '' 's|"github.com/zeromicro/go-zero/rest/httpx"|"github.com/zeromicro/go-zero/rest/httpx"\
	"sundynix-micro-go/common/response"|' "$file"
    
    # 2. 替换 httpx.ErrorCtx(r.Context(), w, err) -> response.Fail(w, err.Error())
    sed -i '' 's|httpx.ErrorCtx(r.Context(), w, err)|response.Fail(w, err.Error())|g' "$file"
    
    # 3. 替换 httpx.Ok(w) -> response.Ok(w)
    sed -i '' 's|httpx.Ok(w)|response.Ok(w)|g' "$file"
done

echo "===全部修复完成==="
