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