feature.py 3.02 KB
"""库存补货功能登记:每天 15:00 出库驱动的定时补货(页面确认清单 → 建单截图 → 页面确认提交)
+ 群里一句话的单次补货(回「订」建单截图 → 发起人回「确认」提交);每周同步近 N 个月 SO 单;截单时间线与告警。"""
from __future__ import annotations

from ...services import texts
from .. import Feature, JobSpec, register
from .config import RestockConfig
from .texts import TEXTS, VARS, WHEN


class RestockFeature(Feature):
    key = "restock"
    name = "库存补货"
    description = "定时补货(出库明细 → 补货推荐,页面确认)/ 群里单次补货 → DMS 建单截图 → 人工确认后提交"
    dms = ("gtmc",)
    config_model = RestockConfig
    edge_tasks = ("restock.outbound", "restock.quote", "restock.so_sync", "restock.create_po", "restock.screenshot_po",
                  "restock.submit_po", "restock.export_history", "restock.find_po")

    @property
    def svc(self):
        from .service import svc
        return svc

    async def on_startup(self) -> None:
        from ... import features
        from . import categories, exports
        exports.recover()
        await self.svc.recover()
        for sid in features.enabled_stores(self.key):
            categories.ensure_seeded(sid)

    def router(self):
        from .api import router
        return router

    def public_router(self):
        from .api import public
        return public

    def tools(self):
        from .tools import TOOLS
        return TOOLS

    def fallback_intent(self, text: str):
        from .tools import fallback_intent
        return fallback_intent(text)

    def sandbox_tools(self, box):
        from .sandbox import tools
        return tools(box)

    def sandbox_view(self, box):
        from .sandbox import view
        return view(box)

    def refuse_hint(self, store_id: int) -> str:
        single = self.svc.single_open(store_id)
        if single and single["status"] in ("awaiting_confirm", "awaiting_submit"):
            return texts.render("restock.refuse_hint", status=single["status"], order_no=single["order_no"] or "")
        return texts.render("restock.refuse_hint_idle")

    def jobs(self):
        s = self.svc
        return [
            JobSpec("preflight", "补货预检", lambda c: list(c.preflight_times), s.preflight, deadline=lambda c: c.cutoff),
            JobSpec("push", "定时推送补货清单", lambda c: [c.push_time] if c.auto_push else [], s.push,
                    deadline=lambda c: c.cutoff),
            JobSpec("close", "截单收口", lambda c: [c.cutoff], s.close, deadline=lambda c: "23:59"),
            JobSpec("so_sync", "同步近期 SO 单", lambda c: [c.so_sync_time], s.so_sync, deadline=lambda c: "23:59",
                    weekdays=lambda c: list(c.so_sync_weekdays)),
        ]

    async def on_tick(self, store_id, now) -> None:
        await self.svc.tick(store_id, now)


texts.register(TEXTS, module="库存补货", when=WHEN)
texts.VAR_DESC.update(VARS)
feature = register(RestockFeature())