package xiaoqu.home.open.utils;

import javax.servlet.http.HttpServletRequest;

import xiaoqu.home.open.constant.Errors;
import xiaoqu.home.open.publicBean.BaseOutput;

/**
 * 响应返回数据工具类
 * 
 */
public class ResultResponseBeanUtil {

	public static <T> BaseOutput<T> success(T data) {
		BaseOutput<T> entity = new BaseOutput<T>();
		entity.setData(data);
		entity.setCode(Errors.SUCCESS.code);
		return entity;
	}

	public static BaseOutput<String> success(String msg) {
		BaseOutput<String> entity = new BaseOutput<String>();
		entity.setData(msg);
		entity.setCode(Errors.SUCCESS.code);
		return entity;
	}

	public static BaseOutput<Void> success() {
		BaseOutput<Void> entity = build();
		entity.setCode(Errors.SUCCESS.code);
		return entity;
	}

	public static <T> BaseOutput<T> fail(Integer httpStatus, Integer code, String message, HttpServletRequest request) {
		BaseOutput<T> entity = new BaseOutput<T>();
		entity.setCode(code);
		entity.setErr(message);
		return entity;
	}

	private static BaseOutput<Void> build() {
		BaseOutput<Void> entity = new BaseOutput<Void>();
		return entity;
	}

	public static <T> BaseOutput<T> fail(Errors errors) {
		BaseOutput<T> entity = new BaseOutput<T>();
		if (errors == null) {
			entity.setCode(9999);
			entity.setErr("未知错误");
			return entity;
		}
		entity.setCode(errors.code);
		entity.setErr(errors.label);
		return entity;
	}

	public static <T> BaseOutput<T> fail(Errors errors, String message) {
		BaseOutput<T> entity = new BaseOutput<T>();
		entity.setCode(errors.code);
		entity.setErr(message);
		return entity;
	}

	public static <T> BaseOutput<T> fail(String message) {
		BaseOutput<T> entity = new BaseOutput<T>();
		entity.setCode(Errors.SYSTEM_ERROR.code);
		entity.setErr(message);
		return entity;
	}

}
