import uuid

from Log import logger
from app.config.const import RESOURCE_STATUS_ON, RESOURCE_STATUS_OFF, RESOURCE_STATUS_DELETE
from app.models import ResourceModel


async def get_resource_tree(db):
    resource = db.query(ResourceModel).filter(ResourceModel.resource_id == None).order_by(ResourceModel.seq.desc()).all()

    return {"total": 0, "rows":  [dept.to_tree_select_json() for dept in resource]}


async def get_resource_info(db, menu_id: str):
    dept = db.query(ResourceModel).filter(ResourceModel.id.__eq__(menu_id)).first()

    return {"total": 0, "data":  dept.to_json()}


async def delete_resource_info(db, menu_id):
    delete_list = []
    dept_list = []
    next_dept_list = []
    base_dept = db.query(ResourceModel).filter(ResourceModel.id.__eq__(menu_id)).first()
    dept_list.append(base_dept)
    while dept_list:
        for dept in dept_list:
            delete_list.append(dept.id)
            for child_dept in dept.children:
                next_dept_list.append(child_dept)

        dept_list = next_dept_list
        next_dept_list = []

    try:
        db.query(ResourceModel).filter(ResourceModel.id.in_(delete_list)).update({"status": RESOURCE_STATUS_DELETE})
        db.commit()
    except Exception as e:
        logger.error(e)
        db.rollback()
        return '服务异常 ！'
    return ""


async def create_menu(db, menu_name, component, path, order_num, perms, menu_type, description, parent_id, status, icon):
    try:
        dept_model = ResourceModel(id=str(uuid.uuid4()),name=menu_name, url=component,path=path,perms=perms,description=description,status=status, icon=icon,seq=order_num, resource_type_id=menu_type)
        if parent_id:
            dept_model.parent = db.get(ResourceModel, parent_id)
        db.add(dept_model)
        db.commit()
        db.refresh(dept_model)
    except Exception as e:
        logger.error(e)
        db.rollback()
        return False
    return True


async def edit_resource_data(db, menu_id, menu_name, component, path, order_num, perms, menu_type, description, parent_id, status, icon):
    try:
        dept_model = db.query(ResourceModel).filter(ResourceModel.id == menu_id).first()
        dept_model.name = menu_name
        dept_model.url = component
        dept_model.path = path
        dept_model.perms = perms
        # dept_model.status = status
        dept_model.icon = icon
        dept_model.seq = order_num
        dept_model.description = description
        dept_model.resource_type_id = menu_type
        if parent_id:
            dept_model.parent = db.get(ResourceModel, parent_id)
        db.commit()
        db.refresh(dept_model)
    except Exception as e:
        logger.error(e)
        db.rollback()
        return False
    return True


async def edit_resource_status(db, menu_id, status):
    delete_list = []
    dept_list = []
    next_dept_list = []
    base_dept = db.query(ResourceModel).filter(ResourceModel.id.__eq__(menu_id)).first()
    if status == RESOURCE_STATUS_ON:
        if base_dept.parent and base_dept.parent.status != RESOURCE_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)
                for child_dept in dept.children:
                    next_dept_list.append(child_dept)

            dept_list = next_dept_list
            next_dept_list = []
        try:
            db.query(ResourceModel).filter(ResourceModel.id.in_(delete_list)).update(
                {"status": RESOURCE_STATUS_OFF})
            db.commit()
        except Exception as e:
            logger.error(e)
            db.rollback()
            return '服务异常 ！'
    return ""