java110
2022-01-15 22bc84f7849e86d3028d671cf0993b376309e39a
java110-service/src/main/java/com/java110/service/smo/impl/QueryServiceSMOImpl.java
old mode 100644 new mode 100755
@@ -4,18 +4,18 @@
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.java110.core.factory.DataTransactionFactory;
import com.java110.db.dao.IQueryServiceDAO;
import com.java110.entity.service.ServiceSql;
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.core.factory.DataTransactionFactory;
import com.java110.utils.log.LoggerEngine;
import com.java110.utils.util.Assert;
import com.java110.utils.util.StringUtil;
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;
@@ -23,7 +23,7 @@
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;
@@ -118,6 +118,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>("未包含sql信息", HttpStatus.BAD_REQUEST);
            }
            int flag = queryServiceDAOImpl.updateSql(sql, null);
        }
        return new ResponseEntity<String>("回退成功", HttpStatus.OK);
    }
    /**
     * {"PARAM:"{
@@ -284,7 +302,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);
@@ -324,6 +342,78 @@
    }
    /**
     * 执行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", params);
            return JSONObject.parseObject(interpreter.eval("execute(dataQuery)").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开始处理sql中的<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("解析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
@@ -340,11 +430,11 @@
        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")){
            if (StringUtil.isNullOrNone(oSql) || !oSql.contains("<if")) {
                newSql.append(oSql);
                continue;
            }
            if (!oSql.startsWith("<if")) {