ai-business-write/check_and_fix_duplicates.py

118 lines
4.2 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.

"""
检查并修复重复记录
"""
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 parent_id = %s
ORDER BY id
""", (TENANT_ID, 1765431558933731)) # 1.初核请示
results = cursor.fetchall()
print(f"'1.初核请示'下有 {len(results)} 条记录:\n")
for r in results:
print(f"ID: {r['id']}, name: {r['name']}, file_path: {r['file_path']}")
# 检查"1请示报告卡"的记录
request_cards = [r for r in results if r['name'] == '1请示报告卡']
if len(request_cards) > 1:
print(f"\n发现 {len(request_cards)} 个重复的'1请示报告卡'记录")
# 保留file_path正确的那个
correct_one = None
for r in request_cards:
if r['file_path'] and '1.请示报告卡XXX' in r['file_path']:
correct_one = r
break
if correct_one:
# 删除其他的
for r in request_cards:
if r['id'] != correct_one['id']:
# 删除关联关系
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']}")
# 检查"走读式谈话审批"下是否有"1请示报告卡"
cursor.execute("""
SELECT id, name, file_path, parent_id
FROM f_polic_file_config
WHERE tenant_id = %s AND parent_id = %s AND name = %s
""", (TENANT_ID, 1765273962700431, '1请示报告卡')) # 走读式谈话审批
result = cursor.fetchone()
if not result:
print("\n[WARN] '走读式谈话审批'下缺少'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,
1765273962700431, # 走读式谈话审批
'1请示报告卡',
None,
'/615873064429507639/TEMPLATE/2025/12/1.请示报告卡(初核谈话).docx',
655162080928945152,
655162080928945152,
1
))
print(f"[CREATE] 在'走读式谈话审批'下创建'1请示报告卡'记录 (ID: {new_id})")
else:
# 检查file_path是否正确
if result['file_path'] and '1.请示报告卡(初核谈话)' not in result['file_path']:
cursor.execute("""
UPDATE f_polic_file_config
SET file_path = %s, updated_time = NOW(), updated_by = %s
WHERE tenant_id = %s AND id = %s
""", ('/615873064429507639/TEMPLATE/2025/12/1.请示报告卡(初核谈话).docx', UPDATED_BY, TENANT_ID, result['id']))
print(f"[UPDATE] 修复'走读式谈话审批''1请示报告卡'的file_path")
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()