""" 验证模板关联关系 """ 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 TEMPLATE_ID = 1765432134276990 # 1.请示报告卡(初核谈话) def verify_relations(): """验证关联关系""" conn = pymysql.connect(**DB_CONFIG) cursor = conn.cursor(pymysql.cursors.DictCursor) try: print(f"验证模板 ID: {TEMPLATE_ID}") print("=" * 80) # 查询所有关联关系(包括 state=0 的) cursor.execute(""" SELECT fff.filed_id, fff.state as relation_state, f.name as field_name, f.filed_code, f.field_type, f.state as field_state 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 ORDER BY f.field_type, f.name """, (TENANT_ID, TEMPLATE_ID)) relations = cursor.fetchall() print(f"\n所有关联关系数: {len(relations)}") # 处理 state 字段 for rel in relations: if isinstance(rel['relation_state'], bytes): rel['relation_state'] = int.from_bytes(rel['relation_state'], byteorder='big') if len(rel['relation_state']) == 1 else 0 if isinstance(rel['field_state'], bytes): rel['field_state'] = int.from_bytes(rel['field_state'], byteorder='big') if len(rel['field_state']) == 1 else 0 # 按状态分组 enabled = [r for r in relations if r['relation_state'] == 1] disabled = [r for r in relations if r['relation_state'] != 1] print(f"启用的关联关系: {len(enabled)}") print(f"未启用的关联关系: {len(disabled)}") # 按字段类型分组 input_fields = [r for r in enabled if r['field_type'] == 1] output_fields = [r for r in enabled if r['field_type'] == 2] print(f"\n启用的输入字段关联: {len(input_fields)}") for r in input_fields: print(f" - {r['field_name']} (ID: {r['filed_id']}, code: {r['filed_code']})") print(f"\n启用的输出字段关联: {len(output_fields)}") for r in output_fields: print(f" - {r['field_name']} (ID: {r['filed_id']}, code: {r['filed_code']})") if disabled: print(f"\n未启用的关联关系: {len(disabled)}") for r in disabled[:5]: print(f" - {r['field_name']} (ID: {r['filed_id']}, relation_state: {r['relation_state']})") finally: cursor.close() conn.close() if __name__ == '__main__': verify_relations()