From 01b3849309648a342f18bd272c32ad98e39d69d3 Mon Sep 17 00:00:00 2001 From: darkclip Date: Fri, 29 May 2026 14:22:11 +0800 Subject: [PATCH] update --- main.py | 82 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 31 deletions(-) diff --git a/main.py b/main.py index 1c9bb0e..c935ce3 100644 --- a/main.py +++ b/main.py @@ -534,8 +534,7 @@ async def create_program(program: ProgramCreate): cursor.execute( """ INSERT INTO programs (sn, name, unit, performer, category, batch_id, order_num, created_at) - SELECT ?, ?, ?, ?, ?, ?, COALESCE(MAX(order_num), 0) + 1, ? - FROM programs WHERE batch_id = ? + VALUES (?, ?, ?, ?, ?, ?, (SELECT COALESCE(MAX(order_num), 0) + 1 FROM programs WHERE batch_id = ?), ?) """, ( program.sn, @@ -559,6 +558,23 @@ async def create_program(program: ProgramCreate): return {'success': True} +@app.post('/api/programs/reorder', dependencies=[Depends(require_admin)]) +async def reorder_programs(data: dict): + """批量更新节目顺序,data: {'batch_id': 1, 'sn_list': ['P001', 'P002', ...]}""" + batch_id = data.get('batch_id') + sn_list = data.get('sn_list', []) + if not batch_id or not sn_list: + raise HTTPException(status_code=400, detail='缺少参数') + with get_db(write=True) as conn: + cursor = conn.cursor() + for idx, sn in enumerate(sn_list, start=1): + cursor.execute( + 'UPDATE programs SET order_num = ? WHERE sn = ? AND batch_id = ?', + (idx, sn, batch_id), + ) + return {'success': True} + + @app.post('/api/programs/import', dependencies=[Depends(require_admin)]) async def import_programs( request: Request, batch_id: int = Query(...), file: UploadFile = File(...) @@ -579,11 +595,15 @@ async def import_programs( with get_db(write=True) as conn: cursor = conn.cursor() cursor.execute( - 'SELECT MAX(order_num) FROM programs WHERE batch_id = ?', (batch_id,) + 'SELECT COALESCE(MAX(order_num), 0) FROM programs WHERE batch_id = ?', + (batch_id,), ) - max_order_result = cursor.fetchone()[0] - start_order = max_order_result if max_order_result is not None else 0 - order_counter = 0 + max_order = cursor.fetchone()[0] + next_order = max_order + 1 + + inserted_count = 0 + updated_count = 0 + for _, row in df.iterrows(): sn = escape_html(row['节目编号']) name = escape_html(row['节目名称']) @@ -598,23 +618,25 @@ async def import_programs( if not sn or not name: continue - order_counter += 1 - current_order = start_order + order_counter + # 检查节目是否已存在 cursor.execute('SELECT sn FROM programs WHERE sn = ?', (sn,)) - if cursor.fetchone(): + exists = cursor.fetchone() + + if exists: + # 更新节目信息,保留原有 order_num cursor.execute( - 'UPDATE programs SET name=?, unit=?, performer=?, category=?, batch_id=?, order_num=? WHERE sn=?', - (name, unit, performer, category, batch_id, current_order, sn), + """UPDATE programs + SET name = ?, unit = ?, performer = ?, category = ?, batch_id = ? + WHERE sn = ?""", + (name, unit, performer, category, batch_id, sn), ) - cursor.execute('SELECT id FROM judges') - for judge in cursor.fetchall(): - cursor.execute( - 'INSERT OR IGNORE INTO scores (judge_id, program_sn, updated_at) VALUES (?, ?, ?)', - (judge['id'], sn, datetime.now()), - ) + updated_count += 1 else: + # 新增节目,分配新的 order_num cursor.execute( - 'INSERT OR IGNORE INTO programs (sn, name, unit, performer, category, batch_id, order_num, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)', + """INSERT INTO programs + (sn, name, unit, performer, category, batch_id, order_num, created_at) + VALUES (?, ?, ?, ?, ?, ?, ?, ?)""", ( sn, name, @@ -622,10 +644,14 @@ async def import_programs( performer, category, batch_id, - current_order, + next_order, datetime.now(), ), ) + next_order += 1 + inserted_count += 1 + + # 为新节目初始化所有评委的评分记录(与原逻辑一致) cursor.execute('SELECT id FROM judges') for judge in cursor.fetchall(): cursor.execute( @@ -633,7 +659,10 @@ async def import_programs( (judge['id'], sn, datetime.now()), ) - return {'success': True, 'message': f'成功导入/更新 {df.shape[0]} 个节目'} + return { + 'success': True, + 'message': f'导入完成:新增 {inserted_count} 个节目,更新 {updated_count} 个节目', + } except Exception as exc: raise HTTPException( status_code=400, @@ -1375,20 +1404,11 @@ HTML_TEMPLATE = """ -
-

📊 获奖名单预览

-
- -