制作开发文档,添加充电卡备注到日报中
This commit is contained in:
@@ -177,6 +177,9 @@ FIELD_MAPPING = {
|
||||
# 车辆信息(从 t_car 表关联)
|
||||
'car_bus_path': '公交线路',
|
||||
'car_sn': '车辆编码',
|
||||
|
||||
# 充电卡信息(从 t_user_card 表关联)
|
||||
'user_card_memo': '充电卡备注',
|
||||
}
|
||||
|
||||
# 获取字段中文名称
|
||||
|
||||
@@ -350,10 +350,10 @@ def generate_daily_report(config_id, start_time=None, end_time=None):
|
||||
report_date = start_time.strftime('%Y-%m-%d')
|
||||
delete_old_history(config_id, report_date, config['split_type'], config['split_value'])
|
||||
|
||||
# 构建查询SQL - 过滤掉虚拟字段(时段电量、自定义服务费、自定义实收总金额、车辆信息等)
|
||||
# 构建查询SQL - 过滤掉虚拟字段(时段电量、自定义服务费、自定义实收总金额、车辆信息、充电卡信息等)
|
||||
# 虚拟字段不是数据库表中的实际字段,需要动态计算或关联查询
|
||||
# 注意:total_amount 是旧版虚拟字段名,为了向后兼容也需要包含在内
|
||||
VIRTUAL_FIELDS = {'sharp_electricity', 'peak_electricity', 'flat_electricity', 'valley_electricity', 'custom_service_fee', 'custom_total_amount', 'total_amount', 'car_bus_path', 'car_sn'}
|
||||
VIRTUAL_FIELDS = {'sharp_electricity', 'peak_electricity', 'flat_electricity', 'valley_electricity', 'custom_service_fee', 'custom_total_amount', 'total_amount', 'car_bus_path', 'car_sn', 'user_card_memo'}
|
||||
db_fields = [f for f in selected_fields if f not in VIRTUAL_FIELDS]
|
||||
|
||||
# 确保 charge_degree 总是被查询(用于计算总电量)
|
||||
@@ -373,6 +373,11 @@ def generate_daily_report(config_id, start_time=None, end_time=None):
|
||||
if need_car_info and 'charge_vin' not in db_fields:
|
||||
db_fields.append('charge_vin')
|
||||
|
||||
# 如果选择了充电卡备注字段,需要确保查询 user_card_no(用于关联 t_user_card 表)
|
||||
need_card_memo = 'user_card_memo' in selected_fields
|
||||
if need_card_memo and 'user_card_no' not in db_fields:
|
||||
db_fields.append('user_card_no')
|
||||
|
||||
fields_str = ', '.join(db_fields)
|
||||
|
||||
# 检查是否为多选值(逗号分隔)
|
||||
@@ -447,6 +452,30 @@ def generate_daily_report(config_id, start_time=None, end_time=None):
|
||||
order['car_sn'] = ''
|
||||
log_info(f"[日报生成] 关联查询到 {len(car_map)} 条车辆信息", 'report')
|
||||
|
||||
# 如果需要充电卡信息,从 t_user_card 表关联查询
|
||||
if need_card_memo and orders:
|
||||
# 获取所有充电卡号
|
||||
card_no_list = list(set(order.get('user_card_no') for order in orders if order.get('user_card_no')))
|
||||
if card_no_list:
|
||||
placeholders = ', '.join(['%s'] * len(card_no_list))
|
||||
card_sql = f"""
|
||||
SELECT card_no, memo
|
||||
FROM t_user_card
|
||||
WHERE card_no IN ({placeholders})
|
||||
"""
|
||||
card_results = execute_query(card_sql, tuple(card_no_list))
|
||||
card_map = {card['card_no']: card for card in card_results}
|
||||
|
||||
# 将充电卡备注添加到订单中
|
||||
for order in orders:
|
||||
card_no = order.get('user_card_no')
|
||||
if card_no and card_no in card_map:
|
||||
card_info = card_map[card_no]
|
||||
order['user_card_memo'] = card_info.get('memo', '')
|
||||
else:
|
||||
order['user_card_memo'] = ''
|
||||
log_info(f"[日报生成] 关联查询到 {len(card_map)} 条充电卡信息", 'report')
|
||||
|
||||
# 检查是否有补单记录(finish_type = 2 表示补单结束)
|
||||
has_supplement = any(order.get('finish_type') == 2 for order in orders)
|
||||
if has_supplement:
|
||||
@@ -472,6 +501,52 @@ def generate_daily_report(config_id, start_time=None, end_time=None):
|
||||
# 检查特来电数据中是否有补单记录
|
||||
if not has_supplement:
|
||||
has_supplement = any(order.get('finish_type') == 2 for order in telecom_orders)
|
||||
|
||||
# 合并后再次处理车辆信息(针对特来电订单)
|
||||
if need_car_info:
|
||||
telecom_vin_list = list(set(order.get('charge_vin') for order in telecom_orders if order.get('charge_vin')))
|
||||
if telecom_vin_list:
|
||||
placeholders = ', '.join(['%s'] * len(telecom_vin_list))
|
||||
car_sql = f"""
|
||||
SELECT car_vin, car_bus_path, car_sn
|
||||
FROM t_car
|
||||
WHERE car_vin IN ({placeholders})
|
||||
"""
|
||||
car_results = execute_query(car_sql, tuple(telecom_vin_list))
|
||||
car_map = {car['car_vin']: car for car in car_results}
|
||||
|
||||
for order in telecom_orders:
|
||||
vin = order.get('charge_vin')
|
||||
if vin and vin in car_map:
|
||||
car_info = car_map[vin]
|
||||
order['car_bus_path'] = car_info.get('car_bus_path', '')
|
||||
order['car_sn'] = car_info.get('car_sn', '')
|
||||
else:
|
||||
order['car_bus_path'] = ''
|
||||
order['car_sn'] = ''
|
||||
log_info(f"[日报生成] 特来电数据关联查询到 {len(car_map)} 条车辆信息", 'report')
|
||||
|
||||
# 合并后再次处理充电卡信息(针对特来电订单)
|
||||
if need_card_memo:
|
||||
telecom_card_no_list = list(set(order.get('user_card_no') for order in telecom_orders if order.get('user_card_no')))
|
||||
if telecom_card_no_list:
|
||||
placeholders = ', '.join(['%s'] * len(telecom_card_no_list))
|
||||
card_sql = f"""
|
||||
SELECT card_no, memo
|
||||
FROM t_user_card
|
||||
WHERE card_no IN ({placeholders})
|
||||
"""
|
||||
card_results = execute_query(card_sql, tuple(telecom_card_no_list))
|
||||
card_map = {card['card_no']: card for card in card_results}
|
||||
|
||||
for order in telecom_orders:
|
||||
card_no = order.get('user_card_no')
|
||||
if card_no and card_no in card_map:
|
||||
card_info = card_map[card_no]
|
||||
order['user_card_memo'] = card_info.get('memo', '')
|
||||
else:
|
||||
order['user_card_memo'] = ''
|
||||
log_info(f"[日报生成] 特来电数据关联查询到 {len(card_map)} 条充电卡信息", 'report')
|
||||
|
||||
if not orders:
|
||||
# 即使没有数据,也保存一条失败记录
|
||||
|
||||
Reference in New Issue
Block a user