<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yeejoin.amos.boot.module.hygf.api.mapper.JpCollectorMapper">
    <select id="queryCollectorCountStatus"  resultType="Map">


        select
        hygf_jp_collector.`state`,

        count(hygf_jp_collector.sequence_nbr) num

        from hygf_jp_collector    left JOIN hygf_jp_station

        on hygf_jp_station.third_station_id=hygf_jp_collector.third_station_id
        <where>
            <if test="dto.snCode!=null and dto.snCode!='' ">
                and hygf_jp_station.sn_code like concat(concat('%',#{dto.snCode}),'%')
            </if>
            <if test="dto.name!=null and dto.name!='' ">
                and hygf_jp_station.name like concat(concat('%',#{dto.name}),'%')
            </if>
            <if test="dto.area!=null">
                and hygf_jp_station.area like concat(concat('%',#{dto.area}),'%')
            </if>
            <if test="dto.statioId!=null">
                and hygf_jp_station.third_station_id in
                <foreach collection="dto.statioId" item="item" index="index" open="(" separator="," close=")">
                    #{item.stationId}
                </foreach>
            </if>
            <if test="dto.thirdStationId!=null">
                and hygf_jp_station.third_station_id =#{dto.thirdStationId}
            </if>

            and hygf_jp_collector.`state` is not null and hygf_jp_collector.`state` in ("在线","报警","离线")
        </where>



        GROUP BY hygf_jp_collector.`state`
    </select>

    <select id="selectPageData" resultType="com.yeejoin.amos.boot.module.hygf.api.dto.JpCollectorDto">
    select
    hjc.state,
        hjc.sn_code as snCode,
        hjc.station_name as stationName,
        hjc.update_time AS updateTime,
    hjc.type as collectorType,
        sta.name as name,
        sta.type as type,
        hjc.signal_strength as signalStrength,
        pc.COMPANY_NAME as companyName,
        hjc.sequence_nbr as sequenceNbr,
        sta.third_station_id thirdStationId
    from
    hygf_jp_collector  hjc
    left join hygf_jp_station sta on sta.third_station_id = hjc.third_station_id
        left join privilege_company pc on sta.regional_companies_code = pc.ORG_CODE
    <where>
        <if test="dto.snCode != null and dto.snCode != ''">
            hjc.sn_code like #{dto.snCode}
        </if>
        <if test="dto.type != null and dto.type !=''">
            hjc.type = #{dto.type}
        </if>
        <if test="dto.state != null and dto.state !=''">
            hjc.state = #{dto.state}
        </if>
    </where>
    limit #{current},#{size}
    </select>

    <select id="selectPageDataTota" resultType="int">
        select
        count(1)
        from
        hygf_jp_collector  hjc
        left join hygf_jp_station sta on sta.third_station_id = hjc.third_station_id
        left join privilege_company pc on sta.regional_companies_code = pc.ORG_CODE
        <where>
            <if test="dto.snCode != null and dto.snCode != ''">
                hjc.sn_code like #{dto.snCode}
            </if>
            <if test="dto.type != null and dto.type !=''">
                hjc.type = #{dto.type}
            </if>
            <if test="dto.state != null and dto.state !=''">
                hjc.state = #{dto.state}
            </if>
        </where>
    </select>
    <select id="selectInverterState" resultType="java.util.Map">
        SELECT
            all_statuses.status,
            COALESCE(COUNT(hygf_jp_collector.state), 0) AS count
        FROM (
            SELECT '在线' AS status
            UNION ALL
            SELECT '离线' AS status
            UNION ALL
            SELECT '报警' AS status
            ) AS all_statuses
            LEFT JOIN hygf_jp_collector ON all_statuses.status = hygf_jp_collector.state and   hygf_jp_collector.third_station_id
        in(
        <foreach collection="thirdStationIds" item="item" separator=",">
            (#{item})
        </foreach>)


        GROUP BY all_statuses.status
        ORDER BY all_statuses.status;
    </select>
</mapper>
