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.
		
		
		
		
			
				
					468 lines
				
				19 KiB
			
		
		
			
		
	
	
					468 lines
				
				19 KiB
			| 
											2 years ago
										 | import json | ||
| 
											1 year ago
										 | from fastapi import Request | ||
|  | from sqlalchemy.ext.asyncio import AsyncSession | ||
|  | from typing import List | ||
| 
											1 year ago
										 | from config.constant import CommonConstant | ||
| 
											1 year ago
										 | from config.enums import RedisInitKeyConfig | ||
| 
											1 year ago
										 | from exceptions.exception import ServiceException | ||
| 
											1 year ago
										 | from module_admin.dao.dict_dao import DictDataDao, DictTypeDao | ||
|  | from module_admin.entity.vo.common_vo import CrudResponseModel | ||
|  | from module_admin.entity.vo.dict_vo import ( | ||
|  |     DeleteDictDataModel, | ||
|  |     DeleteDictTypeModel, | ||
|  |     DictDataModel, | ||
|  |     DictDataPageQueryModel, | ||
|  |     DictTypeModel, | ||
|  |     DictTypePageQueryModel, | ||
|  | ) | ||
|  | from utils.common_util import CamelCaseUtil, export_list2excel | ||
| 
											2 years ago
										 | 
 | ||
|  | 
 | ||
|  | class DictTypeService: | ||
|  |     """
 | ||
|  |     字典类型管理模块服务层 | ||
|  |     """
 | ||
|  | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def get_dict_type_list_services( | ||
|  |         cls, query_db: AsyncSession, query_object: DictTypePageQueryModel, 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
										 |         dict_type_list_result = await DictTypeDao.get_dict_type_list(query_db, query_object, is_page) | ||
| 
											2 years ago
										 | 
 | ||
| 
											2 years ago
										 |         return dict_type_list_result | ||
| 
											2 years ago
										 | 
 | ||
| 
											1 year ago
										 |     @classmethod | ||
|  |     async def check_dict_type_unique_services(cls, query_db: AsyncSession, page_object: DictTypeModel): | ||
|  |         """
 | ||
|  |         校验字典类型称是否唯一service | ||
| 
											1 year ago
										 | 
 | ||
| 
											1 year ago
										 |         :param query_db: orm对象 | ||
|  |         :param page_object: 字典类型对象 | ||
|  |         :return: 校验结果 | ||
|  |         """
 | ||
|  |         dict_id = -1 if page_object.dict_id is None else page_object.dict_id | ||
| 
											1 year ago
										 |         dict_type = await DictTypeDao.get_dict_type_detail_by_info( | ||
|  |             query_db, DictTypeModel(dictType=page_object.dict_type) | ||
|  |         ) | ||
| 
											1 year ago
										 |         if dict_type and dict_type.dict_id != dict_id: | ||
|  |             return CommonConstant.NOT_UNIQUE | ||
|  |         return CommonConstant.UNIQUE | ||
|  | 
 | ||
| 
											2 years ago
										 |     @classmethod | ||
| 
											1 year ago
										 |     async def add_dict_type_services(cls, request: Request, query_db: AsyncSession, page_object: DictTypeModel): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         新增字典类型信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param request: Request对象 | ||
|  |         :param query_db: orm对象 | ||
|  |         :param page_object: 新增岗位对象 | ||
|  |         :return: 新增字典类型校验结果 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         if not await cls.check_dict_type_unique_services(query_db, page_object): | ||
|  |             raise ServiceException(message=f'新增字典{page_object.dict_name}失败,字典类型已存在') | ||
| 
											2 years ago
										 |         else: | ||
|  |             try: | ||
| 
											1 year ago
										 |                 await DictTypeDao.add_dict_type_dao(query_db, page_object) | ||
|  |                 await query_db.commit() | ||
| 
											1 year ago
										 |                 await request.app.state.redis.set(f'{RedisInitKeyConfig.SYS_DICT.key}:{page_object.dict_type}', '') | ||
| 
											2 years ago
										 |                 result = dict(is_success=True, message='新增成功') | ||
|  |             except Exception as e: | ||
| 
											1 year ago
										 |                 await query_db.rollback() | ||
| 
											2 years ago
										 |                 raise e | ||
|  | 
 | ||
|  |         return CrudResponseModel(**result) | ||
|  | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def edit_dict_type_services(cls, request: Request, query_db: AsyncSession, page_object: DictTypeModel): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         编辑字典类型信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param request: Request对象 | ||
|  |         :param query_db: orm对象 | ||
|  |         :param page_object: 编辑字典类型对象 | ||
|  |         :return: 编辑字典类型校验结果 | ||
|  |         """
 | ||
|  |         edit_dict_type = page_object.model_dump(exclude_unset=True) | ||
| 
											1 year ago
										 |         dict_type_info = await cls.dict_type_detail_services(query_db, page_object.dict_id) | ||
|  |         if dict_type_info.dict_id: | ||
|  |             if not await cls.check_dict_type_unique_services(query_db, page_object): | ||
|  |                 raise ServiceException(message=f'修改字典{page_object.dict_name}失败,字典类型已存在') | ||
|  |             else: | ||
|  |                 try: | ||
| 
											1 year ago
										 |                     query_dict_data = DictDataPageQueryModel(dictType=dict_type_info.dict_type) | ||
|  |                     dict_data_list = await DictDataDao.get_dict_data_list(query_db, query_dict_data, is_page=False) | ||
| 
											1 year ago
										 |                     if dict_type_info.dict_type != page_object.dict_type: | ||
|  |                         for dict_data in dict_data_list: | ||
| 
											1 year ago
										 |                             edit_dict_data = DictDataModel( | ||
|  |                                 dictCode=dict_data.dict_code, | ||
|  |                                 dictType=page_object.dict_type, | ||
|  |                                 updateBy=page_object.update_by, | ||
|  |                             ).model_dump(exclude_unset=True) | ||
| 
											1 year ago
										 |                             await DictDataDao.edit_dict_data_dao(query_db, edit_dict_data) | ||
|  |                     await DictTypeDao.edit_dict_type_dao(query_db, edit_dict_type) | ||
|  |                     await query_db.commit() | ||
|  |                     if dict_type_info.dict_type != page_object.dict_type: | ||
|  |                         dict_data = [CamelCaseUtil.transform_result(row) for row in dict_data_list if row] | ||
| 
											1 year ago
										 |                         await request.app.state.redis.set( | ||
| 
											1 year ago
										 |                             f'{RedisInitKeyConfig.SYS_DICT.key}:{page_object.dict_type}', | ||
| 
											1 year ago
										 |                             json.dumps(dict_data, ensure_ascii=False, default=str), | ||
|  |                         ) | ||
| 
											1 year ago
										 |                     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_dict_type_services( | ||
|  |         cls, request: Request, query_db: AsyncSession, page_object: DeleteDictTypeModel | ||
|  |     ): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         删除字典类型信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param request: Request对象 | ||
|  |         :param query_db: orm对象 | ||
|  |         :param page_object: 删除字典类型对象 | ||
|  |         :return: 删除字典类型校验结果 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         if page_object.dict_ids: | ||
| 
											2 years ago
										 |             dict_id_list = page_object.dict_ids.split(',') | ||
|  |             try: | ||
| 
											1 year ago
										 |                 delete_dict_type_list = [] | ||
| 
											2 years ago
										 |                 for dict_id in dict_id_list: | ||
| 
											1 year ago
										 |                     dict_type_into = await cls.dict_type_detail_services(query_db, int(dict_id)) | ||
|  |                     if (await DictDataDao.count_dict_data_dao(query_db, dict_type_into.dict_type)) > 0: | ||
|  |                         raise ServiceException(message=f'{dict_type_into.dict_name}已分配,不能删除') | ||
|  |                     await DictTypeDao.delete_dict_type_dao(query_db, DictTypeModel(dictId=int(dict_id))) | ||
| 
											1 year ago
										 |                     delete_dict_type_list.append(f'{RedisInitKeyConfig.SYS_DICT.key}:{dict_type_into.dict_type}') | ||
| 
											1 year ago
										 |                 await query_db.commit() | ||
| 
											1 year ago
										 |                 if delete_dict_type_list: | ||
|  |                     await request.app.state.redis.delete(*delete_dict_type_list) | ||
|  |                 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 dict_type_detail_services(cls, query_db: AsyncSession, dict_id: int): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         获取字典类型详细信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param query_db: orm对象 | ||
|  |         :param dict_id: 字典类型id | ||
|  |         :return: 字典类型id对应的信息 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         dict_type = await DictTypeDao.get_dict_type_detail_by_id(query_db, dict_id=dict_id) | ||
| 
											1 year ago
										 |         if dict_type: | ||
|  |             result = DictTypeModel(**CamelCaseUtil.transform_result(dict_type)) | ||
|  |         else: | ||
|  |             result = DictTypeModel(**dict()) | ||
| 
											2 years ago
										 | 
 | ||
|  |         return result | ||
|  | 
 | ||
|  |     @staticmethod | ||
| 
											1 year ago
										 |     async def export_dict_type_list_services(dict_type_list: List): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         导出字典类型信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param dict_type_list: 字典信息列表 | ||
|  |         :return: 字典信息对应excel的二进制数据 | ||
|  |         """
 | ||
|  |         # 创建一个映射字典,将英文键映射到中文键 | ||
|  |         mapping_dict = { | ||
| 
											1 year ago
										 |             'dictId': '字典编号', | ||
|  |             'dictName': '字典名称', | ||
|  |             'dictType': '字典类型', | ||
|  |             'status': '状态', | ||
|  |             'createBy': '创建者', | ||
|  |             'createTime': '创建时间', | ||
|  |             'updateBy': '更新者', | ||
|  |             'updateTime': '更新时间', | ||
|  |             'remark': '备注', | ||
| 
											2 years ago
										 |         } | ||
|  | 
 | ||
|  |         data = dict_type_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 | ||
|  | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def refresh_sys_dict_services(cls, request: Request, query_db: AsyncSession): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         刷新字典缓存信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param request: Request对象 | ||
|  |         :param query_db: orm对象 | ||
|  |         :return: 刷新字典缓存校验结果 | ||
|  |         """
 | ||
|  |         await DictDataService.init_cache_sys_dict_services(query_db, request.app.state.redis) | ||
|  |         result = dict(is_success=True, message='刷新成功') | ||
|  | 
 | ||
|  |         return CrudResponseModel(**result) | ||
|  | 
 | ||
|  | 
 | ||
|  | class DictDataService: | ||
|  |     """
 | ||
|  |     字典数据管理模块服务层 | ||
|  |     """
 | ||
|  | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def get_dict_data_list_services( | ||
|  |         cls, query_db: AsyncSession, query_object: DictDataPageQueryModel, 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
										 |         dict_data_list_result = await DictDataDao.get_dict_data_list(query_db, query_object, is_page) | ||
| 
											2 years ago
										 | 
 | ||
| 
											2 years ago
										 |         return dict_data_list_result | ||
| 
											2 years ago
										 | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def query_dict_data_list_services(cls, query_db: AsyncSession, dict_type: str): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         获取字典数据列表信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param query_db: orm对象 | ||
|  |         :param dict_type: 字典类型 | ||
|  |         :return: 字典数据列表信息对象 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         dict_data_list_result = await DictDataDao.query_dict_data_list(query_db, dict_type) | ||
| 
											2 years ago
										 | 
 | ||
|  |         return dict_data_list_result | ||
|  | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def init_cache_sys_dict_services(cls, query_db: AsyncSession, redis): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         应用初始化:获取所有字典类型对应的字典数据信息并缓存service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param query_db: orm对象 | ||
|  |         :param redis: redis对象 | ||
|  |         :return: | ||
|  |         """
 | ||
|  |         # 获取以sys_dict:开头的键列表 | ||
| 
											1 year ago
										 |         keys = await redis.keys(f'{RedisInitKeyConfig.SYS_DICT.key}:*') | ||
| 
											2 years ago
										 |         # 删除匹配的键 | ||
|  |         if keys: | ||
|  |             await redis.delete(*keys) | ||
| 
											1 year ago
										 |         dict_type_all = await DictTypeDao.get_all_dict_type(query_db) | ||
| 
											2 years ago
										 |         for dict_type_obj in [item for item in dict_type_all if item.status == '0']: | ||
|  |             dict_type = dict_type_obj.dict_type | ||
| 
											1 year ago
										 |             dict_data_list = await DictDataDao.query_dict_data_list(query_db, dict_type) | ||
| 
											2 years ago
										 |             dict_data = [CamelCaseUtil.transform_result(row) for row in dict_data_list if row] | ||
| 
											1 year ago
										 |             await redis.set( | ||
| 
											1 year ago
										 |                 f'{RedisInitKeyConfig.SYS_DICT.key}:{dict_type}', | ||
| 
											1 year ago
										 |                 json.dumps(dict_data, ensure_ascii=False, default=str), | ||
|  |             ) | ||
| 
											2 years ago
										 | 
 | ||
|  |     @classmethod | ||
|  |     async def query_dict_data_list_from_cache_services(cls, redis, dict_type: str): | ||
|  |         """
 | ||
|  |         从缓存获取字典数据列表信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param redis: redis对象 | ||
|  |         :param dict_type: 字典类型 | ||
|  |         :return: 字典数据列表信息对象 | ||
|  |         """
 | ||
|  |         result = [] | ||
| 
											1 year ago
										 |         dict_data_list_result = await redis.get(f'{RedisInitKeyConfig.SYS_DICT.key}:{dict_type}') | ||
| 
											2 years ago
										 |         if dict_data_list_result: | ||
|  |             result = json.loads(dict_data_list_result) | ||
|  | 
 | ||
|  |         return CamelCaseUtil.transform_result(result) | ||
|  | 
 | ||
| 
											1 year ago
										 |     @classmethod | ||
|  |     async def check_dict_data_unique_services(cls, query_db: AsyncSession, page_object: DictDataModel): | ||
|  |         """
 | ||
|  |         校验字典数据是否唯一service | ||
| 
											1 year ago
										 | 
 | ||
| 
											1 year ago
										 |         :param query_db: orm对象 | ||
|  |         :param page_object: 字典数据对象 | ||
|  |         :return: 校验结果 | ||
|  |         """
 | ||
|  |         dict_code = -1 if page_object.dict_code is None else page_object.dict_code | ||
|  |         dict_data = await DictDataDao.get_dict_data_detail_by_info(query_db, page_object) | ||
|  |         if dict_data and dict_data.dict_code != dict_code: | ||
|  |             return CommonConstant.NOT_UNIQUE | ||
|  |         return CommonConstant.UNIQUE | ||
|  | 
 | ||
| 
											2 years ago
										 |     @classmethod | ||
| 
											1 year ago
										 |     async def add_dict_data_services(cls, request: Request, query_db: AsyncSession, page_object: DictDataModel): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         新增字典数据信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param request: Request对象 | ||
|  |         :param query_db: orm对象 | ||
|  |         :param page_object: 新增岗位对象 | ||
|  |         :return: 新增字典数据校验结果 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         if not await cls.check_dict_data_unique_services(query_db, page_object): | ||
| 
											1 year ago
										 |             raise ServiceException( | ||
|  |                 message=f'新增字典数据{page_object.dict_label}失败,{page_object.dict_type}下已存在该字典数据' | ||
|  |             ) | ||
| 
											2 years ago
										 |         else: | ||
|  |             try: | ||
| 
											1 year ago
										 |                 await DictDataDao.add_dict_data_dao(query_db, page_object) | ||
|  |                 await query_db.commit() | ||
| 
											1 year ago
										 |                 dict_data_list = await cls.query_dict_data_list_services(query_db, page_object.dict_type) | ||
| 
											1 year ago
										 |                 await request.app.state.redis.set( | ||
| 
											1 year ago
										 |                     f'{RedisInitKeyConfig.SYS_DICT.key}:{page_object.dict_type}', | ||
| 
											1 year ago
										 |                     json.dumps(CamelCaseUtil.transform_result(dict_data_list), ensure_ascii=False, default=str), | ||
|  |                 ) | ||
| 
											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_dict_data_services(cls, request: Request, query_db: AsyncSession, page_object: DictDataModel): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         编辑字典数据信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param request: Request对象 | ||
|  |         :param query_db: orm对象 | ||
|  |         :param page_object: 编辑字典数据对象 | ||
|  |         :return: 编辑字典数据校验结果 | ||
|  |         """
 | ||
|  |         edit_data_type = page_object.model_dump(exclude_unset=True) | ||
| 
											1 year ago
										 |         dict_data_info = await cls.dict_data_detail_services(query_db, page_object.dict_code) | ||
|  |         if dict_data_info.dict_code: | ||
|  |             if not await cls.check_dict_data_unique_services(query_db, page_object): | ||
| 
											1 year ago
										 |                 raise ServiceException( | ||
|  |                     message=f'新增字典数据{page_object.dict_label}失败,{page_object.dict_type}下已存在该字典数据' | ||
|  |                 ) | ||
| 
											1 year ago
										 |             else: | ||
|  |                 try: | ||
|  |                     await DictDataDao.edit_dict_data_dao(query_db, edit_data_type) | ||
|  |                     await query_db.commit() | ||
|  |                     dict_data_list = await cls.query_dict_data_list_services(query_db, page_object.dict_type) | ||
| 
											1 year ago
										 |                     await request.app.state.redis.set( | ||
| 
											1 year ago
										 |                         f'{RedisInitKeyConfig.SYS_DICT.key}:{page_object.dict_type}', | ||
| 
											1 year ago
										 |                         json.dumps(CamelCaseUtil.transform_result(dict_data_list), ensure_ascii=False, default=str), | ||
|  |                     ) | ||
| 
											1 year ago
										 |                     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_dict_data_services( | ||
|  |         cls, request: Request, query_db: AsyncSession, page_object: DeleteDictDataModel | ||
|  |     ): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         删除字典数据信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param request: Request对象 | ||
|  |         :param query_db: orm对象 | ||
|  |         :param page_object: 删除字典数据对象 | ||
|  |         :return: 删除字典数据校验结果 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         if page_object.dict_codes: | ||
| 
											2 years ago
										 |             dict_code_list = page_object.dict_codes.split(',') | ||
|  |             try: | ||
| 
											1 year ago
										 |                 delete_dict_type_list = [] | ||
| 
											2 years ago
										 |                 for dict_code in dict_code_list: | ||
| 
											1 year ago
										 |                     dict_data = await cls.dict_data_detail_services(query_db, int(dict_code)) | ||
| 
											1 year ago
										 |                     await DictDataDao.delete_dict_data_dao(query_db, DictDataModel(dictCode=dict_code)) | ||
| 
											1 year ago
										 |                     delete_dict_type_list.append(dict_data.dict_type) | ||
| 
											1 year ago
										 |                 await query_db.commit() | ||
| 
											1 year ago
										 |                 for dict_type in list(set(delete_dict_type_list)): | ||
|  |                     dict_data_list = await cls.query_dict_data_list_services(query_db, dict_type) | ||
| 
											1 year ago
										 |                     await request.app.state.redis.set( | ||
| 
											1 year ago
										 |                         f'{RedisInitKeyConfig.SYS_DICT.key}:{dict_type}', | ||
| 
											1 year ago
										 |                         json.dumps(CamelCaseUtil.transform_result(dict_data_list), ensure_ascii=False, default=str), | ||
|  |                     ) | ||
| 
											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 dict_data_detail_services(cls, query_db: AsyncSession, dict_code: int): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         获取字典数据详细信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param query_db: orm对象 | ||
|  |         :param dict_code: 字典数据id | ||
|  |         :return: 字典数据id对应的信息 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         dict_data = await DictDataDao.get_dict_data_detail_by_id(query_db, dict_code=dict_code) | ||
| 
											1 year ago
										 |         if dict_data: | ||
|  |             result = DictDataModel(**CamelCaseUtil.transform_result(dict_data)) | ||
|  |         else: | ||
|  |             result = DictDataModel(**dict()) | ||
| 
											2 years ago
										 | 
 | ||
|  |         return result | ||
|  | 
 | ||
|  |     @staticmethod | ||
| 
											1 year ago
										 |     async def export_dict_data_list_services(dict_data_list: List): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         导出字典数据信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param dict_data_list: 字典数据信息列表 | ||
|  |         :return: 字典数据信息对应excel的二进制数据 | ||
|  |         """
 | ||
|  |         # 创建一个映射字典,将英文键映射到中文键 | ||
|  |         mapping_dict = { | ||
| 
											1 year ago
										 |             'dictCode': '字典编码', | ||
|  |             'dictSort': '字典标签', | ||
|  |             'dictLabel': '字典键值', | ||
|  |             'dictValue': '字典排序', | ||
|  |             'dictType': '字典类型', | ||
|  |             'cssClass': '样式属性', | ||
|  |             'listClass': '表格回显样式', | ||
|  |             'isDefault': '是否默认', | ||
|  |             'status': '状态', | ||
|  |             'createBy': '创建者', | ||
|  |             'createTime': '创建时间', | ||
|  |             'updateBy': '更新者', | ||
|  |             'updateTime': '更新时间', | ||
|  |             'remark': '备注', | ||
| 
											2 years ago
										 |         } | ||
|  | 
 | ||
|  |         data = dict_data_list | ||
|  | 
 | ||
|  |         for item in data: | ||
|  |             if item.get('status') == '0': | ||
|  |                 item['status'] = '正常' | ||
|  |             else: | ||
|  |                 item['status'] = '停用' | ||
|  |             if item.get('isDefault') == 'Y': | ||
|  |                 item['isDefault'] = '是' | ||
|  |             else: | ||
|  |                 item['isDefault'] = '否' | ||
| 
											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 |