package com.fdkankan.common.response;

import com.fdkankan.common.constant.ErrorCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Calendar;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResultData<T> implements Serializable {
    /**
     * 状态码
     */
    private int code;
    /**
     * 响应信息
     */
    private String message;
    /**
     * 后端返回结果
     */
    private T data;
    /**
     * 后端返回结果
     */
    private Boolean success;
    /**
     * 响应时间戳
     */
    private long timestamp = Calendar.getInstance().getTimeInMillis();

    public static ResultData ok() {
        return ok("");
    }
    public static ResultData ok(Object data) {
        return ok("", data);
    }
    public static ResultData ok(String msg, Object data) {
        return base(200, msg, data,true);
    }

    public static ResultData ok(int page,int pageSize,int total,Object data){
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPage(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(total);
        pageInfo.setList(data);
        return  ok(pageInfo);
    }

    public static ResultData error(int code, String msg) {
        return error(code, msg, ""); }
    public static ResultData error(int code, String msg, Object data) {
        return base(code, msg, data,false);
    }

    public static ResultData error(ErrorCode errorCode) {
        return error(errorCode.code(), errorCode.message());
    }

    private static ResultData  base(int code, String msg, Object data,Boolean success) {
        ResultData rd = new ResultData();
        rd.setCode(code);
        rd.setMessage(msg);
        rd.setData(data);
        rd.setSuccess(success);
        return rd;
    }
}