fix:修复bug

master
HuangHuiKang 2 days ago
parent 2867340282
commit 2f4eee88f3

@ -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"),

@ -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")

@ -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)

@ -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

@ -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

Loading…
Cancel
Save