package com.genersoft.iot.vmp.streamPush.dao;
import com.genersoft.iot.vmp.streamPush.bean.StreamPush;
import com.genersoft.iot.vmp.service.bean.StreamPushItemFromRedis;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Mapper
@Repository
public interface StreamPushMapper {
    @Insert("INSERT INTO wvp_stream_push (app, stream, media_server_id, server_id, push_time,  update_time, create_time, pushing, start_offline_push) VALUES" +
            "(#{app}, #{stream}, #{mediaServerId} , #{serverId} , #{pushTime} ,#{updateTime}, #{createTime}, #{pushing}, #{startOfflinePush})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int add(StreamPush streamPushItem);
    @Update(value = {" "})
    int update(StreamPush streamPushItem);
    @Delete("DELETE FROM wvp_stream_push WHERE id=#{id}")
    int del(@Param("id") int id);
    @Select(value = {" "})
    List selectAll(@Param("query") String query, @Param("pushing") Boolean pushing, @Param("mediaServerId") String mediaServerId);
    @Select("SELECT st.*, st.id as stream_push_id, wdc.*, wdc.id as gb_id FROM wvp_stream_push st LEFT join wvp_device_channel wdc on st.id = wdc.stream_push_id WHERE st.app=#{app} AND st.stream=#{stream}")
    StreamPush selectByAppAndStream(@Param("app") String app, @Param("stream") String stream);
    @Insert("")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int addAll(List streamPushItems);
    @Select("SELECT st.*, st.id as stream_push_id, wdc.*, wdc.id as gb_id FROM wvp_stream_push st LEFT join wvp_device_channel wdc on st.id = wdc.stream_push_id WHERE st.media_server_id=#{mediaServerId}")
    List selectAllByMediaServerId(String mediaServerId);
    @Select("SELECT st.*, st.id as stream_push_id, wdc.*, wdc.id as gb_id FROM wvp_stream_push st LEFT join wvp_device_channel wdc on st.id = wdc.stream_push_id WHERE st.media_server_id=#{mediaServerId} and wdc.gb_device_id is null")
    List selectAllByMediaServerIdWithOutGbID(String mediaServerId);
    @Update("UPDATE wvp_stream_push " +
            "SET pushing=#{pushing} " +
            "WHERE id=#{id}")
    int updatePushStatus(@Param("id") int id, @Param("pushing") boolean pushing);
    @Select("")
    List getListFromRedis(List offlineStreams);
    @Select("SELECT CONCAT(app,stream) from wvp_stream_push")
    List getAllAppAndStream();
    @Select("select count(1) from wvp_stream_push ")
    int getAllCount();
    @Select(value = {" "})
    int getAllPushing(Boolean usePushingAsStatus);
    @MapKey("uniqueKey")
    @Select("SELECT CONCAT(wsp.app, wsp.stream) as unique_key, wsp.*, wsp.* , wdc.id as gb_id " +
            " from wvp_stream_push wsp " +
            " LEFT join wvp_device_channel wdc on wsp.id = wdc.stream_push_id")
    Map getAllAppAndStreamMap();
    @MapKey("gbDeviceId")
    @Select("SELECT wdc.gb_device_id, wsp.id as stream_push_id, wsp.*, wsp.* , wdc.id as gb_id " +
            " from wvp_stream_push wsp " +
            " LEFT join wvp_device_channel wdc on wsp.id = wdc.stream_push_id")
    Map getAllGBId();
    @Select("SELECT st.*, st.id as stream_push_id, wdc.*, wdc.id as gb_id FROM wvp_stream_push st LEFT join wvp_device_channel wdc on st.id = wdc.stream_push_id WHERE st.id=#{id}")
    StreamPush queryOne(@Param("id") int id);
    @Select("")
    List selectInSet(Set ids);
    @Delete("")
    void batchDel(List streamPushList);
    @Update({""})
    int batchUpdate(List streamPushItemForUpdate);
}