wuxw7
2018-05-22 9e0fa31a2b81a9d2fd2588b78b6de637f0352639
java110-service/src/main/java/com/java110/service/smo/impl/QueryServiceSMOImpl.java
@@ -8,9 +8,10 @@
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;
@@ -19,7 +20,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;
@@ -45,6 +46,13 @@
            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);
@@ -56,7 +64,7 @@
            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()));
        }
@@ -83,7 +91,7 @@
            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()));
        }
@@ -100,11 +108,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);
@@ -115,13 +125,15 @@
                        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, "数据交互失败");
                }
            }
@@ -129,6 +141,9 @@
            logger.error("数据交互异常:",e);
            throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR,"数据交互异常。。。");
        }
        dataQuery.setResponseInfo(DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_CODE_SUCCESS,
                "成功",business));
    }
    /**
@@ -152,7 +167,7 @@
                param = param.substring(0,param.length()-1);
            }
            dataQuery.setResponseInfo(ResponseTemplateUtil.createBusinessResponseJson(ResponseConstant.RESULT_CODE_SUCCESS,
            dataQuery.setResponseInfo(DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_CODE_SUCCESS,
                    "成功",JSONObject.parseObject(interpreter.eval("execute("+param+")").toString())));
        }catch (Exception e){
            logger.error("数据交互异常:",e);
@@ -180,15 +195,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;
@@ -201,7 +212,7 @@
            }
        }
        dataQuery.setResponseInfo(ResponseTemplateUtil.createBusinessResponseJson(ResponseConstant.RESULT_CODE_SUCCESS,
        dataQuery.setResponseInfo(DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_CODE_SUCCESS,
                "成功",business));
    }
@@ -215,6 +226,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("#");
@@ -225,27 +237,51 @@
                    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) {
                if(StringUtil.isNullOrNone(values[1])){
                    return ;
                }
                obj.put(values[1], new JSONObject());
                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){
@@ -284,8 +320,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;
    }
@@ -305,7 +368,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)));
    }