package com.heketai.website.common.result;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
 * @author xjl
 * @param <E>
 */
@Data
public class BaseResult<E> implements Serializable {
    private static final long serialVersionUID = -4694021853622171550L;
    @ApiModelProperty(value = "状态码")
    private String httpCode;
    @ApiModelProperty(value = "时间")
    private Date date;
    @ApiModelProperty(value = "返回消息")
    private String message;
    @ApiModelProperty(value = "返回结果集")
    private E data;

    public BaseResult() {
        setDate(new Date());
    }

    public BaseResult(E data) {
        setData(data);
        setHttpCode(HttpCode.OK.getCode());
        setMessage(HttpCode.OK.getMsg());
        setDate(new Date());
    }

    public BaseResult(String httpCode) {
        setHttpCode(httpCode);
        setDate(new Date());
    }

    public BaseResult(String httpCode, String message) {
        setHttpCode(httpCode);
        setDate(new Date());
        setMessage(message);
    }

    public static <E> BaseResult<E> successWithData(E data) {
        return new BaseResult<E>(data);
    }

    public static <E> BaseResult<E> success() {
        BaseResult<E> result = new BaseResult<E>();
        result.setHttpCode(HttpCode.OK.getCode());
        result.setMessage(HttpCode.OK.getMsg());
        return result;
    }

    public static <E> BaseResult<E> fail(String httpCode) {
        BaseResult<E> result = new BaseResult<E>(httpCode);
        result.setMessage(HttpCode.FAIL.getMsg());
        return result;
    }

    public static <E> BaseResult<E> fail(String httpCode, String message) {
        return new BaseResult<E>(httpCode, message);
    }

}
