ai-business-write/finalize_template_hierarchy.py

159 lines
5.9 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 pymysql
import time
import random
from pathlib import Path
DB_CONFIG = {
'host': '152.136.177.240',
'port': 5012,
'user': 'finyx',
'password': '6QsGK6MpePZDE57Z',
'database': 'finyx',
'charset': 'utf8mb4'
}
TENANT_ID = 615873064429507639
CREATED_BY = 655162080928945152
UPDATED_BY = 655162080928945152
def generate_id():
timestamp = int(time.time() * 1000)
random_part = random.randint(100000, 999999)
return timestamp * 1000 + random_part
conn = pymysql.connect(**DB_CONFIG)
cursor = conn.cursor(pymysql.cursors.DictCursor)
try:
# 检查"1请示报告卡"的记录
# 根据目录结构,应该有两个不同的文件:
# 1. "1.初核请示"下的"1.请示报告卡XXX.docx"
# 2. "走读式谈话审批"下的"1.请示报告卡(初核谈话).docx"
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()
# 检查是否在"1.初核请示"下有记录
in_initial_request = any(r['parent_id'] == 1765431558933731 for r in results)
# 检查是否在"走读式谈话审批"下有记录
in_interview_approval = any(r['parent_id'] == 1765273962700431 for r in results)
if not in_initial_request:
# 需要在"1.初核请示"下创建记录
new_id = generate_id()
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',
CREATED_BY,
CREATED_BY,
1
))
print(f"[CREATE] 在'1.初核请示'下创建'1请示报告卡'记录 (ID: {new_id})")
if not in_interview_approval:
# 需要在"走读式谈话审批"下创建记录
new_id = generate_id()
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',
CREATED_BY,
CREATED_BY,
1
))
print(f"[CREATE] 在'走读式谈话审批'下创建'1请示报告卡'记录 (ID: {new_id})")
# 更新现有记录的文件路径
for result in results:
if result['parent_id'] == 1765431558933731: # 1.初核请示
correct_path = '/615873064429507639/TEMPLATE/2025/12/1.请示报告卡XXX.docx'
elif result['parent_id'] == 1765273962700431: # 走读式谈话审批
correct_path = '/615873064429507639/TEMPLATE/2025/12/1.请示报告卡(初核谈话).docx'
else:
continue
if result['file_path'] != correct_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
""", (correct_path, UPDATED_BY, TENANT_ID, result['id']))
print(f"[UPDATE] 修复'1请示报告卡'的文件路径 (ID: {result['id']}): {result['file_path']} -> {correct_path}")
# 检查重复的"XXX初核情况报告"
cursor.execute("""
SELECT id, name, file_path, parent_id
FROM f_polic_file_config
WHERE tenant_id = %s AND name LIKE %s
ORDER BY id
""", (TENANT_ID, '%XXX初核情况报告%'))
results = cursor.fetchall()
if len(results) > 1:
# 保留最新的,删除旧的
# 或者根据file_path判断哪个是正确的
# 根据目录结构,应该是"8.XXX初核情况报告.docx"
correct_name = 'XXX初核情况报告'
correct_path = '/615873064429507639/TEMPLATE/2025/12/8.XXX初核情况报告.docx'
for r in results:
if r['name'] == '8.XXX初核情况报告':
# 这个应该删除(名称带数字前缀)
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] 删除重复记录: {r['name']} (ID: {r['id']})")
elif r['name'] == 'XXX初核情况报告':
# 更新这个记录的文件路径
if r['file_path'] != correct_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
""", (correct_path, UPDATED_BY, TENANT_ID, r['id']))
print(f"[UPDATE] 更新'XXX初核情况报告'的文件路径: {r['file_path']} -> {correct_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()