BaseService.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package com.fjhx.base;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import com.baomidou.mybatisplus.core.conditions.Wrapper;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  6. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  7. import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
  8. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  9. import com.baomidou.mybatisplus.extension.service.IService;
  10. import com.fjhx.utils.PageUtil;
  11. import com.fjhx.utils.wrapperUtil.IWrapper;
  12. import org.springblade.core.secure.utils.AuthUtil;
  13. import java.util.*;
  14. import java.util.function.Consumer;
  15. import java.util.stream.Collectors;
  16. public interface BaseService<T> extends IService<T> {
  17. /**
  18. * 获取分页
  19. */
  20. default Page<T> createPage(Map<String, ?> condition) {
  21. return PageUtil.createPage(condition);
  22. }
  23. /**
  24. * 获取分页
  25. */
  26. default Page<Map<String, Object>> createPageMap(Map<String, ?> condition) {
  27. return PageUtil.createPageMap(condition);
  28. }
  29. /**
  30. * 执行分页查询
  31. *
  32. * @param condition 条件
  33. * @param queryWrapper wrapper
  34. * @return 分页后的对象
  35. */
  36. default Page<T> page(Map<String, ?> condition, Wrapper<T> queryWrapper) {
  37. Page<T> page = createPage(condition);
  38. return page(page, queryWrapper);
  39. }
  40. /**
  41. * 执行分页查询
  42. *
  43. * @param condition 条件
  44. * @param queryWrapper wrapper
  45. * @return 分页后的对象
  46. */
  47. default Page<Map<String, Object>> pageMaps(Map<String, ?> condition, Wrapper<T> queryWrapper) {
  48. Page<Map<String, Object>> page = createPageMap(condition);
  49. return pageMaps(page, queryWrapper);
  50. }
  51. /**
  52. * 获取添加过的某个字段的去重列表
  53. */
  54. default List<?> getDistinctList(SFunction<T, ?> column) {
  55. return list(Wrappers.<T>lambdaQuery().select(column))
  56. .stream().map(column).filter(Objects::nonNull).distinct().collect(Collectors.toList());
  57. }
  58. /**
  59. * 条件查询
  60. */
  61. default List<T> list(SFunction<T, ?> column, Object val) {
  62. return lambdaQuery().eq(column, val).list();
  63. }
  64. /**
  65. * 条件查询
  66. */
  67. default List<Map<String, Object>> listMaps(SFunction<T, ?> column, Object val) {
  68. return listMaps(Wrappers.<T>lambdaQuery().eq(column, val));
  69. }
  70. /**
  71. * 条件查询
  72. */
  73. default T getOne(SFunction<T, ?> column, Object val) {
  74. return lambdaQuery().eq(column, val).last("limit 1").one();
  75. }
  76. /**
  77. * 条件查询
  78. */
  79. default Long count(SFunction<T, ?> column, Object val) {
  80. return lambdaQuery().eq(column, val).count();
  81. }
  82. default long count(Consumer<IWrapper<T>> consumer) {
  83. return count(IWrapper.<T>getWrapper().func(consumer));
  84. }
  85. default T getOne(Consumer<IWrapper<T>> consumer) {
  86. return getOne(IWrapper.<T>getWrapper().func(consumer).last("limit 1"));
  87. }
  88. default List<T> list(Consumer<IWrapper<T>> consumer) {
  89. return list(IWrapper.<T>getWrapper().func(consumer));
  90. }
  91. default List<Map<String, Object>> listMaps(Consumer<IWrapper<T>> consumer) {
  92. return listMaps(IWrapper.<T>getWrapper().func(consumer));
  93. }
  94. default <V> List<V> listObj(SFunction<T, V> mapper, Consumer<LambdaQueryWrapper<T>> consumer) {
  95. return list(Wrappers.<T>lambdaQuery().select(mapper).func(consumer))
  96. .stream().map(mapper).filter(Objects::nonNull).distinct().collect(Collectors.toList());
  97. }
  98. default boolean update(Consumer<LambdaUpdateWrapper<T>> consumer) {
  99. return update(Wrappers.<T>lambdaUpdate().func(consumer));
  100. }
  101. default boolean remove(Consumer<LambdaQueryWrapper<T>> consumer) {
  102. return remove(Wrappers.<T>lambdaQuery().func(consumer));
  103. }
  104. /**
  105. * 按指定规则删除
  106. */
  107. default boolean remove(SFunction<T, ?> column, Object val) {
  108. return remove(Wrappers.<T>lambdaQuery().eq(column, val));
  109. }
  110. @Override
  111. default boolean save(T entity) {
  112. setCreateInfo(entity);
  113. return IService.super.save(entity);
  114. }
  115. @Override
  116. default boolean saveBatch(Collection<T> entityList) {
  117. entityList.forEach(this::setCreateInfo);
  118. return IService.super.saveBatch(entityList);
  119. }
  120. @Override
  121. default boolean saveOrUpdateBatch(Collection<T> entityList) {
  122. entityList.forEach(item -> {
  123. if (item instanceof BaseEntity) {
  124. if (ObjectUtil.isEmpty(((BaseEntity) item).getId())) {
  125. setCreateInfo(item);
  126. } else {
  127. setUpdateInfo(item);
  128. }
  129. }
  130. });
  131. return IService.super.saveOrUpdateBatch(entityList);
  132. }
  133. @Override
  134. default boolean updateById(T entity) {
  135. setUpdateInfo(entity);
  136. return IService.super.updateById(entity);
  137. }
  138. @Override
  139. default boolean update(T entity, Wrapper<T> updateWrapper) {
  140. setUpdateInfo(entity);
  141. return IService.super.update(entity, updateWrapper);
  142. }
  143. @Override
  144. default boolean updateBatchById(Collection<T> entityList) {
  145. entityList.forEach(this::setUpdateInfo);
  146. return IService.super.updateBatchById(entityList);
  147. }
  148. @Override
  149. default boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper) {
  150. if (entity instanceof BaseEntity) {
  151. if (ObjectUtil.isEmpty(((BaseEntity) entity).getId())) {
  152. setCreateInfo(entity);
  153. } else {
  154. setUpdateInfo(entity);
  155. }
  156. }
  157. return IService.super.saveOrUpdate(entity, updateWrapper);
  158. }
  159. /**
  160. * 调新增方法时默认传入创建人、创建时间、租户id
  161. */
  162. default void setCreateInfo(T entity) {
  163. if (entity instanceof BaseEntity) {
  164. Date date = new Date();
  165. Long userId = AuthUtil.getUserId();
  166. ((BaseEntity) entity).setTenantId(AuthUtil.getTenantId());
  167. ((BaseEntity) entity).setCreateUser(userId);
  168. ((BaseEntity) entity).setCreateTime(date);
  169. ((BaseEntity) entity).setUpdateUser(userId);
  170. ((BaseEntity) entity).setUpdateTime(date);
  171. }
  172. }
  173. /**
  174. * 调编辑方法时默认传入更新人、更新时间
  175. */
  176. default void setUpdateInfo(T entity) {
  177. if (entity instanceof BaseEntity) {
  178. ((BaseEntity) entity).setTenantId(null);
  179. ((BaseEntity) entity).setCreateUser(null);
  180. ((BaseEntity) entity).setCreateTime(null);
  181. ((BaseEntity) entity).setUpdateUser(AuthUtil.getUserId());
  182. ((BaseEntity) entity).setUpdateTime(new Date());
  183. }
  184. }
  185. }