package com.test.musichouduan.repository;

import com.test.musichouduan.entity.UserLikeMusic;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Repository
public interface UserLikeMusicRepository extends JpaRepository<UserLikeMusic, Long> {

    /**
     * 根据用户ID和音乐ID查找点赞记录
     *
     * @param userId  用户ID
     * @param musicId 音乐ID
     * @return 点赞记录
     */
    Optional<UserLikeMusic> findByUserIdAndMusicId(Long userId, Long musicId);

    /**
     * 检查用户是否点赞了某音乐
     *
     * @param userId  用户ID
     * @param musicId 音乐ID
     * @return 是否存在
     */
    boolean existsByUserIdAndMusicId(Long userId, Long musicId);

    /**
     * 根据音乐ID删除点赞记录
     *
     * @param musicId 音乐ID
     */
    @Modifying
    @Transactional
    @Query("DELETE FROM UserLikeMusic ulm WHERE ulm.musicId = :musicId")
    void deleteByMusicId(@Param("musicId") Long musicId);
}
