优化数据库查询逻辑,移除对tenant_id的限制,确保在获取文件配置和字段信息时不再依赖tenant_id。同时,更新文档服务和字段服务的初始化逻辑,从环境变量中读取数据库和MinIO配置,增强了配置的灵活性和可维护性。
This commit is contained in:
parent
2563e7fc74
commit
672dd2e516
87
app.py
87
app.py
@ -337,11 +337,10 @@ def get_file_configs():
|
|||||||
sql = """
|
sql = """
|
||||||
SELECT id, name, file_path
|
SELECT id, name, file_path
|
||||||
FROM f_polic_file_config
|
FROM f_polic_file_config
|
||||||
WHERE tenant_id = %s
|
WHERE state = 1
|
||||||
AND state = 1
|
|
||||||
ORDER BY name
|
ORDER BY name
|
||||||
"""
|
"""
|
||||||
cursor.execute(sql, (document_service.tenant_id,))
|
cursor.execute(sql)
|
||||||
configs = cursor.fetchall()
|
configs = cursor.fetchall()
|
||||||
|
|
||||||
file_configs = []
|
file_configs = []
|
||||||
@ -736,10 +735,9 @@ def get_document_by_task():
|
|||||||
SELECT file_id, file_name
|
SELECT file_id, file_name
|
||||||
FROM f_polic_task_file
|
FROM f_polic_task_file
|
||||||
WHERE task_id = %s
|
WHERE task_id = %s
|
||||||
AND tenant_id = %s
|
|
||||||
AND state = 1
|
AND state = 1
|
||||||
"""
|
"""
|
||||||
cursor.execute(sql, (task_id, document_service.tenant_id))
|
cursor.execute(sql, (task_id,))
|
||||||
task_files = cursor.fetchall()
|
task_files = cursor.fetchall()
|
||||||
|
|
||||||
if task_files:
|
if task_files:
|
||||||
@ -856,35 +854,35 @@ def get_template_field_relations():
|
|||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
SELECT id, name, template_code
|
SELECT id, name, template_code
|
||||||
FROM f_polic_file_config
|
FROM f_polic_file_config
|
||||||
WHERE tenant_id = %s AND state = 1
|
WHERE state = 1
|
||||||
ORDER BY name
|
ORDER BY name
|
||||||
""", (document_service.tenant_id,))
|
""")
|
||||||
templates = cursor.fetchall()
|
templates = cursor.fetchall()
|
||||||
|
|
||||||
# 获取所有启用的输入字段
|
# 获取所有启用的输入字段
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
SELECT id, name, filed_code, field_type
|
SELECT id, name, filed_code, field_type
|
||||||
FROM f_polic_field
|
FROM f_polic_field
|
||||||
WHERE tenant_id = %s AND field_type = 1 AND state = 1
|
WHERE field_type = 1 AND state = 1
|
||||||
ORDER BY name
|
ORDER BY name
|
||||||
""", (document_service.tenant_id,))
|
""")
|
||||||
input_fields = cursor.fetchall()
|
input_fields = cursor.fetchall()
|
||||||
|
|
||||||
# 获取所有启用的输出字段
|
# 获取所有启用的输出字段
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
SELECT id, name, filed_code, field_type
|
SELECT id, name, filed_code, field_type
|
||||||
FROM f_polic_field
|
FROM f_polic_field
|
||||||
WHERE tenant_id = %s AND field_type = 2 AND state = 1
|
WHERE field_type = 2 AND state = 1
|
||||||
ORDER BY name
|
ORDER BY name
|
||||||
""", (document_service.tenant_id,))
|
""")
|
||||||
output_fields = cursor.fetchall()
|
output_fields = cursor.fetchall()
|
||||||
|
|
||||||
# 获取现有的关联关系
|
# 获取现有的关联关系
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
SELECT file_id, filed_id
|
SELECT file_id, filed_id
|
||||||
FROM f_polic_file_field
|
FROM f_polic_file_field
|
||||||
WHERE tenant_id = %s AND state = 1
|
WHERE state = 1
|
||||||
""", (document_service.tenant_id,))
|
""")
|
||||||
relations = cursor.fetchall()
|
relations = cursor.fetchall()
|
||||||
|
|
||||||
# 构建关联关系映射 (file_id -> list of filed_id)
|
# 构建关联关系映射 (file_id -> list of filed_id)
|
||||||
@ -942,8 +940,8 @@ def save_template_field_relations():
|
|||||||
# 验证模板是否存在
|
# 验证模板是否存在
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
SELECT id FROM f_polic_file_config
|
SELECT id FROM f_polic_file_config
|
||||||
WHERE id = %s AND tenant_id = %s AND state = 1
|
WHERE id = %s AND state = 1
|
||||||
""", (template_id, document_service.tenant_id))
|
""", (template_id,))
|
||||||
if not cursor.fetchone():
|
if not cursor.fetchone():
|
||||||
return error_response(400, f"模板ID {template_id} 不存在或未启用")
|
return error_response(400, f"模板ID {template_id} 不存在或未启用")
|
||||||
|
|
||||||
@ -955,8 +953,8 @@ def save_template_field_relations():
|
|||||||
placeholders = ','.join(['%s'] * len(all_field_ids))
|
placeholders = ','.join(['%s'] * len(all_field_ids))
|
||||||
cursor.execute(f"""
|
cursor.execute(f"""
|
||||||
SELECT id FROM f_polic_field
|
SELECT id FROM f_polic_field
|
||||||
WHERE id IN ({placeholders}) AND tenant_id = %s AND state = 1
|
WHERE id IN ({placeholders}) AND state = 1
|
||||||
""", list(all_field_ids) + [document_service.tenant_id])
|
""", list(all_field_ids))
|
||||||
existing_field_ids = {row[0] for row in cursor.fetchall()}
|
existing_field_ids = {row[0] for row in cursor.fetchall()}
|
||||||
invalid_field_ids = all_field_ids - existing_field_ids
|
invalid_field_ids = all_field_ids - existing_field_ids
|
||||||
if invalid_field_ids:
|
if invalid_field_ids:
|
||||||
@ -965,29 +963,50 @@ def save_template_field_relations():
|
|||||||
# 删除该模板的所有现有关联关系
|
# 删除该模板的所有现有关联关系
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
DELETE FROM f_polic_file_field
|
DELETE FROM f_polic_file_field
|
||||||
WHERE file_id = %s AND tenant_id = %s
|
WHERE file_id = %s
|
||||||
""", (template_id, document_service.tenant_id))
|
""", (template_id,))
|
||||||
|
|
||||||
# 插入新的关联关系
|
# 插入新的关联关系
|
||||||
current_time = datetime.now()
|
current_time = datetime.now()
|
||||||
created_by = 655162080928945152 # 默认创建者ID
|
created_by = 655162080928945152 # 默认创建者ID
|
||||||
|
|
||||||
|
# 从环境变量读取tenant_id(如果数据库表需要),如果不需要可以设置为NULL
|
||||||
|
tenant_id = os.getenv('TENANT_ID')
|
||||||
|
|
||||||
if all_field_ids:
|
if all_field_ids:
|
||||||
insert_sql = """
|
# 如果tenant_id是必填字段,从环境变量读取;如果可以为NULL,则使用NULL
|
||||||
INSERT INTO f_polic_file_field
|
if tenant_id:
|
||||||
(tenant_id, file_id, filed_id, created_time, created_by, updated_time, updated_by, state)
|
insert_sql = """
|
||||||
VALUES (%s, %s, %s, %s, %s, %s, %s, 1)
|
INSERT INTO f_polic_file_field
|
||||||
"""
|
(tenant_id, file_id, filed_id, created_time, created_by, updated_time, updated_by, state)
|
||||||
for field_id in all_field_ids:
|
VALUES (%s, %s, %s, %s, %s, %s, %s, 1)
|
||||||
cursor.execute(insert_sql, (
|
"""
|
||||||
document_service.tenant_id,
|
for field_id in all_field_ids:
|
||||||
template_id,
|
cursor.execute(insert_sql, (
|
||||||
field_id,
|
tenant_id,
|
||||||
current_time,
|
template_id,
|
||||||
created_by,
|
field_id,
|
||||||
current_time,
|
current_time,
|
||||||
created_by
|
created_by,
|
||||||
))
|
current_time,
|
||||||
|
created_by
|
||||||
|
))
|
||||||
|
else:
|
||||||
|
# 如果tenant_id可以为NULL,使用NULL
|
||||||
|
insert_sql = """
|
||||||
|
INSERT INTO f_polic_file_field
|
||||||
|
(file_id, filed_id, created_time, created_by, updated_time, updated_by, state)
|
||||||
|
VALUES (%s, %s, %s, %s, %s, %s, 1)
|
||||||
|
"""
|
||||||
|
for field_id in all_field_ids:
|
||||||
|
cursor.execute(insert_sql, (
|
||||||
|
template_id,
|
||||||
|
field_id,
|
||||||
|
current_time,
|
||||||
|
created_by,
|
||||||
|
current_time,
|
||||||
|
created_by
|
||||||
|
))
|
||||||
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
|||||||
@ -19,25 +19,52 @@ class DocumentService:
|
|||||||
"""文档生成服务类"""
|
"""文档生成服务类"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# MinIO配置
|
# MinIO配置(从环境变量读取,不设置默认值)
|
||||||
self.minio_config = {
|
minio_endpoint = os.getenv('MINIO_ENDPOINT')
|
||||||
'endpoint': os.getenv('MINIO_ENDPOINT', 'minio.datacubeworld.com:9000'),
|
minio_access_key = os.getenv('MINIO_ACCESS_KEY')
|
||||||
'access_key': os.getenv('MINIO_ACCESS_KEY', 'JOLXFXny3avFSzB0uRA5'),
|
minio_secret_key = os.getenv('MINIO_SECRET_KEY')
|
||||||
'secret_key': os.getenv('MINIO_SECRET_KEY', 'G1BR8jStNfovkfH5ou39EmPl34E4l7dGrnd3Cz0I'),
|
minio_secure = os.getenv('MINIO_SECURE', 'true').lower() == 'true'
|
||||||
'secure': os.getenv('MINIO_SECURE', 'true').lower() == 'true'
|
minio_bucket = os.getenv('MINIO_BUCKET')
|
||||||
}
|
|
||||||
self.bucket_name = os.getenv('MINIO_BUCKET', 'finyx')
|
if not all([minio_endpoint, minio_access_key, minio_secret_key, minio_bucket]):
|
||||||
|
raise ValueError(
|
||||||
|
"MinIO配置不完整,请在.env文件中配置以下环境变量:\n"
|
||||||
|
"MINIO_ENDPOINT, MINIO_ACCESS_KEY, MINIO_SECRET_KEY, MINIO_BUCKET"
|
||||||
|
)
|
||||||
|
|
||||||
|
self.minio_config = {
|
||||||
|
'endpoint': minio_endpoint,
|
||||||
|
'access_key': minio_access_key,
|
||||||
|
'secret_key': minio_secret_key,
|
||||||
|
'secure': minio_secure
|
||||||
|
}
|
||||||
|
self.bucket_name = minio_bucket
|
||||||
|
|
||||||
|
# 数据库配置(从环境变量读取,不设置默认值)
|
||||||
|
db_host = os.getenv('DB_HOST')
|
||||||
|
db_port = os.getenv('DB_PORT')
|
||||||
|
db_user = os.getenv('DB_USER')
|
||||||
|
db_password = os.getenv('DB_PASSWORD')
|
||||||
|
db_name = os.getenv('DB_NAME')
|
||||||
|
|
||||||
|
if not all([db_host, db_port, db_user, db_password, db_name]):
|
||||||
|
raise ValueError(
|
||||||
|
"数据库配置不完整,请在.env文件中配置以下环境变量:\n"
|
||||||
|
"DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, DB_NAME"
|
||||||
|
)
|
||||||
|
|
||||||
# 数据库配置
|
|
||||||
self.db_config = {
|
self.db_config = {
|
||||||
'host': os.getenv('DB_HOST', '152.136.177.240'),
|
'host': db_host,
|
||||||
'port': int(os.getenv('DB_PORT', 5012)),
|
'port': int(db_port),
|
||||||
'user': os.getenv('DB_USER', 'finyx'),
|
'user': db_user,
|
||||||
'password': os.getenv('DB_PASSWORD', '6QsGK6MpePZDE57Z'),
|
'password': db_password,
|
||||||
'database': os.getenv('DB_NAME', 'finyx'),
|
'database': db_name,
|
||||||
'charset': 'utf8mb4'
|
'charset': 'utf8mb4'
|
||||||
}
|
}
|
||||||
self.tenant_id = 615873064429507639
|
|
||||||
|
# tenant_id 从环境变量读取(用于MinIO路径,如果不需要可以移除)
|
||||||
|
# 如果不需要tenant_id,可以设置为空字符串或从路径中移除
|
||||||
|
self.tenant_id = os.getenv('TENANT_ID', '')
|
||||||
|
|
||||||
def get_connection(self):
|
def get_connection(self):
|
||||||
"""获取数据库连接"""
|
"""获取数据库连接"""
|
||||||
@ -70,10 +97,9 @@ class DocumentService:
|
|||||||
SELECT id, name, file_path
|
SELECT id, name, file_path
|
||||||
FROM f_polic_file_config
|
FROM f_polic_file_config
|
||||||
WHERE id = %s
|
WHERE id = %s
|
||||||
AND tenant_id = %s
|
|
||||||
AND state = 1
|
AND state = 1
|
||||||
"""
|
"""
|
||||||
cursor.execute(sql, (file_id, self.tenant_id))
|
cursor.execute(sql, (file_id,))
|
||||||
config = cursor.fetchone()
|
config = cursor.fetchone()
|
||||||
|
|
||||||
if config:
|
if config:
|
||||||
@ -842,7 +868,11 @@ class DocumentService:
|
|||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
# 使用日期路径组织文件,添加微秒确保唯一性
|
# 使用日期路径组织文件,添加微秒确保唯一性
|
||||||
timestamp = f"{now.strftime('%Y%m%d%H%M%S')}{now.microsecond:06d}"
|
timestamp = f"{now.strftime('%Y%m%d%H%M%S')}{now.microsecond:06d}"
|
||||||
object_name = f"{self.tenant_id}/{timestamp}/{file_name}"
|
# 如果配置了tenant_id,则在路径中包含它;否则直接使用时间戳路径
|
||||||
|
if self.tenant_id:
|
||||||
|
object_name = f"{self.tenant_id}/{timestamp}/{file_name}"
|
||||||
|
else:
|
||||||
|
object_name = f"{timestamp}/{file_name}"
|
||||||
|
|
||||||
# 上传文件
|
# 上传文件
|
||||||
client.fput_object(
|
client.fput_object(
|
||||||
|
|||||||
@ -12,15 +12,27 @@ class FieldService:
|
|||||||
"""字段服务类"""
|
"""字段服务类"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
# 从环境变量读取数据库配置,不设置默认值,确保必须通过.env文件配置
|
||||||
|
db_host = os.getenv('DB_HOST')
|
||||||
|
db_port = os.getenv('DB_PORT')
|
||||||
|
db_user = os.getenv('DB_USER')
|
||||||
|
db_password = os.getenv('DB_PASSWORD')
|
||||||
|
db_name = os.getenv('DB_NAME')
|
||||||
|
|
||||||
|
if not all([db_host, db_port, db_user, db_password, db_name]):
|
||||||
|
raise ValueError(
|
||||||
|
"数据库配置不完整,请在.env文件中配置以下环境变量:\n"
|
||||||
|
"DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, DB_NAME"
|
||||||
|
)
|
||||||
|
|
||||||
self.db_config = {
|
self.db_config = {
|
||||||
'host': os.getenv('DB_HOST', '152.136.177.240'),
|
'host': db_host,
|
||||||
'port': int(os.getenv('DB_PORT', 5012)),
|
'port': int(db_port),
|
||||||
'user': os.getenv('DB_USER', 'finyx'),
|
'user': db_user,
|
||||||
'password': os.getenv('DB_PASSWORD', '6QsGK6MpePZDE57Z'),
|
'password': db_password,
|
||||||
'database': os.getenv('DB_NAME', 'finyx'),
|
'database': db_name,
|
||||||
'charset': 'utf8mb4'
|
'charset': 'utf8mb4'
|
||||||
}
|
}
|
||||||
self.tenant_id = 615873064429507639
|
|
||||||
|
|
||||||
# 加载提示词配置文件
|
# 加载提示词配置文件
|
||||||
self.prompt_config = self._load_prompt_config()
|
self.prompt_config = self._load_prompt_config()
|
||||||
@ -137,17 +149,16 @@ class FieldService:
|
|||||||
cursor = conn.cursor(pymysql.cursors.DictCursor)
|
cursor = conn.cursor(pymysql.cursors.DictCursor)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 根据字段编码查询字段信息
|
# 根据字段编码查询字段信息(不限制tenant_id)
|
||||||
placeholders = ','.join(['%s'] * len(field_codes))
|
placeholders = ','.join(['%s'] * len(field_codes))
|
||||||
sql = f"""
|
sql = f"""
|
||||||
SELECT f.id, f.name, f.filed_code as field_code, f.field_type
|
SELECT f.id, f.name, f.filed_code as field_code, f.field_type
|
||||||
FROM f_polic_field f
|
FROM f_polic_field f
|
||||||
WHERE f.tenant_id = %s
|
WHERE f.filed_code IN ({placeholders})
|
||||||
AND f.filed_code IN ({placeholders})
|
|
||||||
AND f.field_type = 2
|
AND f.field_type = 2
|
||||||
ORDER BY f.id
|
ORDER BY f.id
|
||||||
"""
|
"""
|
||||||
cursor.execute(sql, [self.tenant_id] + field_codes)
|
cursor.execute(sql, field_codes)
|
||||||
fields = cursor.fetchall()
|
fields = cursor.fetchall()
|
||||||
|
|
||||||
# 转换为字典列表
|
# 转换为字典列表
|
||||||
@ -183,12 +194,11 @@ class FieldService:
|
|||||||
sql = """
|
sql = """
|
||||||
SELECT f.id, f.name, f.filed_code as field_code, f.field_type
|
SELECT f.id, f.name, f.filed_code as field_code, f.field_type
|
||||||
FROM f_polic_field f
|
FROM f_polic_field f
|
||||||
WHERE f.tenant_id = %s
|
WHERE f.filed_code = %s
|
||||||
AND f.filed_code = %s
|
|
||||||
AND f.field_type = 1
|
AND f.field_type = 1
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
"""
|
"""
|
||||||
cursor.execute(sql, (self.tenant_id, field_code))
|
cursor.execute(sql, (field_code,))
|
||||||
field = cursor.fetchone()
|
field = cursor.fetchone()
|
||||||
|
|
||||||
if field:
|
if field:
|
||||||
@ -224,12 +234,11 @@ class FieldService:
|
|||||||
sql_input = """
|
sql_input = """
|
||||||
SELECT f.id, f.name, f.filed_code as field_code, f.field_type
|
SELECT f.id, f.name, f.filed_code as field_code, f.field_type
|
||||||
FROM f_polic_field f
|
FROM f_polic_field f
|
||||||
WHERE f.tenant_id = %s
|
WHERE f.field_type = 1
|
||||||
AND f.field_type = 1
|
|
||||||
AND (f.filed_code = 'clue_info' OR f.filed_code = 'target_basic_info_clue')
|
AND (f.filed_code = 'clue_info' OR f.filed_code = 'target_basic_info_clue')
|
||||||
ORDER BY f.id
|
ORDER BY f.id
|
||||||
"""
|
"""
|
||||||
cursor.execute(sql_input, (self.tenant_id,))
|
cursor.execute(sql_input)
|
||||||
input_fields = cursor.fetchall()
|
input_fields = cursor.fetchall()
|
||||||
|
|
||||||
# 获取输出字段(field_type=2)
|
# 获取输出字段(field_type=2)
|
||||||
@ -239,12 +248,11 @@ class FieldService:
|
|||||||
FROM f_polic_field f
|
FROM f_polic_field f
|
||||||
INNER JOIN f_polic_file_field ff ON f.id = ff.filed_id
|
INNER JOIN f_polic_file_field ff ON f.id = ff.filed_id
|
||||||
INNER JOIN f_polic_file_config fc ON ff.file_id = fc.id
|
INNER JOIN f_polic_file_config fc ON ff.file_id = fc.id
|
||||||
WHERE f.tenant_id = %s
|
WHERE f.field_type = 2
|
||||||
AND f.field_type = 2
|
|
||||||
AND fc.state = 1
|
AND fc.state = 1
|
||||||
ORDER BY f.id
|
ORDER BY f.id
|
||||||
"""
|
"""
|
||||||
cursor.execute(sql_output, (self.tenant_id,))
|
cursor.execute(sql_output)
|
||||||
all_output_fields = cursor.fetchall()
|
all_output_fields = cursor.fetchall()
|
||||||
|
|
||||||
# 根据business_type过滤输出字段
|
# 根据business_type过滤输出字段
|
||||||
@ -252,10 +260,9 @@ class FieldService:
|
|||||||
sql_file_configs = """
|
sql_file_configs = """
|
||||||
SELECT id, name, input_data
|
SELECT id, name, input_data
|
||||||
FROM f_polic_file_config
|
FROM f_polic_file_config
|
||||||
WHERE tenant_id = %s
|
WHERE state = 1
|
||||||
AND state = 1
|
|
||||||
"""
|
"""
|
||||||
cursor.execute(sql_file_configs, (self.tenant_id,))
|
cursor.execute(sql_file_configs)
|
||||||
file_configs = cursor.fetchall()
|
file_configs = cursor.fetchall()
|
||||||
|
|
||||||
# 找到匹配business_type的文件配置ID列表
|
# 找到匹配business_type的文件配置ID列表
|
||||||
@ -277,12 +284,11 @@ class FieldService:
|
|||||||
SELECT DISTINCT f.id, f.name, f.filed_code as field_code, f.field_type
|
SELECT DISTINCT f.id, f.name, f.filed_code as field_code, f.field_type
|
||||||
FROM f_polic_field f
|
FROM f_polic_field f
|
||||||
INNER JOIN f_polic_file_field ff ON f.id = ff.filed_id
|
INNER JOIN f_polic_file_field ff ON f.id = ff.filed_id
|
||||||
WHERE f.tenant_id = %s
|
WHERE f.field_type = 2
|
||||||
AND f.field_type = 2
|
|
||||||
AND ff.file_id IN ({placeholders})
|
AND ff.file_id IN ({placeholders})
|
||||||
ORDER BY f.id
|
ORDER BY f.id
|
||||||
"""
|
"""
|
||||||
cursor.execute(sql_filtered, [self.tenant_id] + matching_file_ids)
|
cursor.execute(sql_filtered, matching_file_ids)
|
||||||
output_fields = cursor.fetchall()
|
output_fields = cursor.fetchall()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user