package com.nuvole.util;
|
|
import com.nuvole.common.domain.emnu.CommonResultEmnu;
|
import com.nuvole.common.domain.result.CommonResult;
|
import lombok.AllArgsConstructor;
|
import lombok.Data;
|
|
import java.util.LinkedList;
|
import java.util.List;
|
import java.util.function.Supplier;
|
|
/**
|
* 功能描述:CommonResult工具
|
* @author xuekang
|
* @date 2024-04-16 10:59
|
* @version 1.0
|
* @param <E> data类型
|
*/
|
public class ResultHandler<E> {
|
/** CommonResult */
|
private final CommonResult<E> result;
|
/** 设置空处理函数 */
|
private Supplier<E> nullProcess;
|
/** 设置data空处理函数 */
|
private Supplier<E> dataNullProcess;
|
/** 错误码处理函数 */
|
private List<ErrorProcess<E>> errorProcessList;
|
|
public ResultHandler(CommonResult<E> result) {
|
this.result = result;
|
}
|
|
public ResultHandler(CommonResult<E> result, ResultHandler<E> resultHandler) {
|
this.result = result;
|
this.nullProcess = resultHandler.nullProcess;
|
this.dataNullProcess = resultHandler.dataNullProcess;
|
this.errorProcessList = resultHandler.errorProcessList;
|
}
|
|
/**
|
* 设置空处理函数
|
* @param nullProcess 空处理函数
|
* @return ResultHandler
|
*/
|
public ResultHandler<E> setNullProcess(Supplier<E> nullProcess) {
|
this.nullProcess = nullProcess;
|
return this;
|
}
|
|
/**
|
* 设置data空处理函数
|
* @param dataNullProcess data 空处理函数
|
* @return ResultHandler
|
*/
|
public ResultHandler<E> setDataNullProcess(Supplier<E> dataNullProcess) {
|
this.dataNullProcess = dataNullProcess;
|
return this;
|
}
|
|
/**
|
* 错误码处理函数
|
* @param code 错误码
|
* @param errorProcessList 函数
|
* @return ResultHandler
|
*/
|
public ResultHandler<E> appendError(Integer code, Supplier<E> errorProcessList) {
|
if (this.errorProcessList == null) {
|
this.errorProcessList = new LinkedList<>();
|
}
|
this.errorProcessList.add(new ErrorProcess<>(code, errorProcessList));
|
return this;
|
}
|
|
/**
|
* CommonResult工具
|
* @param result CommonResult
|
* @return ResultHandler
|
* @param <T> data类型
|
*/
|
public static <T>ResultHandler<T> getInstance(CommonResult<T> result) {
|
return new ResultHandler<>(result);
|
}
|
|
/**
|
* 获取
|
* @param result CommonResult
|
* @param resultHandler ResultHandler
|
* @return ResultHandler
|
* @param <T> date类型
|
*/
|
public static <T>ResultHandler<T> getInstance(CommonResult<T> result, ResultHandler<T> resultHandler) {
|
return new ResultHandler<>(result, resultHandler);
|
}
|
|
/**
|
* 获取data
|
* @return data
|
*/
|
public E getDate() {
|
if (result == null) {
|
if (nullProcess != null) {
|
return nullProcess.get();
|
}
|
return null;
|
}
|
Integer code = result.getCode();
|
if (code.equals(CommonResultEmnu.OK.getCode())) {
|
E data = result.getData();
|
if (data == null && dataNullProcess != null) {
|
return this.dataNullProcess.get();
|
}
|
return data;
|
}
|
if (errorProcessList == null) {
|
return null;
|
}
|
for (ErrorProcess<E> process : this.errorProcessList) {
|
if (code.equals(process.getCode())) {
|
return process.getSupplier().get();
|
}
|
}
|
return null;
|
}
|
|
public static void main(String[] args) {
|
// Maps.EntryTransformer<BaseServiceClient, String, CommonResult<String>> getAllManagerByOrgCode = BaseServiceClient::getAllManagerByOrgCode;
|
// ClientUtil.getDate(BaseServiceClient.class, BaseServiceClient::getAllManagerByOrgCode, null);
|
ResultHandler.getInstance(new CommonResult<>())
|
.setNullProcess(() -> {
|
throw new RuntimeException("");
|
})
|
.setDataNullProcess(() -> "")
|
.appendError(1, () -> "")
|
.getDate();
|
}
|
|
/**
|
* 功能描述:错误处理逻辑
|
* @author xuekang
|
* @date 2024-04-16 13:26
|
* @version 1.0
|
*/
|
@Data
|
@AllArgsConstructor
|
private static class ErrorProcess<E> {
|
/** 错误码 */
|
private Integer code;
|
/** 处理函数 */
|
private Supplier<E> supplier;
|
}
|
}
|