|
|
@@ -4,7 +4,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.izouma.jmrh.annotations.Searchable;
|
|
|
import com.izouma.jmrh.domain.*;
|
|
|
import com.izouma.jmrh.dto.PageQuery;
|
|
|
-import com.izouma.jmrh.enums.ResSnDType;
|
|
|
import com.izouma.jmrh.utils.DateTimeUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
@@ -46,6 +45,31 @@ public class BaseController {
|
|
|
return pageRequest;
|
|
|
}
|
|
|
|
|
|
+ public <T> Specification<T> getResAll(PageQuery pageQuery, Class<?> queryClass) {
|
|
|
+ return (Specification<T>) (root, criteriaQuery, criteriaBuilder) -> {
|
|
|
+ List<Predicate> or = new ArrayList<>();
|
|
|
+ List<Predicate> and = new ArrayList<>();
|
|
|
+ or.add(root.get("tresource").isNotNull());
|
|
|
+ or.add(root.get("product").isNotNull());
|
|
|
+ or.add(root.get("artProduct").isNotNull());
|
|
|
+ if (pageQuery.getQuery().get("user") != null) {
|
|
|
+ User user = (User) pageQuery.getQuery().get("user");
|
|
|
+ Join<User, Collect> join = root.join("user", JoinType.LEFT);
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ if (user.getId() != null && user.getId() > 0) {
|
|
|
+ and.add(criteriaBuilder.equal(join.get("id"), user.getId()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Predicate[] array_and = new Predicate[and.size()];
|
|
|
+ Predicate Pre_And = criteriaBuilder.and(and.toArray(array_and));
|
|
|
+
|
|
|
+ Predicate[] arrayOr = new Predicate[or.size()];
|
|
|
+ Predicate Pre_Or = criteriaBuilder.or(or.toArray(arrayOr));
|
|
|
+
|
|
|
+ /*criteriaBuilder.or(or.toArray(new Predicate[0]));*/
|
|
|
+ return criteriaQuery.where(Pre_And, Pre_Or).getRestriction();
|
|
|
+ };
|
|
|
+ }
|
|
|
|
|
|
public static <T> Specification<T> toSpecification(PageQuery pageQuery, Class<?> queryClass) {
|
|
|
return (Specification<T>) (root, criteriaQuery, criteriaBuilder) -> {
|
|
|
@@ -205,148 +229,6 @@ public class BaseController {
|
|
|
Field field = getDeclaredField(queryClass, property);
|
|
|
if (field == null) return;
|
|
|
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)));
|
|
|
- }
|
|
|
- and.add(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));
|
|
|
- }
|
|
|
- and.add(root.get(property).in(list));
|
|
|
- } else {
|
|
|
- and.add(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");
|
|
|
- and.add(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");
|
|
|
- and.add(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(",");
|
|
|
- LocalDateTime start = DateTimeUtils.toLocalDateTime(arr[0], "yyyy-MM-dd HH:mm:ss");
|
|
|
- and.add(criteriaBuilder.greaterThanOrEqualTo(root.get(property), start));
|
|
|
- LocalDateTime end = DateTimeUtils.toLocalDateTime(arr[1], "yyyy-MM-dd HH:mm:ss");
|
|
|
- and.add(criteriaBuilder.lessThanOrEqualTo(root.get(property), end));
|
|
|
- } else {
|
|
|
- and.add(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");
|
|
|
- and.add(criteriaBuilder.greaterThanOrEqualTo(root.get(property), start));
|
|
|
- } else if (list.size() == 2) {
|
|
|
- LocalDate end = DateTimeUtils
|
|
|
- .toLocalDate((String) list.get(1), "yyyy-MM-dd");
|
|
|
- and.add(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(",");
|
|
|
- LocalDate start = DateTimeUtils.toLocalDate(arr[0], "yyyy-MM-dd");
|
|
|
- and.add(criteriaBuilder.greaterThanOrEqualTo(root.get(property), start));
|
|
|
- LocalDate end = DateTimeUtils.toLocalDate(arr[1], "yyyy-MM-dd");
|
|
|
- and.add(criteriaBuilder.lessThanOrEqualTo(root.get(property), end));
|
|
|
- } else {
|
|
|
- and.add(criteriaBuilder.and(criteriaBuilder.equal(root.get(property), DateTimeUtils
|
|
|
- .toLocalDateTime((String) value, "yyyy-MM-dd"))));
|
|
|
- }
|
|
|
- } else if (property.equals("resSnDPropertyList")) {
|
|
|
- String str = String.valueOf(value);
|
|
|
- if (StringUtils.isNotEmpty(str)) {
|
|
|
- Join<ResSnDProperty, Class> join = root.join(property, JoinType.LEFT);
|
|
|
- String[] split = str.split(";");
|
|
|
- for (String item : split) {
|
|
|
- String strItem = item.trim();
|
|
|
- String name = strItem.substring(strItem.indexOf(":") + 1, strItem.indexOf(","));
|
|
|
- String strValue = strItem.substring(strItem.indexOf("value:") + 6);
|
|
|
- if (StringUtils.isNotEmpty(strValue) && !strValue.equals('0')) {
|
|
|
- and.add(criteriaBuilder.equal(join.get("name"), name));
|
|
|
- and.add(criteriaBuilder.equal(join.get("value"), strValue));
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- and.add(criteriaBuilder.and(criteriaBuilder.equal(root.get(property), value)));
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- if (StringUtils.isNotEmpty(pageQuery.getSearch())) {
|
|
|
- Field[] fields = queryClass.getDeclaredFields();
|
|
|
- List<Predicate> or = new ArrayList<>();
|
|
|
- try {
|
|
|
- if (StringUtils.isNumeric(pageQuery.getSearch())) {
|
|
|
- or.add(criteriaBuilder.equal(root.get("id"), Long.parseLong(pageQuery.getSearch())));
|
|
|
- }
|
|
|
- } catch (Exception ignored) {
|
|
|
- }
|
|
|
- for (Field field : fields) {
|
|
|
- Searchable annotation = field.getAnnotation(Searchable.class);
|
|
|
- if (annotation == null) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- if (!annotation.value()) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- or.add(criteriaBuilder.like(root.get(field.getName()), "%" + pageQuery.getSearch() + "%"));
|
|
|
- }
|
|
|
- and.add(criteriaBuilder.or(or.toArray(new Predicate[0])));
|
|
|
- }
|
|
|
- criteriaQuery.distinct(true);
|
|
|
- return criteriaBuilder.and(and.toArray(new Predicate[0]));
|
|
|
- };
|
|
|
- }
|
|
|
-
|
|
|
- public <T> Specification<T> getConversation(PageQuery pageQuery, Class<?> queryClass) {
|
|
|
- return (Specification<T>) (root, criteriaQuery, criteriaBuilder) -> {
|
|
|
- List<Predicate> and = new ArrayList<>();
|
|
|
- pageQuery.getQuery().forEach((property, value) -> {
|
|
|
- if (value == null) {
|
|
|
- return;
|
|
|
- }
|
|
|
- if (String.class.equals(value.getClass())) {
|
|
|
- if (StringUtils.isEmpty((String) value)) {
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
- if (property.equals("typeStr")) {
|
|
|
- String str = String.valueOf(value);
|
|
|
- if (StringUtils.isNotEmpty(str)) {
|
|
|
- Join<ResourceSupplyAndDemand, Conversation> join = root.join("resourceSupplyDemand", JoinType.LEFT);
|
|
|
- String[] split = str.split(",");
|
|
|
- List list = new ArrayList();
|
|
|
- for (String item : split
|
|
|
- ) {
|
|
|
- list.add(Enum.valueOf(ResSnDType.class, String.valueOf(item)));
|
|
|
- }
|
|
|
- and.add(criteriaBuilder.and(join.get("type").in(list)));
|
|
|
- }
|
|
|
- }
|
|
|
- Field field = getDeclaredField(queryClass, property);
|
|
|
- if (field == null) return;
|
|
|
- Class fieldType = field.getType();
|
|
|
if (Enum.class.isAssignableFrom(fieldType)) {
|
|
|
if (value instanceof Collection) {
|
|
|
if (!((Collection) value).isEmpty()) {
|
|
|
@@ -446,7 +328,6 @@ public class BaseController {
|
|
|
};
|
|
|
}
|
|
|
|
|
|
-
|
|
|
private static Field getDeclaredField(Class<?> clazz, String property) {
|
|
|
String className = clazz.getName();
|
|
|
while (clazz != null && clazz != Object.class) {
|
|
|
@@ -459,132 +340,4 @@ public class BaseController {
|
|
|
log.error("no such field [{}] in class [{}]", property, className);
|
|
|
return null;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
- public <T> Specification<T> getNeedMessage(PageQuery pageQuery, Class<?> queryClass) {
|
|
|
- return (Specification<T>) (root, criteriaQuery, criteriaBuilder) -> {
|
|
|
- List<Predicate> and = new ArrayList<>();
|
|
|
- List<Predicate> or = new ArrayList<>();
|
|
|
- or.add(root.get("artNeed").isNotNull());
|
|
|
- or.add(root.get("productNeed").isNotNull());
|
|
|
- or.add(root.get("financingNeeds").isNotNull());
|
|
|
- pageQuery.getQuery().forEach((property, value) -> {
|
|
|
- Field field = getDeclaredField(queryClass, property);
|
|
|
- if (field == null) return;
|
|
|
- Class fieldType = field.getType();
|
|
|
- if (fieldType == User.class) {
|
|
|
- if (property == "user") {
|
|
|
- Join<User, Message> join = root.join("user", JoinType.LEFT);
|
|
|
- ObjectMapper objectMapper = new ObjectMapper();
|
|
|
- User user = objectMapper.convertValue(value, User.class);
|
|
|
- if (user.getId() != null && user.getId() > 0) {
|
|
|
- and.add(criteriaBuilder.equal(join.get("id"), user.getId()));
|
|
|
- }
|
|
|
- } else {
|
|
|
- Join<User, Message> join = root.join("myUser", JoinType.LEFT);
|
|
|
- ObjectMapper objectMapper = new ObjectMapper();
|
|
|
- User user = objectMapper.convertValue(value, User.class);
|
|
|
- if (user.getId() != null && user.getId() > 0) {
|
|
|
- and.add(criteriaBuilder.equal(join.get("id"), user.getId()));
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- } else if (property == "onExist") {
|
|
|
- String[] split = String.valueOf(value).split(",");
|
|
|
- if (split.length > 1) {
|
|
|
- List<Predicate> or2 = new ArrayList<>();
|
|
|
- or2.add(criteriaBuilder.equal(root.get("onExist"), Long.parseLong(split[0])));
|
|
|
- or2.add(criteriaBuilder.equal(root.get("onExist"), Long.parseLong(split[1])));
|
|
|
- //and.add(criteriaBuilder.or(or.toArray(new Predicate[0])));
|
|
|
- Predicate[] array_or2 = new Predicate[or2.size()];
|
|
|
- Predicate Pre_Or2 = criteriaBuilder.or(or2.toArray(array_or2));
|
|
|
- and.add(criteriaBuilder.and(Pre_Or2));
|
|
|
- } else {
|
|
|
- and.add(criteriaBuilder.and(criteriaBuilder.equal(root.get(property), value)));
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- Predicate[] array_and = new Predicate[and.size()];
|
|
|
- Predicate Pre_And = criteriaBuilder.and(and.toArray(array_and));
|
|
|
- Predicate[] arrayOr = new Predicate[or.size()];
|
|
|
- Predicate Pre_Or = criteriaBuilder.or(or.toArray(arrayOr));
|
|
|
- return criteriaQuery.where(Pre_And, Pre_Or).getRestriction();
|
|
|
- };
|
|
|
- }
|
|
|
-
|
|
|
- public <T> Specification<T> getResMessage(PageQuery pageQuery, Class<?> queryClass) {
|
|
|
- return (Specification<T>) (root, criteriaQuery, criteriaBuilder) -> {
|
|
|
- List<Predicate> and = new ArrayList<>();
|
|
|
- List<Predicate> or = new ArrayList<>();
|
|
|
- or.add(root.get("product").isNotNull());
|
|
|
- or.add(root.get("artProduct").isNotNull());
|
|
|
- or.add(root.get("tresource").isNotNull());
|
|
|
- pageQuery.getQuery().forEach((property, value) -> {
|
|
|
- Field field = getDeclaredField(queryClass, property);
|
|
|
- if (field == null) return;
|
|
|
- Class fieldType = field.getType();
|
|
|
- if (fieldType == User.class) {
|
|
|
- if (property == "user") {
|
|
|
- Join<User, Message> join = root.join("user", JoinType.LEFT);
|
|
|
- ObjectMapper objectMapper = new ObjectMapper();
|
|
|
- User user = objectMapper.convertValue(value, User.class);
|
|
|
- if (user.getId() != null && user.getId() > 0) {
|
|
|
- and.add(criteriaBuilder.equal(join.get("id"), user.getId()));
|
|
|
- }
|
|
|
- } else {
|
|
|
- Join<User, Message> join = root.join("myUser", JoinType.LEFT);
|
|
|
- ObjectMapper objectMapper = new ObjectMapper();
|
|
|
- User user = objectMapper.convertValue(value, User.class);
|
|
|
- if (user.getId() != null && user.getId() > 0) {
|
|
|
- and.add(criteriaBuilder.equal(join.get("id"), user.getId()));
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- } else if (property == "onExist") {
|
|
|
- String[] split = String.valueOf(value).split(",");
|
|
|
- if (split.length > 1) {
|
|
|
- List<Predicate> or2 = new ArrayList<>();
|
|
|
- or2.add(criteriaBuilder.equal(root.get("onExist"), Long.parseLong(split[0])));
|
|
|
- or2.add(criteriaBuilder.equal(root.get("onExist"), Long.parseLong(split[1])));
|
|
|
- //and.add(criteriaBuilder.or(or.toArray(new Predicate[0])));
|
|
|
- Predicate[] array_or2 = new Predicate[or2.size()];
|
|
|
- Predicate Pre_Or2 = criteriaBuilder.or(or2.toArray(array_or2));
|
|
|
- and.add(criteriaBuilder.and(Pre_Or2));
|
|
|
- } else {
|
|
|
- and.add(criteriaBuilder.and(criteriaBuilder.equal(root.get(property), value)));
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- Predicate[] array_and = new Predicate[and.size()];
|
|
|
- Predicate Pre_And = criteriaBuilder.and(and.toArray(array_and));
|
|
|
- Predicate[] arrayOr = new Predicate[or.size()];
|
|
|
- Predicate Pre_Or = criteriaBuilder.or(or.toArray(arrayOr));
|
|
|
-
|
|
|
- return criteriaQuery.where(Pre_And, Pre_Or).getRestriction();
|
|
|
- };
|
|
|
- }
|
|
|
-
|
|
|
- public <T> Specification<T> getMessage(Message message) {
|
|
|
- return (Specification<T>) (root, criteriaQuery, criteriaBuilder) -> {
|
|
|
- List<Predicate> and = new ArrayList<>();
|
|
|
- if (message.getUser() != null) {
|
|
|
- Join<User, Message> join = root.join("user", JoinType.LEFT);
|
|
|
- ObjectMapper objectMapper = new ObjectMapper();
|
|
|
- User user = objectMapper.convertValue(message.getUser(), User.class);
|
|
|
- if (user.getId() != null && user.getId() > 0) {
|
|
|
- and.add(criteriaBuilder.equal(join.get("id"), user.getId()));
|
|
|
- }
|
|
|
- }
|
|
|
- if (message.getMyUser() != null) {
|
|
|
- Join<User, Message> join = root.join("myUser", JoinType.LEFT);
|
|
|
- ObjectMapper objectMapper = new ObjectMapper();
|
|
|
- User user = objectMapper.convertValue(message.getMyUser(), User.class);
|
|
|
- if (user.getId() != null && user.getId() > 0) {
|
|
|
- and.add(criteriaBuilder.equal(join.get("id"), user.getId()));
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- return criteriaBuilder.and(and.toArray(new Predicate[0]));
|
|
|
- };
|
|
|
- }
|
|
|
}
|