#!/bin/bash

# 京东Cookie管理模块重构 - 旧代码清理脚本
# 作者：AI助手
# 创建时间：2025-10-08
# 说明：清理旧的card_redeem_cookie相关代码和数据库表

echo "开始清理旧的card_redeem_cookie相关代码..."

# 备份目录
BACKUP_DIR="backup/card_redeem_cookie_$(date +%Y%m%d_%H%M%S)"
mkdir -p "$BACKUP_DIR"

echo "创建备份目录: $BACKUP_DIR"

# ====================================================================================
# 备份旧文件（不直接删除，先备份）
# ====================================================================================

echo "备份旧的控制器文件..."
if [ -d "internal/controller/card_redeem_jd" ]; then
    cp -r internal/controller/card_redeem_jd "$BACKUP_DIR/"
    echo "✓ 备份 internal/controller/card_redeem_jd"
fi

echo "备份旧的逻辑层文件..."
if [ -d "internal/logic/card_redeem_cookie" ]; then
    cp -r internal/logic/card_redeem_cookie "$BACKUP_DIR/"
    echo "✓ 备份 internal/logic/card_redeem_cookie"
fi

echo "备份旧的服务层文件..."
if [ -f "internal/service/card_redeem_cookie.go" ]; then
    cp internal/service/card_redeem_cookie.go "$BACKUP_DIR/"
    echo "✓ 备份 internal/service/card_redeem_cookie.go"
fi

echo "备份旧的模型文件..."
if [ -f "internal/model/card_redeem_cookie.go" ]; then
    cp internal/model/card_redeem_cookie.go "$BACKUP_DIR/"
    echo "✓ 备份 internal/model/card_redeem_cookie.go"
fi

echo "备份旧的常量文件..."
if [ -f "internal/consts/card_redeem_cookie.go" ]; then
    cp internal/consts/card_redeem_cookie.go "$BACKUP_DIR/"
    echo "✓ 备份 internal/consts/card_redeem_cookie.go"
fi

echo "备份旧的API文件..."
if [ -d "api/card_redeem_jd" ]; then
    cp -r api/card_redeem_jd "$BACKUP_DIR/"
    echo "✓ 备份 api/card_redeem_jd"
fi

# ====================================================================================
# 创建数据库表清理SQL（不直接执行）
# ====================================================================================

echo "生成数据库表清理SQL..."
cat > "$BACKUP_DIR/cleanup_old_tables.sql" << 'EOF'
-- 旧card_redeem_cookie相关表清理SQL
-- 注意：执行前请先备份数据！
-- 使用方法：mysql -u root -p kami < cleanup_old_tables.sql

-- 备份现有数据（可选）
-- CREATE TABLE card_redeem_cookie_info_backup AS SELECT * FROM card_redeem_cookie_info;
-- CREATE TABLE card_redeem_cookie_order_backup AS SELECT * FROM card_redeem_cookie_order;
-- CREATE TABLE card_redeem_cookie_order_jd_backup AS SELECT * FROM card_redeem_cookie_order_jd;
-- CREATE TABLE card_redeem_cookie_order_history_backup AS SELECT * FROM card_redeem_cookie_order_history;
-- CREATE TABLE card_redeem_cookie_order_jd_history_backup AS SELECT * FROM card_redeem_cookie_order_jd_history;

-- 删除旧表（请谨慎执行）
-- DROP TABLE IF EXISTS card_redeem_cookie_info;
-- DROP TABLE IF EXISTS card_redeem_cookie_order;
-- DROP TABLE IF EXISTS card_redeem_cookie_order_jd;
-- DROP TABLE IF EXISTS card_redeem_cookie_order_history;
-- DROP TABLE IF EXISTS card_redeem_cookie_order_jd_history;

EOF

echo "✓ 生成 $BACKUP_DIR/cleanup_old_tables.sql"

# ====================================================================================
# 创建代码清理脚本（不直接执行）
# ====================================================================================

echo "生成代码清理脚本..."
cat > "$BACKUP_DIR/cleanup_old_code.sh" << 'EOF'
#!/bin/bash

# 代码清理脚本
# 注意：执行前请确保新模块已经正常工作！

echo "开始清理旧代码文件..."

# 删除旧的控制器
if [ -d "internal/controller/card_redeem_jd" ]; then
    rm -rf internal/controller/card_redeem_jd
    echo "✓ 删除 internal/controller/card_redeem_jd"
fi

# 删除旧的逻辑层
if [ -d "internal/logic/card_redeem_cookie" ]; then
    rm -rf internal/logic/card_redeem_cookie
    echo "✓ 删除 internal/logic/card_redeem_cookie"
fi

# 删除旧的服务层
if [ -f "internal/service/card_redeem_cookie.go" ]; then
    rm internal/service/card_redeem_cookie.go
    echo "✓ 删除 internal/service/card_redeem_cookie.go"
fi

# 删除旧的模型文件
if [ -f "internal/model/card_redeem_cookie.go" ]; then
    rm internal/model/card_redeem_cookie.go
    echo "✓ 删除 internal/model/card_redeem_cookie.go"
fi

# 删除旧的常量文件
if [ -f "internal/consts/card_redeem_cookie.go" ]; then
    rm internal/consts/card_redeem_cookie.go
    echo "✓ 删除 internal/consts/card_redeem_cookie.go"
fi

# 删除旧的API文件
if [ -d "api/card_redeem_jd" ]; then
    rm -rf api/card_redeem_jd
    echo "✓ 删除 api/card_redeem_jd"
fi

# 删除旧的DAO文件（card_redeem_cookie前缀）
echo "清理旧的DAO文件..."
find internal/dao -name "*card_redeem_cookie*" -type f -delete
find internal/model/do -name "*card_redeem_cookie*" -type f -delete
find internal/model/entity -name "*card_redeem_cookie*" -type f -delete

echo "旧代码清理完成！"

EOF

chmod +x "$BACKUP_DIR/cleanup_old_code.sh"
echo "✓ 生成 $BACKUP_DIR/cleanup_old_code.sh"

# ====================================================================================
# 创建迁移指南
# ====================================================================================

echo "生成迁移指南..."
cat > "$BACKUP_DIR/MIGRATION_GUIDE.md" << 'EOF'
# 京东Cookie管理模块重构迁移指南

## 概述

本指南说明了从旧的 `card_redeem_cookie` 模块迁移到新的 `jd_cookie` 模块的步骤。

## 新旧对比

### 数据库表对比

| 旧表名 | 新表名 | 说明 |
|--------|--------|------|
| card_redeem_cookie_info | jd_cookie_account | Cookie账户表 |
| card_redeem_cookie_order | jd_cookie_order | 订单表 |
| card_redeem_cookie_order_jd | jd_cookie_jd_order | 京东订单表 |
| card_redeem_cookie_order_history | jd_cookie_order_change_history | 订单变更历史表 |
| card_redeem_cookie_order_jd_history | jd_cookie_jd_order_change_history | 京东订单变更历史表 |
| 无 | jd_cookie_change_history | Cookie变更历史表（新增） |

### API路径对比

| 旧API路径 | 新API路径 | 说明 |
|-----------|-----------|------|
| /api/v1/card-redeem-jd/* | /api/v1/jd-cookie/* | 路径前缀变更 |

### 代码结构对比

| 旧模块 | 新模块 | 说明 |
|--------|--------|------|
| api/card_redeem_jd | api/jd_cookie | API定义 |
| internal/controller/card_redeem_jd | internal/controller/jd_cookie | 控制器 |
| internal/logic/card_redeem_cookie | internal/logic/jd_cookie | 业务逻辑 |
| internal/service/card_redeem_cookie.go | internal/service/jd_cookie.go | 服务接口 |
| internal/consts/card_redeem_cookie.go | internal/consts/jd_cookie.go | 常量定义 |

## 迁移步骤

### 1. 数据迁移

如果需要保留现有数据，可以执行数据迁移：

```sql
-- 迁移Cookie账户数据
INSERT INTO jd_cookie_account (cookie_id, cookie_value, account_name, status, failure_count, created_at, updated_at, remark)
SELECT 
    CONCAT('migrated_', id) as cookie_id,
    cookie as cookie_value,
    name as account_name,
    CASE 
        WHEN status = 'normal' THEN 1
        WHEN status = 'tmpDisable' THEN 2
        WHEN status = 'expired' THEN 3
        ELSE 1
    END as status,
    fail_count as failure_count,
    created_at,
    updated_at,
    notes as remark
FROM card_redeem_cookie_info;

-- 其他表的迁移SQL请根据实际需求编写
```

### 2. 测试新模块

在清理旧代码前，请确保新模块功能正常：

1. 测试Cookie账户管理功能
2. 测试订单创建和支付流程
3. 测试历史记录查询功能
4. 测试错误处理和异常情况

### 3. 更新配置

更新相关配置文件，确保路由配置正确指向新的控制器。

### 4. 清理旧代码

确认新模块正常工作后，执行清理脚本：

```bash
# 执行代码清理
./cleanup_old_code.sh

# 执行数据库表清理（可选）
mysql -u root -p kami < cleanup_old_tables.sql
```

## 注意事项

1. **备份数据**：清理前务必备份重要数据
2. **渐进式迁移**：建议分步骤进行迁移，先确保新功能正常工作
3. **监控告警**：迁移过程中密切监控系统状态和错误日志
4. **回滚方案**：制定详细的回滚方案，以防迁移失败

## 问题排查

如果遇到问题，请检查：

1. 数据库连接配置是否正确
2. 新表是否正确创建
3. DAO代码是否正确生成
4. 服务注册是否正确
5. 路由配置是否更新

EOF

echo "✓ 生成 $BACKUP_DIR/MIGRATION_GUIDE.md"

echo ""
echo "======================================================================================"
echo "备份和清理脚本生成完成！"
echo "备份目录: $BACKUP_DIR"
echo ""
echo "包含文件："
echo "  - cleanup_old_tables.sql    # 数据库表清理SQL"
echo "  - cleanup_old_code.sh       # 代码清理脚本"
echo "  - MIGRATION_GUIDE.md        # 迁移指南"
echo "  - [备份的旧文件]"
echo ""
echo "重要提醒："
echo "1. 请在新模块测试通过后再执行清理操作"
echo "2. 执行清理前务必备份重要数据"
echo "3. 建议先在测试环境验证迁移流程"
echo "======================================================================================"