from sqlalchemy.orm.query import Query


def get_pager(query: Query, pagerNum: int, pagerSize: int, ):
    total = query.count()
    data = query.limit(pagerSize).offset((pagerNum - 1) * pagerSize).all()
    pager = Pager(total, data)
    return pager


class Pager:
    def __init__(self, total: int, data: object):
        self.total = total
        self.data = data

    def keys(self):
        keys = ["total", "data"]
        return keys

    def __getitem__(self, item):
        '''
        内置方法, 当使用obj['name']的形式的时候, 将调用这个方法, 这里返回的结果就是值
        :param item:
        :return:
        '''
        return getattr(self, item, None)