ai-business-write/fix_duplicate_request_report_card.py

132 lines
4.5 KiB
Python
Raw 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.

"""
修复重复的"1请示报告卡"记录
确保每个文件在正确的位置只有一个记录
"""
import pymysql
DB_CONFIG = {
'host': '152.136.177.240',
'port': 5012,
'user': 'finyx',
'password': '6QsGK6MpePZDE57Z',
'database': 'finyx',
'charset': 'utf8mb4'
}
TENANT_ID = 615873064429507639
UPDATED_BY = 655162080928945152
conn = pymysql.connect(**DB_CONFIG)
cursor = conn.cursor(pymysql.cursors.DictCursor)
try:
# 查找所有"1请示报告卡"记录
cursor.execute("""
SELECT id, name, file_path, parent_id
FROM f_polic_file_config
WHERE tenant_id = %s AND name = %s
ORDER BY id
""", (TENANT_ID, '1请示报告卡'))
results = cursor.fetchall()
print(f"找到 {len(results)}'1请示报告卡'记录:\n")
# 根据file_path和parent_id判断哪些是正确的
correct_records = []
for r in results:
print(f"ID: {r['id']}, file_path: {r['file_path']}, parent_id: {r['parent_id']}")
# 判断是否正确
if r['parent_id'] == 1765431558933731: # 1.初核请示
if '1.请示报告卡XXX' in (r['file_path'] or ''):
correct_records.append(r)
elif r['parent_id'] == 1765273962700431: # 走读式谈话审批
if '1.请示报告卡(初核谈话)' in (r['file_path'] or ''):
correct_records.append(r)
print(f"\n正确的记录数: {len(correct_records)}")
# 删除不正确的记录
for r in results:
if r not in correct_records:
# 先删除关联关系
cursor.execute("""
DELETE FROM f_polic_file_field
WHERE tenant_id = %s AND file_id = %s
""", (TENANT_ID, r['id']))
# 删除模板记录
cursor.execute("""
DELETE FROM f_polic_file_config
WHERE tenant_id = %s AND id = %s
""", (TENANT_ID, r['id']))
print(f"[DELETE] 删除不正确的记录: ID {r['id']}, file_path: {r['file_path']}, parent_id: {r['parent_id']}")
# 确保两个位置都有正确的记录
has_initial_request = any(r['parent_id'] == 1765431558933731 for r in correct_records)
has_interview_approval = any(r['parent_id'] == 1765273962700431 for r in correct_records)
if not has_initial_request:
# 创建"1.初核请示"下的记录
import time
import random
timestamp = int(time.time() * 1000)
random_part = random.randint(100000, 999999)
new_id = timestamp * 1000 + random_part
insert_sql = """
INSERT INTO f_polic_file_config
(id, tenant_id, parent_id, name, input_data, file_path, created_time, created_by, updated_time, updated_by, state)
VALUES (%s, %s, %s, %s, %s, %s, NOW(), %s, NOW(), %s, %s)
"""
cursor.execute(insert_sql, (
new_id,
TENANT_ID,
1765431558933731, # 1.初核请示
'1请示报告卡',
None,
'/615873064429507639/TEMPLATE/2025/12/1.请示报告卡XXX.docx',
655162080928945152,
655162080928945152,
1
))
print(f"[CREATE] 在'1.初核请示'下创建'1请示报告卡'记录 (ID: {new_id})")
if not has_interview_approval:
# 创建"走读式谈话审批"下的记录
import time
import random
timestamp = int(time.time() * 1000)
random_part = random.randint(100000, 999999)
new_id = timestamp * 1000 + random_part
insert_sql = """
INSERT INTO f_polic_file_config
(id, tenant_id, parent_id, name, input_data, file_path, created_time, created_by, updated_time, updated_by, state)
VALUES (%s, %s, %s, %s, %s, %s, NOW(), %s, NOW(), %s, %s)
"""
cursor.execute(insert_sql, (
new_id,
TENANT_ID,
1765273962700431, # 走读式谈话审批
'1请示报告卡',
None,
'/615873064429507639/TEMPLATE/2025/12/1.请示报告卡(初核谈话).docx',
655162080928945152,
655162080928945152,
1
))
print(f"[CREATE] 在'走读式谈话审批'下创建'1请示报告卡'记录 (ID: {new_id})")
conn.commit()
print("\n[OK] 修复完成")
except Exception as e:
conn.rollback()
print(f"[ERROR] 修复失败: {e}")
import traceback
traceback.print_exc()
finally:
cursor.close()
conn.close()