ai-business-write/rollback_incorrect_updates.py

123 lines
4.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
回滚错误的更新,恢复被错误修改的字段
"""
import os
import pymysql
# 数据库连接配置
DB_CONFIG = {
'host': os.getenv('DB_HOST', '152.136.177.240'),
'port': int(os.getenv('DB_PORT', 5012)),
'user': os.getenv('DB_USER', 'finyx'),
'password': os.getenv('DB_PASSWORD', '6QsGK6MpePZDE57Z'),
'database': os.getenv('DB_NAME', 'finyx'),
'charset': 'utf8mb4'
}
TENANT_ID = 615873064429507639
UPDATED_BY = 655162080928945152
# 需要恢复的字段映射字段ID -> 正确的field_code
ROLLBACK_MAPPING = {
# 这些字段被错误地从英文改成了中文,需要恢复
1764656917410273: 'target_issue_description',
1764656918032031: 'filler_name',
1764656917418979: 'department_opinion',
1764836032906561: 'appointment_location',
1764836032488198: 'appointment_time',
1764836033052889: 'approval_time',
1764836032655678: 'handler_name',
1764836033342084: 'handling_department',
1764836033240593: 'investigation_unit_name',
1764836033018470: 'investigation_location',
1764836033274278: 'investigation_team_code',
1764836033094781: 'investigation_team_member_names',
1764836033176386: 'investigation_team_leader_name',
1764836033500799: 'commission_name',
1764656917384058: 'clue_info',
1764656917861268: 'clue_source',
1764836032538308: 'target_address',
1764836033565636: 'target_health_status',
1764836033332970: 'target_other_situation',
1764656917299164: 'target_date_of_birth',
1764836033269146: 'target_date_of_birth_full',
1765151880445876: 'target_organization',
1764656917367205: 'target_organization_and_position',
1764836033405778: 'target_family_situation',
1764836033162748: 'target_work_basic_info',
1764656917996367: 'target_basic_info_clue',
1764836032997850: 'target_age',
1764656917561689: 'target_gender',
1764836032855869: 'target_personality',
1764836032893680: 'target_registered_address',
1764836033603501: 'target_tolerance',
1764656917185956: 'target_political_status',
1764836033786057: 'target_attitude',
1764836033587951: 'target_previous_investigation',
1764836032951705: 'target_ethnicity',
1764836033280024: 'target_other_issues_possibility',
1764836033458872: 'target_issue_severity',
1764836032929811: 'target_social_relations',
1764836033618877: 'target_negative_events',
1764836032926994: 'target_place_of_origin',
1765151880304552: 'target_position',
1764656917802442: 'target_professional_rank',
1764836032817243: 'target_contact',
1764836032902356: 'target_id_number',
1764836032913357: 'target_id_number',
1764656917073644: 'target_name',
1764836033571266: 'target_problem_description',
1764836032827460: 'report_card_request_time',
1764836032694865: 'notification_location',
1764836032909732: 'notification_time',
1764836033451248: 'risk_level',
}
def rollback():
"""回滚错误的更新"""
conn = pymysql.connect(**DB_CONFIG)
cursor = conn.cursor(pymysql.cursors.DictCursor)
print("="*80)
print("回滚错误的字段更新")
print("="*80)
print(f"\n需要恢复 {len(ROLLBACK_MAPPING)} 个字段\n")
# 先查询当前状态
for field_id, correct_code in ROLLBACK_MAPPING.items():
cursor.execute("""
SELECT id, name, filed_code
FROM f_polic_field
WHERE id = %s AND tenant_id = %s
""", (field_id, TENANT_ID))
field = cursor.fetchone()
if field:
print(f" ID: {field_id}")
print(f" 名称: {field['name']}")
print(f" 当前field_code: {field['filed_code']}")
print(f" 恢复为: {correct_code}")
print()
# 执行回滚
print("开始执行回滚...\n")
for field_id, correct_code in ROLLBACK_MAPPING.items():
cursor.execute("""
UPDATE f_polic_field
SET filed_code = %s, updated_time = NOW(), updated_by = %s
WHERE id = %s AND tenant_id = %s
""", (correct_code, UPDATED_BY, field_id, TENANT_ID))
print(f" ✓ 恢复字段 ID {field_id}: {correct_code}")
conn.commit()
print("\n✓ 回滚完成")
cursor.close()
conn.close()
if __name__ == '__main__':
rollback()