package com.hotent.base.model;

import java.io.Serializable;
import com.hotent.base.enums.ResponseErrorEnums;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel("通用返回结果")
public class CommonResult<E> implements Serializable {
	private static final long serialVersionUID = 1L;

	@ApiModelProperty(name="state",notes="状态 true：操作成功  false：操作失败")
	Boolean state = true;

	@ApiModelProperty(name="message",notes="提示信息")
	String message;

	@ApiModelProperty(name="value",notes="返回的数据")
	E value;

	@ApiModelProperty(value ="标准的http状态码：200, 400, 401, 500等")
	private Integer code = 200;

	@ApiModelProperty(value = "错误代码")
	private String errorCode;

	@ApiModelProperty(value = "日志ID")
	private String logId;
	
	/**
	 * 构造成功的返回结果
	 * <p>
	 * 例如：CommonResult.ok(); 或者 CommonResult.&lt;String>ok();<br />
	 * 结果集中各属性默认值如下：<br />
	 * state: true<br />
	 * message: "操作成功"<br />
	 * code: 200
	 * </p>
	 * @param <E>
	 * @return
	 */
	public static <E> CommonResult<E> ok() {
		CommonResult<E> cr = new CommonResult<>(ResponseErrorEnums.SUCCESS_OPTION);
		return cr;
	}
	
	/**
	 * 构造成功的返回结果
	 * <p>
	 * 例如：CommonResult.error(); 或者 CommonResult.&lt;String>error();<br />
	 * 结果集中各属性默认值如下：<br />
	 * state: false<br />
	 * message: "系统异常"<br />
	 * code: 500
	 * </p>
	 * @param <E>
	 * @return
	 */
	public static <E> CommonResult<E> error() {
		CommonResult<E> cr = new CommonResult<>(ResponseErrorEnums.SYSTEM_ERROR);
		return cr;
	}

	/**
	 * 构造指定的返回结果
	 * <p>
	 * 例如：CommonResult.result(ResponseErrorEnums.SUCCESS_OPTION);<br />
	 * 结果集中各属性对应传入的ResponseErrorEnums
	 * </p>
	 * @param <E>
	 * @return
	 */
	public static <E> CommonResult<E> result(ResponseErrorEnums res) {
		CommonResult<E> cr = new CommonResult<>(res);
		return cr;
	}
	
	/**
	 * 设置message并返回结果
	 * @param message
	 * @return
	 */
	public CommonResult<E> message(String message){
		this.setMessage(message);
		return this;
	}

	/**
	 * 设置value并返回结果
	 * @param val
	 * @return
	 */
	public CommonResult<E> value(E val){
		this.setValue(val);
		return this;
	}
	
	/**
	 * 设置logId并返回结果
	 * @param logId
	 * @return
	 */
	public CommonResult<E> log(String logId){
		this.setLogId(logId);
		return this;
	}

	public CommonResult() {
	}

	/**
	 * 返回成功及成功的提示信息
	 * @param message
	 */
	public CommonResult(String message) {
		this(true, message, null);
	}

	/**
	 * 返回成功/失败，及对应的成功/失败提示信息
	 * @param state
	 * @param message
	 */
	public CommonResult(boolean state, String message) {
		this(state, message, null);
	}

	/**
	 * 返回成功/失败，及对应的成功/失败提示信息，还有返回对应的数据
	 * @param state
	 * @param message
	 * @param value
	 */
	public CommonResult(boolean state,String message,E value){
		this.state = state;
		this.message = message;
		this.value = value;
	}

	/**
	 * 返回错误，及错误编码，对应的错误信息
	 * @param error
	 */
	public CommonResult(ResponseErrorEnums error) {
		if(!error.getCode().equals("200")){
			this.state = false;
		}
		this.errorCode = error.getCode();
		this.message = error.getMessage();
		this.code = error.getHttpCode();
	}

	/**
	 * 返回错误，及错误编码，对应的错误信息，还有返回对应的数据
	 * @param error
	 * @param value
	 */
	public CommonResult(ResponseErrorEnums error, E value) {
		if(!error.getCode().equals("200")){
			this.state = false;
		}
		this.errorCode = error.getCode();
		this.message = error.getMessage();
		this.code = error.getHttpCode();
		this.value = value;
	}

	public Boolean getState() {
		return state;
	}

	public void setState(Boolean state) {
		this.state = state;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public E getValue() {
		return value;
	}

	public void setValue(E value) {
		this.value = value;
	}

	public String getLogId() {
		return logId;
	}

	public void setLogId(String logId) {
		this.logId = logId;
	}

	public Integer getCode() {
		return code;
	}

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

	public String getErrorCode() {
		return errorCode;
	}

	public void setErrorCode(String errorCode) {
		this.errorCode = errorCode;
	}
}
