upload project source code
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
@@ -0,0 +1,180 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from fastapi import APIRouter, Body, Depends, Path
|
||||
from fastapi.responses import JSONResponse, StreamingResponse
|
||||
|
||||
from app.common.response import StreamResponse, SuccessResponse
|
||||
from app.common.request import PaginationService
|
||||
from app.core.router_class import OperationLogRoute
|
||||
from app.utils.common_util import bytes2file_response
|
||||
from app.core.base_params import PaginationQueryParam
|
||||
from app.core.dependencies import AuthPermission
|
||||
from app.core.base_schema import BatchSetAvailable
|
||||
from app.core.logger import log
|
||||
|
||||
from ..auth.schema import AuthSchema
|
||||
from .service import PositionService
|
||||
from .schema import (
|
||||
PositionCreateSchema,
|
||||
PositionUpdateSchema,
|
||||
PositionQueryParam
|
||||
)
|
||||
|
||||
|
||||
PositionRouter = APIRouter(route_class=OperationLogRoute, prefix="/position", tags=["岗位管理"])
|
||||
|
||||
|
||||
@PositionRouter.get("/list", summary="查询岗位", description="查询岗位")
|
||||
async def get_obj_list_controller(
|
||||
page: PaginationQueryParam = Depends(),
|
||||
search: PositionQueryParam = Depends(),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:position:query"])),
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
查询岗位列表
|
||||
|
||||
参数:
|
||||
- page (PaginationQueryParam): 分页查询参数
|
||||
- search (PositionQueryParam): 查询参数
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 分页查询结果
|
||||
"""
|
||||
order_by = [{"order": "asc"}]
|
||||
if page.order_by:
|
||||
order_by = page.order_by
|
||||
result_dict_list = await PositionService.get_position_list_service(search=search, auth=auth, 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="查询岗位列表成功")
|
||||
|
||||
|
||||
@PositionRouter.get("/detail/{id}", summary="查询岗位详情", description="查询岗位详情")
|
||||
async def get_obj_detail_controller(
|
||||
id: int = Path(..., description="岗位ID"),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:position:query"])),
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
查询岗位详情
|
||||
|
||||
参数:
|
||||
- id (int): 岗位ID
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 岗位详情对象
|
||||
"""
|
||||
result_dict = await PositionService.get_position_detail_service(id=id, auth=auth)
|
||||
log.info(f"查询岗位详情成功 {id}")
|
||||
return SuccessResponse(data=result_dict, msg="获取岗位详情成功")
|
||||
|
||||
|
||||
@PositionRouter.post("/create", summary="创建岗位", description="创建岗位")
|
||||
async def create_obj_controller(
|
||||
data: PositionCreateSchema,
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:position:create"])),
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
创建岗位
|
||||
|
||||
参数:
|
||||
- data (PositionCreateSchema): 创建岗位模型
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 岗位详情对象
|
||||
"""
|
||||
result_dict = await PositionService.create_position_service(data=data, auth=auth)
|
||||
log.info(f"创建岗位成功: {result_dict}")
|
||||
return SuccessResponse(data=result_dict, msg="创建岗位成功")
|
||||
|
||||
|
||||
@PositionRouter.put("/update/{id}", summary="修改岗位", description="修改岗位")
|
||||
async def update_obj_controller(
|
||||
data: PositionUpdateSchema,
|
||||
id: int = Path(..., description="岗位ID"),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:position:update"])),
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
修改岗位
|
||||
|
||||
参数:
|
||||
- data (PositionUpdateSchema): 修改岗位模型
|
||||
- id (int): 岗位ID
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 岗位详情对象
|
||||
"""
|
||||
result_dict = await PositionService.update_position_service(id=id, data=data, auth=auth)
|
||||
log.info(f"修改岗位成功: {result_dict}")
|
||||
return SuccessResponse(data=result_dict, msg="修改岗位成功")
|
||||
|
||||
|
||||
@PositionRouter.delete("/delete", summary="删除岗位", description="删除岗位")
|
||||
async def delete_obj_controller(
|
||||
ids: list[int] = Body(..., description="ID列表"),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:position:delete"])),
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
删除岗位
|
||||
|
||||
参数:
|
||||
- ids (list[int]): ID列表
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 成功消息
|
||||
"""
|
||||
await PositionService.delete_position_service(ids=ids, auth=auth)
|
||||
log.info(f"删除岗位成功: {ids}")
|
||||
return SuccessResponse(msg="删除岗位成功")
|
||||
|
||||
|
||||
@PositionRouter.patch("/available/setting", summary="批量修改岗位状态", description="批量修改岗位状态")
|
||||
async def batch_set_available_obj_controller(
|
||||
data: BatchSetAvailable,
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:position:patch"])),
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
批量修改岗位状态
|
||||
|
||||
参数:
|
||||
- data (BatchSetAvailable): 批量修改岗位状态模型
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 成功消息
|
||||
"""
|
||||
await PositionService.set_position_available_service(data=data, auth=auth)
|
||||
log.info(f"批量修改岗位状态成功: {data.ids}")
|
||||
return SuccessResponse(msg="批量修改岗位状态成功")
|
||||
|
||||
|
||||
@PositionRouter.post('/export', summary="导出岗位", description="导出岗位")
|
||||
async def export_obj_list_controller(
|
||||
search: PositionQueryParam = Depends(),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:position:export"])),
|
||||
) -> StreamingResponse:
|
||||
"""
|
||||
导出岗位
|
||||
|
||||
参数:
|
||||
- search (PositionQueryParam): 查询参数
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- StreamingResponse: 岗位Excel文件流
|
||||
"""
|
||||
position_query_result = await PositionService.get_position_list_service(search=search, auth=auth)
|
||||
position_export_result = await PositionService.export_position_list_service(position_list=position_query_result)
|
||||
log.info('导出岗位成功')
|
||||
|
||||
return StreamResponse(
|
||||
data=bytes2file_response(position_export_result),
|
||||
media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
headers = {
|
||||
'Content-Disposition': 'attachment; filename=position.xlsx'
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,79 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import Sequence, Any
|
||||
|
||||
from app.core.base_crud import CRUDBase
|
||||
from ..auth.schema import AuthSchema
|
||||
from .model import PositionModel
|
||||
from .schema import PositionCreateSchema, PositionUpdateSchema
|
||||
|
||||
|
||||
class PositionCRUD(CRUDBase[PositionModel, PositionCreateSchema, PositionUpdateSchema]):
|
||||
"""岗位模块数据层"""
|
||||
|
||||
def __init__(self, auth: AuthSchema) -> None:
|
||||
"""
|
||||
初始化岗位CRUD
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
"""
|
||||
self.auth = auth
|
||||
super().__init__(model=PositionModel, auth=auth)
|
||||
|
||||
async def get_by_id_crud(self, id: int, preload: list[str] | None = None) -> PositionModel | None:
|
||||
"""
|
||||
根据 id 获取岗位信息。
|
||||
|
||||
参数:
|
||||
- id (int): 岗位 ID。
|
||||
- preload (list[str] | None): 预加载关系,未提供时使用模型默认项
|
||||
|
||||
返回:
|
||||
- PositionModel | None: 岗位信息,未找到返回 None。
|
||||
"""
|
||||
return await self.get(id=id, preload=preload)
|
||||
|
||||
async def get_list_crud(self, search: dict | None = None, order_by: list[dict[str, Any]] | None = None, preload: list[str] | None = None) -> Sequence[PositionModel]:
|
||||
"""
|
||||
获取岗位列表。
|
||||
|
||||
参数:
|
||||
- search (dict | None): 搜索条件。
|
||||
- order_by (list[dict[str, Any]] | None): 排序字段列表。
|
||||
- preload (list[str] | None): 预加载关系,未提供时使用模型默认项
|
||||
|
||||
返回:
|
||||
- Sequence[PositionModel]: 岗位列表。
|
||||
"""
|
||||
return await self.list(search=search, order_by=order_by, preload=preload)
|
||||
|
||||
async def set_available_crud(self, ids: list[int], status: str) -> None:
|
||||
"""
|
||||
批量设置岗位可用状态。
|
||||
|
||||
参数:
|
||||
- ids (list[int]): 岗位 ID 列表。
|
||||
- status (bool): 可用状态。
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
await self.set(ids=ids, status=status)
|
||||
|
||||
async def get_name_crud(self, ids: list[int]) -> list[str]:
|
||||
"""
|
||||
根据 id 列表获取岗位名称。
|
||||
|
||||
参数:
|
||||
- ids (list[int]): 岗位 ID 列表。
|
||||
|
||||
返回:
|
||||
- list[str]: 岗位名称列表。
|
||||
"""
|
||||
position_names = []
|
||||
for id in ids:
|
||||
obj = await self.get(id=id)
|
||||
if obj:
|
||||
position_names.append(obj.name)
|
||||
return position_names
|
||||
@@ -0,0 +1,30 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
from sqlalchemy import String, Integer
|
||||
from sqlalchemy.orm import relationship, Mapped, mapped_column
|
||||
|
||||
from app.core.base_model import ModelMixin, UserMixin
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.api.v1.module_system.user.model import UserModel
|
||||
|
||||
|
||||
class PositionModel(ModelMixin, UserMixin):
|
||||
"""
|
||||
岗位模型
|
||||
"""
|
||||
__tablename__: str = "sys_position"
|
||||
__table_args__: dict[str, str] = ({'comment': '岗位表'})
|
||||
__loader_options__: list[str] = ["users", "created_by", "updated_by"]
|
||||
|
||||
name: Mapped[str] = mapped_column(String(40), nullable=False, comment="岗位名称")
|
||||
order: Mapped[int] = mapped_column(Integer, nullable=False, default=1, comment="显示排序")
|
||||
|
||||
# 关联关系
|
||||
users: Mapped[list["UserModel"]] = relationship(
|
||||
secondary="sys_user_positions",
|
||||
back_populates="positions",
|
||||
lazy="selectin"
|
||||
)
|
||||
@@ -0,0 +1,64 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
from fastapi import Query
|
||||
|
||||
from app.core.validator import DateTimeStr
|
||||
from app.core.base_schema import BaseSchema, UserBySchema
|
||||
|
||||
|
||||
class PositionCreateSchema(BaseModel):
|
||||
"""岗位创建模型"""
|
||||
name: str = Field(..., max_length=64, description="岗位名称")
|
||||
order: int = Field(default=1, ge=1, description='显示排序')
|
||||
status: str = Field(default="0", description="是否启用(0:启用 1:禁用)")
|
||||
description: str | None = Field(default=None, max_length=255, description="描述")
|
||||
|
||||
@field_validator('name')
|
||||
@classmethod
|
||||
def _validate_name(cls, v: str) -> str:
|
||||
v = v.strip()
|
||||
if not v:
|
||||
raise ValueError('岗位名称不能为空')
|
||||
return v
|
||||
|
||||
|
||||
class PositionUpdateSchema(PositionCreateSchema):
|
||||
"""岗位更新模型"""
|
||||
...
|
||||
|
||||
|
||||
class PositionOutSchema(PositionCreateSchema, BaseSchema, UserBySchema):
|
||||
"""岗位信息响应模型"""
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
...
|
||||
|
||||
|
||||
class PositionQueryParam:
|
||||
"""岗位管理查询参数"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: Optional[str] = Query(None, description="岗位名称"),
|
||||
status: Optional[str] = Query(None, description="是否可用"),
|
||||
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"]),
|
||||
created_id: int | None = Query(None, description="创建人"),
|
||||
updated_id: int | None = Query(None, description="更新人"),
|
||||
) -> None:
|
||||
|
||||
# 模糊查询字段
|
||||
self.name = ("like", name)
|
||||
|
||||
# 精确查询字段
|
||||
self.created_id = created_id
|
||||
self.updated_id = updated_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]))
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from app.core.base_schema import BatchSetAvailable
|
||||
from app.core.exceptions import CustomException
|
||||
from app.utils.excel_util import ExcelUtil
|
||||
|
||||
from ..auth.schema import AuthSchema
|
||||
from .crud import PositionCRUD
|
||||
from .schema import (
|
||||
PositionCreateSchema,
|
||||
PositionUpdateSchema,
|
||||
PositionOutSchema,
|
||||
PositionQueryParam
|
||||
)
|
||||
|
||||
|
||||
class PositionService:
|
||||
"""岗位模块服务层"""
|
||||
|
||||
@classmethod
|
||||
async def get_position_detail_service(cls, auth: AuthSchema, id: int) -> dict:
|
||||
"""
|
||||
获取岗位详情
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- id (int): 岗位ID
|
||||
|
||||
返回:
|
||||
- Dict: 岗位详情对象
|
||||
"""
|
||||
position = await PositionCRUD(auth).get_by_id_crud(id=id)
|
||||
return PositionOutSchema.model_validate(position).model_dump()
|
||||
|
||||
@classmethod
|
||||
async def get_position_list_service(cls, auth: AuthSchema, search: PositionQueryParam | None = None, order_by: list[dict] | None = None) -> list[dict]:
|
||||
"""
|
||||
获取岗位列表
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- search (PositionQueryParam | None): 查询参数对象
|
||||
- order_by (list[dict] | None): 排序参数列表
|
||||
|
||||
返回:
|
||||
- list[dict]: 岗位列表对象
|
||||
"""
|
||||
position_list = await PositionCRUD(auth).get_list_crud(search=search.__dict__, order_by=order_by)
|
||||
return [PositionOutSchema.model_validate(position).model_dump() for position in position_list]
|
||||
|
||||
@classmethod
|
||||
async def create_position_service(cls, auth: AuthSchema, data: PositionCreateSchema) -> dict:
|
||||
"""
|
||||
创建岗位
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- data (PositionCreateSchema): 岗位创建模型
|
||||
|
||||
返回:
|
||||
- Dict: 创建的岗位对象
|
||||
"""
|
||||
position = await PositionCRUD(auth).get(name=data.name)
|
||||
if position:
|
||||
raise CustomException(msg='创建失败,该岗位已存在')
|
||||
new_position = await PositionCRUD(auth).create(data=data)
|
||||
return PositionOutSchema.model_validate(new_position).model_dump()
|
||||
|
||||
@classmethod
|
||||
async def update_position_service(cls, auth: AuthSchema, id:int, data: PositionUpdateSchema) -> dict:
|
||||
"""
|
||||
更新岗位
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- id (int): 岗位ID
|
||||
- data (PositionUpdateSchema): 岗位更新模型
|
||||
|
||||
返回:
|
||||
- dict: 更新的岗位对象
|
||||
"""
|
||||
position = await PositionCRUD(auth).get_by_id_crud(id=id)
|
||||
if not position:
|
||||
raise CustomException(msg='更新失败,该岗位不存在')
|
||||
exist_position = await PositionCRUD(auth).get(name=data.name)
|
||||
if exist_position and exist_position.id != id:
|
||||
raise CustomException(msg='更新失败,岗位名称重复')
|
||||
updated_position = await PositionCRUD(auth).update(id=id, data=data)
|
||||
return PositionOutSchema.model_validate(updated_position).model_dump()
|
||||
|
||||
@classmethod
|
||||
async def delete_position_service(cls, auth: AuthSchema, ids: list[int]) -> None:
|
||||
"""
|
||||
删除岗位
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- ids (list[int]): 岗位ID列表
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
if len(ids) < 1:
|
||||
raise CustomException(msg='删除失败,删除对象不能为空')
|
||||
for id in ids:
|
||||
position = await PositionCRUD(auth).get_by_id_crud(id=id)
|
||||
if not position:
|
||||
raise CustomException(msg='删除失败,该岗位不存在')
|
||||
await PositionCRUD(auth).delete(ids=ids)
|
||||
|
||||
@classmethod
|
||||
async def set_position_available_service(cls, auth: AuthSchema, data: BatchSetAvailable) -> None:
|
||||
"""
|
||||
设置岗位状态
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- data (BatchSetAvailable): 批量设置状态模型
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
await PositionCRUD(auth).set_available_crud(ids=data.ids, status=data.status)
|
||||
|
||||
@classmethod
|
||||
async def export_position_list_service(cls, position_list: list[dict]) -> bytes:
|
||||
"""
|
||||
导出岗位列表
|
||||
|
||||
参数:
|
||||
- position_list (list[dict]): 岗位列表对象
|
||||
|
||||
返回:
|
||||
- bytes: 导出的Excel文件字节流
|
||||
"""
|
||||
mapping_dict = {
|
||||
'id': '编号',
|
||||
'name': '岗位名称',
|
||||
'order': '显示顺序',
|
||||
'status': '状态',
|
||||
'description': '备注',
|
||||
'created_time': '创建时间',
|
||||
'updated_time': '更新时间',
|
||||
'created_id': '创建者ID',
|
||||
'updated_id': '更新者ID',
|
||||
}
|
||||
|
||||
# 复制数据并转换状态
|
||||
data = position_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)
|
||||
Reference in New Issue
Block a user