package com.fs.live.mapper;


import com.fs.common.annotation.DataSource;
import com.fs.common.enums.DataSourceType;
import com.fs.live.domain.LiveUserFirstEntry;
import com.fs.live.vo.LiveDashBoardDataVo;
import com.fs.live.vo.LiveUserFirstProfit;
import com.fs.live.vo.LiveUserFirstVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.security.core.parameters.P;

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

/**
 * 用户每日首次进入直播间记录Mapper接口
 *
 * @author fs
 * @date 2025-09-04
 */
public interface LiveUserFirstEntryMapper {
    /**
     * 查询用户每日首次进入直播间记录
     *
     * @param id 用户每日首次进入直播间记录主键
     * @return 用户每日首次进入直播间记录
     */
    LiveUserFirstEntry selectLiveUserFirstEntryById(Long id);

    /**
     * 查询用户每日首次进入直播间记录列表
     *
     * @param liveUserFirstEntry 用户每日首次进入直播间记录
     * @return 用户每日首次进入直播间记录集合
     */
    List<LiveUserFirstEntry> selectLiveUserFirstEntryList(LiveUserFirstEntry liveUserFirstEntry);

    /**
     * 新增用户每日首次进入直播间记录
     *
     * @param liveUserFirstEntry 用户每日首次进入直播间记录
     * @return 结果
     */
    int insertLiveUserFirstEntry(LiveUserFirstEntry liveUserFirstEntry);

    /**
     * 修改用户每日首次进入直播间记录
     *
     * @param liveUserFirstEntry 用户每日首次进入直播间记录
     * @return 结果
     */
    int updateLiveUserFirstEntry(LiveUserFirstEntry liveUserFirstEntry);

    /**
     * 删除用户每日首次进入直播间记录
     *
     * @param id 用户每日首次进入直播间记录主键
     * @return 结果
     */
    int deleteLiveUserFirstEntryById(Long id);

    /**
     * 批量删除用户每日首次进入直播间记录
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    int deleteLiveUserFirstEntryByIds(Long[] ids);

    @Select("select count(*) from live_user_first_entry where user_id=#{userId} and DATE(entry_date)=DATE(#{now})")
    int selectTodayEntry(@Param("userId") long userId,@Param("now") Date now);
    @DataSource(DataSourceType.SLAVE)
    List<LiveUserFirstProfit> selectLiveProfitList();

    @Select("select * from live_user_first_entry where live_id=#{liveId} and user_id=#{userId}")
    @DataSource(DataSourceType.SLAVE)
    LiveUserFirstEntry selectEntityByLiveIdUserId(@Param("liveId") long liveId,@Param("userId") long userId);

    @Select("SELECT  " +
            "  cu.nick_name as userName,  " +
            "  sum( case when company_user_id > 0 then 1 else 0 end ) AS invite_num   " +
            "FROM  " +
            "  live_user_first_entry lufe  " +
            "  LEFT JOIN company_user cu ON lufe.company_user_id = cu.user_id   " +
            "WHERE  " +
            "  lufe.company_user_id > 0   " +
            "GROUP BY  " +
            "  lufe.company_user_id   " +
            "ORDER BY  " +
            "  invite_num DESC   " +
            "  LIMIT 10")
    @DataSource(DataSourceType.SLAVE)
    List<LiveUserFirstVo> selectDashboardInviteCount(@Param("liveId") Long liveId);

    @Select("SELECT  " +
            "  cu.user_name companyUserName,  " +
            "  count( 1 ) AS invite_num   " +
            "FROM  " +
            "  live_user_first_entry lufe  " +
            "  LEFT JOIN company_user cu ON lufe.company_user_id = cu.user_id   " +
            "WHERE  " +
            "  lufe.company_user_id > 0   " +
            "GROUP BY  " +
            "  lufe.company_user_id   " +
            "ORDER BY  " +
            "  invite_num DESC   ")
    @DataSource(DataSourceType.SLAVE)
    List<LiveUserFirstVo> inviteList(@Param("liveId") Long liveId);

    @Select("SELECT  sum(case when company_user_id > 0 then 1 else 0 end ) as shareUrlNum, " +
            " sum(case when company_user_id < 0 then 1 else 0 end ) as directAccessNum  " +
            " from live_user_first_entry where live_id=#{liveId}" )
    Map<String, BigDecimal> selectDashboardCount(@Param("liveId") Long liveId);
}
