package com.hs.admin.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 请求统一返回结果
 */
@ApiModel(description = "接口结果")
public class Result<T> {
    @ApiModelProperty("错误编码")
    private int code;
    @ApiModelProperty("错误信息")
    private String msg;
    @ApiModelProperty(value = "返回的数据")
    private T data;

    private Result(int code,String msg,T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    @Override
    public String toString() {
        return "{" +
                "\"code\":" + code +
                ", \"msg\":\"" + msg + '\"' +
                ", \"data\":" + data +
                '}';
    }

    public int getCode() {
        return code;
    }

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

    public String getMsg() {
        return msg;
    }

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

    public Object getData() {
        return data;
    }

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

    public static <T> Result<T> success(T data) {
        return new Result(ErrorCode.SUCCESS.getCode(),ErrorCode.SUCCESS.getEnMessage(),data);
    }

    public static Result error(int code,String msg) {
        return new Result(code,msg,null);
    }

    public static Result error() {
        return new Result(ErrorCode.ERROR.getCode(),ErrorCode.ERROR.getEnMessage(),null);
    }

    public static Result error(String msg) {
        return new Result(ErrorCode.ERROR.getCode(),msg,null);
    }

    public static Result error(ErrorCode errorCode) {
        return new Result(errorCode.getCode(),errorCode.getEnMessage(),null);
    }


    public static enum ErrorCode {
        SUCCESS(1,"成功","success"),
        INVALID_TOKEN(401,"无效的TOKEN","invalid token"),
        ERROR(400,"错误","error"),
        USERERROR(2,"账号或密码错误","wrong account or password"),
        NO_PERMISSION(3,"非管理员账号","no permission");
        private int code;
        private String cnMessage;
        private String enMessage;

        ErrorCode(int code,String cnMessage,String enMessage) {
            this.code = code;
            this.cnMessage = cnMessage;
            this.enMessage = enMessage;
        }

        public int getCode() {
            return code;
        }

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

        public String getCnMessage() {
            return cnMessage;
        }

        public void setCnMessage(String cnMessage) {
            this.cnMessage = cnMessage;
        }

        public String getEnMessage() {
            return enMessage;
        }

        public void setEnMessage(String enMessage) {
            this.enMessage = enMessage;
        }
    }
}
