java110
2020-09-11 b889cb66aabba6553b7bc7027cf3d768abc58db1
java110-db/src/main/java/com/java110/db/Java110MybatisInterceptor.java
@@ -8,6 +8,7 @@
import com.java110.dto.order.OrderItemDto;
import com.java110.utils.constant.ServiceConstant;
import com.java110.utils.factory.ApplicationContextFactory;
import com.java110.utils.util.DateUtil;
import com.java110.utils.util.StringUtil;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
@@ -22,6 +23,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.http.*;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.util.*;
@@ -86,6 +88,7 @@
        if (deleteDatas != null && deleteDatas.size() > 0) {
            for (Map<String, Object> map : deleteDatas) {
                dealReturnMap(map);
                preValues.add(map);
            }
        }
@@ -157,6 +160,7 @@
        if (deleteDatas != null && deleteDatas.size() > 0) {
            for (Map<String, Object> map : deleteDatas) {
                dealReturnMap(map);
                preValues.add(map);
                afterVaule = new JSONObject();
                afterVaule.putAll(map);
@@ -187,6 +191,30 @@
        }
    }
    private void dealReturnMap(Map<String, Object> map) {
        for (String key : map.keySet()) {
            Object value = map.get(key);
            if (value instanceof String) {
                map.put(key, "'" + map.get(key) + "'");
            } else if (value instanceof Date) {
                String tmpValue = DateUtil.getFormatTimeString((Date) value, DateUtil.DATE_FORMATE_STRING_A);
                map.put(key, "'" + tmpValue + "'");
            } else if (value instanceof Timestamp) {
                Date date = new Date(((Timestamp) value).getTime());
                String tmpValue = DateUtil.getFormatTimeString(date, DateUtil.DATE_FORMATE_STRING_A);
                map.put(key, "'" + tmpValue + "'");
            } else if (value instanceof Double) {
                map.put(key, "'" + map.get(key) + "'");
            } else {
                if (value != null) {
                    map.put(key, "'" + value.toString() + "'");
                } else {
                    map.put(key, "''");
                }
            }
        }
    }
    /**
     * 处理insert 语句
     *
@@ -201,24 +229,42 @@
        JSONArray preValues = new JSONArray();
        JSONArray afterValues = new JSONArray();
        JSONObject afterValue = new JSONObject();
        String tmpTable = sql.substring(sql.toLowerCase().indexOf("into") + 4, sql.indexOf("("));
        String tmpKey = sql.substring(sql.indexOf("(") + 1, sql.indexOf(")"));
        String tmpValue = sql.substring(sql.lastIndexOf("(") + 1, sql.lastIndexOf(")"));
        String[] tmpKeys = tmpKey.split(",");
        String[] tmpValues = tmpValue.split(",");
        int valuePos = 0;
        if (sql.contains("VALUES")) {
            valuePos = sql.indexOf("VALUES") + 6;
        } else {
            valuePos = sql.indexOf("values") + 6;
        }
        String sqlValues = sql.substring(valuePos);
        //说明批操作
        if (tmpKeys.length != tmpValues.length) {
            throw new IllegalArgumentException("sql 错误 key 和value 个数不等" + sql);
        String[] sqlVauleses = sqlValues.split("\\)");
        JSONObject afterValue = null;
        for (String sqlV : sqlVauleses) {
            String tmpValue = sqlV.substring(sqlV.lastIndexOf("(") + 1);
            String[] tmpValues = tmpValue.split(",");
            afterValue = new JSONObject();
            if (tmpKeys.length != tmpValues.length) {
                throw new IllegalArgumentException("sql 错误 key 和value 个数不等" + sql);
            }
            if (tmpKeys.length < 1) {
                throw new IllegalArgumentException("sql 错误 未找到key" + sql);
            }
            for (int keyIndex = 0; keyIndex < tmpKeys.length; keyIndex++) {
                if ("''".equals(tmpValues[keyIndex])) {
                    continue;
                }
                afterValue.put(tmpKeys[keyIndex], tmpValues[keyIndex]);
            }
            afterValues.add(afterValue);
        }
        if (tmpKeys.length < 1) {
            throw new IllegalArgumentException("sql 错误 未找到key" + sql);
        }
        for (int keyIndex = 0; keyIndex < tmpKeys.length; keyIndex++) {
            afterValue.put(tmpKeys[keyIndex], tmpValues[keyIndex]);
        }
        afterValues.add(afterValue);
        JSONObject logText = new JSONObject();
        logText.put("preValue", preValues);
@@ -293,10 +339,22 @@
            if (obj != null) {
                value = obj.toString();
            } else {
                value = "";
                value = "''";
            }
        }
        return value;
    }
    public static void main(String[] args) {
        String tmpKey = " prime_rate,detail_id,receivable_amount,cycles,remark,status_cd,received_amount,community_id,b_id,fee_id,state";
        String tmpValue = "'1.00','912020080411040001','1500.0','1.0',,'0','1500.0','7020181217000001','-1',,";
        String[] tmpKeys = tmpKey.split(",");
        String[] tmpValues = tmpValue.split(",");
        if (tmpKeys.length != tmpValues.length) {
            throw new IllegalArgumentException("sql 错误 key 和value 个数不等");
        }
    }
}