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


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonge.toolset.base.page.PageInfo;

import java.util.List;
import java.util.Objects;

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

    public static <T> PageInfo<T> pageInfo(com.microsvc.toolkit.common.response.paging.PageInfo<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.getLimit()));
        PageInfo<T> resultPage = new PageInfo<>();
        resultPage.setRows(source.getRows());
        resultPage.setPageNo(Long.valueOf(source.getCurrent()).intValue());
        resultPage.setLimit(limit);
        resultPage.setTotal(total);
        return resultPage;
    }

    public static <T,E> PageInfo<T> pageInfo(com.microsvc.toolkit.common.response.paging.PageInfo<E> source, List<T> rows) {
        if (Objects.isNull(source)) {
            return new PageInfo<>(1, 20);
        }
        int total = Integer.parseInt(String.valueOf(source.getTotal()));
        int limit = Integer.parseInt(String.valueOf(source.getLimit()));
        PageInfo<T> resultPage = new PageInfo<>();
        resultPage.setRows(rows);
        resultPage.setPageNo(Long.valueOf(source.getCurrent()).intValue());
        resultPage.setLimit(limit);
        resultPage.setTotal(total);
        return resultPage;
    }

    public static <T,E> com.microsvc.toolkit.common.response.paging.PageInfo<T> transPageInfo(com.microsvc.toolkit.common.response.paging.PageInfo<E> source, List<T> rows) {
        if (Objects.isNull(source)) {
            return new com.microsvc.toolkit.common.response.paging.PageInfo<>();
        }
        int total = Integer.parseInt(String.valueOf(source.getTotal()));
        int limit = Integer.parseInt(String.valueOf(source.getLimit()));
        com.microsvc.toolkit.common.response.paging.PageInfo<T> resultPage = new com.microsvc.toolkit.common.response.paging.PageInfo<>();
        resultPage.setRows(rows);
        resultPage.setCurrent(Long.valueOf(source.getCurrent()).intValue());
        resultPage.setLimit(limit);
        resultPage.setTotal(total);
        return resultPage;
    }

    public static <T,E> IPage<T> transPageInfo(IPage<E> source, List<T> rows) {
        if (Objects.isNull(source)) {
            return new Page<>();
        }
        int total = Integer.parseInt(String.valueOf(source.getTotal()));
        int limit = Integer.parseInt(String.valueOf(source.getSize()));
        IPage<T> resultPage = new Page<>();
        resultPage.setRecords(rows);
        resultPage.setCurrent(Long.valueOf(source.getCurrent()).intValue());
        resultPage.setSize(limit);
        resultPage.setTotal(total);
        return resultPage;
    }

    public static <T,E> PageInfo<T> transPageInfo(PageInfo<E> source, List<T> rows) {
        if (Objects.isNull(source)) {
            return new PageInfo<>();
        }
        int total = Integer.parseInt(String.valueOf(source.getTotal()));
        int limit = Integer.parseInt(String.valueOf(source.getLimit()));
        PageInfo<T> resultPage = new PageInfo<>();
        resultPage.setRows(rows);
        resultPage.setPageNo(Long.valueOf(source.getPageNo()).intValue());
        resultPage.setLimit(limit);
        resultPage.setTotal(total);
        return resultPage;
    }
}
