""" 修复数据库中模板配置的 input_data 字段 确保所有记录的 input_data JSON 中包含正确的 template_code """ import pymysql import json import os from datetime import datetime # 数据库连接配置 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 UPDATED_BY = 655162080928945152 CURRENT_TIME = datetime.now() # 模板编码映射(从 init_all_templates.py 复制) DOCUMENT_TYPE_MAPPING = { "1.请示报告卡(XXX)": { "template_code": "REPORT_CARD", "business_type": "INVESTIGATION" }, "2.初步核实审批表(XXX)": { "template_code": "PRELIMINARY_VERIFICATION_APPROVAL", "business_type": "INVESTIGATION" }, "3.附件初核方案(XXX)": { "template_code": "INVESTIGATION_PLAN", "business_type": "INVESTIGATION" }, "谈话通知书第一联": { "template_code": "NOTIFICATION_LETTER_1", "business_type": "INVESTIGATION" }, "谈话通知书第二联": { "template_code": "NOTIFICATION_LETTER_2", "business_type": "INVESTIGATION" }, "谈话通知书第三联": { "template_code": "NOTIFICATION_LETTER_3", "business_type": "INVESTIGATION" }, "1.请示报告卡(初核谈话)": { "template_code": "REPORT_CARD_INTERVIEW", "business_type": "INVESTIGATION" }, "2谈话审批表": { "template_code": "INTERVIEW_APPROVAL_FORM", "business_type": "INVESTIGATION" }, "3.谈话前安全风险评估表": { "template_code": "PRE_INTERVIEW_RISK_ASSESSMENT", "business_type": "INVESTIGATION" }, "4.谈话方案": { "template_code": "INTERVIEW_PLAN", "business_type": "INVESTIGATION" }, "5.谈话后安全风险评估表": { "template_code": "POST_INTERVIEW_RISK_ASSESSMENT", "business_type": "INVESTIGATION" }, "1.谈话笔录": { "template_code": "INTERVIEW_RECORD", "business_type": "INVESTIGATION" }, "2.谈话询问对象情况摸底调查30问": { "template_code": "INVESTIGATION_30_QUESTIONS", "business_type": "INVESTIGATION" }, "3.被谈话人权利义务告知书": { "template_code": "RIGHTS_OBLIGATIONS_NOTICE", "business_type": "INVESTIGATION" }, "4.点对点交接单": { "template_code": "HANDOVER_FORM", "business_type": "INVESTIGATION" }, "4.点对点交接单2": { "template_code": "HANDOVER_FORM_2", "business_type": "INVESTIGATION" }, "5.陪送交接单(新)": { "template_code": "ESCORT_HANDOVER_FORM", "business_type": "INVESTIGATION" }, "6.1保密承诺书(谈话对象使用-非中共党员用)": { "template_code": "CONFIDENTIALITY_COMMITMENT_NON_PARTY", "business_type": "INVESTIGATION" }, "6.2保密承诺书(谈话对象使用-中共党员用)": { "template_code": "CONFIDENTIALITY_COMMITMENT_PARTY", "business_type": "INVESTIGATION" }, "7.办案人员-办案安全保密承诺书": { "template_code": "INVESTIGATOR_CONFIDENTIALITY_COMMITMENT", "business_type": "INVESTIGATION" }, "8-1请示报告卡(初核报告结论) ": { "template_code": "REPORT_CARD_CONCLUSION", "business_type": "INVESTIGATION" }, "8.XXX初核情况报告": { "template_code": "INVESTIGATION_REPORT", "business_type": "INVESTIGATION" } } def fix_input_data(): """修复所有记录的 input_data 字段""" print("="*80) print("修复模板配置的 input_data 字段") print("="*80) try: conn = pymysql.connect(**DB_CONFIG) cursor = conn.cursor(pymysql.cursors.DictCursor) # 查询所有文件配置 sql = """ SELECT id, name, template_code, input_data, state FROM f_polic_file_config WHERE tenant_id = %s ORDER BY name """ cursor.execute(sql, (TENANT_ID,)) configs = cursor.fetchall() print(f"\n找到 {len(configs)} 个文件配置\n") updated_count = 0 skipped_count = 0 for config in configs: config_id = config['id'] name = config['name'] template_code_col = config.get('template_code') input_data_str = config.get('input_data') state = config.get('state', 0) # 确定应该使用的 template_code target_template_code = None target_business_type = "INVESTIGATION" # 默认值 # 方法1: 从映射表中查找 if name in DOCUMENT_TYPE_MAPPING: target_template_code = DOCUMENT_TYPE_MAPPING[name]['template_code'] target_business_type = DOCUMENT_TYPE_MAPPING[name]['business_type'] # 方法2: 使用 template_code 列的值 elif template_code_col: target_template_code = template_code_col if not target_template_code: print(f"⚠ 跳过: {name} (无法确定 template_code)") skipped_count += 1 continue # 检查 input_data 是否需要更新 need_update = False current_input_data = {} if input_data_str: try: current_input_data = json.loads(input_data_str) if isinstance(input_data_str, str) else input_data_str if not isinstance(current_input_data, dict): current_input_data = {} except: current_input_data = {} # 检查 template_code 是否匹配 if current_input_data.get('template_code') != target_template_code: need_update = True # 检查 business_type 是否存在 if not current_input_data.get('business_type'): need_update = True if need_update: # 更新 input_data new_input_data = { 'template_code': target_template_code, 'business_type': target_business_type } # 保留原有的其他字段(如果有) for key, value in current_input_data.items(): if key not in ['template_code', 'business_type']: new_input_data[key] = value new_input_data_str = json.dumps(new_input_data, ensure_ascii=False) update_sql = """ UPDATE f_polic_file_config SET input_data = %s, updated_time = %s, updated_by = %s WHERE id = %s """ cursor.execute(update_sql, (new_input_data_str, CURRENT_TIME, UPDATED_BY, config_id)) conn.commit() print(f"✓ 更新: {name}") print(f" template_code: {target_template_code}") print(f" business_type: {target_business_type}") updated_count += 1 else: print(f"✓ 已正确: {name} (template_code: {target_template_code})") print("\n" + "="*80) print("修复完成") print("="*80) print(f"更新: {updated_count} 个记录") print(f"跳过: {skipped_count} 个记录") print(f"总计: {len(configs)} 个记录") except Exception as e: print(f"✗ 修复失败: {str(e)}") import traceback traceback.print_exc() finally: if 'cursor' in locals(): cursor.close() if 'conn' in locals(): conn.close() if __name__ == '__main__': fix_input_data()