|
|
@@ -1,5 +1,7 @@
|
|
|
package com.izouma.uwip.utils;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.izouma.uwip.annotations.Searchable;
|
|
|
import com.izouma.uwip.dto.PageQuery;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
@@ -8,10 +10,7 @@ import org.springframework.data.domain.PageRequest;
|
|
|
import org.springframework.data.domain.Sort;
|
|
|
import org.springframework.data.jpa.domain.Specification;
|
|
|
|
|
|
-import javax.persistence.criteria.CriteriaBuilder;
|
|
|
-import javax.persistence.criteria.CriteriaQuery;
|
|
|
-import javax.persistence.criteria.Predicate;
|
|
|
-import javax.persistence.criteria.Root;
|
|
|
+import javax.persistence.criteria.*;
|
|
|
import java.lang.reflect.Field;
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
|
@@ -162,6 +161,161 @@ public class JpaUtils {
|
|
|
return and;
|
|
|
}
|
|
|
|
|
|
+ public static Predicate getPredicate(String property, Object value, Class<?> queryClass, Root<?> root,
|
|
|
+ CriteriaBuilder criteriaBuilder, boolean isPatent) {
|
|
|
+ if (value == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (String.class.equals(value.getClass())) {
|
|
|
+ if (StringUtils.isEmpty((String) value)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Field field = getDeclaredField(queryClass, property);
|
|
|
+ if (field == null) return null;
|
|
|
+
|
|
|
+ Class fieldType = field.getType();
|
|
|
+ if (Enum.class.isAssignableFrom(fieldType)) {
|
|
|
+ if (value instanceof Collection) {
|
|
|
+ if (!((Collection) value).isEmpty()) {
|
|
|
+ List list = new ArrayList();
|
|
|
+ for (Object o : ((Collection) value)) {
|
|
|
+ list.add(Enum.valueOf(fieldType, String.valueOf(o)));
|
|
|
+ }
|
|
|
+ if (isPatent) {
|
|
|
+ return root.join("patent", JoinType.LEFT).get(property).in(list);
|
|
|
+ }
|
|
|
+ return root.get(property).in(list);
|
|
|
+ }
|
|
|
+ } else if (value instanceof String && StringUtils.isNotEmpty((String) value)) {
|
|
|
+ if (((String) value).contains(",")) {
|
|
|
+ String[] arr = ((String) value).split(",");
|
|
|
+ List list = new ArrayList();
|
|
|
+ for (String s : arr) {
|
|
|
+ list.add(Enum.valueOf(fieldType, s));
|
|
|
+ }
|
|
|
+ if (isPatent) {
|
|
|
+ return root.join("patent", JoinType.LEFT).get(property).in(list);
|
|
|
+ }
|
|
|
+ return root.get(property).in(list);
|
|
|
+ } else {
|
|
|
+ if (isPatent) {
|
|
|
+ return criteriaBuilder.and(criteriaBuilder
|
|
|
+ .equal(root.join("patent", JoinType.LEFT)
|
|
|
+ .get(property), Enum.valueOf(fieldType, String.valueOf(value))));
|
|
|
+ }
|
|
|
+ return criteriaBuilder.and(criteriaBuilder
|
|
|
+ .equal(root.get(property), Enum.valueOf(fieldType, String.valueOf(value))));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (LocalDateTime.class == fieldType) {
|
|
|
+ if (value instanceof List) {
|
|
|
+ List list = (List) value;
|
|
|
+ if (list.size() == 1) {
|
|
|
+ LocalDateTime start = DateTimeUtils
|
|
|
+ .toLocalDateTime((String) list.get(0), "yyyy-MM-dd HH:mm:ss");
|
|
|
+ if (isPatent) {
|
|
|
+ return criteriaBuilder.greaterThanOrEqualTo(root.join("patent", JoinType.LEFT)
|
|
|
+ .get(property), start);
|
|
|
+ }
|
|
|
+ return criteriaBuilder.greaterThanOrEqualTo(root.get(property), start);
|
|
|
+ } else if (list.size() == 2) {
|
|
|
+ LocalDateTime end = DateTimeUtils
|
|
|
+ .toLocalDateTime((String) list.get(1), "yyyy-MM-dd HH:mm:ss");
|
|
|
+ if (isPatent) {
|
|
|
+ return criteriaBuilder.lessThanOrEqualTo(root.join("patent", JoinType.LEFT).get(property), end);
|
|
|
+ }
|
|
|
+ return criteriaBuilder.lessThanOrEqualTo(root.get(property), end);
|
|
|
+ }
|
|
|
+ } else if (value instanceof String && Pattern
|
|
|
+ .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)) {
|
|
|
+ String[] arr = ((String) value).split(",");
|
|
|
+ List<Predicate> and = new ArrayList<>();
|
|
|
+ LocalDateTime start = DateTimeUtils.toLocalDateTime(arr[0], "yyyy-MM-dd HH:mm:ss");
|
|
|
+ if (isPatent) {
|
|
|
+ and.add(criteriaBuilder.greaterThanOrEqualTo(root.join("patent", JoinType.LEFT)
|
|
|
+ .get(property), start));
|
|
|
+ }
|
|
|
+ and.add(criteriaBuilder.greaterThanOrEqualTo(root.get(property), start));
|
|
|
+ LocalDateTime end = DateTimeUtils.toLocalDateTime(arr[1], "yyyy-MM-dd HH:mm:ss");
|
|
|
+ if (isPatent) {
|
|
|
+ and.add(criteriaBuilder.lessThanOrEqualTo(root.join("patent", JoinType.LEFT).get(property), end));
|
|
|
+ }
|
|
|
+ and.add(criteriaBuilder.lessThanOrEqualTo(root.get(property), end));
|
|
|
+ return criteriaBuilder.and(and.toArray(new Predicate[0]));
|
|
|
+ } else {
|
|
|
+ if (isPatent) {
|
|
|
+ return criteriaBuilder.and(criteriaBuilder.equal(root.join("patent", JoinType.LEFT)
|
|
|
+ .get(property), DateTimeUtils
|
|
|
+ .toLocalDateTime((String) value, "yyyy-MM-dd HH:mm:ss")));
|
|
|
+ }
|
|
|
+ return criteriaBuilder.and(criteriaBuilder.equal(root.get(property), DateTimeUtils
|
|
|
+ .toLocalDateTime((String) value, "yyyy-MM-dd HH:mm:ss")));
|
|
|
+ }
|
|
|
+ } else if (LocalDate.class == fieldType) {
|
|
|
+ if (value instanceof List) {
|
|
|
+ List list = (List) value;
|
|
|
+ if (list.size() == 1) {
|
|
|
+ LocalDate start = DateTimeUtils
|
|
|
+ .toLocalDate((String) list.get(0), "yyyy-MM-dd");
|
|
|
+ if (isPatent) {
|
|
|
+ return criteriaBuilder.greaterThanOrEqualTo(root.join("patent", JoinType.LEFT)
|
|
|
+ .get(property), start);
|
|
|
+ }
|
|
|
+ return criteriaBuilder.greaterThanOrEqualTo(root.get(property), start);
|
|
|
+ } else if (list.size() == 2) {
|
|
|
+ LocalDate end = DateTimeUtils
|
|
|
+ .toLocalDate((String) list.get(1), "yyyy-MM-dd");
|
|
|
+ if (isPatent) {
|
|
|
+ return criteriaBuilder.lessThanOrEqualTo(root.join("patent", JoinType.LEFT).get(property), end);
|
|
|
+ }
|
|
|
+ return criteriaBuilder.lessThanOrEqualTo(root.get(property), end);
|
|
|
+ }
|
|
|
+ } else if (value instanceof String && Pattern
|
|
|
+ .matches("^\\d{4}-\\d{2}-\\d{2},\\d{4}-\\d{2}-\\d{2}$", (String) value)) {
|
|
|
+ String[] arr = ((String) value).split(",");
|
|
|
+ List<Predicate> and = new ArrayList<>();
|
|
|
+ LocalDate start = DateTimeUtils.toLocalDate(arr[0], "yyyy-MM-dd");
|
|
|
+ if (isPatent) {
|
|
|
+ and.add(criteriaBuilder.greaterThanOrEqualTo(root.join("patent", JoinType.LEFT).get(property), start));
|
|
|
+ }
|
|
|
+ and.add(criteriaBuilder.greaterThanOrEqualTo(root.get(property), start));
|
|
|
+ LocalDate end = DateTimeUtils.toLocalDate(arr[1], "yyyy-MM-dd");
|
|
|
+ if (isPatent) {
|
|
|
+ and.add(criteriaBuilder.lessThanOrEqualTo(root.join("patent", JoinType.LEFT).get(property), end));
|
|
|
+ }
|
|
|
+ and.add(criteriaBuilder.lessThanOrEqualTo(root.get(property), end));
|
|
|
+ return criteriaBuilder.and(and.toArray(new Predicate[0]));
|
|
|
+ } else {
|
|
|
+ if (isPatent) {
|
|
|
+ return criteriaBuilder.and(criteriaBuilder.equal(root.join("patent", JoinType.LEFT)
|
|
|
+ .get(property), DateTimeUtils
|
|
|
+ .toLocalDateTime((String) value, "yyyy-MM-dd")));
|
|
|
+ }
|
|
|
+ return criteriaBuilder.and(criteriaBuilder.equal(root.get(property), DateTimeUtils
|
|
|
+ .toLocalDateTime((String) value, "yyyy-MM-dd")));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (isPatent) {
|
|
|
+ return criteriaBuilder.and(criteriaBuilder.equal(root.join("patent", JoinType.LEFT).get(property), value));
|
|
|
+ }
|
|
|
+ return criteriaBuilder.and(criteriaBuilder.equal(root.get(property), value));
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Boolean isExistField(String field, Object obj) {
|
|
|
+ if (obj == null || StringUtils.isEmpty(field)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Object o = JSON.toJSON(obj);
|
|
|
+ JSONObject jsonObj = new JSONObject();
|
|
|
+ if (o instanceof JSONObject) {
|
|
|
+ jsonObj = (JSONObject) o;
|
|
|
+ }
|
|
|
+ return jsonObj.containsKey(field);
|
|
|
+ }
|
|
|
+
|
|
|
private static Field getDeclaredField(Class<?> clazz, String property) {
|
|
|
String className = clazz.getName();
|
|
|
while (clazz != null && clazz != Object.class) {
|