package com.trade.common.core.page;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.annotation.JSONField;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class GridPage<T> implements Serializable {

	private static final long serialVersionUID = 1L;
	/** 每页数据条数 */
	@JSONField(serialize = false)
	private Integer pageNum = 10;
	/** 当前页 */
	private Integer page = 1;
	/** 总页数 */
	private Integer total = 1;
	/** 查询出的记录数 */
	private Integer records = 1;
	/** 实际数据集合 */
	private List<T> rows;
	/**
	 * 响应码
	 */
	private String code = "200";

	private Boolean success = true;

	public Boolean getSuccess() {
		return success;
	}

	public void setSuccess(Boolean success) {
		this.success = success;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public long getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	public Integer getRecords() {
		return records;
	}

	public void setRecords(Integer records) {
		this.records = records;
	}

	public List<T> getRows() {
		return rows;
	}

	public void setRows(List<T> rows) {
		this.rows = rows;
	}

	/**
	 * @param pageNum
	 *            每页数据条数
	 * @param page
	 *            当前页
	 * @param count
	 *            查询总记录数
	 * @param list
	 *            数据集合
	 */
	public GridPage(int pageNum, int page, long count, List<T> list) {
		setPage(page);
		setRecords((int)count);
		setRows(list);
		if (count % pageNum > 0) {
			setTotal((int) (count / pageNum) + 1);
		} else {
			setTotal((int) (count / pageNum));
		}
	}

	public GridPage(List<T> list) {
		setRecords(list.size());
		setRows(list);
	}

	public GridPage() {
		super();
	}

	/**
	 * MP转换
	 * @param list 数据
	 * @param total 总记录数
	 * @param current 当前页
	 * @param count 总页数
	 */
    public GridPage(List<T> list, int total, int current, int count){
        setRows(list);
        setRecords(total);
        setPage(current);
        setTotal(count);
    }

    public static <V, P> GridPage<V> empty(Page<P> p) {
        return new GridPage<>(Collections.emptyList(), (int)p.getTotal(), (int)p.getCurrent(),(int) p.getPages());
    }

    public static <V, P> GridPage<V> of(Page<P> p, Class<V> voClass) {
        // 1. 非空校验
        List<P> records = p.getRecords();
        if (records == null || records.isEmpty()) {
            // 无数据，返回空结果
            return empty(p);
        }
        // 2. 数据转换
        List<V> vos = BeanUtil.copyToList(records, voClass);
        // 3. 封装返回
        return new GridPage<>(vos, (int)p.getTotal(), (int)p.getCurrent(), (int)p.getPages());
    }

    public static <V, P> GridPage<V> of(Page<P> p, Function<P, V> convertor) {
        // 1. 非空校验
        List<P> records = p.getRecords();
        if (records == null || records.isEmpty()) {
            // 无数据，返回空结果
            return empty(p);
        }
        // 2. 数据转换
        List<V> vos = records.stream().map(convertor).collect(Collectors.toList());
        // 3. 封装返回
        return new GridPage<>(vos, (int)p.getTotal(), (int)p.getCurrent(), (int)p.getPages());
    }
}
