package net.northking.cctp.platform.util;


import net.northking.cctp.common.db.OrderBy;
import net.northking.cctp.common.db.SqlOrderBy;
import net.northking.cctp.common.http.QueryOrderBy;
import org.apache.commons.lang.StringUtils;

import java.util.List;

/**
 * 分页查询工具类
 *
 * @author jieying.li
 * @since 2020.08.23
 */
public class PageUtils {

    /**
     * 排序的组装
     * 要求必须有createdBy字段
     * @param orderByList
     * @return
     */
    public static OrderBy createOrderBy(List<QueryOrderBy> orderByList) {
        OrderBy sqlOrderBy = new SqlOrderBy();
        if (null != orderByList && orderByList.size() > 0) {
            for (QueryOrderBy queryOrderBy : orderByList) {
                if (StringUtils.isNotBlank(queryOrderBy.getColumn()) && StringUtils.isNotBlank(queryOrderBy.getDir())) {
                    sqlOrderBy.addOrderBy(queryOrderBy.getColumn(), queryOrderBy.getDir());
                }
            }
        }
        if (sqlOrderBy.isEmpty()) {
            sqlOrderBy.desc("createdTime");
        }
        return sqlOrderBy;
    }

    /**
     * 校验 pageNo
     * @param pageNo 当前页
     * @return
     */
    public static Integer validatePageNo(Integer pageNo) {
        if (pageNo <= 0) {
            pageNo = 1;
        }
        return pageNo;
    }

    /**
     * 校验 pageSize
     * @param pageSize 页面大小
     * @return
     */
    public static Integer validatePageSize(Integer pageSize) {
        if (pageSize <= 0 || pageSize > 500) {
            pageSize = 500;
        }
        return pageSize;
    }
}
