修复工具类

This commit is contained in:
dftre 2024-11-06 16:07:52 +08:00
parent e17d9f4618
commit b94ccbb74f

View File

@ -9,44 +9,46 @@ import java.util.Map;
import com.ruoyi.common.core.domain.BaseEntity; import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.mybatis.annotation.Query; import com.ruoyi.mybatis.annotation.Query;
import com.ruoyi.mybatis.enums.QueryEnum;
public class QueryUtil { public class QueryUtil {
/**
* 判断该字段是否可以被列为查询条件
* @param entity 参与判断的对象
* @param field 参与判断的字段
* @param query 参与判断的查询注解
* @return
*/
public static boolean fieldQueryIsNotNull(Object entity, Field field, Query query) { public static boolean fieldQueryIsNotNull(Object entity, Field field, Query query) {
try { try {
if (query != null) { if (query == null)return false;
BaseEntity baseEntity = (BaseEntity) entity; BaseEntity baseEntity = (BaseEntity) entity;
Map<String, Object> map = baseEntity.getParams(); Map<String, Object> map = baseEntity.getParams();
if (query.operation().equals(QueryEnum.between)) { return switch(query.operation()){
return map.get(query.sections()[0]) != null && map.get(query.sections()[1]) != null; case between -> map.get(query.sections()[0]) != null && map.get(query.sections()[1]) != null;
} else if (query.operation().equals(QueryEnum.in)) { case in -> {
Object obj = map.get(query.section()); Object section = map.get(query.section());
if (obj == null) { if (section == null) yield false;
return false; if (section instanceof String) {
} else { List<String> list = new ArrayList<>();
if (obj instanceof String) { for (String split : ((String) section).split(",")) {
List<String> list = new ArrayList<>(); list.add("\"" + split + "\"");
for (String split : ((String) obj).split(",")) {
list.add("\"" + split + "\"");
}
map.put(query.section() + "_operation", String.join(",", list));
return true;
} else if (obj instanceof Collection) {
List<String> list = new ArrayList<>();
for (Object split : ((Collection<?>) obj)) {
list.add("\"" + split.toString() + "\"");
}
map.put(query.section() + "_operation", String.join(",", list));
return true;
} else {
return false;
} }
map.put(query.section() + "_operation", String.join(",", list));
yield true;
} else if (section instanceof Collection) {
List<String> list = new ArrayList<>();
for (Object split : ((Collection<?>) section)) {
list.add("\"" + split.toString() + "\"");
}
map.put(query.section() + "_operation", String.join(",", list));
yield true;
} else {
yield false;
} }
} }
} default ->field.get(entity) != null;
return field.get(entity) != null; };
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
throw new RuntimeException("Failed to access field for building query conditions.", e); throw new RuntimeException("Failed to access field for building query conditions.", e);
} }