WrapperUtil.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package com.fjhx.utils;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.util.ObjectUtil;
  5. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  6. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  7. import org.springblade.core.log.exception.ServiceException;
  8. import org.springblade.core.secure.utils.AuthUtil;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. public class WrapperUtil {
  12. private Map<String, String> condition;
  13. private QueryWrapper<?> wrapper;
  14. private WrapperUtil() {
  15. }
  16. /**
  17. * 初始化
  18. */
  19. public static WrapperUtil init() {
  20. WrapperUtil wrapperUtil = new WrapperUtil();
  21. wrapperUtil.wrapper = Wrappers.query();
  22. wrapperUtil.condition = new HashMap<>();
  23. return wrapperUtil;
  24. }
  25. /**
  26. * 初始化
  27. *
  28. * @param condition 查询条件
  29. */
  30. public static WrapperUtil init(Map<String, String> condition) {
  31. WrapperUtil wrapperUtil = new WrapperUtil();
  32. wrapperUtil.wrapper = Wrappers.query();
  33. wrapperUtil.condition = condition;
  34. return wrapperUtil;
  35. }
  36. /**
  37. * 初始化
  38. *
  39. * @param condition 查询条件
  40. */
  41. public static WrapperUtil init(Map<String, String> condition, QueryWrapper<?> wrapper) {
  42. WrapperUtil wrapperUtil = new WrapperUtil();
  43. wrapperUtil.wrapper = wrapper;
  44. wrapperUtil.condition = condition;
  45. return wrapperUtil;
  46. }
  47. /**
  48. * 获取wrapper
  49. */
  50. public QueryWrapper<?> getWrapper() {
  51. return wrapper;
  52. }
  53. /**
  54. * 时间段查询
  55. *
  56. * @param field 查询字段名
  57. */
  58. public WrapperUtil periodOfTime(String field) {
  59. Object beginTimeObj = condition.get("beginTime");
  60. Object endTimeObj = condition.get("endTime");
  61. if (ObjectUtil.isAllNotEmpty(beginTimeObj, endTimeObj)) {
  62. try {
  63. DateTime beginTime = DateUtil.beginOfDay(DateUtil.parse(beginTimeObj.toString()));
  64. DateTime endTime = DateUtil.endOfDay(DateUtil.parse(endTimeObj.toString()));
  65. wrapper.between(field, beginTime, endTime);
  66. } catch (Exception e) {
  67. throw new ServiceException("传入日期格式解析错误");
  68. }
  69. }
  70. return this;
  71. }
  72. /**
  73. * 查询未删除数据
  74. */
  75. public WrapperUtil notDelete() {
  76. wrapper.eq("del_flag", 0);
  77. return this;
  78. }
  79. /**
  80. * 查询未删除数据
  81. *
  82. * @param prefix 查询字段表前缀
  83. */
  84. public WrapperUtil notDelete(String prefix) {
  85. if (ObjectUtil.isNotEmpty(prefix)) {
  86. wrapper.eq(prefix + ".del_flag", 0);
  87. } else {
  88. wrapper.eq("del_flag", 0);
  89. }
  90. return this;
  91. }
  92. /**
  93. * 查询未删除数据
  94. *
  95. * @param prefix 查询字段表前缀
  96. */
  97. public WrapperUtil notDelete(String... prefix) {
  98. for (String item : prefix) {
  99. if (ObjectUtil.isNotEmpty(prefix)) {
  100. wrapper.eq(item + ".del_flag", 0);
  101. }
  102. }
  103. return this;
  104. }
  105. /**
  106. * 创建时间倒叙排序
  107. * <p>
  108. * 雪花算法生成id是顺序id,因此按时间倒叙可以按id排序,效率更高
  109. * <p/>
  110. *
  111. * @param prefix 查询字段表前缀
  112. */
  113. public WrapperUtil createTimeDesc(String prefix) {
  114. if (ObjectUtil.isNotEmpty(prefix)) {
  115. wrapper.orderByDesc(prefix + ".id");
  116. } else {
  117. wrapper.orderByDesc("id");
  118. }
  119. return this;
  120. }
  121. /**
  122. * 创建时间倒叙排序
  123. */
  124. public WrapperUtil createTimeDesc() {
  125. createTimeDesc(null);
  126. return this;
  127. }
  128. /**
  129. * 关键字查询
  130. *
  131. * @param fields 查询字段名
  132. */
  133. public WrapperUtil keyword(String... fields) {
  134. Object keyword = condition.get("keyword");
  135. if (ObjectUtil.isNotEmpty(keyword)) {
  136. wrapper.and(q -> {
  137. for (String field : fields) {
  138. q.or().like(field, keyword);
  139. }
  140. });
  141. }
  142. return this;
  143. }
  144. /**
  145. * 相等
  146. *
  147. * @param field 查询字段名
  148. * @param mapKey 搜索条件key
  149. */
  150. public WrapperUtil eq(String field, String mapKey) {
  151. Object value = condition.get(mapKey);
  152. if (ObjectUtil.isNotEmpty(value)) {
  153. wrapper.eq(field, value);
  154. }
  155. return this;
  156. }
  157. /**
  158. * 相等(查询条件必填)
  159. *
  160. * @param field 查询字段名
  161. * @param mapKey 搜索条件key
  162. * @param exceptionSpecification 若搜索条件为空抛出的异常提示
  163. */
  164. public WrapperUtil eq(String field, String mapKey, String exceptionSpecification) {
  165. Object value = condition.get(mapKey);
  166. if (ObjectUtil.isNotEmpty(value)) {
  167. wrapper.eq(field, value);
  168. } else {
  169. throw new ServiceException(exceptionSpecification);
  170. }
  171. return this;
  172. }
  173. /**
  174. * like
  175. *
  176. * @param field 查询字段名
  177. * @param mapKey 搜索条件key
  178. */
  179. public WrapperUtil like(String field, String mapKey) {
  180. Object value = condition.get(mapKey);
  181. if (ObjectUtil.isNotEmpty(value)) {
  182. wrapper.like(field, value);
  183. }
  184. return this;
  185. }
  186. /**
  187. * eq租户id
  188. *
  189. * @param field
  190. * @return
  191. */
  192. public WrapperUtil eqTenantId(String field) {
  193. if (ObjectUtil.isEmpty(field)) {
  194. wrapper.eq("tenant_id", AuthUtil.getTenantId());
  195. } else {
  196. wrapper.eq(field + ".tenant_id", AuthUtil.getTenantId());
  197. }
  198. return this;
  199. }
  200. /**
  201. * eq租户id
  202. *
  203. * @return
  204. */
  205. public WrapperUtil eqTenantId() {
  206. wrapper.eq("tenant_id", AuthUtil.getTenantId());
  207. return this;
  208. }
  209. }