308 lines
9.9 KiB
Python
308 lines
9.9 KiB
Python
"""
|
||
谈话前安全风险评估表字段数据初始化脚本
|
||
添加风险评估相关字段并关联到"谈话前安全风险评估表"模板
|
||
"""
|
||
import pymysql
|
||
from datetime import datetime
|
||
import uuid
|
||
|
||
# 数据库连接配置
|
||
DB_CONFIG = {
|
||
'host': '152.136.177.240',
|
||
'port': 5012,
|
||
'user': 'finyx',
|
||
'password': '6QsGK6MpePZDE57Z',
|
||
'database': 'finyx',
|
||
'charset': 'utf8mb4'
|
||
}
|
||
|
||
# 固定值
|
||
TENANT_ID = 615873064429507639 # 从现有数据中获取
|
||
CREATED_BY = 655162080928945152 # 从现有数据中获取
|
||
CURRENT_TIME = datetime.now()
|
||
|
||
# 字段配置(带默认值)
|
||
FIELDS = [
|
||
{
|
||
'name': '被核查人员家庭情况',
|
||
'field_code': 'target_family_situation',
|
||
'field_type': 2, # 输出字段
|
||
'description': '被核查人员家庭情况',
|
||
'default_value': '家庭关系和谐稳定'
|
||
},
|
||
{
|
||
'name': '被核查人员社会关系',
|
||
'field_code': 'target_social_relations',
|
||
'field_type': 2, # 输出字段
|
||
'description': '被核查人员社会关系',
|
||
'default_value': '社会交往较多,人机关系基本正常'
|
||
},
|
||
{
|
||
'name': '被核查人员健康状况',
|
||
'field_code': 'target_health_status',
|
||
'field_type': 2, # 输出字段
|
||
'description': '被核查人员健康状况',
|
||
'default_value': '良好'
|
||
},
|
||
{
|
||
'name': '被核查人员性格特征',
|
||
'field_code': 'target_personality',
|
||
'field_type': 2, # 输出字段
|
||
'description': '被核查人员性格特征',
|
||
'default_value': '开朗'
|
||
},
|
||
{
|
||
'name': '被核查人员承受能力',
|
||
'field_code': 'target_tolerance',
|
||
'field_type': 2, # 输出字段
|
||
'description': '被核查人员承受能力',
|
||
'default_value': '较强'
|
||
},
|
||
{
|
||
'name': '被核查人员涉及问题严重程度',
|
||
'field_code': 'target_issue_severity',
|
||
'field_type': 2, # 输出字段
|
||
'description': '被核查人员涉及问题严重程度',
|
||
'default_value': '较轻'
|
||
},
|
||
{
|
||
'name': '被核查人员涉及其他问题的可能性',
|
||
'field_code': 'target_other_issues_possibility',
|
||
'field_type': 2, # 输出字段
|
||
'description': '被核查人员涉及其他问题的可能性',
|
||
'default_value': '较小'
|
||
},
|
||
{
|
||
'name': '被核查人员此前被审查情况',
|
||
'field_code': 'target_previous_investigation',
|
||
'field_type': 2, # 输出字段
|
||
'description': '被核查人员此前被审查情况',
|
||
'default_value': '无'
|
||
},
|
||
{
|
||
'name': '被核查人员社会负面事件',
|
||
'field_code': 'target_negative_events',
|
||
'field_type': 2, # 输出字段
|
||
'description': '被核查人员社会负面事件',
|
||
'default_value': '无'
|
||
},
|
||
{
|
||
'name': '被核查人员其他情况',
|
||
'field_code': 'target_other_situation',
|
||
'field_type': 2, # 输出字段
|
||
'description': '被核查人员其他情况',
|
||
'default_value': '无'
|
||
},
|
||
{
|
||
'name': '风险等级',
|
||
'field_code': 'risk_level',
|
||
'field_type': 2, # 输出字段
|
||
'description': '风险等级',
|
||
'default_value': '低'
|
||
}
|
||
]
|
||
|
||
# 文件配置
|
||
FILE_CONFIG = {
|
||
'name': '谈话前安全风险评估表',
|
||
'template_code': 'PRE_INTERVIEW_RISK_ASSESSMENT',
|
||
'file_path': '/templates/谈话前安全风险评估表模板.docx', # MinIO相对路径
|
||
'business_type': 'INVESTIGATION', # 调查核实
|
||
'parent_id': None # 顶级分类,可以根据实际情况设置
|
||
}
|
||
|
||
|
||
def generate_id():
|
||
"""生成ID(使用时间戳+随机数的方式,模拟雪花算法)"""
|
||
import time
|
||
import random
|
||
timestamp = int(time.time() * 1000)
|
||
random_part = random.randint(100000, 999999)
|
||
return timestamp * 1000 + random_part
|
||
|
||
|
||
def init_fields(conn):
|
||
"""初始化字段数据"""
|
||
cursor = conn.cursor()
|
||
field_ids = {}
|
||
|
||
print("="*60)
|
||
print("开始初始化字段数据...")
|
||
print("="*60)
|
||
|
||
for field in FIELDS:
|
||
# 检查字段是否已存在
|
||
check_sql = """
|
||
SELECT id FROM f_polic_field
|
||
WHERE tenant_id = %s AND filed_code = %s
|
||
"""
|
||
cursor.execute(check_sql, (TENANT_ID, field['field_code']))
|
||
existing = cursor.fetchone()
|
||
|
||
if existing:
|
||
field_id = existing[0]
|
||
print(f"字段 '{field['name']}' (code: {field['field_code']}) 已存在,ID: {field_id}")
|
||
else:
|
||
field_id = generate_id()
|
||
insert_sql = """
|
||
INSERT INTO f_polic_field
|
||
(id, tenant_id, name, filed_code, field_type, created_time, created_by, updated_time, updated_by, state)
|
||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||
"""
|
||
cursor.execute(insert_sql, (
|
||
field_id,
|
||
TENANT_ID,
|
||
field['name'],
|
||
field['field_code'],
|
||
field['field_type'],
|
||
CURRENT_TIME,
|
||
CREATED_BY,
|
||
CURRENT_TIME,
|
||
CREATED_BY,
|
||
0 # state: 0表示未启用,1表示启用
|
||
))
|
||
print(f"✓ 创建字段: {field['name']} (code: {field['field_code']}), 默认值: {field['default_value']}, ID: {field_id}")
|
||
|
||
field_ids[field['field_code']] = field_id
|
||
|
||
conn.commit()
|
||
return field_ids
|
||
|
||
|
||
def init_file_config(conn):
|
||
"""初始化文件配置"""
|
||
cursor = conn.cursor()
|
||
|
||
print("\n" + "="*60)
|
||
print("开始初始化文件配置...")
|
||
print("="*60)
|
||
|
||
# 检查文件配置是否已存在
|
||
check_sql = """
|
||
SELECT id FROM f_polic_file_config
|
||
WHERE tenant_id = %s AND name = %s
|
||
"""
|
||
cursor.execute(check_sql, (TENANT_ID, FILE_CONFIG['name']))
|
||
existing = cursor.fetchone()
|
||
|
||
if existing:
|
||
file_config_id = existing[0]
|
||
print(f"文件配置 '{FILE_CONFIG['name']}' 已存在,ID: {file_config_id}")
|
||
else:
|
||
file_config_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, %s, %s, %s, %s, %s)
|
||
"""
|
||
# input_data字段存储模板编码和业务类型(JSON格式)
|
||
import json
|
||
input_data = json.dumps({
|
||
'template_code': FILE_CONFIG['template_code'],
|
||
'business_type': FILE_CONFIG['business_type']
|
||
}, ensure_ascii=False)
|
||
|
||
cursor.execute(insert_sql, (
|
||
file_config_id,
|
||
TENANT_ID,
|
||
FILE_CONFIG['parent_id'],
|
||
FILE_CONFIG['name'],
|
||
input_data,
|
||
FILE_CONFIG['file_path'],
|
||
CURRENT_TIME,
|
||
CREATED_BY,
|
||
CURRENT_TIME,
|
||
CREATED_BY,
|
||
1 # state: 1表示启用
|
||
))
|
||
print(f"✓ 创建文件配置: {FILE_CONFIG['name']}, ID: {file_config_id}")
|
||
|
||
conn.commit()
|
||
return file_config_id
|
||
|
||
|
||
def init_file_field_relations(conn, file_config_id, field_ids):
|
||
"""初始化文件和字段的关联关系"""
|
||
cursor = conn.cursor()
|
||
|
||
print("\n" + "="*60)
|
||
print("开始建立文件和字段的关联关系...")
|
||
print("="*60)
|
||
|
||
# 只关联输出字段(field_type=2)
|
||
output_fields = {k: v for k, v in field_ids.items()
|
||
if any(f['field_code'] == k and f['field_type'] == 2 for f in FIELDS)}
|
||
|
||
for field_code, field_id in output_fields.items():
|
||
# 检查关联关系是否已存在
|
||
check_sql = """
|
||
SELECT id FROM f_polic_file_field
|
||
WHERE tenant_id = %s AND filed_id = %s AND file_id = %s
|
||
"""
|
||
cursor.execute(check_sql, (TENANT_ID, field_id, file_config_id))
|
||
existing = cursor.fetchone()
|
||
|
||
if existing:
|
||
print(f"关联关系已存在: 文件ID {file_config_id} <-> 字段ID {field_id} ({field_code})")
|
||
else:
|
||
insert_sql = """
|
||
INSERT INTO f_polic_file_field
|
||
(tenant_id, filed_id, file_id, created_time, created_by, updated_time, updated_by, state)
|
||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
|
||
"""
|
||
cursor.execute(insert_sql, (
|
||
TENANT_ID,
|
||
field_id,
|
||
file_config_id,
|
||
CURRENT_TIME,
|
||
CREATED_BY,
|
||
CURRENT_TIME,
|
||
CREATED_BY,
|
||
0 # state: 0表示未启用,1表示启用
|
||
))
|
||
field_name = next(f['name'] for f in FIELDS if f['field_code'] == field_code)
|
||
print(f"✓ 建立关联: {field_name} ({field_code})")
|
||
|
||
conn.commit()
|
||
|
||
|
||
def main():
|
||
"""主函数"""
|
||
try:
|
||
# 连接数据库
|
||
conn = pymysql.connect(**DB_CONFIG)
|
||
print("数据库连接成功!\n")
|
||
|
||
# 初始化字段
|
||
field_ids = init_fields(conn)
|
||
|
||
# 初始化文件配置
|
||
file_config_id = init_file_config(conn)
|
||
|
||
# 建立关联关系
|
||
init_file_field_relations(conn, file_config_id, field_ids)
|
||
|
||
print("\n" + "="*60)
|
||
print("初始化完成!")
|
||
print("="*60)
|
||
print(f"\n文件配置ID: {file_config_id}")
|
||
print(f"创建的字段数量: {len(field_ids)}")
|
||
print(f"建立的关联关系数量: {len([f for f in FIELDS if f['field_type'] == 2])}")
|
||
|
||
# 输出字段默认值信息
|
||
print("\n字段默认值配置:")
|
||
for field in FIELDS:
|
||
if field['field_type'] == 2:
|
||
print(f" - {field['name']} ({field['field_code']}): {field['default_value']}")
|
||
|
||
conn.close()
|
||
|
||
except Exception as e:
|
||
print(f"\n错误: {e}")
|
||
import traceback
|
||
traceback.print_exc()
|
||
|
||
|
||
if __name__ == '__main__':
|
||
main()
|