package com.lanyuanxiaoyao.service.configuration.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分页实体类
 *
 * @author lanyuanxiaoyao
 * @date 2023-04-26
 */
public class PageResponse<T> {
    private Iterable<T> data = new ArrayList<>(0);
    private Long total = 0L;
    private Map<String, Object> metadata = new HashMap<>();

    public PageResponse() {
    }

    public PageResponse(Iterable<T> data, Long total) {
        this.data = data;
        this.total = total;
    }

    public PageResponse(Iterable<T> data, Integer total) {
        this.data = data;
        this.total = total.longValue();
    }

    public PageResponse(List<T> data, Long total, Map<String, Object> metadata) {
        this(data, total);
        this.metadata = metadata;
    }

    public Iterable<T> getData() {
        return data;
    }

    public void setData(Iterable<T> data) {
        this.data = data;
    }

    public Long getTotal() {
        return total;
    }

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

    public Map<String, Object> getMetadata() {
        return metadata;
    }

    public void setMetadata(Map<String, Object> metadata) {
        this.metadata = metadata;
    }

    public PageResponse<T> withMetadata(String key, Object value) {
        this.metadata.put(key, value);
        return this;
    }

    @Override
    public String toString() {
        return "PageResponse{" +
                "data=" + data +
                ", total=" + total +
                ", metadata=" + metadata +
                '}';
    }
}
