#!/bin/bash

# 数据库连接信息
DB_NAME="www_359844_cn"
DB_USER="www_359844_cn"
DB_PASSWORD="kdGSRtw4hy5HJAjc"
DB_HOST="localhost"
DB_PORT="3306"

# 表名和条件
TABLE_NAME="fa_admin_log"
CONDITION="admin_id = 3"

echo "开始删除数据库 $DB_NAME 中表 $TABLE_NAME 的数据..."
echo "删除条件: $CONDITION"

# 首先查询要删除的数据数量
echo "查询要删除的数据数量..."
COUNT_QUERY="SELECT COUNT(*) as count FROM $TABLE_NAME WHERE $CONDITION;"
COUNT_RESULT=$(mysql -h$DB_HOST -P$DB_PORT -u$DB_USER -p$DB_PASSWORD $DB_NAME -e "$COUNT_QUERY" 2>/dev/null | tail -n 1)

if [ $? -eq 0 ]; then
    echo "将要删除 $COUNT_RESULT 条数据"
    
    # 直接执行删除操作
    echo "正在执行删除操作..."
    DELETE_QUERY="DELETE FROM $TABLE_NAME WHERE $CONDITION;"
    mysql -h$DB_HOST -P$DB_PORT -u$DB_USER -p$DB_PASSWORD $DB_NAME -e "$DELETE_QUERY"
    
    if [ $? -eq 0 ]; then
        echo "删除成功！"
        
        # 验证删除结果
        VERIFY_QUERY="SELECT COUNT(*) as count FROM $TABLE_NAME WHERE $CONDITION;"
        VERIFY_RESULT=$(mysql -h$DB_HOST -P$DB_PORT -u$DB_USER -p$DB_PASSWORD $DB_NAME -e "$VERIFY_QUERY" 2>/dev/null | tail -n 1)
        echo "删除后剩余符合条件的记录数: $VERIFY_RESULT"
    else
        echo "删除失败！请检查数据库连接和权限。"
        exit 1
    fi
else
    echo "查询失败！请检查数据库连接信息。"
    exit 1
fi 