package com.yfd.monitor.storager.dao;

import com.yfd.monitor.gdw2019.bean.ParentPlatform;
import com.yfd.monitor.storager.dao.dto.ChannelSourceInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 用于存储上级平台
 */
@Mapper
@Repository
public interface ParentPlatformMapper {

    @Insert("INSERT INTO mon_parent_platform (enable, name, serverGBId, serverGBDomain, serverIP, serverPort, deviceGBId, deviceIp,  " +
            "            devicePort, username, password, expires, keepTimeout, transport, characterSet, ptz, rtcp, asMessageChannel, " +
            "            status, startOfflinePush, catalogId, administrativeDivision, catalogGroup, createTime, updateTime, treeType) " +
            "            VALUES (#{enable}, #{name}, #{serverGBId}, #{serverGBDomain}, #{serverIP}, #{serverPort}, #{deviceGBId}, #{deviceIp}, " +
            "            #{devicePort}, #{username}, #{password}, #{expires}, #{keepTimeout}, #{transport}, #{characterSet}, #{ptz}, #{rtcp}, #{asMessageChannel}, " +
            "            #{status},  #{startOfflinePush}, #{catalogId}, #{administrativeDivision}, #{catalogGroup}, #{createTime}, #{updateTime}, #{treeType})")
    int addParentPlatform(ParentPlatform parentPlatform);

    @Update("UPDATE mon_parent_platform " +
            "SET enable=#{enable}, " +
            "name=#{name}," +
            "deviceGBId=#{deviceGBId}," +
            "serverGBId=#{serverGBId}, " +
            "serverGBDomain=#{serverGBDomain}, " +
            "serverIP=#{serverIP}," +
            "serverPort=#{serverPort}, " +
            "deviceIp=#{deviceIp}, " +
            "devicePort=#{devicePort}, " +
            "username=#{username}, " +
            "password=#{password}, " +
            "expires=#{expires}, " +
            "keepTimeout=#{keepTimeout}, " +
            "transport=#{transport}, " +
            "characterSet=#{characterSet}, " +
            "ptz=#{ptz}, " +
            "rtcp=#{rtcp}, " +
            "asMessageChannel=#{asMessageChannel}, " +
            "status=#{status}, " +
            "startOfflinePush=#{startOfflinePush}, " +
            "catalogGroup=#{catalogGroup}, " +
            "administrativeDivision=#{administrativeDivision}, " +
            "createTime=#{createTime}, " +
            "updateTime=#{updateTime}, " +
            "treeType=#{treeType}, " +
            "catalogId=#{catalogId} " +
            "WHERE id=#{id}")
    int updateParentPlatform(ParentPlatform parentPlatform);

    @Delete("DELETE FROM mon_parent_platform WHERE serverGBId=#{serverGBId}")
    int delParentPlatform(ParentPlatform parentPlatform);

    @Select("SELECT *, ((SELECT count(0)\n" +
            "              FROM mon_platform_gb_channel pc\n" +
            "              WHERE pc.platformId = pp.serverGBId)\n" +
            "              +\n" +
            "              (SELECT count(0)\n" +
            "              FROM mon_platform_gb_stream pgs\n" +
            "              WHERE pgs.platformId = pp.serverGBId)\n" +
            "              +\n" +
            "              (SELECT count(0)\n" +
            "              FROM mon_platform_catalog pgc\n" +
            "              WHERE pgc.platformId = pp.serverGBId)) as channelCount\n" +
            "FROM mon_parent_platform pp ")
    List<ParentPlatform> getParentPlatformList();

    @Select("SELECT * FROM mon_parent_platform WHERE enable=#{enable} ")
    List<ParentPlatform> getEnableParentPlatformList(boolean enable);

    @Select("SELECT * FROM mon_parent_platform WHERE enable=1 and asMessageChannel = 1")
    List<ParentPlatform> queryEnablePlatformListWithAsMessageChannel();

    @Select("SELECT * FROM mon_parent_platform WHERE serverGBId=#{platformGbId}")
    ParentPlatform getParentPlatByServerGBId(String platformGbId);

    @Select("SELECT * FROM mon_parent_platform WHERE id=#{id}")
    ParentPlatform getParentPlatById(int id);

    @Update("UPDATE mon_parent_platform SET status=false" )
    int outlineForAllParentPlatform();

    @Update("UPDATE mon_parent_platform SET status=#{online} WHERE serverGBId=#{platformGbID}" )
    int updateParentPlatformStatus(String platformGbID, boolean online);

    @Update(value = {" <script>" +
            "UPDATE mon_parent_platform " +
            "SET catalogId=#{catalogId}, updateTime=#{updateTime}" +
            "WHERE serverGBId=#{platformId}"+
            "</script>"})
    int setDefaultCatalog(String platformId, String catalogId, String updateTime);

    @Select("select 'channel' as name, count(pgc.platformId) count from mon_platform_gb_channel pgc left join mon_device_channel dc on dc.id = pgc.deviceChannelId where  pgc.platformId=#{platformId} and dc.channelId =#{gbId} " +
            "union " +
            "select 'stream' as name, count(pgs.platformId) count from mon_platform_gb_stream pgs left join mon_gb_stream gs on pgs.gbStreamId = gs.gbStreamId where  pgs.platformId=#{platformId} and gs.gbId = #{gbId}")
    List<ChannelSourceInfo> getChannelSource(String platformId, String gbId);
}
