|
|
|
|
@ -1,12 +1,15 @@
|
|
|
|
|
from app.api.v1.module_common.file.crud import FileCRUD
|
|
|
|
|
from app.api.v1.module_energy.report_template.crud import EnergyReportTemplateCRUD
|
|
|
|
|
from app.api.v1.module_system.user.crud import UserCRUD
|
|
|
|
|
from app.core.base_schema import AuthSchema
|
|
|
|
|
from app.core.base_schema import AuthSchema, PageResultSchema
|
|
|
|
|
from app.core.exceptions import CustomException
|
|
|
|
|
|
|
|
|
|
from .crud import EnergyReportReviewCRUD
|
|
|
|
|
from .crud import EnergyReportReviewCRUD, EnergyReportReviewLogCRUD
|
|
|
|
|
from .schema import (
|
|
|
|
|
EnergyReportReviewCreateSchema,
|
|
|
|
|
EnergyReportReviewLogCreateSchema,
|
|
|
|
|
EnergyReportReviewLogOutSchema,
|
|
|
|
|
EnergyReportReviewLogQueryParam,
|
|
|
|
|
EnergyReportReviewOutSchema,
|
|
|
|
|
EnergyReportReviewQueryParam,
|
|
|
|
|
EnergyReportReviewStatsSchema,
|
|
|
|
|
@ -15,6 +18,13 @@ from .schema import (
|
|
|
|
|
|
|
|
|
|
ENERGY_REPORT_REVIEW_BUSINESS_TYPE = "energy_report_review"
|
|
|
|
|
|
|
|
|
|
REVIEW_LOG_ACTION_NAMES = {
|
|
|
|
|
"create_task": "创建审查任务",
|
|
|
|
|
"start_review": "开始审查",
|
|
|
|
|
"review_pass": "审核通过",
|
|
|
|
|
"review_reject": "审核不通过",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class EnergyReportReviewService:
|
|
|
|
|
"""能源报告审核任务服务。"""
|
|
|
|
|
@ -59,6 +69,12 @@ class EnergyReportReviewService:
|
|
|
|
|
await self._validate_reviewer(reviewer_id=data.reviewer_id)
|
|
|
|
|
review = await EnergyReportReviewCRUD(self.auth).create(data=data)
|
|
|
|
|
await self._bind_file(file_id=data.file_id, review_id=review.id)
|
|
|
|
|
await EnergyReportReviewLogService(self.auth).create_system_log(
|
|
|
|
|
report_review_id=review.id,
|
|
|
|
|
task_name=review.task_name,
|
|
|
|
|
action_type="create_task",
|
|
|
|
|
description=f"创建审查任务:{review.task_name}",
|
|
|
|
|
)
|
|
|
|
|
return self._to_out_schema(review)
|
|
|
|
|
|
|
|
|
|
async def update(self, id: int, data: EnergyReportReviewUpdateSchema) -> EnergyReportReviewOutSchema:
|
|
|
|
|
@ -154,3 +170,83 @@ class EnergyReportReviewService:
|
|
|
|
|
return
|
|
|
|
|
if file_record.business_type == ENERGY_REPORT_REVIEW_BUSINESS_TYPE and file_record.business_id == review_id:
|
|
|
|
|
await FileCRUD(self.auth).set(ids=[file_id], business_id=None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class EnergyReportReviewLogService:
|
|
|
|
|
"""能源报告审核操作记录服务。"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, auth: AuthSchema) -> None:
|
|
|
|
|
self.auth = auth
|
|
|
|
|
|
|
|
|
|
async def detail(self, id: int) -> EnergyReportReviewLogOutSchema:
|
|
|
|
|
log = await EnergyReportReviewLogCRUD(self.auth).get_or_404(id=id, msg="操作记录不存在")
|
|
|
|
|
return self._to_out_schema(log)
|
|
|
|
|
|
|
|
|
|
async def page(
|
|
|
|
|
self,
|
|
|
|
|
page_no: int,
|
|
|
|
|
page_size: int,
|
|
|
|
|
search: EnergyReportReviewLogQueryParam | None = None,
|
|
|
|
|
order_by: list[dict[str, str]] | None = None,
|
|
|
|
|
) -> PageResultSchema[EnergyReportReviewLogOutSchema]:
|
|
|
|
|
search_dict = vars(search) if search else {}
|
|
|
|
|
page_result = await EnergyReportReviewLogCRUD(self.auth).page(
|
|
|
|
|
offset=(page_no - 1) * page_size,
|
|
|
|
|
limit=page_size,
|
|
|
|
|
order_by=order_by or [{"id": "desc"}],
|
|
|
|
|
search=search_dict,
|
|
|
|
|
out_schema=EnergyReportReviewLogOutSchema,
|
|
|
|
|
)
|
|
|
|
|
page_result.items = [self._fill_out_dict(item) for item in page_result.items]
|
|
|
|
|
return page_result
|
|
|
|
|
|
|
|
|
|
async def create(self, data: EnergyReportReviewLogCreateSchema) -> EnergyReportReviewLogOutSchema:
|
|
|
|
|
review = await EnergyReportReviewCRUD(self.auth).get_or_404(
|
|
|
|
|
id=data.report_review_id,
|
|
|
|
|
msg="新增操作记录失败,能源报告审核任务不存在",
|
|
|
|
|
)
|
|
|
|
|
log = await self.create_system_log(
|
|
|
|
|
report_review_id=review.id,
|
|
|
|
|
task_name=review.task_name,
|
|
|
|
|
action_type=data.action_type,
|
|
|
|
|
description=data.description or self._default_description(review.task_name, data.action_type),
|
|
|
|
|
)
|
|
|
|
|
return self._to_out_schema(log)
|
|
|
|
|
|
|
|
|
|
async def delete(self, ids: list[int]) -> None:
|
|
|
|
|
if len(ids) < 1:
|
|
|
|
|
raise CustomException(msg="删除失败,删除对象不能为空")
|
|
|
|
|
await EnergyReportReviewLogCRUD(self.auth).delete(ids=ids)
|
|
|
|
|
|
|
|
|
|
async def create_system_log(self, report_review_id: int, task_name: str, action_type: str, description: str | None = None):
|
|
|
|
|
action_name = REVIEW_LOG_ACTION_NAMES.get(action_type)
|
|
|
|
|
if not action_name:
|
|
|
|
|
raise CustomException(msg="操作类型不支持")
|
|
|
|
|
return await EnergyReportReviewLogCRUD(self.auth).create(
|
|
|
|
|
data=EnergyReportReviewLogCreateSchema(
|
|
|
|
|
report_review_id=report_review_id,
|
|
|
|
|
action_type=action_type, # type: ignore[arg-type]
|
|
|
|
|
description=description,
|
|
|
|
|
task_name=task_name,
|
|
|
|
|
action_name=action_name,
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _to_out_schema(self, log) -> EnergyReportReviewLogOutSchema:
|
|
|
|
|
log_out = EnergyReportReviewLogOutSchema.model_validate(log)
|
|
|
|
|
log_out.operator_id = log_out.created_id
|
|
|
|
|
if getattr(log, "created_by", None):
|
|
|
|
|
log_out.operator_name = log.created_by.name
|
|
|
|
|
log_out.action_name = REVIEW_LOG_ACTION_NAMES.get(log.action_type, log.action_type)
|
|
|
|
|
return log_out
|
|
|
|
|
|
|
|
|
|
def _fill_out_dict(self, item: dict) -> dict:
|
|
|
|
|
created_by = item.get("created_by") or {}
|
|
|
|
|
item["operator_id"] = item.get("created_id")
|
|
|
|
|
item["operator_name"] = created_by.get("name")
|
|
|
|
|
item["action_name"] = REVIEW_LOG_ACTION_NAMES.get(item.get("action_type"), item.get("action_name"))
|
|
|
|
|
return item
|
|
|
|
|
|
|
|
|
|
def _default_description(self, task_name: str, action_type: str) -> str:
|
|
|
|
|
action_name = REVIEW_LOG_ACTION_NAMES.get(action_type, action_type)
|
|
|
|
|
return f"{action_name}:{task_name}"
|
|
|
|
|
|