organization.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import uuid
  2. from Log import logger
  3. from app.config.const import DEPT_STATUS_DELETE, DEPT_STATUS_ON, DEPT_STATUS_OFF
  4. from app.models import OrganizationModel
  5. from app.models.role_model import RoleModel
  6. async def get_organization_list(db, deptName: str):
  7. query = db.query(OrganizationModel).filter(OrganizationModel.organization_id == None)
  8. if deptName:
  9. query = query.filter(OrganizationModel.name.like('%{}%'.format(deptName)))
  10. depts = query.order_by(OrganizationModel.seq.desc()).all()
  11. return {"total": query.count(), "rows": [dept.to_json() for dept in depts]}
  12. async def create_dept(db, dept_name, leader, phone, address, status, order_num, roles, groups,parent_id):
  13. try:
  14. dept_model = OrganizationModel(id=str(uuid.uuid4()),name=dept_name, address=address,leader=leader,phone=phone,seq=order_num,status=str(status))
  15. if parent_id:
  16. dept_model.parent = db.get(OrganizationModel, parent_id)
  17. if roles:
  18. dept_model.roles = [db.get(RoleModel, roleId) for roleId in roles]
  19. # if groups:
  20. # dept_model.groups = [db.get(GroupModel, groupId) for groupId in groups]
  21. db.add(dept_model)
  22. db.commit()
  23. db.refresh(dept_model)
  24. except Exception as e:
  25. logger.error(e)
  26. db.rollback()
  27. return False
  28. return True
  29. async def edit_dept_data(db, dept_id, dept_name, leader, phone, address, status, order_num, roles, groups,parent_id):
  30. try:
  31. dept_model = db.query(OrganizationModel).filter(OrganizationModel.id == dept_id).first()
  32. dept_model.name = dept_name
  33. dept_model.address = address
  34. dept_model.phone = phone
  35. dept_model.leader = leader
  36. # dept_model.status = status
  37. dept_model.seq = order_num
  38. # if parent_id:
  39. # dept_model.parent = db.get(OrganizationModel, parent_id)
  40. if roles:
  41. dept_model.roles = [db.get(RoleModel, roleId) for roleId in roles]
  42. # if groups:
  43. # dept_model.groups = [db.get(GroupModel, groupId) for groupId in groups]
  44. # db.add(dept_model)
  45. db.commit()
  46. db.refresh(dept_model)
  47. except Exception as e:
  48. logger.error(e)
  49. db.rollback()
  50. return False
  51. return True
  52. async def edit_dept_parent(db, dept_id, parent_id, order_num):
  53. try:
  54. dept_model = db.query(OrganizationModel).filter(OrganizationModel.id == dept_id).first()
  55. dept_model.parent = db.get(OrganizationModel, parent_id)
  56. dept_model.seq = order_num
  57. db.commit()
  58. db.refresh(dept_model)
  59. except Exception as e:
  60. logger.error(e)
  61. db.rollback()
  62. return False
  63. return True
  64. async def get_organization_info(db, dept_id: str):
  65. dept = db.query(OrganizationModel).filter(OrganizationModel.id.__eq__(dept_id)).first()
  66. return {"total": 0, "data": dept.to_dict()}
  67. async def delete_organization_info(db, dept_id: str):
  68. delete_list = []
  69. dept_list = []
  70. next_dept_list = []
  71. base_dept = db.query(OrganizationModel).filter(OrganizationModel.id.__eq__(dept_id)).first()
  72. dept_list.append(base_dept)
  73. while dept_list:
  74. for dept in dept_list:
  75. delete_list.append(dept.id)
  76. if dept.roles:
  77. return "部门:{}已配置角色,不允许删除!".format(dept.name)
  78. for child_dept in dept.children:
  79. next_dept_list.append(child_dept)
  80. dept_list = next_dept_list
  81. next_dept_list = []
  82. try:
  83. db.query(OrganizationModel).filter(OrganizationModel.id.in_(delete_list)).update({"status": DEPT_STATUS_DELETE})
  84. db.commit()
  85. except Exception as e:
  86. logger.error(e)
  87. db.rollback()
  88. return '服务异常 !'
  89. return ""
  90. async def edit_organization_status(db, dept_id: str, status:str):
  91. delete_list = []
  92. dept_list = []
  93. next_dept_list = []
  94. base_dept = db.query(OrganizationModel).filter(OrganizationModel.id.__eq__(dept_id)).first()
  95. if status == DEPT_STATUS_ON:
  96. print(11)
  97. print(base_dept.name)
  98. if base_dept.parent and base_dept.parent.status != DEPT_STATUS_ON:
  99. return '上级节点状态异常!'
  100. try:
  101. base_dept.status=status
  102. db.commit()
  103. except Exception as e:
  104. logger.error(e)
  105. db.rollback()
  106. return '服务异常 !'
  107. else:
  108. dept_list.append(base_dept)
  109. while dept_list:
  110. for dept in dept_list:
  111. delete_list.append(dept.id)
  112. if dept.roles:
  113. return "部门:{}已配置角色,不允许!".format(dept.name)
  114. for child_dept in dept.children:
  115. next_dept_list.append(child_dept)
  116. dept_list = next_dept_list
  117. next_dept_list = []
  118. try:
  119. db.query(OrganizationModel).filter(OrganizationModel.id.in_(delete_list)).update({"status": DEPT_STATUS_OFF})
  120. db.commit()
  121. except Exception as e:
  122. logger.error(e)
  123. db.rollback()
  124. return '服务异常 !'
  125. return ""