package com.yfd.monitor.storager.dao;

import com.yfd.monitor.gdw2019.bean.Device;
import com.yfd.monitor.gdw2019.bean.DeviceChannel;
import com.yfd.monitor.gdw2019.bean.DeviceChannelInPlatform;
import com.yfd.monitor.vmanager.bean.ResourceBaceInfo;
import com.yfd.monitor.vmanager.gdw2019.platform.bean.ChannelReduce;
import com.yfd.monitor.web.gdw2019.dto.DeviceChannelExtend;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用于存储设备通道信息
 */
@Mapper
@Repository
public interface DeviceChannelMapper {

    @Insert("INSERT INTO mon_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 = {" <script>" +
            "UPDATE mon_device_channel " +
            "SET updateTime=#{updateTime}" +
            "<if test='name != null'>, name=#{name}</if>" +
            "<if test='manufacture != null'>, manufacture=#{manufacture}</if>" +
            "<if test='model != null'>, model=#{model}</if>" +
            "<if test='owner != null'>, owner=#{owner}</if>" +
            "<if test='civilCode != null'>, civilCode=#{civilCode}</if>" +
            "<if test='block != null'>, block=#{block}</if>" +
            "<if test='address != null'>, address=#{address}</if>" +
            "<if test='parental != null'>, parental=#{parental}</if>" +
            "<if test='parentId != null'>, parentId=#{parentId}</if>" +
            "<if test='safetyWay != null'>, safetyWay=#{safetyWay}</if>" +
            "<if test='registerWay != null'>, registerWay=#{registerWay}</if>" +
            "<if test='certNum != null'>, certNum=#{certNum}</if>" +
            "<if test='certifiable != null'>, certifiable=#{certifiable}</if>" +
            "<if test='errCode != null'>, errCode=#{errCode}</if>" +
            "<if test='secrecy != null'>, secrecy=#{secrecy}</if>" +
            "<if test='ipAddress != null'>, ipAddress=#{ipAddress}</if>" +
            "<if test='port != null'>, port=#{port}</if>" +
            "<if test='password != null'>, password=#{password}</if>" +
            "<if test='PTZType != null'>, PTZType=#{PTZType}</if>" +
            "<if test='status != null'>, status=#{status}</if>" +
            "<if test='streamId != null'>, streamId=#{streamId}</if>" +
            "<if test='hasAudio != null'>, hasAudio=#{hasAudio}</if>" +
            "<if test='longitude != null'>, longitude=#{longitude}</if>" +
            "<if test='latitude != null'>, latitude=#{latitude}</if>" +
            "<if test='longitudeGcj02 != null'>, longitudeGcj02=#{longitudeGcj02}</if>" +
            "<if test='latitudeGcj02 != null'>, latitudeGcj02=#{latitudeGcj02}</if>" +
            "<if test='longitudeWgs84 != null'>, longitudeWgs84=#{longitudeWgs84}</if>" +
            "<if test='latitudeWgs84 != null'>, latitudeWgs84=#{latitudeWgs84}</if>" +
            "<if test='businessGroupId != null'>, businessGroupId=#{businessGroupId}</if>" +
            "<if test='gpsTime != null'>, gpsTime=#{gpsTime}</if>" +
            "WHERE deviceId=#{deviceId} AND channelId=#{channelId}"+
            " </script>"})
    int update(DeviceChannel channel);

    @Select(value = {" <script>" +
            "SELECT " +
            "dc.* " +
            "from " +
            "mon_device_channel dc " +
            "WHERE " +
            "dc.deviceId = #{deviceId} " +
" <if test='query != null'> AND (dc.channelId LIKE concat('%',#{query},'%') OR dc.name LIKE concat('%',#{query},'%') OR dc.name LIKE concat('%',#{query},'%'))</if> " +
            " <if test='parentChannelId != null'> AND (dc.parentId=#{parentChannelId} OR dc.civilCode = #{parentChannelId}) </if> " +
            " <if test='online == true' > AND dc.status=1</if>" +
            " <if test='online == false' > AND dc.status=0</if>" +
            " <if test='hasSubChannel == true' >  AND dc.subCount > 0 </if>" +
            " <if test='hasSubChannel == false' >  AND dc.subCount = 0 </if>" +
            "<if test='channelIds != null'> AND dc.channelId in <foreach item='item' index='index' collection='channelIds' open='(' separator=',' close=')'>" +
            "#{item} " +
            "</foreach> </if>" +
            "ORDER BY dc.channelId " +
            " </script>"})
    List<DeviceChannel> queryChannels(String deviceId, String parentChannelId, String query, Boolean hasSubChannel, Boolean online, List<String> channelIds);

    @Select(value = {" <script>" +
            "SELECT " +
            "dc.*, " +
            "de.name as deviceName, " +
            "de.online as deviceOnline " +
            "from " +
            "mon_device_channel dc " +
            "LEFT JOIN mon_device de ON dc.deviceId = de.deviceId " +
            "WHERE 1=1" +
            " <if test='deviceId != null'> AND dc.deviceId = #{deviceId} </if> " +
            " <if test='query != null'> AND (dc.channelId LIKE '%${query}%' OR dc.name LIKE '%${query}%' OR dc.name LIKE '%${query}%')</if> " +
            " <if test='parentChannelId != null'> AND dc.parentId=#{parentChannelId} </if> " +
            " <if test='online == true' > AND dc.status=1</if>" +
            " <if test='online == false' > AND dc.status=0</if>" +
            " <if test='hasSubChannel == true' >  AND dc.subCount > 0 </if>" +
            " <if test='hasSubChannel == false' >  AND dc.subCount = 0 </if>" +
            "<if test='channelIds != null'> AND dc.channelId in <foreach item='item' index='index' collection='channelIds' open='(' separator=',' close=')'>" +
            "#{item} " +
            "</foreach> </if>" +
            "ORDER BY dc.channelId ASC" +
            " </script>"})
    List<DeviceChannelExtend> queryChannelsWithDeviceInfo(String deviceId, String parentChannelId, String query, Boolean hasSubChannel, Boolean online, List<String> channelIds);


    @Select(value = {" <script>" +
            "SELECT " +
            "dc.*, " +
            "de.name as deviceName, " +
            "de.online as deviceOnline " +
            "from " +
            "mon_device_channel dc " +
            "LEFT JOIN mon_device de ON dc.deviceId = de.deviceId " +
            "WHERE 1=1" +
            " <if test='deviceId != null'> AND dc.deviceId = #{deviceId} </if> " +
            " <if test='query != null'> AND (dc.channelId LIKE '%${query}%' OR dc.name LIKE '%${query}%' OR dc.name LIKE '%${query}%')</if> " +
            " <if test='parentChannelId != null'> AND dc.parentId=#{parentChannelId} </if> " +
            " <if test='online == true' > AND dc.status=1</if>" +
            " <if test='online == false' > AND dc.status=0</if>" +
            " <if test='hasSubChannel == true' >  AND dc.subCount > 0 </if>" +
            " <if test='hasSubChannel == false' >  AND dc.subCount = 0 </if>" +
            "<if test='channelIds != null'> AND dc.channelId in <foreach item='item' index='index' collection='channelIds' open='(' separator=',' close=')'>" +
            "#{item} " +
            "</foreach> </if>" +
            "ORDER BY dc.channelId ASC " +
            "Limit #{limit} OFFSET #{start}" +
            " </script>"})
    List<DeviceChannelExtend> queryChannelsByDeviceIdWithStartAndLimit(String deviceId,List<String> channelIds, String parentChannelId, String query,
                                                                       Boolean hasSubChannel, Boolean online, int start, int limit);

    @Select("SELECT * FROM mon_device_channel WHERE deviceId=#{deviceId} AND channelId=#{channelId}")
    DeviceChannel queryChannel(String deviceId, String channelId);

    //SELECT * FROM mon_device_channel WHERE (ipAddress=#{deviceIp} OR address = #{deviceIp}) AND port != 0 AND parentId is not null
    @Select("SELECT * FROM mon_device_channel WHERE (ipAddress=#{deviceIp} OR address = #{deviceIp}) AND parentId is not null ORDER BY `port` DESC")
    List<DeviceChannel> queryChannelByDeviceIp(String deviceIp);

    @Delete("DELETE FROM mon_device_channel WHERE deviceId=#{deviceId}")
    int cleanChannelsByDeviceId(String deviceId);

    @Delete("DELETE FROM mon_device_channel WHERE deviceId=#{deviceId} AND channelId=#{channelId}")
    int del(String deviceId, String channelId);

    @Update(value = {"UPDATE mon_device_channel SET streamId=null WHERE deviceId=#{deviceId} AND channelId=#{channelId}"})
    void stopPlay(String deviceId, String channelId);

    @Update(value = {"UPDATE mon_device_channel SET streamId=#{streamId} WHERE deviceId=#{deviceId} AND channelId=#{channelId}"})
    void startPlay(String deviceId, String channelId, String streamId);

    @Select(value = {" <script>" +
            "SELECT " +
            "    dc.id,\n" +
            "    dc.channelId,\n" +
            "    dc.deviceId,\n" +
            "    dc.name,\n" +
            "    de.manufacturer,\n" +
            "    de.hostAddress,\n" +
            "    dc.subCount,\n" +
            "    pgc.platformId as platformId,\n" +
            "    pgc.catalogId as catalogId " +
            " FROM mon_device_channel dc " +
            " LEFT JOIN mon_device de ON dc.deviceId = de.deviceId " +
            " LEFT JOIN mon_platform_gb_channel pgc on pgc.deviceChannelId = dc.id " +
            " WHERE 1=1 " +
            " <if test='query != null'> AND (dc.channelId LIKE concat('%',#{query},'%') OR dc.name LIKE concat('%',#{query},'%') OR dc.name LIKE concat('%',#{query},'%'))</if> " +
            " <if test='online == true' > AND dc.status=1</if> " +
            " <if test='online == false' > AND dc.status=0</if> " +
            " <if test='hasSubChannel!= null and hasSubChannel == true' >  AND dc.subCount > 0</if> " +
            " <if test='hasSubChannel!= null and hasSubChannel == false' >  AND dc.subCount = 0</if> " +
            " <if test='catalogId == null ' >  AND dc.id not in (select deviceChannelId from mon_platform_gb_channel where platformId=#{platformId} ) </if> " +
            " <if test='catalogId != null ' >  AND pgc.platformId = #{platformId} and pgc.catalogId=#{catalogId} </if> " +
            " ORDER BY dc.deviceId, dc.channelId ASC" +
            " </script>"})
    List<ChannelReduce> queryChannelListInAll(String query, Boolean online, Boolean hasSubChannel, String platformId, String catalogId);

    @Select(value = {" <script>" +
            "SELECT " +
            "    dc.*,\n" +
            "    pgc.platformId as platformId,\n" +
            "    pgc.catalogId as catalogId " +
            " FROM mon_device_channel dc " +
            " LEFT JOIN mon_platform_gb_channel pgc on pgc.deviceChannelId = dc.id " +
            " WHERE pgc.platformId = #{platformId} " +
            " ORDER BY dc.deviceId, dc.channelId ASC" +
            " </script>"})
    List<DeviceChannelInPlatform> queryChannelByPlatformId(String platformId);


    @Select("SELECT * FROM mon_device_channel WHERE channelId=#{channelId}")
    List<DeviceChannel> queryChannelByChannelId( String channelId);

    @Update(value = {"UPDATE mon_device_channel SET status=0 WHERE deviceId=#{deviceId} AND channelId=#{channelId}"})
    void offline(String deviceId,  String channelId);

    @Update(value = {"UPDATE mon_device_channel SET status=0 WHERE deviceId=#{deviceId}"})
    void offlineByDeviceId(String deviceId);

    @Insert("<script> " +
            "insert into mon_device_channel " +
            "(channelId, deviceId, name, manufacture, model, owner, civilCode, block, subCount, " +
            "  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 " +
            "<foreach collection='addChannels' index='index' item='item' separator=','> " +
            "(#{item.channelId}, #{item.deviceId}, #{item.name}, #{item.manufacture}, #{item.model}, " +
            "#{item.owner}, #{item.civilCode}, #{item.block},#{item.subCount}," +
            "#{item.address}, #{item.parental}, #{item.parentId}, #{item.safetyWay}, #{item.registerWay}, " +
            "#{item.certNum}, #{item.certifiable}, #{item.errCode}, #{item.secrecy}, " +
            "#{item.ipAddress}, #{item.port}, #{item.password}, #{item.PTZType}, #{item.status}, " +
            "#{item.streamId}, #{item.longitude}, #{item.latitude},#{item.longitudeGcj02}, " +
            "#{item.latitudeGcj02},#{item.longitudeWgs84}, #{item.latitudeWgs84}, #{item.hasAudio}, now(), now(), " +
            "#{item.businessGroupId}, #{item.gpsTime}) " +
            "</foreach> " +
            "ON DUPLICATE KEY UPDATE " +
            "updateTime=VALUES(updateTime), " +
            "name=VALUES(name), " +
            "manufacture=VALUES(manufacture), " +
            "model=VALUES(model), " +
            "owner=VALUES(owner), " +
            "civilCode=VALUES(civilCode), " +
            "block=VALUES(block), " +
            "subCount=VALUES(subCount), " +
            "address=VALUES(address), " +
            "parental=VALUES(parental), " +
            "parentId=VALUES(parentId), " +
            "safetyWay=VALUES(safetyWay), " +
            "registerWay=VALUES(registerWay), " +
            "certNum=VALUES(certNum), " +
            "certifiable=VALUES(certifiable), " +
            "errCode=VALUES(errCode), " +
            "secrecy=VALUES(secrecy), " +
            "ipAddress=VALUES(ipAddress), " +
            "port=VALUES(port), " +
            "password=VALUES(password), " +
            "PTZType=VALUES(PTZType), " +
            "status=VALUES(status), " +
            "streamId=VALUES(streamId), " +
            "longitude=VALUES(longitude), " +
            "latitude=VALUES(latitude), " +
            "longitudeGcj02=VALUES(longitudeGcj02), " +
            "latitudeGcj02=VALUES(latitudeGcj02), " +
            "longitudeWgs84=VALUES(longitudeWgs84), " +
            "latitudeWgs84=VALUES(latitudeWgs84), " +
            "hasAudio=VALUES(hasAudio), " +
            "businessGroupId=VALUES(businessGroupId), " +
            "gpsTime=VALUES(gpsTime)" +
            "</script>")
    int batchAdd(List<DeviceChannel> addChannels);

    @Update(value = {"UPDATE mon_device_channel SET status=1 WHERE deviceId=#{deviceId} AND channelId=#{channelId}"})
    void online(String deviceId,  String channelId);

    @Update({"<script>" +
            "<foreach collection='updateChannels' item='item' separator=';'>" +
            " UPDATE" +
            " mon_device_channel" +
            " SET updateTime=#{item.updateTime}" +
            "<if test='item.name != null'>, name=#{item.name}</if>" +
            "<if test='item.manufacture != null'>, manufacture=#{item.manufacture}</if>" +
            "<if test='item.model != null'>, model=#{item.model}</if>" +
            "<if test='item.owner != null'>, owner=#{item.owner}</if>" +
            "<if test='item.civilCode != null'>, civilCode=#{item.civilCode}</if>" +
            "<if test='item.block != null'>, block=#{item.block}</if>" +
            "<if test='item.subCount != null'>, subCount=#{item.subCount}</if>" +
            "<if test='item.address != null'>, address=#{item.address}</if>" +
            "<if test='item.parental != null'>, parental=#{item.parental}</if>" +
            "<if test='item.parentId != null'>, parentId=#{item.parentId}</if>" +
            "<if test='item.safetyWay != null'>, safetyWay=#{item.safetyWay}</if>" +
            "<if test='item.registerWay != null'>, registerWay=#{item.registerWay}</if>" +
            "<if test='item.certNum != null'>, certNum=#{item.certNum}</if>" +
            "<if test='item.certifiable != null'>, certifiable=#{item.certifiable}</if>" +
            "<if test='item.errCode != null'>, errCode=#{item.errCode}</if>" +
            "<if test='item.secrecy != null'>, secrecy=#{item.secrecy}</if>" +
            "<if test='item.ipAddress != null'>, ipAddress=#{item.ipAddress}</if>" +
            "<if test='item.port != null'>, port=#{item.port}</if>" +
            "<if test='item.password != null'>, password=#{item.password}</if>" +
            "<if test='item.PTZType != null'>, PTZType=#{item.PTZType}</if>" +
            "<if test='item.status != null'>, status=#{item.status}</if>" +
            "<if test='item.streamId != null'>, streamId=#{item.streamId}</if>" +
            "<if test='item.hasAudio != null'>, hasAudio=#{item.hasAudio}</if>" +
            "<if test='item.longitude != null'>, longitude=#{item.longitude}</if>" +
            "<if test='item.latitude != null'>, latitude=#{item.latitude}</if>" +
            "<if test='item.longitudeGcj02 != null'>, longitudeGcj02=#{item.longitudeGcj02}</if>" +
            "<if test='item.latitudeGcj02 != null'>, latitudeGcj02=#{item.latitudeGcj02}</if>" +
            "<if test='item.longitudeWgs84 != null'>, longitudeWgs84=#{item.longitudeWgs84}</if>" +
            "<if test='item.latitudeWgs84 != null'>, latitudeWgs84=#{item.latitudeWgs84}</if>" +
            "<if test='item.businessGroupId != null'>, businessGroupId=#{item.businessGroupId}</if>" +
            "<if test='item.gpsTime != null'>, gpsTime=#{item.gpsTime}</if>" +
            "<if test='item.id > 0'>WHERE id=#{item.id}</if>" +
            "<if test='item.id == 0'>WHERE deviceId=#{item.deviceId} AND channelId=#{item.channelId}</if>" +
            "</foreach>" +
            "</script>"})
    int batchUpdate(List<DeviceChannel> updateChannels);


    @Select("SELECT * FROM mon_device_channel WHERE deviceId=#{deviceId} AND status=1")
    List<DeviceChannel> queryOnlineChannelsByDeviceId(String deviceId);

    @Delete(value = {" <script>" +
            "DELETE " +
            "from " +
            "mon_device_channel " +
            "WHERE " +
            "deviceId = #{deviceId} " +
            " AND channelId NOT IN " +
            "<foreach collection='channels'  item='item'  open='(' separator=',' close=')' > #{item.channelId}</foreach>" +
            " </script>"})
    int cleanChannelsNotInList(String deviceId, List<DeviceChannel> channels);

    @Update(" update mon_device_channel" +
            " set subCount = (select *" +
            "                from (select count(0)" +
            "                      from mon_device_channel" +
            "                      where deviceId = #{deviceId} and parentId = #{channelId}) as temp)" +
            " where deviceId = #{deviceId} " +
            " and channelId = #{channelId}")
    int updateChannelSubCount(String deviceId, String channelId);

    @Update(value = {" <script>" +
            "UPDATE mon_device_channel " +
            "SET " +
            "latitude=#{latitude}, " +
            "longitude=#{longitude}, " +
            "longitudeGcj02=#{longitudeGcj02}, " +
            "latitudeGcj02=#{latitudeGcj02}, " +
            "longitudeWgs84=#{longitudeWgs84}, " +
            "latitudeWgs84=#{latitudeWgs84}, " +
            "gpsTime=#{gpsTime} " +
            "WHERE deviceId=#{deviceId} " +
            " <if test='channelId != null' >  AND channelId=#{channelId}</if>" +
            " </script>"})
    void updatePosition(DeviceChannel deviceChannel);

    @Select("SELECT * FROM mon_device_channel WHERE length(trim(streamId)) > 0")
    List<DeviceChannel> getAllChannelInPlay();

    @Select("select * from mon_device_channel where longitude*latitude > 0 and deviceId = #{deviceId}")
    List<DeviceChannel> getAllChannelWithCoordinate(String deviceId);


    @Select(value = {" <script>" +
            "select * " +
            "from mon_device_channel " +
            "where deviceId=#{deviceId}" +
            " <if test='parentId != null and length != null' > and parentId = #{parentId} or left(channelId, LENGTH(#{parentId})) = #{parentId} and length(channelId)=#{length} </if>" +
            " <if test='parentId == null and length != null' > and parentId = #{parentId} or length(channelId)=#{length} </if>" +
            " <if test='parentId == null and length == null' > and parentId = #{parentId} </if>" +
            " <if test='parentId != null and length == null' > and parentId = #{parentId} or left(channelId, LENGTH(#{parentId})) = #{parentId} </if>" +
            " </script>"})
    List<DeviceChannel> getChannelsWithCivilCodeAndLength(String deviceId, String parentId, Integer length);

    @Select(value = {" <script>" +
            "select * " +
            "from mon_device_channel " +
            "where deviceId=#{deviceId} and length(channelId)>14 and civilCode=#{parentId}" +
            " </script>"})
    List<DeviceChannel> getChannelsByCivilCode(String deviceId, String parentId);

    @Select("select min(length(channelId)) as minLength " +
            "from mon_device_channel " +
            "where deviceId=#{deviceId}")
    Integer getChannelMinLength(String deviceId);

    @Select("select * from mon_device_channel where deviceId=#{deviceId} and civilCode not in " +
            "(select civilCode from mon_device_channel where deviceId=#{deviceId} group by civilCode)")
    List<DeviceChannel> getChannelWithoutCiviCode(String deviceId);

    @Select("select * from mon_device_channel where deviceId=#{deviceId} and SUBSTRING(channelId, 11, 3)=#{typeCode}")
    List<DeviceChannel> getBusinessGroups(String deviceId, String typeCode);

//    @Select("select dc.id, dc.channelId, CONCAT(SUBSTRING('100110000101040000', 1, CHAR_LENGTH('100110000101040000') - 4), SUBSTRING(sp.patroldevice_code, CHAR_LENGTH(sp.patroldevice_code) - 3, 4)) 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 mon_device_channel dc" +
//            " left join mon_platform_gb_channel pgc on  dc.id = pgc.deviceChannelId" +
//            " left join iis_substation_patroldevice sp on  sp.internationalId = dc.deviceId" +
//            " left join mon_platform_catalog pc on pgc.catalogId = pc.id and pgc.platformId = pc.platformId" +
//            " where pc.id=#{catalogId} and sp.patroldevice_code is not null")
@Select("select dc.id, dc.channelId, sp.patroldevice_code 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 mon_device_channel dc" +
        " left join mon_platform_gb_channel pgc on  dc.id = pgc.deviceChannelId" +
        " left join iis_substation_patroldevice sp on  sp.internationalId = dc.deviceId" +
        " left join mon_platform_catalog pc on pgc.catalogId = pc.id and pgc.platformId = pc.platformId" +
        " where pc.id=#{catalogId} and sp.patroldevice_code is not null")
    List<DeviceChannel> queryChannelWithCatalog(String catalogId);


//    @Select("select dc.id, dc.channelId,CONCAT(SUBSTRING('100110000101040000', 1, CHAR_LENGTH('100110000101040000') - 4), SUBSTRING(sp.patroldevice_code, CHAR_LENGTH(sp.patroldevice_code) - 3, 4)) 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 mon_device_channel dc" +
//            " left join iis_substation_patroldevice sp on  sp.internationalId = dc.deviceId" +
//            " left join mon_platform_gb_channel pgc on  dc.id = pgc.deviceChannelId" +
//            " left join mon_platform_catalog pc on pgc.catalogId = pc.id and pgc.platformId = pc.platformId" +
//            " where pc.platformId=#{platformId} and sp.patroldevice_code is not null")
@Select("select dc.id, dc.channelId,sp.patroldevice_code 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 mon_device_channel dc" +
        " left join iis_substation_patroldevice sp on  sp.internationalId = dc.deviceId" +
        " left join mon_platform_gb_channel pgc on  dc.id = pgc.deviceChannelId" +
        " left join mon_platform_catalog pc on pgc.catalogId = pc.id and pgc.platformId = pc.platformId" +
        " where pc.platformId=#{platformId} and sp.patroldevice_code is not null")
    List<DeviceChannel> queryChannelWithPlatform(String platformId);


    @Select("select * from mon_device_channel where deviceId = #{deviceId}")
    List<DeviceChannel> queryAllChannels(String deviceId);

    @Select("select b.* from iis_substation_patroldevice a join mon_device_channel b on (a.patroldevice_code=b.deviceId) where a.online='1' and a.type='11' order by b.channelId")
    List<DeviceChannel> queryChannelByOnLineNvr();


    @Select("select count(1) as total, sum(status) as online from mon_device_channel")
    ResourceBaceInfo getOverview();

    @Select("select channelId" +
            ", deviceId" +
            ", latitude" +
            ", longitude" +
            ", latitudeWgs84" +
            ", longitudeWgs84" +
            ", latitudeGcj02" +
            ", longitudeGcj02 " +
            "from mon_device_channel where deviceId = #{deviceId} " +
            "and latitude != 0 " +
            "and  longitude != 0 " +
            "and (latitudeGcj02 = 0 or latitudeWgs84 = 0 or longitudeWgs84 = 0 or longitudeGcj02 = 0)")
    List<DeviceChannel> getChannelsWithoutTransform(String deviceId);

    @Select("select de.* from mon_device de left join mon_device_channel dc on de.deviceId = dc.deviceId where dc.channelId=#{channelId}")
    List<Device> getDeviceByChannelId(String channelId);


    @Delete({"<script>" +
            "<foreach collection='deleteChannelList' item='item' separator=';'>" +
            "DELETE FROM mon_device_channel WHERE deviceId=#{item.deviceId} AND channelId=#{item.channelId}" +
            "</foreach>" +
            "</script>"})
    int batchDel(List<DeviceChannel> deleteChannelList);

    @Update({"<script>" +
            "<foreach collection='channels' item='item' separator=';'>" +
            "UPDATE mon_device_channel SET status=1 WHERE deviceId=#{item.deviceId} AND channelId=#{item.channelId}" +
            "</foreach>" +
            "</script>"})
    int batchOnline(List<DeviceChannel> channels);

    @Update({"<script>" +
            "<foreach collection='channels' item='item' separator=';'>" +
            "UPDATE mon_device_channel SET status=0 WHERE deviceId=#{item.deviceId} AND channelId=#{item.channelId}" +
            "</foreach>" +
            "</script>"})
    int batchOffline(List<DeviceChannel> channels);

    @Select("select dc.* from mon_device_channel dc INNER JOIN iis_substation_patroldevice sp ON sp.internationalId=dc.deviceId where sp.patroldevice_code = #{deviceId}")
    List<DeviceChannel> queryChannelList(String code);
}
