This commit is contained in:
hl committed 2026-05-29 14:22:11 +08:00
1 parent 2971483764
commit 01b3849309
1 file changed
+51 -31
+51 -31
View File
@@ -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 = """
<button class="btn btn-primary btn-sm" onclick="saveAwardNames()">保存名称</button>
</div>
</div>
<div class="settings-panel">
<h3>📊 获奖名单预览</h3>
<div id="awardPreview"></div>
<div class="print-buttons">
<button class="btn btn-success btn-sm" onclick="printAward('first')">打印一等奖名单</button>
<button class="btn btn-success btn-sm" onclick="printAward('second')">打印二等奖名单</button>
<button class="btn btn-success btn-sm" onclick="printAward('third')">打印三等奖名单</button>
</div>
</div>
</div>
<!-- 打印中心页面 -->
<div id="printTabContent" style="display:none;">
<div class="settings-panel"><h3>🏆 获奖名单打印</h3>
<div class="settings-panel"><h3>🏆 获奖名单预览</h3>
<div id="awardPreviewPrint"></div>
<div class="print-buttons">
<button class="btn btn-success btn-sm" onclick="printAward('first')">打印一等奖名单</button>
@@ -1959,7 +1979,7 @@ HTML_TEMPLATE = """
html += `<tr><td style="text-align:center;">${idx + 1}</td><td style="text-align:center;">${p.sn}</td><td style="text-align:left;">${p.name}</td><td style="text-align:left;">${p.unit}</td><td style="text-align:left;">${p.performer}</td><td style="text-align:center;">${p.category}</td><td style="text-align:center;">${avgDisplay}</td><td style="text-align:center;">${award}</td></tr>`;
});
html += '</tbody></table></div>';
const containers = ['awardPreview', 'awardPreviewPrint'];
const containers = ['awardPreviewPrint'];
containers.forEach(id => {
const el = document.getElementById(id);
if (el) el.innerHTML = html;