package com.fs.live.mapper;


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

import java.math.BigDecimal;
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 params    参数Map，包含liveId、userId、liveFlag、replayFlag
     * @return LiveWatchUserVO
     */
    LiveWatchUserVO selectWatchUserByLiveIdAndUserId(@Param("params") Map<String, Object> params);



    List<LiveWatchUserVO> selectOnlineUserList(LiveWatchUser param);

    List<LiveWatchUser> checkOnlineNoRewardUser(@Param("params") Map<String, Object> params);

    List<LiveWatchUser> selectLiveWatchAndRegisterUser(@Param("params") Map<String, Object> params);

    List<LiveWatchUser> selectUserByLiveIdAndUserId(@Param("params") Map<String, Object> params);

    /**
     * 根据唯一索引查询：live_id, user_id, live_flag, replay_flag
     */
    LiveWatchUser selectByUniqueIndex(@Param("liveId") Long liveId, @Param("userId") Long userId,
                                      @Param("liveFlag") Integer liveFlag, @Param("replayFlag") Integer replayFlag);

    /**
     * 根据唯一索引更新或插入（ON DUPLICATE KEY UPDATE）
     */
    int insertOrUpdateByUniqueIndex(LiveWatchUser liveWatchUser);

    @Select("SELECT " +
            "    SUM(CASE WHEN lwu.online = 0 and lwu.msg_status = 0 THEN 1 ELSE 0 END) AS online, " +
            "    SUM(CASE WHEN lwu.online = 1 and lwu.msg_status = 0 THEN 1 ELSE 0 END) AS offline, " +
            "    SUM(CASE WHEN lwu.msg_status = 1 THEN 1 ELSE 0 END) AS silenced, " +
            "    SUM(CASE WHEN lwu.user_id > 0 THEN 1 ELSE 0 END) AS al " +
            "FROM " +
            "    live_watch_user lwu left join fs_user fu on lwu.user_id=fu.user_id  where fu.status=1 and  live_id=#{liveId}")
    LiveWatchUserStatistics liveUserTotals(LiveWatchUser liveWatchUser);

    List<LiveWatchUserVO> selectWatchUserListAllByLiveId(@Param("params") Map<String, Object> param);

    @Select("SELECT " +
            "    SUM(CASE WHEN online = 0 and msg_status = 0 THEN 1 ELSE 0 END) AS onlineNum, " +
            "    SUM(CASE WHEN user_id > 0 AND live_flag = 1 THEN 1 ELSE 0 END) AS liveNewUserNum, " +
            "    SUM(CASE WHEN user_id > 0 AND live_flag = 1 THEN 1 ELSE 0 END) AS liveOldUserNum, " +
            "    SUM(CASE WHEN user_id > 0 AND replay_flag = 1 THEN 1 ELSE 0 END) AS replayNewUserNum, " +
            "    SUM(CASE WHEN user_id > 0 AND replay_flag = 1 THEN 1 ELSE 0 END) AS replayOldUserNum " +
            "FROM " +
            "    live_watch_user where live_id=#{liveId}"
    )
    Map<String, BigDecimal> selectDashboardCount(Long liveId);

    @Update("update live_watch_user set global_visible = #{status} where live_id = #{liveId}")
    void updateGlobalVisible(@Param("liveId") long liveId,@Param("status") int status);

    @Update("update live_watch_user set single_visible = #{status} where live_id = #{liveId} and user_id=#{userId}")
    void updateSingleVisible(@Param("liveId") long liveId,@Param("status") int status,@Param("userId") long userId);
}
