|
|
@@ -1,120 +0,0 @@
|
|
|
-package com.zhongshu.iot.server.core.util.result;
|
|
|
-
|
|
|
-import com.fasterxml.jackson.annotation.JsonInclude;
|
|
|
-import com.zhongshu.iot.client.type.ResultState;
|
|
|
-import lombok.*;
|
|
|
-
|
|
|
-import java.util.Optional;
|
|
|
-import java.util.function.Consumer;
|
|
|
-
|
|
|
-/**
|
|
|
- * 结果
|
|
|
- *
|
|
|
- * @param <T>
|
|
|
- */
|
|
|
-@Builder
|
|
|
-@AllArgsConstructor
|
|
|
-@NoArgsConstructor
|
|
|
-@ToString(callSuper = true)
|
|
|
-@JsonInclude(JsonInclude.Include.NON_NULL)
|
|
|
-public class ResultContent<T> {
|
|
|
-
|
|
|
- //内容
|
|
|
- @Getter
|
|
|
- @Setter
|
|
|
- private T content;
|
|
|
-
|
|
|
- //状态
|
|
|
- @Getter
|
|
|
- @Setter
|
|
|
- private ResultState state;
|
|
|
-
|
|
|
- //文本
|
|
|
- @Getter
|
|
|
- @Setter
|
|
|
- private String msg;
|
|
|
-
|
|
|
- @Getter
|
|
|
- @Setter
|
|
|
- private Integer code = 200;
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取内容
|
|
|
- *
|
|
|
- * @return
|
|
|
- */
|
|
|
- public Optional<T> optionalContent() {
|
|
|
- return Optional.ofNullable(this.content);
|
|
|
- }
|
|
|
-
|
|
|
- public static <T> ResultContent build(ResultState state, T content) {
|
|
|
- return ResultContent.builder().state(state).content(content).msg(state.getRemark()).build();
|
|
|
- }
|
|
|
-
|
|
|
- public static <T> ResultContent build(ResultState state, T content, String msg) {
|
|
|
- return ResultContent.builder().state(state).content(content).msg(msg).build();
|
|
|
- }
|
|
|
-
|
|
|
- public static <T> ResultContent build(ResultState state, String msg) {
|
|
|
- return ResultContent.builder().state(state).msg(msg).build();
|
|
|
- }
|
|
|
-
|
|
|
- public static <T> ResultContent build(boolean bool) {
|
|
|
- return build(bool ? ResultState.Success : ResultState.Fail, null);
|
|
|
- }
|
|
|
-
|
|
|
- public static <T> ResultContent buildFail(String msg) {
|
|
|
- return ResultContent.builder().state(ResultState.Fail).code(500).msg(msg).build();
|
|
|
- }
|
|
|
-
|
|
|
- public static <T> ResultContent buildFail(String msg, Integer code) {
|
|
|
- return ResultContent.builder().state(ResultState.Fail).code(code).msg(msg).build();
|
|
|
- }
|
|
|
-
|
|
|
- public static <T> ResultContent buildSuccess(String msg) {
|
|
|
- return build(ResultState.Success, msg);
|
|
|
- }
|
|
|
-
|
|
|
- public static <T> ResultContent buildSuccess(T content) {
|
|
|
- return build(ResultState.Success, content);
|
|
|
- }
|
|
|
-
|
|
|
- public static <T> ResultContent buildSuccess() {
|
|
|
- return build(ResultState.Success, ResultState.Success.getRemark());
|
|
|
- }
|
|
|
-
|
|
|
- public static <T> ResultContent build(ResultState state) {
|
|
|
- return build(state, state.getRemark());
|
|
|
- }
|
|
|
-
|
|
|
- public static <T> ResultContent buildContent(T content) {
|
|
|
- return build(content == null ? ResultState.Fail : ResultState.Success, content);
|
|
|
- }
|
|
|
-
|
|
|
- public void ifSuccess(Consumer<? super T> action) {
|
|
|
- if (isSuccess() && content != null) {
|
|
|
- action.accept(content);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void ifSucessOrElse(Consumer<? super T> action, Runnable emptyAction) {
|
|
|
- if (isSuccess() && content != null) {
|
|
|
- action.accept(content);
|
|
|
- } else {
|
|
|
- emptyAction.run();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private Boolean isSuccess = false;
|
|
|
-
|
|
|
- public Boolean isSuccess() {
|
|
|
- return ResultState.Success.equals(this.state);
|
|
|
- }
|
|
|
-
|
|
|
- private boolean isFailed = false;
|
|
|
-
|
|
|
- public boolean isFailed() {
|
|
|
- return !ResultState.Success.equals(this.state);
|
|
|
- }
|
|
|
-}
|