package com.genersoft.iot.vmp.storager.dao;
import com.genersoft.iot.vmp.gb28181.bean.Device;
import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
import com.genersoft.iot.vmp.gb28181.bean.DeviceChannelInPlatform;
import com.genersoft.iot.vmp.vmanager.gb28181.platform.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 (channel_id, device_id, name, manufacture, model, owner, civil_code, block, " +
            "address, parental, parent_id, safety_way, register_way, cert_num, certifiable, err_code, secrecy, " +
            "ip_address, port, password, ptz_type, status, stream_id, longitude, latitude, longitude_gcj02, latitude_gcj02, " +
            "longitude_wgs84, latitude_wgs84, has_audio, create_time, update_time, business_group_id, gps_time) " +
            "VALUES (#{channelId}, #{deviceId}, #{name}, #{manufacture}, #{model}, #{owner}, #{civilCode}, #{block}," +
            "#{address}, #{parental}, #{parentId}, #{safetyWay}, #{registerWay}, #{certNum}, #{certifiable}, #{errCode}, #{secrecy}, " +
            "#{ipAddress}, #{port}, #{password}, #{PTZType}, #{status}, #{streamId}, #{longitude}, #{latitude}, #{longitudeGcj02}, " +
            "#{latitudeGcj02}, #{longitudeWgs84}, #{latitudeWgs84}, #{hasAudio}, #{createTime}, #{updateTime}, #{businessGroupId}, #{gpsTime})")
    int add(DeviceChannel channel);
    @Update(value = {" "})
    int update(DeviceChannel channel);
    @Select(value = {" "})
    List queryChannels(String deviceId, String parentChannelId, String query, Boolean hasSubChannel, Boolean online, List channelIds);
    @Select(value = {" "})
    List queryChannelsWithDeviceInfo(String deviceId, String parentChannelId, String query, Boolean hasSubChannel, Boolean online, List channelIds);
    @Select(value = {" "})
    List queryChannelsByDeviceIdWithStartAndLimit(String deviceId,List channelIds, String parentChannelId, String query,
                                                                       Boolean hasSubChannel, Boolean online, int start, int limit);
    @Select("SELECT * FROM wvp_device_channel WHERE device_id=#{deviceId} AND channel_id=#{channelId}")
    DeviceChannel queryChannel(String deviceId, String channelId);
    @Delete("DELETE FROM wvp_device_channel WHERE device_id=#{deviceId}")
    int cleanChannelsByDeviceId(String deviceId);
    @Delete("DELETE FROM wvp_device_channel WHERE device_id=#{deviceId} AND channel_id=#{channelId}")
    int del(String deviceId, String channelId);
    @Update(value = {"UPDATE wvp_device_channel SET stream_id=null WHERE device_id=#{deviceId} AND channel_id=#{channelId}"})
    void stopPlay(String deviceId, String channelId);
    @Update(value = {"UPDATE wvp_device_channel SET stream_id=#{streamId} WHERE device_id=#{deviceId} AND channel_id=#{channelId}"})
    void startPlay(String deviceId, String channelId, String streamId);
    @Select(value = {" "})
    List queryChannelListInAll(String query, Boolean online, Boolean hasSubChannel, String platformId, String catalogId);
    @Select(value = {" "})
    List queryChannelByPlatformId(String platformId);
    @Select("SELECT * FROM wvp_device_channel WHERE channel_id=#{channelId}")
    List queryChannelByChannelId( String channelId);
    @Update(value = {"UPDATE wvp_device_channel SET status=false WHERE device_id=#{deviceId} AND channel_id=#{channelId}"})
    void offline(String deviceId,  String channelId);
    @Update(value = {"UPDATE wvp_device_channel SET status=fasle WHERE device_id=#{deviceId}"})
    void offlineByDeviceId(String deviceId);
    @Insert("")
    int batchAdd(List addChannels);
    @Insert("")
    int batchAddOrUpdate(List addChannels);
    @Update(value = {"UPDATE wvp_device_channel SET status=true WHERE device_id=#{deviceId} AND channel_id=#{channelId}"})
    void online(String deviceId,  String channelId);
    @Update({""})
    int batchUpdate(List updateChannels);
    @Select("SELECT * FROM wvp_device_channel WHERE device_id=#{deviceId} AND status=true")
    List queryOnlineChannelsByDeviceId(String deviceId);
    @Delete(value = {" "})
    int cleanChannelsNotInList(String deviceId, List channels);
    @Update(" update wvp_device_channel" +
            " set sub_count = (select *" +
            "                from (select count(0)" +
            "                      from wvp_device_channel" +
            "                      where device_id = #{deviceId} and parent_id = #{channelId}) as temp)" +
            " where device_id = #{deviceId} " +
            " and channel_id = #{channelId}")
    int updateChannelSubCount(String deviceId, String channelId);
    @Update(value = {" "})
    void updatePosition(DeviceChannel deviceChannel);
    @Select("SELECT * FROM wvp_device_channel WHERE length(trim(stream_id)) > 0")
    List getAllChannelInPlay();
    @Select("select * from wvp_device_channel where longitude*latitude > 0 and device_id = #{deviceId}")
    List getAllChannelWithCoordinate(String deviceId);
    @Select(value = {" "})
    List getChannelsWithCivilCodeAndLength(String deviceId, String parentId, Integer length);
    @Select(value = {" "})
    List getChannelsByCivilCode(String deviceId, String parentId);
    @Select("select min(length(channel_id)) as minLength " +
            "from wvp_device_channel " +
            "where device_id=#{deviceId}")
    Integer getChannelMinLength(String deviceId);
    @Select("select * from wvp_device_channel where device_id=#{deviceId} and civil_code not in " +
            "(select civil_code from wvp_device_channel where device_id=#{deviceId} group by civil_code)")
    List getChannelWithoutCivilCode(String deviceId);
    @Select("select * from wvp_device_channel where device_id=#{deviceId} and SUBSTRING(channel_id, 11, 3)=#{typeCode}")
    List getBusinessGroups(String deviceId, String typeCode);
    @Select("select dc.id, dc.channel_id, dc.device_id, dc.name, dc.manufacture,dc.model,dc.owner, pc.civil_code,dc.block, " +
            " dc.address, '0' as parental,'0' as channel_type, pc.id as parent_id, dc.safety_way, dc.register_way,dc.cert_num, dc.certifiable,  " +
            " dc.err_code,dc.end_time, dc.secrecy,   dc.ip_address,  dc.port,  dc.ptz_type,  dc.password, dc.status, " +
            " dc.longitude_wgs84 as longitude, dc.latitude_wgs84 as latitude,  pc.business_group_id " +
            " from wvp_device_channel dc" +
            " LEFT JOIN wvp_platform_gb_channel pgc on  dc.id = pgc.device_channel_id" +
            " LEFT JOIN wvp_platform_catalog pc on pgc.catalog_id = pc.id and pgc.platform_id = pc.platform_id" +
            " where pgc.platform_id=#{serverGBId}")
    List queryChannelWithCatalog(String serverGBId);
    @Select("select * from wvp_device_channel where device_id = #{deviceId}")
    List queryAllChannels(String deviceId);
    @Select("select channelId" +
            ", device_id" +
            ", latitude" +
            ", longitude"+
            ",latitude_wgs84"+
            ",longitude_wgs84"+
            ",latitude_gcj02"+
            ",longitude_gcj02"+
            "from wvp_device_channel where device_id = #{deviceId} " +
            "and latitude != 0 " +
            "and  longitude != 0 " +
            "and(latitude_gcj02=0 or latitude_wgs84=0 or longitude_wgs84= 0 or longitude_gcj02 = 0)")
    List getChannelsWithoutTransform(String deviceId);
    @Select("select de.* from wvp_device de left join wvp_device_channel dc on de.device_id = dc.deviceId where dc.channel_id=#{channelId}")
    List getDeviceByChannelId(String channelId);
    @Delete({""})
    int batchDel(List deleteChannelList);
    @Update({""})
    int batchOnline(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();
    // 设备主子码流逻辑START
    @Update(value = {"UPDATE wvp_device_channel SET stream_id=null WHERE device_id=#{deviceId}"})
    void clearPlay(String deviceId);
    // 设备主子码流逻辑END
    @Select(value = {" "})
    List getSubChannelsByDeviceId(String deviceId, String parentId, boolean onlyCatalog);
}