package com.elab.spring.intercept;

import com.elab.core.spring.method.SQLEventHandler;
import com.elab.core.sql.config.SqlCommandType;
import com.elab.log.utils.EventProcessUtils;

/**
 * SQL 事件触发
 *
 * @author liukx
 */
public class SQLMonitorEventHandler implements SQLEventHandler {

    private String SQL_MONITOR_TYPE = "SQL_MONITOR_NODE";

    private Integer affectNumber = 100;

    public Integer getAffectNumber() {
        return affectNumber;
    }

    public void setAffectNumber(Integer affectNumber) {
        this.affectNumber = affectNumber;
    }

    @Override
    public void sqlInvokeBefore(SqlCommandType sqlCommandType, String sqlId, String table, String sql, Object params) {

    }

    @Override
    public void sqlInvokeAfter(SqlCommandType sqlCommandType, String sqlId, String table, String sql, Object params, Object result) {
        if (SqlCommandType.UPDATE == sqlCommandType || SqlCommandType.DELETE == sqlCommandType) {
            if (result!= null && result instanceof Number && Long.valueOf(result.toString()) > getAffectNumber()) {
                EventProcessUtils.write(SQL_MONITOR_TYPE, sqlCommandType.name() + "_" + table + "_" + result.toString());
            }
        }
    }
}
