package com.fs.live.mapper;


import com.fs.live.domain.LiveWatchUser;
import com.fs.live.vo.LiveWatchUserVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 直播间观看用户Mapper接口
 *
 * @author fs
 * @date 2025-01-18
 */
public interface LiveWatchUserMapper {
    /**
     * 查询直播间观看用户
     *
     * @param id 直播间观看用户主键
     * @return 直播间观看用户
     */
    LiveWatchUser selectLiveWatchUserById(Long id);

    /**
     * 查询企业直播间观看用户
     * @param id                直播间用户ID
     * @param companyId         企业ID
     * @param companyUserId     企业用户ID
     * @return LiveWatchUser
     */
    LiveWatchUser selectLiveWatchUserByIdAndCompanyIdAndCompanyUserId(@Param("id") Long id, @Param("companyId") Long companyId, @Param("companyUserId") Long companyUserId);

    /**
     * 查询直播间观看用户列表
     *
     * @param liveWatchUser 直播间观看用户
     * @return 直播间观看用户集合
     */
    List<LiveWatchUser> selectLiveWatchUserList(LiveWatchUser liveWatchUser);

    /**
     * 新增直播间观看用户
     *
     * @param liveWatchUser 直播间观看用户
     * @return 结果
     */
    int insertLiveWatchUser(LiveWatchUser liveWatchUser);

    /**
     * 修改直播间观看用户
     *
     * @param liveWatchUser 直播间观看用户
     * @return 结果
     */
    int updateLiveWatchUser(LiveWatchUser liveWatchUser);

    /**
     * 删除直播间观看用户
     *
     * @param id 直播间观看用户主键
     * @return 结果
     */
    int deleteLiveWatchUserById(Long id);

    /**
     * 批量删除直播间观看用户
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    int deleteLiveWatchUserByIds(Long[] ids);

    /**
     * 根据直播间ID查询直播间在线观看用户列表
     * @param params 参数
     * @return list
     */
    List<LiveWatchUserVO> selectWatchUserListByLiveId(@Param("params") Map<String, Object> params);

    /**
     * 根据直播间ID和用户ID查询观看用户信息
     * @param liveId    直播间ID
     * @param userId    观看用户ID
     * @return LiveWatchUserVO
     */
    LiveWatchUserVO selectWatchUserByLiveIdAndUserId(@Param("liveId") Long liveId, @Param("userId") Long userId);



    List<LiveWatchUserVO> selectOnlineUserList(LiveWatchUser param);

    List<LiveWatchUser> checkOnlineNoRewardUser(@Param("liveId") Long liveId,@Param("now") Date now);

    @Select("select a.*,fu.nickname as nick_name from (select lws.* from live_watch_user lws where live_id=#{liveId} and online = 0 and " +
            "user_id in (select user_id from live_lottery_registration where live_id = #{liveId} and lottery_id=#{lotteryId} and registration_id >= " +
            "(SELECT FLOOR(RAND() * (SELECT MAX(registration_id) FROM live_lottery_registration)))) limit #{totalLots} ) a left join fs_user fu on fu.user_id = a.user_id")
    List<LiveWatchUser> selectLiveWatchAndRegisterUser(@Param("liveId") Long liveId,@Param("lotteryId") Long lotteryId,@Param("totalLots") Integer totalLots);

    @Select("select * from live_watch_user where live_id = #{liveId} and user_id = #{userId}")
    LiveWatchUser selectUserByLiveIdAndUserId(@Param("liveId") long liveId,@Param("userId")  long userId);
}
