package com.energy.manage.common.reponse;

import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.StringUtils;

import java.util.Optional;

/**
 * 前端统一结构返回数据
 */
@Getter
@Setter
public class ResultResp<T> {
  private Integer code;
  private String msg;
  private Boolean status;
  private T data;

  /**
   * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   */
  /**
   * 构造器
   *
   * @param <T>
   * @return
   */
  private static <T> ResultResp<T> buildResponse(Integer code, String msg, T data, boolean status) {
    ResultResp<T> resp = new ResultResp<>();
    if (Optional.ofNullable(code).isPresent()) {
      resp.setCode(code);
    }
    if (!StringUtils.isEmpty(msg)) {
      resp.setMsg(msg);
    }
    if (Optional.ofNullable(data).isPresent()) {
      resp.setData(data);
    }
    resp.setStatus(status);
    return resp;
  }


  /**
   * 操作成功
   */
  public static ResultResp SUCCESS() {
    return ResultResp.buildResponse(ConstVar.SUCCESS.getCode(),
            ConstVar.SUCCESS.getMsg(), null, true);
  }

  /**
   * 操作成功
   */
  public static <T> ResultResp<T> SUCCESS(T data) {
    return ResultResp.buildResponse(ConstVar.SUCCESS.getCode(),
            ConstVar.SUCCESS.getMsg(), data, true);
  }

  /**
   * 操作成功
   */
  public static <T> ResultResp<T> SUCCESS(Integer code, String msg) {
    return ResultResp.buildResponse(code, msg, null, true);
  }

  /**
   * 操作成功
   */
  public static <T> ResultResp<T> SUCCESS(Integer code, String msg, T data) {
    return ResultResp.buildResponse(code, msg, data, true);
  }

  /**
   * 操作失败
   */
  public static ResultResp FAIL() {
    return ResultResp.buildResponse(ConstVar.FAIL.getCode(), ConstVar.FAIL.getMsg(), null, false);
  }

  public static ResultResp FAIL(String msg) {
    return ResultResp.buildResponse(ConstVar.FAIL.getCode(), msg, null, false);
  }

  public static ResultResp FAIL(Integer code, String msg) {
    return ResultResp.buildResponse(code, msg, null, false);
  }

  public static ResultResp FAIL(ConstVar constVar) {
    return ResultResp.buildResponse(constVar.getCode(), constVar.getMsg(), null, false);
  }


  /**
   * 密码错误
   *
   * @return
   */
  public static ResultResp ERRORPWD() {
    return ResultResp.buildResponse(ConstVar.ERRORPWD.getCode(), ConstVar.ERRORPWD.getMsg(), null, false);
  }

  /**
   * 参数不能为空
   *
   * @return
   */
  public static ResultResp NOTNULLPARAM() {
    return ResultResp.buildResponse(ConstVar.NOTNULLPARAM.getCode(), ConstVar.NOTNULLPARAM.getMsg(), null, false);
  }

  /**
   * 用户不存在
   *
   * @return
   */
  public static ResultResp NOTEXISTSUSER() {
    return ResultResp.buildResponse(ConstVar.NOTEXISTSUSER.getCode(),ConstVar.NOTEXISTSUSER.getMsg(), null, false);
  }


  /**
   * 用户已经存在
   *
   * @return
   */
  public static ResultResp EXISTSUSER() {
    return ResultResp.buildResponse(ConstVar.EXISTSUSER.getCode(),ConstVar.EXISTSUSER.getMsg(), null, false);
  }

  /**
   * 无效参数
   *
   * @return
   */
  public static ResultResp INVALIDPARAM(String param) {
    return ResultResp.buildResponse(ConstVar.INVALIDPARAM.getCode(), ConstVar.INVALIDPARAM.getMsg(), null, false);
  }

  /**
   * token失效
   *
   * @return
   */
  public static ResultResp TOKENVALID() {
    return ResultResp.buildResponse(ConstVar.TOKENINVALID.getCode(), ConstVar.TOKENINVALID.getMsg(), null, false);
  }

  /**
   * 验签失败
   *
   * @return
   */
  public static ResultResp ERRORSIGN() {
    return ResultResp.buildResponse(ConstVar.ERRORSIGN.getCode(), ConstVar.ERRORSIGN.getMsg(), null, false);
  }

  /**
   * 系统异常
   *
   * @return
   */
  public static ResultResp USERNOTLOGIN() {
    return ResultResp.buildResponse(ConstVar.PLEASELOGIN.getCode(), ConstVar.PLEASELOGIN.getMsg(), null, false);
  }

  /**
   * 系统异常
   *
   * @return
   */
  public static ResultResp FAILException() {
    return ResultResp.buildResponse(ConstVar.FAIL.getCode(), "系统错误，请联系管理员", null, false);
  }

}
