增加求和采集和细节修改
This commit is contained in:
@@ -208,50 +208,60 @@ def move_config(config_id):
|
||||
"""移动配置排序"""
|
||||
try:
|
||||
direction = request.json.get('direction') # up 或 down
|
||||
print(f"[move_config] 开始移动 config_id={config_id}, direction={direction}")
|
||||
|
||||
# 获取当前配置
|
||||
current = execute_query(
|
||||
'SELECT sort_order FROM t_daily_report_config WHERE id = %s',
|
||||
(config_id,)
|
||||
# 查出所有配置,按 sort_order, id 排序
|
||||
all_configs = execute_query(
|
||||
'SELECT id, sort_order FROM t_daily_report_config ORDER BY sort_order ASC, id ASC'
|
||||
)
|
||||
if not current:
|
||||
print(f"[move_config] 所有配置数量: {len(all_configs)}")
|
||||
for idx, c in enumerate(all_configs):
|
||||
print(f" [{idx}] id={c['id']}, sort_order={c['sort_order']}")
|
||||
|
||||
# 找到当前配置的索引
|
||||
current_index = None
|
||||
for i, c in enumerate(all_configs):
|
||||
if c['id'] == config_id:
|
||||
current_index = i
|
||||
break
|
||||
|
||||
if current_index is None:
|
||||
print(f"[move_config] 未找到配置 id={config_id}")
|
||||
return jsonify({'success': False, 'message': '配置不存在'}), 404
|
||||
|
||||
current_order = current[0]['sort_order']
|
||||
print(f"[move_config] 当前配置索引: {current_index}")
|
||||
|
||||
# 计算目标索引
|
||||
if direction == 'up':
|
||||
# 找到上一个配置
|
||||
prev = execute_query(
|
||||
'SELECT id, sort_order FROM t_daily_report_config WHERE sort_order < %s ORDER BY sort_order DESC LIMIT 1',
|
||||
(current_order,)
|
||||
)
|
||||
if prev:
|
||||
# 交换排序
|
||||
execute_update(
|
||||
'UPDATE t_daily_report_config SET sort_order = %s WHERE id = %s',
|
||||
(prev[0]['sort_order'], config_id)
|
||||
)
|
||||
execute_update(
|
||||
'UPDATE t_daily_report_config SET sort_order = %s WHERE id = %s',
|
||||
(current_order, prev[0]['id'])
|
||||
)
|
||||
target_index = current_index - 1
|
||||
elif direction == 'down':
|
||||
# 找到下一个配置
|
||||
next_config = execute_query(
|
||||
'SELECT id, sort_order FROM t_daily_report_config WHERE sort_order > %s ORDER BY sort_order ASC LIMIT 1',
|
||||
(current_order,)
|
||||
)
|
||||
if next_config:
|
||||
# 交换排序
|
||||
execute_update(
|
||||
'UPDATE t_daily_report_config SET sort_order = %s WHERE id = %s',
|
||||
(next_config[0]['sort_order'], config_id)
|
||||
)
|
||||
execute_update(
|
||||
'UPDATE t_daily_report_config SET sort_order = %s WHERE id = %s',
|
||||
(current_order, next_config[0]['id'])
|
||||
)
|
||||
target_index = current_index + 1
|
||||
else:
|
||||
return jsonify({'success': False, 'message': '无效的移动方向'}), 400
|
||||
|
||||
# 边界检查
|
||||
if target_index < 0 or target_index >= len(all_configs):
|
||||
print(f"[move_config] 已到边界,无法移动 target_index={target_index}")
|
||||
return jsonify({'success': True, 'message': '已到边界'})
|
||||
|
||||
# 在内存数组中交换位置
|
||||
all_configs[current_index], all_configs[target_index] = all_configs[target_index], all_configs[current_index]
|
||||
|
||||
print(f"[move_config] 交换后顺序: {[c['id'] for c in all_configs]}")
|
||||
|
||||
# 全量重排:按新顺序给每个配置重新赋值 sort_order
|
||||
for idx, cfg in enumerate(all_configs):
|
||||
new_sort = idx + 1
|
||||
print(f" 更新 id={cfg['id']}: sort_order {cfg['sort_order']} -> {new_sort}")
|
||||
execute_update(
|
||||
'UPDATE t_daily_report_config SET sort_order = %s WHERE id = %s',
|
||||
(new_sort, cfg['id'])
|
||||
)
|
||||
|
||||
print("[move_config] 重排完成")
|
||||
return jsonify({'success': True, 'message': '移动成功'})
|
||||
except Exception as e:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
print(f"[move_config] 异常: {e}")
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
|
||||
Reference in New Issue
Block a user