package com.yonge.toolset.base.result;

import org.springframework.http.HttpStatus;

import java.util.Date;

/**
 * HttpResponse 返回结果
 */
public class BaseResult<T> {

	private boolean status = true;
	private String msg;
	private T data;
	private int code;
	private Date nowTime = new Date();

	public BaseResult(boolean status, int code, T data, String message) {
		this.status = status;
		this.msg = message;
		this.data = data;
		this.code = code;
	}

	public BaseResult(boolean status, HttpStatus resultCode, T data, String message) {
		this.status = status;
		this.msg = message;
		this.data = data;
		this.code = resultCode.value();
	}

	public BaseResult() {
	}

	public int getCode() {
		return code;
	}

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

	public boolean getStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	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 void success() {
		this.status = true;
	}

	public void fail() {
		this.status = false;
	}

	public Date getNowTime() {
		return nowTime;
	}

	public void setNowTime(Date nowTime) {
		this.nowTime = nowTime;
	}


	public static <T> BaseResult<T> succeed(T object) {
		return getResponseData(true, HttpStatus.OK, object, "");
	}

	public static <T> BaseResult<T> succeed() {
		return getResponseData(true, HttpStatus.OK, null, "");
	}

	public static <T> BaseResult<T> succeedData(T obj) {
		return getResponseData(true, HttpStatus.OK, obj, "操作成功");
	}

	public static <T> BaseResult<T> warned(String message) {
		return failed(HttpStatus.MULTI_STATUS, message);
	}

	public static <T> BaseResult<T> failed() {
		return failed("");
	}

	public static <T> BaseResult<T> failed(String msg) {
		return failed(HttpStatus.INTERNAL_SERVER_ERROR, msg);
	}

	public static <T> BaseResult<T> failed(HttpStatus statusCode, String msg) {
		return getResponseData(false, statusCode, null, msg);
	}

	public static <T> BaseResult<T> failed(HttpStatus statusCode, T data, String msg) {
		return getResponseData(false, statusCode, data, msg);
	}
	public static <T> BaseResult<T> failed(int code, T data, String msg) {
		return new BaseResult<>(false, code, data, msg);
	}

	public static <T> BaseResult<T> status(boolean flag) {
		return flag ? succeed() : failed("操作失败");
	}

	private static <T> BaseResult<T> getResponseData(boolean status, HttpStatus statusCode, T data, String message) {
		BaseResult<T> obj = new BaseResult<T>();
		obj.setStatus(status);
		obj.setCode(statusCode.value());
		obj.setData(data);
		obj.setMsg(message);
		return obj;
	}
}
