129 lines
4.0 KiB
Python
129 lines
4.0 KiB
Python
"""
|
|
backend/app/services/admin/department_service.py
|
|
|
|
用途:管理端部门 CRUD 业务编排。
|
|
依赖:models.Department、app.core.errors。
|
|
逻辑:由 api/admin/departments.py 调用。
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from sqlalchemy import func
|
|
|
|
from app import db
|
|
from app.core.errors import NotFoundError, ValidationError
|
|
from app.models import Department, User
|
|
|
|
|
|
def serialize_department(dept: Department) -> dict:
|
|
"""部门 ORM → API dict。"""
|
|
return {
|
|
'id': dept.id,
|
|
'name': dept.name,
|
|
'code': dept.code,
|
|
'company': dept.company,
|
|
'description': dept.description,
|
|
}
|
|
|
|
|
|
def list_departments() -> list[dict]:
|
|
"""返回全部部门(按 name 升序)。"""
|
|
rows = (
|
|
db.session.query(Department, func.count(User.id).label('member_count'))
|
|
.outerjoin(User, User.department == Department.name)
|
|
.group_by(Department.id)
|
|
.order_by(Department.name.asc())
|
|
.all()
|
|
)
|
|
return [{**serialize_department(dept), 'member_count': int(member_count or 0)} for dept, member_count in rows]
|
|
|
|
|
|
def list_department_members(dept_id: int) -> dict:
|
|
"""
|
|
查询指定部门的员工名单。
|
|
|
|
[STEP 1] 通过部门主键读取部门名称。
|
|
[STEP 2] 复用现有 users.department 文本关联查询成员。
|
|
[STEP 3] 返回人数与脱敏后的管理端成员资料。
|
|
"""
|
|
dept = db.session.get(Department, dept_id)
|
|
if not dept:
|
|
raise NotFoundError('Not found')
|
|
users = db.session.query(User).filter(User.department == dept.name).order_by(User.real_name.asc(), User.username.asc()).all()
|
|
return {
|
|
'department': serialize_department(dept),
|
|
'count': len(users),
|
|
'members': [
|
|
{
|
|
'id': user.id,
|
|
'username': user.username,
|
|
'real_name': user.real_name,
|
|
'employee_id': user.employee_id,
|
|
'email': user.email,
|
|
'status': user.status,
|
|
}
|
|
for user in users
|
|
],
|
|
}
|
|
|
|
|
|
def create_department(data: dict[str, Any]) -> dict:
|
|
"""
|
|
创建部门。
|
|
Returns:
|
|
{ id, name }。
|
|
Raises:
|
|
ValidationError: name 缺失或 name/code 重复。
|
|
"""
|
|
if not data.get('name'):
|
|
raise ValidationError('Name is required')
|
|
if db.session.query(Department).filter_by(name=data['name']).first():
|
|
raise ValidationError('Name exists')
|
|
if data.get('code') and db.session.query(Department).filter_by(code=data['code']).first():
|
|
raise ValidationError('Code exists')
|
|
dept = Department(
|
|
name=data['name'],
|
|
code=data.get('code'),
|
|
company=data.get('company'),
|
|
description=data.get('description'),
|
|
)
|
|
db.session.add(dept)
|
|
db.session.commit()
|
|
return {'id': dept.id, 'name': dept.name}
|
|
|
|
|
|
def update_department(dept_id: int, data: dict[str, Any]) -> None:
|
|
"""
|
|
更新部门字段。
|
|
Raises:
|
|
NotFoundError: 部门不存在。
|
|
ValidationError: name/code 与其它记录冲突。
|
|
"""
|
|
dept = db.session.get(Department, dept_id)
|
|
if not dept:
|
|
raise NotFoundError('Not found')
|
|
if 'name' in data:
|
|
if db.session.query(Department).filter(Department.name == data['name'], Department.id != dept_id).first():
|
|
raise ValidationError('Name exists')
|
|
dept.name = data['name']
|
|
if 'code' in data:
|
|
if data['code'] and db.session.query(Department).filter(
|
|
Department.code == data['code'], Department.id != dept_id
|
|
).first():
|
|
raise ValidationError('Code exists')
|
|
dept.code = data['code']
|
|
if 'company' in data:
|
|
dept.company = data['company']
|
|
if 'description' in data:
|
|
dept.description = data['description']
|
|
db.session.commit()
|
|
|
|
|
|
def delete_department(dept_id: int) -> None:
|
|
"""删除部门(不存在时静默成功)。"""
|
|
dept = db.session.get(Department, dept_id)
|
|
if dept:
|
|
db.session.delete(dept)
|
|
db.session.commit()
|