From 2f4eee88f327298d17b8b1c0094e8c999ef72b6d Mon Sep 17 00:00:00 2001 From: HuangHuiKang Date: Thu, 9 Jul 2026 17:33:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8Dbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../v1/module_energy/report_review/model.py | 2 +- .../v1/module_energy/report_review/schema.py | 10 ++++---- .../v1/module_energy/report_template/model.py | 5 ++-- .../module_energy/report_template/schema.py | 1 + .../module_energy/report_template/service.py | 24 ++++++++++++++++++- 5 files changed, 32 insertions(+), 10 deletions(-) diff --git a/app/api/v1/module_energy/report_review/model.py b/app/api/v1/module_energy/report_review/model.py index 529abef..26c3366 100644 --- a/app/api/v1/module_energy/report_review/model.py +++ b/app/api/v1/module_energy/report_review/model.py @@ -34,7 +34,7 @@ class EnergyReportReviewModel(ModelMixin, TenantMixin, UserMixin): index=True, comment="审核文档文件ID", ) - status: Mapped[int] = mapped_column(Integer, default=0, nullable=False, comment="状态 0:待审查 1:已审查 2:审查失败", index=True) + status: Mapped[int] = mapped_column(Integer, default=0, nullable=False, comment="状态 0:待审查 1:审核通过 2:审核不通过", index=True) reviewer_id: Mapped[int | None] = mapped_column( Integer, ForeignKey("sys_user.id", ondelete="SET NULL", onupdate="CASCADE"), diff --git a/app/api/v1/module_energy/report_review/schema.py b/app/api/v1/module_energy/report_review/schema.py index b0994c0..2bb7665 100644 --- a/app/api/v1/module_energy/report_review/schema.py +++ b/app/api/v1/module_energy/report_review/schema.py @@ -17,7 +17,7 @@ class EnergyReportReviewCreateSchema(BaseModel): task_name: str = Field(..., min_length=1, max_length=128, description="任务名称") template_id: int = Field(..., ge=1, description="能源报告模板ID") file_id: int = Field(..., ge=1, description="上传文档文件ID") - status: int = Field(default=0, ge=0, le=2, description="状态 0:待审查 1:已审查 2:审查失败") + status: int = Field(default=0, ge=0, le=2, description="状态 0:待审查 1:审核通过 2:审核不通过") reviewer_id: int | None = Field(default=None, ge=1, description="审查人ID") description: str | None = Field(default=None, max_length=500, description="描述") @@ -33,7 +33,7 @@ class EnergyReportReviewCreateSchema(BaseModel): @classmethod def validate_status(cls, value: int) -> int: if value not in {0, 1, 2}: - raise ValueError("状态仅支持 0(待审查)、1(已审查)、2(审查失败)") + raise ValueError("状态仅支持 0(待审查)、1(审核通过)、2(审核不通过)") return value @@ -59,9 +59,9 @@ class EnergyReportReviewStatsSchema(BaseModel): """能源报告审核任务统计模型。""" submitted_count: int = Field(default=0, description="提交任务数") - reviewed_count: int = Field(default=0, description="已审查任务数") + reviewed_count: int = Field(default=0, description="审核通过任务数") pending_count: int = Field(default=0, description="待审查任务数") - failed_count: int = Field(default=0, description="审查失败任务数") + failed_count: int = Field(default=0, description="审核不通过任务数") class EnergyReportReviewPageOutSchema(BaseModel): @@ -80,7 +80,7 @@ class EnergyReportReviewQueryParam(BaseQueryParam, UserByQueryParam, TenantByQue """能源报告审核任务查询参数。""" task_name: str | None = Query(None, description="任务名称") - status: int | None = Query(None, ge=0, le=2, description="状态 0:待审查 1:已审查 2:审查失败") + status: int | None = Query(None, ge=0, le=2, description="状态 0:待审查 1:审核通过 2:审核不通过") template_id: int | None = Query(None, ge=1, description="模板ID") reviewer_id: int | None = Query(None, ge=1, description="审查人ID") diff --git a/app/api/v1/module_energy/report_template/model.py b/app/api/v1/module_energy/report_template/model.py index 07df493..9c6a12f 100644 --- a/app/api/v1/module_energy/report_template/model.py +++ b/app/api/v1/module_energy/report_template/model.py @@ -1,4 +1,4 @@ -from sqlalchemy import ForeignKey, Integer, String, Text, UniqueConstraint +from sqlalchemy import ForeignKey, Integer, String, Text from sqlalchemy.orm import Mapped, mapped_column, relationship from app.core.base_model import ModelMixin, TenantMixin, UserMixin @@ -8,7 +8,7 @@ class EnergyReportTemplateModel(ModelMixin, TenantMixin, UserMixin): """能源报告模板模型。""" __tablename__: str = "energy_report_template" - __table_args__ = (UniqueConstraint("tenant_id", "template_name"), {"comment": "能源报告模板表"}) + __table_args__: dict[str, str] = {"comment": "能源报告模板表"} __loader_options__: list[str] = [ "file", "created_by", @@ -32,4 +32,3 @@ class EnergyReportTemplateModel(ModelMixin, TenantMixin, UserMixin): description: Mapped[str | None] = mapped_column(Text, default=None, nullable=True, comment="描述") file = relationship("FileModel", lazy="selectin", foreign_keys=[file_id], uselist=False) - diff --git a/app/api/v1/module_energy/report_template/schema.py b/app/api/v1/module_energy/report_template/schema.py index 09bbceb..8df2837 100644 --- a/app/api/v1/module_energy/report_template/schema.py +++ b/app/api/v1/module_energy/report_template/schema.py @@ -49,6 +49,7 @@ class EnergyReportTemplateOutSchema(EnergyReportTemplateCreateSchema, BaseSchema file_url: str | None = Field(default=None, description="文件访问地址") original_name: str | None = Field(default=None, description="原始文件名") object_name: str | None = Field(default=None, description="MinIO对象名称") + annotation_count: int = Field(default=0, description="标注数量") @dataclass diff --git a/app/api/v1/module_energy/report_template/service.py b/app/api/v1/module_energy/report_template/service.py index a664843..12043b3 100644 --- a/app/api/v1/module_energy/report_template/service.py +++ b/app/api/v1/module_energy/report_template/service.py @@ -1,6 +1,7 @@ -from sqlalchemy import select +from sqlalchemy import func, select from app.api.v1.module_common.file.crud import FileCRUD +from app.api.v1.module_energy.chapter.model import EnergyReportChapterModel from app.core.base_schema import AuthSchema from app.core.exceptions import CustomException @@ -41,11 +42,15 @@ class EnergyReportTemplateService: search=vars(search) if search else None, out_schema=EnergyReportTemplateOutSchema, ) + annotation_count_map = await self._annotation_count_map( + template_ids=[item.get("id") for item in page_result.items if item.get("id")], + ) for item in page_result.items: file_info = item.get("file") or {} item["file_url"] = file_info.get("file_url") item["original_name"] = file_info.get("original_name") item["object_name"] = file_info.get("object_name") + item["annotation_count"] = annotation_count_map.get(item.get("id"), 0) return page_result async def create(self, data: EnergyReportTemplateCreateSchema) -> EnergyReportTemplateOutSchema: @@ -95,6 +100,7 @@ class EnergyReportTemplateService: raise CustomException(msg="数据库会话不存在") sql = select(EnergyReportTemplateModel.id).where( EnergyReportTemplateModel.template_name == template_name, + EnergyReportTemplateModel.is_deleted == False, ) if tenant_id is not None: sql = sql.where(EnergyReportTemplateModel.tenant_id == tenant_id) @@ -104,6 +110,22 @@ class EnergyReportTemplateService: if result.scalar_one_or_none() is not None: raise CustomException(msg="能源报告模板名称已存在") + + async def _annotation_count_map(self, template_ids: list[int]) -> dict[int, int]: + if not template_ids: + return {} + if self.auth.db is None: + raise CustomException(msg="数据库会话不存在") + result = await self.auth.db.execute( + select(EnergyReportChapterModel.template_id, func.count(EnergyReportChapterModel.id)) + .where( + EnergyReportChapterModel.template_id.in_(template_ids), + EnergyReportChapterModel.is_deleted == False, + ) + .group_by(EnergyReportChapterModel.template_id) + ) + return {template_id: count for template_id, count in result.all()} + def _current_tenant_id(self) -> int | None: if not self.auth.user: return self.auth.tenant_id