package com.heyu.api.data.utils;


import com.heyu.api.data.dto.RespCode;
import org.slf4j.MDC;

import java.util.HashMap;
import java.util.Map;

/**
 * 统一响应类
 *
 * @author fuli
 * @version 1.0.0
 * @date 2018年6月23日
 */
public class R<T>  {

    /**
     * 业务数据放在
     */
    private T data;
    private String code;
    private String msg;

    // 联系方式
    //private String contactUs = "如果接口有疑问请加微信 fmai0923,shell_quyixiao,wulinq,或 致电18969093321,或发邮" +
//            "件service@ihzhy.com,如需咨询技术问题，请提供traceId";

    // 官网地址
  //  private String officialWebsiteAddress = "https://www.ihzhy.com";

    /**
     * 日志编号
     */
    private String traceId;

    // code = 288    模拟登录
    // code = 289    真实登录
    public R() {
        this.code = RespCode.SUCCESS.code;
        this.msg = RespCode.SUCCESS.msg;
        this.traceId = MDC.get("traceId");
        if (this.traceId == null) {
            this.traceId = MDC.get("X-B3-TraceId");
        }
    }

    public R(RespCode respCode) {
        this.code = respCode.code;
        this.msg = respCode.msg;
    }

    public R(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }


    public R(T data) {
        setCode(RespCode.SUCCESS.code);
        setMsg(RespCode.SUCCESS.msg);
        this.data = data;
    }

    public R(RespCode respCode, T data) {
        setCode(respCode.code);
        setMsg(respCode.msg);
        this.data = data;
    }

    public String getCode() {
        return code;
    }

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

    public String getMsg() {
        return msg;
    }

    public R setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public boolean ifSuccessful() {
        return code.equals(RespCode.SUCCESS.code);
    }

    public T getData() {
        return data;
    }

    public R setData(T data) {
        this.data = data;
        return this;
    }

    public static R error() {
        return error(RespCode.FAILED.getCode(), RespCode.FAILED.getMsg());
    }

    public static R error(String msg) {
        return error(RespCode.FAILED.getCode() + "", msg);
    }

    public static R error(String code, String msg) {
        R r = new R();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }

    public static R error(RespCode rsCode) {
        R r = new R();
        r.setCode(rsCode.getCode());
        r.setMsg(rsCode.getMsg());
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.setMsg(msg);
        return r;
    }

    public R put(String key, Object value) {
        Object object = this.getData();
        Map<String, Object> data = new HashMap<>();
        if (object != null) {
            data = (Map<String, Object>) object;
        }

        data.put(key, value);
        this.setData((T) data);
        return this;
    }

    public static R ok() {
        return new R();
    }

    public String getTraceId() {
        return traceId;
    }

    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }

  
}
