后端更新
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):
|
||||
|
||||
Reference in New Issue
Block a user