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 data类型 */ public class ResultHandler { /** CommonResult */ private final CommonResult result; /** 设置空处理函数 */ private Supplier nullProcess; /** 设置data空处理函数 */ private Supplier dataNullProcess; /** 错误码处理函数 */ private List> errorProcessList; public ResultHandler(CommonResult result) { this.result = result; } public ResultHandler(CommonResult result, ResultHandler resultHandler) { this.result = result; this.nullProcess = resultHandler.nullProcess; this.dataNullProcess = resultHandler.dataNullProcess; this.errorProcessList = resultHandler.errorProcessList; } /** * 设置空处理函数 * @param nullProcess 空处理函数 * @return ResultHandler */ public ResultHandler setNullProcess(Supplier nullProcess) { this.nullProcess = nullProcess; return this; } /** * 设置data空处理函数 * @param dataNullProcess data 空处理函数 * @return ResultHandler */ public ResultHandler setDataNullProcess(Supplier dataNullProcess) { this.dataNullProcess = dataNullProcess; return this; } /** * 错误码处理函数 * @param code 错误码 * @param errorProcessList 函数 * @return ResultHandler */ public ResultHandler appendError(Integer code, Supplier 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 data类型 */ public static ResultHandler getInstance(CommonResult result) { return new ResultHandler<>(result); } /** * 获取 * @param result CommonResult * @param resultHandler ResultHandler * @return ResultHandler * @param date类型 */ public static ResultHandler getInstance(CommonResult result, ResultHandler 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 process : this.errorProcessList) { if (code.equals(process.getCode())) { return process.getSupplier().get(); } } return null; } public static void main(String[] args) { // Maps.EntryTransformer> 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 { /** 错误码 */ private Integer code; /** 处理函数 */ private Supplier supplier; } }