增加日志
This commit is contained in:
@@ -5,6 +5,7 @@ import json
|
||||
from datetime import datetime
|
||||
from flask import request, jsonify
|
||||
from lib.db import execute_query, execute_update, execute_insert
|
||||
from lib.logger import log_info, log_error, log_warning
|
||||
from . import config_bp
|
||||
|
||||
|
||||
@@ -12,6 +13,7 @@ from . import config_bp
|
||||
def get_configs():
|
||||
"""获取所有配置"""
|
||||
try:
|
||||
log_info('[配置API] 获取配置列表', 'api')
|
||||
configs = execute_query(
|
||||
'SELECT * FROM t_daily_report_config ORDER BY sort_order ASC, id DESC'
|
||||
)
|
||||
@@ -22,8 +24,10 @@ def get_configs():
|
||||
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 {}
|
||||
|
||||
log_info(f'[配置API] 获取到 {len(configs)} 条配置', 'api')
|
||||
return jsonify({'success': True, 'data': configs})
|
||||
except Exception as e:
|
||||
log_error(f'[配置API] 获取配置列表失败: {e}', 'api')
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
|
||||
|
||||
@@ -31,6 +35,7 @@ def get_configs():
|
||||
def create_config():
|
||||
"""创建配置"""
|
||||
try:
|
||||
log_info('[配置API] 创建配置', 'api')
|
||||
data = request.json
|
||||
config_name = data.get('config_name')
|
||||
split_type = data.get('split_type')
|
||||
@@ -52,9 +57,11 @@ def create_config():
|
||||
custom_service_fee_name = data.get('custom_service_fee_name')
|
||||
|
||||
if not config_name or not split_type or not split_value:
|
||||
log_warning('[配置API] 创建配置失败:缺少必填字段', 'api')
|
||||
return jsonify({'success': False, 'message': '缺少必填字段'}), 400
|
||||
|
||||
if not selected_fields:
|
||||
log_warning('[配置API] 创建配置失败:未选择字段', 'api')
|
||||
return jsonify({'success': False, 'message': '请至少选择一个字段'}), 400
|
||||
|
||||
# 生成 ID
|
||||
@@ -92,12 +99,14 @@ def create_config():
|
||||
|
||||
execute_insert(sql, params)
|
||||
|
||||
log_info(f'[配置API] 配置创建成功,ID: {config_id},名称: {config_name}', 'api')
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'message': '配置创建成功',
|
||||
'data': {'id': config_id}
|
||||
})
|
||||
except Exception as e:
|
||||
log_error(f'[配置API] 创建配置失败: {e}', 'api')
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
|
||||
|
||||
@@ -105,6 +114,7 @@ def create_config():
|
||||
def update_config(config_id):
|
||||
"""更新配置"""
|
||||
try:
|
||||
log_info(f'[配置API] 更新配置,ID: {config_id}', 'api')
|
||||
data = request.json
|
||||
config_name = data.get('config_name')
|
||||
split_type = data.get('split_type')
|
||||
@@ -125,6 +135,12 @@ def update_config(config_id):
|
||||
|
||||
custom_service_fee_name = data.get('custom_service_fee_name')
|
||||
|
||||
# 查询原配置,检查拆分是否变化
|
||||
old_configs = execute_query('SELECT * FROM t_daily_report_config WHERE id = %s', (config_id,))
|
||||
if not old_configs:
|
||||
return jsonify({'success': False, 'message': '配置不存在'}), 404
|
||||
old_config = old_configs[0]
|
||||
|
||||
sql = """
|
||||
UPDATE t_daily_report_config
|
||||
SET config_name = %s, split_type = %s, split_value = %s, split_name = %s,
|
||||
@@ -154,8 +170,32 @@ def update_config(config_id):
|
||||
|
||||
execute_update(sql, params)
|
||||
|
||||
# 如果拆分类型或拆分值变化了,清理旧的求和数据(以当前拆分为准)
|
||||
old_split_type = old_config.get('split_type')
|
||||
old_split_value = old_config.get('split_value', '')
|
||||
if old_split_type != split_type or old_split_value != split_value:
|
||||
new_values = set(v.strip() for v in split_value.split(',') if v.strip())
|
||||
old_values = set(v.strip() for v in str(old_split_value).split(',') if v.strip())
|
||||
removed_values = old_values - new_values
|
||||
if removed_values:
|
||||
placeholders = ','.join(['%s'] * len(removed_values))
|
||||
delete_sql = f'''
|
||||
DELETE FROM t_daily_report_sum_data
|
||||
WHERE config_id = %s AND split_type = %s
|
||||
AND split_value IN ({placeholders})
|
||||
'''
|
||||
delete_params = [config_id, old_split_type] + list(removed_values)
|
||||
try:
|
||||
from lib.db import execute_update
|
||||
execute_update(delete_sql, tuple(delete_params))
|
||||
log_info(f'[配置API] 拆分值变化,已清理{len(removed_values)}个旧拆分的求和数据', 'api')
|
||||
except Exception as e:
|
||||
log_error(f'[配置API] 清理旧求和数据失败: {e}', 'api')
|
||||
|
||||
log_info(f'[配置API] 配置更新成功,ID: {config_id}', 'api')
|
||||
return jsonify({'success': True, 'message': '配置更新成功'})
|
||||
except Exception as e:
|
||||
log_error(f'[配置API] 更新配置失败: {e}', 'api')
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
|
||||
|
||||
@@ -163,9 +203,18 @@ def update_config(config_id):
|
||||
def delete_config(config_id):
|
||||
"""删除配置"""
|
||||
try:
|
||||
log_info(f'[配置API] 删除配置,ID: {config_id}', 'api')
|
||||
execute_update('DELETE FROM t_daily_report_config WHERE id = %s', (config_id,))
|
||||
# 同时删除对应的求和数据
|
||||
try:
|
||||
execute_update('DELETE FROM t_daily_report_sum_data WHERE config_id = %s', (config_id,))
|
||||
log_info(f'[配置API] 已清理配置对应的求和数据', 'api')
|
||||
except Exception as e:
|
||||
log_error(f'[配置API] 清理求和数据失败: {e}', 'api')
|
||||
log_info(f'[配置API] 配置删除成功,ID: {config_id}', 'api')
|
||||
return jsonify({'success': True, 'message': '配置删除成功'})
|
||||
except Exception as e:
|
||||
log_error(f'[配置API] 删除配置失败: {e}', 'api')
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
|
||||
|
||||
@@ -236,15 +285,13 @@ def move_config(config_id):
|
||||
"""移动配置排序"""
|
||||
try:
|
||||
direction = request.json.get('direction') # up 或 down
|
||||
print(f"[move_config] 开始移动 config_id={config_id}, direction={direction}")
|
||||
log_info(f"[配置API] 移动配置,ID: {config_id}, 方向: {direction}", 'api')
|
||||
|
||||
# 查出所有配置,按 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']}")
|
||||
log_info(f"[配置API] 所有配置数量: {len(all_configs)}", 'api')
|
||||
|
||||
# 找到当前配置的索引
|
||||
current_index = None
|
||||
@@ -254,10 +301,10 @@ def move_config(config_id):
|
||||
break
|
||||
|
||||
if current_index is None:
|
||||
print(f"[move_config] 未找到配置 id={config_id}")
|
||||
log_warning(f"[配置API] 未找到配置,ID: {config_id}", 'api')
|
||||
return jsonify({'success': False, 'message': '配置不存在'}), 404
|
||||
|
||||
print(f"[move_config] 当前配置索引: {current_index}")
|
||||
log_info(f"[配置API] 当前配置索引: {current_index}", 'api')
|
||||
|
||||
# 计算目标索引
|
||||
if direction == 'up':
|
||||
@@ -269,29 +316,24 @@ def move_config(config_id):
|
||||
|
||||
# 边界检查
|
||||
if target_index < 0 or target_index >= len(all_configs):
|
||||
print(f"[move_config] 已到边界,无法移动 target_index={target_index}")
|
||||
log_info(f"[配置API] 已到边界,无法移动", 'api')
|
||||
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] 重排完成")
|
||||
log_info(f"[配置API] 配置移动成功,ID: {config_id}", 'api')
|
||||
return jsonify({'success': True, 'message': '移动成功'})
|
||||
except Exception as e:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
print(f"[move_config] 异常: {e}")
|
||||
log_error(f"[配置API] 移动配置失败: {e}", 'api')
|
||||
return jsonify({'success': False, 'message': str(e)}), 500
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user