package com.chushang.common.mybatis.page;

import com.chushang.common.core.exception.utils.AssertUtil;
import com.chushang.common.core.text.Convert;
import com.chushang.common.core.util.ServletUtils;
import com.chushang.common.core.util.StringUtils;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * by zhaowenyuan create 2021/11/5 14:53
 * 公共 请求 query
 */
@Data
public class CommonParam implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 当前页码
     */
    private Integer page = 1;
    /**
     * 页面大小
     */
    private Integer limit = 10;
    /**
     * 排序字段: 排序方式 -- 为 null 时, 没有排序
     * 默认创建时间倒叙
     */
    private String orderBy = "create_time";
    /**
     * 额外分组选项
     */
    private String groupStr;
    /**
     * 排序方式
     */
    private String isAsc = "asc";
    /**
     * 是否查询全部数据
     */
    private Boolean isAll = false;
    /**
     * 内部使用 sql 查询, 仅用于 角色权限所需查询, 前端不能传递
     */
    private Map<String, Object> sqlParam;

    public Map<String, Object> getSqlParam()
    {
        if (sqlParam == null)
        {
            sqlParam = new HashMap<>();
        }
        return sqlParam;
    }
    public String getOrderBy()
    {
        if (StringUtils.isEmpty(orderBy))
        {
            return "";
        }
        return StringUtils.toUnderScoreCase(orderBy);
    }

    public void setIsAsc(String isAsc)
    {
        if (StringUtils.isNotEmpty(isAsc))
        {
            // 兼容前端排序类型
            if ("ascending".equals(isAsc))
            {
                isAsc = "asc";
            }
            else if ("descending".equals(isAsc))
            {
                isAsc = "desc";
            }
            this.isAsc = isAsc;
        }
    }

    /**
     * 当前记录起始索引
     */
    private static final String PAGE_NUM = "page";
    /**
     * 每页显示记录数
     */
    private static final String PAGE_SIZE = "limit";
    /**
     * 排序列
     */
    private static final String ORDER_BY_COLUMN = "orderBy";
    /**
     * 排序的方向 "desc" 或者 "asc".
     */
    private static final String IS_ASC = "isAsc";

    private static final String GROUP_STR = "groupStr";
    /**
     * 封装分页对象
     */
    private static CommonParam getPageParam()
    {
        CommonParam pageDomain = new CommonParam();
        pageDomain.setPage(Convert.toInt(ServletUtils.getParameter(PAGE_NUM), 1));
        pageDomain.setLimit(Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), 10));
        if (StringUtils.isNotEmpty(ServletUtils.getParameter(ORDER_BY_COLUMN)))
            pageDomain.setOrderBy(ServletUtils.getParameter(ORDER_BY_COLUMN));
        if (StringUtils.isNotEmpty(ServletUtils.getParameter(IS_ASC)))
            pageDomain.setIsAsc(ServletUtils.getParameter(IS_ASC));
        if (StringUtils.isNotEmpty(ServletUtils.getParameter(GROUP_STR)))
            pageDomain.setGroupStr(ServletUtils.getParameter(GROUP_STR));
        return pageDomain;
    }

    public static CommonParam buildPageRequest()
    {
        return getPageParam();
    }

    /**
     * 用于查询所有数据
     */
    public static CommonParam buildAllRequest()
    {
        CommonParam commonParam = new CommonParam();
        commonParam.setLimit(null);
        commonParam.setPage(null);
        commonParam.setIsAll(true);
        return commonParam;
    }
}
