package com.zd.common.core.web.domain;

import com.zd.common.core.constant.HttpStatus;

import java.util.function.Supplier;

/**
 * 返回值包装类型
 *
 * @Author: zhoupan
 * @Date: 2021/09/09/10:46
 * @Description:
 */
public class ResultData<T> {


    /**
     * 状态码
     */
    private Integer code;


    private String msg;


    private T data;

    private long timestamp;

    public ResultData() {
        this.timestamp = System.currentTimeMillis();
    }

    public Integer getCode() {
        return code;
    }

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

    public String getMsg() {
        return msg;
    }

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

    public T getData() {
        return data;
    }

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


    public static <T> ResultData<T> success(T data) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(HttpStatus.SUCCESS);
        resultData.setMsg("操作成功");
        resultData.setData(data);
        return resultData;
    }

    public static <T> ResultData<T> success(T data,String message) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(HttpStatus.SUCCESS);
        resultData.setMsg(message);
        resultData.setData(data);
        return resultData;
    }


    public static <T> ResultData<T> success() {
        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(HttpStatus.SUCCESS);
        resultData.setMsg("操作成功");
        return resultData;
    }

    public static <T> ResultData<T> result(Boolean b, Supplier<String> message) {

        return result(b, null, message);
    }
    public static <T> ResultData<T> resultMessage(Boolean b, Supplier<String> message) {

        return result(b, null, message);
    }
    public static <T> ResultData<T> result(Boolean b) {

        return result(b, null, null);
    }

    public static <T> ResultData<T> result(Boolean b, T data, Supplier<String> message) {

        if (b) {
            return success(data);

        } else {
            if (message == null) {
                return fail();
            }
            return fail(message.get());
        }
    }
    public static <T> ResultData<T> resultMessage(Boolean b, T data, Supplier<String> message) {

        if (b) {
            return success(data,message.get());

        } else {
            if (message == null) {
                return fail();
            }
            return fail(message.get());
        }
    }
    /**
     * 根据ID判断成功还是失败，并返回ID 使用场景为需要返回ID的地方
     *
     * @param id
     * @param <T>
     * @return
     */
    public static ResultData<Long> result(Long id) {
        boolean f = id == null ? false : id > 0;
        return result(f, id, null);
    }

    public static <T> ResultData<T> result(int i) {

        return result(i > 0);
    }

    public static <T> ResultData<T> fail(int code, String message) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(code);
        resultData.setMsg(message);
        return resultData;
    }

    public static <T> ResultData<T> fail(String message) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(HttpStatus.ERROR);
        resultData.setMsg(message);
        return resultData;
    }

    public static <T> ResultData<T> fail() {
        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(HttpStatus.ERROR);
        resultData.setMsg("操作失败");
        return resultData;
    }
}
