package cn.skcks.matrix.v2.model.services;

import cn.skcks.matrix.v2.utils.json.JsonResponse;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;

@RequiredArgsConstructor
@AllArgsConstructor
@Builder
@Data
public class ServiceResult<T> {
    private final T result;
    private String exception;
    private String message;

    public boolean hasException() {
        return StringUtils.isNotBlank(exception);
    }

    public boolean hasMessage() {
        return StringUtils.isNotBlank(message);
    }

    public JsonResponse<T> parseResponse() {
        if (hasException()) {
            return JsonResponse.error(null, exception);
        } else {
            if (hasMessage()) {
                return JsonResponse.success(result, message);
            } else {
                return JsonResponse.success(result);
            }
        }
    }
}
