调整数据库表名,增加催青阶段分析演示模块

This commit is contained in:
BBIT-Kai
2026-01-04 16:06:13 +08:00
parent 209d087fa5
commit e2982b141d
16 changed files with 589 additions and 34 deletions
+1
View File
@@ -1,6 +1,7 @@
from .aimessage import *
from .aiprofile import *
from .aisession import *
from .image_ysa import *
from .iot import *
from .knowledge import *
from .license import *
+1 -1
View File
@@ -41,7 +41,7 @@ def get_all_ai_bot(user_id: str, module: str) -> List[Dict]:
with pg_pool.getConn() as conn:
with conn.cursor() as cur:
# 查询用户角色
cur.execute("SELECT roles FROM users WHERE id = %s", (user_id,))
cur.execute("SELECT roles FROM sys_users WHERE id = %s", (user_id,))
role_row = cur.fetchone()
if not role_row:
return [] # 用户不存在
+122
View File
@@ -0,0 +1,122 @@
import json
from config.minIO import get_temp_url
from config.pgDb import pg_pool
from utils import MyUtils
def insert_ysa_image(
file_name,
resolution,
size,
cocoon_count,
max_confidence,
min_confidence,
average_confidence,
other_info,
preprocess_time_ms,
inference_time_ms,
postprocess_time_ms,
name,
image_pre,
image_after,
created_by,
):
with pg_pool.getConn() as conn:
with conn.cursor() as cursor:
other_info = json.dumps(other_info)
cursor.execute(
"""
INSERT INTO image_sca (
upload_datetime, file_name, resolution, size, cocoon_count, max_confidence, min_confidence,
average_confidence, other_info, preprocess_time_ms, inference_time_ms, postprocess_time_ms, name, image_pre, image_after, created_by
)
VALUES (NOW(), %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s )
RETURNING id
""",
(
file_name,
resolution,
size,
cocoon_count,
max_confidence,
min_confidence,
average_confidence,
other_info,
preprocess_time_ms,
inference_time_ms,
postprocess_time_ms,
name,
image_pre,
image_after,
created_by,
),
)
new_id = cursor.fetchone()[0]
conn.commit()
return new_id
def get_ysa_image_list(user_id, name, page=1, page_size=10):
"""
获取用户已分析图片列表,带分页
"""
offset = (page - 1) * page_size
with pg_pool.getConn() as conn:
with conn.cursor() as cursor:
# 1️⃣ 查询总条数
# ✅ 改进版:支持 name 为空时统计全部,不为空时模糊统计
cursor.execute(
"""
SELECT COUNT(*)
FROM image_ysa
WHERE created_by = %s
AND (%s = '' OR name LIKE '%%' || %s || '%%')
""",
(user_id, name, name),
)
total = cursor.fetchone()[0]
# 2️⃣ 查询当前页数据
# ✅ 改进版
cursor.execute(
"""
SELECT id, name, upload_datetime, file_name, image_pre, image_after, resolution,
size, silkworm_count, max_confidence, min_confidence, average_confidence, other_info, preprocess_time_ms, inference_time_ms, postprocess_time_ms
FROM image_ysa
WHERE created_by = %s
AND (%s = '' OR name LIKE '%%' || %s || '%%')
ORDER BY upload_datetime DESC
LIMIT %s OFFSET %s
""",
(user_id, name, name, page_size, offset),
)
rows = cursor.fetchall()
result = []
for row in rows:
result.append(
{
"id": row[0],
"name": row[1],
"upload_datetime": MyUtils.format_datetime(row[2]),
"file_name": row[3],
"image_pre": get_temp_url("image-ysa", "raw/" + row[4]),
"image_after": get_temp_url("image-ysa", "ai/" + row[5]),
"resolution": row[6],
"size": MyUtils.safe_round(row[7] / 1024, 2),
"silkworm_count": row[8],
"max_confidence": row[9],
"min_confidence": row[10],
"average_confidence": row[11],
"other_info": row[12],
"preprocess_time_ms": MyUtils.safe_round(row[13], 4),
"inference_time_ms": MyUtils.safe_round(row[14], 4),
"postprocess_time_ms": MyUtils.safe_round(row[15], 4),
}
)
return total, result
+3 -3
View File
@@ -10,7 +10,7 @@ def insert_license_image(
with conn.cursor() as cursor:
cursor.execute(
"""
INSERT INTO license_images (
INSERT INTO image_license (
created_by, created_at, file_name, resolution, size, name, oss,
type, content
)
@@ -36,7 +36,7 @@ def get_license_image_list(user_id, page=1, page_size=10):
cursor.execute(
"""
SELECT COUNT(*)
FROM license_images
FROM image_license
WHERE created_by = %s
""",
(user_id,),
@@ -47,7 +47,7 @@ def get_license_image_list(user_id, page=1, page_size=10):
cursor.execute(
"""
SELECT created_at, file_name, resolution, size, name, oss, id, type, content
FROM license_images
FROM image_license
WHERE created_by = %s
ORDER BY created_at DESC
LIMIT %s OFFSET %s
+3 -3
View File
@@ -27,7 +27,7 @@ def insert_sca_image(
other_info = json.dumps(other_info)
cursor.execute(
"""
INSERT INTO sca_images (
INSERT INTO image_sca (
upload_datetime, file_name, resolution, size, cocoon_count, max_confidence, min_confidence,
average_confidence, other_info, preprocess_time_ms, inference_time_ms, postprocess_time_ms, name, image_pre, image_after, created_by
)
@@ -70,7 +70,7 @@ def get_sca_image_list(user_id, name, page=1, page_size=10):
cursor.execute(
"""
SELECT COUNT(*)
FROM sca_images
FROM image_sca
WHERE created_by = %s
AND (%s = '' OR name LIKE '%%' || %s || '%%')
""",
@@ -85,7 +85,7 @@ def get_sca_image_list(user_id, name, page=1, page_size=10):
"""
SELECT id, name, upload_datetime, file_name, image_pre, image_after, resolution,
size, cocoon_count, max_confidence, min_confidence, average_confidence, other_info, preprocess_time_ms, inference_time_ms, postprocess_time_ms
FROM sca_images
FROM image_sca
WHERE created_by = %s
AND (%s = '' OR name LIKE '%%' || %s || '%%')
ORDER BY upload_datetime DESC
+4 -4
View File
@@ -17,7 +17,7 @@ def get_sca_video_list(name, page=1, page_size=10):
cursor.execute(
"""
SELECT COUNT(*)
FROM sca_videos
FROM video_sca
WHERE (%s = '' OR name LIKE '%%' || %s || '%%')
""",
(name, name),
@@ -30,7 +30,7 @@ def get_sca_video_list(name, page=1, page_size=10):
SELECT id, name, raw_object_name, ai_object_name, duration, size, video_codec, audio_codec,
overall_bit_rate, resolution, sc_analysis_time, sc_analysis_total_count, sc_analysis_max_count,
sc_analysis_primary_type, sc_analysis_secondary_type, other_info, created_at
FROM sca_videos
FROM video_sca
WHERE (%s = '' OR name LIKE '%%' || %s || '%%')
ORDER BY created_at DESC
LIMIT %s OFFSET %s
@@ -67,7 +67,7 @@ def get_sca_video_list(name, page=1, page_size=10):
return total, result
def get_sca_video_details(v_id):
def get_video_sca_details(v_id):
"""
获取指定视频的分析明细列表
"""
@@ -76,7 +76,7 @@ def get_sca_video_details(v_id):
cursor.execute(
"""
SELECT id, v_id, time_stamp, other_info
FROM sca_video_details
FROM video_sca_details
WHERE v_id = %s
ORDER BY time_stamp ASC
""",
+11 -9
View File
@@ -446,7 +446,7 @@ def get_user_list_db_page(
with conn.cursor() as cursor:
# ---- 统计总数 ----
count_sql = f"SELECT COUNT(*) FROM users u {where_clause};"
count_sql = f"SELECT COUNT(*) FROM sys_users u {where_clause};"
cursor.execute(count_sql, params)
total = cursor.fetchone()[0]
@@ -461,7 +461,7 @@ def get_user_list_db_page(
u.dept_id,
d.name AS dept_name,
u.created_at
FROM users u
FROM sys_users u
LEFT JOIN sys_dept d ON u.dept_id = d.id
{where_clause}
ORDER BY u.created_at DESC
@@ -511,7 +511,9 @@ def get_user_list_db_page(
def db_user_name_exists(username: str) -> bool:
with pg_pool.getConn() as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT COUNT(*) FROM users WHERE email = %s;", (username,))
cursor.execute(
"SELECT COUNT(*) FROM sys_users WHERE email = %s;", (username,)
)
return cursor.fetchone()[0] > 0
@@ -521,7 +523,7 @@ def insert_user(data: dict) -> str:
with conn.cursor() as cursor:
cursor.execute(
"""
INSERT INTO users (username, email, phone, is_active, dept_id, password_hash)
INSERT INTO sys_users (username, email, phone, is_active, dept_id, password_hash)
VALUES (%s, %s, %s, %s, %s, %s) RETURNING id;
""",
(
@@ -578,7 +580,7 @@ def patch_user_db(id: str, data: dict) -> int:
# 如果有需要更新的字段
if fields:
sql = f"UPDATE users SET {', '.join(fields)} WHERE id = %s"
sql = f"UPDATE sys_users SET {', '.join(fields)} WHERE id = %s"
params.append(id)
cursor.execute(sql, tuple(params))
@@ -603,7 +605,7 @@ def delete_user_db(id: str) -> int:
with pg_pool.getConn() as conn:
with conn.cursor() as cursor:
cursor.execute("DELETE FROM sys_user_role WHERE user_id=%s;", (id,))
cursor.execute("DELETE FROM users WHERE id=%s;", (id,))
cursor.execute("DELETE FROM sys_users WHERE id=%s;", (id,))
conn.commit()
return cursor.rowcount
@@ -882,7 +884,7 @@ def get_dept_ids_by_user_id(user_id: UUID) -> list:
# 第一步:通过 user_id 查找其所属的 dept_id
with pg_pool.getConn() as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT dept_id FROM users WHERE id = %s", (user_id,))
cursor.execute("SELECT dept_id FROM sys_users WHERE id = %s", (user_id,))
dept_id = cursor.fetchone()
if not dept_id:
@@ -914,7 +916,7 @@ def get_dept_ids_by_user_id(user_id: UUID) -> list:
# 第一步:通过 user_id 查找其所属的 dept_id
with pg_pool.getConn() as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT dept_id FROM users WHERE id = %s", (user_id,))
cursor.execute("SELECT dept_id FROM sys_users WHERE id = %s", (user_id,))
dept_id = cursor.fetchone()
if not dept_id:
@@ -946,7 +948,7 @@ def get_dept_id_by_user_id(user_id: str) -> str:
# 通过 user_id 查找其所属的 dept_id
with pg_pool.getConn() as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT dept_id FROM users WHERE id = %s", (user_id,))
cursor.execute("SELECT dept_id FROM sys_users WHERE id = %s", (user_id,))
dept_id = cursor.fetchone()
dept_id = dept_id[0]
return str(dept_id)
+2 -2
View File
@@ -12,7 +12,7 @@ def get_ticket_image_list(user_id):
moisture_content, cocoon_weight, defective_pupa_count,
fresh_shell_weight, sample_count, barcode, oss,
net_weight_total, evaluator, reviewer,id ,dead_pupa_count
FROM ticket_images
FROM image_ticket
WHERE created_by = %s
""",
(user_id,),
@@ -66,7 +66,7 @@ def insert_ticket_image(
with conn.cursor() as cursor:
cursor.execute(
"""
INSERT INTO ticket_images (
INSERT INTO image_ticket (
created_by, file_name, resolution, size, name,
moisture_content, cocoon_weight, defective_pupa_count, dead_pupa_count,
fresh_shell_weight, sample_count, barcode, oss,