package com.iotechn.unimall.app.common;

import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @description:
 * @author: YeQingchao
 * @create: 2020-03-11 16:27
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResultVO<T> implements Serializable {

    private static final long serialVersionUID = 8518955172797076258L;
    // 数据
    private T data;
    // 返回码
    private ErrorCodeEnums code;

    public static <T> ResultVO<T> success() {
        ResultVO<T> resultVO = new ResultVO<>();
//        JSONObject codeJson = new JSONObject();
//        codeJson.put("code", ErrorCodeEnums.SUCCESS);
//        resultVO.setCode(codeJson);
        resultVO.setCode(ErrorCodeEnums.SUCCESS);
        return resultVO;
    }

    public static <T> ResultVO<T> success(T data) {
        ResultVO<T> resultVO = new ResultVO<>();
//        JSONObject codeJson = new JSONObject();
//        codeJson
        resultVO.setCode(ErrorCodeEnums.SUCCESS);
        resultVO.setData(data);
        return resultVO;
    }


    public static <T> ResultVO<T> fail(ErrorCodeEnums errorCodeEnums) {
        ResultVO<T> resultVO = new ResultVO<>();
//        JSONObject codeJson = JSONObject.parseObject(errorCodeEnums.toString());
//        resultVO.setCode(codeJson);
        resultVO.setCode(errorCodeEnums);
        return resultVO;
    }

    public static <T> ResultVO<T> fail(ErrorCodeEnums errorCodeEnums, T data) {
        ResultVO<T> resultVO = new ResultVO<>();
//        JSONObject codeJson = JSONObject.parseObject(errorCodeEnums.toString());
//        resultVO.setCode(codeJson);
        resultVO.setCode(errorCodeEnums);
        resultVO.setData(data);
        return resultVO;
    }

    public JSONObject toJSONObject() {
        JSONObject out = new JSONObject();
        out.put("data", data);
        out.put("code", code);
        return out;
    }

    @Override
    public String toString() {
        return toJSONObject().toString();
    }

    public static void main(String[] args) {
        ResultVO resultVO = ResultVO.success(new JSONObject());
    }
}
