<?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.njcn.device.pq.mapper.TerminalOnlineRateDataMapper">

    <!--通过终端等级获取终端信息-->
    <select id="getDeviceLevelData" resultType="com.njcn.device.pq.pojo.vo.TerminalOnlineRateDataVO">
        SELECT
        device.id,
        min( sdd.Algo_Describe ) AS algoDescribe
        FROM
        pq_line AS device
        INNER JOIN pq_line AS voltage ON device.id = voltage.pid
        INNER JOIN pq_line AS line ON voltage.id = line.pid
        INNER JOIN pq_line_detail AS pld ON line.id = pld.id
        LEFT JOIN sys_dict_data AS sdd ON pld.Line_Grade = sdd.id
        WHERE
        device.id IN
        <foreach collection="deviceIndexes" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        GROUP BY
        device.id,
        device.NAME,
        sdd.Algo_Describe
    </select>

    <!--通过部分终端等级信息查询终端信息-->
    <select id="getDevice" resultType="com.njcn.device.pq.pojo.vo.TerminalOnlineRateDataVO">
        SELECT
        device.id,
        device.pid,
        device.`Name`,
        device.`Name` AS dataName,
        device.`Level`,
        device.Sort,
        pdevice.ip,
        pdevice.Com_Flag AS comFlag,
        sddManufacturer.`Name` AS manufacturer,
        pdevice.Update_Time AS updateTime,
        min(sddLineGrade.Algo_Describe) as algoDescribe
        FROM
        pq_line AS device
        LEFT JOIN pq_line AS voltage ON device.id = voltage.pid
        LEFT JOIN pq_line AS line ON voltage.id = line.pid
        LEFT JOIN pq_line_detail AS pld ON line.id = pld.id
        LEFT JOIN sys_dict_data AS sddLineGrade ON pld.Line_Grade = sddLineGrade.id
        LEFT JOIN pq_device AS pdevice ON device.id = pdevice.id
        LEFT JOIN sys_dict_data AS sddManufacturer ON pdevice.Manufacturer = sddManufacturer.id
        WHERE
        device.`Level` = 4
        AND device.id in
        <foreach collection="devIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        <if test="lineGrade!='' and lineGrade!=null">
            AND pld.line_grade = #{lineGrade}
        </if>
        group by
        device.id,
        device.pid,
        device.`Name`,
        device.`Level`,
        device.Sort,
        pdevice.ip,
        pdevice.Com_Flag,
        sddManufacturer.`Name`,
        pdevice.Update_Time

    </select>

    <!--根据供电公司索引获取出省会的信息-->
    <select id="getProvinceList" resultType="com.njcn.device.pq.pojo.vo.TerminalOnlineRateDataVO">
        SELECT
        DISTINCT(pro.id),
        pro.pid,
        pro.level,
        pro.sort,
        area.name name
        FROM
        pq_line pro,
        pq_line gd,
        sys_area area
        WHERE
        pro.id = gd.pid
        AND
        pro.name = area.id
        AND
        gd.id in
        <foreach collection="gdIndex" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <!--获取出供电公司的信息-->
    <select id="getGdList" resultType="com.njcn.device.pq.pojo.vo.TerminalOnlineRateDataVO">
        SELECT gd.id,
        gd.pid,
        gd.LEVEL,
        gd.sort,
        gd.Name
        FROM pq_line gd
        WHERE
        gd.id in
        <foreach collection="gdIndex" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <!--获取出变电站的信息-->
    <select id="getSubList" resultType="com.njcn.device.pq.pojo.vo.TerminalOnlineRateDataVO">
        SELECT
        sub.id,
        sub.pid,
        sub.`Level`,
        sub.sort,
        sub.`Name`,
        sdd.`Name` AS subScale
        FROM
        pq_line sub
        LEFT JOIN pq_substation AS psub ON sub.id = psub.id
        LEFT JOIN sys_dict_data AS sdd ON psub.Scale = sdd.id
        WHERE
        sub.id in
        <foreach collection="subIndex" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <!--根据监测点索引集合获取监测点详细参数-->
    <select id="getDetailedParametersForLine" resultType="com.njcn.device.pq.pojo.vo.TerminalOnlineRateDataVO">
        SELECT
        line.Id as 'id',
        line.pid as 'pid',
        line.Pids as 'pids',
        line.`Name` AS 'name',
        line.`Level`as 'Level',
        line.Update_Time AS 'updateTime',
        line.Sort as 'sort',
        pd.Call_Flag as 'ComFlag',
        sdd.`Name` AS 'LineScale',
        device.`Name` AS 'deviceName',
        pd.IP AS 'ip',
        msdd.`Name` AS 'manufacturer',
        pldsdd.`Name` AS 'lineGrade',
        pld.Load_Type AS 'loadType'
        FROM
        pq_line AS line
        LEFT JOIN pq_line AS voltage ON line.Pid = voltage.Id
        LEFT JOIN pq_line AS device ON voltage.pid = device.id
        LEFT JOIN pq_voltage AS pv ON voltage.id = pv.id
        LEFT JOIN pq_device AS pd ON device.id = pd.id
        LEFT JOIN sys_dict_data AS sdd ON sdd.id = pv.Scale
        LEFT JOIN pq_line_detail AS pld ON line.id = pld.Id
        LEFT JOIN sys_dict_data AS pldsdd ON pld.Line_Grade = pldsdd.id
        LEFT JOIN sys_dict_data AS msdd ON pd.Manufacturer = msdd.`id`
        WHERE
        line.`Level` = 6
        AND
        line.id in
        <foreach collection="lineIndexes" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        <if test="lineGrade != null and lineGrade != ''">
            and pld.Line_Grade = #{lineGrade}
        </if>
        <if test="searchBeginTime != null and searchBeginTime != ''"><!-- 开始创建时间 -->
            and date_format(line.update_Time,'%y%m%d') &gt;= date_format(#{searchBeginTime},'%y%m%d')
        </if>
        <if test="searchEndTime != null and searchEndTime != ''"><!--  -->
            and date_format(line.update_Time,'%y%m%d') &lt;= date_format(#{searchEndTime},'%y%m%d')
        </if>
    </select>


    <select id="getDevOnlineByDevIds" resultType="TerminalOnlineRateData">
        select
                dev_index devId,
                avg(online_min/(online_min+offline_min))*100 onlineRate,
                sum(online_min) onlineMin,
                sum(offline_min) offlineMin
        from r_stat_onlinerate_d
        where dev_index in
        <foreach collection="devIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        and time_id between #{startTime} and #{endTime}
        group by dev_index
    </select>
</mapper>
