feat(server): 工程骨架后端——上传 → 解析 → 清单最小链路
- core:配置(prod 拒绝默认密钥 / dev 短信 / 假模型)、租户事务 + 行级安全策略、 problem+json 错误码、信封加密与签名下载、对象存储、事务内入队、Valkey stream 事件流、权限点 - identity:手机号 HMAC 存储、注册、登录(挤下线需确认,API3)、服务端会话 - billing:额度流水只追加,预扣 / 结算 / 退回,一笔预扣只有一个结局 - document:分片上传、按文件头识别格式、Gotenberg 转换、文字层与稳定句子编号 - checklist:标注规则识别、模型抽取、三道校验、标注类条款规则兜底、重跑对齐 - ai:提示词注册表(extract.substantive v1 来自 S4 v2)、假模型、OpenAI 兼容供应商、ai_call 流水 - 迁移:data-model 基线、套餐、Procrastinate、会话状态函数 - 测试 30 个:租户隔离、额度、失败退回、对象加密、契约一致性 - Compose(含 migrate)、镜像、CI Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
85
server/src/bid_doctor/project/service.py
Normal file
85
server/src/bid_doctor/project/service.py
Normal file
@@ -0,0 +1,85 @@
|
||||
"""项目、项目成员、分包(FR-M6-01、02)。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from bid_doctor.audit import service as audit
|
||||
from bid_doctor.billing import service as billing
|
||||
from bid_doctor.core.authz import Permission, require
|
||||
from bid_doctor.core.db import TenantContext
|
||||
from bid_doctor.core.errors import AppError
|
||||
from bid_doctor.core.ids import uuid7
|
||||
from bid_doctor.project.models import Lot, Project, ProjectMember
|
||||
|
||||
UNNAMED = "未命名项目"
|
||||
|
||||
|
||||
async def create(s: AsyncSession, ctx: TenantContext, name: str | None) -> Project:
|
||||
require(ctx, Permission.PROJECT_CREATE)
|
||||
_, plan = await billing.current_plan(s, ctx)
|
||||
limit = plan.limits.get("max_projects")
|
||||
if limit is not None:
|
||||
n = (await s.execute(select(func.count()).select_from(Project))).scalar_one()
|
||||
if n >= limit:
|
||||
raise AppError("plan_limit_exceeded", f"{plan.name}最多 {limit} 个项目")
|
||||
p = Project(id=uuid7(), tenant_id=ctx.tenant_id, name=(name or "").strip() or UNNAMED, created_by=ctx.user_id)
|
||||
s.add(p)
|
||||
await s.flush()
|
||||
s.add(
|
||||
ProjectMember(tenant_id=ctx.tenant_id, project_id=p.id, user_id=ctx.user_id, role="lead", added_by=ctx.user_id)
|
||||
)
|
||||
# 没有分包时也有一个默认包(04 §2);解析出分包后替换
|
||||
s.add(Lot(id=uuid7(), tenant_id=ctx.tenant_id, project_id=p.id, lot_no="默认", is_default=True, origin="ai"))
|
||||
await audit.record(s, ctx, "project.create", "project", p.id, project_id=p.id, after={"name": p.name})
|
||||
await s.flush()
|
||||
await s.refresh(p)
|
||||
return p
|
||||
|
||||
|
||||
async def get(s: AsyncSession, ctx: TenantContext, project_id: UUID) -> Project:
|
||||
"""按可见范围取项目(AR12):MVP 只有租户这一层,交给行级安全策略;以后在这里加项目成员条件。"""
|
||||
del ctx
|
||||
p = (await s.execute(select(Project).where(Project.id == project_id))).scalar_one_or_none()
|
||||
if p is None:
|
||||
raise AppError("not_found")
|
||||
return p
|
||||
|
||||
|
||||
def ensure_editable(p: Project) -> None:
|
||||
if p.status in ("submitted", "archived"):
|
||||
raise AppError("project_read_only")
|
||||
|
||||
|
||||
async def mark_drafting(s: AsyncSession, p: Project) -> None:
|
||||
"""首份招标文件解析成功:跟进中 → 编制中(04 §3.1)。"""
|
||||
if p.status == "tracking":
|
||||
p.status = "drafting"
|
||||
|
||||
|
||||
async def list_projects(s: AsyncSession, ctx: TenantContext, limit: int) -> list[tuple[Project, list[str]]]:
|
||||
"""只读本模块的表;工作台要的其他模块信息由 workbench 组合(模块之间不直接读写对方的表,架构 §3.2)。"""
|
||||
del ctx
|
||||
projects = (
|
||||
(
|
||||
await s.execute(
|
||||
select(Project).order_by(Project.deadline_at.asc().nulls_last(), Project.created_at.desc()).limit(limit)
|
||||
)
|
||||
)
|
||||
.scalars()
|
||||
.all()
|
||||
)
|
||||
lots: dict[Any, list[str]] = {}
|
||||
for pid, lot_no in (
|
||||
await s.execute(
|
||||
select(Lot.project_id, Lot.lot_no)
|
||||
.where(Lot.project_id.in_([p.id for p in projects]), Lot.is_bidding, Lot.is_default.is_(False))
|
||||
.order_by(Lot.sort_order)
|
||||
)
|
||||
).tuples():
|
||||
lots.setdefault(pid, []).append(lot_no)
|
||||
return [(p, lots.get(p.id, [])) for p in projects]
|
||||
Reference in New Issue
Block a user