fix:修复bug

master
HuangHuiKang 3 weeks ago
parent 1d7d64cc19
commit 2867340282

@ -89,8 +89,6 @@ class TemplateConfigService:
return await ChapterCRUD(self.auth).update(id=chapter_id, data=chapter_data) return await ChapterCRUD(self.auth).update(id=chapter_id, data=chapter_data)
async def _save_variables(self, template_id: int, chapter_name: str, variables: list) -> None: async def _save_variables(self, template_id: int, chapter_name: str, variables: list) -> None:
chapter_names = self._collect_variable_chapter_names(chapter_name=chapter_name, variables=variables)
await self._validate_variable_chapters(template_id=template_id, chapter_names=chapter_names)
for variable in variables: for variable in variables:
existing = await VariableCRUD(self.auth).get_list( existing = await VariableCRUD(self.auth).get_list(
search={"template_id": template_id, "variable_name": variable.variable_name}, search={"template_id": template_id, "variable_name": variable.variable_name},

@ -1,7 +1,8 @@
from app.api.v1.module_common.file.crud import FileCRUD from sqlalchemy import select
from app.api.v1.module_common.file.crud import FileCRUD
from app.core.base_schema import AuthSchema from app.core.base_schema import AuthSchema
from app.core.exceptions import CustomException from app.core.exceptions import CustomException
from sqlalchemy import select
from .crud import EnergyReportTemplateCRUD from .crud import EnergyReportTemplateCRUD
from .model import EnergyReportTemplateModel from .model import EnergyReportTemplateModel
@ -16,7 +17,7 @@ ENERGY_REPORT_TEMPLATE_BUSINESS_TYPE = "energy_report_template"
class EnergyReportTemplateService: class EnergyReportTemplateService:
"""鑳芥簮鎶ュ憡妯℃澘鏈嶅姟銆?"" """能源报告模板服务。"""
def __init__(self, auth: AuthSchema) -> None: def __init__(self, auth: AuthSchema) -> None:
self.auth = auth self.auth = auth
@ -48,15 +49,15 @@ class EnergyReportTemplateService:
return page_result return page_result
async def create(self, data: EnergyReportTemplateCreateSchema) -> EnergyReportTemplateOutSchema: async def create(self, data: EnergyReportTemplateCreateSchema) -> EnergyReportTemplateOutSchema:
await self._validate_unique_name(template_name=data.template_name) await self._validate_unique_name(template_name=data.template_name, tenant_id=self._current_tenant_id())
await self._validate_file(file_id=data.file_id) await self._validate_file(file_id=data.file_id)
template = await EnergyReportTemplateCRUD(self.auth).create(data=data) template = await EnergyReportTemplateCRUD(self.auth).create(data=data)
await self._bind_file(file_id=data.file_id, template_id=template.id) await self._bind_file(file_id=data.file_id, template_id=template.id)
return self._to_out_schema(template) return self._to_out_schema(template)
async def update(self, id: int, data: EnergyReportTemplateUpdateSchema) -> EnergyReportTemplateOutSchema: async def update(self, id: int, data: EnergyReportTemplateUpdateSchema) -> EnergyReportTemplateOutSchema:
old_template = await EnergyReportTemplateCRUD(self.auth).get_or_404(id=id, msg="鏇存柊澶辫触锛岃兘婧愭姤鍛婃ā鏉夸笉瀛樺湪") old_template = await EnergyReportTemplateCRUD(self.auth).get_or_404(id=id, msg="更新失败,能源报告模板不存在")
await self._validate_unique_name(template_name=data.template_name, exclude_id=id) await self._validate_unique_name(template_name=data.template_name, exclude_id=id, tenant_id=old_template.tenant_id)
await self._validate_file(file_id=data.file_id, current_template_id=id) await self._validate_file(file_id=data.file_id, current_template_id=id)
template = await EnergyReportTemplateCRUD(self.auth).update(id=id, data=data) template = await EnergyReportTemplateCRUD(self.auth).update(id=id, data=data)
if old_template.file_id != data.file_id: if old_template.file_id != data.file_id:
@ -66,12 +67,12 @@ class EnergyReportTemplateService:
async def delete(self, ids: list[int]) -> None: async def delete(self, ids: list[int]) -> None:
if len(ids) < 1: if len(ids) < 1:
raise CustomException(msg="鍒犻櫎澶辫触锛屽垹闄ゅ璞′笉鑳戒负绌?) raise CustomException(msg="删除失败,删除对象不能为空")
templates = await EnergyReportTemplateCRUD(self.auth).get_list(search={"id": ("in", ids)}) templates = await EnergyReportTemplateCRUD(self.auth).get_list(search={"id": ("in", ids)})
template_map = {template.id: template for template in templates} template_map = {template.id: template for template in templates}
for template_id in ids: for template_id in ids:
if template_id not in template_map: if template_id not in template_map:
raise CustomException(msg="鍒犻櫎澶辫触锛岃兘婧愭姤鍛婃ā鏉夸笉瀛樺湪") raise CustomException(msg="删除失败,能源报告模板不存在")
for template in templates: for template in templates:
await self._unbind_file(file_id=template.file_id, template_id=template.id) await self._unbind_file(file_id=template.file_id, template_id=template.id)
await EnergyReportTemplateCRUD(self.auth).delete(ids=ids) await EnergyReportTemplateCRUD(self.auth).delete(ids=ids)
@ -84,19 +85,38 @@ class EnergyReportTemplateService:
template_out.object_name = template_out.file.object_name template_out.object_name = template_out.file.object_name
return template_out return template_out
async def _validate_unique_name(self, template_name: str, exclude_id: int | None = None) -> None: async def _validate_unique_name(
template = await EnergyReportTemplateCRUD(self.auth).get(template_name=template_name) self,
if template and template.id != exclude_id: template_name: str,
raise CustomException(msg="鑳芥簮鎶ュ憡妯℃澘鍚嶇О宸插瓨鍦?) exclude_id: int | None = None,
tenant_id: int | None = None,
) -> None:
if self.auth.db is None:
raise CustomException(msg="数据库会话不存在")
sql = select(EnergyReportTemplateModel.id).where(
EnergyReportTemplateModel.template_name == template_name,
)
if tenant_id is not None:
sql = sql.where(EnergyReportTemplateModel.tenant_id == tenant_id)
if exclude_id is not None:
sql = sql.where(EnergyReportTemplateModel.id != exclude_id)
result = await self.auth.db.execute(sql.limit(1))
if result.scalar_one_or_none() is not None:
raise CustomException(msg="能源报告模板名称已存在")
def _current_tenant_id(self) -> int | None:
if not self.auth.user:
return self.auth.tenant_id
return self.auth.tenant_id or self.auth.user.tenant_id
async def _validate_file(self, file_id: int, current_template_id: int | None = None) -> None: async def _validate_file(self, file_id: int, current_template_id: int | None = None) -> None:
file_record = await FileCRUD(self.auth).get(id=file_id) file_record = await FileCRUD(self.auth).get(id=file_id)
if not file_record: if not file_record:
raise CustomException(msg="鏂囦欢璁板綍涓嶅瓨鍦?) raise CustomException(msg="文件记录不存在")
if file_record.business_type and file_record.business_type != ENERGY_REPORT_TEMPLATE_BUSINESS_TYPE: if file_record.business_type and file_record.business_type != ENERGY_REPORT_TEMPLATE_BUSINESS_TYPE:
raise CustomException(msg="鏂囦欢宸茬粦瀹氬埌鍏朵粬涓氬姟绫诲瀷") raise CustomException(msg="文件已绑定到其他业务类型")
if file_record.business_id and file_record.business_id != current_template_id: if file_record.business_id and file_record.business_id != current_template_id:
raise CustomException(msg="鏂囦欢宸茬粦瀹氬埌鍏朵粬鑳芥簮鎶ュ憡妯℃澘") raise CustomException(msg="文件已绑定到其他能源报告模板")
async def _bind_file(self, file_id: int, template_id: int) -> None: async def _bind_file(self, file_id: int, template_id: int) -> None:
await FileCRUD(self.auth).set( await FileCRUD(self.auth).set(
@ -111,4 +131,3 @@ class EnergyReportTemplateService:
return return
if file_record.business_type == ENERGY_REPORT_TEMPLATE_BUSINESS_TYPE and file_record.business_id == template_id: if file_record.business_type == ENERGY_REPORT_TEMPLATE_BUSINESS_TYPE and file_record.business_id == template_id:
await FileCRUD(self.auth).set(ids=[file_id], business_id=None) await FileCRUD(self.auth).set(ids=[file_id], business_id=None)

@ -71,9 +71,6 @@ class VariableService:
} }
async def save(self, data: VariableSaveSchema) -> list[dict]: async def save(self, data: VariableSaveSchema) -> list[dict]:
chapter_names = self._collect_chapter_names(data)
await self._validate_chapters(template_id=data.template_id, chapter_names=chapter_names)
existing = await VariableCRUD(self.auth).get_list(search={"template_id": data.template_id}) existing = await VariableCRUD(self.auth).get_list(search={"template_id": data.template_id})
if existing: if existing:
await VariableCRUD(self.auth).hard_delete(ids=[record.id for record in existing]) await VariableCRUD(self.auth).hard_delete(ids=[record.id for record in existing])

@ -29,7 +29,7 @@ class Settings(BaseSettings):
# ******************* 服务器配置 ****************** # # ******************* 服务器配置 ****************** #
# ================================================= # # ================================================= #
SERVER_HOST: str = "0.0.0.0" # 允许访问的IP地址 SERVER_HOST: str = "0.0.0.0" # 允许访问的IP地址
SERVER_PORT: int = 8001 # 服务端口 SERVER_PORT: int = 8011 # 服务端口
# ================================================= # # ================================================= #
# ******************* API文档配置 ****************** # # ******************* API文档配置 ****************** #

Loading…
Cancel
Save