Newer
Older
bremer / src / main / kotlin / dao / AudioPlayHistoryDao.kt
/*
 * Copyright (c) 2023. yo-saito. All Rights Reserved.
 */

package net.piedpiper.bremer.dao

import net.piedpiper.bremer.entity.AudioPlayHistoryEntity
import net.piedpiper.bremer.utils.WHERE_IN
import org.apache.ibatis.annotations.*
import org.apache.ibatis.jdbc.SQL
import org.springframework.stereotype.Repository

@Repository("bremer.dao.AudioPlayHistoryDao")
@Mapper
interface AudioPlayHistoryDao {

    @ResultMap("net.piedpiper.bremer.AudioPlayHistoryEntity")
    @Select(
        "SELECT * FROM audio_play_history ORDER BY last_played_at DESC " +
                "LIMIT #{limit} OFFSET #{offset}"
    )
    fun findAllOrderLastPlayedAtDescLimit(
        @Param("limit") limit: Int,
        @Param("offset") offset: Long
    ): List<AudioPlayHistoryEntity>

    @Insert(
        """
    INSERT INTO audio_play_history (audio_id,last_played_at) VALUES (#{entity.audioId},
    #{entity.lastPlayedAt, typeHandler=net.piedpiper.bremer.dao.handler.LocalDateTimeTypeHandler})
    ON DUPLICATE KEY UPDATE last_played_at = VALUES (last_played_at);
    """
    )
    @Options(useGeneratedKeys = true, keyProperty = "id")
    fun insertOrUpdateOne(@Param("entity") entity: AudioPlayHistoryEntity): Boolean

    @DeleteProvider(type = Sql::class, method = "deleteAllByIds")
    fun deleteAllByIds(@Param("ids") ids: List<Long>): Long

    class Sql {
        companion object {
            @JvmStatic
            fun deleteAllByIds(@Param("ids") ids: List<Long>): String = SQL()
                .DELETE_FROM("audio_play_history")
                .WHERE_IN("id", "ids", ids.size)
                .toString()
        }
    }
}