upload project source code
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
@@ -0,0 +1,425 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from fastapi import APIRouter, Body, Depends, Path
|
||||
from fastapi.responses import JSONResponse, StreamingResponse
|
||||
from redis.asyncio.client import Redis
|
||||
|
||||
from app.common.response import StreamResponse, SuccessResponse
|
||||
from app.common.request import PaginationService
|
||||
from app.core.base_params import PaginationQueryParam
|
||||
from app.core.base_schema import BatchSetAvailable
|
||||
from app.core.dependencies import AuthPermission, redis_getter
|
||||
from app.core.logger import log
|
||||
from app.core.router_class import OperationLogRoute
|
||||
from app.utils.common_util import bytes2file_response
|
||||
|
||||
from ..auth.schema import AuthSchema
|
||||
from .service import DictTypeService, DictDataService
|
||||
from .schema import (
|
||||
DictTypeCreateSchema,
|
||||
DictTypeUpdateSchema,
|
||||
DictDataCreateSchema,
|
||||
DictDataUpdateSchema,
|
||||
DictDataQueryParam,
|
||||
DictTypeQueryParam
|
||||
)
|
||||
|
||||
|
||||
DictRouter = APIRouter(route_class=OperationLogRoute, prefix="/dict", tags=["字典管理"])
|
||||
|
||||
@DictRouter.get("/type/detail/{id}", summary="获取字典类型详情", description="获取字典类型详情")
|
||||
async def get_type_detail_controller(
|
||||
id: int = Path(..., description="字典类型ID", ge=1),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:dict_type:query"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
获取字典类型详情
|
||||
|
||||
参数:
|
||||
- id (int): 字典类型ID
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含字典类型详情的响应模型
|
||||
|
||||
异常:
|
||||
- CustomException: 获取字典类型详情失败时抛出异常。
|
||||
"""
|
||||
result_dict = await DictTypeService.get_obj_detail_service(id=id, auth=auth)
|
||||
log.info(f"获取字典类型详情成功 {id}")
|
||||
return SuccessResponse(data=result_dict, msg="获取字典类型详情成功")
|
||||
|
||||
@DictRouter.get("/type/list", summary="查询字典类型", description="查询字典类型")
|
||||
async def get_type_list_controller(
|
||||
page: PaginationQueryParam = Depends(),
|
||||
search: DictTypeQueryParam = Depends(),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:dict_type:query"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
查询字典类型
|
||||
|
||||
参数:
|
||||
- page (PaginationQueryParam): 分页参数模型
|
||||
- search (DictTypeQueryParam): 查询参数模型
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含查询字典类型结果的响应模型
|
||||
|
||||
异常:
|
||||
- CustomException: 查询字典类型失败时抛出异常。
|
||||
"""
|
||||
result_dict_list = await DictTypeService.get_obj_list_service(auth=auth, search=search, order_by=page.order_by)
|
||||
result_dict = await PaginationService.paginate(data_list= result_dict_list, page_no= page.page_no, page_size = page.page_size)
|
||||
log.info(f"查询字典类型列表成功")
|
||||
return SuccessResponse(data=result_dict, msg="查询字典类型列表成功")
|
||||
|
||||
@DictRouter.get("/type/optionselect", summary="获取全部字典类型", description="获取全部字典类型")
|
||||
async def get_type_loptionselect_controller(
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:dict_type:query"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
获取全部字典类型
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含全部字典类型的响应模型
|
||||
|
||||
异常:
|
||||
- CustomException: 获取字典类型列表失败时抛出异常。
|
||||
"""
|
||||
result_dict_list = await DictTypeService.get_obj_list_service(auth=auth)
|
||||
log.info(f"获取字典类型列表成功")
|
||||
return SuccessResponse(data=result_dict_list, msg="获取字典类型列表成功")
|
||||
|
||||
@DictRouter.post("/type/create", summary="创建字典类型", description="创建字典类型")
|
||||
async def create_type_controller(
|
||||
data: DictTypeCreateSchema,
|
||||
redis: Redis = Depends(redis_getter),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:dict_type:create"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
创建字典类型
|
||||
|
||||
参数:
|
||||
- data (DictTypeCreateSchema): 创建字典类型的入参模型
|
||||
- redis (Redis): Redis数据库连接
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含创建字典类型结果的响应模型
|
||||
|
||||
异常:
|
||||
- CustomException: 创建字典类型失败时抛出异常。
|
||||
"""
|
||||
result_dict = await DictTypeService.create_obj_service(auth=auth, redis=redis, data=data)
|
||||
log.info(f"创建字典类型成功: {result_dict}")
|
||||
return SuccessResponse(data=result_dict, msg="创建字典类型成功")
|
||||
|
||||
@DictRouter.put("/type/update/{id}", summary="修改字典类型", description="修改字典类型")
|
||||
async def update_type_controller(
|
||||
data: DictTypeUpdateSchema,
|
||||
redis: Redis = Depends(redis_getter),
|
||||
id: int = Path(..., description="字典类型ID", ge=1),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:dict_type:update"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
修改字典类型
|
||||
|
||||
参数:
|
||||
- data (DictTypeUpdateSchema): 修改字典类型的入参模型
|
||||
- redis (Redis): Redis数据库连接
|
||||
- id (int): 字典类型ID
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含修改字典类型结果的响应模型
|
||||
|
||||
异常:
|
||||
- CustomException: 修改字典类型失败时抛出异常。
|
||||
"""
|
||||
result_dict = await DictTypeService.update_obj_service(auth=auth, redis=redis, id=id, data=data)
|
||||
log.info(f"修改字典类型成功: {result_dict}")
|
||||
return SuccessResponse(data=result_dict, msg="修改字典类型成功")
|
||||
|
||||
@DictRouter.delete("/type/delete", summary="删除字典类型", description="删除字典类型")
|
||||
async def delete_type_controller(
|
||||
redis: Redis = Depends(redis_getter),
|
||||
ids: list[int] = Body(..., description="ID列表"),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:dict_type:delete"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
删除字典类型
|
||||
|
||||
参数:
|
||||
- redis (Redis): Redis数据库连接
|
||||
- ids (list[int]): 字典类型ID列表
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含删除字典类型结果的响应模型
|
||||
|
||||
异常:
|
||||
- CustomException: 删除字典类型失败时抛出异常。
|
||||
"""
|
||||
await DictTypeService.delete_obj_service(auth=auth, redis=redis, ids=ids)
|
||||
log.info(f"删除字典类型成功: {ids}")
|
||||
return SuccessResponse(msg="删除字典类型成功")
|
||||
|
||||
@DictRouter.patch("/type/available/setting", summary="批量修改字典类型状态", description="批量修改字典类型状态")
|
||||
async def batch_set_available_dict_type_controller(
|
||||
data: BatchSetAvailable,
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:dict_type:patch"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
批量修改字典类型状态
|
||||
|
||||
参数:
|
||||
- data (BatchSetAvailable): 批量修改字典类型状态负载模型
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含批量修改字典类型状态结果的响应模型
|
||||
|
||||
异常:
|
||||
- CustomException: 批量修改字典类型状态失败时抛出异常。
|
||||
"""
|
||||
await DictTypeService.set_obj_available_service(auth=auth, data=data)
|
||||
log.info(f"批量修改字典类型状态成功: {data.ids}")
|
||||
return SuccessResponse(msg="批量修改字典类型状态成功")
|
||||
|
||||
@DictRouter.post('/type/export', summary="导出字典类型", description="导出字典类型")
|
||||
async def export_type_list_controller(
|
||||
search: DictTypeQueryParam = Depends(),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:dict_type:export"]))
|
||||
) -> StreamingResponse:
|
||||
"""
|
||||
导出字典类型
|
||||
|
||||
参数:
|
||||
- search (DictTypeQueryParam): 查询参数模型
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- StreamingResponse: 包含导出字典类型结果的响应模型
|
||||
|
||||
异常:
|
||||
- CustomException: 导出字典类型失败时抛出异常。
|
||||
"""
|
||||
# 获取全量数据
|
||||
result_dict_list = await DictTypeService.get_obj_list_service(search=search, auth=auth)
|
||||
export_result = await DictTypeService.export_obj_service(data_list=result_dict_list)
|
||||
log.info('导出字典类型成功')
|
||||
|
||||
return StreamResponse(
|
||||
data=bytes2file_response(export_result),
|
||||
media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
headers = {
|
||||
'Content-Disposition': 'attachment; filename=dict_type.xlsx'
|
||||
}
|
||||
)
|
||||
|
||||
@DictRouter.get("/data/detail/{id}", summary="获取字典数据详情", description="获取字典数据详情")
|
||||
async def get_data_detail_controller(
|
||||
id: int = Path(..., description="字典数据ID", ge=1),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:dict_data:query"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
获取字典数据详情
|
||||
|
||||
参数:
|
||||
- id (int): 字典数据ID
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含字典数据详情的响应模型
|
||||
|
||||
异常:
|
||||
- CustomException: 获取字典数据详情失败时抛出异常。
|
||||
"""
|
||||
result_dict = await DictDataService.get_obj_detail_service(id=id, auth=auth)
|
||||
log.info(f"获取字典数据详情成功 {id}")
|
||||
return SuccessResponse(data=result_dict, msg="获取字典数据详情成功")
|
||||
|
||||
@DictRouter.get("/data/list", summary="查询字典数据", description="查询字典数据")
|
||||
async def get_data_list_controller(
|
||||
page: PaginationQueryParam = Depends(),
|
||||
search: DictDataQueryParam = Depends(),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:dict_data:query"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
查询字典数据
|
||||
|
||||
参数:
|
||||
- page (PaginationQueryParam): 分页查询参数模型
|
||||
- search (DictDataQueryParam): 查询参数模型
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含字典数据列表的响应模型
|
||||
|
||||
异常:
|
||||
- CustomException: 查询字典数据列表失败时抛出异常。
|
||||
"""
|
||||
order_by = [{"order": "asc"}]
|
||||
if page.order_by:
|
||||
order_by = page.order_by
|
||||
result_dict_list = await DictDataService.get_obj_list_service(auth=auth, search=search, order_by=order_by)
|
||||
result_dict = await PaginationService.paginate(data_list= result_dict_list, page_no= page.page_no, page_size = page.page_size)
|
||||
log.info(f"查询字典数据列表成功")
|
||||
return SuccessResponse(data=result_dict, msg="查询字典数据列表成功")
|
||||
|
||||
@DictRouter.post("/data/create", summary="创建字典数据", description="创建字典数据")
|
||||
async def create_data_controller(
|
||||
data: DictDataCreateSchema,
|
||||
redis: Redis = Depends(redis_getter),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:dict_data:create"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
创建字典数据
|
||||
|
||||
参数:
|
||||
- data (DictDataCreateSchema): 创建字典数据负载模型
|
||||
- redis (Redis): Redis数据库连接
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含创建字典数据结果的响应模型
|
||||
|
||||
异常:
|
||||
- CustomException: 创建字典数据失败时抛出异常。
|
||||
"""
|
||||
result_dict = await DictDataService.create_obj_service(auth=auth, redis=redis, data=data)
|
||||
log.info(f"创建字典数据成功: {result_dict}")
|
||||
return SuccessResponse(data=result_dict, msg="创建字典数据成功")
|
||||
|
||||
@DictRouter.put("/data/update/{id}", summary="修改字典数据", description="修改字典数据")
|
||||
async def update_data_controller(
|
||||
data: DictDataUpdateSchema,
|
||||
redis: Redis = Depends(redis_getter),
|
||||
id: int = Path(..., description="字典数据ID"),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:dict_data:update"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
修改字典数据
|
||||
|
||||
参数:
|
||||
- data (DictDataUpdateSchema): 修改字典数据负载模型
|
||||
- redis (Redis): Redis数据库连接
|
||||
- id (int): 字典数据ID
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含修改字典数据结果的响应模型
|
||||
|
||||
异常:
|
||||
- CustomException: 修改字典数据失败时抛出异常。
|
||||
"""
|
||||
result_dict = await DictDataService.update_obj_service(auth=auth, redis=redis, id=id, data=data)
|
||||
log.info(f"修改字典数据成功: {result_dict}")
|
||||
return SuccessResponse(data=result_dict, msg="修改字典数据成功")
|
||||
|
||||
@DictRouter.delete("/data/delete", summary="删除字典数据", description="删除字典数据")
|
||||
async def delete_data_controller(
|
||||
redis: Redis = Depends(redis_getter),
|
||||
ids: list[int] = Body(..., description="ID列表"),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:dict_data:delete"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
删除字典数据
|
||||
|
||||
参数:
|
||||
- redis (Redis): Redis数据库连接
|
||||
- ids (list[int]): 字典数据ID列表
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含删除字典数据结果的响应模型
|
||||
|
||||
异常:
|
||||
- CustomException: 删除字典数据失败时抛出异常。
|
||||
"""
|
||||
await DictDataService.delete_obj_service(auth=auth, redis=redis, ids=ids)
|
||||
log.info(f"删除字典数据成功: {ids}")
|
||||
return SuccessResponse(msg="删除字典数据成功")
|
||||
|
||||
@DictRouter.patch("/data/available/setting", summary="批量修改字典数据状态", description="批量修改字典数据状态")
|
||||
async def batch_set_available_dict_data_controller(
|
||||
data: BatchSetAvailable,
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:dict_data:patch"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
批量修改字典数据状态
|
||||
|
||||
参数:
|
||||
- data (BatchSetAvailable): 批量修改字典数据状态负载模型
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含批量修改字典数据状态结果的响应模型
|
||||
|
||||
异常:
|
||||
- CustomException: 批量修改字典数据状态失败时抛出异常。
|
||||
"""
|
||||
await DictDataService.set_obj_available_service(auth=auth, data=data)
|
||||
log.info(f"批量修改字典数据状态成功: {data.ids}")
|
||||
return SuccessResponse(msg="批量修改字典数据状态成功")
|
||||
|
||||
@DictRouter.post('/data/export', summary="导出字典数据", description="导出字典数据")
|
||||
async def export_data_list_controller(
|
||||
search: DictDataQueryParam = Depends(),
|
||||
page: PaginationQueryParam = Depends(),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:dict_data:export"]))
|
||||
) -> StreamingResponse:
|
||||
"""
|
||||
导出字典数据
|
||||
|
||||
参数:
|
||||
- search (DictDataQueryParam): 查询参数模型
|
||||
- page (PaginationQueryParam): 分页参数模型
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- StreamingResponse: 包含导出字典数据结果的响应模型
|
||||
|
||||
异常:
|
||||
- CustomException: 导出字典数据失败时抛出异常。
|
||||
"""
|
||||
result_dict_list = await DictDataService.get_obj_list_service(auth=auth, search=search, order_by=page.order_by)
|
||||
export_result = await DictDataService.export_obj_service(data_list=result_dict_list)
|
||||
log.info('导出字典数据成功')
|
||||
|
||||
return StreamResponse(
|
||||
data=bytes2file_response(export_result),
|
||||
media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
headers = {
|
||||
'Content-Disposition': 'attachment; filename=dice_data.xlsx'
|
||||
}
|
||||
)
|
||||
|
||||
@DictRouter.get('/data/info/{dict_type}', summary="根据字典类型获取数据", description="根据字典类型获取数据")
|
||||
async def get_init_dict_data_controller(
|
||||
dict_type: str,
|
||||
redis: Redis = Depends(redis_getter)
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
根据字典类型获取数据
|
||||
|
||||
参数:
|
||||
- dict_type (str): 字典类型
|
||||
- redis (Redis): Redis数据库连接
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含根据字典类型获取数据结果的响应模型
|
||||
|
||||
异常:
|
||||
- CustomException: 根据字典类型获取数据失败时抛出异常。
|
||||
"""
|
||||
dict_data_query_result = await DictDataService.get_init_dict_service(
|
||||
redis=redis, dict_type=dict_type
|
||||
)
|
||||
log.info(f"获取初始化字典数据成功:{dict_data_query_result}")
|
||||
|
||||
return SuccessResponse(data=dict_data_query_result, msg="获取初始化字典数据成功")
|
||||
257
后端源码/yifan.action-ai.cn/app/api/v1/module_system/dict/crud.py
Normal file
257
后端源码/yifan.action-ai.cn/app/api/v1/module_system/dict/crud.py
Normal file
@@ -0,0 +1,257 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import Sequence
|
||||
|
||||
from app.core.base_crud import CRUDBase
|
||||
from app.api.v1.module_system.dict.model import DictDataModel, DictTypeModel
|
||||
from app.api.v1.module_system.dict.schema import DictDataCreateSchema, DictDataUpdateSchema, DictTypeCreateSchema, DictTypeUpdateSchema
|
||||
from app.api.v1.module_system.auth.schema import AuthSchema
|
||||
|
||||
|
||||
class DictTypeCRUD(CRUDBase[DictTypeModel, DictTypeCreateSchema, DictTypeUpdateSchema]):
|
||||
"""数据字典类型数据层"""
|
||||
|
||||
def __init__(self, auth: AuthSchema) -> None:
|
||||
"""
|
||||
初始化数据字典类型CRUD
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
"""
|
||||
self.auth = auth
|
||||
super().__init__(model=DictTypeModel, auth=auth)
|
||||
|
||||
async def get_obj_by_id_crud(self, id: int, preload: list | None = None) -> DictTypeModel | None:
|
||||
"""
|
||||
获取数据字典类型详情
|
||||
|
||||
参数:
|
||||
- id (int): 数据字典类型ID
|
||||
- preload (list | None): 预加载关系,未提供时使用模型默认项
|
||||
|
||||
返回:
|
||||
- DictTypeModel | None: 数据字典类型模型,如果不存在则为None
|
||||
"""
|
||||
# 添加默认预加载字典数据关系
|
||||
if preload is None:
|
||||
preload = []
|
||||
return await self.get(id=id, preload=preload)
|
||||
|
||||
async def get_obj_list_crud(self, search: dict | None = None, order_by: list[dict] | None = None, preload: list | None = None) -> Sequence[DictTypeModel]:
|
||||
"""
|
||||
获取数据字典类型列表
|
||||
|
||||
参数:
|
||||
- search (dict | None): 查询参数,默认值为None
|
||||
- order_by (list[dict] | None): 排序参数,默认值为None
|
||||
- preload (list | None): 预加载关系,未提供时使用模型默认项
|
||||
|
||||
返回:
|
||||
- Sequence[DictTypeModel]: 数据字典类型模型序列
|
||||
"""
|
||||
# 添加默认预加载字典数据关系
|
||||
if preload is None:
|
||||
preload = []
|
||||
return await self.list(search=search, order_by=order_by, preload=preload)
|
||||
|
||||
async def create_obj_crud(self, data: DictTypeCreateSchema) -> DictTypeModel | None:
|
||||
"""
|
||||
创建数据字典类型
|
||||
|
||||
参数:
|
||||
- data (DictTypeCreateSchema): 数据字典类型创建模型
|
||||
|
||||
返回:
|
||||
- DictTypeModel | None: 创建的数据字典类型模型,如果创建失败则为None
|
||||
"""
|
||||
return await self.create(data=data)
|
||||
|
||||
async def update_obj_crud(self, id: int, data: DictTypeUpdateSchema) -> DictTypeModel | None:
|
||||
"""
|
||||
更新数据字典类型
|
||||
|
||||
参数:
|
||||
- id (int): 数据字典类型ID
|
||||
- data (DictTypeUpdateSchema): 数据字典类型更新模型
|
||||
|
||||
返回:
|
||||
- DictTypeModel | None: 更新的数据字典类型模型,如果更新失败则为None
|
||||
"""
|
||||
return await self.update(id=id, data=data)
|
||||
|
||||
async def delete_obj_crud(self, ids: list[int]) -> None:
|
||||
"""
|
||||
删除数据字典类型
|
||||
|
||||
参数:
|
||||
- ids (list[int]): 数据字典类型ID列表
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
return await self.delete(ids=ids)
|
||||
|
||||
async def set_obj_available_crud(self, ids: list[int], status: str) -> None:
|
||||
"""
|
||||
设置数据字典类型的可用状态
|
||||
|
||||
参数:
|
||||
- ids (list[int]): 数据字典类型ID列表
|
||||
- status (str): 可用状态,0表示正常,1表示停用
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
return await self.set(ids=ids, status=status)
|
||||
|
||||
async def batch_delete_obj_crud(self, ids: list[int]) -> int:
|
||||
"""
|
||||
批量删除数据字典类型
|
||||
|
||||
参数:
|
||||
- ids (List[int]): 数据字典类型ID列表
|
||||
|
||||
返回:
|
||||
- int: 删除的记录数量
|
||||
"""
|
||||
await self.delete(ids=ids)
|
||||
return len(ids)
|
||||
|
||||
|
||||
class DictDataCRUD(CRUDBase[DictDataModel, DictDataCreateSchema, DictDataUpdateSchema]):
|
||||
"""数据字典数据层"""
|
||||
|
||||
def __init__(self, auth: AuthSchema) -> None:
|
||||
"""
|
||||
初始化数据字典数据CRUD
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
"""
|
||||
self.auth = auth
|
||||
super().__init__(model=DictDataModel, auth=auth)
|
||||
|
||||
async def get_obj_by_id_crud(self, id: int, preload: list | None = None) -> DictDataModel | None:
|
||||
"""
|
||||
获取数据字典数据详情
|
||||
|
||||
参数:
|
||||
- id (int): 数据字典数据ID
|
||||
- preload (list | None): 预加载关系,未提供时使用模型默认项
|
||||
|
||||
返回:
|
||||
- DictDataModel | None: 数据字典数据模型,如果不存在则为None
|
||||
"""
|
||||
# 添加默认预加载字典类型关系
|
||||
if preload is None:
|
||||
preload = []
|
||||
return await self.get(id=id, preload=preload)
|
||||
|
||||
async def get_obj_list_crud(self, search: dict | None = None, order_by: list[dict] | None = None, preload: list | None = None) -> Sequence[DictDataModel]:
|
||||
"""
|
||||
获取数据字典数据列表
|
||||
|
||||
参数:
|
||||
- search (dict | None): 查询参数,默认值为None
|
||||
- order_by (list[dict] | None): 排序参数,默认值为None
|
||||
- preload (list | None): 预加载关系,未提供时使用模型默认项
|
||||
|
||||
返回:
|
||||
- Sequence[DictDataModel]: 数据字典数据模型序列
|
||||
"""
|
||||
# 添加默认预加载字典类型关系
|
||||
if preload is None:
|
||||
preload = []
|
||||
return await self.list(search=search, order_by=order_by, preload=preload)
|
||||
|
||||
async def create_obj_crud(self, data: DictDataCreateSchema) -> DictDataModel | None:
|
||||
"""
|
||||
创建数据字典数据
|
||||
|
||||
参数:
|
||||
- data (DictDataCreateSchema): 数据字典数据创建模型
|
||||
|
||||
返回:
|
||||
- DictDataModel | None: 创建的数据字典数据模型,如果创建失败则为None
|
||||
"""
|
||||
return await self.create(data=data)
|
||||
|
||||
async def update_obj_crud(self, id: int, data: DictDataUpdateSchema) -> DictDataModel | None:
|
||||
"""
|
||||
更新数据字典数据
|
||||
|
||||
参数:
|
||||
- id (int): 数据字典数据ID
|
||||
- data (DictDataUpdateSchema): 数据字典数据更新模型
|
||||
|
||||
返回:
|
||||
- DictDataModel | None: 更新的数据字典数据模型,如果更新失败则为None
|
||||
"""
|
||||
return await self.update(id=id, data=data)
|
||||
|
||||
async def delete_obj_crud(self, ids: list[int]) -> None:
|
||||
"""
|
||||
删除数据字典数据
|
||||
|
||||
参数:
|
||||
- ids (list[int]): 数据字典数据ID列表
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
return await self.delete(ids=ids)
|
||||
|
||||
async def set_obj_available_crud(self, ids: list[int], status: str) -> None:
|
||||
"""
|
||||
设置数据字典数据的可用状态
|
||||
|
||||
参数:
|
||||
- ids (list[int]): 数据字典数据ID列表
|
||||
- status (str): 可用状态,0表示正常,1表示停用
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
return await self.set(ids=ids, status=status)
|
||||
|
||||
async def batch_delete_obj_crud(self, ids: list[int], exclude_system: bool = True) -> int:
|
||||
"""
|
||||
批量删除数据字典数据
|
||||
|
||||
参数:
|
||||
- ids (List[int]): 数据字典数据ID列表
|
||||
- exclude_system (bool): 是否排除系统默认数据,默认为True
|
||||
|
||||
返回:
|
||||
- int: 删除的记录数量
|
||||
"""
|
||||
# 如果需要排除系统默认数据,可以在这里添加过滤逻辑
|
||||
# 假设系统默认数据在remark字段中包含"系统默认"字符串
|
||||
if exclude_system:
|
||||
# 获取非系统默认数据的ID
|
||||
system_data_filter = {"id__in": ids, "remark__contains": "系统默认"}
|
||||
system_data = await self.list(search=system_data_filter)
|
||||
system_ids = [item.id for item in system_data]
|
||||
# 从待删除ID列表中排除系统默认数据
|
||||
ids = [id for id in ids if id not in system_ids]
|
||||
|
||||
if ids:
|
||||
await self.delete(ids=ids)
|
||||
return len(ids)
|
||||
|
||||
async def get_obj_list_by_dict_type_crud(self, dict_type: str, status: str | None = "0") -> Sequence[DictDataModel]:
|
||||
"""
|
||||
根据字典类型获取字典数据列表
|
||||
|
||||
参数:
|
||||
- dict_type (str): 字典类型
|
||||
- status (str | None): 状态过滤,None表示不过滤
|
||||
|
||||
返回:
|
||||
- Sequence[DictDataModel]: 数据字典数据模型序列
|
||||
"""
|
||||
search = {"dict_type": dict_type}
|
||||
if status is not None:
|
||||
search["status"] = status
|
||||
order_by = [{"id": "asc"}]
|
||||
return await self.list(search=search, order_by=order_by)
|
||||
@@ -0,0 +1,47 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from sqlalchemy import String, Integer, Boolean, ForeignKey
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.core.base_model import ModelMixin
|
||||
|
||||
|
||||
class DictTypeModel(ModelMixin):
|
||||
"""
|
||||
字典类型表
|
||||
"""
|
||||
__tablename__: str = "sys_dict_type"
|
||||
__table_args__: dict[str, str] = ({'comment': '字典类型表'})
|
||||
|
||||
dict_name: Mapped[str] = mapped_column(String(255), nullable=False, comment='字典名称')
|
||||
dict_type: Mapped[str] = mapped_column(String(255), nullable=False, unique=True, comment='字典类型')
|
||||
|
||||
# 关系定义
|
||||
dict_data_list: Mapped[list["DictDataModel"]] = relationship("DictDataModel", back_populates="dict_type_obj", cascade="all, delete-orphan")
|
||||
|
||||
|
||||
class DictDataModel(ModelMixin):
|
||||
"""
|
||||
字典数据表
|
||||
"""
|
||||
__tablename__: str = "sys_dict_data"
|
||||
__table_args__: dict[str, str] = ({'comment': '字典数据表'})
|
||||
|
||||
dict_sort: Mapped[int] = mapped_column(Integer, nullable=False, default=0, comment='字典排序')
|
||||
dict_label: Mapped[str] = mapped_column(String(255), nullable=False, comment='字典标签')
|
||||
dict_value: Mapped[str] = mapped_column(String(255), nullable=False, comment='字典键值')
|
||||
css_class: Mapped[str | None] = mapped_column(String(255), nullable=True, comment='样式属性(其他样式扩展)')
|
||||
list_class: Mapped[str | None] = mapped_column(String(255), nullable=True, comment='表格回显样式')
|
||||
is_default: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, comment='是否默认(True是 False否)')
|
||||
dict_type: Mapped[str] = mapped_column(String(255), nullable=False, comment='字典类型')
|
||||
|
||||
# 添加外键关系,同时保留dict_type字段用于业务查询
|
||||
dict_type_id: Mapped[int] = mapped_column(
|
||||
Integer,
|
||||
ForeignKey('sys_dict_type.id', ondelete='CASCADE'),
|
||||
nullable=False,
|
||||
comment='字典类型ID'
|
||||
)
|
||||
|
||||
# 关系定义
|
||||
dict_type_obj: Mapped[DictTypeModel] = relationship("DictTypeModel", back_populates="dict_data_list")
|
||||
143
后端源码/yifan.action-ai.cn/app/api/v1/module_system/dict/schema.py
Normal file
143
后端源码/yifan.action-ai.cn/app/api/v1/module_system/dict/schema.py
Normal file
@@ -0,0 +1,143 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import re
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
|
||||
from fastapi import Query
|
||||
|
||||
from app.core.validator import DateTimeStr
|
||||
from app.core.base_schema import BaseSchema
|
||||
|
||||
|
||||
class DictTypeCreateSchema(BaseModel):
|
||||
"""
|
||||
字典类型表对应pydantic模型
|
||||
"""
|
||||
|
||||
dict_name: str = Field(..., min_length=1, max_length=64, description='字典名称')
|
||||
dict_type: str = Field(..., min_length=1, max_length=100, description='字典类型')
|
||||
status: str = Field(default='0', description='状态(0正常 1停用)')
|
||||
description: str | None = Field(default=None, max_length=255, description="描述")
|
||||
|
||||
@field_validator('dict_name')
|
||||
def validate_dict_name(cls, value: str):
|
||||
if not value or value.strip() == '':
|
||||
raise ValueError('字典名称不能为空')
|
||||
return value.strip()
|
||||
|
||||
@field_validator('dict_type')
|
||||
def validate_dict_type(cls, value: str):
|
||||
if not value or value.strip() == '':
|
||||
raise ValueError('字典类型不能为空')
|
||||
regexp = r'^[a-z][a-z0-9_]*$'
|
||||
if not re.match(regexp, value):
|
||||
raise ValueError('字典类型必须以字母开头,且只能为(小写字母,数字,下滑线)')
|
||||
return value.strip()
|
||||
|
||||
|
||||
class DictTypeUpdateSchema(DictTypeCreateSchema):
|
||||
"""字典类型更新模型"""
|
||||
...
|
||||
|
||||
|
||||
class DictTypeOutSchema(DictTypeCreateSchema, BaseSchema):
|
||||
"""字典类型响应模型"""
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
|
||||
class DictTypeQueryParam:
|
||||
"""字典类型查询参数"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
dict_name: str | None = Query(default=None, description="字典名称", max_length=100),
|
||||
dict_type: str | None = Query(default=None, description="字典类型", max_length=100),
|
||||
status: str | None = Query(default=None, description="状态(0正常 1停用)"),
|
||||
created_time: list[DateTimeStr] | None = Query(None, description="创建时间范围", examples=["2025-01-01 00:00:00", "2025-12-31 23:59:59"]),
|
||||
updated_time: list[DateTimeStr] | None = Query(None, description="更新时间范围", examples=["2025-01-01 00:00:00", "2025-12-31 23:59:59"]),
|
||||
) -> None:
|
||||
super().__init__()
|
||||
|
||||
# 模糊查询字段
|
||||
self.dict_name = ("like", f"%{dict_name.strip()}%") if dict_name and dict_name.strip() else None
|
||||
|
||||
# 精确查询字段
|
||||
self.dict_type = dict_type.strip() if dict_type else None
|
||||
self.status = status
|
||||
|
||||
# 时间范围查询
|
||||
if created_time and len(created_time) == 2:
|
||||
self.created_time = ("between", (created_time[0], created_time[1]))
|
||||
if updated_time and len(updated_time) == 2:
|
||||
self.updated_time = ("between", (updated_time[0], updated_time[1]))
|
||||
|
||||
|
||||
class DictDataCreateSchema(BaseModel):
|
||||
"""
|
||||
字典数据表对应pydantic模型
|
||||
"""
|
||||
dict_sort: int = Field(..., ge=1, le=999, description='字典排序')
|
||||
dict_label: str = Field(..., max_length=100, description='字典标签')
|
||||
dict_value: str = Field(..., max_length=100, description='字典键值')
|
||||
dict_type: str = Field(..., max_length=100, description='字典类型')
|
||||
dict_type_id: int = Field(..., description='字典类型ID')
|
||||
css_class: str | None = Field(default=None, max_length=100, description='样式属性(其他样式扩展)')
|
||||
list_class: str | None = Field(default=None, description='表格回显样式')
|
||||
is_default: bool = Field(default=False, description='是否默认(True是 False否)')
|
||||
status: str = Field(default='0', description='状态(0正常 1停用)')
|
||||
description: str | None = Field(default=None, max_length=255, description="描述")
|
||||
|
||||
@model_validator(mode='after')
|
||||
def validate_after(self):
|
||||
if not self.dict_label or not self.dict_label.strip():
|
||||
raise ValueError('字典标签不能为空')
|
||||
if not self.dict_value or not self.dict_value.strip():
|
||||
raise ValueError('字典键值不能为空')
|
||||
if not self.dict_type or not self.dict_type.strip():
|
||||
raise ValueError('字典类型不能为空')
|
||||
if not hasattr(self, 'dict_type_id') or self.dict_type_id <= 0:
|
||||
raise ValueError('字典类型ID不能为空且必须大于0')
|
||||
|
||||
# 确保字符串字段被正确处理
|
||||
self.dict_label = self.dict_label.strip()
|
||||
self.dict_value = self.dict_value.strip()
|
||||
self.dict_type = self.dict_type.strip()
|
||||
|
||||
return self
|
||||
|
||||
|
||||
class DictDataUpdateSchema(DictDataCreateSchema):
|
||||
"""字典数据更新模型"""
|
||||
...
|
||||
|
||||
|
||||
class DictDataOutSchema(DictDataCreateSchema, BaseSchema):
|
||||
"""字典数据响应模型"""
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
|
||||
class DictDataQueryParam:
|
||||
"""字典数据查询参数"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
dict_label: str | None = Query(default=None, description="字典标签", max_length=100),
|
||||
dict_type: str | None = Query(default=None, description="字典类型", max_length=100),
|
||||
dict_type_id: int | None = Query(default=None, description="字典类型ID"),
|
||||
status: str | None = Query(default=None, description="状态(0正常 1停用)"),
|
||||
created_time: list[DateTimeStr] | None = Query(default=None, description="创建时间范围", examples=["2025-01-01 00:00:00", "2025-12-31 23:59:59"]),
|
||||
updated_time: list[DateTimeStr] | None = Query(default=None, description="更新时间范围", examples=["2025-01-01 00:00:00", "2025-12-31 23:59:59"]),
|
||||
) -> None:
|
||||
|
||||
# 模糊查询字段
|
||||
self.dict_label = ("like", f"%{dict_label.strip()}%") if dict_label and dict_label.strip() else None
|
||||
|
||||
# 精确查询字段
|
||||
self.dict_type = dict_type.strip() if dict_type else None
|
||||
self.dict_type_id = dict_type_id
|
||||
self.status = status
|
||||
|
||||
# 时间范围查询
|
||||
if created_time and len(created_time) == 2:
|
||||
self.created_time = ("between", (created_time[0], created_time[1]))
|
||||
if updated_time and len(updated_time) == 2:
|
||||
self.updated_time = ("between", (updated_time[0], updated_time[1]))
|
||||
573
后端源码/yifan.action-ai.cn/app/api/v1/module_system/dict/service.py
Normal file
573
后端源码/yifan.action-ai.cn/app/api/v1/module_system/dict/service.py
Normal file
@@ -0,0 +1,573 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import json
|
||||
from redis.asyncio.client import Redis
|
||||
|
||||
from app.common.enums import RedisInitKeyConfig
|
||||
from app.utils.excel_util import ExcelUtil
|
||||
from app.core.database import async_db_session
|
||||
from app.core.base_schema import BatchSetAvailable
|
||||
from app.core.redis_crud import RedisCURD
|
||||
from app.core.exceptions import CustomException
|
||||
from app.core.logger import log
|
||||
from app.api.v1.module_system.auth.schema import AuthSchema
|
||||
from .schema import (
|
||||
DictDataCreateSchema,
|
||||
DictDataOutSchema,
|
||||
DictDataUpdateSchema,
|
||||
DictTypeCreateSchema,
|
||||
DictTypeOutSchema,
|
||||
DictTypeUpdateSchema,
|
||||
DictDataQueryParam,
|
||||
DictTypeQueryParam
|
||||
)
|
||||
from .crud import DictDataCRUD, DictTypeCRUD
|
||||
|
||||
|
||||
class DictTypeService:
|
||||
"""
|
||||
字典类型管理模块服务层
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
async def get_obj_detail_service(cls, auth: AuthSchema, id: int) -> dict:
|
||||
"""
|
||||
获取数据字典类型详情
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- id (int): 数据字典类型ID
|
||||
|
||||
返回:
|
||||
- dict: 数据字典类型详情字典
|
||||
"""
|
||||
obj = await DictTypeCRUD(auth).get_obj_by_id_crud(id=id)
|
||||
return DictTypeOutSchema.model_validate(obj).model_dump()
|
||||
|
||||
@classmethod
|
||||
async def get_obj_list_service(cls, auth: AuthSchema, search: DictTypeQueryParam | None = None, order_by: list[dict] | None = None) -> list[dict]:
|
||||
"""
|
||||
获取数据字典类型列表
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- search (DictTypeQueryParam | None): 搜索条件模型
|
||||
- order_by (list[dict] | None): 排序字段列表
|
||||
|
||||
返回:
|
||||
- list[dict]: 数据字典类型详情字典列表
|
||||
"""
|
||||
obj_list = await DictTypeCRUD(auth).get_obj_list_crud(search=search.__dict__, order_by=order_by)
|
||||
return [DictTypeOutSchema.model_validate(obj).model_dump() for obj in obj_list]
|
||||
|
||||
@classmethod
|
||||
async def create_obj_service(cls, auth: AuthSchema, redis: Redis, data: DictTypeCreateSchema) -> dict:
|
||||
"""
|
||||
创建数据字典类型
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- redis (Redis): Redis客户端
|
||||
- data (DictTypeCreateSchema): 数据字典类型创建模型
|
||||
|
||||
返回:
|
||||
- dict: 数据字典类型详情字典
|
||||
"""
|
||||
exist_obj = await DictTypeCRUD(auth).get(dict_name=data.dict_name)
|
||||
if exist_obj:
|
||||
raise CustomException(msg='创建失败,该数据字典类型已存在')
|
||||
obj = await DictTypeCRUD(auth).create_obj_crud(data=data)
|
||||
|
||||
new_obj_dict = DictTypeOutSchema.model_validate(obj).model_dump()
|
||||
|
||||
redis_key = f"{RedisInitKeyConfig.SYSTEM_DICT.key}:{data.dict_type}"
|
||||
|
||||
try:
|
||||
await RedisCURD(redis).set(
|
||||
key=redis_key,
|
||||
value="",
|
||||
)
|
||||
log.info(f"创建字典类型成功: {new_obj_dict}")
|
||||
except Exception as e:
|
||||
log.error(f"创建字典类型失败: {e}")
|
||||
raise CustomException(msg=f"创建字典类型失败 {e}")
|
||||
|
||||
return new_obj_dict
|
||||
|
||||
@classmethod
|
||||
async def update_obj_service(cls, auth: AuthSchema, redis: Redis, id:int, data: DictTypeUpdateSchema) -> dict:
|
||||
"""
|
||||
更新数据字典类型
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- redis (Redis): Redis客户端
|
||||
- id (int): 数据字典类型ID
|
||||
- data (DictTypeUpdateSchema): 数据字典类型更新模型
|
||||
|
||||
返回:
|
||||
- dict: 数据字典类型详情字典
|
||||
"""
|
||||
exist_obj = await DictTypeCRUD(auth).get_obj_by_id_crud(id=id)
|
||||
if not exist_obj:
|
||||
raise CustomException(msg='更新失败,该数据字典类型不存在')
|
||||
if exist_obj.dict_name != data.dict_name:
|
||||
raise CustomException(msg='更新失败,数据字典类型名称不可以修改')
|
||||
|
||||
|
||||
dict_data_list = []
|
||||
# 如果字典类型修改或状态变更,则修改对应字典数据的类型和状态,并更新Redis缓存
|
||||
if exist_obj.dict_type != data.dict_type or exist_obj.status != data.status:
|
||||
# 检查字典数据类型是否被修改
|
||||
exist_obj_type_list = await DictDataCRUD(auth).list(search={'dict_type': exist_obj.dict_type})
|
||||
if exist_obj_type_list:
|
||||
for item in exist_obj_type_list:
|
||||
item.dict_type = data.dict_type
|
||||
dict_data = DictDataUpdateSchema(
|
||||
dict_sort=item.dict_sort,
|
||||
dict_label=item.dict_label,
|
||||
dict_value=item.dict_value,
|
||||
dict_type=data.dict_type,
|
||||
dict_type_id=item.dict_type_id,
|
||||
css_class=item.css_class,
|
||||
list_class=item.list_class,
|
||||
is_default=item.is_default,
|
||||
status=data.status,
|
||||
description=item.description
|
||||
)
|
||||
obj = await DictDataCRUD(auth).update_obj_crud(id=item.id, data=dict_data)
|
||||
dict_data_list.append(DictDataOutSchema.model_validate(obj).model_dump())
|
||||
|
||||
obj = await DictTypeCRUD(auth).update_obj_crud(id=id, data=data)
|
||||
|
||||
new_obj_dict = DictTypeOutSchema.model_validate(obj).model_dump()
|
||||
|
||||
redis_key = f"{RedisInitKeyConfig.SYSTEM_DICT.key}:{data.dict_type}"
|
||||
try:
|
||||
# 获取当前字典类型的所有字典数据,确保包含最新状态
|
||||
dict_data_list = await DictDataCRUD(auth).get_obj_list_crud(search={'dict_type': data.dict_type})
|
||||
dict_data = [DictDataOutSchema.model_validate(row).model_dump() for row in dict_data_list if row]
|
||||
|
||||
value = json.dumps(dict_data, ensure_ascii=False)
|
||||
await RedisCURD(redis).set(
|
||||
key=redis_key,
|
||||
value=value,
|
||||
)
|
||||
log.info(f"更新字典类型成功并刷新缓存: {new_obj_dict}")
|
||||
except Exception as e:
|
||||
log.error(f"更新字典类型缓存失败: {e}")
|
||||
raise CustomException(msg=f"更新字典类型缓存失败 {e}")
|
||||
|
||||
return new_obj_dict
|
||||
|
||||
@classmethod
|
||||
async def delete_obj_service(cls, auth: AuthSchema, redis: Redis, ids: list[int]) -> None:
|
||||
"""
|
||||
删除数据字典类型
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- redis (Redis): Redis客户端
|
||||
- ids (list[int]): 数据字典类型ID列表
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
if len(ids) < 1:
|
||||
raise CustomException(msg='删除失败,删除对象不能为空')
|
||||
for id in ids:
|
||||
exist_obj = await DictTypeCRUD(auth).get_obj_by_id_crud(id=id)
|
||||
if not exist_obj:
|
||||
raise CustomException(msg='删除失败,该数据字典类型不存在')
|
||||
# 检查是否有字典数据
|
||||
exist_obj_type_list = await DictDataCRUD(auth).list(search={'dict_type': id})
|
||||
if len(exist_obj_type_list) > 0:
|
||||
# 如果有字典数据,不能删除
|
||||
raise CustomException(msg='删除失败,该数据字典类型下存在字典数据')
|
||||
# 删除Redis缓存
|
||||
redis_key = f"{RedisInitKeyConfig.SYSTEM_DICT.key}:{exist_obj.dict_type}"
|
||||
try:
|
||||
await RedisCURD(redis).delete(redis_key)
|
||||
log.info(f"删除字典类型成功: {id}")
|
||||
except Exception as e:
|
||||
log.error(f"删除字典类型失败: {e}")
|
||||
raise CustomException(msg=f"删除字典类型失败")
|
||||
await DictTypeCRUD(auth).delete_obj_crud(ids=ids)
|
||||
|
||||
@classmethod
|
||||
async def set_obj_available_service(cls, auth: AuthSchema, data: BatchSetAvailable) -> None:
|
||||
"""
|
||||
设置数据字典类型状态
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- data (BatchSetAvailable): 批量设置状态模型
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
await DictTypeCRUD(auth).set_obj_available_crud(ids=data.ids, status=data.status)
|
||||
|
||||
@classmethod
|
||||
async def export_obj_service(cls, data_list: list[dict]) -> bytes:
|
||||
"""
|
||||
导出数据字典类型列表
|
||||
|
||||
参数:
|
||||
- data_list (list[dict]): 数据字典类型列表
|
||||
|
||||
返回:
|
||||
- bytes: Excel文件字节流
|
||||
"""
|
||||
mapping_dict = {
|
||||
'id': '编号',
|
||||
'dict_name': '字典名称',
|
||||
'dict_type': '字典类型',
|
||||
'status': '状态',
|
||||
'description': '备注',
|
||||
'created_time': '创建时间',
|
||||
'updated_time': '更新时间',
|
||||
'created_id': '创建者ID',
|
||||
'updated_id': '更新者ID',
|
||||
}
|
||||
|
||||
# 复制数据并转换状态
|
||||
data = data_list.copy()
|
||||
for item in data:
|
||||
# 处理状态
|
||||
item['status'] = '启用' if item.get('status') == '0' else '停用'
|
||||
item['creator'] = item.get('creator', {}).get('name', '未知') if isinstance(item.get('creator'), dict) else '未知'
|
||||
|
||||
return ExcelUtil.export_list2excel(list_data=data, mapping_dict=mapping_dict)
|
||||
|
||||
|
||||
class DictDataService:
|
||||
"""
|
||||
字典数据管理模块服务层
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
async def get_obj_detail_service(cls, auth: AuthSchema, id: int) -> dict:
|
||||
"""
|
||||
获取数据字典数据详情
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- id (int): 数据字典数据ID
|
||||
|
||||
返回:
|
||||
- dict: 数据字典数据详情字典
|
||||
"""
|
||||
obj = await DictDataCRUD(auth).get_obj_by_id_crud(id=id)
|
||||
return DictDataOutSchema.model_validate(obj).model_dump()
|
||||
|
||||
@classmethod
|
||||
async def get_obj_list_service(cls, auth: AuthSchema, search: DictDataQueryParam | None = None, order_by: list[dict] | None = None) -> list[dict]:
|
||||
"""
|
||||
获取数据字典数据列表
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- search (DictDataQueryParam | None): 搜索条件模型
|
||||
- order_by (list[dict] | None): 排序字段列表
|
||||
|
||||
返回:
|
||||
- list[dict]: 数据字典数据详情字典列表
|
||||
"""
|
||||
obj_list = await DictDataCRUD(auth).get_obj_list_crud(search=search.__dict__, order_by=order_by)
|
||||
return [DictDataOutSchema.model_validate(obj).model_dump() for obj in obj_list]
|
||||
|
||||
@classmethod
|
||||
async def init_dict_service(cls, redis: Redis):
|
||||
"""
|
||||
应用初始化: 获取所有字典类型对应的字典数据信息并缓存service
|
||||
|
||||
参数:
|
||||
- redis (Redis): Redis客户端
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
try:
|
||||
async with async_db_session() as session:
|
||||
async with session.begin():
|
||||
# 在初始化过程中,不需要检查数据权限
|
||||
auth = AuthSchema(db=session, check_data_scope=False)
|
||||
obj_list = await DictTypeCRUD(auth).get_obj_list_crud()
|
||||
if not obj_list:
|
||||
log.warning("未找到任何字典类型数据")
|
||||
return
|
||||
|
||||
success_count = 0
|
||||
fail_count = 0
|
||||
|
||||
for obj in obj_list:
|
||||
dict_type = obj.dict_type
|
||||
try:
|
||||
dict_data_list = await DictDataCRUD(auth).get_obj_list_crud(search={'dict_type': dict_type})
|
||||
dict_data = [DictDataOutSchema.model_validate(row).model_dump() for row in dict_data_list if row]
|
||||
# 保存到Redis并设置过期时间
|
||||
redis_key = f"{RedisInitKeyConfig.SYSTEM_DICT.key}:{dict_type}"
|
||||
value = json.dumps(dict_data, ensure_ascii=False)
|
||||
await RedisCURD(redis).set(
|
||||
key=redis_key,
|
||||
value=value,
|
||||
)
|
||||
success_count += 1
|
||||
log.info(f"✅ 字典数据缓存成功: {dict_type}")
|
||||
|
||||
except Exception as e:
|
||||
fail_count += 1
|
||||
log.error(f"❌ 初始化字典数据失败 [{dict_type}]: {e}")
|
||||
# 继续处理其他字典类型,不中断整个初始化过程
|
||||
|
||||
log.info(f"字典数据初始化完成 - 成功: {success_count}, 失败: {fail_count}")
|
||||
|
||||
except Exception as e:
|
||||
log.error(f"字典初始化过程发生错误: {e}")
|
||||
# 只在严重错误时抛出异常,允许单个字典加载失败
|
||||
raise CustomException(msg=f"字典数据初始化失败: {str(e)}")
|
||||
|
||||
@classmethod
|
||||
async def get_init_dict_service(cls, redis: Redis, dict_type: str)->list[dict]:
|
||||
"""
|
||||
从缓存获取字典数据列表信息service
|
||||
|
||||
参数:
|
||||
- redis (Redis): Redis客户端
|
||||
- dict_type (str): 字典类型
|
||||
|
||||
返回:
|
||||
- list[dict]: 字典数据列表
|
||||
"""
|
||||
try:
|
||||
redis_key = f"{RedisInitKeyConfig.SYSTEM_DICT.key}:{dict_type}"
|
||||
obj_list_dict = await RedisCURD(redis).get(redis_key)
|
||||
|
||||
# 确保返回数据正确序列化
|
||||
if obj_list_dict:
|
||||
if isinstance(obj_list_dict, str):
|
||||
try:
|
||||
return json.loads(obj_list_dict)
|
||||
except json.JSONDecodeError:
|
||||
log.warning(f"字典数据反序列化失败,尝试重新初始化缓存: {dict_type}")
|
||||
elif isinstance(obj_list_dict, list):
|
||||
return obj_list_dict
|
||||
|
||||
# 缓存不存在或格式错误时重新初始化
|
||||
await cls.init_dict_service(redis)
|
||||
obj_list_dict = await RedisCURD(redis).get(redis_key)
|
||||
if not obj_list_dict:
|
||||
raise CustomException(msg="数据字典不存在")
|
||||
|
||||
# 再次确保返回数据正确序列化
|
||||
if isinstance(obj_list_dict, str):
|
||||
try:
|
||||
return json.loads(obj_list_dict)
|
||||
except json.JSONDecodeError:
|
||||
raise CustomException(msg="字典数据格式错误")
|
||||
return obj_list_dict
|
||||
except CustomException:
|
||||
raise
|
||||
except Exception as e:
|
||||
log.error(f"获取字典缓存失败: {str(e)}")
|
||||
raise CustomException(msg=f"获取字典数据失败: {str(e)}")
|
||||
|
||||
@classmethod
|
||||
async def create_obj_service(cls, auth: AuthSchema, redis: Redis, data: DictDataCreateSchema) -> dict:
|
||||
"""
|
||||
创建数据字典数据
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- redis (Redis): Redis客户端
|
||||
- data (DictDataCreateSchema): 数据字典数据创建模型
|
||||
|
||||
返回:
|
||||
- dict: 数据字典数据详情字典
|
||||
"""
|
||||
exist_obj = await DictDataCRUD(auth).get(dict_label=data.dict_label)
|
||||
if exist_obj:
|
||||
raise CustomException(msg='创建失败,该字典数据已存在')
|
||||
obj = await DictDataCRUD(auth).create_obj_crud(data=data)
|
||||
|
||||
redis_key = f"{RedisInitKeyConfig.SYSTEM_DICT.key}:{data.dict_type}"
|
||||
try:
|
||||
# 获取当前字典类型的所有字典数据
|
||||
dict_data_list = await DictDataCRUD(auth).get_obj_list_crud(search={'dict_type': data.dict_type})
|
||||
dict_data = [DictDataOutSchema.model_validate(row).model_dump() for row in dict_data_list if row]
|
||||
|
||||
value = json.dumps(dict_data, ensure_ascii=False)
|
||||
await RedisCURD(redis).set(
|
||||
key=redis_key,
|
||||
value=value,
|
||||
)
|
||||
log.info(f"创建字典数据写入缓存成功: {obj}")
|
||||
except Exception as e:
|
||||
log.error(f"创建字典数据写入缓存失败: {e}")
|
||||
raise CustomException(msg=f"创建字典数据失败 {e}")
|
||||
|
||||
return DictDataOutSchema.model_validate(obj).model_dump()
|
||||
|
||||
@classmethod
|
||||
async def update_obj_service(cls, auth: AuthSchema, redis: Redis, id:int, data: DictDataUpdateSchema) -> dict:
|
||||
"""
|
||||
更新数据字典数据
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- redis (Redis): Redis客户端
|
||||
- id (int): 数据字典数据ID
|
||||
- data (DictDataUpdateSchema): 数据字典数据更新模型
|
||||
|
||||
返回:
|
||||
- Dict: 数据字典数据详情字典
|
||||
"""
|
||||
exist_obj = await DictDataCRUD(auth).get_obj_by_id_crud(id=id)
|
||||
if not exist_obj:
|
||||
raise CustomException(msg='更新失败,该字典数据不存在')
|
||||
|
||||
if exist_obj.id != id:
|
||||
raise CustomException(msg='更新失败,数据字典数据重复')
|
||||
|
||||
# 如果字典类型变更,仅刷新旧类型缓存,不联动字典类型状态
|
||||
if exist_obj.dict_type != data.dict_type:
|
||||
dict_type = await DictTypeCRUD(auth).get(dict_type=exist_obj.dict_type)
|
||||
if dict_type:
|
||||
redis_key = f"{RedisInitKeyConfig.SYSTEM_DICT.key}:{dict_type.dict_type}"
|
||||
try:
|
||||
dict_data_list = await DictDataCRUD(auth).get_obj_list_crud(search={'dict_type': dict_type.dict_type})
|
||||
dict_data = [DictDataOutSchema.model_validate(row).model_dump() for row in dict_data_list if row]
|
||||
value = json.dumps(dict_data, ensure_ascii=False)
|
||||
await RedisCURD(redis).set(
|
||||
key=redis_key,
|
||||
value=value,
|
||||
)
|
||||
except Exception as e:
|
||||
log.error(f"更新字典数据类型变更时刷新旧缓存失败: {e}")
|
||||
|
||||
obj = await DictDataCRUD(auth).update_obj_crud(id=id, data=data)
|
||||
redis_key = f"{RedisInitKeyConfig.SYSTEM_DICT.key}:{data.dict_type}"
|
||||
try:
|
||||
# 获取当前字典类型的所有字典数据
|
||||
dict_data_list = await DictDataCRUD(auth).get_obj_list_crud(search={'dict_type': data.dict_type})
|
||||
dict_data = [DictDataOutSchema.model_validate(row).model_dump() for row in dict_data_list if row]
|
||||
|
||||
value = json.dumps(dict_data, ensure_ascii=False)
|
||||
await RedisCURD(redis).set(
|
||||
key=redis_key,
|
||||
value=value,
|
||||
)
|
||||
log.info(f"更新字典数据写入缓存成功: {obj}")
|
||||
except Exception as e:
|
||||
log.error(f"更新字典数据写入缓存失败: {e}")
|
||||
raise CustomException(msg=f"更新字典数据失败 {e}")
|
||||
|
||||
return DictDataOutSchema.model_validate(obj).model_dump()
|
||||
|
||||
@classmethod
|
||||
async def delete_obj_service(cls, auth: AuthSchema, redis: Redis, ids: list[int]) -> None:
|
||||
"""
|
||||
删除数据字典数据
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- redis (Redis): Redis客户端
|
||||
- ids (list[int]): 数据字典数据ID列表
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
try:
|
||||
if len(ids) < 1:
|
||||
raise CustomException(msg='删除失败,删除对象不能为空')
|
||||
|
||||
# 首先检查是否包含系统默认数据
|
||||
for id in ids:
|
||||
exist_obj = await DictDataCRUD(auth).get_obj_by_id_crud(id=id)
|
||||
if not exist_obj:
|
||||
raise CustomException(msg=f'{id} 删除失败,该字典数据不存在')
|
||||
# 系统默认字典数据不允许删除
|
||||
if exist_obj.is_default:
|
||||
raise CustomException(msg=f'删除失败,ID为{id}的系统默认字典数据不允许删除')
|
||||
|
||||
# 获取所有需要清除的缓存键
|
||||
dict_types_to_clear = set()
|
||||
for id in ids:
|
||||
exist_obj = await DictDataCRUD(auth).get_obj_by_id_crud(id=id)
|
||||
if exist_obj:
|
||||
dict_types_to_clear.add(exist_obj.dict_type)
|
||||
|
||||
# 执行删除操作
|
||||
await DictDataCRUD(auth).delete_obj_crud(ids=ids)
|
||||
|
||||
# 清除缓存
|
||||
for dict_type in dict_types_to_clear:
|
||||
try:
|
||||
redis_key = f"{RedisInitKeyConfig.SYSTEM_DICT.key}:{dict_type}"
|
||||
await RedisCURD(redis).delete(redis_key)
|
||||
log.info(f"清除字典缓存成功: {dict_type}")
|
||||
except Exception as e:
|
||||
log.warning(f"清除字典缓存失败: {e}")
|
||||
# 缓存清除失败不影响删除操作
|
||||
|
||||
log.info(f"删除字典数据成功,ID列表: {ids}")
|
||||
|
||||
except CustomException:
|
||||
raise
|
||||
except Exception as e:
|
||||
log.error(f"删除字典数据失败: {str(e)}")
|
||||
raise CustomException(msg=f"删除字典数据失败: {str(e)}")
|
||||
|
||||
@classmethod
|
||||
async def set_obj_available_service(cls, auth: AuthSchema, data: BatchSetAvailable) -> None:
|
||||
"""
|
||||
批量修改数据字典数据状态
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- data (BatchSetAvailable): 批量修改数据字典数据状态负载模型
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
await DictDataCRUD(auth).set_obj_available_crud(ids=data.ids, status=data.status)
|
||||
|
||||
@classmethod
|
||||
async def export_obj_service(cls, data_list: list[dict]) -> bytes:
|
||||
"""
|
||||
导出数据字典数据列表
|
||||
|
||||
参数:
|
||||
- data_list (list[dict]): 数据字典数据列表
|
||||
|
||||
返回:
|
||||
- bytes: Excel文件字节流
|
||||
"""
|
||||
mapping_dict = {
|
||||
'id': '编号',
|
||||
'dict_sort': '字典排序',
|
||||
'dict_label': '字典标签',
|
||||
'dict_value': '字典键值',
|
||||
'dict_type': '字典类型',
|
||||
'css_class': '样式属性',
|
||||
'list_class': '表格回显样式',
|
||||
'is_default': '是否默认',
|
||||
'status': '状态',
|
||||
'description': '备注',
|
||||
'created_time': '创建时间',
|
||||
'updated_time': '更新时间',
|
||||
'created_id': '创建者ID',
|
||||
'updated_id': '更新者ID',
|
||||
}
|
||||
|
||||
# 复制数据并转换状态
|
||||
data = data_list.copy()
|
||||
for item in data:
|
||||
# 处理状态
|
||||
item['status'] = '启用' if item.get('status') == '0' else '停用'
|
||||
# 处理是否默认
|
||||
item['is_default'] = '是' if item.get('is_default') else '否'
|
||||
item['creator'] = item.get('creator', {}).get('name', '未知') if isinstance(item.get('creator'), dict) else '未知'
|
||||
|
||||
return ExcelUtil.export_list2excel(list_data=data, mapping_dict=mapping_dict)
|
||||
Reference in New Issue
Block a user