This commit is contained in:
HuangHai
2026-01-16 19:15:40 +08:00
parent a565d9c45d
commit c7cf0db77e
18 changed files with 232 additions and 12 deletions

View File

@@ -1,4 +1,5 @@
# coding=utf-8
import asyncio
import hashlib
import logging
import os
@@ -11,14 +12,23 @@ if project_root not in sys.path:
sys.path.append(project_root)
from DbKit.Db import Db
from Config.Config import DB_URL, PRICE_FLATTEN_TO_24H_GLOBAL
try:
from Config.Config import DB_URL, PRICE_FLATTEN_TO_24H_GLOBAL
except ModuleNotFoundError:
import importlib.util
_config_path_cfg = os.path.join(project_root, "Config", "Config.py")
_spec_cfg = importlib.util.spec_from_file_location("project_config_fallback", _config_path_cfg)
_cfg_mod = importlib.util.module_from_spec(_spec_cfg)
assert _spec_cfg.loader is not None
_spec_cfg.loader.exec_module(_cfg_mod)
DB_URL = _cfg_mod.DB_URL
PRICE_FLATTEN_TO_24H_GLOBAL = _cfg_mod.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")
class TeLaiDianService:
@@ -347,3 +357,52 @@ class TeLaiDianService:
await session.commit()
return True
async def save_station_profile_and_status(self, station_name, address, total_piles=None, free_piles=None, piles_detail=None, parking_info=None, distance=None):
if not station_name:
return False
now = datetime.now()
station_hash = self.get_hash(station_name, address)
async with await self.db.get_session() as session:
profile_id = self.generate_id()
await self.station_profile_model.save(
session=session,
id=profile_id,
station_hash=station_hash,
operator=self.operator,
station_name=station_name,
address=address,
valid_start_time=now
)
status_id = self.generate_id()
await self.station_status_model.save(
session=session,
id=status_id,
station_hash=station_hash,
total_piles=total_piles,
free_piles=free_piles,
piles_detail_json=piles_detail,
parking_info=parking_info,
distance=distance,
current_price=None,
valid_start_time=now
)
await session.commit()
return True
async def _test_save_profile():
service = TeLaiDianService()
await service.init_db()
try:
ok = await service.save_station_profile_only(
"测试场站-AI调试",
"测试地址-杭州滨江",
"1.2km"
)
print("save_station_profile_only_return:", ok)
finally:
await service.close_db()
if __name__ == "__main__":
asyncio.run(_test_save_profile())