123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- package com.fjhx.base;
- import cn.hutool.core.util.ObjectUtil;
- import com.baomidou.mybatisplus.core.conditions.Wrapper;
- 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 com.fjhx.utils.PageUtil;
- import com.fjhx.utils.wrapperUtil.IWrapper;
- import org.springblade.core.secure.utils.AuthUtil;
- import java.util.*;
- import java.util.function.Consumer;
- import java.util.stream.Collectors;
- public interface BaseService<T> extends IService<T> {
- /**
- * 获取分页
- */
- default Page<T> createPage(Map<String, ?> condition) {
- return PageUtil.createPage(condition);
- }
- /**
- * 获取分页
- */
- default Page<Map<String, Object>> createPageMap(Map<String, ?> condition) {
- return PageUtil.createPageMap(condition);
- }
- /**
- * 执行分页查询
- *
- * @param condition 条件
- * @param queryWrapper wrapper
- * @return 分页后的对象
- */
- default Page<T> page(Map<String, ?> condition, Wrapper<T> queryWrapper) {
- Page<T> page = createPage(condition);
- return page(page, queryWrapper);
- }
- /**
- * 执行分页查询
- *
- * @param condition 条件
- * @param queryWrapper wrapper
- * @return 分页后的对象
- */
- default Page<Map<String, Object>> pageMaps(Map<String, ?> condition, Wrapper<T> queryWrapper) {
- Page<Map<String, Object>> page = createPageMap(condition);
- return pageMaps(page, queryWrapper);
- }
- /**
- * 获取添加过的某个字段的去重列表
- */
- default List<?> getDistinctList(SFunction<T, ?> column) {
- return list(Wrappers.<T>lambdaQuery().select(column))
- .stream().map(column).filter(Objects::nonNull).distinct().collect(Collectors.toList());
- }
- /**
- * 条件查询
- */
- default List<T> list(SFunction<T, ?> column, Object val) {
- return lambdaQuery().eq(column, val).list();
- }
- /**
- * 条件查询
- */
- default List<Map<String, Object>> listMaps(SFunction<T, ?> column, Object val) {
- return listMaps(Wrappers.<T>lambdaQuery().eq(column, val));
- }
- /**
- * 条件查询
- */
- default T getOne(SFunction<T, ?> column, Object val) {
- return lambdaQuery().eq(column, val).last("limit 1").one();
- }
- /**
- * 条件查询
- */
- default Long count(SFunction<T, ?> column, Object val) {
- return lambdaQuery().eq(column, val).count();
- }
- default long count(Consumer<IWrapper<T>> consumer) {
- return count(IWrapper.<T>getWrapper().func(consumer));
- }
- default T getOne(Consumer<IWrapper<T>> consumer) {
- return getOne(IWrapper.<T>getWrapper().func(consumer).last("limit 1"));
- }
- default List<T> list(Consumer<IWrapper<T>> consumer) {
- return list(IWrapper.<T>getWrapper().func(consumer));
- }
- default List<Map<String, Object>> listMaps(Consumer<IWrapper<T>> consumer) {
- return listMaps(IWrapper.<T>getWrapper().func(consumer));
- }
- default <V> List<V> listObj(SFunction<T, V> mapper, Consumer<LambdaQueryWrapper<T>> consumer) {
- return list(Wrappers.<T>lambdaQuery().select(mapper).func(consumer))
- .stream().map(mapper).filter(Objects::nonNull).distinct().collect(Collectors.toList());
- }
- default boolean update(Consumer<LambdaUpdateWrapper<T>> consumer) {
- return update(Wrappers.<T>lambdaUpdate().func(consumer));
- }
- default boolean remove(Consumer<LambdaQueryWrapper<T>> consumer) {
- return remove(Wrappers.<T>lambdaQuery().func(consumer));
- }
- /**
- * 按指定规则删除
- */
- default boolean remove(SFunction<T, ?> column, Object val) {
- return remove(Wrappers.<T>lambdaQuery().eq(column, val));
- }
- @Override
- default boolean save(T entity) {
- setCreateInfo(entity);
- return IService.super.save(entity);
- }
- @Override
- default boolean saveBatch(Collection<T> entityList) {
- entityList.forEach(this::setCreateInfo);
- return IService.super.saveBatch(entityList);
- }
- @Override
- default boolean saveOrUpdateBatch(Collection<T> entityList) {
- entityList.forEach(item -> {
- if (item instanceof BaseEntity) {
- if (ObjectUtil.isEmpty(((BaseEntity) item).getId())) {
- setCreateInfo(item);
- } else {
- setUpdateInfo(item);
- }
- }
- });
- return IService.super.saveOrUpdateBatch(entityList);
- }
- @Override
- default boolean updateById(T entity) {
- setUpdateInfo(entity);
- return IService.super.updateById(entity);
- }
- @Override
- default boolean update(T entity, Wrapper<T> updateWrapper) {
- setUpdateInfo(entity);
- return IService.super.update(entity, updateWrapper);
- }
- @Override
- default boolean updateBatchById(Collection<T> entityList) {
- entityList.forEach(this::setUpdateInfo);
- return IService.super.updateBatchById(entityList);
- }
- @Override
- default boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper) {
- if (entity instanceof BaseEntity) {
- if (ObjectUtil.isEmpty(((BaseEntity) entity).getId())) {
- setCreateInfo(entity);
- } else {
- setUpdateInfo(entity);
- }
- }
- return IService.super.saveOrUpdate(entity, updateWrapper);
- }
- /**
- * 调新增方法时默认传入创建人、创建时间、租户id
- */
- default void setCreateInfo(T entity) {
- if (entity instanceof BaseEntity) {
- Date date = new Date();
- Long userId = AuthUtil.getUserId();
- ((BaseEntity) entity).setTenantId(AuthUtil.getTenantId());
- ((BaseEntity) entity).setCreateUser(userId);
- ((BaseEntity) entity).setCreateTime(date);
- ((BaseEntity) entity).setUpdateUser(userId);
- ((BaseEntity) entity).setUpdateTime(date);
- }
- }
- /**
- * 调编辑方法时默认传入更新人、更新时间
- */
- default void setUpdateInfo(T entity) {
- if (entity instanceof BaseEntity) {
- ((BaseEntity) entity).setTenantId(null);
- ((BaseEntity) entity).setCreateUser(null);
- ((BaseEntity) entity).setCreateTime(null);
- ((BaseEntity) entity).setUpdateUser(AuthUtil.getUserId());
- ((BaseEntity) entity).setUpdateTime(new Date());
- }
- }
- }
|