package com.ms.biz.common;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.Data;

import java.util.Map;

/**
 * 方法处理结果
 */
@Data
public class Ret {
    String result;
    String reason;
    String code;
    Map data;
    Object queueMsg;
    String logId;
    String solution;

    public Ret() {
    }

    public Ret(String result, Map data) {
        this.result = result;
        this.data = data;
    }

    public Ret(String result, String reason) {
        this.result = result;
        this.reason = reason;
    }

    public Ret(String result, String reason, String code) {
        this.result = result;
        this.reason = reason;
        this.code = code;
    }

    public static Ret success() {
        return Ret.success(null);
    }

    public static Ret success(Map data) {
        if (ObjectUtil.isEmpty(data)){
            return new Ret("success", data);
        }
        String jsonStr = JSONObject.toJSONString(data, SerializerFeature.WriteDateUseDateFormat);
        return new Ret("success", JSONObject.parseObject(jsonStr));
    }

    public static Ret fail() {
        return Ret.fail("");
    }

    public static Ret fail(String reason) {
        return new Ret("fail", reason);
    }

    @Override
    public String toString() {
        return "Ret{" +
            "result='" + result + '\'' +
            ", reason='" + reason + '\'' +
            ", code='" + code + '\'' +
            ", data=" + data +
            '}';
    }
}
