后端更新
This commit is contained in:
@@ -35,34 +35,46 @@ import time
|
||||
|
||||
|
||||
def reset_all_exchange_status():
|
||||
"""将所有记录 is_finished 置为 False,并随机 position(以当前时间作为随机种子)"""
|
||||
"""将所有记录 is_finished 置为 False,
|
||||
且 gift_code == 2 的记录强制排在最后,其余随机排序
|
||||
"""
|
||||
with pg_pool.getConn() as conn:
|
||||
with conn.cursor() as cur:
|
||||
# 获取总记录数
|
||||
cur.execute("SELECT id FROM annual_meeting_exchange")
|
||||
ids = [row[0] for row in cur.fetchall()]
|
||||
# 取出 id 和 gift_code
|
||||
cur.execute("SELECT id, gift_code FROM annual_meeting_exchange")
|
||||
rows = cur.fetchall()
|
||||
|
||||
# 用当前时间戳作为随机种子
|
||||
seed = int(time.time() * 1000) # 毫秒级
|
||||
# 分组
|
||||
normal_ids = [r[0] for r in rows if r[1] != 2]
|
||||
tail_ids = [r[0] for r in rows if r[1] == 2]
|
||||
|
||||
# 随机种子
|
||||
seed = int(time.time() * 1000)
|
||||
random.seed(seed)
|
||||
|
||||
# 生成随机顺序的 position
|
||||
positions = list(range(1, len(ids) + 1))
|
||||
random.shuffle(positions)
|
||||
# 只打乱非 gift_code == 2 的部分
|
||||
random.shuffle(normal_ids)
|
||||
|
||||
# 更新每条记录
|
||||
for record_id, pos in zip(ids, positions):
|
||||
# 合并顺序:普通在前,gift_code==2 在后
|
||||
ordered_ids = normal_ids + tail_ids
|
||||
|
||||
# 依次更新 sort
|
||||
for idx, record_id in enumerate(ordered_ids, start=1):
|
||||
cur.execute(
|
||||
"""
|
||||
UPDATE annual_meeting_exchange
|
||||
SET is_finished = FALSE, sort = %s
|
||||
WHERE id = %s
|
||||
""",
|
||||
(pos, record_id),
|
||||
(idx, record_id),
|
||||
)
|
||||
|
||||
conn.commit()
|
||||
return {"updated_count": len(ids), "seed_used": seed}
|
||||
return {
|
||||
"updated_count": len(ordered_ids),
|
||||
"seed_used": seed,
|
||||
"tail_count": len(tail_ids),
|
||||
}
|
||||
|
||||
|
||||
def reset_user_status(target_user_id: str):
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
from config.minIO import get_temp_url_dict
|
||||
from config.pgDb import pg_pool
|
||||
from models.SentinelRecordRequest import SentinelRecordRequest
|
||||
from utils.MyUtils import format_datetime
|
||||
|
||||
|
||||
@@ -90,6 +89,7 @@ def get_sentinel_record_list_db_page(
|
||||
r.vehicle_image,
|
||||
r.livestock_type,
|
||||
r.livestock_source,
|
||||
r.license_plate_color,
|
||||
r.is_inspected,
|
||||
r.dept_id,
|
||||
sd.name AS dept_name,
|
||||
@@ -116,6 +116,7 @@ def get_sentinel_record_list_db_page(
|
||||
vehicle_image,
|
||||
livestock_type,
|
||||
livestock_source,
|
||||
license_plate_color,
|
||||
is_inspected,
|
||||
dept_id,
|
||||
dept_name,
|
||||
@@ -130,13 +131,14 @@ def get_sentinel_record_list_db_page(
|
||||
"license_plate": license_plate,
|
||||
"vehicle_type": vehicle_type,
|
||||
"license_plate_image": get_temp_url_dict(
|
||||
"sentinel", "license_plate", license_plate_image
|
||||
"sentinel", "vehicle_image_front", license_plate_image
|
||||
),
|
||||
"vehicle_image": get_temp_url_dict(
|
||||
"sentinel", "vehicle_image", vehicle_image
|
||||
"sentinel", "vehicle_image_side", vehicle_image
|
||||
),
|
||||
"livestock_type": livestock_type,
|
||||
"livestock_source": livestock_source,
|
||||
"license_plate_color": license_plate_color,
|
||||
"is_inspected": 1 if is_inspected else 0,
|
||||
"dept_id": dept_id,
|
||||
"dept_name": dept_name,
|
||||
@@ -161,6 +163,7 @@ def get_sentinel_record_by_id(record_id):
|
||||
r.license_plate_image,
|
||||
r.vehicle_image,
|
||||
r.livestock_type,
|
||||
r.license_plate_color,
|
||||
r.livestock_source,
|
||||
r.is_inspected,
|
||||
r.dept_id,
|
||||
@@ -188,6 +191,7 @@ def get_sentinel_record_by_id(record_id):
|
||||
license_plate_image,
|
||||
vehicle_image,
|
||||
livestock_type,
|
||||
license_plate_color,
|
||||
livestock_source,
|
||||
is_inspected,
|
||||
dept_id,
|
||||
@@ -202,12 +206,13 @@ def get_sentinel_record_by_id(record_id):
|
||||
"license_plate": license_plate,
|
||||
"vehicle_type": vehicle_type,
|
||||
"license_plate_image": get_temp_url_dict(
|
||||
"sentinel", "license_plate", license_plate_image
|
||||
"sentinel", "vehicle_image_front", license_plate_image
|
||||
),
|
||||
"vehicle_image": get_temp_url_dict(
|
||||
"sentinel", "vehicle_image", vehicle_image
|
||||
"sentinel", "vehicle_image_side", vehicle_image
|
||||
),
|
||||
"livestock_type": livestock_type,
|
||||
"license_plate_color": license_plate_color,
|
||||
"livestock_source": livestock_source,
|
||||
"is_inspected": 1 if is_inspected else 0,
|
||||
"dept_id": str(dept_id),
|
||||
@@ -333,15 +338,30 @@ def delete_sentinel_record_db(id: str) -> int:
|
||||
return cursor.rowcount
|
||||
|
||||
|
||||
def saveSentinelRecord(data: SentinelRecordRequest) -> str:
|
||||
def saveSentinelRecord(
|
||||
id: str,
|
||||
vehicle_type: str,
|
||||
vehicle_image: str,
|
||||
livestock_type: str,
|
||||
remark: str,
|
||||
dept_id: str,
|
||||
license_plate: str,
|
||||
license_plate_image: str,
|
||||
license_plate_color: int = 0,
|
||||
) -> str:
|
||||
sql = """
|
||||
INSERT INTO sentinel_records (
|
||||
id,
|
||||
vehicle_type,
|
||||
vehicle_image,
|
||||
livestock_type,
|
||||
remark,
|
||||
dept_id,
|
||||
license_plate,
|
||||
license_plate_image,
|
||||
vehicle_type,
|
||||
vehicle_image
|
||||
license_plate_color
|
||||
)
|
||||
VALUES (%s, %s, %s, %s)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||||
RETURNING id;
|
||||
"""
|
||||
|
||||
@@ -350,36 +370,17 @@ def saveSentinelRecord(data: SentinelRecordRequest) -> str:
|
||||
cursor.execute(
|
||||
sql,
|
||||
(
|
||||
data.LicensePlate,
|
||||
data.LicensePlateImage,
|
||||
data.VehicleType,
|
||||
data.VehicleImage,
|
||||
id,
|
||||
vehicle_type,
|
||||
vehicle_image,
|
||||
livestock_type,
|
||||
remark,
|
||||
dept_id,
|
||||
license_plate,
|
||||
license_plate_image,
|
||||
license_plate_color,
|
||||
),
|
||||
)
|
||||
new_id = cursor.fetchone()[0]
|
||||
conn.commit()
|
||||
return str(new_id)
|
||||
|
||||
|
||||
def update_sentinel_record(
|
||||
id: str, livestock_type: str, remark: str, dept_id: str
|
||||
) -> bool:
|
||||
"""
|
||||
根据 id 更新 sentinel_records 表中的 livestock_type 和 dept_id
|
||||
"""
|
||||
sql = """
|
||||
UPDATE sentinel_records
|
||||
SET livestock_type = %s,
|
||||
remark = %s,
|
||||
dept_id = %s,
|
||||
updated_at = now()
|
||||
WHERE id = %s
|
||||
RETURNING id;
|
||||
"""
|
||||
|
||||
with pg_pool.getConn() as conn:
|
||||
with conn.cursor() as cursor:
|
||||
cursor.execute(sql, (livestock_type, remark, dept_id, id))
|
||||
record = cursor.fetchone()
|
||||
conn.commit()
|
||||
return record is not None
|
||||
|
||||
Reference in New Issue
Block a user