wuxw
2019-09-08 599453aa4944cd1d67e6a5894f3833b84ca2d36c
java110-service/src/main/java/com/java110/service/smo/impl/QueryServiceSMOImpl.java
@@ -12,11 +12,16 @@
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.service.context.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.apache.ibatis.ognl.Ognl;
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 org.springframework.beans.factory.annotation.Autowired;
@@ -172,25 +177,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 +248,9 @@
            List<Object> currentParams = new ArrayList<Object>();
            String currentSql = sqlObj.getString(dataQuery.getTemplateKey());
            //处理 if 判断
            currentSql = dealSqlIf(currentSql, params);
            String[] sqls = currentSql.split("#");
            String currentSqlNew = "";
            for (int sqlIndex = 0; sqlIndex < sqls.length; sqlIndex++) {
@@ -321,6 +321,57 @@
    }
    /**
     * 处理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) {
            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);
            Object value = Ognl.getValue(condObj, requestParams);
            if (value instanceof Boolean) {
                conditionResult = (Boolean) value;
            } else {
                throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR, "配置错误,if语句配置错误 " + condition);
            }
            if (conditionResult) {
                newSql.append(root.getText());
            }
        }
        return newSql.toString();
    }
    /**
     * 处理JSONArray
     *
     * @param objs