| | |
| | | import uuid |
| | | |
| | | from Log import logger |
| | | from app.config.const import DEPT_STATUS_DELETE, DEPT_STATUS_ON, DEPT_STATUS_OFF |
| | | from app.models import OrganizationModel |
| | | from app.models.role_model import RoleModel |
| | | |
| | |
| | | |
| | | async def create_dept(db, dept_name, leader, phone, address, status, order_num, roles, groups,parent_id): |
| | | try: |
| | | dept_model = OrganizationModel(id=str(uuid.uuid4()),name=dept_name, address=address,leader=leader,phone=phone,seq=order_num,status=status) |
| | | dept_model = OrganizationModel(id=str(uuid.uuid4()),name=dept_name, address=address,leader=leader,phone=phone,seq=order_num,status=str(status)) |
| | | if parent_id: |
| | | dept_model.parent = db.get(OrganizationModel, parent_id) |
| | | if roles: |
| | |
| | | dept_model.address = address |
| | | dept_model.phone = phone |
| | | dept_model.leader = leader |
| | | dept_model.status = status |
| | | # dept_model.status = status |
| | | dept_model.seq = order_num |
| | | # if parent_id: |
| | | # dept_model.parent = db.get(OrganizationModel, parent_id) |
| | |
| | | async def get_organization_info(db, dept_id: str): |
| | | dept = db.query(OrganizationModel).filter(OrganizationModel.id.__eq__(dept_id)).first() |
| | | |
| | | return {"total": 0, "data": dept.to_json()} |
| | | return {"total": 0, "data": dept.to_dict()} |
| | | |
| | | |
| | | async def delete_organization_info(db, dept_id: str): |
| | | delete_list = [] |
| | | dept_list = [] |
| | | next_dept_list = [] |
| | | base_dept = db.query(OrganizationModel).filter(OrganizationModel.id.__eq__(dept_id)).first() |
| | | dept_list.append(base_dept) |
| | | while dept_list: |
| | | for dept in dept_list: |
| | | delete_list.append(dept.id) |
| | | if dept.roles: |
| | | return "部门:{}已配置角色,不允许删除!".format(dept.name) |
| | | for child_dept in dept.children: |
| | | next_dept_list.append(child_dept) |
| | | |
| | | dept_list = next_dept_list |
| | | next_dept_list = [] |
| | | |
| | | |
| | | try: |
| | | db.query(OrganizationModel).filter(OrganizationModel.id.__eq__(dept_id)).delete() |
| | | db.query(OrganizationModel).filter(OrganizationModel.id.in_(delete_list)).update({"status": DEPT_STATUS_DELETE}) |
| | | db.commit() |
| | | except Exception as e: |
| | | logger.error(e) |
| | | db.rollback() |
| | | return False |
| | | return True |
| | | return '服务异常 !' |
| | | return "" |
| | | |
| | | |
| | | async def edit_organization_status(db, dept_id: str, status:str): |
| | | delete_list = [] |
| | | dept_list = [] |
| | | next_dept_list = [] |
| | | base_dept = db.query(OrganizationModel).filter(OrganizationModel.id.__eq__(dept_id)).first() |
| | | if status == DEPT_STATUS_ON: |
| | | print(11) |
| | | print(base_dept.name) |
| | | if base_dept.parent and base_dept.parent.status != DEPT_STATUS_ON: |
| | | return '上级节点状态异常!' |
| | | try: |
| | | base_dept.status=status |
| | | db.commit() |
| | | except Exception as e: |
| | | logger.error(e) |
| | | db.rollback() |
| | | return '服务异常 !' |
| | | else: |
| | | dept_list.append(base_dept) |
| | | while dept_list: |
| | | for dept in dept_list: |
| | | delete_list.append(dept.id) |
| | | if dept.roles: |
| | | return "部门:{}已配置角色,不允许!".format(dept.name) |
| | | for child_dept in dept.children: |
| | | next_dept_list.append(child_dept) |
| | | |
| | | dept_list = next_dept_list |
| | | next_dept_list = [] |
| | | try: |
| | | db.query(OrganizationModel).filter(OrganizationModel.id.in_(delete_list)).update({"status": DEPT_STATUS_OFF}) |
| | | db.commit() |
| | | except Exception as e: |
| | | logger.error(e) |
| | | db.rollback() |
| | | return '服务异常 !' |
| | | return "" |