Commit 489d7810 authored by liaozan's avatar liaozan 🏀

Polish

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