From 0819935913aecfb9e45b64d113a30820ba762f1e Mon Sep 17 00:00:00 2001
From: wuxw7 <wuxw7@asiainfo.com>
Date: 星期日, 18 十一月 2018 23:13:06 +0800
Subject: [PATCH] console 修改登录时走api 服务

---
 java110-service/src/main/java/com/java110/service/smo/impl/QueryServiceSMOImpl.java |  171 +++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 143 insertions(+), 28 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 f56171a..3849b2b 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,18 +8,22 @@
 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.ResponseTemplateUtil;
+import com.java110.common.util.StringUtil;
 import com.java110.entity.service.DataQuery;
 import com.java110.entity.service.ServiceSql;
 import com.java110.service.dao.IQueryServiceDAO;
 import com.java110.service.smo.IQueryServiceSMO;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
 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;
@@ -36,6 +41,7 @@
     @Override
     public void commonQueryService(DataQuery dataQuery) throws BusinessException {
         //鏌ヨ缂撳瓨鏌ヨ 瀵瑰簲澶勭悊鐨凷erviceSql
+        ResponseEntity<String> responseEntity = null;
         try {
             ServiceSql currentServiceSql = ServiceSqlCache.getServiceSql(dataQuery.getServiceCode());
             if (currentServiceSql == null) {
@@ -44,17 +50,31 @@
             if ("".equals(currentServiceSql.getQueryModel())) {
                 throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR,"閰嶇疆鏈嶅姟 serviceCode = " + dataQuery.getServiceCode() + " 閿欒锛屾湭閰嶇疆QueryModel,璇疯仈绯荤鐞嗗憳");
             }
+            //璇锋眰鍙傛暟鏍¢獙
+            List<String> sysParams = currentServiceSql.getParamList();
+            for(String param : sysParams) {
+                if(!dataQuery.getRequestParams().containsKey(param)){
+                    throw new BusinessException(ResponseConstant.RESULT_PARAM_ERROR,"璇锋眰鍙傛暟閿欒锛岃姹傛姤鏂囦腑鏈寘鍚弬鏁� " + param + " 淇℃伅");
+                }
+            }
             dataQuery.setServiceSql(currentServiceSql);
             if (CommonConstant.QUERY_MODEL_SQL.equals(currentServiceSql.getQueryModel())) {
                 doExecuteSql(dataQuery);
-                return;
+            }else if(CommonConstant.QUERY_MODE_JAVA.equals(currentServiceSql.getQueryModel())){
+                doExecuteJava(dataQuery);
+            }else {
+                doExecuteProc(dataQuery);
             }
-            doExecuteProc(dataQuery);
+            responseEntity = new ResponseEntity<String>(dataQuery.getResponseInfo().toJSONString(), HttpStatus.OK);
         }catch (BusinessException e){
             logger.error("鍏敤鏌ヨ寮傚父锛�",e);
-            dataQuery.setResponseInfo(ResponseTemplateUtil.createBusinessResponseJson(ResponseConstant.RESULT_PARAM_ERROR,
-                    e.getMessage()));
+            /*dataQuery.setResponseInfo(DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_PARAM_ERROR,
+                    e.getMessage()));*/
+            responseEntity = new ResponseEntity<String>("璇锋眰鍙戠敓寮傚父锛�"+e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+        }finally {
+            dataQuery.setResponseEntity(responseEntity);
         }
+
 
     }
     @Override
@@ -72,11 +92,14 @@
             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(ResponseTemplateUtil.createBusinessResponseJson(ResponseConstant.RESULT_PARAM_ERROR,
+            dataQuery.setResponseInfo(DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_PARAM_ERROR,
                     e.getMessage()));
         }
 
@@ -93,11 +116,13 @@
      * @param dataQuery
      */
     private void doExecuteUpdateSql(DataQuery dataQuery) throws BusinessException{
-
+        JSONObject business = null;
         try {
             JSONObject params = dataQuery.getRequestParams();
             JSONObject sqlObj = JSONObject.parseObject(dataQuery.getServiceSql().getSql());
-
+            JSONObject templateObj = JSONObject.parseObject(dataQuery.getServiceSql().getTemplate());
+            business = JSONObject.parseObject(templateObj.getString("TEMPLATE"));
+            List<Object> currentParams = new ArrayList<Object>();
             String currentSql = "";
             for(String key : sqlObj.keySet()) {
                 currentSql = sqlObj.getString(key);
@@ -108,16 +133,50 @@
                         currentSqlNew += sqls[sqlIndex];
                         continue;
                     }
-                    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]) + "'";
                 }
 
-                int flag = queryServiceDAOImpl.updateSql(currentSqlNew);
+                int flag = queryServiceDAOImpl.updateSql(currentSqlNew,currentParams.toArray());
 
                 if (flag < 1) {
-                    throw new BusinessException(ResponseConstant.RESULT_PARAM_ERROR, "璋冪敤鎺ュ彛澶辫触");
+                    throw new BusinessException(ResponseConstant.RESULT_PARAM_ERROR, "鏁版嵁浜や簰澶辫触");
                 }
             }
 
+        }catch (Exception e){
+            logger.error("鏁版嵁浜や簰寮傚父锛�",e);
+            throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR,"鏁版嵁浜や簰寮傚父銆傘�傘��");
+        }
+
+        dataQuery.setResponseInfo(DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_CODE_SUCCESS,
+                "鎴愬姛",business));
+    }
+
+    /**
+     * 鎵ц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,"鏁版嵁浜や簰寮傚父銆傘�傘��");
@@ -144,15 +203,11 @@
         JSONArray currentJsonArr = null;
         for(String key:templateParams.keySet()){
             template = templateParams.getString(key);
-            if(!template.startsWith("$.")){
-                throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR,"template 閰嶇疆 涓嶆纭紝value 蹇呴』浠�$.寮�澶�");
-            }
-            values = template.split("#");
 
-            if(values == null || values.length != 3){
-                throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR,"template 閰嶇疆 涓嶆纭紝value 蹇呴』鏈変袱涓�#鍙�");
-            }
-            Object o = JSONPath.eval(business,values[0]);
+            values = judgeResponseTemplate(template);
+
+            Object o = JSONPath.eval(business, values[0]);
+
             dataQuery.setTemplateKey(key);
             if(o instanceof JSONObject){
                 currentJsonObj = (JSONObject)o;
@@ -165,7 +220,7 @@
             }
         }
 
-        dataQuery.setResponseInfo(ResponseTemplateUtil.createBusinessResponseJson(ResponseConstant.RESULT_CODE_SUCCESS,
+        dataQuery.setResponseInfo(DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_CODE_SUCCESS,
                 "鎴愬姛",business));
     }
 
@@ -179,6 +234,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("#");
@@ -189,27 +245,59 @@
                     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 += "?";
+                    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".contains(sqls[sqlIndex])){
+                        param = Integer.parseInt(param.toString());
+                    }
+                    currentParams.add(param);
+                    //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());
+                if(StringUtil.isNullOrNone(values[1])){
+                    return ;
+                }
+                obj.put(values[1], values[2].equals("Object")?new JSONObject():new JSONArray());
                 return;
             }
             if (values[2].equals("Object")) {
+                if(StringUtil.isNullOrNone(values[1])){
+                    obj.putAll(JSONObject.parseObject(JSONObject.toJSONString(results.get(0))));
+                    return ;
+                }
                 obj.put(values[1], JSONObject.parseObject(JSONObject.toJSONString(results.get(0))));
             } else if (values[2].equals("Array")) {
+                if(StringUtil.isNullOrNone(values[1])){
+                    JSONArray datas = JSONArray.parseArray(JSONArray.toJSONString(results));
+                    for(int dataIndex = 0;dataIndex < datas.size();dataIndex ++){
+                        obj.putAll(datas.getJSONObject(dataIndex));
+                    }
+                    return ;
+                }
                 obj.put(values[1], JSONArray.parseArray(JSONArray.toJSONString(results)));
             }
         }catch (Exception e){
@@ -248,8 +336,35 @@
             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)));
+    }
+
+
+    /**
+     * 鏍¢獙 杩斿洖妯℃澘
+     * @param template
+     * @return
+     * @throws BusinessException
+     */
+    private String[] judgeResponseTemplate(String template) throws BusinessException{
+
+
+        if(!template.startsWith("$.")){
+            throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR,"template 閰嶇疆 涓嶆纭紝value 蹇呴』浠�$.寮�澶�");
+        }
+
+        String[] values = template.split("#");
+
+        if(values == null || values.length != 3){
+            throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR,"template 閰嶇疆 涓嶆纭紝value 蹇呴』鏈変袱涓�#鍙�");
+        }
+
+        if(StringUtil.isNullOrNone(values[1]) && !"$.##Object".equals(template) && !"$.##Array".equals(template)){
+            throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR,"template 閰嶇疆 涓嶆纭紝鐩墠鍙敮鎸� $.##Object 鍜� $.##Array ");
+        }
+
+        return values;
     }
 
 
@@ -269,7 +384,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