<?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.JpInverterMapper">
    <select id="queryInverterCountStatus"  resultType="Map">


        select
        hygf_jp_inverter.`state`,

        count(hygf_jp_inverter.sequence_nbr) num

        from hygf_jp_inverter    left JOIN hygf_jp_station

        on hygf_jp_station.third_station_id=hygf_jp_inverter.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_inverter.`state` is not null and hygf_jp_inverter.`state` in ("在线","报警","离线")
        </where>
        GROUP BY hygf_jp_inverter.`state`
    </select>


    <select id="selectPageData" resultType="com.yeejoin.amos.boot.module.hygf.api.dto.JpInverterDtoNew">
        select
        hjc.sequence_nbr,
        hjc.rec_date,
        hjc.rec_user_id,
        hjc.rec_user_name,
        hjc.sn_code,
        hjc.state,
        hjc.rated_power,
        hjc.collector_id,
        hjc.collector_sn_code,
        hjc.update_time,
        hjc.current_power,
        ROUND(hjc.day_power_generation,3) as dayPowerGeneration,
        ROUND(hjc.month_power_generation,3) as monthPowerGeneration,
        ROUND(hjc.year_power_generation,3) as yearPowerGeneration,
        hjc.brand,
        hjc.model,
        hjc.national_standard,
        hjc.version,
        hjc.generation_hours,
        hjc.warranty_period,
        hjc.IGBT_temperature,
        hjc.AFCI_programme,
        hjc.AFCI_version,
        hjc.ID,
        hjc.capacity,
        ROUND(hjc.total_power_generation/1000,3) as totalPowerGeneration,
        hjc.third_station_id,
        hjc.is_delete,
        hjc.third_code,
        hjc.station_name,
        hjc.addr,
        hjc.sn_name as snName,
        sta.type as stationType,
        (select COMPANY_NAME from privilege_company where ORG_CODE = sta.regional_companies_code  ) as companyName
        from
        hygf_jp_inverter  hjc
        left join hygf_jp_station sta on sta.third_station_id = hjc.third_station_id
      <where>
            <if test="dto.snCode != null and dto.snCode != ''">
                AND  hjc.sn_code like #{dto.snCode}
            </if>
          <if test="dto.state != null and dto.state != ''">
              AND  hjc.state = #{dto.state}
          </if>
            <if test="dto.minValue != null and dto.minValue !=''">
                AND hjc.capacity >= #{dto.minValue}
            </if>
            <if test="dto.maxValue != null and dto.maxValue !=''">
                AND  hjc.capacity <![CDATA[<=]]> #{dto.maxValue}
            </if>
        </where>
        limit #{current},#{size}
    </select>

    <select id="selectPageDataTotal" resultType="int">
        select
      count(1)
        from
        hygf_jp_inverter  hjc
        left join hygf_jp_station sta on sta.third_station_id = hjc.third_station_id

        <where>
            <if test="dto.snCode != null and dto.snCode != ''">
                AND  hjc.sn_code like #{dto.snCode}
            </if>
            <if test="dto.state != null and dto.state != ''">
                AND  hjc.state = #{dto.state}
            </if>
            <if test="dto.minValue != null and dto.minValue !=''">
                AND  hjc. capacity >= #{dto.minValue}
            </if>
            <if test="dto.maxValue != null and dto.maxValue !=''">
                AND  hjc.capacity <![CDATA[<=]]> #{dto.maxValue}
            </if>
        </where>
    </select>
    <select id="selectInverterState" resultType="java.util.Map">
        SELECT
            all_statuses.status,
            COALESCE(COUNT(hygf_jp_inverter.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_inverter ON all_statuses.status = hygf_jp_inverter.state and hygf_jp_inverter.third_station_id
        in(
        <foreach collection="thirdStationIds" item="item" separator=",">
            (#{item})
        </foreach>

    )
        GROUP BY all_statuses.status
        ORDER BY all_statuses.status;

    </select>
</mapper>
