You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					188 lines
				
				7.1 KiB
			
		
		
			
		
	
	
					188 lines
				
				7.1 KiB
			| 
											1 year ago
										 | from sqlalchemy.ext.asyncio import AsyncSession | ||
|  | from typing import List | ||
| 
											1 year ago
										 | from config.constant import CommonConstant | ||
|  | from exceptions.exception import ServiceException | ||
| 
											1 year ago
										 | from module_admin.dao.post_dao import PostDao | ||
|  | from module_admin.entity.vo.common_vo import CrudResponseModel | ||
|  | from module_admin.entity.vo.post_vo import DeletePostModel, PostModel, PostPageQueryModel | ||
|  | from utils.common_util import CamelCaseUtil, export_list2excel | ||
| 
											2 years ago
										 | 
 | ||
|  | 
 | ||
|  | class PostService: | ||
|  |     """
 | ||
|  |     岗位管理模块服务层 | ||
|  |     """
 | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |     @classmethod | ||
| 
											1 year ago
										 |     async def get_post_list_services( | ||
|  |         cls, query_db: AsyncSession, query_object: PostPageQueryModel, is_page: bool = False | ||
|  |     ): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         获取岗位列表信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param query_db: orm对象 | ||
|  |         :param query_object: 查询参数对象 | ||
| 
											2 years ago
										 |         :param is_page: 是否开启分页 | ||
| 
											2 years ago
										 |         :return: 岗位列表信息对象 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         post_list_result = await PostDao.get_post_list(query_db, query_object, is_page) | ||
| 
											2 years ago
										 | 
 | ||
| 
											2 years ago
										 |         return post_list_result | ||
| 
											2 years ago
										 | 
 | ||
| 
											1 year ago
										 |     @classmethod | ||
|  |     async def check_post_name_unique_services(cls, query_db: AsyncSession, page_object: PostModel): | ||
|  |         """
 | ||
|  |         检查岗位名称是否唯一service | ||
| 
											1 year ago
										 | 
 | ||
| 
											1 year ago
										 |         :param query_db: orm对象 | ||
|  |         :param page_object: 岗位对象 | ||
|  |         :return: 校验结果 | ||
|  |         """
 | ||
|  |         post_id = -1 if page_object.post_id is None else page_object.post_id | ||
|  |         post = await PostDao.get_post_detail_by_info(query_db, PostModel(postName=page_object.post_name)) | ||
|  |         if post and post.post_id != post_id: | ||
|  |             return CommonConstant.NOT_UNIQUE | ||
|  |         return CommonConstant.UNIQUE | ||
|  | 
 | ||
|  |     @classmethod | ||
|  |     async def check_post_code_unique_services(cls, query_db: AsyncSession, page_object: PostModel): | ||
|  |         """
 | ||
|  |         检查岗位编码是否唯一service | ||
| 
											1 year ago
										 | 
 | ||
| 
											1 year ago
										 |         :param query_db: orm对象 | ||
|  |         :param page_object: 岗位对象 | ||
|  |         :return: 校验结果 | ||
|  |         """
 | ||
|  |         post_id = -1 if page_object.post_id is None else page_object.post_id | ||
|  |         post = await PostDao.get_post_detail_by_info(query_db, PostModel(postCode=page_object.post_code)) | ||
|  |         if post and post.post_id != post_id: | ||
|  |             return CommonConstant.NOT_UNIQUE | ||
|  |         return CommonConstant.UNIQUE | ||
|  | 
 | ||
| 
											2 years ago
										 |     @classmethod | ||
| 
											1 year ago
										 |     async def add_post_services(cls, query_db: AsyncSession, page_object: PostModel): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         新增岗位信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param query_db: orm对象 | ||
|  |         :param page_object: 新增岗位对象 | ||
|  |         :return: 新增岗位校验结果 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         if not await cls.check_post_name_unique_services(query_db, page_object): | ||
|  |             raise ServiceException(message=f'新增岗位{page_object.post_name}失败,岗位名称已存在') | ||
|  |         elif not await cls.check_post_code_unique_services(query_db, page_object): | ||
|  |             raise ServiceException(message=f'新增岗位{page_object.post_name}失败,岗位编码已存在') | ||
| 
											2 years ago
										 |         else: | ||
|  |             try: | ||
| 
											1 year ago
										 |                 await PostDao.add_post_dao(query_db, page_object) | ||
|  |                 await query_db.commit() | ||
| 
											1 year ago
										 |                 return CrudResponseModel(is_success=True, message='新增成功') | ||
| 
											2 years ago
										 |             except Exception as e: | ||
| 
											1 year ago
										 |                 await query_db.rollback() | ||
| 
											2 years ago
										 |                 raise e | ||
|  | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def edit_post_services(cls, query_db: AsyncSession, page_object: PostModel): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         编辑岗位信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param query_db: orm对象 | ||
|  |         :param page_object: 编辑岗位对象 | ||
|  |         :return: 编辑岗位校验结果 | ||
|  |         """
 | ||
|  |         edit_post = page_object.model_dump(exclude_unset=True) | ||
| 
											1 year ago
										 |         post_info = await cls.post_detail_services(query_db, page_object.post_id) | ||
|  |         if post_info.post_id: | ||
|  |             if not await cls.check_post_name_unique_services(query_db, page_object): | ||
|  |                 raise ServiceException(message=f'修改岗位{page_object.post_name}失败,岗位名称已存在') | ||
|  |             elif not await cls.check_post_code_unique_services(query_db, page_object): | ||
|  |                 raise ServiceException(message=f'修改岗位{page_object.post_name}失败,岗位编码已存在') | ||
|  |             else: | ||
|  |                 try: | ||
|  |                     await PostDao.edit_post_dao(query_db, edit_post) | ||
|  |                     await query_db.commit() | ||
|  |                     return CrudResponseModel(is_success=True, message='更新成功') | ||
|  |                 except Exception as e: | ||
|  |                     await query_db.rollback() | ||
|  |                     raise e | ||
| 
											2 years ago
										 |         else: | ||
| 
											1 year ago
										 |             raise ServiceException(message='岗位不存在') | ||
| 
											2 years ago
										 | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def delete_post_services(cls, query_db: AsyncSession, page_object: DeletePostModel): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         删除岗位信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param query_db: orm对象 | ||
|  |         :param page_object: 删除岗位对象 | ||
|  |         :return: 删除岗位校验结果 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         if page_object.post_ids: | ||
| 
											2 years ago
										 |             post_id_list = page_object.post_ids.split(',') | ||
|  |             try: | ||
|  |                 for post_id in post_id_list: | ||
| 
											1 year ago
										 |                     post = await cls.post_detail_services(query_db, int(post_id)) | ||
|  |                     if (await PostDao.count_user_post_dao(query_db, int(post_id))) > 0: | ||
|  |                         raise ServiceException(message=f'{post.post_name}已分配,不能删除') | ||
| 
											1 year ago
										 |                     await PostDao.delete_post_dao(query_db, PostModel(postId=post_id)) | ||
|  |                 await query_db.commit() | ||
| 
											1 year ago
										 |                 return CrudResponseModel(is_success=True, message='删除成功') | ||
| 
											2 years ago
										 |             except Exception as e: | ||
| 
											1 year ago
										 |                 await query_db.rollback() | ||
| 
											2 years ago
										 |                 raise e | ||
|  |         else: | ||
| 
											1 year ago
										 |             raise ServiceException(message='传入岗位id为空') | ||
| 
											2 years ago
										 | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def post_detail_services(cls, query_db: AsyncSession, post_id: int): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         获取岗位详细信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param query_db: orm对象 | ||
|  |         :param post_id: 岗位id | ||
|  |         :return: 岗位id对应的信息 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         post = await PostDao.get_post_detail_by_id(query_db, post_id=post_id) | ||
| 
											1 year ago
										 |         if post: | ||
|  |             result = PostModel(**CamelCaseUtil.transform_result(post)) | ||
|  |         else: | ||
|  |             result = PostModel(**dict()) | ||
| 
											2 years ago
										 | 
 | ||
|  |         return result | ||
|  | 
 | ||
|  |     @staticmethod | ||
| 
											1 year ago
										 |     async def export_post_list_services(post_list: List): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         导出岗位信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param post_list: 岗位信息列表 | ||
|  |         :return: 岗位信息对应excel的二进制数据 | ||
|  |         """
 | ||
|  |         # 创建一个映射字典,将英文键映射到中文键 | ||
|  |         mapping_dict = { | ||
| 
											1 year ago
										 |             'postId': '岗位编号', | ||
|  |             'postCode': '岗位编码', | ||
|  |             'postName': '岗位名称', | ||
|  |             'postSort': '显示顺序', | ||
|  |             'status': '状态', | ||
|  |             'createBy': '创建者', | ||
|  |             'createTime': '创建时间', | ||
|  |             'updateBy': '更新者', | ||
|  |             'updateTime': '更新时间', | ||
|  |             'remark': '备注', | ||
| 
											2 years ago
										 |         } | ||
|  | 
 | ||
|  |         data = post_list | ||
|  | 
 | ||
|  |         for item in data: | ||
|  |             if item.get('status') == '0': | ||
|  |                 item['status'] = '正常' | ||
|  |             else: | ||
|  |                 item['status'] = '停用' | ||
| 
											1 year ago
										 |         new_data = [ | ||
|  |             {mapping_dict.get(key): value for key, value in item.items() if mapping_dict.get(key)} for item in data | ||
|  |         ] | ||
| 
											2 years ago
										 |         binary_data = export_list2excel(new_data) | ||
|  | 
 | ||
|  |         return binary_data |