package com.yanzuoguang.db.impl;

import com.yanzuoguang.db.DbPrintSql;
import com.yanzuoguang.db.DbPrintSqlExcept;
import com.yanzuoguang.util.log.Log;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 打印SQL语句日志工具类
 *
 * @author 颜佐光
 */
@Component
public class DbExecutePrintSql {

    private final List<DbPrintSql> dbPrintSqlList;
    private final List<DbPrintSqlExcept> dbPrintSqlExceptList;

    public DbExecutePrintSql(List<DbPrintSql> dbPrintSqlList, List<DbPrintSqlExcept> dbPrintSqlExceptList) {
        this.dbPrintSqlList = dbPrintSqlList;
        this.dbPrintSqlExceptList = dbPrintSqlExceptList;
    }


    /**
     * 打印SQL语句
     *
     * @param sqlInfo sql语句嘻嘻你
     * @param time    执行的时间
     * @param row     SQL语句
     */
    public void print(SqlInfo sqlInfo, long time, int row) {
        try {
            for (DbPrintSqlExcept except : dbPrintSqlExceptList) {
                if (except.isPrintExcept(sqlInfo, time, row)) {
                    return;
                }
            }
            dbPrintSqlList.forEach(k ->
                    k.printSql(sqlInfo, time, row)
            );
        } catch (Exception ex) {
            Log.error(DbExecutePrintSql.class, ex);
        }
    }
}