ai-business-write/check_template_with_output_fields.py

77 lines
2.7 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
import os
from dotenv import load_dotenv
load_dotenv()
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
def check_templates_with_output_fields():
"""检查哪些模板有输出字段关联"""
conn = pymysql.connect(**DB_CONFIG)
cursor = conn.cursor(pymysql.cursors.DictCursor)
try:
# 查询所有模板及其关联的输出字段
cursor.execute("""
SELECT
fc.id as template_id,
fc.name as template_name,
COUNT(CASE WHEN f.field_type = 2 THEN 1 END) as output_field_count,
COUNT(CASE WHEN f.field_type = 1 THEN 1 END) as input_field_count,
COUNT(*) as total_field_count
FROM f_polic_file_config fc
INNER JOIN f_polic_file_field fff ON fc.id = fff.file_id AND fc.tenant_id = fff.tenant_id
INNER JOIN f_polic_field f ON fff.filed_id = f.id AND fff.tenant_id = f.tenant_id
WHERE fc.tenant_id = %s
AND fff.state = 1
AND fc.state = 1
GROUP BY fc.id, fc.name
HAVING output_field_count > 0
ORDER BY output_field_count DESC
LIMIT 10
""", (TENANT_ID,))
templates = cursor.fetchall()
print(f"有输出字段关联的模板前10个:")
print("=" * 80)
for t in templates:
print(f"\n模板: {t['template_name']} (ID: {t['template_id']})")
print(f" 输入字段: {t['input_field_count']}, 输出字段: {t['output_field_count']}, 总计: {t['total_field_count']}")
# 查询该模板的具体输出字段
cursor.execute("""
SELECT f.id, f.name, f.filed_code
FROM f_polic_file_field fff
INNER JOIN f_polic_field f ON fff.filed_id = f.id AND fff.tenant_id = f.tenant_id
WHERE fff.tenant_id = %s
AND fff.file_id = %s
AND fff.state = 1
AND f.field_type = 2
LIMIT 5
""", (TENANT_ID, t['template_id']))
output_fields = cursor.fetchall()
print(f" 输出字段示例前5个:")
for f in output_fields:
print(f" - {f['name']} (ID: {f['id']}, code: {f['filed_code']})")
finally:
cursor.close()
conn.close()
if __name__ == '__main__':
check_templates_with_output_fields()