package com.genersoft.iot.vmp.gb28181.dao;
import com.genersoft.iot.vmp.gb28181.bean.Device;
import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
import com.genersoft.iot.vmp.gb28181.controller.bean.ChannelReduce;
import com.genersoft.iot.vmp.web.gb28181.dto.DeviceChannelExtend;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
 * 用于存储设备通道信息
 */
@Mapper
@Repository
public interface DeviceChannelMapper {
    @Insert("INSERT INTO wvp_device_channel (device_id, device_db_id, name, manufacturer, model, owner, civil_code, gb_civil_code, block, " +
            "address, parental, parent_id, safety_way, register_way, cert_num, certifiable, err_code, end_time, secrecy, " +
            "ip_address, port, password, status, longitude, latitude, ptz_type, position_type, room_type, use_type, " +
            "supply_light_type, direction_type, resolution, business_group_id, download_speed, svc_space_support_mod, " +
            "svc_time_support_mode, create_time, update_time, sub_count,stream_id, has_audio, gps_time, stream_identification) " +
            "VALUES (#{deviceId}, #{deviceDbId}, #{name}, #{manufacturer}, #{model}, #{owner}, #{civilCode}, #{civilCode}, #{block}," +
            "#{address}, #{parental}, #{parentId}, #{safetyWay}, #{registerWay}, #{certNum}, #{certifiable}, #{errCode}, #{endTime}, #{secrecy}, " +
            "#{ipAddress}, #{port}, #{password}, #{status}, #{longitude}, #{latitude}, #{ptzType}, #{positionType}, #{roomType}, #{useType}, " +
            "#{supplyLightType}, #{directionType}, #{resolution}, #{businessGroupId}, #{downloadSpeed}, #{svcSpaceSupportMod}," +
            " #{svcTimeSupportMode}, #{createTime}, #{updateTime}, #{subCount}, #{streamId}, #{hasAudio}, #{gpsTime}, #{streamIdentification})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int add(DeviceChannel channel);
    @Update(value = {" "})
    int update(DeviceChannel channel);
    @Update(value = {" "})
    int updateCustomInfo(DeviceChannel channel);
    @Select(value = {" "})
    List queryChannels(@Param("deviceDbId") int deviceDbId, @Param("parentChannelId") String parentChannelId, @Param("query") String query, @Param("hasSubChannel") Boolean hasSubChannel, @Param("online") Boolean online, @Param("channelIds") List channelIds);
    @Select("select\n" +
            " id,\n" +
            " device_db_id,\n" +
            " create_time,\n" +
            " update_time,\n" +
            " sub_count,\n" +
            " stream_id,\n" +
            " has_audio,\n" +
            " gps_time,\n" +
            " stream_identification,\n" +
            " coalesce(gb_device_id, device_id) as device_id,\n" +
            " coalesce(gb_name, name) as name,\n" +
            " coalesce(gb_manufacturer, manufacturer) as manufacturer,\n" +
            " coalesce(gb_model, model) as model,\n" +
            " coalesce(gb_owner, owner) as owner,\n" +
            " gb_civil_code as civil_code,\n" +
            " coalesce(gb_block, block) as block,\n" +
            " coalesce(gb_address, address) as address,\n" +
            " coalesce(gb_parental, parental) as parental,\n" +
            " coalesce(gb_parent_id, parent_id) as parent_id,\n" +
            " coalesce(gb_safety_way, safety_way) as safety_way,\n" +
            " coalesce(gb_register_way, register_way) as register_way,\n" +
            " coalesce(gb_cert_num, cert_num) as cert_num,\n" +
            " coalesce(gb_certifiable, certifiable) as certifiable,\n" +
            " coalesce(gb_err_code, err_code) as err_code,\n" +
            " coalesce(gb_end_time, end_time) as end_time,\n" +
            " coalesce(gb_secrecy, secrecy) as secrecy,\n" +
            " coalesce(gb_ip_address, ip_address) as ip_address,\n" +
            " coalesce(gb_port, port) as port,\n" +
            " coalesce(gb_password, password) as password,\n" +
            " coalesce(gb_status, status) as status,\n" +
            " coalesce(gb_longitude, longitude) as longitude,\n" +
            " coalesce(gb_latitude, latitude) as latitude,\n" +
            " coalesce(gb_ptz_type, ptz_type) as ptz_type,\n" +
            " coalesce(gb_position_type, position_type) as position_type,\n" +
            " coalesce(gb_room_type, room_type) as room_type,\n" +
            " coalesce(gb_use_type, use_type) as use_type,\n" +
            " coalesce(gb_supply_light_type, supply_light_type) as supply_light_type,\n" +
            " coalesce(gb_direction_type, direction_type) as direction_type,\n" +
            " coalesce(gb_resolution, resolution) as resolution,\n" +
            " coalesce(gb_business_group_id, business_group_id) as business_group_id,\n" +
            " coalesce(gb_download_speed, download_speed) as download_speed,\n" +
            " coalesce(gb_svc_space_support_mod, svc_space_support_mod) as svc_space_support_mod,\n" +
            " coalesce(gb_svc_time_support_mode,svc_time_support_mode) as svc_time_support_mode\n" +
            " from wvp_device_channel\n" +
            " where device_db_id = #{deviceDbId}")
    List queryChannelsByDeviceDbId(@Param("deviceDbId") int deviceDbId);
    @Select(value = {" "})
    List queryChannelsWithDeviceInfo(@Param("deviceId") String deviceId, @Param("parentChannelId") String parentChannelId, @Param("query") String query, @Param("hasSubChannel") Boolean hasSubChannel, @Param("online") Boolean online, @Param("channelIds") List channelIds);
    @Select("SELECT " +
            " dc.id,\n" +
            " dc.device_db_id,\n" +
            " dc.create_time,\n" +
            " dc.update_time,\n" +
            " dc.sub_count,\n" +
            " dc.stream_id,\n" +
            " dc.has_audio,\n" +
            " dc.gps_time,\n" +
            " dc.stream_identification,\n" +
            " coalesce(dc.gb_device_id, dc.device_id) as device_id,\n" +
            " coalesce(dc.gb_name, dc.name) as name,\n" +
            " coalesce(dc.gb_manufacturer, dc.manufacturer) as manufacturer,\n" +
            " coalesce(dc.gb_model, dc.model) as model,\n" +
            " coalesce(dc.gb_owner, dc.owner) as owner,\n" +
            " dc.gb_civil_code as civil_code,\n" +
            " coalesce(dc.gb_block, dc.block) as block,\n" +
            " coalesce(dc.gb_address, dc.address) as address,\n" +
            " coalesce(dc.gb_parental, dc.parental) as parental,\n" +
            " coalesce(dc.gb_parent_id, dc.parent_id) as parent_id,\n" +
            " coalesce(dc.gb_safety_way, dc.safety_way) as safety_way,\n" +
            " coalesce(dc.gb_register_way, dc.register_way) as register_way,\n" +
            " coalesce(dc.gb_cert_num, dc.cert_num) as cert_num,\n" +
            " coalesce(dc.gb_certifiable, dc.certifiable) as certifiable,\n" +
            " coalesce(dc.gb_err_code, dc.err_code) as err_code,\n" +
            " coalesce(dc.gb_end_time, dc.end_time) as end_time,\n" +
            " coalesce(dc.gb_secrecy, dc.secrecy) as secrecy,\n" +
            " coalesce(dc.gb_ip_address, dc.ip_address) as ip_address,\n" +
            " coalesce(dc.gb_port, dc.port) as port,\n" +
            " coalesce(dc.gb_password, dc.password) as password,\n" +
            " coalesce(dc.gb_status, dc.status) as status,\n" +
            " coalesce(dc.gb_longitude, dc.longitude) as longitude,\n" +
            " coalesce(dc.gb_latitude, dc.latitude) as latitude,\n" +
            " coalesce(dc.gb_ptz_type, dc.ptz_type) as ptz_type,\n" +
            " coalesce(dc.gb_position_type, dc.position_type) as position_type,\n" +
            " coalesce(dc.gb_room_type, dc.room_type) as room_type,\n" +
            " coalesce(dc.gb_use_type, dc.use_type) as use_type,\n" +
            " coalesce(dc.gb_supply_light_type, dc.supply_light_type) as supply_light_type,\n" +
            " coalesce(dc.gb_direction_type, dc.direction_type) as direction_type,\n" +
            " coalesce(dc.gb_resolution, dc.resolution) as resolution,\n" +
            " coalesce(dc.gb_business_group_id, dc.business_group_id) as business_group_id,\n" +
            " coalesce(dc.gb_download_speed, dc.download_speed) as download_speed,\n" +
            " coalesce(dc.gb_svc_space_support_mod, dc.svc_space_support_mod) as svc_space_support_mod,\n" +
            " coalesce(dc.gb_svc_time_support_mode,dc.svc_time_support_mode) as svc_time_support_mode\n" +
            " FROM wvp_device_channel dc " +
            " left join wvp_device d on d.id=dc.device_db_id" +
            " WHERE d.device_id=#{deviceId} AND dc.device_id=#{channelId}")
    DeviceChannel queryChannel(@Param("deviceId") String deviceId, @Param("channelId") String channelId);
    @Delete("DELETE FROM wvp_device_channel WHERE device_db_id=#{deviceId}")
    int cleanChannelsByDeviceId(@Param("deviceId") int deviceId);
    @Delete("DELETE FROM wvp_device_channel WHERE id=#{id}")
    int del(@Param("id") int id);
    @Update(value = {"UPDATE wvp_device_channel SET stream_id=null WHERE device_db_id=#{deviceId} AND device_id=#{channelId}"})
    void stopPlay(@Param("deviceId") int deviceId, @Param("channelId") String channelId);
    @Update(value = {"UPDATE wvp_device_channel SET stream_id=#{streamId} WHERE device_db_id=#{deviceId} AND device_id=#{channelId}"})
    void startPlay(@Param("deviceId") int deviceId, @Param("channelId") String channelId, @Param("streamId") String streamId);
    @Select(value = {" "})
    List queryChannelListInAll(@Param("query") String query, @Param("online") Boolean online, @Param("hasSubChannel") Boolean hasSubChannel, @Param("platformId") String platformId, @Param("catalogId") String catalogId);
    @Select("SELECT " +
            " id,\n" +
            " device_db_id,\n" +
            " create_time,\n" +
            " update_time,\n" +
            " sub_count,\n" +
            " stream_id,\n" +
            " has_audio,\n" +
            " gps_time,\n" +
            " stream_identification,\n" +
            " coalesce(gb_device_id, device_id) as device_id,\n" +
            " coalesce(gb_name, name) as name,\n" +
            " coalesce(gb_manufacturer, manufacturer) as manufacturer,\n" +
            " coalesce(gb_model, model) as model,\n" +
            " coalesce(gb_owner, owner) as owner,\n" +
            " gb_civil_code as civil_code,\n" +
            " coalesce(gb_block, block) as block,\n" +
            " coalesce(gb_address, address) as address,\n" +
            " coalesce(gb_parental, parental) as parental,\n" +
            " coalesce(gb_parent_id, parent_id) as parent_id,\n" +
            " coalesce(gb_safety_way, safety_way) as safety_way,\n" +
            " coalesce(gb_register_way, register_way) as register_way,\n" +
            " coalesce(gb_cert_num, cert_num) as cert_num,\n" +
            " coalesce(gb_certifiable, certifiable) as certifiable,\n" +
            " coalesce(gb_err_code, err_code) as err_code,\n" +
            " coalesce(gb_end_time, end_time) as end_time,\n" +
            " coalesce(gb_secrecy, secrecy) as secrecy,\n" +
            " coalesce(gb_ip_address, ip_address) as ip_address,\n" +
            " coalesce(gb_port, port) as port,\n" +
            " coalesce(gb_password, password) as password,\n" +
            " coalesce(gb_status, status) as status,\n" +
            " coalesce(gb_longitude, longitude) as longitude,\n" +
            " coalesce(gb_latitude, latitude) as latitude,\n" +
            " coalesce(gb_ptz_type, ptz_type) as ptz_type,\n" +
            " coalesce(gb_position_type, position_type) as position_type,\n" +
            " coalesce(gb_room_type, room_type) as room_type,\n" +
            " coalesce(gb_use_type, use_type) as use_type,\n" +
            " coalesce(gb_supply_light_type, supply_light_type) as supply_light_type,\n" +
            " coalesce(gb_direction_type, direction_type) as direction_type,\n" +
            " coalesce(gb_resolution, resolution) as resolution,\n" +
            " coalesce(gb_business_group_id, business_group_id) as business_group_id,\n" +
            " coalesce(gb_download_speed, download_speed) as download_speed,\n" +
            " coalesce(gb_svc_space_support_mod, svc_space_support_mod) as svc_space_support_mod,\n" +
            " coalesce(gb_svc_time_support_mode,svc_time_support_mode) as svc_time_support_mode\n" +
            " FROM wvp_device_channel WHERE device_id=#{channelId}")
    List queryChannelByChannelId(@Param("channelId") String channelId);
    @Update(value = {"UPDATE wvp_device_channel SET status=0 WHERE id=#{id}"})
    void offline(@Param("id") int id);
    @Insert("")
    int batchAdd(@Param("addChannels") List addChannels);
    @Update(value = {"UPDATE wvp_device_channel SET status=0 WHERE id=#{id}"})
    void online(@Param("id") int id);
    @Update({""})
    int batchUpdate(List updateChannels);
    @Update(" update wvp_device_channel" +
            " set sub_count = (select *" +
            "             from (select count(0)" +
            "                   from wvp_device_channel" +
            "                   where device_db_id = #{deviceDbId} and parent_id = #{channelId}) as temp)" +
            " where device_db_id = #{deviceDbId} and device_id = #{channelId}")
    int updateChannelSubCount(@Param("deviceDbId") int deviceDbId, @Param("channelId") String channelId);
    @Update(value = {" "})
    int updatePosition(DeviceChannel deviceChannel);
    @Select("select " +
            " id,\n" +
            " device_db_id,\n" +
            " create_time,\n" +
            " update_time,\n" +
            " sub_count,\n" +
            " stream_id,\n" +
            " has_audio,\n" +
            " gps_time,\n" +
            " stream_identification,\n" +
            " coalesce(gb_device_id, device_id) as device_id,\n" +
            " coalesce(gb_name, name) as name,\n" +
            " coalesce(gb_manufacturer, manufacturer) as manufacturer,\n" +
            " coalesce(gb_model, model) as model,\n" +
            " coalesce(gb_owner, owner) as owner,\n" +
            " gb_civil_code as civil_code,\n" +
            " coalesce(gb_block, block) as block,\n" +
            " coalesce(gb_address, address) as address,\n" +
            " coalesce(gb_parental, parental) as parental,\n" +
            " coalesce(gb_parent_id, parent_id) as parent_id,\n" +
            " coalesce(gb_safety_way, safety_way) as safety_way,\n" +
            " coalesce(gb_register_way, register_way) as register_way,\n" +
            " coalesce(gb_cert_num, cert_num) as cert_num,\n" +
            " coalesce(gb_certifiable, certifiable) as certifiable,\n" +
            " coalesce(gb_err_code, err_code) as err_code,\n" +
            " coalesce(gb_end_time, end_time) as end_time,\n" +
            " coalesce(gb_secrecy, secrecy) as secrecy,\n" +
            " coalesce(gb_ip_address, ip_address) as ip_address,\n" +
            " coalesce(gb_port, port) as port,\n" +
            " coalesce(gb_password, password) as password,\n" +
            " coalesce(gb_status, status) as status,\n" +
            " coalesce(gb_longitude, longitude) as longitude,\n" +
            " coalesce(gb_latitude, latitude) as latitude,\n" +
            " coalesce(gb_ptz_type, ptz_type) as ptz_type,\n" +
            " coalesce(gb_position_type, position_type) as position_type,\n" +
            " coalesce(gb_room_type, room_type) as room_type,\n" +
            " coalesce(gb_use_type, use_type) as use_type,\n" +
            " coalesce(gb_supply_light_type, supply_light_type) as supply_light_type,\n" +
            " coalesce(gb_direction_type, direction_type) as direction_type,\n" +
            " coalesce(gb_resolution, resolution) as resolution,\n" +
            " coalesce(gb_business_group_id, business_group_id) as business_group_id,\n" +
            " coalesce(gb_download_speed, download_speed) as download_speed,\n" +
            " coalesce(gb_svc_space_support_mod, svc_space_support_mod) as svc_space_support_mod,\n" +
            " coalesce(gb_svc_time_support_mode, svc_time_support_mode) as svc_time_support_mode\n" +
            " from wvp_device_channel where device_db_id = #{deviceDbId}")
    List queryAllChannels(@Param("deviceDbId") int deviceDbId);
    @Select("select de.* from wvp_device de left join wvp_device_channel dc on de.device_id = dc.deviceId where dc.device_id=#{channelId}")
    List getDeviceByChannelId(String channelId);
    @Delete({""})
    int batchDel(@Param("deleteChannelList") List deleteChannelList);
    @Update({""})
    int batchOnline(@Param("channels") List channels);
    @Update({""})
    int batchOffline(List channels);
    @Select("select count(1) from wvp_device_channel where status = true")
    int getOnlineCount();
    @Select("select count(1) from wvp_device_channel")
    int getAllChannelCount();
    @Select(value = {" "})
    List getSubChannelsByDeviceId(@Param("deviceDbId") int deviceDbId, @Param("parentId") String parentId, @Param("onlyCatalog") boolean onlyCatalog);
    @Update("")
    void updateChannelStreamIdentification(DeviceChannel channel);
    @Update({""})
    void batchUpdatePosition(List channelList);
    @Select(value = {" "})
    DeviceChannel getOne(@Param("id") int id);
    @Select(value = {" "})
    DeviceChannel getOneByDeviceId(@Param("deviceDbId") int deviceDbId, @Param("channelId") String channelId);
}