Files
AILab/bbit_ai/app/db/postgres/license.py
T
2025-12-08 18:11:48 +08:00

76 lines
2.4 KiB
Python

from config.minIO import get_temp_url
from config.pgDb import pg_pool
from utils import MyUtils
def insert_license_image(
created_by, file_name, resolution, size, name, oss, type, content
):
with pg_pool.getConn() as conn:
with conn.cursor() as cursor:
cursor.execute(
"""
INSERT INTO license_images (
created_by, created_at, file_name, resolution, size, name, oss,
type, content
)
VALUES (%s, NOW(), %s, %s, %s, %s, %s, %s, %s )
RETURNING id
""",
(created_by, file_name, resolution, size, name, oss, type, content),
)
new_id = cursor.fetchone()[0]
conn.commit()
return new_id
def get_license_image_list(user_id, page=1, page_size=10):
"""
获取用户已分析图片列表,带分页
"""
offset = (page - 1) * page_size
with pg_pool.getConn() as conn:
with conn.cursor() as cursor:
# 1️⃣ 查询总条数
cursor.execute(
"""
SELECT COUNT(*)
FROM license_images
WHERE created_by = %s
""",
(user_id,),
)
total = cursor.fetchone()[0]
# 2️⃣ 查询当前页数据
cursor.execute(
"""
SELECT created_at, file_name, resolution, size, name, oss, id, type, content
FROM license_images
WHERE created_by = %s
ORDER BY created_at DESC
LIMIT %s OFFSET %s
""",
(user_id, page_size, offset),
)
rows = cursor.fetchall()
result = []
for row in rows:
result.append(
{
"created_at": MyUtils.format_datetime(row[0]),
"file_name": row[1],
"resolution": row[2],
"size": round(row[3] / 1024, 2),
"name": row[4],
"oss_url": get_temp_url("image-license", row[5]),
"id": row[6],
"type": row[7],
"content": row[8],
}
)
return total, result