This commit is contained in:
HuangHai
2026-01-15 16:35:24 +08:00
parent ba46eb15f6
commit 46770fc820
14 changed files with 366 additions and 272 deletions

View File

@@ -6,17 +6,17 @@ import sys
import uuid
from datetime import datetime
# Ensure sys path includes root for imports if not already
project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
if project_root not in sys.path:
sys.path.append(project_root)
from DbKit.Db import Db
from Config.Config import DB_URL
from Config.Config import DB_URL, PRICE_FLATTEN_TO_24H_GLOBAL
from Model.StationProfile import StationProfile
from Model.StationStatus import StationStatus
from Model.StationPriceSchedule import StationPriceSchedule
from Apps.TeLaiDian.Kit import setup_logger
from Apps.TeLaiDian.Config.Setting import PRICE_FLATTEN_TO_24H
# 初始化日志
logger = setup_logger("TeLaiDianService")
@@ -243,22 +243,89 @@ class TeLaiDianService:
now = datetime.now()
station_hash = self.get_hash(station_name, None)
schedule_to_save = hourly_schedule
use_flatten = (PRICE_FLATTEN_TO_24H_GLOBAL or PRICE_FLATTEN_TO_24H) and isinstance(hourly_schedule, list)
if use_flatten:
tmp = [None] * 24
for p in hourly_schedule:
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])
if end_hour == 0 and start_hour != 0:
end_hour = 24
if end_hour < start_hour:
end_hour += 24
price_val = p.get("price")
if price_val is None or price_val == 0:
try:
e = float(p.get("elec_price", 0) or 0)
s = float(p.get("service_price", 0) or 0)
if e > 0 or s > 0:
price_val = e + s
except:
pass
if price_val is None or price_val == 0:
price_val = p.get("plus_price") or p.get("market_price") or 0.0
price_data = {
"price": float(price_val) if price_val is not None else 0.0,
"plus_price": float(p.get("plus_price")) if p.get("plus_price") not in (None, 0) else None,
"market_price": float(p.get("market_price")) if p.get("market_price") not in (None, 0) else None,
"elec_price": float(p.get("elec_price")) if p.get("elec_price") not in (None, 0) else None,
"service_price": float(p.get("service_price")) if p.get("service_price") not in (None, 0) else None,
}
curr = start_hour
while curr < end_hour:
hour_idx = curr % 24
tmp[hour_idx] = {
"start": f"{hour_idx:02d}:00",
"end": f"{(hour_idx + 1):02d}:00",
**price_data,
}
curr += 1
except Exception as e:
logger.error(f"解析价格时段失败: {p}, error: {e}")
last_valid = None
for item in tmp:
if item:
last_valid = item
break
if last_valid:
for i in range(24):
if tmp[i] is None:
new_item = last_valid.copy()
new_item["start"] = f"{i:02d}:00"
new_item["end"] = f"{(i+1):02d}:00"
tmp[i] = new_item
else:
last_valid = tmp[i]
if any(tmp):
schedule_to_save = tmp
async with await self.db.get_session() as session:
schedule_id = self.generate_id()
await self.station_price_schedule_model.save(
session=session,
id=schedule_id,
station_hash=station_hash,
schedule_json=hourly_schedule,
valid_start_time=now
schedule_json=schedule_to_save,
valid_start_time=now,
)
current_hour = now.hour
current_price_info = None
if isinstance(hourly_schedule, list) and 0 <= current_hour < len(hourly_schedule):
current_price_info = hourly_schedule[current_hour] or {}
elif isinstance(hourly_schedule, dict):
current_price_info = hourly_schedule
if isinstance(schedule_to_save, list) and 0 <= current_hour < len(schedule_to_save):
current_price_info = schedule_to_save[current_hour] or {}
elif isinstance(schedule_to_save, dict):
current_price_info = schedule_to_save
else:
current_price_info = {}
@@ -272,12 +339,11 @@ class TeLaiDianService:
piles_detail_json=piles_detail,
parking_info=parking_info,
distance=distance,
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
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()
return True