完善牧安云哨-后端

This commit is contained in:
BBIT-Kai
2025-12-29 16:30:36 +08:00
parent cd7aa35960
commit b9b8d30ebf
23 changed files with 1074 additions and 41 deletions
+211
View File
@@ -1,5 +1,6 @@
from hashlib import sha256
from config.minIO import get_temp_url
from config.pgDb import pg_pool
from utils.MyUtils import format_datetime, is_valid_uuid
@@ -201,3 +202,213 @@ def delete_device_db(id: str) -> int:
cursor.execute("DELETE FROM iot_users WHERE id=%s;", (id,))
conn.commit()
return cursor.rowcount
def delete_update_db(id: str) -> int:
with pg_pool.getConn() as conn:
with conn.cursor() as cursor:
cursor.execute(
"DELETE FROM iot_update WHERE id = %s;",
(id,),
)
conn.commit()
return cursor.rowcount
def get_update_list_db_page(
page: int,
page_size: int,
id=None,
code=None,
dept_id=None,
startTime=None,
endTime=None,
):
offset = (page - 1) * page_size
conditions = []
params = []
if id is not None:
conditions.append("u.id::text LIKE %s")
params.append(f"%{id}%")
# ---- 版本 / 升级代码 ----
if code is not None:
conditions.append("u.code = %s")
params.append(code)
# ---- 部门 ----
if dept_id and is_valid_uuid(dept_id):
conditions.append("u.dept_id = %s")
params.append(dept_id)
# ---- 时间过滤 ----
if startTime:
conditions.append("u.created_at >= %s")
params.append(startTime)
if endTime:
conditions.append("u.created_at <= %s")
params.append(endTime)
where_clause = " WHERE " + " AND ".join(conditions) if conditions else ""
with pg_pool.getConn() as conn:
with conn.cursor() as cursor:
# ---- 总数 ----
count_sql = f"""
SELECT COUNT(*)
FROM iot_update u
{where_clause};
"""
cursor.execute(count_sql, params)
total = cursor.fetchone()[0]
# ---- 列表 ----
list_sql = f"""
SELECT
u.id,
u.code,
u.dept_id,
sd.name AS dept_name,
u.remark,
u.oss,
u.size,
u.created_at
FROM iot_update u
LEFT JOIN sys_dept sd ON u.dept_id = sd.id
{where_clause}
ORDER BY u.created_at DESC
LIMIT %s OFFSET %s;
"""
cursor.execute(list_sql, params + [page_size, offset])
rows = cursor.fetchall()
result = []
for r in rows:
(
update_id,
code,
dept_id,
dept_name,
remark,
oss,
size,
created_at,
) = r
result.append(
{
"id": update_id,
"code": code,
"dept_id": dept_id,
"dept_name": dept_name,
"remark": remark,
"oss_url": oss,
"size": size,
"created_at": format_datetime(created_at),
}
)
return result, total
def insert_update(data: dict) -> str:
with pg_pool.getConn() as conn:
with conn.cursor() as cursor:
cursor.execute(
"""
INSERT INTO iot_update
(code, dept_id, remark, oss, size)
VALUES
(%s, %s, %s, %s, %s)
RETURNING id;
""",
(
data.get("code"),
data.get("dept_id"),
data.get("remark"),
data.get("uploadId"),
data.get("size"),
),
)
update_id = cursor.fetchone()[0]
conn.commit()
return update_id
def get_update_package(device_id: str | None = None):
"""
根据设备 ID 获取所属组织最新版本的更新包信息
返回示例:
{
"version": 1001,
"url": "https://xxx",
"notes": "更新内容描述"
}
"""
if not device_id:
return None
sql_get_dept = """
SELECT dept_id
FROM iot_users
WHERE name = %s
LIMIT 1
"""
sql_get_package = """
SELECT code, oss, remark
FROM iot_update
WHERE dept_id = %s
ORDER BY code DESC
LIMIT 1
"""
with pg_pool.getConn() as conn:
with conn.cursor() as cursor:
# 1. 查询设备所属组织
cursor.execute(sql_get_dept, (device_id,))
row = cursor.fetchone()
if not row:
return None
dept_id = row[0]
# 2. 查询该组织最新更新包
cursor.execute(sql_get_package, (dept_id,))
row = cursor.fetchone()
if not row:
return None
version, oss_path, content = row
return {
"version": version,
"url": get_temp_url("iot-update", oss_path),
"notes": content,
}
def getMaxCodeByDeptId(dept_id: str | None = None) -> int:
"""
根据组织ID获取 iot_update_package 最大 code,并在结果上加 1
返回整数,如果没有记录则返回 1
"""
if not dept_id:
return 0 # dept_id 为空直接返回初始版本号 1
sql = """
SELECT MAX(code)
FROM iot_update
WHERE dept_id = %s
"""
with pg_pool.getConn() as conn:
with conn.cursor() as cursor:
cursor.execute(sql, (dept_id,))
row = cursor.fetchone()
max_code = row[0] if row and row[0] is not None else 0
return max_code