package com.common.mapper;


import com.common.entity.AppLogEntity;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface AppLogMapper {

    /**
     * 插入日志
     */
    @Insert("INSERT INTO applog (es_index, es_type, es_id, es_score, dt_time, collect_time, log_type, " +
            "trace_id, method, app_name, ip, class_name, env, content, thread_name, log_level, seq, " +
            "indexed_at, log_date, created_at, updated_at) " +
            "VALUES (#{esIndex}, #{esType}, #{esId}, #{esScore}, #{dtTime}, #{collectTime}, #{logType}, " +
            "#{traceId}, #{method}, #{appName}, #{ip}, #{className}, #{env}, #{content}, #{threadName}, " +
            "#{logLevel}, #{seq}, #{indexedAt}, #{logDate}, #{createdAt}, #{updatedAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(AppLogEntity appLog);

    /**
     * 批量插入
     */
    int batchInsert(@Param("list") List<AppLogEntity> appLogs);

    /**
     * 根据ES ID检查是否存在
     */
    @Select("SELECT COUNT(1) FROM applog WHERE es_id = #{esId}")
    boolean existsByEsId(String esId);

    /**
     * 根据ES ID和索引检查是否存在
     */
    @Select("SELECT COUNT(1) FROM applog WHERE es_id = #{esId} AND es_index = #{esIndex}")
    boolean existsByEsIdAndIndex(@Param("esId") String esId, @Param("esIndex") String esIndex);
}