diff --git a/main.py b/main.py index c935ce3..3ef92a0 100644 --- a/main.py +++ b/main.py @@ -567,6 +567,11 @@ async def reorder_programs(data: dict): raise HTTPException(status_code=400, detail='缺少参数') with get_db(write=True) as conn: cursor = conn.cursor() + # 1. 先将该批次所有节目的 order_num 清空,避免唯一约束冲突 + cursor.execute( + 'UPDATE programs SET order_num = NULL WHERE batch_id = ?', (batch_id,) + ) + # 2. 按新顺序重新赋值 for idx, sn in enumerate(sn_list, start=1): cursor.execute( 'UPDATE programs SET order_num = ? WHERE sn = ? AND batch_id = ?', @@ -901,10 +906,10 @@ async def get_aggregate(batch_id: int | None = Query(None)): f""" SELECT t.program_sn, - AVG(CASE WHEN t.final_total > 0 THEN t.final_total ELSE NULL END) AS avg_score, - MAX(CASE WHEN t.final_total > 0 THEN t.final_total ELSE NULL END) AS max_score, - MIN(CASE WHEN t.final_total > 0 THEN t.final_total ELSE NULL END) AS min_score, - COUNT(CASE WHEN t.final_total > 0 THEN 1 ELSE NULL END) AS scored_count + AVG(CASE WHEN t.has_score THEN t.final_total ELSE NULL END) AS avg_score, + MAX(CASE WHEN t.has_score THEN t.final_total ELSE NULL END) AS max_score, + MIN(CASE WHEN t.has_score THEN t.final_total ELSE NULL END) AS min_score, + COUNT(CASE WHEN t.has_score THEN 1 ELSE NULL END) AS scored_count FROM ( SELECT program_sn, @@ -912,7 +917,10 @@ async def get_aggregate(batch_id: int | None = Query(None)): COALESCE(admin_total_score, COALESCE(content_score,0) + COALESCE(stage_score,0) + COALESCE(art_score,0) + COALESCE(overall_score,0) - ) AS final_total + ) AS final_total, + (admin_total_score IS NOT NULL OR + content_score IS NOT NULL OR stage_score IS NOT NULL OR + art_score IS NOT NULL OR overall_score IS NOT NULL) AS has_score FROM scores WHERE program_sn IN ({placeholders}) ) t @@ -921,13 +929,10 @@ async def get_aggregate(batch_id: int | None = Query(None)): sn_list, ) - # 将结果转换为字典,key为program_sn,值为字典 stats = {} for row in cursor.fetchall(): stats[row['program_sn']] = { - 'avg_score': row['avg_score'] - if row['avg_score'] is not None and row['scored_count'] > 0 - else None, + 'avg_score': row['avg_score'] if row['avg_score'] is not None else None, 'max_score': row['max_score'] or 0, 'min_score': row['min_score'] or 0, 'scored_count': row['scored_count'] or 0, @@ -937,7 +942,7 @@ async def get_aggregate(batch_id: int | None = Query(None)): for prog in programs: stat = stats.get( prog['sn'], - {'avg_score': 0, 'max_score': 0, 'min_score': 0, 'scored_count': 0}, + {'avg_score': None, 'max_score': 0, 'min_score': 0, 'scored_count': 0}, ) results.append( { @@ -1029,25 +1034,36 @@ async def print_program_score(program_sn: str): scores_data, all_scores = {}, [] for judge in judges: score = conn.execute( - 'SELECT content_score, stage_score, art_score, overall_score, comment FROM scores WHERE judge_id = ? AND program_sn = ?', + """SELECT content_score, stage_score, art_score, overall_score, + admin_total_score, comment + FROM scores WHERE judge_id = ? AND program_sn = ?""", (judge['id'], program_sn), ).fetchone() total = 0 + has_score = False comment = '' if score: - total = 0 - if score['content_score'] is not None: - total += score['content_score'] - if score['stage_score'] is not None: - total += score['stage_score'] - if score['art_score'] is not None: - total += score['art_score'] - if score['overall_score'] is not None: - total += score['overall_score'] - if total == 0 and score['content_score'] is not None: - total = score['content_score'] + # 优先使用管理员总分 + if score['admin_total_score'] is not None: + total = score['admin_total_score'] + has_score = True + else: + # 计算明细分之和 + if score['content_score'] is not None: + total += score['content_score'] + has_score = True + if score['stage_score'] is not None: + total += score['stage_score'] + has_score = True + if score['art_score'] is not None: + total += score['art_score'] + has_score = True + if score['overall_score'] is not None: + total += score['overall_score'] + has_score = True comment = score['comment'] or '' - if total > 0: + # 只有已评分的评委才计入统计(包括0分) + if has_score: all_scores.append(total) scores_data[judge['name']] = {'total': total, 'comment': comment} max_score = max(all_scores) if all_scores else 0 @@ -1160,7 +1176,7 @@ def render_print_blank_template(program, judges, criteria, titles):