'use strict';

module.exports = options => {
    /**
     * 排序筛选
     *
     * @param {function} next - 中间件继续执行的方法
     * @return {void}
     */
    return function* sortFilter(next) {
        this.sort = [];
        // 排序相关
        const sort = this.request.query.sort;
        if (sort !== undefined) {
            const sortData = sort.split('-');
            this.sort = sortData.length > 1 ? [sortData[1], 'DESC'] : [sortData[0], 'ASC'];
        }
        // 分页数据相关
        let page = this.request.query.page;
        page = parseInt(page);
        page = isNaN(page) || page <= 0 ? 1 : page;
        this.page = page;
        yield next;
    };
};
