feat: 添加分时段电量统计功能(尖/峰/平/谷)
Coze-Commit-Type: user Coze-User-ID: 3722323274763196 Coze-Conversation-ID: 9894087
This commit is contained in:
@@ -160,6 +160,12 @@ FIELD_MAPPING = {
|
||||
# 时间戳
|
||||
'create_time': '创建时间',
|
||||
'update_time': '更新时间',
|
||||
|
||||
# 分时段电量(从detail表计算)
|
||||
'sharp_electricity': '尖时电量(kWh)',
|
||||
'peak_electricity': '峰时电量(kWh)',
|
||||
'flat_electricity': '平时电量(kWh)',
|
||||
'valley_electricity': '谷时电量(kWh)',
|
||||
}
|
||||
|
||||
# 获取字段中文名称
|
||||
|
||||
@@ -67,6 +67,93 @@ def build_id_to_name_map(orders, selected_fields):
|
||||
return id_name_maps
|
||||
|
||||
|
||||
def calculate_time_period_electricity(orders, time_periods_config):
|
||||
"""
|
||||
计算分时段电量
|
||||
|
||||
从 t_equipment_charge_order_detail 表查询分时电量数据,
|
||||
根据时段配置计算各时段的电量总和。
|
||||
|
||||
Args:
|
||||
orders: 订单数据列表
|
||||
time_periods_config: 时段配置 {sharp: [11,12], peak: [9,10], flat: [7,8], valley: [0,1,2,3]}
|
||||
|
||||
Returns:
|
||||
dict: {order_no: {sharp_electricity: x, peak_electricity: y, flat_electricity: z, valley_electricity: w}}
|
||||
"""
|
||||
if not time_periods_config or not any(time_periods_config.values()):
|
||||
return {}
|
||||
|
||||
# 收集所有订单号
|
||||
order_nos = [order.get('order_no') for order in orders if order.get('order_no')]
|
||||
if not order_nos:
|
||||
return {}
|
||||
|
||||
# 构建小时到时段的映射
|
||||
hour_to_period = {}
|
||||
for period_key, hours in time_periods_config.items():
|
||||
for hour in hours:
|
||||
hour_to_period[hour] = period_key
|
||||
|
||||
if not hour_to_period:
|
||||
return {}
|
||||
|
||||
# 批量查询订单的分时电量
|
||||
placeholders = ', '.join(['%s'] * len(order_nos))
|
||||
sql = f"""
|
||||
SELECT order_no, charge_degree, charge_startr_time, time_flag
|
||||
FROM t_equipment_charge_order_detail
|
||||
WHERE order_no IN ({placeholders})
|
||||
ORDER BY order_no, charge_startr_time
|
||||
"""
|
||||
|
||||
try:
|
||||
details = execute_query(sql, tuple(order_nos))
|
||||
print(f"[时段电量] 查询到 {len(details)} 条分时记录")
|
||||
except Exception as e:
|
||||
print(f"[时段电量] 查询失败: {e}")
|
||||
return {}
|
||||
|
||||
# 按订单号分组计算各时段电量
|
||||
result = {}
|
||||
for order_no in order_nos:
|
||||
result[order_no] = {
|
||||
'sharp_electricity': 0.0,
|
||||
'peak_electricity': 0.0,
|
||||
'flat_electricity': 0.0,
|
||||
'valley_electricity': 0.0
|
||||
}
|
||||
|
||||
for detail in details:
|
||||
order_no = detail.get('order_no')
|
||||
if order_no not in result:
|
||||
continue
|
||||
|
||||
charge_degree = float(detail.get('charge_degree', 0) or 0)
|
||||
charge_start_time = detail.get('charge_startr_time')
|
||||
|
||||
if not charge_start_time:
|
||||
continue
|
||||
|
||||
# 获取小时数
|
||||
if isinstance(charge_start_time, datetime):
|
||||
hour = charge_start_time.hour
|
||||
elif isinstance(charge_start_time, str):
|
||||
try:
|
||||
hour = datetime.strptime(str(charge_start_time), '%Y-%m-%d %H:%M:%S').hour
|
||||
except:
|
||||
continue
|
||||
else:
|
||||
continue
|
||||
|
||||
# 根据小时确定时段
|
||||
period_key = hour_to_period.get(hour)
|
||||
if period_key and f'{period_key}_electricity' in result[order_no]:
|
||||
result[order_no][f'{period_key}_electricity'] += charge_degree
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def get_beijing_time():
|
||||
"""获取北京时间"""
|
||||
beijing_tz = pytz.timezone('Asia/Shanghai')
|
||||
@@ -176,6 +263,24 @@ def generate_daily_report(config_id, start_time=None, end_time=None):
|
||||
'message': f'时间范围内没有符合条件的订单数据(查询条件:{start_time_str} 至 {end_time_str},{config["split_type"]}={config["split_value"]},state=3)'
|
||||
}
|
||||
|
||||
# 解析时段配置并计算分时段电量
|
||||
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}")
|
||||
time_period_electricity = calculate_time_period_electricity(orders, time_periods_config)
|
||||
|
||||
# 将分时段电量添加到订单数据中
|
||||
for order in orders:
|
||||
order_no = order.get('order_no')
|
||||
if order_no and order_no in time_period_electricity:
|
||||
ep = time_period_electricity[order_no]
|
||||
order['sharp_electricity'] = ep['sharp_electricity']
|
||||
order['peak_electricity'] = ep['peak_electricity']
|
||||
order['flat_electricity'] = ep['flat_electricity']
|
||||
order['valley_electricity'] = ep['valley_electricity']
|
||||
|
||||
print(f"[日报生成] 已计算 {len(time_period_electricity)} 条订单的分时段电量")
|
||||
|
||||
# 计算求和
|
||||
sum_results = {}
|
||||
for field in sum_fields:
|
||||
|
||||
Reference in New Issue
Block a user