package com.ankaibei.workFlow.common.core.object;

import com.alibaba.fastjson.JSON;
import lombok.Data;

import java.io.Serializable;

/**
 * @author 亚信安全 by cuijs
 * @version 2019-10-08 16:17
 * Description:返回信息
 */
@Data
public class ResMsg<T> implements Serializable {

    private String code;

    private boolean success;

    private String msg;

    private T data;

    private String errorCode;

    private String errorMessage;

    public ResMsg() {
    }

    public ResMsg(String code) {
        this.code  =  code;
        if(code.equals("200")){
            this.success = true;
        }else {
            this.success = false;
        }
    }

    public ResMsg(String code, String msg) {
        this.code  =  code;
        if(code.equals("200")){
            this.success = true;
        }else {
            this.success = false;
            this.errorCode = code;
            this.errorMessage = msg;
        }
        this.msg = msg;
    }

    public ResMsg(String code, String msg, T data) {
        this.code  =  code;
        if(code.equals("200")){
            this.success = true;
        }else {
            this.success = false;
            this.errorCode = code;
            this.errorMessage = msg;
        }
        this.msg = msg;
        this.data = data;
    }


    public ResMsg(CodeEnum CodeEnum) {
        this(CodeEnum.getCode(), CodeEnum.toString());
    }

    public ResMsg(CodeEnum CodeEnum, T data) {
        this(CodeEnum.getCode(), CodeEnum.toString(), data);
    }

    public static String toJsonString(CodeEnum CodeEnum) {
        return JSON.toJSONString(new ResMsg(CodeEnum));
    }

    public static String toJsonString(CodeEnum CodeEnum, Object data) {
        return JSON.toJSONString(new ResMsg(CodeEnum, data));
    }

    public static ResMsg newInstance(CodeEnum CodeEnum, Object data) {
        return new ResMsg(CodeEnum, data);
    }

    public static ResMsg newInstance(String enumCode, String enumMsg, Object data) {
        return new ResMsg(enumCode, enumMsg, data);
    }

    public static ResMsg error(CodeEnum CodeEnum, String message, Object data) {
        return new ResMsg(CodeEnum.getCode(), message, data);
    }

    public static ResMsg error(String enumCode, String enumMsg) {
        return new ResMsg(enumCode, enumMsg, null);
    }

    public static ResMsg newInstance(CodeEnum CodeEnum) {
        return new ResMsg(CodeEnum, null);
    }

    public static String serverErrorResponse() {
        return toJsonString(CodeEnum.SERVER_ERROR_UNKNOWN);
    }

    public static String paramEmptyErrorResponse() {
        return toJsonString(CodeEnum.SERVER_ERROR_UNKNOWN);
    }

    public static String OK() {
        return toJsonString(CodeEnum.OK);
    }
}

