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.
		
		
		
		
			
				
					236 lines
				
				9.2 KiB
			
		
		
			
		
	
	
					236 lines
				
				9.2 KiB
			| 
											2 years ago
										 | from fastapi import Request | ||
| 
											1 year ago
										 | 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.config_dao import ConfigDao | ||
|  | from module_admin.entity.vo.common_vo import CrudResponseModel | ||
|  | from module_admin.entity.vo.config_vo import ConfigModel, ConfigPageQueryModel, DeleteConfigModel | ||
|  | from utils.common_util import CamelCaseUtil, export_list2excel | ||
| 
											2 years ago
										 | 
 | ||
|  | 
 | ||
|  | class ConfigService: | ||
|  |     """
 | ||
|  |     参数配置管理模块服务层 | ||
|  |     """
 | ||
|  | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def get_config_list_services( | ||
|  |         cls, query_db: AsyncSession, query_object: ConfigPageQueryModel, 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
										 |         config_list_result = await ConfigDao.get_config_list(query_db, query_object, is_page) | ||
| 
											2 years ago
										 | 
 | ||
| 
											2 years ago
										 |         return config_list_result | ||
| 
											2 years ago
										 | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def init_cache_sys_config_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_config:开头的键列表 | ||
| 
											1 year ago
										 |         keys = await redis.keys(f'{RedisInitKeyConfig.SYS_CONFIG.key}:*') | ||
| 
											2 years ago
										 |         # 删除匹配的键 | ||
|  |         if keys: | ||
|  |             await redis.delete(*keys) | ||
| 
											1 year ago
										 |         config_all = await ConfigDao.get_config_list(query_db, ConfigPageQueryModel(**dict()), is_page=False) | ||
| 
											2 years ago
										 |         for config_obj in config_all: | ||
| 
											1 year ago
										 |             await redis.set( | ||
| 
											1 year ago
										 |                 f"{RedisInitKeyConfig.SYS_CONFIG.key}:{config_obj.get('configKey')}", | ||
| 
											1 year ago
										 |                 config_obj.get('configValue'), | ||
|  |             ) | ||
| 
											2 years ago
										 | 
 | ||
|  |     @classmethod | ||
|  |     async def query_config_list_from_cache_services(cls, redis, config_key: str): | ||
|  |         """
 | ||
|  |         从缓存获取参数键名对应值service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param redis: redis对象 | ||
|  |         :param config_key: 参数键名 | ||
|  |         :return: 参数键名对应值 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         result = await redis.get(f'{RedisInitKeyConfig.SYS_CONFIG.key}:{config_key}') | ||
| 
											2 years ago
										 | 
 | ||
|  |         return result | ||
|  | 
 | ||
| 
											1 year ago
										 |     @classmethod | ||
|  |     async def check_config_key_unique_services(cls, query_db: AsyncSession, page_object: ConfigModel): | ||
|  |         """
 | ||
|  |         校验参数键名是否唯一service | ||
| 
											1 year ago
										 | 
 | ||
| 
											1 year ago
										 |         :param query_db: orm对象 | ||
|  |         :param page_object: 参数配置对象 | ||
|  |         :return: 校验结果 | ||
|  |         """
 | ||
|  |         config_id = -1 if page_object.config_id is None else page_object.config_id | ||
|  |         config = await ConfigDao.get_config_detail_by_info(query_db, ConfigModel(configKey=page_object.config_key)) | ||
|  |         if config and config.config_id != config_id: | ||
|  |             return CommonConstant.NOT_UNIQUE | ||
|  |         return CommonConstant.UNIQUE | ||
|  | 
 | ||
| 
											2 years ago
										 |     @classmethod | ||
| 
											1 year ago
										 |     async def add_config_services(cls, request: Request, query_db: AsyncSession, page_object: ConfigModel): | ||
| 
											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_config_key_unique_services(query_db, page_object): | ||
| 
											1 year ago
										 |             raise ServiceException(message=f'新增参数{page_object.config_name}失败,参数键名已存在') | ||
| 
											2 years ago
										 |         else: | ||
|  |             try: | ||
| 
											1 year ago
										 |                 await ConfigDao.add_config_dao(query_db, page_object) | ||
|  |                 await query_db.commit() | ||
| 
											1 year ago
										 |                 await request.app.state.redis.set( | ||
| 
											1 year ago
										 |                     f'{RedisInitKeyConfig.SYS_CONFIG.key}:{page_object.config_key}', page_object.config_value | ||
| 
											1 year ago
										 |                 ) | ||
| 
											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_config_services(cls, request: Request, query_db: AsyncSession, page_object: ConfigModel): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         编辑参数配置信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param request: Request对象 | ||
|  |         :param query_db: orm对象 | ||
|  |         :param page_object: 编辑参数配置对象 | ||
|  |         :return: 编辑参数配置校验结果 | ||
|  |         """
 | ||
|  |         edit_config = page_object.model_dump(exclude_unset=True) | ||
| 
											1 year ago
										 |         config_info = await cls.config_detail_services(query_db, page_object.config_id) | ||
|  |         if config_info.config_id: | ||
|  |             if not await cls.check_config_key_unique_services(query_db, page_object): | ||
| 
											1 year ago
										 |                 raise ServiceException(message=f'修改参数{page_object.config_name}失败,参数键名已存在') | ||
| 
											1 year ago
										 |             else: | ||
|  |                 try: | ||
|  |                     await ConfigDao.edit_config_dao(query_db, edit_config) | ||
|  |                     await query_db.commit() | ||
|  |                     if config_info.config_key != page_object.config_key: | ||
| 
											1 year ago
										 |                         await request.app.state.redis.delete( | ||
| 
											1 year ago
										 |                             f'{RedisInitKeyConfig.SYS_CONFIG.key}:{config_info.config_key}' | ||
| 
											1 year ago
										 |                         ) | ||
|  |                     await request.app.state.redis.set( | ||
| 
											1 year ago
										 |                         f'{RedisInitKeyConfig.SYS_CONFIG.key}:{page_object.config_key}', page_object.config_value | ||
| 
											1 year ago
										 |                     ) | ||
| 
											1 year ago
										 |                     return CrudResponseModel(is_success=True, message='更新成功') | ||
| 
											1 year ago
										 |                 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_config_services(cls, request: Request, query_db: AsyncSession, page_object: DeleteConfigModel): | ||
| 
											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.config_ids: | ||
| 
											2 years ago
										 |             config_id_list = page_object.config_ids.split(',') | ||
|  |             try: | ||
| 
											1 year ago
										 |                 delete_config_key_list = [] | ||
| 
											2 years ago
										 |                 for config_id in config_id_list: | ||
| 
											1 year ago
										 |                     config_info = await cls.config_detail_services(query_db, int(config_id)) | ||
|  |                     if config_info.config_type == CommonConstant.YES: | ||
| 
											1 year ago
										 |                         raise ServiceException(message=f'内置参数{config_info.config_key}不能删除') | ||
| 
											1 year ago
										 |                     else: | ||
|  |                         await ConfigDao.delete_config_dao(query_db, ConfigModel(configId=int(config_id))) | ||
| 
											1 year ago
										 |                         delete_config_key_list.append(f'{RedisInitKeyConfig.SYS_CONFIG.key}:{config_info.config_key}') | ||
| 
											1 year ago
										 |                 await query_db.commit() | ||
| 
											1 year ago
										 |                 if delete_config_key_list: | ||
|  |                     await request.app.state.redis.delete(*delete_config_key_list) | ||
| 
											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 config_detail_services(cls, query_db: AsyncSession, config_id: int): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         获取参数配置详细信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param query_db: orm对象 | ||
|  |         :param config_id: 参数配置id | ||
|  |         :return: 参数配置id对应的信息 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         config = await ConfigDao.get_config_detail_by_id(query_db, config_id=config_id) | ||
| 
											1 year ago
										 |         if config: | ||
|  |             result = ConfigModel(**CamelCaseUtil.transform_result(config)) | ||
|  |         else: | ||
|  |             result = ConfigModel(**dict()) | ||
| 
											2 years ago
										 | 
 | ||
|  |         return result | ||
|  | 
 | ||
|  |     @staticmethod | ||
| 
											1 year ago
										 |     async def export_config_list_services(config_list: List): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         导出参数配置信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param config_list: 参数配置信息列表 | ||
|  |         :return: 参数配置信息对应excel的二进制数据 | ||
|  |         """
 | ||
|  |         # 创建一个映射字典,将英文键映射到中文键 | ||
|  |         mapping_dict = { | ||
| 
											1 year ago
										 |             'configId': '参数主键', | ||
|  |             'configName': '参数名称', | ||
|  |             'configKey': '参数键名', | ||
|  |             'configValue': '参数键值', | ||
|  |             'configType': '系统内置', | ||
|  |             'createBy': '创建者', | ||
|  |             'createTime': '创建时间', | ||
|  |             'updateBy': '更新者', | ||
|  |             'updateTime': '更新时间', | ||
|  |             'remark': '备注', | ||
| 
											2 years ago
										 |         } | ||
|  | 
 | ||
|  |         data = config_list | ||
|  | 
 | ||
|  |         for item in data: | ||
|  |             if item.get('configType') == 'Y': | ||
|  |                 item['configType'] = '是' | ||
|  |             else: | ||
|  |                 item['configType'] = '否' | ||
| 
											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_config_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 cls.init_cache_sys_config_services(query_db, request.app.state.redis) | ||
|  | 
 | ||
| 
											1 year ago
										 |         return CrudResponseModel(is_success=True, message='刷新成功') |