perf: 优化通知公告管理模块service层及异常处理

This commit is contained in:
insistence
2024-07-09 21:03:05 +08:00
parent 1eff840afa
commit da54a7e5f5
3 changed files with 69 additions and 85 deletions

View File

@@ -1,5 +1,7 @@
from module_admin.dao.notice_dao import *
from module_admin.entity.vo.common_vo import CrudResponseModel
from config.constant import CommonConstant
from exceptions.exception import ServiceException
from utils.common_util import export_list2excel, CamelCaseUtil
@@ -21,6 +23,20 @@ class NoticeService:
return notice_list_result
@classmethod
async def check_notice_unique_services(cls, query_db: AsyncSession, page_object: NoticeModel):
"""
校验通知公告是否存在service
:param query_db: orm对象
:param page_object: 通知公告对象
:return: 校验结果
"""
notice_id = -1 if page_object.notice_id is None else page_object.notice_id
notice = await NoticeDao.get_notice_detail_by_info(query_db, page_object)
if notice and notice.notice_id != notice_id:
return CommonConstant.NOT_UNIQUE
return CommonConstant.UNIQUE
@classmethod
async def add_notice_services(cls, query_db: AsyncSession, page_object: NoticeModel):
"""
@@ -29,20 +45,17 @@ class NoticeService:
:param page_object: 新增通知公告对象
:return: 新增通知公告校验结果
"""
notice = await NoticeDao.get_notice_detail_by_info(query_db, page_object)
if notice:
result = dict(is_success=False, message='通知公告已存在')
if not await cls.check_notice_unique_services(query_db, page_object):
raise ServiceException(message=f'新增通知公告{page_object.notice_title}失败,通知公告已存在')
else:
try:
await NoticeDao.add_notice_dao(query_db, page_object)
await query_db.commit()
result = dict(is_success=True, message='新增成功')
return CrudResponseModel(is_success=True, message='新增成功')
except Exception as e:
await query_db.rollback()
raise e
return CrudResponseModel(**result)
@classmethod
async def edit_notice_services(cls, query_db: AsyncSession, page_object: NoticeModel):
"""
@@ -52,24 +65,20 @@ class NoticeService:
:return: 编辑通知公告校验结果
"""
edit_notice = page_object.model_dump(exclude_unset=True)
notice_info = await cls.notice_detail_services(query_db, edit_notice.get('notice_id'))
if notice_info:
if notice_info.notice_title != page_object.notice_title or notice_info.notice_type != page_object.notice_type or notice_info.notice_content != page_object.notice_content:
notice = await NoticeDao.get_notice_detail_by_info(query_db, page_object)
if notice:
result = dict(is_success=False, message='通知公告已存在')
return CrudResponseModel(**result)
try:
await NoticeDao.edit_notice_dao(query_db, edit_notice)
await query_db.commit()
result = dict(is_success=True, message='更新成功')
except Exception as e:
await query_db.rollback()
raise e
notice_info = await cls.notice_detail_services(query_db, page_object.notice_id)
if notice_info.notice_id:
if not await cls.check_notice_unique_services(query_db, page_object):
raise ServiceException(message=f'修改通知公告{page_object.notice_title}失败,通知公告已存在')
else:
try:
await NoticeDao.edit_notice_dao(query_db, edit_notice)
await query_db.commit()
return CrudResponseModel(is_success=True, message='更新成功')
except Exception as e:
await query_db.rollback()
raise e
else:
result = dict(is_success=False, message='通知公告不存在')
return CrudResponseModel(**result)
raise ServiceException(message='通知公告不存在')
@classmethod
async def delete_notice_services(cls, query_db: AsyncSession, page_object: DeleteNoticeModel):
@@ -85,13 +94,12 @@ class NoticeService:
for notice_id in notice_id_list:
await NoticeDao.delete_notice_dao(query_db, NoticeModel(noticeId=notice_id))
await query_db.commit()
result = dict(is_success=True, message='删除成功')
return CrudResponseModel(is_success=True, message='删除成功')
except Exception as e:
await query_db.rollback()
raise e
else:
result = dict(is_success=False, message='传入通知公告id为空')
return CrudResponseModel(**result)
raise ServiceException(message='传入通知公告id为空')
@classmethod
async def notice_detail_services(cls, query_db: AsyncSession, notice_id: int):
@@ -102,6 +110,9 @@ class NoticeService:
:return: 通知公告id对应的信息
"""
notice = await NoticeDao.get_notice_detail_by_id(query_db, notice_id=notice_id)
result = NoticeModel(**CamelCaseUtil.transform_result(notice))
if notice:
result = NoticeModel(**CamelCaseUtil.transform_result(notice))
else:
result = NoticeModel(**dict())
return result