This commit is contained in:
HuangHai
2026-01-15 11:46:59 +08:00
parent f038929687
commit 8cbcca472b
11 changed files with 178 additions and 41 deletions

View File

@@ -90,16 +90,71 @@ class YiLaiTeService:
except Exception as e:
logger.error(f"更新任务结束日志失败: {e}")
async def process_price_detail_data(self, station_name, hourly_schedule, address=None) -> bool:
async def process_price_detail_data(self, station_name, prices, address=None, distance=None) -> bool:
"""
直接保存已处理好的小时段价格数据
保存场站数据:名称、地址、24小时价格计划、距离
"""
if not station_name or not hourly_schedule:
if not station_name or not prices:
return False
station_hash = self.get_hash(station_name)
now = datetime.now()
# 1. 预处理价格:生成 24 小时的价格映射
hourly_schedule = [None] * 24
for p in prices:
try:
start_str = p.get('start') or '00:00'
end_str = p.get('end') or '00:00'
# 解析小时
start_hour = int(start_str.split(':')[0])
end_hour = int(end_str.split(':')[0])
# 处理 00:00 作为结束时间
if end_hour == 0 and start_hour != 0:
end_hour = 24
elif end_hour == 0 and start_hour == 0 and len(prices) == 1:
end_hour = 24
# 如果跨天
if end_hour < start_hour:
end_hour += 24
price_val = self._to_float(p.get('price'))
curr = start_hour
while curr < end_hour:
hour_idx = curr % 24
hourly_schedule[hour_idx] = {
"start": f"{hour_idx:02d}:00",
"end": f"{(hour_idx + 1):02d}:00",
"price": price_val
}
curr += 1
except Exception as e:
logger.error(f"解析价格时段失败: {p}, error: {e}")
# 2. 兜底填充 (补全 24 小时)
last_valid = None
for item in hourly_schedule:
if item:
last_valid = item
break
if last_valid:
for i in range(24):
if hourly_schedule[i] is None:
new_item = last_valid.copy()
new_item["start"] = f"{i:02d}:00"
new_item["end"] = f"{(i+1):02d}:00"
hourly_schedule[i] = new_item
else:
last_valid = hourly_schedule[i]
else:
logger.warning(f"场站 {station_name} 未能生成有效的 24 小时价格表")
async with await self.db.get_session() as session:
# 1. 保存 Profile (如果不存在)
profile_id = self.generate_id()
@@ -122,7 +177,27 @@ class YiLaiTeService:
schedule_json=hourly_schedule,
valid_start_time=now
)
# 3. 保存状态 (记录距离和当前价格)
current_hour = now.hour
current_price_info = hourly_schedule[current_hour] or {}
status_id = self.generate_id()
await self.station_status_model.save(
session=session,
id=status_id,
station_hash=station_hash,
total_piles=None,
free_piles=None,
piles_detail_json=None,
current_price=current_price_info.get('price'),
distance=distance,
valid_start_time=now
)
await session.commit()
logger.info(f"成功保存驿来特场站数据: {station_name} | 距离: {distance} | 价格条数: {len([x for x in hourly_schedule if x])}")
return True
async def process_station_list_vl(self, image_path, json_metadata, device_info=None, max_count=None) -> list: