1.8-接口参数校验

90 阅读1分钟

参数校验

public interface ExecutorWrapper {

    Logger logger = LoggerFactory.getLogger(ExecutorWrapper.class);

    default void checkParam(Object... params) {
        Stream.of(params).forEach(param -> {
            if (param == null) {
                throw new RomeoException(I18nClassUtil.newI18nObject(ErrorCode.PARAM_ERROR));
            }
            if (param instanceof Long) {
                if (param == null || ((Long) param) <= 0) {
                    throw new RomeoException(I18nClassUtil.newI18nObject(ErrorCode.PARAM_ERROR));
                }
            }
            if (param instanceof Integer) {
                if (param == null) {
                    throw new RomeoException(I18nClassUtil.newI18nObject(ErrorCode.PARAM_ERROR));
                }
            }
            if (param instanceof String) {
                if (StringUtils.isBlank((String) param)) {
                    throw new RomeoException(I18nClassUtil.newI18nObject(ErrorCode.PARAM_ERROR));
                }
            }
            if (param instanceof List) {
                if (CollectionUtils.isEmpty((List) param)) {
                    throw new RomeoException(I18nClassUtil.newI18nObject(ErrorCode.PARAM_ERROR));
                }
            }
        });
    }


    /**
     * 判断参数不为空
     * String != null && String != ""
     * Number != null && Number > 0
     * Collection != null && Collection.size > 0
     *存在参数为空 则抛出异常
     * @param params
     */
    default void paramNotNull(Object... params) {
        Stream.of(params).forEach(item -> {
            if (item == null) {
                throw new RomeoException(I18nClassUtil.newI18nObject(ErrorCode.PARAM_ERROR));
            } else if (item instanceof Number) {
                Number n = (Number) item;
                if (n.doubleValue() <= 0) {
                    throw new RomeoException(I18nClassUtil.newI18nObject(ErrorCode.PARAM_ERROR));
                }
            } else if (item instanceof String) {
                String s = (String) item;
                if ("".equals(s)) {
                    throw new RomeoException(I18nClassUtil.newI18nObject(ErrorCode.PARAM_ERROR));
                }
            } else if (item instanceof Collection) {
                Collection c = (Collection) item;
                if (c.isEmpty()) {
                    throw new RomeoException(I18nClassUtil.newI18nObject(ErrorCode.PARAM_ERROR));
                }
            }
        });
    }

    @Transactional
    default <T extends IResult> T execWrapper(Supplier<T> supplier, String bizDesc, Class<T> tClass, Object... params) {
        T result = null;
        try {
            logger.info("{}->参数:{}", bizDesc, StringUtils.join(params, "->"));
            result = tClass.newInstance();
            if (supplier == null) {
                return result;
            }
            result = supplier.get();
        } catch (RomeoException e) {
            logger.warn("业务异常->" + bizDesc, e);
            result.getBaseResult().isSuccess=false;
            result.getBaseResult().setError(e.getErrorCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("系统异常->" + bizDesc, e);
            result.getBaseResult().isSuccess=false;
            result.getBaseResult().setError(ErrorCode.SYSTEM_ERROR);
        }
        return result;
    }

}