def set_powers_by_role_ids(roles_menus_powers_list):
        all_menus = []
        for item in roles_menus_powers_list:
            all_menus.extend(item['menus'])
        # 判断powers属于哪个menu
        res = AdminMenuPower.get_menu_powers_by_group_menu_id(
            list(set(all_menus)))

        new_menu_powers_dict = {}
        for item in res:
            tmp_power_ids = item[1].split(',')
            new_menu_powers_dict[item[0]] = [int(x) for x in tmp_power_ids]

        roles_menus_powers_dict = {}
        for item in roles_menus_powers_list:
            if item['menus']:
                res_menu_power_dict = {}
                for menu in item['menus']:
                    res_menu_power_dict[menu] = list(
                        set(new_menu_powers_dict[menu]) & set(item['powers']))
                roles_menus_powers_dict[item['role_id']] = res_menu_power_dict
            else:
                roles_menus_powers_dict[item['role_id']] = {}

        return AdminRoleMenuPower.transanction_save_roles_menus_powers(
            roles_menus_powers_dict)
 def add_menu_power(menu_id, title, code, description, sorts, status):
     if not menu_id:
         raise APIException(msg='菜单标识不能为空', code=50009)
     if not title:
         raise APIException(msg='标题不能为空', code=50009)
     if not code:
         raise APIException(msg='code不能为空', code=50009)
     return AdminMenuPower.add_menu_power(menu_id, title, code, description,
                                          sorts, status)
    def set_powers_by_role_id(role_id, menus, powers):
        # 判断powers属于哪个menu
        res = AdminMenuPower.get_menu_powers_by_group_menu_id(menus)
        res_menu_power_dict = {}
        for item in res:
            tmp_power_ids = item[1].split(',')
            res_menu_power_dict[item[0]] = list(
                set(tmp_power_ids) & set(powers))

        return AdminRoleMenuPower.transanction_save_role_menu_powers(
            role_id, res_menu_power_dict)
    def get_menu_powers():
        menus = AdminMenu.get_all_menus()
        ids = []
        menus_dict = {}
        for item in menus:
            ids.append(item.uuid)
            item = dict(item)
            item['powers'] = []
            menus_dict[item['uuid']] = item

        list_powers = AdminMenuPower.get_powers_by_menu_ids(ids)
        for item in list_powers:
            menus_dict[item.menu_id]['powers'].append(item)
        return list(menus_dict.values())
 def del_menu_power(uuid):
     if not uuid:
         raise APIException(msg='权限标识不能为空', code=50009)
     return AdminMenuPower.del_menu_power(uuid)
 def update_menu_power(kwargs):
     if not kwargs['uuid']:
         raise APIException(msg='权限标识不能为空', code=50009)
     uuid = kwargs['uuid']
     del kwargs['uuid']
     return AdminMenuPower.update_menu_power(uuid, **kwargs)
 def get_menu_powers_by_menu_id(menu_ids: typing.List):
     return AdminMenuPower.get_powers_by_menu_ids(ids=menu_ids, order=False)
 def get_menu_powers_by_power_ids(power_ids):
     if not isinstance(power_ids, list):
         raise ValError()
     return AdminMenuPower.get_powers_by_power_ids(power_ids, order=False)