package com.platform.yijia.utils;

public class ResultData<T> {
    private String message;
    private int retCode;
    private T data;
    private ResultData(T data) {
        this.retCode = 0;
        this.message = "成功";
        this.data = data;
    }
    private ResultData(CodeMsg cm) {
        if(cm == null){
            return;
        }
        this.retCode = cm.getRetCode();
        this.message = cm.getMessage();
    }
    /**
     * 成功时候的调用
     * @return
     */
    public static <T> ResultData<T> success(T data){
        return new ResultData<T>(data);
    }
    /**
     * 成功，不需要传入参数
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> ResultData<T> success(){
        return (ResultData<T>) success("");
    }
    /**
     * 失败时候的调用
     * @return
     */
    public static <T> ResultData<T> error(CodeMsg cm){
        return new ResultData<T>(cm);
    }
    /**
     * 失败时候的调用,扩展消息参数
     * @param cm
     * @param msg
     * @return
     */
    public static <T> ResultData<T> error(CodeMsg cm,String msg){
        cm.setMessage(cm.getMessage()+"--"+msg);
        return new ResultData<T>(cm);
    }
    public T getData() {
        return data;
    }
    public String getMessage() {
        return message;
    }
    public int getRetCode() {
        return retCode;
    }
}
