|
@@ -0,0 +1,104 @@
|
|
|
+package org.dromara.base.domain;
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.baomidou.mybatisplus.extension.service.IService;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.io.Serializable;
|
|
|
+import java.util.Collection;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.function.Consumer;
|
|
|
+import java.util.function.Function;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@SuppressWarnings({"unused", "unchecked", "UnusedReturnValue"})
|
|
|
+public interface BaseService<T extends BaseIdPo> extends IService<T> {
|
|
|
+
|
|
|
+ // =========================================================
|
|
|
+ // mp方法扩展
|
|
|
+ // =========================================================
|
|
|
+
|
|
|
+ default Map<Long, T> byIdsToMap(Collection<? extends Serializable> ids) {
|
|
|
+ return listByIds(ids).stream().collect(Collectors.toMap(BaseIdPo::getId, Function.identity()));
|
|
|
+ }
|
|
|
+
|
|
|
+ default List<Map<String, Object>> listMaps(Consumer<LambdaQueryWrapper<T>> consumer) {
|
|
|
+ return listMaps(Wrappers.<T>lambdaQuery().func(ObjectUtil.isNotEmpty(consumer), consumer));
|
|
|
+ }
|
|
|
+
|
|
|
+ default <K> List<K> fieldList(SFunction<T, K> mapper, Consumer<LambdaQueryWrapper<T>> consumer) {
|
|
|
+ List<T> list = list(Wrappers.<T>lambdaQuery().select(mapper).func(ObjectUtil.isNotEmpty(consumer), consumer));
|
|
|
+ return list.stream().map(mapper).filter(ObjectUtil::isNotEmpty).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ default <K> Set<K> fieldSet(SFunction<T, K> mapper, Consumer<LambdaQueryWrapper<T>> consumer) {
|
|
|
+ List<T> list = list(Wrappers.<T>lambdaQuery().select(mapper).func(ObjectUtil.isNotEmpty(consumer), consumer));
|
|
|
+ return list.stream().map(mapper).filter(ObjectUtil::isNotEmpty).collect(Collectors.toSet());
|
|
|
+ }
|
|
|
+
|
|
|
+ default long count(Consumer<LambdaQueryWrapper<T>> consumer) {
|
|
|
+ return count(Wrappers.<T>lambdaQuery().func(ObjectUtil.isNotEmpty(consumer), consumer));
|
|
|
+ }
|
|
|
+
|
|
|
+ default T getOne(Consumer<LambdaQueryWrapper<T>> consumer) {
|
|
|
+ return getOne(Wrappers.<T>lambdaQuery().func(ObjectUtil.isNotEmpty(consumer), consumer).last("limit 1"));
|
|
|
+ }
|
|
|
+
|
|
|
+ default Page<T> page(Page<T> page, Consumer<LambdaQueryWrapper<T>> consumer) {
|
|
|
+ return page(page, Wrappers.<T>lambdaQuery().func(ObjectUtil.isNotEmpty(consumer), consumer));
|
|
|
+ }
|
|
|
+
|
|
|
+ default Page<T> page(BaseSelectDto dto, Consumer<LambdaQueryWrapper<T>> consumer) {
|
|
|
+ return page(dto.getPage(), Wrappers.<T>lambdaQuery().func(ObjectUtil.isNotEmpty(consumer), consumer));
|
|
|
+ }
|
|
|
+
|
|
|
+ default List<T> list(Consumer<LambdaQueryWrapper<T>> consumer) {
|
|
|
+ return list(Wrappers.<T>lambdaQuery().func(ObjectUtil.isNotEmpty(consumer), consumer));
|
|
|
+ }
|
|
|
+
|
|
|
+ default boolean update(Consumer<LambdaUpdateWrapper<T>> consumer) {
|
|
|
+ return update(Wrappers.<T>lambdaUpdate().func(ObjectUtil.isNotEmpty(consumer), consumer));
|
|
|
+ }
|
|
|
+
|
|
|
+ default boolean update(T t, Consumer<LambdaUpdateWrapper<T>> consumer) {
|
|
|
+ return update(t, Wrappers.<T>lambdaUpdate().func(ObjectUtil.isNotEmpty(consumer), consumer));
|
|
|
+ }
|
|
|
+
|
|
|
+ default boolean remove(Consumer<LambdaQueryWrapper<T>> consumer) {
|
|
|
+ return remove(Wrappers.<T>lambdaQuery().func(ObjectUtil.isNotEmpty(consumer), consumer));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ default boolean saveBatchEx(Collection<? extends T> entityList) {
|
|
|
+ return saveBatch((List<T>) entityList, DEFAULT_BATCH_SIZE);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ default boolean updateBatchExById(Collection<? extends T> entityList) {
|
|
|
+ return updateBatchById((List<T>) entityList, DEFAULT_BATCH_SIZE);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ default boolean saveOrUpdateBatchEx(Collection<? extends T> entityList) {
|
|
|
+ return saveOrUpdateBatch((List<T>) entityList, DEFAULT_BATCH_SIZE);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 编辑关联表
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ default void editLinked(List<? extends T> list, SFunction<T, Long> getMasterIdFun, Long masterId) {
|
|
|
+ List<Long> idList = list.stream().map(BaseIdPo::getId).filter(Objects::nonNull).collect(Collectors.toList());
|
|
|
+ remove(q -> q.eq(getMasterIdFun, masterId).notIn(ObjectUtil.isNotEmpty(idList), BaseIdPo::getId, idList));
|
|
|
+ saveOrUpdateBatchEx(list);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|