package com.yonge.cooleshow.biz.dal.support;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonge.toolset.mybatis.page.PageInfo;
import com.yonge.toolset.mybatis.page.QueryInfo;
import com.yonge.toolset.utils.string.StringUtil;
import org.springframework.beans.BeanUtils;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * @author hgw
 * Created by 2021-12-03
 */
public class PageUtil {

    private static final String ASC = "ASC";
    private static final String DESC = "DESC";

    private static Integer toInt(Integer num, Integer bak) {
        if (null == num || 0 == num) {
            return bak;
        }
        return num;
    }

    /**
     * 转化成mybatis plus中的Page
     *
     * @param query 查询条件
     * @return IPage
     */
    public static <T> IPage<T> getPage(QueryInfo query) {
        Page<T> page = new Page<T>(toInt(query.getPage(), 1), toInt(query.getRows(), 10));
        if (ASC.equals(query.getOrder()) && !StringUtil.isEmpty(query.getSort())) {
            page.setAsc(StringUtil.toStrArray(SqlKeyword.filter(query.getSort())));
        }
        if (DESC.equals(query.getOrder()) && !StringUtil.isEmpty(query.getSort())) {
            page.setDesc(StringUtil.toStrArray(SqlKeyword.filter(query.getSort())));
        }
        return page;
    }

    /**
     * 获取mybatis plus中的QueryWrapper
     *
     * @param entity 实体
     * @param <T>    类型
     * @return QueryWrapper
     */
    public static <T> QueryWrapper<T> getQueryWrapper(T entity) {
        return new QueryWrapper<>(entity);
    }

    /**
     * 获取mybatis plus中的QueryWrapper
     *
     * @param query   查询条件
     * @param exclude 排除的查询条件
     * @param clazz   实体类
     * @param <T>     类型
     * @return QueryWrapper
     */
    public static <T> QueryWrapper<T> getQueryWrapper(Map<String, Object> query, Map<String, Object> exclude, Class<T> clazz) {
        exclude.forEach((k, v) -> query.remove(k));
        QueryWrapper<T> qw = new QueryWrapper<>();
        qw.setEntity(BeanUtils.instantiateClass(clazz));
        SqlKeyword.buildCondition(query, qw);
        return qw;
    }

    /**
     * 获取mybatisPlus的分页模型
     *
     * @param page 页数
     * @param rows 查询数
     * @param <T>  返回的类
     */
    public static <T> Page<T> getPage(Integer page, Integer rows) {
        Integer pageIndex = Optional.ofNullable(page).orElse(1);
        Integer pageSize = Optional.ofNullable(rows).orElse(20);
        return new Page<T>(pageIndex, pageSize);
    }

    /**
     * 将mybatisPlus的分页模型 转换为本项目的分页模型
     *
     * @param source  mybatisPlus的分页模型
     * @param <T>返回的类
     */
    public static <T> PageInfo<T> pageInfo(IPage<T> source) {
        if (Objects.isNull(source)) {
            return new PageInfo<>(1, 20);
        }
        int total = Integer.parseInt(String.valueOf(source.getTotal()));
        int limit = Integer.parseInt(String.valueOf(source.getSize()));

        PageInfo<T> resultPage = new PageInfo<T>(
                ((Long) source.getCurrent()).intValue(),
                ((Long) source.getSize()).intValue());

        resultPage.setRows(source.getRecords());
        resultPage.setLimit(limit);
        resultPage.setTotal(total);
        return resultPage;
    }


    /**
     * 获取Map中的关键字获取分页数据
     *
     * @param param Map<String, Object> param
     * @param str   关键字
     * @return Optional
     */
    public static <O> Optional<Integer> getPage(Map<String, O> param, String str) {
        return Optional.ofNullable(param)
                .map(p -> p.get(str))
                .map(String::valueOf)
                .map(Integer::valueOf);
    }

    public static <O, T> Page<T> getPageInfo(Map<String, O> param) {
        int pageSize = getPage(param, "rows").orElse(20);
        int pageIndex = getPage(param, "page").orElse(1);
        return new Page<>(pageIndex, pageSize);
    }

}
