fix: 修复日报生成后历史记录不显示的问题
问题:生成日报后,历史记录中没有显示订单。
原因:
1. 如果时间范围内没有订单数据,函数直接返回失败,没有保存失败记录
2. 缺少详细的日志输出,难以诊断问题
修复内容:
1. lib/report_generator.py:
- 即使没有订单数据,也保存失败记录到历史表(status=0)
- 添加详细的日志输出:
* 开始生成时的配置信息
* 查询到的订单数量
* 生成成功的详细信息
* 生成失败的错误堆栈
- 改进异常处理,确保失败记录一定被保存
2. 新增 test_diagnosis.py:
- 自动检查数据库连接
- 检查表是否存在
- 查看配置和历史记录
- 检查订单数据统计
- 提供诊断建议
3. 新增 诊断指南.md:
- 常见问题和解决方案
- 手动检查数据库的SQL语句
- 调试技巧和API测试方法
现在用户可以:
1. 运行 python test_diagnosis.py 快速诊断问题
2. 查看服务器控制台的详细日志
3. 参考诊断指南排查问题
4. 即使生成失败,也能在历史记录中看到记录
Coze-Commit-Type: user
Coze-User-ID: 3722323274763196
Coze-Conversation-ID: 9894087
This commit is contained in:
@@ -56,6 +56,13 @@ def generate_daily_report(config_id, start_time=None, end_time=None):
|
||||
start_time_str = start_time.strftime('%Y-%m-%d %H:%M:%S')
|
||||
end_time_str = end_time.strftime('%Y-%m-%d %H:%M:%S')
|
||||
|
||||
print(f"[日报生成] 开始生成日报")
|
||||
print(f" 配置ID: {config_id}")
|
||||
print(f" 配置名称: {config['config_name']}")
|
||||
print(f" 拆分方式: {config['split_type']} = {config['split_value']}")
|
||||
print(f" 时间范围: {start_time_str} 至 {end_time_str}")
|
||||
print(f" 选择字段: {len(selected_fields)} 个")
|
||||
|
||||
# 构建查询SQL
|
||||
fields_str = ', '.join(selected_fields)
|
||||
sql = f"""
|
||||
@@ -71,10 +78,27 @@ def generate_daily_report(config_id, start_time=None, end_time=None):
|
||||
# 执行查询
|
||||
orders = execute_query(sql, (start_time_str, end_time_str, config['split_value']))
|
||||
|
||||
print(f"[日报生成] 查询到 {len(orders)} 条订单")
|
||||
|
||||
if not orders:
|
||||
# 即使没有数据,也保存一条失败记录
|
||||
save_report_history(
|
||||
report_date=start_time.strftime('%Y-%m-%d'),
|
||||
start_time=start_time_str,
|
||||
end_time=end_time_str,
|
||||
split_type=config['split_type'],
|
||||
split_value=config['split_value'],
|
||||
split_name=config.get('split_name', config['split_value']),
|
||||
config_id=config_id,
|
||||
total_orders=0,
|
||||
total_amount=0,
|
||||
sum_results={},
|
||||
file_path='',
|
||||
status=0
|
||||
)
|
||||
return {
|
||||
'success': False,
|
||||
'message': f'时间范围内没有符合条件的订单数据'
|
||||
'message': f'时间范围内没有符合条件的订单数据(查询条件:{start_time_str} 至 {end_time_str},{config["split_type"]}={config["split_value"]},state=3)'
|
||||
}
|
||||
|
||||
# 计算求和
|
||||
@@ -115,6 +139,12 @@ def generate_daily_report(config_id, start_time=None, end_time=None):
|
||||
status=1
|
||||
)
|
||||
|
||||
print(f"[日报生成] ✓ 生成成功")
|
||||
print(f" 订单数量: {len(orders)}")
|
||||
print(f" 总金额: {total_amount}")
|
||||
print(f" 文件路径: {file_path}")
|
||||
print(f" 历史记录ID: {history_id}")
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
'message': '日报生成成功',
|
||||
@@ -126,15 +156,19 @@ def generate_daily_report(config_id, start_time=None, end_time=None):
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
print(f"[日报生成] ✗ 生成失败: {str(e)}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
# 保存失败记录
|
||||
try:
|
||||
save_report_history(
|
||||
report_date=start_time.strftime('%Y-%m-%d') if start_time else datetime.now().strftime('%Y-%m-%d'),
|
||||
start_time=start_time_str if start_time else '',
|
||||
end_time=end_time_str if end_time else '',
|
||||
split_type=config['split_type'] if config else '',
|
||||
split_value=config['split_value'] if config else '',
|
||||
split_name=config.get('split_name', '') if config else '',
|
||||
start_time=start_time_str if 'start_time_str' in locals() else '',
|
||||
end_time=end_time_str if 'end_time_str' in locals() else '',
|
||||
split_type=config['split_type'] if 'config' in locals() and config else '',
|
||||
split_value=config['split_value'] if 'config' in locals() and config else '',
|
||||
split_name=config.get('split_name', '') if 'config' in locals() and config else '',
|
||||
config_id=config_id,
|
||||
total_orders=0,
|
||||
total_amount=0,
|
||||
@@ -142,8 +176,9 @@ def generate_daily_report(config_id, start_time=None, end_time=None):
|
||||
file_path='',
|
||||
status=0
|
||||
)
|
||||
except:
|
||||
pass
|
||||
print(f"[日报生成] 已保存失败记录到历史表")
|
||||
except Exception as save_error:
|
||||
print(f"[日报生成] 保存失败记录时出错: {str(save_error)}")
|
||||
|
||||
return {
|
||||
'success': False,
|
||||
|
||||
Reference in New Issue
Block a user