a1886074dd
后端: FastAPI + SQLAlchemy 2.0 (async) + Alembic + MinIO + Casdoor + 企微 前端: Vue 3 + Vite + TypeScript + Element Plus + Pinia 功能清单: - 8 张数据表自动建表 / Casdoor OIDC 登录 / 企微静默登录 - 双布局: 移动端(填报) + PC端(汇总管理) - 拜访记录 CRUD + MinIO 照片直传 + 缩略图预览 + 同访人草稿 - 今日纪要 (6 分类) / 工作计划 / 小微商机 / 要客拜访 CRUD - 客户档案: 备注/收支费用/联系人/归属分配/批量转移 - 客户导入导出 + 模板下载 + 搜索/分页/筛选 - 仪表盘: 四卡统计 + 填报进度 (拜访+纪要双维度) - 周报详情: 五 Tab + 按人/客户筛选 + 时间轴 - 用户管理 / 客户经理 PC 端工作台 - 企微: 催办/公告/定时提醒 / 时区修正 - Docker 部署配置 Co-Authored-By: Claude <noreply@anthropic.com>
132 lines
4.7 KiB
Python
132 lines
4.7 KiB
Python
import uuid
|
|
from datetime import date
|
|
from typing import Optional
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy import select
|
|
from app.database import get_db
|
|
from app.middleware.auth import get_current_user, require_any_role
|
|
from app.models.daily_note import DailyNote
|
|
from app.models.user import User
|
|
from app.schemas.daily_note import DailyNoteCreate, DailyNoteUpdate, DailyNoteOut
|
|
from app.utils.timezone import today_cst, parse_date
|
|
|
|
router = APIRouter(prefix="/daily-notes", tags=["DailyNotes"])
|
|
|
|
|
|
async def _enrich(note: DailyNote, db: AsyncSession) -> dict:
|
|
mgr = await db.execute(select(User.name).where(User.id == note.manager_id))
|
|
return {
|
|
"id": note.id, "manager_id": note.manager_id,
|
|
"note_date": note.note_date, "category": note.category,
|
|
"content": note.content, "time_range": note.time_range,
|
|
"created_at": note.created_at, "updated_at": note.updated_at,
|
|
"manager_name": mgr.scalar_one_or_none(),
|
|
}
|
|
|
|
|
|
@router.get("/")
|
|
async def list_notes(
|
|
date_from: Optional[str] = Query(None),
|
|
date_to: Optional[str] = Query(None),
|
|
current_user: dict = Depends(get_current_user),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
query = select(DailyNote)
|
|
if current_user["role"] == "manager":
|
|
query = query.where(DailyNote.manager_id == uuid.UUID(current_user["user_id"]))
|
|
if date_from:
|
|
query = query.where(DailyNote.note_date >= parse_date(date_from))
|
|
if date_to:
|
|
query = query.where(DailyNote.note_date <= parse_date(date_to))
|
|
query = query.order_by(DailyNote.note_date.desc(), DailyNote.created_at.desc()).limit(100)
|
|
result = await db.execute(query)
|
|
return [await _enrich(n, db) for n in result.scalars().all()]
|
|
|
|
|
|
@router.get("/today")
|
|
async def list_today_notes(
|
|
current_user: dict = Depends(get_current_user),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
query = select(DailyNote).where(DailyNote.note_date == today_cst())
|
|
if current_user["role"] == "manager":
|
|
query = query.where(DailyNote.manager_id == uuid.UUID(current_user["user_id"]))
|
|
result = await db.execute(query)
|
|
notes = [await _enrich(n, db) for n in result.scalars().all()]
|
|
return {"count": len(notes), "notes": notes}
|
|
|
|
|
|
@router.get("/{note_id}")
|
|
async def get_note(
|
|
note_id: str,
|
|
current_user: dict = Depends(get_current_user),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
result = await db.execute(select(DailyNote).where(DailyNote.id == note_id))
|
|
note = result.scalar_one_or_none()
|
|
if not note:
|
|
raise HTTPException(status_code=404, detail="Not found")
|
|
if current_user["role"] == "manager" and str(note.manager_id) != current_user["user_id"]:
|
|
raise HTTPException(status_code=403, detail="Access denied")
|
|
return await _enrich(note, db)
|
|
|
|
|
|
@router.post("/")
|
|
async def create_note(
|
|
data: DailyNoteCreate,
|
|
current_user: dict = Depends(require_any_role),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
note = DailyNote(
|
|
manager_id=uuid.UUID(current_user["user_id"]),
|
|
note_date=parse_date(data.note_date),
|
|
category=data.category,
|
|
content=data.content,
|
|
time_range=data.time_range,
|
|
)
|
|
db.add(note)
|
|
await db.commit()
|
|
await db.refresh(note)
|
|
return await _enrich(note, db)
|
|
|
|
|
|
@router.put("/{note_id}")
|
|
async def update_note(
|
|
note_id: str, data: DailyNoteUpdate,
|
|
current_user: dict = Depends(require_any_role),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
result = await db.execute(select(DailyNote).where(DailyNote.id == note_id))
|
|
note = result.scalar_one_or_none()
|
|
if not note:
|
|
raise HTTPException(status_code=404, detail="Not found")
|
|
if current_user["role"] == "manager" and str(note.manager_id) != current_user["user_id"]:
|
|
raise HTTPException(status_code=403, detail="Access denied")
|
|
|
|
update_data = data.model_dump(exclude_unset=True)
|
|
if "note_date" in update_data and update_data["note_date"]:
|
|
update_data["note_date"] = parse_date(update_data["note_date"])
|
|
for k, v in update_data.items():
|
|
setattr(note, k, v)
|
|
await db.commit()
|
|
await db.refresh(note)
|
|
return await _enrich(note, db)
|
|
|
|
|
|
@router.delete("/{note_id}")
|
|
async def delete_note(
|
|
note_id: str,
|
|
current_user: dict = Depends(require_any_role),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
result = await db.execute(select(DailyNote).where(DailyNote.id == note_id))
|
|
note = result.scalar_one_or_none()
|
|
if not note:
|
|
raise HTTPException(status_code=404, detail="Not found")
|
|
if current_user["role"] == "manager" and str(note.manager_id) != current_user["user_id"]:
|
|
raise HTTPException(status_code=403, detail="Access denied")
|
|
await db.delete(note)
|
|
await db.commit()
|
|
return {"detail": "deleted"}
|