增加日志

This commit is contained in:
2026-07-15 16:01:06 +08:00
parent d34dd82bd5
commit 727f5434b2
11 changed files with 461 additions and 192 deletions

View File

@@ -6,6 +6,7 @@ import os
from datetime import datetime
from lib.db import execute_query, execute_update, execute_insert
from lib.field_mapping import get_field_display_name
from lib.logger import log_info, log_error, log_warning
def collect_sum_data_from_history(history_id=None):
@@ -85,9 +86,9 @@ def collect_sum_data_from_history(history_id=None):
charge_degree = _calculate_charge_degree(history)
if charge_degree is not None:
sum_results['charge_degree'] = charge_degree
print(f"[采集] 历史记录 {history['id']} 补充计算充电电量: {charge_degree} kWh")
log_info(f"[采集] 历史记录 {history['id']} 补充计算充电电量: {charge_degree} kWh", 'sum_data')
except Exception as calc_error:
print(f"[采集] 历史记录 {history['id']} 计算充电电量失败: {calc_error}")
log_error(f"[采集] 历史记录 {history['id']} 计算充电电量失败: {calc_error}", 'sum_data')
# 如果还是没有任何求和数据,跳过
if not sum_results:
@@ -167,15 +168,15 @@ def collect_sum_data_from_history(history_id=None):
execute_insert(sql, params)
collected_count += 1
except Exception as fee_error:
print(f"[采集] 计算自定义服务费失败: {fee_error}")
log_error(f"[采集] 计算自定义服务费失败: {fee_error}", 'sum_data')
log_info(f'[采集] 采集完成,共采集 {collected_count} 条求和数据,跳过 {skipped_count} 条无求和数据的记录', 'sum_data')
return {
'success': True,
'message': f'采集完成,共采集 {collected_count} 条求和数据,跳过 {skipped_count} 条无求和数据的记录'
}
except Exception as e:
import traceback
traceback.print_exc()
log_error(f'[采集] 采集失败: {e}', 'sum_data')
return {'success': False, 'message': f'采集失败: {str(e)}'}
@@ -229,7 +230,7 @@ def _calculate_charge_degree(history):
return 0.0
except Exception as e:
print(f"[计算充电电量] 失败: {e}")
log_error(f"[计算充电电量] 失败: {e}", 'sum_data')
return None
@@ -669,14 +670,17 @@ def export_sum_data_to_excel(start_date=None, end_date=None, config_id=None, dat
return {'success': False, 'message': str(e)}
def get_monthly_charge_degree_total(config_id=None, month=None):
def get_monthly_charge_degree_total(config_id=None, month=None, split_type=None, split_value=None):
"""
获取当月充电电量累计(从求和数据表查询,对账后的准确数据)
- 如果当天已经生成了日报并采集,自动包含在内,不会重复计算
- 以配置当前拆分为准,只统计匹配的拆分值数据
Args:
config_id: 配置ID可选不传则统计所有配置
month: 月份,格式 YYYY-MM可选默认当月
split_type: 拆分类型可选company_id/user_id/station_id
split_value: 拆分值,逗号分隔的字符串(可选,用于过滤)
Returns:
dict: {
@@ -716,6 +720,15 @@ def get_monthly_charge_degree_total(config_id=None, month=None):
sql += ' AND config_id = %s'
params.append(config_id)
# 按拆分值过滤(以配置当前拆分为准)
if split_type and split_value:
values = [v.strip() for v in split_value.split(',') if v.strip()]
if values:
placeholders = ','.join(['%s'] * len(values))
sql += f' AND split_type = %s AND split_value IN ({placeholders})'
params.append(split_type)
params.extend(values)
sql += ' GROUP BY report_date ORDER BY report_date ASC'
rows = execute_query(sql, tuple(params))