From e3d2c10dcd06fa0075794435829ac25fd3fc12a7 Mon Sep 17 00:00:00 2001
From: wuxw7 <wuxw7@asiainfo.com>
Date: 星期一, 30 四月 2018 22:09:57 +0800
Subject: [PATCH] 加入控制中心服务

---
 java110-service/src/main/java/com/java110/service/smo/impl/QueryServiceSMOImpl.java |  153 ++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 144 insertions(+), 9 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
index 0421835..24070f2 100644
--- 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
@@ -1,5 +1,6 @@
 package com.java110.service.smo.impl;
 
+import bsh.Interpreter;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.alibaba.fastjson.JSONPath;
@@ -7,9 +8,9 @@
 import com.java110.common.constant.CommonConstant;
 import com.java110.common.constant.ResponseConstant;
 import com.java110.common.exception.BusinessException;
+import com.java110.common.factory.DataTransactionFactory;
 import com.java110.common.log.LoggerEngine;
 import com.java110.common.util.Assert;
-import com.java110.common.util.ResponseTemplateUtil;
 import com.java110.entity.service.DataQuery;
 import com.java110.entity.service.ServiceSql;
 import com.java110.service.dao.IQueryServiceDAO;
@@ -18,7 +19,7 @@
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
-import java.util.HashMap;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
@@ -48,16 +49,118 @@
             if (CommonConstant.QUERY_MODEL_SQL.equals(currentServiceSql.getQueryModel())) {
                 doExecuteSql(dataQuery);
                 return;
+            }else if(CommonConstant.QUERY_MODE_JAVA.equals(currentServiceSql.getQueryModel())){
+                doExecuteJava(dataQuery);
+                return ;
             }
             doExecuteProc(dataQuery);
         }catch (BusinessException e){
             logger.error("鍏敤鏌ヨ寮傚父锛�",e);
-            dataQuery.setResponseInfo(ResponseTemplateUtil.createBusinessResponseJson(ResponseConstant.RESULT_PARAM_ERROR,
+            dataQuery.setResponseInfo(DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_PARAM_ERROR,
+                    e.getMessage()));
+        }
+
+    }
+    @Override
+    public void commonDoService(DataQuery dataQuery) throws BusinessException {
+        //鏌ヨ缂撳瓨鏌ヨ 瀵瑰簲澶勭悊鐨凷erviceSql
+        try {
+            ServiceSql currentServiceSql = ServiceSqlCache.getServiceSql(dataQuery.getServiceCode());
+            if (currentServiceSql == null) {
+                throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR,"鏈彁渚涜鏈嶅姟 serviceCode = " + dataQuery.getServiceCode());
+            }
+            if ("".equals(currentServiceSql.getQueryModel())) {
+                throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR,"閰嶇疆鏈嶅姟 serviceCode = " + dataQuery.getServiceCode() + " 閿欒锛屾湭閰嶇疆QueryModel,璇疯仈绯荤鐞嗗憳");
+            }
+            dataQuery.setServiceSql(currentServiceSql);
+            if (CommonConstant.QUERY_MODEL_SQL.equals(currentServiceSql.getQueryModel())) {
+                doExecuteUpdateSql(dataQuery);
+                return;
+            }else if(CommonConstant.QUERY_MODE_JAVA.equals(currentServiceSql.getQueryModel())){
+                doExecuteJava(dataQuery);
+                return ;
+            }
+            doExecuteUpdateProc(dataQuery);
+        }catch (BusinessException e){
+            logger.error("鍏敤鏌ヨ寮傚父锛�",e);
+            dataQuery.setResponseInfo(DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_PARAM_ERROR,
                     e.getMessage()));
         }
 
     }
 
+    /**
+     * {"PARAM:"{
+     "param1": "$.a.#A#Object",
+     "param2": "$.a.b.A#B#Array",
+     "param3": "$.a.b.c.A.B#C#Array"
+     },"TEMPLATE":"{}"
+     }
+     * 鎵цsql
+     * @param dataQuery
+     */
+    private void doExecuteUpdateSql(DataQuery dataQuery) throws BusinessException{
+
+        try {
+            JSONObject params = dataQuery.getRequestParams();
+            JSONObject sqlObj = JSONObject.parseObject(dataQuery.getServiceSql().getSql());
+            List<Object> currentParams = new ArrayList<Object>();
+            String currentSql = "";
+            for(String key : sqlObj.keySet()) {
+                currentSql = sqlObj.getString(key);
+                String[] sqls = currentSql.split("#");
+                String currentSqlNew = "";
+                for (int sqlIndex = 0; sqlIndex < sqls.length; sqlIndex++) {
+                    if (sqlIndex % 2 == 0) {
+                        currentSqlNew += sqls[sqlIndex];
+                        continue;
+                    }
+                    currentSqlNew += "?";
+                    currentParams.add(params.get(sqls[sqlIndex]) instanceof Integer ? params.getInteger(sqls[sqlIndex]) : "'" + params.getString(sqls[sqlIndex]) + "'");
+                    //currentSqlNew += params.get(sqls[sqlIndex]) instanceof Integer ? params.getInteger(sqls[sqlIndex]) : "'" + params.getString(sqls[sqlIndex]) + "'";
+                }
+
+                int flag = queryServiceDAOImpl.updateSql(currentSqlNew,currentParams.toArray());
+
+                if (flag < 1) {
+                    throw new BusinessException(ResponseConstant.RESULT_PARAM_ERROR, "鏁版嵁浜や簰澶辫触");
+                }
+            }
+
+        }catch (Exception e){
+            logger.error("鏁版嵁浜や簰寮傚父锛�",e);
+            throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR,"鏁版嵁浜や簰寮傚父銆傘�傘��");
+        }
+    }
+
+    /**
+     * 鎵цjava鑴氭湰
+     * @param dataQuery
+     * @throws BusinessException
+     */
+    private void doExecuteJava(DataQuery dataQuery) throws BusinessException{
+        try {
+            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);
+            }
+
+            dataQuery.setResponseInfo(DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_CODE_SUCCESS,
+                    "鎴愬姛",JSONObject.parseObject(interpreter.eval("execute("+param+")").toString())));
+        }catch (Exception e){
+            logger.error("鏁版嵁浜や簰寮傚父锛�",e);
+            throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR,"鏁版嵁浜や簰寮傚父銆傘�傘��");
+        }
+    }
     /**
      * {"PARAM:"{
      "param1": "$.a.#A#Object",
@@ -100,7 +203,7 @@
             }
         }
 
-        dataQuery.setResponseInfo(ResponseTemplateUtil.createBusinessResponseJson(ResponseConstant.RESULT_CODE_SUCCESS,
+        dataQuery.setResponseInfo(DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_CODE_SUCCESS,
                 "鎴愬姛",business));
     }
 
@@ -114,6 +217,7 @@
         try {
             JSONObject params = dataQuery.getRequestParams();
             JSONObject sqlObj = JSONObject.parseObject(dataQuery.getServiceSql().getSql());
+            List<Object> currentParams = new ArrayList<Object>();
 
             String currentSql = sqlObj.getString(dataQuery.getTemplateKey());
             String[] sqls = currentSql.split("#");
@@ -124,19 +228,29 @@
                     continue;
                 }
                 if (sqls[sqlIndex].startsWith("PARENT_")) {
+                    if(obj.isEmpty()){
+                        currentSqlNew += "?";
+                        currentParams.add("''");
+                        continue;
+                    }
                     for (String key : obj.keySet()) {
                         if (sqls[sqlIndex].substring("PARENT_".length()).equals(key)) {
-                            currentSqlNew += obj.get(key) instanceof Integer
-                                    ? obj.getInteger(key) : "'" + obj.getString(key) + "'";
+                            /*currentSqlNew += obj.get(key) instanceof Integer
+                                    ? obj.getInteger(key) : "'" + obj.getString(key) + "'";*/
+                            currentSqlNew += "?";
+                            currentParams.add(obj.get(key) instanceof Integer
+                                    ? obj.getInteger(key) : "" + obj.getString(key) + "");
                             continue;
                         }
                     }
                 } else {
-                    currentSqlNew += params.get(sqls[sqlIndex]) instanceof Integer ? params.getInteger(sqls[sqlIndex]) : "'" + params.getString(sqls[sqlIndex]) + "'";
+                    currentSqlNew += "?";
+                    currentParams.add(params.get(sqls[sqlIndex]) instanceof Integer ? params.getInteger(sqls[sqlIndex]) : "" + params.getString(sqls[sqlIndex]) + "");
+                    //currentSqlNew += params.get(sqls[sqlIndex]) instanceof Integer ? params.getInteger(sqls[sqlIndex]) : "'" + params.getString(sqls[sqlIndex]) + "'";
                 }
             }
 
-            List<Map> results = queryServiceDAOImpl.executeSql(currentSqlNew);
+            List<Map<String,Object>> results = queryServiceDAOImpl.executeSql(currentSqlNew, currentParams.toArray());
 
             if (results == null || results.size() == 0) {
                 obj.put(values[1], new JSONObject());
@@ -171,6 +285,27 @@
      * 鎵ц瀛樺偍
      * @param dataQuery
      */
+    private void doExecuteUpdateProc(DataQuery dataQuery){
+        Map info = new TreeMap();
+        info.put("procName",dataQuery.getServiceSql().getProc());
+        JSONObject params = dataQuery.getRequestParams();
+        info.putAll(params);
+
+        String jsonStr = queryServiceDAOImpl.updateProc(info);
+
+        if(!Assert.isJsonObject(jsonStr)){
+            throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR,"瀛樺偍杩囩▼ procName = " + dataQuery.getServiceSql().getProc() + " 杩斿洖缁撴灉涓嶆槸Json鏍煎紡");
+        }
+
+        dataQuery.setResponseInfo(DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_CODE_SUCCESS,
+                "鎴愬姛",JSONObject.parseObject(jsonStr)));
+    }
+
+
+    /**
+     * 鎵ц瀛樺偍
+     * @param dataQuery
+     */
     private void doExecuteProc(DataQuery dataQuery){
         Map info = new TreeMap();
         info.put("procName",dataQuery.getServiceSql().getProc());
@@ -183,7 +318,7 @@
             throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR,"瀛樺偍杩囩▼ procName = " + dataQuery.getServiceSql().getProc() + " 杩斿洖缁撴灉涓嶆槸Json鏍煎紡");
         }
 
-        dataQuery.setResponseInfo(ResponseTemplateUtil.createBusinessResponseJson(ResponseConstant.RESULT_CODE_SUCCESS,
+        dataQuery.setResponseInfo(DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_CODE_SUCCESS,
                 "鎴愬姛",JSONObject.parseObject(jsonStr)));
     }
 

--
Gitblit v1.8.0