from flask import Blueprint, request, jsonify from app.models import Department, User, db from sqlalchemy.exc import IntegrityError bp = Blueprint('departments', __name__) @bp.route('', methods=['POST']) def create_department(): data = request.get_json() if not data or 'name' not in data: return jsonify({'message': '部门名称是必需的'}), 400 name = data['name'] # 检查部门名称是否已存在 if Department.query.filter_by(name=name).first(): return jsonify({'message': '部门名称已存在'}), 400 new_department = Department(name=name) db.session.add(new_department) db.session.flush() # 这会给 new_department 分配一个 id # 如果提供了其他字段,设置它们 for field in ['department_code', 'manager', 'phone', 'email', 'location']: if field in data: new_department.set_detail(field, data[field]) try: db.session.commit() except IntegrityError: db.session.rollback() return jsonify({'message': '创建部门失败,可能是由于数据完整性问题'}), 500 return jsonify({'message': '部门创建成功', 'id': new_department.id}), 201 @bp.route('//children', methods=['POST']) def create_child_department(parent_id): parent_department = Department.query.get(parent_id) if not parent_department: return jsonify({'message': '父部门不存在'}), 404 data = request.get_json() if not data or 'name' not in data: return jsonify({'message': '子部门名称是必需的'}), 400 name = data['name'] # 检查部门名称是否已存在 if Department.query.filter_by(name=name).first(): return jsonify({'message': '部门名称已存在'}), 400 new_department = Department(name=name, parent_id=parent_id) db.session.add(new_department) db.session.flush() # 这会给 new_department 分配一个 id # 如果提供了其他字段,设置它们 for field in ['department_code', 'manager', 'phone', 'email', 'location']: if field in data: new_department.set_detail(field, data[field]) try: db.session.commit() except IntegrityError: db.session.rollback() return jsonify({'message': '创建子部门失败,可能是由于数据完整性问题'}), 500 return jsonify({'message': '子部门创建成功', 'id': new_department.id}), 201 @bp.route('//add_user', methods=['POST']) def add_user_to_department(department_id): data = request.get_json() if not data or 'user_id' not in data: return jsonify({'message': '用户ID是必需的'}), 400 user_id = data['user_id'] department = Department.query.get(department_id) if not department: return jsonify({'message': '部门不存在'}), 404 user = User.query.get(user_id) if not user: return jsonify({'message': '用户不存在'}), 404 if 'is_primary' in data and data['is_primary']: # 如果是主要部门,直接设置 user.primary_department = department else: # 如果不是主要部门,添加到次要部门 if department not in user.secondary_departments: user.secondary_departments.append(department) try: db.session.commit() return jsonify({'message': '用户成功添加到部门'}), 200 except IntegrityError: db.session.rollback() return jsonify({'message': '添加用户到部门失败,可能是由于数据完整性问题'}), 500 @bp.route('', methods=['GET']) def get_departments(): departments = Department.query.all() departments_list = [{ 'id': dept.id, 'name': dept.name, 'parent_id': dept.parent_id, 'created_at': dept.created_at.isoformat() if dept.created_at else None, 'updated_at': dept.updated_at.isoformat() if dept.updated_at else None } for dept in departments] return jsonify(departments_list), 200 @bp.route('/', methods=['GET']) def get_department_details(department_id): department = Department.query.get(department_id) if not department: return jsonify({'message': '部门不存在'}), 404 # 获取主要部门的用户 primary_users = User.query.filter_by(primary_department_id=department.id).all() # 获取次要部门的用户 secondary_users = department.users.all() # 合并用户列表并去重 all_users = list(set(primary_users + secondary_users)) users = [{'id': u.id, 'username': u.username} for u in all_users] children = [{'id': c.id, 'name': c.name} for c in department.children] department_details = { 'id': department.id, 'name': department.name, 'parent_id': department.parent_id, 'created_at': department.created_at.isoformat() if department.created_at else None, 'updated_at': department.updated_at.isoformat() if department.updated_at else None, 'department_code': department.get_detail('department_code'), 'manager': department.get_detail('manager'), 'phone': department.get_detail('phone'), 'email': department.get_detail('email'), 'location': department.get_detail('location'), 'users': users, 'children': children } return jsonify(department_details), 200 # 你可以在这里添加更多的部门相关路由...