package cn.common.page.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import cn.common.enums.ResultCode;
import cn.common.exception.BusinessException;
import cn.config.SysConfig;
import cn.util.core.IsKit;
import cn.util.string.NamingKit;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通用的结果返回类
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> implements Serializable
{
	/**静态代码块获取静态注入变量值*/
	private final static boolean humpConversion = new SysConfig().isHumpConversion();
	//私有构造器
	private Result() {}

	/**成功标志.与code = 200 作用一样*/
	private boolean success = true;
	/**成功的code值为200；*/
	private Integer code = 200;

	//返回处理消息
	private String msg = "操作成功！";
	//返回详细数据
	private T data;

	/**支持Element-plus返回值*/
	@SuppressWarnings("unchecked")
	public static <T> Result<T> ok_ePlus(final Long totalRecord,List<Map<String, Object>> resultList)
	{
		Long total = 0L;
		if(IsKit.isNotNullOrEmpty(totalRecord)) {
			total = totalRecord;
		}
		Map<String,Object> resultMap = new HashMap<>();
		resultMap.put("total",total);
		//字段的驼峰表示
		if(humpConversion) {
			resultMap.put("records",NamingKit.underlineToCaml(resultList));
		}
		else {
			resultMap.put("records",resultList);
		}
		return (Result<T>)new Result<>(resultMap);
	}
	/**支持app页面*/
	@SuppressWarnings("unchecked")
	public static <T> Result<T> ok_ePlusList(final Long totalRecord,List<?> resultList)
	{
		Long total = 0L;
		if(IsKit.isNotNullOrEmpty(totalRecord)) {
			total = totalRecord;
		}
		Map<String,Object> resultMap = new HashMap<>();
		resultMap.put("total",total);
		resultMap.put("records",resultList);
		return (Result<T>)new Result<>(resultMap);
	}

	public static Result<?> ok()
	{
		return setOkHttpCodeEnum(ResultCode.HTTP_SUCCESS,ResultCode.HTTP_SUCCESS.getMsg());
	}
	public static <T> Result<T> ok(final T data)
	{
		return new Result<>(data);
	}
	public static <T> Result<T> ok(String msg,final T data)
	{
		return new Result<>(data,msg);
	}
	public static Result<?> ok(int code, String msg)
	{
		Result<?> result = new Result<>();
		return result.okObj(code,msg);
	}
	/**SQL查询结果提示信息*/
	public static Result<?> ok(Boolean flag)
	{
		if(flag) {
			return ok();
		}
		else {
			Result<?> result = new Result<>();
			return result.errorObj(ResultCode.HTTP_SUCCESS.getCode(), ResultCode.SQL_RUN.getMsg());
		}
	}
	public static Result<?> okMsg(String msg)
	{
		Result<?> result = new Result<>();
		return result.okObj(ResultCode.HTTP_SUCCESS.getCode(), msg);
	}
	/**功能等同OK，返回success，但并不是所希望的操作结果. 用success:false进行表示*/
	public static Result<?> warnMsg(String msg)
	{
		Result<?> result = new Result<>();
		return result.errorObj(ResultCode.HTTP_SUCCESS.getCode(), msg);
	}
	/**默认错误提示*/
	public static Result<?> errorMsg(String msg) {
		Result<?> result = new Result<>();
		return result.errorObj(ResultCode.SYS_SERVER.getCode(), msg);
	}
	//默认返回500
	public static Result<?> error()
	{
		return setOkHttpCodeEnum(ResultCode.HTTP_INTERNAL_SERVER_ERROR,
				ResultCode.HTTP_INTERNAL_SERVER_ERROR.getMsg());
	}
	public static Result<?> error(BusinessException e) {
		return error(e.getCode(), e.getMsg());
	}
	public static Result<?> error(ResultCode enums) {
		return setErrorHttpCodeEnum(enums, enums.getMsg());
	}
	public static Result<?> error(ResultCode enums, String message) {
		return setErrorHttpCodeEnum(enums, message);
	}
	public static Result<?> error(int code, String msg) {
		Result<?> result = new Result<>();
		return result.errorObj(code, msg);
	}
	private static Result<?> setErrorHttpCodeEnum(ResultCode enums,String message) {
		return error(enums.getCode(), message);
	}
	private static Result<?> setOkHttpCodeEnum(ResultCode enums, String message) {
		return ok(enums.getCode(), message);
	}
	private Result(T data)
	{
		this.code = ResultCode.HTTP_SUCCESS.getCode();
		this.msg = ResultCode.HTTP_SUCCESS.getMsg();
		this.data = data;
	}
	private Result(T data, String msg)
	{
		this.code = ResultCode.HTTP_SUCCESS.getCode();
		this.msg = msg;
		this.data = data;
		this.success = true;
	}
	private Result<T> okObj(Integer code, String msg) {
		this.code = code;
		this.msg = msg;
		this.success = true;
		return this;
	}
	private Result<T> errorObj(Integer code, String msg) {
		this.code = code;
		this.msg = msg;
		this.success = false;
		return this;
	}

	//自定义参数,链式编程
	public Result<T> data(T data) {
		this.setData(data);
		return this;
	}
	public Result<T> code(int code) {
		this.setCode(code);
		return this;
	}
	public Result<T> msg(String msg) {
		this.setMsg(msg);
		return this;
	}

	private void setCode(Integer code) {
		this.code = code;
	}
	private void setData(T data) {
		this.data = data;
	}
	private void setMsg(String msg) {
		this.msg = msg;
	}
	public boolean isSuccess() {return success;}
	public void setSuccess(boolean success) {this.success = success;}
	public Integer getCode() {
		return code;
	}
	public String getMsg() {
		return msg;
	}
	public T getData() {
		return data;
	}
	private long timestamp = System.currentTimeMillis();
	public void setTimestamp(long timestamp) {
		this.timestamp = timestamp;
	}
	public long getTimestamp() {
		return timestamp;
	}

	private String reqTime;
	private String respTime;
	public void setReqTime(String reqTime) {
		this.reqTime = reqTime;
	}
	public void setRespTime(String respTime) {
		this.respTime = respTime;
	}
	//自定义返回数据
	@Override
	public String toString() {
		return "RestResult{" +
				"code=" + code +
				", msg='" + msg + '\'' +
				", data=" + data +
				", success=" + success +
				", timestamp=" + timestamp +
				'}';
	}
}