organization.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import uuid
  2. from Log import logger
  3. from app.models import OrganizationModel
  4. from app.models.role_model import RoleModel
  5. async def get_organization_list(db, deptName: str):
  6. query = db.query(OrganizationModel).filter(OrganizationModel.organization_id == None)
  7. if deptName:
  8. query = query.filter(OrganizationModel.name.like('%{}%'.format(deptName)))
  9. depts = query.order_by(OrganizationModel.seq.desc()).all()
  10. return {"total": query.count(), "rows": [dept.to_json() for dept in depts]}
  11. async def create_dept(db, dept_name, leader, phone, address, status, order_num, roles, groups,parent_id):
  12. try:
  13. dept_model = OrganizationModel(id=str(uuid.uuid4()),name=dept_name, address=address,leader=leader,phone=phone,seq=order_num,status=status)
  14. if parent_id:
  15. dept_model.parent = db.get(OrganizationModel, parent_id)
  16. if roles:
  17. dept_model.roles = [db.get(RoleModel, roleId) for roleId in roles]
  18. # if groups:
  19. # dept_model.groups = [db.get(GroupModel, groupId) for groupId in groups]
  20. db.add(dept_model)
  21. db.commit()
  22. db.refresh(dept_model)
  23. except Exception as e:
  24. logger.error(e)
  25. db.rollback()
  26. return False
  27. return True
  28. async def edit_dept_data(db, dept_id, dept_name, leader, phone, address, status, order_num, roles, groups,parent_id):
  29. try:
  30. dept_model = db.query(OrganizationModel).filter(OrganizationModel.id == dept_id).first()
  31. dept_model.name = dept_name
  32. dept_model.address = address
  33. dept_model.phone = phone
  34. dept_model.leader = leader
  35. dept_model.status = status
  36. dept_model.seq = order_num
  37. # if parent_id:
  38. # dept_model.parent = db.get(OrganizationModel, parent_id)
  39. if roles:
  40. dept_model.roles = [db.get(RoleModel, roleId) for roleId in roles]
  41. # if groups:
  42. # dept_model.groups = [db.get(GroupModel, groupId) for groupId in groups]
  43. # db.add(dept_model)
  44. db.commit()
  45. db.refresh(dept_model)
  46. except Exception as e:
  47. logger.error(e)
  48. db.rollback()
  49. return False
  50. return True
  51. async def edit_dept_parent(db, dept_id, parent_id, order_num):
  52. try:
  53. dept_model = db.query(OrganizationModel).filter(OrganizationModel.id == dept_id).first()
  54. dept_model.parent = db.get(OrganizationModel, parent_id)
  55. dept_model.seq = order_num
  56. db.commit()
  57. db.refresh(dept_model)
  58. except Exception as e:
  59. logger.error(e)
  60. db.rollback()
  61. return False
  62. return True
  63. async def get_organization_info(db, dept_id: str):
  64. dept = db.query(OrganizationModel).filter(OrganizationModel.id.__eq__(dept_id)).first()
  65. return {"total": 0, "data": dept.to_json()}
  66. async def delete_organization_info(db, dept_id: str):
  67. try:
  68. db.query(OrganizationModel).filter(OrganizationModel.id.__eq__(dept_id)).delete()
  69. db.commit()
  70. except Exception as e:
  71. logger.error(e)
  72. db.rollback()
  73. return False
  74. return True