|
@@ -9,9 +9,9 @@ import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.IService;
|
|
|
import com.ruoyi.common.core.domain.BaseIdPo;
|
|
|
-import com.ruoyi.common.exception.ServiceException;
|
|
|
import com.ruoyi.common.utils.AttributeAssignBuilder;
|
|
|
import com.ruoyi.common.utils.wrapper.IWrapper;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.io.Serializable;
|
|
|
import java.util.Collection;
|
|
@@ -23,7 +23,7 @@ import java.util.function.Consumer;
|
|
|
import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
-@SuppressWarnings({"unused", "unchecked"})
|
|
|
+@SuppressWarnings({"unused"})
|
|
|
public interface BaseService<T extends BaseIdPo> extends IService<T> {
|
|
|
|
|
|
default <V> AttributeAssignBuilder<T, V> attributeAssignBuilder(List<V> list) {
|
|
@@ -38,15 +38,6 @@ public interface BaseService<T extends BaseIdPo> extends IService<T> {
|
|
|
return IWrapper.getWrapper();
|
|
|
}
|
|
|
|
|
|
- // =========================================================
|
|
|
- // mp方法扩展
|
|
|
- // =========================================================
|
|
|
-
|
|
|
- default void assertExecutable(Consumer<LambdaQueryWrapper<T>> consumer, String errorMsgTemplate, Object... params) {
|
|
|
- long count = count(consumer);
|
|
|
- Assert.equals(count, 0L, errorMsgTemplate, params);
|
|
|
- }
|
|
|
-
|
|
|
default Map<Long, T> byIdsToMap(Collection<? extends Serializable> ids) {
|
|
|
return listByIds(ids).stream().collect(Collectors.toMap(BaseIdPo::getId, Function.identity()));
|
|
|
}
|
|
@@ -63,25 +54,45 @@ public interface BaseService<T extends BaseIdPo> extends IService<T> {
|
|
|
return list(Wrappers.<T>lambdaQuery().func(ObjectUtil.isNotEmpty(consumer), consumer));
|
|
|
}
|
|
|
|
|
|
+ default Page<T> page(Page<T> page, Consumer<LambdaQueryWrapper<T>> consumer) {
|
|
|
+ return page(page, Wrappers.<T>lambdaQuery().func(ObjectUtil.isNotEmpty(consumer), consumer));
|
|
|
+ }
|
|
|
+
|
|
|
default List<Map<String, Object>> listMaps(Consumer<LambdaQueryWrapper<T>> consumer) {
|
|
|
return listMaps(Wrappers.<T>lambdaQuery().func(ObjectUtil.isNotEmpty(consumer), consumer));
|
|
|
}
|
|
|
|
|
|
+ default <K> List<K> listObject(SFunction<T, K> mapper) {
|
|
|
+ return listObject(mapper, null);
|
|
|
+ }
|
|
|
+
|
|
|
default <K> List<K> listObject(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(Objects::nonNull).distinct().collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
+ default <K, V> Map<K, V> mapKV(SFunction<T, K> kFun, SFunction<T, V> vFun) {
|
|
|
+ return mapKV(kFun, vFun, null);
|
|
|
+ }
|
|
|
+
|
|
|
default <K, V> Map<K, V> mapKV(SFunction<T, K> kFun, SFunction<T, V> vFun, Consumer<LambdaQueryWrapper<T>> consumer) {
|
|
|
List<T> list = list(Wrappers.<T>lambdaQuery().select(kFun, vFun).func(ObjectUtil.isNotEmpty(consumer), consumer));
|
|
|
return list.stream().collect(Collectors.toMap(kFun, vFun, (v1, v2) -> v2));
|
|
|
}
|
|
|
|
|
|
+ default <K> Map<K, T> mapKEntity(SFunction<T, K> kFun) {
|
|
|
+ return mapKEntity(kFun, null);
|
|
|
+ }
|
|
|
+
|
|
|
default <K> Map<K, T> mapKEntity(SFunction<T, K> kFun, Consumer<LambdaQueryWrapper<T>> consumer) {
|
|
|
List<T> list = list(Wrappers.<T>lambdaQuery().func(ObjectUtil.isNotEmpty(consumer), consumer));
|
|
|
return list.stream().collect(Collectors.toMap(kFun, item -> item, (v1, v2) -> v2));
|
|
|
}
|
|
|
|
|
|
+ default <K> Map<K, List<T>> mapKGroup(SFunction<T, K> kFun) {
|
|
|
+ return mapKGroup(kFun, null);
|
|
|
+ }
|
|
|
+
|
|
|
default <K> Map<K, List<T>> mapKGroup(SFunction<T, K> kFun, Consumer<LambdaQueryWrapper<T>> consumer) {
|
|
|
List<T> list = list(Wrappers.<T>lambdaQuery().func(ObjectUtil.isNotEmpty(consumer), consumer));
|
|
|
return list.stream().collect(Collectors.groupingBy(kFun));
|
|
@@ -95,43 +106,46 @@ public interface BaseService<T extends BaseIdPo> extends IService<T> {
|
|
|
return remove(Wrappers.<T>lambdaQuery().func(ObjectUtil.isNotEmpty(consumer), consumer));
|
|
|
}
|
|
|
|
|
|
- default void nameDuplication(SFunction<T, ?> column, Object value, String errMsg) {
|
|
|
+ /**
|
|
|
+ * 验证字段是否重复
|
|
|
+ */
|
|
|
+ default void assertExecutable(Consumer<LambdaQueryWrapper<T>> consumer, String errMsg, Object... params) {
|
|
|
+ long count = count(consumer);
|
|
|
+ Assert.equals(count, 0L, errMsg, params);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证字段是否重复
|
|
|
+ */
|
|
|
+ default void nameDuplication(SFunction<T, ?> column, Object value, String errMsg, Object... params) {
|
|
|
long count = count(q -> q.eq(column, value));
|
|
|
- if (count != 0) {
|
|
|
- throw new ServiceException(errMsg);
|
|
|
- }
|
|
|
+ Assert.equals(count, 0L, errMsg, params);
|
|
|
}
|
|
|
|
|
|
- default void nameDuplication(SFunction<T, ?> column, Object value, Long id, String errMsg) {
|
|
|
+ /**
|
|
|
+ * 验证字段是否重复
|
|
|
+ */
|
|
|
+ default void nameDuplication(SFunction<T, ?> column, Object value, Long id, String errMsg, Object... params) {
|
|
|
long count = count(q -> q.eq(column, value).ne(T::getId, id));
|
|
|
- if (count != 0) {
|
|
|
- throw new ServiceException(errMsg);
|
|
|
- }
|
|
|
+ Assert.equals(count, 0L, errMsg, params);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取添加过的某个字段的去重列表
|
|
|
*/
|
|
|
default <V> List<V> getDistinctList(SFunction<T, V> column) {
|
|
|
- return list(Wrappers.<T>lambdaQuery().select(column))
|
|
|
- .stream().map(column).filter(Objects::nonNull).distinct().collect(Collectors.toList());
|
|
|
+ return list(Wrappers.<T>lambdaQuery().select(column)).stream()
|
|
|
+ .map(column).filter(Objects::nonNull).distinct().collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 编辑关联表
|
|
|
*/
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
default void editLinked(List<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));
|
|
|
saveOrUpdateBatch(list);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 分页
|
|
|
- */
|
|
|
- default Page<T> page(Page<T> page, Consumer<LambdaQueryWrapper<T>> consumer) {
|
|
|
- return page(page, Wrappers.<T>lambdaQuery().func(ObjectUtil.isNotEmpty(consumer), consumer));
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
}
|