Commit 489d7810 authored by liaozan's avatar liaozan 🏀

Polish

parent 9354b293
package com.schbrain.common.util;
import cn.hutool.core.collection.ListUtil;
import com.google.common.collect.Maps;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Function;
......@@ -55,23 +54,49 @@ public class TreeUtils {
return doBuildTree(keyExtractor, childrenSetter, childMapper, parentWithSubNodes, childrenComparator, parentId);
}
public static <T, E> List<E> buildNodeList(List<T> treeList,
Function<T, List<T>> childrenGetter,
Function<T, E> mapper) {
public static <T, E> List<E> getParents(E id, List<T> nodeList, Function<T, E> keyMapper, Function<T, E> parentMapper, boolean includeSelf) {
// toMap 不允许 value 为空,当 parentId 为空时,单独处理下
Map<E, E> parentMap = Maps.newHashMapWithExpectedSize(nodeList.size());
for (T node : nodeList) {
parentMap.put(keyMapper.apply(node), parentMapper.apply(node));
}
return getParents(id, parentMap, includeSelf);
}
public static <T> List<T> getParents(T id, Map<T, T> parentMap, boolean includeSelf) {
List<T> parentIds = new LinkedList<>();
if (includeSelf) {
parentIds.add(id);
}
if (MapUtils.isEmpty(parentMap)) {
return parentIds;
}
return getParents(id, parentMap, parentIds);
}
public static <T, E> List<E> buildNodeList(List<T> tree, Function<T, List<T>> childGetter, Function<T, E> mapper) {
List<E> nodes = new ArrayList<>();
doBuildNodeList(treeList, childrenGetter, mapper, nodes);
if (CollectionUtils.isEmpty(tree)) {
return nodes;
}
doBuildNodeList(tree, childGetter, mapper, nodes);
return nodes;
}
private static <E, T> void doBuildNodeList(List<T> treeList,
Function<T, List<T>> childrenGetter,
Function<T, E> mapper,
List<E> nodes) {
if (CollectionUtils.isEmpty(treeList)) {
return;
}
nodes.addAll(StreamUtils.toList(treeList, mapper));
treeList.forEach(tree -> doBuildNodeList(childrenGetter.apply(tree), childrenGetter, mapper, nodes));
private static <E, T> void doBuildNodeList(List<T> tree, Function<T, List<T>> childGetter, Function<T, E> mapper, List<E> nodes) {
tree.forEach(node -> {
nodes.add(mapper.apply(node));
doBuildNodeList(childGetter.apply(node), childGetter, mapper, nodes);
});
}
private static <T> List<T> getParents(T id, Map<T, T> parentMap, List<T> parentIds) {
T parentId = parentMap.get(id);
if (parentId == null) {
return parentIds;
}
parentIds.add(0, parentId);
return getParents(parentId, parentMap, parentIds);
}
private static <E, K, T> List<E> doBuildTree(Function<T, K> keyExtractor,
......
......@@ -24,7 +24,7 @@ public class ExceptionHandingConfiguration {
@Bean
@ConditionalOnMissingBean
public ExceptionTranslator<ResponseDTO<String>> defaultExceptionTranslator() {
public ExceptionTranslator<ResponseDTO<Void>> defaultExceptionTranslator() {
return new DefaultExceptionTranslator();
}
......
......@@ -10,12 +10,12 @@ import org.springframework.core.Ordered;
* @author liaozan
* @since 2023-06-01
*/
public class DefaultExceptionTranslator implements ExceptionTranslator<ResponseDTO<String>> {
public class DefaultExceptionTranslator implements ExceptionTranslator<ResponseDTO<Void>> {
private final boolean isProduction = EnvUtils.isProduction();
@Override
public ResponseDTO<String> translate(Throwable throwable, int code, int action, String message) {
public ResponseDTO<Void> translate(Throwable throwable, int code, int action, String message) {
if (throwable instanceof BaseException) {
return ResponseDTO.error((BaseException) throwable);
}
......
......@@ -20,7 +20,7 @@ public class BaseEntityWithLogicDelete extends BaseEntity {
*/
@TableLogic
@TableField(value = MybatisConstants.DELETED, select = false)
protected boolean deleted;
protected Boolean deleted;
/**
* 逻辑删除版本
......
package com.schbrain.framework.autoconfigure.mybatis.base;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;
public interface BaseService<T extends BaseEntity> extends IService<T> {
......@@ -32,7 +32,7 @@ public interface BaseService<T extends BaseEntity> extends IService<T> {
/**
* 根据 id 获取
*/
<V> Map<Long, V> getMapByIds(Collection<Long> ids, Function<T, V> mapper);
<V> Map<Long, V> getMapByIds(Collection<Long> ids, SFunction<T, V> mapper);
/**
* 根据业务主键获取记录
......@@ -66,7 +66,7 @@ public interface BaseService<T extends BaseEntity> extends IService<T> {
/**
* 根据业务主键获取
*/
<K, V> Map<K, V> getMapByBizIds(Collection<K> bizIds, Function<T, V> mapper);
<K, V> Map<K, V> getMapByBizIds(Collection<K> bizIds, SFunction<T, V> mapper);
/**
* 根据 id 更新,null 会被更新为 null
......
......@@ -2,6 +2,7 @@ package com.schbrain.framework.autoconfigure.mybatis.base;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.schbrain.common.exception.BaseException;
......@@ -22,7 +23,6 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;
/**
......@@ -34,6 +34,10 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
@Nullable
private BizIdColumnField bizIdColumnField;
private static <T> SFunction<T, T> identity() {
return any -> any;
}
@Override
public T getById(Serializable id) {
return getById((Long) id, false);
......@@ -59,15 +63,17 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
@Override
public Map<Long, T> getMapByIds(Collection<Long> ids) {
return getMapByIds(ids, Function.identity());
return getMapByIds(ids, identity());
}
@Override
public <V> Map<Long, V> getMapByIds(Collection<Long> ids, Function<T, V> mapper) {
public <V> Map<Long, V> getMapByIds(Collection<Long> ids, SFunction<T, V> mapper) {
if (isEmpty(ids)) {
return Collections.emptyMap();
}
return StreamUtils.toMap(listByIds(ids), T::getId, mapper);
// noinspection unchecked
List<T> dataList = lambdaQuery().select(T::getId, mapper).in(T::getId, ids).list();
return StreamUtils.toMap(dataList, T::getId, mapper);
}
@Override
......@@ -86,8 +92,7 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
@Override
public T getByBizId(Object bizId, Supplier<? extends RuntimeException> notFoundSupplier) {
assertBidColumnFieldExist();
T entity = query().eq(bizIdColumnField.getColumnName(), bizId).one();
T entity = query().eq(getBidColumnField().getColumnName(), bizId).one();
if (entity == null && notFoundSupplier != null) {
throw notFoundSupplier.get();
}
......@@ -96,25 +101,24 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
@Override
public <K> List<T> listByBizIds(Collection<K> bizIds) {
assertBidColumnFieldExist();
if (isEmpty(bizIds)) {
return Collections.emptyList();
}
return query().in(bizIdColumnField.getColumnName(), bizIds).list();
return query().in(getBidColumnField().getColumnName(), bizIds).list();
}
@Override
public <K> Map<K, T> getMapByBizIds(Collection<K> bizIds) {
return getMapByBizIds(bizIds, Function.identity());
return getMapByBizIds(bizIds, identity());
}
@Override
public <K, V> Map<K, V> getMapByBizIds(Collection<K> bizIds, Function<T, V> mapper) {
assertBidColumnFieldExist();
public <K, V> Map<K, V> getMapByBizIds(Collection<K> bizIds, SFunction<T, V> mapper) {
if (isEmpty(bizIds)) {
return Collections.emptyMap();
}
return StreamUtils.toMap(listByBizIds(bizIds), entity -> bizIdColumnField.getValue(entity), mapper);
// How to get the mapper fieldName ?
return StreamUtils.toMap(listByBizIds(bizIds), entity -> getBidColumnField().getValue(entity), mapper);
}
@Override
......@@ -150,10 +154,11 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
}, field -> field.isAnnotationPresent(BizId.class));
}
private void assertBidColumnFieldExist() {
protected BizIdColumnField getBidColumnField() {
if (bizIdColumnField == null) {
throw new BaseException(String.format("@BizId not exist in Class: \"%s\"", entityClass.getName()));
}
return bizIdColumnField;
}
private String getUpdateByIdWithNullStatementId(Class<M> mapperClass) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment