From fc1090ef8e44e8cf010c3d352482d78d389ba4c8 Mon Sep 17 00:00:00 2001
From: wuxw <928255095@qq.com>
Date: 星期三, 11 五月 2022 15:52:02 +0800
Subject: [PATCH] 雨花代码

---
 java110-service/src/main/java/com/java110/service/smo/impl/QueryServiceSMOImpl.java |  199 ++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 176 insertions(+), 23 deletions(-)

diff --git a/java110-service/src/main/java/com/java110/service/smo/impl/QueryServiceSMOImpl.java b/java110-service/src/main/java/com/java110/service/smo/impl/QueryServiceSMOImpl.java
old mode 100644
new mode 100755
index 773b4dc..ee69e8d
--- a/java110-service/src/main/java/com/java110/service/smo/impl/QueryServiceSMOImpl.java
+++ b/java110-service/src/main/java/com/java110/service/smo/impl/QueryServiceSMOImpl.java
@@ -4,21 +4,27 @@
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.alibaba.fastjson.JSONPath;
-import com.java110.common.cache.ServiceSqlCache;
-import com.java110.common.constant.CommonConstant;
-import com.java110.common.constant.ResponseConstant;
-import com.java110.common.exception.BusinessException;
 import com.java110.core.factory.DataTransactionFactory;
-import com.java110.common.log.LoggerEngine;
-import com.java110.common.util.Assert;
-import com.java110.common.util.StringUtil;
-import com.java110.entity.service.DataQuery;
+import com.java110.db.dao.IQueryServiceDAO;
 import com.java110.entity.service.ServiceSql;
-import com.java110.service.dao.IQueryServiceDAO;
+import com.java110.service.context.DataQuery;
 import com.java110.service.smo.IQueryServiceSMO;
+import com.java110.utils.cache.ServiceSqlCache;
+import com.java110.utils.constant.CommonConstant;
+import com.java110.utils.constant.ResponseConstant;
+import com.java110.utils.exception.BusinessException;
+import com.java110.utils.log.LoggerEngine;
+import com.java110.utils.util.Assert;
+import com.java110.utils.util.StringUtil;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.ibatis.ognl.Ognl;
+import org.apache.ibatis.ognl.OgnlContext;
+import org.apache.ibatis.ognl.OgnlException;
+import org.dom4j.DocumentException;
+import org.dom4j.DocumentHelper;
+import org.dom4j.Element;
 import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import com.java110.core.log.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
@@ -114,6 +120,24 @@
 
     }
 
+    @Override
+    public ResponseEntity<String> fallBack(String fallBackInfo) throws BusinessException {
+
+        JSONArray params = JSONArray.parseArray(fallBackInfo);
+        for (int paramIndex = 0; paramIndex < params.size(); paramIndex++) {
+            JSONObject param = params.getJSONObject(paramIndex);
+            String sql = param.getString("fallBackSql");
+            if (StringUtil.isEmpty(sql)) {
+                return new ResponseEntity<String>("鏈寘鍚玸ql淇℃伅", HttpStatus.BAD_REQUEST);
+            }
+            int flag = queryServiceDAOImpl.updateSql(sql, null);
+
+        }
+        return new ResponseEntity<String>("鍥為��鎴愬姛", HttpStatus.OK);
+
+    }
+
+
     /**
      * {"PARAM:"{
      * "param1": "$.a.#A#Object",
@@ -172,25 +196,17 @@
      */
     private void doExecuteJava(DataQuery dataQuery) throws BusinessException {
         try {
-            JSONObject params = dataQuery.getRequestParams();
+            //JSONObject params = dataQuery.getRequestParams();
             String javaCode = dataQuery.getServiceSql().getJavaScript();
 
             Interpreter interpreter = new Interpreter();
             interpreter.eval(javaCode);
-            String param = "";
-            for (String key : params.keySet()) {
-                param += (params.getString(key) + ",");
-            }
-
-            if (param.endsWith(",")) {
-                param = param.substring(0, param.length() - 1);
-            }
-
+            interpreter.set("dataQuery", dataQuery);
             dataQuery.setResponseInfo(DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_CODE_SUCCESS,
-                    "鎴愬姛", JSONObject.parseObject(interpreter.eval("execute(" + param + ")").toString())));
+                    "鎴愬姛", JSONObject.parseObject(interpreter.eval("execute(dataQuery)").toString())));
         } catch (Exception e) {
             logger.error("鏁版嵁浜や簰寮傚父锛�", e);
-            throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR, "鏁版嵁浜や簰寮傚父銆傘�傘��");
+            throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR, "鏁版嵁浜や簰寮傚父," + e.getMessage());
         }
     }
 
@@ -251,6 +267,12 @@
             List<Object> currentParams = new ArrayList<Object>();
 
             String currentSql = sqlObj.getString(dataQuery.getTemplateKey());
+            //澶勭悊 if 鍒ゆ柇
+            logger.debug("dealSqlIf寮�濮嬪鐞唖ql涓殑<if>鑺傜偣 " + currentSql + " 鍏ュ弬:" + params.toJSONString());
+            currentSql = dealSqlIf(currentSql, params);
+            logger.debug("dealSqlIf澶勭悊瀹屾垚sql涓殑<if>鑺傜偣 " + currentSql + " 鍏ュ弬:" + params.toJSONString());
+
+
             String[] sqls = currentSql.split("#");
             String currentSqlNew = "";
             for (int sqlIndex = 0; sqlIndex < sqls.length; sqlIndex++) {
@@ -281,7 +303,7 @@
                         param = params.getInteger(sqls[sqlIndex]);
                     }
                     //杩欓噷瀵� page 鍜� rows 鐗规畩澶勭悊 锛岀洰鍓嶆病鏈夋兂鍒板叾浠栫殑鍔炴硶
-                    if (StringUtils.isNumeric(param.toString()) && "page,rows".contains(sqls[sqlIndex])) {
+                    if (StringUtils.isNumeric(param.toString()) && "page,rows,row".contains(sqls[sqlIndex])) {
                         param = Integer.parseInt(param.toString());
                     }
                     currentParams.add(param);
@@ -321,6 +343,137 @@
     }
 
     /**
+     * 鎵цjava鑴氭湰
+     *
+     * @param javaCode
+     * @throws BusinessException
+     */
+    public JSONObject execJava(JSONObject params, String javaCode) throws BusinessException {
+        try {
+            //JSONObject params = dataQuery.getRequestParams();
+            Interpreter interpreter = new Interpreter();
+            interpreter.eval(javaCode);
+            interpreter.set("params", new Object[]{params,queryServiceDAOImpl});
+            //interpreter.set("queryServiceDAOImpl",queryServiceDAOImpl);
+            return JSONObject.parseObject(interpreter.eval("execute(params,queryServiceDAOImpl)").toString());
+        } catch (Exception e) {
+            logger.error("鏁版嵁浜や簰寮傚父锛�", e);
+            throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR, "鏁版嵁浜や簰寮傚父," + e.getMessage());
+        }
+    }
+
+    @Override
+    public JSONObject execQuerySql(JSONObject params, String currentSql) throws BusinessException {
+        List<Map<String, Object>> results = null;
+        List<String> columns = new ArrayList<>();
+        try {
+            List<Object> currentParams = new ArrayList<Object>();
+            //澶勭悊 if 鍒ゆ柇
+            logger.debug("dealSqlIf寮�濮嬪鐞唖ql涓殑<if>鑺傜偣 " + currentSql + " 鍏ュ弬:" + params.toJSONString());
+            currentSql = dealSqlIf(currentSql, params);
+            logger.debug("dealSqlIf澶勭悊瀹屾垚sql涓殑<if>鑺傜偣 " + currentSql + " 鍏ュ弬:" + params.toJSONString());
+            String[] sqls = currentSql.split("#");
+            String currentSqlNew = "";
+            for (int sqlIndex = 0; sqlIndex < sqls.length; sqlIndex++) {
+                if (sqlIndex % 2 == 0) {
+                    currentSqlNew += sqls[sqlIndex];
+                    continue;
+                }
+                currentSqlNew += "?";
+                Object param = params.getString(sqls[sqlIndex]);
+                if (params.get(sqls[sqlIndex]) instanceof Integer) {
+                    param = params.getInteger(sqls[sqlIndex]);
+                }
+                //杩欓噷瀵� page 鍜� rows 鐗规畩澶勭悊 锛岀洰鍓嶆病鏈夋兂鍒板叾浠栫殑鍔炴硶
+                if (StringUtils.isNumeric(param.toString()) && "page,rows,row".contains(sqls[sqlIndex])) {
+                    param = Integer.parseInt(param.toString());
+                }
+                currentParams.add(param);
+            }
+            results = queryServiceDAOImpl.executeSql(currentSqlNew, currentParams.toArray(), columns);
+        } catch (Exception e) {
+            logger.error("瑙f瀽sql 寮傚父", e);
+            throw new BusinessException("1999", e.getLocalizedMessage());
+        }
+        JSONArray data = null;
+        if (results == null || results.size() < 1) {
+            data = new JSONArray();
+        } else {
+            data = JSONArray.parseArray(JSONArray.toJSONString(results));
+        }
+        JSONArray th = null;
+        if (columns.size() < 1) {
+            th = new JSONArray();
+        } else {
+            th = JSONArray.parseArray(JSONArray.toJSONString(columns));
+        }
+
+
+        JSONObject paramOut = new JSONObject();
+        paramOut.put("th", th);
+        paramOut.put("td", data);
+        return paramOut;
+    }
+
+    /**
+     * 澶勭悊SQL璇彞
+     *
+     * @param oldSql select * from s_a a
+     *               where <if test="name != null && name != ''">
+     *               a.name = #name#
+     *               </if>
+     * @return
+     */
+    public String dealSqlIf(String oldSql, JSONObject requestParams) throws DocumentException, OgnlException {
+        StringBuffer newSql = new StringBuffer();
+        String tmpSql = "";
+        Boolean conditionResult = false;
+        // 鏈寘鍚� 鏉′欢璇彞
+        if (!oldSql.contains("<if")) {
+            return oldSql;
+        }
+        String[] oSqls = oldSql.split("</if>");
+        for (String oSql : oSqls) {
+            logger.debug("澶勭悊if 鑺傜偣锛屽綋鍓嶅鐞嗙殑oSql=" + oSql + "鎬荤殑oSqls = " + oSqls);
+            if (StringUtil.isNullOrNone(oSql) || !oSql.contains("<if")) {
+                newSql.append(oSql);
+                continue;
+            }
+            if (!oSql.startsWith("<if")) {
+                newSql.append(oSql.substring(0, oSql.indexOf("<if")));
+            }
+
+            tmpSql = oSql.substring(oSql.indexOf("<if")) + "</if>";
+
+            Element root = DocumentHelper.parseText(tmpSql).getRootElement();
+
+            String condition = root.attribute("test").getValue();
+
+            Object condObj = Ognl.parseExpression(condition);
+
+            OgnlContext context = new OgnlContext(null,null,new DefaultMemberAccess(true));
+
+            Object value = Ognl.getValue(condObj,context, requestParams);
+
+            if (value instanceof Boolean) {
+                conditionResult = (Boolean) value;
+            } else {
+                throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR, "閰嶇疆閿欒锛宨f璇彞閰嶇疆閿欒 " + condition);
+            }
+
+            if (conditionResult) {
+                newSql.append(root.getText());
+            }
+
+
+        }
+
+
+        return newSql.toString().replace("&gt;", ">").replace("&lt;", "<");
+
+    }
+
+    /**
      * 澶勭悊JSONArray
      *
      * @param objs

--
Gitblit v1.8.0