BaseController.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package com.izouma.jmrh.web;
  2. import com.izouma.jmrh.annotations.Searchable;
  3. import com.izouma.jmrh.dto.PageQuery;
  4. import com.izouma.jmrh.utils.DateTimeUtils;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.springframework.data.domain.PageRequest;
  8. import org.springframework.data.domain.Sort;
  9. import org.springframework.data.jpa.domain.Specification;
  10. import javax.persistence.criteria.Predicate;
  11. import java.lang.reflect.Field;
  12. import java.time.LocalDate;
  13. import java.time.LocalDateTime;
  14. import java.util.ArrayList;
  15. import java.util.Collection;
  16. import java.util.List;
  17. import java.util.regex.Pattern;
  18. @SuppressWarnings("ALL")
  19. @Slf4j
  20. public class BaseController {
  21. public PageRequest toPageRequest(PageQuery pageQuery) {
  22. PageRequest pageRequest;
  23. if (StringUtils.isNotEmpty(pageQuery.getSort())) {
  24. List<Sort.Order> orders = new ArrayList<>();
  25. for (String sortStr : pageQuery.getSort().split(";")) {
  26. String direction = "asc";
  27. String prop = sortStr;
  28. if (sortStr.contains(",asc") || sortStr.contains(",desc")) {
  29. prop = sortStr.split(",")[0];
  30. direction = sortStr.split(",")[1];
  31. }
  32. orders.add("asc".equals(direction) ? Sort.Order.asc(prop) : Sort.Order.desc(prop));
  33. }
  34. pageRequest = PageRequest.of(pageQuery.getPage(), pageQuery.getSize(), Sort.by(orders));
  35. } else {
  36. pageRequest = PageRequest.of(pageQuery.getPage(), pageQuery.getSize());
  37. }
  38. return pageRequest;
  39. }
  40. public <T> Specification<T> toSpecification(PageQuery pageQuery, Class<?> queryClass) {
  41. return (Specification<T>) (root, criteriaQuery, criteriaBuilder) -> {
  42. List<Predicate> and = new ArrayList<>();
  43. pageQuery.getQuery().forEach((property, value) -> {
  44. if (value == null) {
  45. return;
  46. }
  47. if (String.class.equals(value.getClass())) {
  48. if (StringUtils.isEmpty((String) value)) {
  49. return;
  50. }
  51. }
  52. Field field = getDeclaredField(queryClass, property);
  53. if (field == null) return;
  54. Class fieldType = field.getType();
  55. if (Enum.class.isAssignableFrom(fieldType)) {
  56. if (value instanceof Collection) {
  57. if (!((Collection) value).isEmpty()) {
  58. List list = new ArrayList();
  59. for (Object o : ((Collection) value)) {
  60. list.add(Enum.valueOf(fieldType, String.valueOf(o)));
  61. }
  62. and.add(root.get(property).in(list));
  63. }
  64. } else if (value instanceof String && StringUtils.isNotEmpty((String) value)) {
  65. if (((String) value).contains(",")) {
  66. String[] arr = ((String) value).split(",");
  67. List list = new ArrayList();
  68. for (String s : arr) {
  69. list.add(Enum.valueOf(fieldType, s));
  70. }
  71. and.add(root.get(property).in(list));
  72. } else {
  73. and.add(criteriaBuilder.and(criteriaBuilder
  74. .equal(root.get(property), Enum.valueOf(fieldType, String.valueOf(value)))));
  75. }
  76. }
  77. } else if (LocalDateTime.class == fieldType) {
  78. if (value instanceof List) {
  79. List list = (List) value;
  80. if (list.size() == 1) {
  81. LocalDateTime start = DateTimeUtils
  82. .toLocalDateTime((String) list.get(0), "yyyy-MM-dd HH:mm:ss");
  83. and.add(criteriaBuilder.greaterThanOrEqualTo(root.get(property), start));
  84. } else if (list.size() == 2) {
  85. LocalDateTime end = DateTimeUtils
  86. .toLocalDateTime((String) list.get(1), "yyyy-MM-dd HH:mm:ss");
  87. and.add(criteriaBuilder.lessThanOrEqualTo(root.get(property), end));
  88. }
  89. } else if (value instanceof String && Pattern
  90. .matches("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2},\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$", (String) value)) {
  91. String[] arr = ((String) value).split(",");
  92. LocalDateTime start = DateTimeUtils.toLocalDateTime(arr[0], "yyyy-MM-dd HH:mm:ss");
  93. and.add(criteriaBuilder.greaterThanOrEqualTo(root.get(property), start));
  94. LocalDateTime end = DateTimeUtils.toLocalDateTime(arr[1], "yyyy-MM-dd HH:mm:ss");
  95. and.add(criteriaBuilder.lessThanOrEqualTo(root.get(property), end));
  96. } else {
  97. and.add(criteriaBuilder.and(criteriaBuilder.equal(root.get(property), DateTimeUtils
  98. .toLocalDateTime((String) value, "yyyy-MM-dd HH:mm:ss"))));
  99. }
  100. } else if (LocalDate.class == fieldType) {
  101. if (value instanceof List) {
  102. List list = (List) value;
  103. if (list.size() == 1) {
  104. LocalDate start = DateTimeUtils
  105. .toLocalDate((String) list.get(0), "yyyy-MM-dd");
  106. and.add(criteriaBuilder.greaterThanOrEqualTo(root.get(property), start));
  107. } else if (list.size() == 2) {
  108. LocalDate end = DateTimeUtils
  109. .toLocalDate((String) list.get(1), "yyyy-MM-dd");
  110. and.add(criteriaBuilder.lessThanOrEqualTo(root.get(property), end));
  111. }
  112. } else if (value instanceof String && Pattern
  113. .matches("^\\d{4}-\\d{2}-\\d{2},\\d{4}-\\d{2}-\\d{2}$", (String) value)) {
  114. String[] arr = ((String) value).split(",");
  115. LocalDate start = DateTimeUtils.toLocalDate(arr[0], "yyyy-MM-dd");
  116. and.add(criteriaBuilder.greaterThanOrEqualTo(root.get(property), start));
  117. LocalDate end = DateTimeUtils.toLocalDate(arr[1], "yyyy-MM-dd");
  118. and.add(criteriaBuilder.lessThanOrEqualTo(root.get(property), end));
  119. } else {
  120. and.add(criteriaBuilder.and(criteriaBuilder.equal(root.get(property), DateTimeUtils
  121. .toLocalDateTime((String) value, "yyyy-MM-dd"))));
  122. }
  123. } else {
  124. and.add(criteriaBuilder.and(criteriaBuilder.equal(root.get(property), value)));
  125. }
  126. });
  127. if (StringUtils.isNotEmpty(pageQuery.getSearch())) {
  128. Field[] fields = queryClass.getDeclaredFields();
  129. List<Predicate> or = new ArrayList<>();
  130. try {
  131. if (StringUtils.isNumeric(pageQuery.getSearch())) {
  132. or.add(criteriaBuilder.equal(root.get("id"), Long.parseLong(pageQuery.getSearch())));
  133. }
  134. } catch (Exception ignored) {
  135. }
  136. for (Field field : fields) {
  137. Searchable annotation = field.getAnnotation(Searchable.class);
  138. if (annotation == null) {
  139. continue;
  140. }
  141. if (!annotation.value()) {
  142. continue;
  143. }
  144. or.add(criteriaBuilder.like(root.get(field.getName()), "%" + pageQuery.getSearch() + "%"));
  145. }
  146. and.add(criteriaBuilder.or(or.toArray(new Predicate[0])));
  147. }
  148. return criteriaBuilder.and(and.toArray(new Predicate[0]));
  149. };
  150. }
  151. private Field getDeclaredField(Class<?> clazz, String property) {
  152. String className = clazz.getName();
  153. while (clazz != null && clazz != Object.class) {
  154. try {
  155. return clazz.getDeclaredField(property);
  156. } catch (NoSuchFieldException ignored) {
  157. }
  158. clazz = clazz.getSuperclass();
  159. }
  160. log.error("no such field [{}] in class [{}]", property, className);
  161. return null;
  162. }
  163. }