package org.holer.server.model;

import org.holer.server.constant.HolerCode;
import org.holer.server.util.ServerUtil;
import org.springframework.data.domain.Page;


public class HolerResult<T> {
    private Integer code;
    private String msg;
    private T data;
    private Long total;

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

    public void setMsg(String msg) {
        this.msg = msg;
    }

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

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


    public Integer getCode() {
        return this.code;
    }


    public String getMsg() {
        return this.msg;
    }


    public T getData() {
        return (T) this.data;
    }


    public Long getTotal() {
        return this.total;
    }


    public HolerResult(Integer code) {
        HolerStatus status = ServerUtil.status(code);
        this.code = status.getCode();
        this.msg = status.getMsg();
    }


    public HolerResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }


    public HolerResult(HolerCode code) {
        HolerStatus status = ServerUtil.status(code.value());
        this.code = status.getCode();
        this.msg = status.getMsg();
    }


    public HolerResult(Page<?> page, HolerCode code) {
        this.total = page.getTotalElements();
        this.data = (T) page.getContent();

        HolerStatus status = ServerUtil.status(code.value());
        this.code = status.getCode();
        this.msg = status.getMsg();
    }


    public HolerResult(T data, HolerCode code) {
        this.data = data;
        HolerStatus status = ServerUtil.status(code.value());
        this.code = status.getCode();
        this.msg = status.getMsg();
    }
}
