ai-business-write/verify_template_code.py

93 lines
3.0 KiB
Python

"""
验证模板编码是否存在
"""
import pymysql
import json
import os
# 数据库连接配置
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 verify_template_code(template_code: str):
"""验证指定的模板编码是否存在"""
print("="*60)
print(f"验证模板编码: {template_code}")
print("="*60)
try:
conn = pymysql.connect(**DB_CONFIG)
cursor = conn.cursor(pymysql.cursors.DictCursor)
# 查询所有文件配置
sql = """
SELECT id, name, file_path, input_data, state
FROM f_polic_file_config
WHERE tenant_id = %s
AND state = 1
"""
cursor.execute(sql, (TENANT_ID,))
configs = cursor.fetchall()
found = False
print(f"\n查询到 {len(configs)} 个启用的文件配置\n")
for config in configs:
try:
input_data = json.loads(config['input_data']) if config['input_data'] else {}
config_template_code = input_data.get('template_code')
if config_template_code == template_code:
found = True
print(f"✓ 找到匹配的模板:")
print(f" - 名称: {config['name']}")
print(f" - ID: {config['id']}")
print(f" - 模板编码: {config_template_code}")
print(f" - 文件路径: {config['file_path']}")
print(f" - 状态: {'启用' if config['state'] == 1 else '未启用'}")
print()
except (json.JSONDecodeError, TypeError) as e:
continue
if not found:
print(f"✗ 未找到模板编码: {template_code}")
print("\n可用的模板编码列表:")
print("-" * 60)
for config in configs:
try:
input_data = json.loads(config['input_data']) if config['input_data'] else {}
config_template_code = input_data.get('template_code')
if config_template_code:
print(f" - {config_template_code} ({config['name']})")
except:
continue
print()
return found
except Exception as e:
print(f"✗ 查询失败: {str(e)}")
return False
finally:
if 'cursor' in locals():
cursor.close()
if 'conn' in locals():
conn.close()
if __name__ == '__main__':
# 验证 REPORT_CARD 模板编码
verify_template_code('REPORT_CARD')
print("\n" + "="*60)
print("验证完成")
print("="*60)