package com.genersoft.iot.vmp.storager.dao;
import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
import com.genersoft.iot.vmp.gb28181.bean.DeviceChannelInPlatform;
import com.genersoft.iot.vmp.vmanager.bean.ResourceBaceInfo;
import com.genersoft.iot.vmp.vmanager.gb28181.platform.bean.ChannelReduce;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
 * 用于存储设备通道信息
 */
@Mapper
@Repository
public interface DeviceChannelMapper {
    @Insert("INSERT INTO device_channel (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) " +
            "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);
    @Select("SELECT * FROM device_channel WHERE deviceId=#{deviceId} AND channelId=#{channelId}")
    DeviceChannel queryChannel(String deviceId, String channelId);
    @Delete("DELETE FROM device_channel WHERE deviceId=#{deviceId}")
    int cleanChannelsByDeviceId(String deviceId);
    @Delete("DELETE FROM device_channel WHERE deviceId=#{deviceId} AND channelId=#{channelId}")
    int del(String deviceId, String channelId);
    @Update(value = {"UPDATE device_channel SET streamId=null WHERE deviceId=#{deviceId} AND channelId=#{channelId}"})
    void stopPlay(String deviceId, String channelId);
    @Update(value = {"UPDATE device_channel SET streamId=#{streamId} WHERE deviceId=#{deviceId} AND channelId=#{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 device_channel WHERE channelId=#{channelId}")
    List queryChannelByChannelId( String channelId);
    @Update(value = {"UPDATE device_channel SET status=0 WHERE deviceId=#{deviceId} AND channelId=#{channelId}"})
    void offline(String deviceId,  String channelId);
    @Update(value = {"UPDATE device_channel SET status=0 WHERE deviceId=#{deviceId}"})
    void offlineByDeviceId(String deviceId);
    @Insert("")
    int batchAdd(List addChannels);
    @Update(value = {"UPDATE device_channel SET status=1 WHERE deviceId=#{deviceId} AND channelId=#{channelId}"})
    void online(String deviceId,  String channelId);
    @Update({""})
    int batchUpdate(List updateChannels);
    @Select(value = {" "})
    List queryChannelsByDeviceIdWithStartAndLimit(String deviceId, String parentChannelId, String query,
                                                                 Boolean hasSubChannel, Boolean online, int start, int limit);
    @Select("SELECT * FROM device_channel WHERE deviceId=#{deviceId} AND status=1")
    List queryOnlineChannelsByDeviceId(String deviceId);
    @Delete(value = {" "})
    int cleanChannelsNotInList(String deviceId, List channels);
    @Update(" update device_channel" +
            " set subCount = (select *" +
            "                from (select count(0)" +
            "                      from device_channel" +
            "                      where deviceId = #{deviceId} and parentId = #{channelId}) as temp)" +
            " where deviceId = #{deviceId} " +
            " and channelId = #{channelId}")
    int updateChannelSubCount(String deviceId, String channelId);
    @Update(value = {" "})
    void updatePosition(DeviceChannel deviceChannel);
    @Select("SELECT * FROM device_channel WHERE length(trim(streamId)) > 0")
    List getAllChannelInPlay();
    @Select("select * from device_channel where longitude*latitude > 0 and deviceId = #{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(channelId)) as minLength " +
            "from device_channel " +
            "where deviceId=#{deviceId}")
    Integer getChannelMinLength(String deviceId);
    @Select("select * from device_channel where deviceId=#{deviceId} and civilCode not in " +
            "(select civilCode from device_channel where deviceId=#{deviceId} group by civilCode)")
    List getChannelWithoutCiviCode(String deviceId);
    @Select("select * from device_channel where deviceId=#{deviceId} and SUBSTRING(channelId, 11, 3)=#{typeCode}")
    List getBusinessGroups(String deviceId, String typeCode);
    @Select("select dc.id, dc.channelId, dc.deviceId, dc.name, dc.manufacture,dc.model,dc.owner, pc.civilCode,dc.block, " +
            " dc.address, '0' as parental,'0' as channelType, pc.id as parentId, dc.safetyWay, dc.registerWay,dc.certNum, dc.certifiable,  " +
            " dc.errCode,dc.endTime, dc.secrecy,   dc.ipAddress,  dc.port,  dc.PTZType,  dc.password, dc.status, " +
            " dc.longitudeWgs84 as longitude, dc.latitudeWgs84 as latitude,  pc.businessGroupId " +
            " from device_channel dc" +
            " left join platform_gb_channel pgc on  dc.id = pgc.deviceChannelId" +
            " left join platform_catalog pc on pgc.catalogId = pc.id and pgc.platformId = pc.platformId" +
            " where pgc.platformId=#{serverGBId}")
    List queryChannelWithCatalog(String serverGBId);
    @Select("select * from device_channel where deviceId = #{deviceId}")
    List queryAllChannels(String deviceId);
    @Select("select count(1) as total, sum(status) as online from device_channel")
    ResourceBaceInfo getOverview();
}