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 image_license ( 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 image_license 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 image_license 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