84 lines
3.0 KiB
Python
84 lines
3.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""助教作废任务"""
|
|
|
|
import json
|
|
|
|
from .base_task import BaseTask
|
|
from loaders.facts.assistant_abolish import AssistantAbolishLoader
|
|
from models.parsers import TypeParser
|
|
|
|
|
|
class AssistantAbolishTask(BaseTask):
|
|
"""同步助教作废记录"""
|
|
|
|
def get_task_code(self) -> str:
|
|
return "ASSISTANT_ABOLISH"
|
|
|
|
def execute(self) -> dict:
|
|
self.logger.info("开始执行 ASSISTANT_ABOLISH 任务")
|
|
window_start, window_end, _ = self._get_time_window()
|
|
params = {
|
|
"storeId": self.config.get("app.store_id"),
|
|
"startTime": TypeParser.format_timestamp(window_start, self.tz),
|
|
"endTime": TypeParser.format_timestamp(window_end, self.tz),
|
|
}
|
|
|
|
try:
|
|
records, _ = self.api.get_paginated(
|
|
endpoint="/Assistant/AbolishList",
|
|
params=params,
|
|
page_size=self.config.get("api.page_size", 200),
|
|
data_path=("data", "abolitionAssistants"),
|
|
)
|
|
|
|
parsed = []
|
|
for raw in records:
|
|
mapped = self._parse_record(raw)
|
|
if mapped:
|
|
parsed.append(mapped)
|
|
|
|
loader = AssistantAbolishLoader(self.db)
|
|
inserted, updated, skipped = loader.upsert_records(parsed)
|
|
|
|
self.db.commit()
|
|
counts = {
|
|
"fetched": len(records),
|
|
"inserted": inserted,
|
|
"updated": updated,
|
|
"skipped": skipped,
|
|
"errors": 0,
|
|
}
|
|
self.logger.info(f"ASSISTANT_ABOLISH 完成: {counts}")
|
|
return self._build_result("SUCCESS", counts)
|
|
except Exception:
|
|
self.db.rollback()
|
|
self.logger.error("ASSISTANT_ABOLISH 失败", exc_info=True)
|
|
raise
|
|
|
|
def _parse_record(self, raw: dict) -> dict | None:
|
|
abolish_id = TypeParser.parse_int(raw.get("id"))
|
|
if not abolish_id:
|
|
self.logger.warning("跳过缺少 id 的助教作废记录: %s", raw)
|
|
return None
|
|
|
|
store_id = self.config.get("app.store_id")
|
|
return {
|
|
"store_id": store_id,
|
|
"abolish_id": abolish_id,
|
|
"table_id": TypeParser.parse_int(raw.get("tableId")),
|
|
"table_name": raw.get("tableName"),
|
|
"table_area_id": TypeParser.parse_int(raw.get("tableAreaId")),
|
|
"table_area": raw.get("tableArea"),
|
|
"assistant_no": raw.get("assistantOn"),
|
|
"assistant_name": raw.get("assistantName"),
|
|
"charge_minutes": TypeParser.parse_int(raw.get("pdChargeMinutes")),
|
|
"abolish_amount": TypeParser.parse_decimal(
|
|
raw.get("assistantAbolishAmount")
|
|
),
|
|
"create_time": TypeParser.parse_timestamp(
|
|
raw.get("createTime") or raw.get("create_time"), self.tz
|
|
),
|
|
"trash_reason": raw.get("trashReason"),
|
|
"raw_data": json.dumps(raw, ensure_ascii=False),
|
|
}
|