275 lines
11 KiB
Python
275 lines
11 KiB
Python
"""
|
|
配置管理 API
|
|
"""
|
|
import json
|
|
from datetime import datetime
|
|
from flask import request, jsonify
|
|
from lib.db import execute_query, execute_update, execute_insert
|
|
from . import config_bp
|
|
|
|
|
|
@config_bp.route('/config', methods=['GET'])
|
|
def get_configs():
|
|
"""获取所有配置"""
|
|
try:
|
|
configs = execute_query(
|
|
'SELECT * FROM t_daily_report_config ORDER BY sort_order ASC, id DESC'
|
|
)
|
|
|
|
# 解析 JSON 字段
|
|
for config in configs:
|
|
config['selected_fields'] = json.loads(config['selected_fields'])
|
|
config['sum_fields'] = json.loads(config['sum_fields']) if config['sum_fields'] else []
|
|
config['time_periods'] = json.loads(config['time_periods']) if config.get('time_periods') else {}
|
|
|
|
return jsonify({'success': True, 'data': configs})
|
|
except Exception as e:
|
|
return jsonify({'success': False, 'message': str(e)}), 500
|
|
|
|
|
|
@config_bp.route('/config', methods=['POST'])
|
|
def create_config():
|
|
"""创建配置"""
|
|
try:
|
|
data = request.json
|
|
config_name = data.get('config_name')
|
|
split_type = data.get('split_type')
|
|
split_value = data.get('split_value')
|
|
split_name = data.get('split_name', '')
|
|
selected_fields = data.get('selected_fields', [])
|
|
sum_fields = data.get('sum_fields', [])
|
|
time_periods = data.get('time_periods', {})
|
|
merge_telecom = 1 if data.get('merge_telecom') else 0
|
|
telecom_vehicle_no = data.get('telecom_vehicle_no', '')
|
|
field_custom_names = data.get('field_custom_names', {})
|
|
show_monthly_total = 1 if data.get('show_monthly_total') else 0
|
|
|
|
if not config_name or not split_type or not split_value:
|
|
return jsonify({'success': False, 'message': '缺少必填字段'}), 400
|
|
|
|
if not selected_fields:
|
|
return jsonify({'success': False, 'message': '请至少选择一个字段'}), 400
|
|
|
|
# 生成 ID
|
|
config_id = int(datetime.now().timestamp() * 1000)
|
|
|
|
# 获取最大排序值
|
|
max_sort = execute_query('SELECT MAX(sort_order) as max_sort FROM t_daily_report_config')
|
|
sort_order = (max_sort[0]['max_sort'] or 0) + 1
|
|
|
|
sql = """
|
|
INSERT INTO t_daily_report_config
|
|
(id, config_name, split_type, split_value, split_name, selected_fields,
|
|
sum_fields, time_periods, merge_telecom, telecom_vehicle_no, field_custom_names,
|
|
show_monthly_total, is_active, sort_order, create_time, update_time)
|
|
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, 1, %s, NOW(), NOW())
|
|
"""
|
|
|
|
params = (
|
|
config_id,
|
|
config_name,
|
|
split_type,
|
|
split_value,
|
|
split_name,
|
|
json.dumps(selected_fields, ensure_ascii=False),
|
|
json.dumps(sum_fields, ensure_ascii=False),
|
|
json.dumps(time_periods, ensure_ascii=False),
|
|
merge_telecom,
|
|
telecom_vehicle_no,
|
|
json.dumps(field_custom_names, ensure_ascii=False) if field_custom_names else None,
|
|
show_monthly_total,
|
|
sort_order
|
|
)
|
|
|
|
execute_insert(sql, params)
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'message': '配置创建成功',
|
|
'data': {'id': config_id}
|
|
})
|
|
except Exception as e:
|
|
return jsonify({'success': False, 'message': str(e)}), 500
|
|
|
|
|
|
@config_bp.route('/config/<int:config_id>', methods=['PUT'])
|
|
def update_config(config_id):
|
|
"""更新配置"""
|
|
try:
|
|
data = request.json
|
|
config_name = data.get('config_name')
|
|
split_type = data.get('split_type')
|
|
split_value = data.get('split_value')
|
|
split_name = data.get('split_name', '')
|
|
selected_fields = data.get('selected_fields', [])
|
|
sum_fields = data.get('sum_fields', [])
|
|
time_periods = data.get('time_periods', {})
|
|
merge_telecom = 1 if data.get('merge_telecom') else 0
|
|
telecom_vehicle_no = data.get('telecom_vehicle_no', '')
|
|
field_custom_names = data.get('field_custom_names', {})
|
|
show_monthly_total = 1 if data.get('show_monthly_total') else 0
|
|
|
|
sql = """
|
|
UPDATE t_daily_report_config
|
|
SET config_name = %s, split_type = %s, split_value = %s, split_name = %s,
|
|
selected_fields = %s, sum_fields = %s, time_periods = %s,
|
|
merge_telecom = %s, telecom_vehicle_no = %s, field_custom_names = %s,
|
|
show_monthly_total = %s, update_time = NOW()
|
|
WHERE id = %s
|
|
"""
|
|
|
|
params = (
|
|
config_name,
|
|
split_type,
|
|
split_value,
|
|
split_name,
|
|
json.dumps(selected_fields, ensure_ascii=False),
|
|
json.dumps(sum_fields, ensure_ascii=False),
|
|
json.dumps(time_periods, ensure_ascii=False),
|
|
merge_telecom,
|
|
telecom_vehicle_no,
|
|
json.dumps(field_custom_names, ensure_ascii=False) if field_custom_names else None,
|
|
show_monthly_total,
|
|
config_id
|
|
)
|
|
|
|
execute_update(sql, params)
|
|
|
|
return jsonify({'success': True, 'message': '配置更新成功'})
|
|
except Exception as e:
|
|
return jsonify({'success': False, 'message': str(e)}), 500
|
|
|
|
|
|
@config_bp.route('/config/<int:config_id>', methods=['DELETE'])
|
|
def delete_config(config_id):
|
|
"""删除配置"""
|
|
try:
|
|
execute_update('DELETE FROM t_daily_report_config WHERE id = %s', (config_id,))
|
|
return jsonify({'success': True, 'message': '配置删除成功'})
|
|
except Exception as e:
|
|
return jsonify({'success': False, 'message': str(e)}), 500
|
|
|
|
|
|
@config_bp.route('/config/<int:config_id>/copy', methods=['POST'])
|
|
def copy_config(config_id):
|
|
"""复制配置"""
|
|
try:
|
|
# 获取原配置
|
|
configs = execute_query('SELECT * FROM t_daily_report_config WHERE id = %s', (config_id,))
|
|
if not configs:
|
|
return jsonify({'success': False, 'message': '原配置不存在'}), 404
|
|
|
|
original = configs[0]
|
|
|
|
# 生成新 ID 和新名称
|
|
new_id = int(datetime.now().timestamp() * 1000)
|
|
new_name = f"{original['config_name']}_副本"
|
|
|
|
# 获取最大排序值
|
|
max_sort = execute_query('SELECT MAX(sort_order) as max_sort FROM t_daily_report_config')
|
|
sort_order = (max_sort[0]['max_sort'] or 0) + 1
|
|
|
|
# 插入新配置
|
|
execute_insert(
|
|
'''INSERT INTO t_daily_report_config
|
|
(id, config_name, split_type, split_value, split_name, selected_fields, sum_fields, time_periods, merge_telecom, telecom_vehicle_no, field_custom_names, show_monthly_total, is_active, sort_order, create_time, update_time)
|
|
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NOW(), NOW())''',
|
|
(
|
|
new_id,
|
|
new_name,
|
|
original['split_type'],
|
|
original['split_value'],
|
|
original['split_name'],
|
|
original['selected_fields'],
|
|
original.get('sum_fields', ''),
|
|
original.get('time_periods', ''),
|
|
original.get('merge_telecom', 0),
|
|
original.get('telecom_vehicle_no', ''),
|
|
original.get('field_custom_names', None),
|
|
original.get('show_monthly_total', 0),
|
|
original['is_active'],
|
|
sort_order
|
|
)
|
|
)
|
|
|
|
return jsonify({'success': True, 'message': '配置复制成功', 'id': new_id})
|
|
except Exception as e:
|
|
return jsonify({'success': False, 'message': str(e)}), 500
|
|
|
|
|
|
@config_bp.route('/config/<int:config_id>/toggle', methods=['POST'])
|
|
def toggle_config(config_id):
|
|
"""启用/禁用配置"""
|
|
try:
|
|
execute_update(
|
|
'UPDATE t_daily_report_config SET is_active = NOT is_active, update_time = NOW() WHERE id = %s',
|
|
(config_id,)
|
|
)
|
|
return jsonify({'success': True, 'message': '操作成功'})
|
|
except Exception as e:
|
|
return jsonify({'success': False, 'message': str(e)}), 500
|
|
|
|
|
|
@config_bp.route('/config/<int:config_id>/move', methods=['POST'])
|
|
def move_config(config_id):
|
|
"""移动配置排序"""
|
|
try:
|
|
direction = request.json.get('direction') # up 或 down
|
|
print(f"[move_config] 开始移动 config_id={config_id}, direction={direction}")
|
|
|
|
# 查出所有配置,按 sort_order, id 排序
|
|
all_configs = execute_query(
|
|
'SELECT id, sort_order FROM t_daily_report_config ORDER BY sort_order ASC, id ASC'
|
|
)
|
|
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
|
|
|
|
print(f"[move_config] 当前配置索引: {current_index}")
|
|
|
|
# 计算目标索引
|
|
if direction == 'up':
|
|
target_index = current_index - 1
|
|
elif direction == 'down':
|
|
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
|