package com.fs.live.mapper;


import com.fs.live.domain.Live;
import com.fs.live.param.LiveDataParam;
import com.fs.live.vo.LiveListVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Collection;
import java.util.List;

/**
 * 直播Mapper接口
 *
 * @author fs
 * @date 2025-01-17
 */
public interface LiveMapper
{
    /**
     * 查询直播
     *
     * @param liveId 直播主键
     * @return 直播
     */
    public Live selectLiveByLiveId(Long liveId);

    /**
     * 查询企业直播
     * @param liveId        直播间ID
     * @param companyId     企业ID
     * @param companyUserId 企业用户ID
     * @return Live
     */
    Live selectLiveByLiveIdAndCompanyIdAndCompanyUserId(@Param("liveId") Long liveId, @Param("companyId") Long companyId, @Param("companyUserId") Long companyUserId);


    List<Live> selectLiveToStudent( @Param("liveName") String liveName,@Param("status") String status,@Param("startTime") String startTime,@Param("finishTime") String finishTime);

    /**
     * 查询直播列表
     *
     * @param live 直播
     * @return 直播集合
     */
    public List<Live> selectLiveList(Live live);

    /**
     * 新增直播
     *
     * @param live 直播
     * @return 结果
     */
    public int insertLive(Live live);

    /**
     * 修改直播
     *
     * @param live 直播
     * @return 结果
     */
    public int updateLive(Live live);

    /**
     * 删除直播
     *
     * @param liveId 直播主键
     * @return 结果
     */
    public int deleteLiveByLiveId(Long liveId);

    /**
     * 批量删除直播
     *
     * @param liveIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteLiveByLiveIds(Long[] liveIds);

    List<Live> liveList();

    @Select("select * from live where talent_id=#{talentId}")
    Live selectLiveByTalentId(@Param("talentId") String talentId);

    /**
     * 批量更新直播上下架状态
     * @return 更新记录数
     */
    int updateBatchLiveList(@Param("liveVo") LiveListVo liveVo);

    /**
     * 批量更新直播
     * @return 更新记录数
     */
    int updateLiveList(@Param("liveList") List<Live> liveVo);
    /**
     * 批量删除直播
     * @return 更新记录数
     */
    int deleteBatchLiveList(@Param("liveVo") LiveListVo listVo);

    /**
     * 找一个最近的直播间
     * @return 直播间
     */
    Live selectLiveOne(String s);


    int handleShelfOrUnAdmin(@Param("liveVo") LiveListVo listVo);

    int handleDeleteSelectedAdmin(@Param("liveVo") LiveListVo listVo);

    /**
     * 查询没有结束的直播
     * @return 直播列表
     */
    @Select("select * from live where status != 3 and live_type in (2,3) and is_audit = 1")
    List<Live> selectNoEndLiveList();

    void updateStatusAndTimeBatchById(@Param("liveList") List<Live> list);

    @Select("select * from live where (company_id = #{companyId} or company_id is null)  and is_audit = 1 and is_del = 0 and is_show = 1 and status != 3\n")
    List<Live> liveCompanyList(@Param("companyId") Long companyId);

    @Select("select * from live where live_id = #{liveId} and (company_id = #{companyId} or company_id is null)")
    Live selectLiveByLiveIdAndCompanyId(@Param("liveId") Long liveId,@Param("companyId") Long companyId);

    @Update("update live set is_audit = #{isAudit} where live_id = #{liveId}")
    Integer updateLiveIsAudit(Live live);

    List<Live> selectReadyStartLiveList();

    List<Live> liveShowList(@Param("companyIds") List<Long> companyIds);

    List<Live> selectLiveShowReadyStartLiveList(@Param("companyIds") List<Long> companyIds);

    @Select("select * from live where is_audit = 1 and is_del = 0 and status in (1,2,4) and live_type in (2,3) order by create_time desc")
    List<Live> liveListAll();

    @Select({"<script>" +
            "select * from live where 1=1 " +
            " <if test='param.companyId!=null' > and (company_id is null or company_id = #{param.companyId}) </if> and live_type IN (1,2, 3) AND status IN (3, 4) AND is_del = 0 and is_audit=1 " +
            " <if test='param.liveName!=null' > and live_name like concat('%' ,#{param.liveName},'%') </if>" +
            "  order by create_time desc" +
            " </script>"})
    List<Live> listLiveData(@Param("param") LiveDataParam param);

    @Select({"<script>" +
            "select count(1) from live where 1=1 " +
            " <if test='param.companyId!=null' > and company_id = #{param.companyId} </if> and live_type IN (1,2, 3) AND status IN (3, 4) AND is_del = 0 and is_audit=1" +
            " <if test='param.liveName!=null' > and live_name like concat('%' ,#{param.liveName},'%') </if>" +
            "  order by create_time desc" +
            " </script>"})
    int listLiveDataCount(@Param("param") LiveDataParam param);

    /**
     * 查询直播间是直播还是回放状态
     * 判断标准：当前直播间开始时间 + 视频类型为1的视频时长，如果大于当前时间，返回1，否则返回0
     * @param liveId 直播间ID
     * @return 1表示直播中，0表示回放中
     */
    @Select("SELECT CASE " +
            "WHEN l.start_time IS NOT NULL AND " +
            "     (l.start_time + INTERVAL COALESCE(SUM(CASE WHEN lv.video_type = 1 THEN lv.duration ELSE 0 END), 0) SECOND) > NOW() " +
            "THEN 1 " +
            "ELSE 0 " +
            "END AS liveFlag " +
            "FROM live l " +
            "LEFT JOIN live_video lv ON l.live_id = lv.live_id AND lv.video_type = 1 " +
            "WHERE l.live_id = #{liveId} " +
            "GROUP BY l.live_id, l.start_time")
    Integer selectLiveFlagByLiveId(@Param("liveId") Long liveId);

    @Update("update live set global_visible = #{status} where live_id = #{liveId}")
    void updateGlobalVisible(@Param("liveId")Long liveId,@Param("status") Integer status);
}
