b343970ecc
1. 计划过期自动提醒: - scheduler.py 新增 check_overdue_plans() - 每日 9:00 检查过期计划(plan_date < today, status=计划中) - 按经理汇总 → 企微推送提醒消息 2. 拜访后自动关闭计划: - create_visit 后自动将匹配的计划标记为「已完成」 - 条件: customer_id 匹配 + status=计划中 + plan_date <= visit_date 3. 亮灯表批量制定: - 红灯/黄灯卡片右上角 ☐ 复选框 - 勾选后出现「批量制定计划」按钮 - 弹窗统一设置日期+内容 → 批量 POST Co-Authored-By: Claude <noreply@anthropic.com>
105 lines
3.9 KiB
Python
105 lines
3.9 KiB
Python
from contextlib import asynccontextmanager
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
|
from app.config import settings
|
|
from app.database import engine, Base, async_session
|
|
from app.api import router as api_router
|
|
from app.api import auth, users, customers, visits, work_plans, mini_business, key_visits
|
|
from app.api import dashboard, upload, export, import_data, wecom, daily_notes, ai_summary
|
|
from app.services.scheduler import check_daily_reporting, check_overdue_plans
|
|
|
|
_scheduler = AsyncIOScheduler()
|
|
|
|
|
|
async def _scheduled_check():
|
|
"""Wrapper for APScheduler: create a fresh session and run the daily check."""
|
|
async with async_session() as db:
|
|
await check_daily_reporting(db)
|
|
|
|
|
|
async def _scheduled_overdue_check():
|
|
"""Check for overdue plans and remind managers (9:00 AM)."""
|
|
async with async_session() as db:
|
|
await check_overdue_plans(db)
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
# Startup: create tables if not exists (for dev convenience)
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
# Add columns that may be missing from older tables
|
|
await conn.run_sync(lambda c: c.exec_driver_sql(
|
|
"ALTER TABLE customers ADD COLUMN IF NOT EXISTS remarks TEXT DEFAULT ''"
|
|
))
|
|
await conn.run_sync(lambda c: c.exec_driver_sql(
|
|
"ALTER TABLE visits ADD COLUMN IF NOT EXISTS visitor_name VARCHAR(50) DEFAULT ''"
|
|
))
|
|
await conn.run_sync(lambda c: c.exec_driver_sql(
|
|
"ALTER TABLE visits ADD COLUMN IF NOT EXISTS visitor_phone VARCHAR(20) DEFAULT ''"
|
|
))
|
|
# edit_log columns for change tracking
|
|
for tbl in ["visits", "daily_notes", "work_plans", "mini_business", "key_visits"]:
|
|
await conn.run_sync(lambda c, t=tbl: c.exec_driver_sql(
|
|
f"ALTER TABLE {t} ADD COLUMN IF NOT EXISTS edit_log JSONB DEFAULT '[]'"
|
|
))
|
|
# New columns for v0.3
|
|
await conn.run_sync(lambda c: c.exec_driver_sql(
|
|
"ALTER TABLE users ADD COLUMN IF NOT EXISTS require_report BOOLEAN DEFAULT TRUE"
|
|
))
|
|
await conn.run_sync(lambda c: c.exec_driver_sql(
|
|
"ALTER TABLE customers ADD COLUMN IF NOT EXISTS last_visit_date DATE"
|
|
))
|
|
await conn.run_sync(lambda c: c.exec_driver_sql(
|
|
"ALTER TABLE customers ADD COLUMN IF NOT EXISTS last_visit_manager_id UUID"
|
|
))
|
|
|
|
# Start daily reporting scheduler (17:30 CST = 09:30 UTC)
|
|
_scheduler.add_job(_scheduled_check, "cron", hour=17, minute=30, id="daily_check")
|
|
_scheduler.add_job(_scheduled_overdue_check, "cron", hour=9, minute=0, id="overdue_check")
|
|
_scheduler.start()
|
|
|
|
yield
|
|
|
|
# Shutdown
|
|
_scheduler.shutdown(wait=False)
|
|
await engine.dispose()
|
|
|
|
|
|
app = FastAPI(
|
|
title=settings.APP_NAME,
|
|
version="0.1.0",
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
# CORS
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.CORS_ORIGINS,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Mount all routers
|
|
app.include_router(auth.router, prefix="/api")
|
|
app.include_router(users.router, prefix="/api")
|
|
app.include_router(customers.router, prefix="/api")
|
|
app.include_router(visits.router, prefix="/api")
|
|
app.include_router(work_plans.router, prefix="/api")
|
|
app.include_router(mini_business.router, prefix="/api")
|
|
app.include_router(key_visits.router, prefix="/api")
|
|
app.include_router(dashboard.router, prefix="/api")
|
|
app.include_router(upload.router, prefix="/api")
|
|
app.include_router(export.router, prefix="/api")
|
|
app.include_router(import_data.router, prefix="/api")
|
|
app.include_router(wecom.router, prefix="/api")
|
|
app.include_router(daily_notes.router, prefix="/api")
|
|
app.include_router(ai_summary.router, prefix="/api")
|
|
|
|
|
|
@app.get("/health")
|
|
async def health_check():
|
|
return {"status": "ok", "app": settings.APP_NAME}
|