Przeglądaj źródła

【CHG】过滤错误的排序字段名,避免抛出字段不存在的异常

xczzmn 4 lat temu
rodzic
commit
0658e75715

+ 17 - 4
railway-business/src/main/java/com/railway/business/catenary/service/impl/BusJcabServiceImpl.java

@@ -11,7 +11,12 @@ import com.railway.common.enums.DelFlagEnum;
 import com.railway.common.utils.SecurityUtils;
 import com.railway.common.utils.StringUtils;
 import com.railway.common.utils.sql.SqlUtil;
+import java.lang.reflect.Field;
+import java.util.Arrays;
 import java.util.Date;
+import java.util.List;
+import java.util.stream.Collectors;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 
 /**
@@ -20,6 +25,7 @@ import org.springframework.stereotype.Service;
  * @author ZhaoMn
  * @date 2021/12/10
  */
+@Slf4j
 @Service
 public abstract class BusJcabServiceImpl {
 
@@ -39,18 +45,24 @@ public abstract class BusJcabServiceImpl {
    * 列表
    */
   protected void setList(BusJcab jcab) {
-    // 设置查询参数
+    // 设置查询权限
     JcebQueryUtil.setBusJcab(jcab);
+    // 设置查询参数
     if (StringUtils.isNotEmpty(jcab.getOperator()) && StringUtils.isEmpty(jcab.getCheckUser())) {
       jcab.setCheckUser(jcab.getOperator());
     }
     if (StringUtils.isNotNull(jcab.getOperationDate()) && StringUtils.isNull(jcab.getCheckDate())) {
       jcab.setCheckDate(jcab.getOperationDate());
     }
-    // 设置查询权限
-    if (StringUtils.isNotEmpty(jcab.getOrderBy())) {
+    // 设置查询排序字段
+    // 取BusJcab类的所有属性
+    Field[] fields = BusJcab.class.getDeclaredFields();
+    List<String> fieldList = Arrays.stream(fields).map(Field::getName).collect(Collectors.toList());
+    // 如果排序字段不是BusJcab类的属性时,取默认排序字段
+    log.info("前端指定的排序字段 - {}", jcab.getOrderBy());
+    if (StringUtils.isNotEmpty(jcab.getOrderBy()) && fieldList.contains(jcab.getOrderBy())) {
       jcab.setOrderBy(SqlUtil.humpToLine(jcab.getOrderBy()));
-      if(JcebOrderEnum.OPERATION_DATE.getField().equals(jcab.getOrderBy())){
+      if (JcebOrderEnum.OPERATION_DATE.getField().equals(jcab.getOrderBy())) {
         jcab.setOrderBy(JcebOrderEnum.CHECK_DATE.getField());
       }
     } else {
@@ -58,6 +70,7 @@ public abstract class BusJcabServiceImpl {
       jcab.setOrderBy(JcebOrderEnum.CHECK_DATE.getField());
       jcab.setIsAsc("0");
     }
+    log.info("最终使用的排序字段 - {}", jcab.getOrderBy());
   }
 
   /**

+ 14 - 1
railway-business/src/main/java/com/railway/business/catenary/service/impl/BusJcebServiceImpl.java

@@ -15,7 +15,12 @@ import com.railway.common.enums.DelFlagEnum;
 import com.railway.common.utils.SecurityUtils;
 import com.railway.common.utils.StringUtils;
 import com.railway.common.utils.sql.SqlUtil;
+import java.lang.reflect.Field;
+import java.util.Arrays;
 import java.util.Date;
+import java.util.List;
+import java.util.stream.Collectors;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -24,6 +29,7 @@ import org.springframework.transaction.annotation.Transactional;
  *
  * @author zhaomn
  */
+@Slf4j
 @Service
 public abstract class BusJcebServiceImpl<T extends BusJceb> implements IBusJcebService<T> {
 
@@ -75,13 +81,20 @@ public abstract class BusJcebServiceImpl<T extends BusJceb> implements IBusJcebS
   protected void setList(BusJceb jceb) {
     // 设置查询权限
     JcebQueryUtil.setBusJceb(jceb);
-    if (StringUtils.isNotEmpty(jceb.getOrderBy())) {
+    // 设置查询排序字段
+    // 取BusJcab类的所有属性
+    Field[] fields = BusJceb.class.getDeclaredFields();
+    List<String> fieldList = Arrays.stream(fields).map(Field::getName).collect(Collectors.toList());
+    // 如果排序字段不是BusJcab类的属性时,取默认排序字段
+    log.info("前端指定的排序字段 - {}", jceb.getOrderBy());
+    if (StringUtils.isNotEmpty(jceb.getOrderBy()) && fieldList.contains(jceb.getOrderBy())) {
       jceb.setOrderBy(SqlUtil.humpToLine(jceb.getOrderBy()));
     } else {
       // 默认倒序排列
       jceb.setOrderBy(JcebOrderEnum.OPERATION_DATE.getField());
       jceb.setIsAsc("0");
     }
+    log.info("最终使用的排序字段 - {}", jceb.getOrderBy());
   }
 
   /**