package com.yfd.monitor.storager.dao;

import com.yfd.monitor.gdw2019.bean.DeviceChannel;
import com.yfd.monitor.gdw2019.bean.GbStream;
import com.yfd.monitor.gdw2019.bean.PlatformCatalog;
import com.yfd.monitor.gdw2019.bean.PlatformGbStream;
import com.yfd.monitor.media.zlm.dto.StreamProxyItem;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;


@Mapper
@Repository
public interface PlatformCatalogMapper {

    @Insert("INSERT INTO mon_platform_catalog (id, name, platformId, parentId, civilCode, businessGroupId) VALUES" +
            "(#{id}, #{name}, #{platformId}, #{parentId}, #{civilCode}, #{businessGroupId})")
    int add(PlatformCatalog platformCatalog);

    @Delete("DELETE FROM mon_platform_catalog WHERE id=#{id}")
    int del(String id);

    @Delete("DELETE FROM mon_platform_catalog WHERE platformId=#{platformId}")
    int delByPlatformId(String platformId);

    @Select("SELECT pc.*, count(pc2.id) as childrenCount FROM mon_platform_catalog pc " +
            "left join mon_platform_catalog pc2 on pc.id = pc2.parentId " +
            "WHERE pc.parentId=#{parentId} AND pc.platformId=#{platformId} group by pc.id")
    List<PlatformCatalog> selectByParentId(String platformId, String parentId);

    @Select("SELECT *, (SELECT COUNT(1) from mon_platform_catalog where parentId = pc.id) as childrenCount  FROM mon_platform_catalog pc WHERE pc.id=#{id}")
    PlatformCatalog select(String id);

    @Update(value = {" <script>" +
            "UPDATE mon_platform_catalog " +
            "SET name=#{name}" +
            "WHERE id=#{id}"+
            "</script>"})
    int update(PlatformCatalog platformCatalog);

    @Select("SELECT *, (SELECT COUNT(1) from mon_platform_catalog where parentId = pc.id) as childrenCount  FROM mon_platform_catalog pc WHERE pc.platformId=#{platformId}")
    List<PlatformCatalog> selectByPlatForm(String platformId);

    @Select("SELECT pc.* FROM  mon_platform_catalog pc WHERE  pc.id = (SELECT pp.catalogId from mon_parent_platform pp WHERE pp.serverGBId=#{platformId})")
    PlatformCatalog selectDefaultByPlatFormId(String platformId);


    @Select("SELECT pc.* FROM  mon_platform_catalog pc WHERE pc.id = #{id}")
    PlatformCatalog selectParentCatalog(String id);

    @Select("SELECT pc.id as channelId, pc.name, pc.civilCode, pc.businessGroupId,'1' as parental, pc.parentId  " +
            " FROM mon_platform_catalog pc WHERE pc.platformId=#{platformId}")
    List<DeviceChannel> queryCatalogInPlatform(String platformId);

    @Select("SELECT *, " +
            "(SELECT COUNT(1) from mon_platform_catalog where parent_id = pc.id) as children_count " +
            " from mon_platform_catalog pc " +
            " WHERE pc.id=#{id} and pc.platform_id=#{platformId}")
    PlatformCatalog selectByPlatFormAndCatalogId(@Param("platformId") String platformId, @Param("id") String id);
}
