Эх сурвалжийг харах

fix 基础查询条件处理

classic_blue 1 сар өмнө
parent
commit
93bc685541

+ 34 - 31
edu-travel-common/edu-travel-common-datasource/src/main/java/edu/travel/datasource/ProjectInterceptor.java

@@ -1,6 +1,7 @@
 package edu.travel.datasource;
 
 import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
 import net.sf.jsqlparser.JSQLParserException;
 import net.sf.jsqlparser.expression.Expression;
@@ -29,38 +30,40 @@ public class ProjectInterceptor implements InnerInterceptor {
     @Override
     public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
         HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
-        String originSql = boundSql.getSql();
-        // 获取 Statement 执行sql的对象
-        Statement statement = null;
-        try {
-            statement = CCJSqlParserUtil.parse(originSql);
-        } catch (JSQLParserException e) {
-            throw new RuntimeException(e);
-        }
-        Select select = (Select) statement;
-
-        SelectBody selectBody = select.getSelectBody();
-        if (selectBody == null) {
-            return;
-        }
-        String targetSql = "";
-        if (selectBody instanceof PlainSelect) {
-            PlainSelect plainSelect = (PlainSelect) selectBody;
-            Expression where = plainSelect.getWhere();
-            EqualsTo projectEq = new EqualsTo();
-            projectEq.setLeftExpression(new Column("project"));
-            projectEq.setRightExpression(new StringValue(request.getHeader("project")));
-            Parenthesis parenthesis = new Parenthesis();
-            parenthesis.setExpression(where);
-            AndExpression andExpression = new AndExpression(where, parenthesis);
-            plainSelect.setWhere(andExpression);
-            targetSql = plainSelect.toString();
-            System.out.println(targetSql);
-        }
-        // 修改完成的sql 再设置回去
-        PluginUtils.MPBoundSql mpBoundSql = PluginUtils.mpBoundSql(boundSql);
-        mpBoundSql.sql(targetSql);
+        String project = request.getHeader("project");
+        if (StringUtils.isNotBlank(project)) {
+            String originSql = boundSql.getSql();
+            // 获取 Statement 执行sql的对象
+            Statement statement = null;
+            try {
+                statement = CCJSqlParserUtil.parse(originSql);
+            } catch (JSQLParserException e) {
+                throw new RuntimeException(e);
+            }
+            Select select = (Select) statement;
 
+            SelectBody selectBody = select.getSelectBody();
+            if (selectBody == null) {
+                return;
+            }
+            String targetSql = "";
+            if (selectBody instanceof PlainSelect) {
+                PlainSelect plainSelect = (PlainSelect) selectBody;
+                Expression where = plainSelect.getWhere();
+                EqualsTo projectEq = new EqualsTo();
+                projectEq.setLeftExpression(new Column("project"));
+                projectEq.setRightExpression(new StringValue(project));
+                Parenthesis parenthesis = new Parenthesis();
+                parenthesis.setExpression(where);
+                AndExpression andExpression = new AndExpression(where, parenthesis);
+                plainSelect.setWhere(andExpression);
+                targetSql = plainSelect.toString();
+                System.out.println(targetSql);
+            }
+            // 修改完成的sql 再设置回去
+            PluginUtils.MPBoundSql mpBoundSql = PluginUtils.mpBoundSql(boundSql);
+            mpBoundSql.sql(targetSql);
 
+        }
     }
 }