From c15f3e9925df6b8c72b76fc28b1c1a231094d0e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=96=B9=E6=88=90?= Date: Wed, 26 Nov 2025 18:17:47 +0800 Subject: [PATCH] =?UTF-8?q?fix(invoice):=20=E7=A7=BB=E9=99=A4=E5=8F=91?= =?UTF-8?q?=E7=A5=A8=E5=88=9B=E5=BB=BA=E6=A8=A1=E5=9E=8B=E4=B8=AD=E4=B8=8D?= =?UTF-8?q?=E5=BF=85=E8=A6=81=E7=9A=84=E5=BF=85=E5=A1=AB=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refactor(valuations): 添加估值计算前的用户配额检查逻辑 docs: 添加发票抬头必填字段的修改方案文档 --- .trae/documents/修复添加发票抬头必填项校验.md | 34 +++++++++++++++++++ app/api/v1/app_valuations/app_valuations.py | 23 ++++++++++++- app/schemas/invoice.py | 8 ++--- 3 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 .trae/documents/修复添加发票抬头必填项校验.md diff --git a/.trae/documents/修复添加发票抬头必填项校验.md b/.trae/documents/修复添加发票抬头必填项校验.md new file mode 100644 index 0000000..6b90a13 --- /dev/null +++ b/.trae/documents/修复添加发票抬头必填项校验.md @@ -0,0 +1,34 @@ +## 目标 + +* 添加抬头时将 `公司名称`、`公司税号`、`电子邮箱`设为必填,其他字段可为空。 + +* 现状确认(代码引用) + +- 后端必填:`app/schemas/invoice.py:6–12` 中 `InvoiceHeaderCreate` 已要求 `company_name`、`tax_number` 为必填,`email: EmailStr` 为必填。 + +- API 入口:`app/api/v1/app_invoices/app_invoices.py:55–58` 新增抬头接口使用 `InvoiceHeaderCreate`,后端将严格校验三项必填。 + +## 修改方案 + +1. 统一前端校验文案 + + * 统一三项必填的错误提示为简洁中文,如:“请输入公司名称 / 公司税号 / 电子邮箱”。 + + * 邮箱格式提示统一为:“请输入有效的电子邮箱”。 + +2. 后端校验与返回确认 + + * 保持 `InvoiceHeaderCreate` 的必填与格式限制不变(`app/schemas/invoice.py:6–12`)。 + + * 确认更新接口 `InvoiceHeaderUpdate`(`app/schemas/invoice.py:32–39`)允许局部更新、但不影响创建必填逻辑。 + +3. 验证与测试 + + * 后端接口验证:对 `POST /app-invoices/headers`(`app/api/v1/app_invoices/app_invoices.py:55–58`)进行用例:缺失任一必填字段应返回 422;全部正确应 200/201。 + + * 可补充最小化单元测试:Pydantic 校验用例覆盖必填与格式。 + +## 交付内容。 + +* 完成基本交互与接口验证,确保行为符合预期。 + diff --git a/app/api/v1/app_valuations/app_valuations.py b/app/api/v1/app_valuations/app_valuations.py index 1288593..837c449 100644 --- a/app/api/v1/app_valuations/app_valuations.py +++ b/app/api/v1/app_valuations/app_valuations.py @@ -301,7 +301,28 @@ async def calculate_valuation( """ try: - # 添加后台任务 + from app.models.user import AppUser, AppUserQuotaLog + user = await AppUser.filter(id=user_id).first() + if not user: + raise HTTPException(status_code=404, detail="用户不存在") + if (user.remaining_quota or 0) < 1: + raise HTTPException(status_code=400, detail="估值次数不足") + before = user.remaining_quota or 0 + user.remaining_quota = before - 1 + await user.save() + try: + await AppUserQuotaLog.create( + app_user_id=user_id, + operator_id=user_id, + operator_name=user.alias or user.username or user.phone or "", + before_count=before, + after_count=before - 1, + op_type="consume", + remark="发起估值" + ) + except Exception: + pass + background_tasks.add_task(_perform_valuation_calculation, user_id, data) logger.info("valuation.task_queued user_id={} asset_name={} industry={}", diff --git a/app/schemas/invoice.py b/app/schemas/invoice.py index 31cc2d4..70765aa 100644 --- a/app/schemas/invoice.py +++ b/app/schemas/invoice.py @@ -46,10 +46,10 @@ class InvoiceCreate(BaseModel): email: EmailStr company_name: str = Field(..., min_length=1, max_length=128) tax_number: str = Field(..., min_length=1, max_length=32) - register_address: str = Field(..., min_length=1, max_length=256) - register_phone: str = Field(..., min_length=1, max_length=32) - bank_name: str = Field(..., min_length=1, max_length=128) - bank_account: str = Field(..., min_length=1, max_length=64) + register_address: str = Field(..., max_length=256) + register_phone: str = Field(..., max_length=32) + bank_name: str = Field(..., max_length=128) + bank_account: str = Field(..., max_length=64) app_user_id: Optional[int] = None header_id: Optional[int] = None wechat: Optional[str] = None