You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.6 KiB
Python
93 lines
2.6 KiB
Python
from apiflask import APIBlueprint
|
|
from flask_jwt_extended import jwt_required
|
|
from iti.applications.common.exceptions.biz_exp import BizException
|
|
from iti.applications.extensions import db
|
|
from iti.applications.models import SysDept, SysDeptSchema, sys_user_dept
|
|
from .schemas.dept import SysDeptQuery, SysDeptCreateRequest, SysDeptUpdateRequest
|
|
from iti.applications.service.sys_dept import get_dept_tree_or_page
|
|
from iti.applications.common.utils import page, page_schema, success
|
|
from iti.applications.common import permission
|
|
from sqlalchemy import select, delete
|
|
|
|
bp = APIBlueprint("sys_dept", __name__, url_prefix="/dept", tag="系统.部门管理")
|
|
|
|
|
|
@bp.get("/list")
|
|
@jwt_required()
|
|
@permission("system:dept:list")
|
|
@bp.input(SysDeptQuery.Schema, location="query")
|
|
@bp.output(SysDeptSchema(many=True, exclude=["parent"]))
|
|
def list_dept(query_data: SysDeptQuery):
|
|
"""
|
|
获取部门列表
|
|
"""
|
|
|
|
return success(get_dept_tree_or_page(query_data, with_leader=True))
|
|
|
|
|
|
@bp.get("/page")
|
|
@jwt_required()
|
|
@permission("system:dept:list")
|
|
@bp.input(SysDeptQuery.Schema, location="query")
|
|
@bp.output(page_schema(SysDeptSchema(many=True, exclude=["parent"])))
|
|
def page_dept(query_data: SysDeptQuery):
|
|
"""
|
|
获取部门分页列表
|
|
"""
|
|
|
|
return page(get_dept_tree_or_page(query_data))
|
|
|
|
|
|
@bp.post("")
|
|
@jwt_required()
|
|
@permission("system:dept:create")
|
|
@bp.input(SysDeptCreateRequest, location="json")
|
|
def create_dept(json_data: dict):
|
|
"""
|
|
创建部门
|
|
"""
|
|
dept = SysDept(**json_data)
|
|
db.session.add(dept)
|
|
db.session.commit()
|
|
return success()
|
|
|
|
|
|
@bp.put("/<string:id>")
|
|
@jwt_required()
|
|
@permission("system:dept:edit")
|
|
@bp.input(SysDeptUpdateRequest(partial=True), location="json")
|
|
def update_dept(id: str, json_data: dict):
|
|
"""
|
|
更新部门
|
|
"""
|
|
dept = db.session.scalar(select(SysDept).filter_by(id=id))
|
|
if not dept:
|
|
raise BizException("部门不存在")
|
|
for key, value in json_data.items():
|
|
if value is not None:
|
|
setattr(dept, key, value)
|
|
db.session.commit()
|
|
return success()
|
|
|
|
|
|
@bp.delete("/<string:id>")
|
|
@jwt_required()
|
|
@permission("system:dept:delete")
|
|
def delete_dept(id: str):
|
|
"""
|
|
删除部门
|
|
"""
|
|
dept = db.session.scalar(select(SysDept).filter_by(id=id))
|
|
if not dept:
|
|
raise BizException("部门不存在")
|
|
try:
|
|
# 删除关联关系
|
|
db.session.execute(delete(sys_user_dept).filter_by(dept_id=id))
|
|
# 删除部门
|
|
db.session.delete(dept)
|
|
db.session.commit()
|
|
except Exception as e:
|
|
db.session.rollback()
|
|
raise BizException(f"删除部门失败: {str(e)}")
|
|
return success()
|