<?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.DeviceMapper">


    <select id="getDeviceBySubId" resultType="com.njcn.device.pq.pojo.po.Device">
        select * from pq_line a
        inner join pq_device b on a.id=b.id
        where a.pid = #{subId}
        and a.state = 1
        and b.ip = #{ip}
        and b.port = #{port}
        <if test="devId !=null and devId !=''">
           and a.id != #{devId}
        </if>

    </select>

    <select id="getRunManageList" resultType="com.njcn.device.pq.pojo.vo.RunManageVO">
        SELECT
                linedetail.Num AS id,
                line.NAME AS lineName,
                area.NAME AS areaName,
                gd.NAME AS gdName,
                sub.NAME AS bdName,
                scaleId.Name as scale,
                manufacturerId.name as manufacturer,
                dev.name as devName,
                device.IP as ip,
                case device.Run_Flag when 0 then "投运"
                when 1 then "热备用"
                when 2 then "停运"
                end as runFlag,
                case device.Com_Flag when 0 then "中断"
                when 1 then "正常"
                end as comFlag,
                loadtypeId.Name as loadType,
                businesstypeId.name as businessType,
                IFNULL(linedetail.Obj_Name,"/") as objName,
                case linedetail.PT_Type when 0 then "星型接线"
                when 1 then "三角型接线"
                when 2 then "开口三角型接线"
                end as ptType,
                CONCAT(linedetail.PT1,"/", linedetail.PT2) as pt,
                CONCAT(linedetail.CT1,"/", linedetail.CT2) as ct,
                linedetail.Standard_Capacity as standardCapacity,
                linedetail.Short_Capacity as shortCapacity,
                linedetail.Dev_Capacity as devCapacity,
                linedetail.Deal_Capacity as dealCapacity,
                over.Freq_Dev as freqDev,
                over.Voltage_Dev as voltageDev,
                over.Uvoltage_Dev as uvoltageDev,
                over.Ubalance as ubalance,
                over.I_Neg as iNeg,
                over.Flicker as flicker,
                over.Uaberrance as uaberrance,
                over.Uharm_3 as oddHarm,
                over.Uharm_2 as evenHarm,
                over.Iharm_2 as iharm2,
                over.Iharm_3 as iharm3,
                over.Iharm_4 as iharm4,
                over.Iharm_5 as iharm5,
                over.Iharm_6 as iharm6,
                over.Iharm_7 as iharm7,
                over.Iharm_8 as iharm8,
                over.Iharm_9 as iharm9,
                over.Iharm_10 as iharm10,
                over.Iharm_11 as iharm11,
                over.Iharm_12 as iharm12,
                over.Iharm_13 as iharm13,
                over.Iharm_14 as iharm14,
                over.Iharm_15 as iharm15,
                over.Iharm_16 as iharm16,
                over.Iharm_17 as iharm17,
                over.Iharm_18 as iharm18,
                over.Iharm_19 as iharm19,
                over.Iharm_20 as iharm20,
                over.Iharm_21 as iharm21,
                over.Iharm_22 as iharm22,
                over.Iharm_23 as iharm23,
                over.Iharm_24 as iharm24,
                over.Iharm_25 as iharm25,
                over.InUharm_1 as inUharm,
                over.InUharm_16 as inUharm16
        FROM    pq_line line
        INNER JOIN pq_line vol ON vol.Id = line.Pid
        INNER JOIN pq_line dev ON dev.Id = vol.Pid
        INNER JOIN pq_line sub ON sub.Id = dev.Pid
        INNER JOIN pq_line gd ON gd.Id = sub.Pid
        INNER JOIN pq_line areaId ON areaId.Id = gd.Pid
        INNER JOIN sys_area area ON area.Id = areaId.NAME
        INNER JOIN pq_overlimit over ON over.Id = line.Id
        INNER JOIN pq_voltage voltage ON voltage.Id = vol.Id
        INNER JOIN sys_dict_data scaleId ON scaleId.Id = voltage.Scale
        INNER JOIN pq_device device ON device.Id = dev.Id
        INNER JOIN sys_dict_data manufacturerId ON manufacturerId.Id = device.Manufacturer
        INNER JOIN pq_line_detail linedetail ON linedetail.Id = line.Id
        INNER JOIN sys_dict_data loadtypeId ON loadtypeId.Id = linedetail.Load_Type
        INNER JOIN sys_dict_data businesstypeId ON businesstypeId.Id = linedetail.Business_Type
        WHERE line.Id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
        <if test="comFlag.size()!=0">
            and device.Com_Flag in
            <foreach item="item2" collection="comFlag" open="(" separator="," close=")">
                #{item2}
            </foreach>
        </if>
        <if test="searchValue != '' and searchValue != null ">
            <bind name="searchValueLike" value="'%'+searchValue+'%'"/>
            AND sub.NAME LIKE #{searchValueLike}
            OR dev.name LIKE #{searchValueLike}
            OR line.NAME LIKE #{searchValueLike}
        </if>
        ORDER BY
            gdName,
            bdName,
            devNAME,
            objName;
    </select>

    <select id="getRunManageDevList" resultType="com.njcn.device.pq.pojo.vo.RunTimeVO">
        SELECT
        dev.Id AS id,
        area.NAME AS areaName,
        gd.NAME AS gdName,
        sub.NAME AS bdName,
        manufacturerId.NAME AS manufacturer,
        dev.NAME AS devName,
        device.IP AS ip,
        device.Login_Time AS loginTime,
        devT.Name AS devType,
        device.PORT AS PORT,
        CASE
        device.Run_Flag
        WHEN 0 THEN
        "投运"
        WHEN 1 THEN
        "热备用"
        WHEN 2 THEN
        "停运"
        END AS runFlag,
        CASE
        device.Com_Flag
        WHEN 0 THEN
        "中断"
        WHEN 1 THEN
        "正常"
        END AS comFlag,
        device.Update_Time AS updateTime
        FROM
        pq_line dev
        INNER JOIN pq_line sub ON sub.Id = dev.Pid
        INNER JOIN pq_line gd ON gd.Id = sub.Pid
        INNER JOIN pq_line areaId ON areaId.Id = gd.Pid
        INNER JOIN sys_area area ON area.Id = areaId.NAME
        INNER JOIN pq_device device ON device.Id = dev.Id
        INNER JOIN sys_dict_data manufacturerId ON manufacturerId.Id = device.Manufacturer
        INNER JOIN sys_dict_data devT ON devT.Id = device.Dev_Type
        WHERE dev.Id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
        <if test="runFlag.size()!=0">
            and device.Run_Flag in
            <foreach item="item1" collection="runFlag" separator="," open="("  close=")">
                #{item1}
            </foreach>
        </if>
        <if test="comFlag.size()!=0">
            and device.Com_Flag in
            <foreach item="item2" collection="comFlag" separator="," open="("  close=")">
                #{item2}
            </foreach>
        </if>
        <if test="manufacturers.size() != 0">
            and device.Manufacturer in
            <foreach collection="manufacturers" item="item3" separator="," open="("  close=")">
                #{item3}
            </foreach>
        </if>
        <if test="searchValue != '' and searchValue != null ">
            <bind name="searchValueLike" value="'%'+searchValue+'%'"/>
            AND sub.NAME LIKE #{searchValueLike}
            OR dev.NAME LIKE #{searchValueLike}
            OR devT.Name LIKE #{searchValueLike}
            OR device.IP LIKE #{searchValueLike}
        </if>
        ORDER BY
            gdName,
            bdName,
            devName;
    </select>
    <select id="getOnlineEvaluate" resultType="com.njcn.device.pq.pojo.vo.LineInfluxDbOnlineVO">
        SELECT
            SUM(r.online_min) / (SUM(r.online_min)+SUM(r.offline_min)) AS onlineRate,
            r.dev_index AS devIndex
        FROM r_stat_onlinerate_d r
        <where>
            <if test=" begin != null and begin !=''">
                AND  time_id >= #{begin}
            </if>
            <if test="end != null and end != ''">
                AND  time_id &lt;= #{end}
            </if>
            <if test="list.size > 0">
                AND r.dev_index IN
                <foreach collection="list" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>

        GROUP BY r.dev_index
    </select>
    <select id="selectDevByIds" resultType="com.njcn.device.pq.pojo.vo.DevDetail">
        SELECT DISTINCT
            device.id as devIndex,
            gd.NAME as gdName,
            substation.NAME as bdzName,
            device.NAME as devName,
            lineDetail.Line_Grade AS lineGrade,
            deviceDetail.Update_Time AS timeID,
            deviceDetail.ip as ip,
            deviceDetail.Com_Flag as comFlag
        FROM
            pq_line line,
            pq_line voltage,
            pq_line device,
            pq_line substation,
            pq_line gd,
            pq_device deviceDetail,
            pq_line_detail lineDetail
        <where>
            <if test="ids!=null and ids.size()>0">
                device.id IN
                <foreach collection="ids" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            AND line.pid = voltage.id
            AND voltage.pid = device.id
            AND device.pid = substation.id
            AND substation.pid = gd.id
            AND device.id = deviceDetail.id
            AND line.id = lineDetail.id
        </where>
    </select>
</mapper>
