增加日志
This commit is contained in:
@@ -10,6 +10,7 @@ import pytz
|
||||
|
||||
from lib.db import execute_query, execute_insert
|
||||
from lib.field_mapping import get_field_display_name
|
||||
from lib.logger import log_info, log_error, log_warning
|
||||
|
||||
|
||||
# ID字段到名称的映射配置
|
||||
@@ -102,9 +103,9 @@ def query_telecom_orders(vehicle_self_nos, start_time_str, end_time_str):
|
||||
|
||||
try:
|
||||
results = execute_query(sql, params)
|
||||
print(f"[特来电] 查询到 {len(results)} 条订单")
|
||||
log_info(f"[特来电] 查询到 {len(results)} 条订单", 'report')
|
||||
except Exception as e:
|
||||
print(f"[特来电] 查询失败: {e}")
|
||||
log_error(f"[特来电] 查询失败: {e}", 'report')
|
||||
return []
|
||||
|
||||
# 转换字段名
|
||||
@@ -165,7 +166,7 @@ def build_id_to_name_map(orders, selected_fields):
|
||||
# 合并特来电的名称映射
|
||||
if telecom_name_values:
|
||||
id_name_maps[field] = telecom_name_values
|
||||
print(f"[ID转换] {field}: 特来电数据直接使用名称 {len(telecom_name_values)} 个")
|
||||
log_info(f"[ID转换] {field}: 特来电数据直接使用名称 {len(telecom_name_values)} 个", 'report')
|
||||
|
||||
# 查询驿来特数据的ID映射
|
||||
if not id_values:
|
||||
@@ -180,9 +181,9 @@ def build_id_to_name_map(orders, selected_fields):
|
||||
if field not in id_name_maps:
|
||||
id_name_maps[field] = {}
|
||||
id_name_maps[field].update({str(row['id_val']): row['name_val'] for row in results})
|
||||
print(f"[ID转换] {field}: 查询到 {len(results)} 个名称映射")
|
||||
log_info(f"[ID转换] {field}: 查询到 {len(results)} 个名称映射", 'report')
|
||||
except Exception as e:
|
||||
print(f"[ID转换] {field} 查询失败: {e}")
|
||||
log_error(f"[ID转换] {field} 查询失败: {e}", 'report')
|
||||
if field not in id_name_maps:
|
||||
id_name_maps[field] = {}
|
||||
|
||||
@@ -231,9 +232,9 @@ def calculate_time_period_electricity(orders, time_periods_config):
|
||||
|
||||
try:
|
||||
details = execute_query(sql, tuple(order_nos))
|
||||
print(f"[时段电量] 查询到 {len(details)} 条分时记录")
|
||||
log_info(f"[时段电量] 查询到 {len(details)} 条分时记录", 'report')
|
||||
except Exception as e:
|
||||
print(f"[时段电量] 查询失败: {e}")
|
||||
log_error(f"[时段电量] 查询失败: {e}", 'report')
|
||||
return {}
|
||||
|
||||
# 按订单号分组计算各时段电量
|
||||
@@ -320,12 +321,12 @@ 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)} 个")
|
||||
log_info(f"[日报生成] 开始生成日报", 'report')
|
||||
log_info(f" 配置ID: {config_id}", 'report')
|
||||
log_info(f" 配置名称: {config['config_name']}", 'report')
|
||||
log_info(f" 拆分方式: {config['split_type']} = {config['split_value']}", 'report')
|
||||
log_info(f" 时间范围: {start_time_str} 至 {end_time_str}", 'report')
|
||||
log_info(f" 选择字段: {len(selected_fields)} 个", 'report')
|
||||
|
||||
# 构建查询SQL - 过滤掉虚拟字段(时段电量等)
|
||||
# 虚拟字段不是数据库表中的实际字段,需要动态计算
|
||||
@@ -370,7 +371,7 @@ def generate_daily_report(config_id, start_time=None, end_time=None):
|
||||
# 执行查询
|
||||
orders = execute_query(sql, tuple(params))
|
||||
|
||||
print(f"[日报生成] 驿来特查询到 {len(orders)} 条订单")
|
||||
log_info(f"[日报生成] 驿来特查询到 {len(orders)} 条订单", 'report')
|
||||
|
||||
# 添加数据来源标记
|
||||
for order in orders:
|
||||
@@ -379,7 +380,7 @@ def generate_daily_report(config_id, start_time=None, end_time=None):
|
||||
# 检查是否有补单记录(finish_type = 2 表示补单结束)
|
||||
has_supplement = any(order.get('finish_type') == 2 for order in orders)
|
||||
if has_supplement:
|
||||
print(f"[日报生成] 检测到补单记录,将在文件名中添加标记")
|
||||
log_info(f"[日报生成] 检测到补单记录,将在文件名中添加标记", 'report')
|
||||
|
||||
# 检查是否需要合并特来电数据
|
||||
merge_telecom = config.get('merge_telecom', 0)
|
||||
@@ -389,12 +390,12 @@ def generate_daily_report(config_id, start_time=None, end_time=None):
|
||||
# 解析车量自编号(支持逗号分隔的多个)
|
||||
vehicle_nos = [v.strip() for v in telecom_vehicle_no.split(',') if v.strip()]
|
||||
if vehicle_nos:
|
||||
print(f"[日报生成] 开始合并特来电数据,车量自编号: {vehicle_nos}")
|
||||
log_info(f"[日报生成] 开始合并特来电数据,车量自编号: {vehicle_nos}", 'report')
|
||||
telecom_orders = query_telecom_orders(vehicle_nos, start_time_str, end_time_str)
|
||||
if telecom_orders:
|
||||
# 将特来电数据添加到订单列表
|
||||
orders.extend(telecom_orders)
|
||||
print(f"[日报生成] 合并后总订单数: {len(orders)}")
|
||||
log_info(f"[日报生成] 合并后总订单数: {len(orders)}", 'report')
|
||||
# 检查特来电数据中是否有补单记录
|
||||
if not has_supplement:
|
||||
has_supplement = any(order.get('finish_type') == 2 for order in telecom_orders)
|
||||
@@ -424,7 +425,7 @@ def generate_daily_report(config_id, start_time=None, end_time=None):
|
||||
# 解析时段配置并计算分时段电量
|
||||
time_periods_config = json.loads(config['time_periods']) if config.get('time_periods') else {}
|
||||
if time_periods_config and any(time_periods_config.values()):
|
||||
print(f"[日报生成] 时段配置: {time_periods_config}")
|
||||
log_info(f"[日报生成] 时段配置: {time_periods_config}", 'report')
|
||||
time_period_electricity = calculate_time_period_electricity(orders, time_periods_config)
|
||||
|
||||
# 将分时段电量添加到订单数据中(仅对驿来特订单,特来电订单已有数据)
|
||||
@@ -441,7 +442,7 @@ def generate_daily_report(config_id, start_time=None, end_time=None):
|
||||
order['flat_electricity'] = ep['flat_electricity']
|
||||
order['valley_electricity'] = ep['valley_electricity']
|
||||
|
||||
print(f"[日报生成] 已计算 {len(time_period_electricity)} 条订单的分时段电量")
|
||||
log_info(f"[日报生成] 已计算 {len(time_period_electricity)} 条订单的分时段电量", 'report')
|
||||
|
||||
# 计算求和(保留三位小数)
|
||||
sum_results = {}
|
||||
@@ -454,7 +455,7 @@ def generate_daily_report(config_id, start_time=None, end_time=None):
|
||||
if 'charge_degree' not in sum_results and orders and 'charge_degree' in orders[0]:
|
||||
total_charge_degree = sum(float(order.get('charge_degree', 0) or 0) for order in orders)
|
||||
sum_results['charge_degree'] = round(total_charge_degree, 3)
|
||||
print(f"[日报生成] 强制采集充电电量: {sum_results['charge_degree']} kWh")
|
||||
log_info(f"[日报生成] 强制采集充电电量: {sum_results['charge_degree']} kWh", 'report')
|
||||
|
||||
# 构建ID到名称的映射
|
||||
id_name_maps = build_id_to_name_map(orders, selected_fields)
|
||||
@@ -476,17 +477,19 @@ def generate_daily_report(config_id, start_time=None, end_time=None):
|
||||
config_id=config['id'],
|
||||
report_date=start_time.strftime('%Y-%m-%d'),
|
||||
sort_order=config.get('sort_order'),
|
||||
split_type=config.get('split_type'),
|
||||
split_value=config.get('split_value'),
|
||||
custom_service_fee_price=config.get('custom_service_fee_price'),
|
||||
custom_service_fee_name=config.get('custom_service_fee_name')
|
||||
)
|
||||
|
||||
print(f"[日报生成] Excel文件已生成: {file_path}")
|
||||
log_info(f"[日报生成] Excel文件已生成: {file_path}", 'report')
|
||||
|
||||
# 计算总金额
|
||||
total_amount = sum(float(order.get('total_money', 0) or 0) for order in orders)
|
||||
|
||||
# 保存历史记录
|
||||
print(f"[日报生成] 准备保存历史记录,file_path={file_path}")
|
||||
log_info(f"[日报生成] 准备保存历史记录,file_path={file_path}", 'report')
|
||||
history_id = save_report_history(
|
||||
report_date=start_time.strftime('%Y-%m-%d'),
|
||||
start_time=start_time_str,
|
||||
@@ -502,7 +505,7 @@ def generate_daily_report(config_id, start_time=None, end_time=None):
|
||||
file_path=file_path,
|
||||
status=1
|
||||
)
|
||||
print(f"[日报生成] 历史记录已保存,ID={history_id}")
|
||||
log_info(f"[日报生成] 历史记录已保存,ID={history_id}", 'report')
|
||||
|
||||
# 自动采集求和数据到求和数据表
|
||||
try:
|
||||
@@ -510,17 +513,17 @@ def generate_daily_report(config_id, start_time=None, end_time=None):
|
||||
from lib.sum_data_collector import collect_sum_data_from_history
|
||||
collect_result = collect_sum_data_from_history(history_id)
|
||||
if collect_result['success']:
|
||||
print(f"[日报生成] 求和数据采集成功")
|
||||
log_info(f"[日报生成] 求和数据采集成功", 'report')
|
||||
else:
|
||||
print(f"[日报生成] 求和数据采集失败: {collect_result['message']}")
|
||||
log_warning(f"[日报生成] 求和数据采集失败: {collect_result['message']}", 'report')
|
||||
except Exception as collect_error:
|
||||
print(f"[日报生成] 求和数据采集异常: {str(collect_error)}")
|
||||
log_error(f"[日报生成] 求和数据采集异常: {str(collect_error)}", 'report')
|
||||
|
||||
print(f"[日报生成] ✓ 生成成功")
|
||||
print(f" 订单数量: {len(orders)}")
|
||||
print(f" 总金额: {total_amount}")
|
||||
print(f" 文件路径: {file_path}")
|
||||
print(f" 历史记录ID: {history_id}")
|
||||
log_info(f"[日报生成] ✓ 生成成功", 'report')
|
||||
log_info(f" 订单数量: {len(orders)}", 'report')
|
||||
log_info(f" 总金额: {total_amount}", 'report')
|
||||
log_info(f" 文件路径: {file_path}", 'report')
|
||||
log_info(f" 历史记录ID: {history_id}", 'report')
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
@@ -533,9 +536,7 @@ 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()
|
||||
log_error(f"[日报生成] ✗ 生成失败: {str(e)}", 'report')
|
||||
|
||||
# 保存失败记录
|
||||
try:
|
||||
@@ -554,9 +555,9 @@ def generate_daily_report(config_id, start_time=None, end_time=None):
|
||||
file_path='',
|
||||
status=0
|
||||
)
|
||||
print(f"[日报生成] 已保存失败记录到历史表")
|
||||
log_info(f"[日报生成] 已保存失败记录到历史表", 'report')
|
||||
except Exception as save_error:
|
||||
print(f"[日报生成] 保存失败记录时出错: {str(save_error)}")
|
||||
log_error(f"[日报生成] 保存失败记录时出错: {str(save_error)}", 'report')
|
||||
|
||||
return {
|
||||
'success': False,
|
||||
@@ -568,6 +569,7 @@ def generate_excel(orders, selected_fields, sum_fields, sum_results,
|
||||
config_name, split_name, start_time, end_time,
|
||||
id_name_maps=None, field_custom_names=None, has_supplement=False,
|
||||
show_monthly_total=False, config_id=None, report_date=None, sort_order=None,
|
||||
split_type=None, split_value=None,
|
||||
custom_service_fee_price=None, custom_service_fee_name=None):
|
||||
"""
|
||||
生成Excel文件
|
||||
@@ -615,7 +617,14 @@ def generate_excel(orders, selected_fields, sum_fields, sum_results,
|
||||
|
||||
# 表头从第1行开始
|
||||
header_row = 1
|
||||
field_custom_names = field_custom_names or {}
|
||||
if field_custom_names:
|
||||
if isinstance(field_custom_names, str):
|
||||
try:
|
||||
field_custom_names = json.loads(field_custom_names)
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
field_custom_names = {}
|
||||
else:
|
||||
field_custom_names = {}
|
||||
|
||||
# 导入边框样式
|
||||
from openpyxl.styles import Border, Side
|
||||
@@ -820,7 +829,12 @@ def generate_excel(orders, selected_fields, sum_fields, sum_results,
|
||||
try:
|
||||
from lib.sum_data_collector import get_monthly_charge_degree_total
|
||||
month = report_date[:7]
|
||||
monthly_result = get_monthly_charge_degree_total(config_id, month)
|
||||
monthly_result = get_monthly_charge_degree_total(
|
||||
config_id=config_id,
|
||||
month=month,
|
||||
split_type=split_type,
|
||||
split_value=split_value
|
||||
)
|
||||
|
||||
if monthly_result['success'] and monthly_result['data']:
|
||||
total_degree = monthly_result['data']['total_degree']
|
||||
@@ -868,7 +882,7 @@ def generate_excel(orders, selected_fields, sum_fields, sum_results,
|
||||
|
||||
ws.row_dimensions[monthly_row].height = 25
|
||||
except Exception as e:
|
||||
print(f"[日报生成] 写入月度总计时出错: {e}")
|
||||
log_error(f"[日报生成] 写入月度总计时出错: {e}", 'report')
|
||||
|
||||
# 调整列宽(中文按2个字符宽度计算)
|
||||
# 序号列固定宽度
|
||||
@@ -929,10 +943,10 @@ def save_report_history(report_date, start_time, end_time, split_type, split_val
|
||||
# 生成ID
|
||||
history_id = int(datetime.now().timestamp() * 1000)
|
||||
|
||||
print(f"[保存历史] 准备保存历史记录")
|
||||
print(f" ID: {history_id}")
|
||||
print(f" file_path: '{file_path}'")
|
||||
print(f" status: {status}")
|
||||
log_info(f"[保存历史] 准备保存历史记录", 'report')
|
||||
log_info(f" ID: {history_id}", 'report')
|
||||
log_info(f" file_path: '{file_path}'", 'report')
|
||||
log_info(f" status: {status}", 'report')
|
||||
|
||||
sql = """
|
||||
INSERT INTO t_daily_report_history
|
||||
@@ -958,8 +972,8 @@ def save_report_history(report_date, start_time, end_time, split_type, split_val
|
||||
status
|
||||
)
|
||||
|
||||
print(f"[保存历史] 执行SQL插入...")
|
||||
log_info(f"[保存历史] 执行SQL插入...", 'report')
|
||||
result = execute_insert(sql, params)
|
||||
print(f"[保存历史] 插入完成,返回ID: {result}")
|
||||
log_info(f"[保存历史] 插入完成,返回ID: {result}", 'report')
|
||||
|
||||
return history_id
|
||||
Reference in New Issue
Block a user