This commit is contained in:
HuangHai
2026-01-14 09:56:04 +08:00
parent eed2b6f152
commit ee91bf76d2
14 changed files with 233 additions and 67 deletions

View File

@@ -52,18 +52,38 @@ class TeLaiDianService:
now = datetime.now()
# 将价格转换为 24 小时的 schedule 格式 (0-23)
hourly_schedule = [0.0] * 24
# 每个小时存储一个包含多重价格的字典,以记录优惠价、PLUS价和挂牌价
hourly_schedule = [None] * 24
for p in prices:
try:
start_hour = int(p['start'].split(':')[0])
end_hour = int(p['end'].split(':')[0])
price = float(p['price'])
start_parts = p['start'].split(':')
end_parts = p['end'].split(':')
start_hour = int(start_parts[0])
end_hour = int(end_parts[0])
# 处理跨天的情况(如 23:00 - 01:00)
# 处理 00:00 作为结束时间的情况 (表示 24:00)
if end_hour == 0 and (int(end_parts[1]) == 0 if len(end_parts) > 1 else True):
if start_hour != 0:
end_hour = 24
# 提取各项价格
price_data = {
"price": float(p.get('price')) if p.get('price') is not None else 0.0,
"plus_price": float(p.get('plus_price')) if p.get('plus_price') is not None else None,
"market_price": float(p.get('market_price')) if p.get('market_price') is not None else None,
"elec_price": float(p.get('elec_price')) if p.get('elec_price') is not None else None,
"service_price": float(p.get('service_price')) if p.get('service_price') is not None else None
}
# 填充对应的小时槽位
curr = start_hour
while curr != end_hour:
hourly_schedule[curr] = price
curr = (curr + 1) % 24
# 如果是跨天的,比如 23:00 - 01:00
if end_hour < start_hour:
end_hour += 24
while curr < end_hour:
hourly_schedule[curr % 24] = price_data
curr += 1
except Exception as e:
logger.error(f"解析价格时段失败: {p}, error: {e}")
@@ -80,7 +100,7 @@ class TeLaiDianService:
valid_start_time=now
)
# 2. 保存价格
# 2. 保存价格计划
schedule_id = self.generate_id()
await self.station_price_schedule_model.save(
session=session,
@@ -89,6 +109,24 @@ class TeLaiDianService:
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'),
pro_price=current_price_info.get('plus_price'),
market_price=current_price_info.get('market_price'),
valid_start_time=now
)
await session.commit()
logger.info(f"成功保存场站数据: {station_name}")