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


    <select id="getDeviceBySubId" resultType="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 in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <select id="getDeviceBySubIdForUpdate" resultType="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 in
        <foreach collection="ipList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        and a.id not in
        <foreach collection="devList" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>


    <select id="getLineDetailByDeviceId" resultType="com.njcn.device.pq.pojo.po.LineDetail">
        select c.id,
               c.num,
               c.pt1,
               c.pt2,
               c.ct1,
               c.ct2,
               c.Dev_Capacity      devCapacity,
               c.short_capacity    shortCapacity,
               c.standard_capacity standardCapacity,
               c.deal_capacity     dealCapacity
        from pq_line a
                 inner join pq_line b on a.id = b.pid
                 inner join pq_line_detail c on b.id = c.id
        where a.pid = #{devId}
          and a.state = 1
    </select>

    <select id="getVoltageByDevId" resultType="Voltage">
        select a.id,a.name,b.num
        from pq_line a
                 inner join pq_voltage b on a.id = b.id
        where a.pid = #{devId}
          and a.state = 1
    </select>


    <select id="getVoltageByLineId" resultType="Voltage">
        select *
        from pq_line a
                 inner join pq_voltage b on a.pid = b.id
        where a.id = #{lineId}
          and a.state = 1
    </select>

    <select id="getAllList" resultType="TerminalTree">
        select id, pid, name, level, sort
        from pq_line
        where state = 1
    </select>

    <select id="getProvinceList" resultType="TerminalTree">
        select a.id,a.pid,b.name,a.level,a.sort from pq_line a
        inner join sys_area b on a.name = b.id
        where a.level = 1
        <if test="type == 1 and type !=null">
            and a.id in
            <foreach collection="pids" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        and a.state = 1
        order by a.sort
    </select>

    <select id="getInfoByIdList" resultType="TerminalTree">
        select id,pid,name,level,sort from pq_line
        where id in
        <foreach collection="ids" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        and state = 1
        order by sort
    </select>

    <select id="getInfoByPidList" resultType="TerminalTree">
        select a.id,a.pid,a.name,a.level,a.sort from pq_line
        where pid in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        and a.state = 1
        order by a.sort
    </select>

    <select id="getAllDeviceList" resultType="TerminalTree">
        select a.id,a.pid,b.com_flag comFlag,a.level,a.sort from pq_line a
        inner join pq_device b on a.id = b.id
        where a.id in
        <foreach collection="ids" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        and a.level = 4
        and a.state = 1
        order by a.sort
    </select>

    <select id="getAllVolList" resultType="TerminalTree">
        select a.id,a.pid,a.name,c.name scale,a.level,a.sort from pq_line a
        inner join pq_voltage b on a.id = b.id
        inner join sys_dict_data c on b.scale = c.id
        where a.id in
        <foreach collection="ids" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        and a.level = 5
        and a.state = 1
        order by a.sort
    </select>


    <select id="getAllLineList" resultType="TerminalTree">
        select a.id,a.pid,a.name,a.level,a.sort,b.pt_type
        from pq_line a
        inner join pq_line_detail b on a.id = b.id
        where a.id in
        <foreach collection="ids" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        and a.state = 1
        order by sort
    </select>

    <!--华丽分割线   ++++                          -->




    <select id="getUnBindList" resultType="TerminalTree">
        select id, pid, name, level, sort
        from pq_line
        where state = 1
          and id not in (select Line_Id from pq_dept_line)
        order by sort
    </select>

    <select id="getBindList" resultType="TerminalTree">
        select id, pid, name, level, sort
        from pq_line
        where state = 1
          and id in (select Line_Id from pq_dept_line)
        order by sort
    </select>

    <select id="selectProject" resultType="String">
        select pid
        from pq_line
        where name = #{areaId}
    </select>

    <select id="getProviceName" resultType="String">
        select name
        from sys_area
        where id = #{areaId}
    </select>

    <select id="getDeviceById" resultType="Line">
        SELECT
        t1.*
        FROM
        pq_line t1,
        pq_device t2
        WHERE
        t1.id = t2.id
        <if test="deviceType.devModel!=null and deviceType.devModel.size()!=0">
            AND t2.Dev_Model in
            <foreach collection="deviceType.devModel" open="(" close=")" item="item" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="deviceType.runFlag!=null and deviceType.runFlag.size()!=0">
            AND t2.Run_Flag in
            <foreach collection="deviceType.runFlag" open="(" close=")" item="item" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="deviceType.dataType!=null and deviceType.dataType.size()!=0">
            AND t2.Dev_Data_Type in
            <foreach collection="deviceType.dataType" open="(" close=")" item="item" separator=",">
                #{item}
            </foreach>
        </if>
        AND t1.id IN
        <foreach collection="devIds" open="(" close=")" item="item" separator=",">
            #{item}
        </foreach>
    </select>


    <select id="getDeviceByIdOnOrOff" resultType="com.njcn.device.pq.pojo.vo.TerminalBaseVO">
        SELECT
        t1.id devId,t1.name devName,t2.ip,t2.dev_type,t2.update_time,sub.name subName,gd.name gdName
        FROM
        pq_line t1,
        pq_device t2,
        pq_line sub,
        pq_line gd
        WHERE
        t1.id = t2.id
        and t1.pid = sub.id
        and sub.pid = gd.id
        <if test="deviceType.devModel!=null  and deviceType.devModel.size()!=0">
            AND t2.Dev_Model in
            <foreach collection="deviceType.devModel" open="(" close=")" item="item" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="deviceType.runFlag!=null  and deviceType.runFlag.size()!=0">
            AND t2.Run_Flag in
            <foreach collection="deviceType.runFlag" open="(" close=")" item="item" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="deviceType.dataType!=null  and deviceType.dataType.size()!=0">
            AND t2.Dev_Data_Type in
            <foreach collection="deviceType.dataType" open="(" close=")" item="item" separator=",">
                #{item}
            </foreach>
        </if>
        AND t1.id IN
        <foreach collection="devIds" open="(" close=")" item="item" separator=",">
            #{item}
        </foreach>
        <if test="comFlag!=null">
            and t2.com_flag = #{comFlag}
        </if>
        order by update_time desc

    </select>


    <select id="getLineDetail" resultType="com.njcn.web.pojo.vo.LineDataVO">
        SELECT line.*
        FROM PQ_LINE line
        WHERE line.id IN
        <foreach collection="ids" open="(" close=")" item="item" separator=",">
            #{item}
        </foreach>
        order by line.level asc
    </select>

    <select id="getLineByCondition" resultType="Line">
        select
        t1.*
        from
        pq_line t1 ,
        pq_line_detail t2
        where
        t1.id = t2.id
        and
        t1.id in
        <foreach collection="ids" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
        <if test="deviceInfoParam.monitorFlag!=2 and deviceInfoParam.monitorFlag!=null">
            and t2.Monitor_Flag = #{deviceInfoParam.monitorFlag}
        </if>
        <if test="deviceInfoParam.powerFlag!=2 and deviceInfoParam.powerFlag!=null ">
            and t2.Power_Flag = #{deviceInfoParam.powerFlag}
        </if>
        <if test="deviceInfoParam.lineGrade!='' and deviceInfoParam.lineGrade!=null">
            and t2.line_grade = #{deviceInfoParam.lineGrade}
        </if>
        <if test="deviceInfoParam.loadType!=null  and deviceInfoParam.loadType.size()!=0">
            AND t2.Load_Type in
            <foreach collection="deviceInfoParam.loadType" open="(" close=")" item="item" separator=",">
                #{item.id}
            </foreach>
        </if>
        <!-- xy -->
        <choose>
            <when test="deviceInfoParam.statFlag">
                AND t2.Stat_Flag = 1
            </when>
        </choose>
        <!-- -->
    </select>

    <select id="getDeviceByCondition" resultType="Line">
        SELECT
        t1.*
        FROM
        pq_line t1,
        pq_device t2
        WHERE
        t1.id = t2.id
        <if test="deviceType.devModel!=null and deviceType.devModel.size()!=0">
            AND t2.Dev_Model in
            <foreach collection="deviceType.devModel" open="(" close=")" item="item" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="deviceType.runFlag!=null and deviceType.runFlag.size()!=0">
            AND t2.Run_Flag in
            <foreach collection="deviceType.runFlag" open="(" close=")" item="item" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="deviceType.dataType!=null and deviceType.dataType.size()!=0">
            AND t2.Dev_Data_Type in
            <foreach collection="deviceType.dataType" open="(" close=")" item="item" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="manufacturer!=null and manufacturer.size()!=0">
            AND t2.manufacturer in
            <foreach collection="manufacturer" open="(" close=")" item="item" separator=",">
                #{item.id}
            </foreach>
        </if>
        AND t1.id IN
        <foreach collection="devIds" open="(" close=")" item="item" separator=",">
            #{item}
        </foreach>
    </select>


    <select id="getVoltageByCondition" resultType="Line">
        select
        t1.*
        from
        pq_line t1 ,
        pq_voltage t2
        where
        t1.id = t2.id
        and
        t1.id in
        <foreach collection="voltageIds" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
        <if test="scale!=null and scale.size()!=0">
            AND t2.scale in
            <foreach collection="scale" open="(" close=")" item="item" separator=",">
                #{item.id}
            </foreach>
        </if>
    </select>

    <select id="getVoltageIdByScale" resultType="String">
        select
        t1.id
        from
        pq_line t1 ,
        pq_voltage t2
        where
        t1.id = t2.id
        and
        t1.id in
        <foreach collection="voltageIds" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
        AND
        t2.scale = #{scale}
    </select>

    <select id="getLineIdByLoadType" resultType="String">
        select
        t1.id
        from
        pq_line t1 ,
        pq_line_detail t2
        where
        t1.id = t2.id
        and
        t1.id in
        <foreach collection="lineIds" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
        AND
        t2.Load_Type = #{loadType}
    </select>

    <select id="getDeviceIdByManufacturer" resultType="String">
        select
        t1.id
        from
        pq_line t1 ,
        pq_device t2
        where
        t1.id = t2.id
        and
        t1.id in
        <foreach collection="deviceIds" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
        AND
        t2.Manufacturer = #{manufacturer}
    </select>


    <select id="getAllLineOverLimit" resultType="com.njcn.device.biz.pojo.po.Overlimit">
        select f.* from pq_line a
        inner join pq_line_detail b on a.id = b.id
        inner join pq_line c on a.pid = c.id
        inner join pq_line d on c.pid = d.id
        inner join pq_device e on d.id = e.id
        inner join pq_overlimit f on a.id=f.id
        where e.dev_data_type in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        and e.run_flag = 0
        and e.dev_model = 1
    </select>

    <select id="getBaseLineInfo" resultType="BaseLineInfo">
        SELECT
        line.Id,
        area.`Name` provincialName,
        gd.`Name` gdName,
        substation.`Name` subName,
        subscale.`Name` subScale,
        device.`Name` deviceName,
        pqdevice.IP networkParam,
        pqdevice.Com_Flag comState,
        factory.`Name` factoryName,
        pqdevice.Update_Time time,
        voltage.`Name` subvName,
        scale.`Name` subvScale,
        line.`Name` lineName,
        line.Sort
        FROM
        pq_line line,
        pq_line voltage,
        pq_voltage pqvoltage,
        pq_line device,
        pq_device pqdevice,
        pq_line substation,
        pq_substation pqsubstation,
        pq_line gd,
        pq_line provincial,
        sys_dict_data scale,
        sys_dict_data factory,
        sys_dict_data loadtype,
        sys_dict_data subscale,
        sys_area area
        WHERE
        line.pid = voltage.id
        AND voltage.id = pqvoltage.id
        AND pqvoltage.Scale = scale.id
        AND voltage.pid = device.id
        AND voltage.pid = pqdevice.id
        AND pqdevice.Manufacturer = factory.id
        AND device.pid = substation.id
        AND substation.id = pqsubstation.id
        AND pqsubstation.Scale = subscale.id
        AND substation.pid = gd.id
        AND gd.pid = provincial.id
        AND provincial.NAME = area.id
        AND line.Id IN
        <foreach item="item" collection="list" separator="," open="(" close=")">
            #{item}
        </foreach>
        GROUP BY line.id
    </select>


    <select id="getBaseLineAreaInfo" resultType="AreaLineInfoVO">
        SELECT
        line.Id lineId,
        line.`Name` lineName,
        gd.`Name` gdName,
        substation.id subId,
        substation.`Name` subName,
        subscale.name subScale,
        pqdevice.IP ip,
        factory.`Name` manufacturer,
        voltage.id voltageId,
        voltage.`Name` voltageName,
        scale.`Name` voltageScale,
        pqdevice.run_flag,
        pqdevice.com_flag,
        pqsubstation.Lng,
        pqsubstation.lat,
        detail.num,
        detail.ct1,
        detail.ct2,
        detail.pt1,
        detail.pt2,
        detail.obj_name,
        detail.Dev_Capacity deviceCapacity,
        detail.Short_Capacity,
        detail.Standard_Capacity,
        detail.Deal_Capacity,
        detail.Business_Type,
        detail.Calssification_Grade,
        detail.Superiors_Substation,
        detail.Hang_Line,
        loadtype.name loadType
        FROM
        pq_line line,
        pq_line_detail detail,
        pq_line voltage,
        pq_voltage pqvoltage,
        pq_line device,
        pq_device pqdevice,
        pq_line substation,
        pq_substation pqsubstation,
        pq_line gd,
        sys_dict_data scale,
        sys_dict_data factory,
        sys_dict_data loadtype,
        sys_dict_data subscale
        WHERE
        line.id = detail.id
        AND line.pid = voltage.id
        AND voltage.id = pqvoltage.id
        AND pqvoltage.Scale = scale.id
        AND voltage.pid = device.id
        AND voltage.pid = pqdevice.id
        AND pqdevice.Manufacturer = factory.id
        AND device.pid = substation.id
        AND substation.id = pqsubstation.id
        AND pqsubstation.scale = subscale.id
        AND substation.pid = gd.id
        AND detail.load_type = loadtype.id
        AND line.Id IN
        <foreach item="item" collection="list" separator="," open="(" close=")">
            #{item}
        </foreach>
        <if test="searchValue!=null and searchValue!=''">
            and (line.name like  CONCAT('%', #{searchValue},'%')
            or voltage.name like  CONCAT('%', #{searchValue},'%')
            or device.name like  CONCAT('%', #{searchValue},'%')
            or substation.name like  CONCAT('%', #{searchValue},'%')
            or gd.name like  CONCAT('%', #{searchValue},'%'))
        </if>
        <if test="comFlag!=null">
            and pqdevice.com_flag = #{comFlag}
        </if>

    </select>

    <select id="getLineDeviceStateVO" resultType="com.njcn.device.pq.pojo.vo.LineDeviceStateVO">
        SELECT
            t.id id,
            t.pids pids,
            t.NAME name,
            sub.Name subName,
            device.Com_Flag state
        FROM
            pq_line t,
            pq_line subv,
            pq_line device1,
            pq_device device,
            pq_line sub
        WHERE
            subv.id = t.pid
            AND device.id = subv.pid
            AND device1.id = device.id
            AND sub.id = device1.pid
        AND t.id in
        <foreach item="item" collection="list" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="getSubstationInfo" resultType="PollutionSubstationDTO">
        SELECT A.id,
               A.`Name` name,
               C.`Name` voltageLevel
        FROM pq_line A,
             pq_substation B,
             sys_dict_data C
        WHERE A.Id = #{id}
          AND A.Id = B.Id
          AND B.Scale = C.Id
    </select>

    <select id="getLineInfo" resultType="PollutionLineDTO">
        SELECT
        A.Id,
        A.`Name`,
        A1.Name busBar,
        A3.Name substation,
        A3.Id substationId,
        A4.Name powerCompany
        FROM
        pq_line A,
        pq_line A1,
        pq_line A2,
        pq_line A3,
        pq_line A4
        WHERE
        A.Id IN
        <foreach item="item" collection="list" separator="," open="(" close=")">
            #{item}
        </foreach>
        AND A.Pid = A1.Id
        AND A.State = 1
        AND A1.Pid = A2.Id
        AND A2.Pid = A3.Id
        AND A3.Pid = A4.Id
    </select>
    <select id="getLineSubGdDetail" resultType="com.njcn.device.pq.pojo.vo.LineDetailVO">
        SELECT DISTINCT gd.NAME         gdName,
                        substation.NAME subName,
                        device.NAME devName,
                        deviceDetail.ip ip,
                        line.NAME       lineName
        FROM pq_line line,
             pq_line voltage,
             pq_line device,
             pq_device deviceDetail,
             pq_line substation,
             pq_line gd
        WHERE 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 = #{id}
    </select>

    <select id="getWarningSub" resultType="WarningSubstationDTO">
        SELECT
        A1.Id,
        A1.`Name` subName,
        B.`Name` scale,
        A2.`Name` city,
        D.`Name` province
        FROM
        pq_line A1,
        pq_line A2,
        pq_line A3,
        sys_dict_data B,
        pq_substation C,
        sys_area D
        WHERE
        A1.Id in
        <foreach item="item" collection="list" separator="," open="(" close=")">
            #{item}
        </foreach>
        AND A1.Pid = A2.Id AND A2.Pid = A3.Id AND A3.`Name` = D.Area_Code AND A1.Id = C.Id AND C.Scale = B.Id
    </select>

    <select id="getOverLimitLineInfo" resultType="OverLimitLineDTO">
        SELECT
        A1.id,
        A1.`Name` lineName,
        C1.`Name` lineScale,
        C2.`Name` loadType,
        D.Obj_Name lineObjectName,
        D.Monitor_Id monitorNumber,
        A4.`Name` subName,
        C3.`Name` subScale,
        A5.`Name` cityCompany,
        F.`Name` provinceCompany
        FROM
        pq_line A1,
        pq_line A2,
        pq_line A3,
        pq_line A4,
        pq_line A5,
        pq_line A6,
        pq_voltage B,
        sys_dict_data C1,
        sys_dict_data C2,
        sys_dict_data C3,
        pq_line_detail D,
        pq_substation E,
        sys_area F
        WHERE
        A1.id in
        <foreach item="item" collection="list" separator="," open="(" close=")">
            #{item}
        </foreach>
        AND A1.Pid = A2.Id AND A2.Id = B.Id AND B.Scale = C1.Id AND A1.Id = D.Id AND D.Load_Type = C2.Id
        AND A2.Pid = A3.Id AND A3.Pid = A4.Id AND A4.Pid = A5.Id AND A4.Id = E.Id AND E.Scale = C3.Id
        AND A5.Pid = A6.Id AND A6.`Name` = F.Area_Code
    </select>

    <select id="getDeviceStatus" resultType="LineStateVO">
        SELECT
        count(IF( device.Run_Flag = 0, IF ( device.Com_Flag = 1, 1, NULL ), NULL )) normalStateNum,
        count(IF( device.Run_Flag = 0, IF ( device.Com_Flag = 0, 1, NULL ), NULL )) suspendStateNum,
        count( device.Run_Flag = 1 OR NULL ) hotSpareStateNum,
        count( device.Run_Flag = 2 OR NULL ) offStreamStateNum
        FROM
        pq_device device
        WHERE
        device.id IN
        <foreach item="item" collection="list" separator="," open="(" close=")">
            #{item}
        </foreach>

    </select>

    <select id="getLineStatisticsDetail" resultType="LineStateVO">
        SELECT
        count(IF( device.Run_Flag = 0, IF ( device.Com_Flag = 1, 1, NULL ), NULL )) normalStateNum,
        count(IF( device.Run_Flag = 0, IF ( device.Com_Flag = 0, 1, NULL ), NULL )) suspendStateNum,
        count( device.Run_Flag = 1 OR NULL ) hotSpareStateNum,
        count( device.Run_Flag = 2 OR NULL ) offStreamStateNum
        FROM
        pq_line line
        INNER JOIN pq_line voltage ON voltage.id = line.pid
        INNER JOIN pq_device device ON device.id = voltage.pid
        WHERE
        line.id IN
        <foreach item="item" collection="list" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="getLineInfoByTableList" resultType="LineStatisticsTableVO">
        SELECT
        Id,
        Pid,
        Pids,
        `Name`
        FROM
        pq_line
        WHERE
        Id IN
        <foreach item="item" collection="list" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="getLineStatisticsTableData" resultType="LineStatisticsTableVO">
        SELECT
        A.Id,
        A.Pid,
        A.Pids,
        A.`Name` name,
        D.run_flag runFlag,
        D.com_flag comFlag
        FROM
        pq_line A,
        pq_line B,
        pq_device D
        WHERE
        A.Id IN
        <foreach item="item" collection="list" separator="," open="(" close=")">
            #{item}
        </foreach>
        AND A.Pid = B.Id
        AND B.Pid = D.Id
    </select>
    <select id="getDeviceRunStatistics" resultType="com.njcn.device.pq.pojo.vo.LineFlowMealDetailVO">
        select t.*
        from (SELECT a.id,
        a.pid,
        a.pids,
        a.NAME name,
        a.level,
        a.update_Time updateTime,
        b.run_flag runFlag,
        b.com_flag comFlag,
        ifnull(d.flow, (select flow from cld_flow_meal where type = 0 and flag = 1)) + ifnull(d1.flow, 0) flowMeal
        FROM pq_line a
        INNER JOIN pq_device b ON a.id = b.id
        LEFT JOIN cld_dev_meal c ON b.id = c.line_id
        LEFT JOIN cld_flow_meal d ON c.Base_Meal_Id = d.id
        LEFT JOIN cld_flow_meal d1 ON c.Ream_Meal_Id = d1.id
        LEFT JOIN sys_dict_data e ON b.dev_type = e.id
        where a.id in
        <foreach item="item" collection="list" separator="," open="(" close=")">
            #{item}
        </foreach>
        )t
    </select>

    <select id="getLineRunStatistics" resultType="LineFlowMealDetailVO">
        select t.*
        from (SELECT a.id,
        a.pid,
        a.pids,
        a.NAME name,
        a.level,
        a.update_Time updateTime,
        b.run_flag runFlag,
        b.com_flag comFlag,
        b.id deviceId,
        dev.name deviceName,
        sub.name substation,
        elc.name electricPowerCompany,
        ifnull(d.flow, (select flow from cld_flow_meal where type = 0 and flag = 1)) + ifnull(d1.flow, 0) flowMeal,
        ifnull(m.Actual_Value,0) statisValue
        FROM pq_line a
        INNER JOIN pq_line vol ON a.pid = vol.id
        INNER JOIN pq_line dev ON vol.pid = dev.id
        INNER JOIN pq_device b ON vol.pid = b.id
        INNER JOIN pq_line sub ON dev.pid = sub.id
        INNER JOIN pq_line elc ON sub.pid = elc.id
        LEFT JOIN  cld_month_flow  m ON b.id = m.Dev_Id
        LEFT JOIN cld_dev_meal c ON b.id = c.line_id
        LEFT JOIN cld_flow_meal d ON c.Base_Meal_Id = d.id
        LEFT JOIN cld_flow_meal d1 ON c.Ream_Meal_Id = d1.id
        LEFT JOIN sys_dict_data e ON b.dev_type = e.id
        where a.id in
        <foreach item="item" collection="list" separator="," open="(" close=")">
            #{item}
        </foreach>
        and
        a.Create_Time between #{startTime} and #{endTime}
        ) t
    </select>


    <select id="getNewDeviceRunStatistics" resultType="LineFlowMealDetailVO">
        select id,pid,pids,name,level,updateTime,runFlag,comFlag,deviceId,substation,flowMeal,round(statisValue,2) statisValue,statisValue/flowMeal as flowProportion from
        (
        select
        a.id,
        a.pid,
        a.pids,
        a.NAME name,
        a.level,
        b.update_Time updateTime,
        b.run_flag runFlag,
        b.com_flag comFlag,
        b.id deviceId,
        sub.name substation,
        ifnull(d.flow, (select flow from cld_flow_meal where type = 0 and flag = 1)) + ifnull(d1.flow, 0) flowMeal,
        ifnull(m.Actual_Value/1024/1024,0) statisValue
        FROM pq_line a
        INNER JOIN pq_device b ON a.id = b.id
        INNER JOIN pq_line sub ON a.pid = sub.id
        LEFT JOIN cld_dev_meal c ON b.id = c.line_id
        LEFT JOIN cld_flow_meal d ON c.Base_Meal_Id = d.id
        LEFT JOIN cld_flow_meal d1 ON c.Ream_Meal_Id = d1.id
        LEFT JOIN  cld_month_flow m ON b.id = m.Dev_Id and m.time_id between #{startTime} and #{endTime}
        where a.id in
        <foreach item="item" collection="devs" separator="," open="(" close=")">
            #{item}
        </foreach>
        ) t
        order by flowProportion desc


    </select>

    <select id="getNewDeviceFlowStatistics" resultType="LineFlowMealDetailVO">
        SELECT
        ANY_VALUE(t.ID) id,
        ANY_VALUE(t.name) name,
        ANY_VALUE(t.substation) substation,
        ANY_VALUE(t.updateTime) updateTime,
        ANY_VALUE(t.runFlag) runFlag,
        ANY_VALUE(t.comFlag)  comFlag,
        ANY_VALUE(t.flowMeal)  flowMeal,
        SUM(t.statisValue) statisValue,
        ROUND(SUM(t.statisValue)/flowMeal,2) flowProportion
        from (
                SELECT
                    device.id id,
                    device.Name name,
                    sub.Name substation,
                    device.Update_Time updateTime,
                    b.run_flag runFlag,
                    b.com_flag comFlag,
                    ifnull(d.flow, (select flow from cld_flow_meal where type = 0 and flag = 1)) + ifnull(d1.flow, 0) flowMeal,
                    ROUND(ifnull(m.Actual_Value,0)/1024/1024,2) statisValue
                FROM cld_month_flow m
                INNER JOIN pq_line device ON device.id=m.Dev_Id
                INNER JOIN pq_device b ON device.id=b.id
                INNER JOIN pq_line sub ON device.pid = sub.id
                LEFT JOIN cld_dev_meal c ON device.id = c.line_id
                LEFT JOIN cld_flow_meal d ON c.Base_Meal_Id = d.id
                LEFT JOIN cld_flow_meal d1 ON c.Ream_Meal_Id = d1.id
                WHERE m.Dev_Id IN
                <foreach collection="devs" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
                AND
                    device.Update_Time between #{startTime} and #{endTime}
                AND
                    b.Run_Flag != 2
        ) t
        GROUP BY t.id
        ORDER BY flowProportion DESC
    </select>

    <select id="getFlowLineInfoByTableList" resultType="LineFlowMealDetailVO">
        SELECT
        Id,
        Pid,
        Pids,
        `level`,
        `Name`
        FROM
        pq_line
        WHERE
        Id IN
        <foreach item="item" collection="list" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="getFlowLineRunStatistics" resultType="LineFlowMealDetailVO">
        select t.*
        from (SELECT a.id,
        a.pid,
        a.pids,
        a.NAME name,
        a.level,
        a.update_Time updateTime,
        b.run_flag runFlag,
        b.com_flag comFlag,
        ifnull(d.flow, (select flow from cld_flow_meal where type = 0 and flag = 1)) +
        ifnull(d1.flow, 0) flowMeal
        FROM pq_line a
        INNER JOIN pq_device b ON a.id = b.id
        LEFT JOIN cld_dev_meal c ON b.id = c.line_id
        LEFT JOIN cld_flow_meal d ON c.Base_Meal_Id = d.id
        LEFT JOIN cld_flow_meal d1 ON c.Ream_Meal_Id = d1.id
        LEFT JOIN sys_dict_data e ON b.dev_type = e.id
        where a.id in
        <foreach item="item" collection="list" separator="," open="(" close=")">
            #{item}
        </foreach>
        )t
    </select>

    <select id="getLineList" resultType="string">
        SELECT
            A1.Id
        FROM
            pq_line A1,
            pq_line A2,
            pq_line A3,
            pq_line_detail B,
            pq_device C
        WHERE
            A1.`Level` = 6 AND A1.Id = B.Id AND
            A1.Pid = A2.Id AND A2.Pid = A3.Id AND A3.Id = C.Id AND
            C.Dev_Model = 1 AND C.Dev_Data_Type IN (1,2) AND C.Run_Flag = 0
    </select>


    <select id="getTerminalBaseExcel" resultType="TerminalBaseExcel">
        SELECT
            pj.NAME projectName,
            area.NAME provinceName,
            gd.NAME gdName,
            sub.NAME substationName,
            dic.NAME subStationScale,
            su.Lat,
            su.Lng,
            dev.NAME deviceName,
            d.Dev_Model,
            d.Dev_Data_Type,
            d.call_flag,
            d.Run_Flag,
            d.Com_Flag,
            dic6.name Manufacturer,
            dic7.name Dev_Type,
            d.ip,
            d.PORT,
            d.Series,
            d.Dev_Key,
            dic8.name frontType,
            node.NAME nodeName,
            vol.NAME subvName,
            vo.Num subvNum,
            dic2.NAME subvScale,
            vo.Model subvModel,
            line.NAME lineName,
            de.Num lineNum,
            concat( de.PT1, "/", de.PT2 ) AS pt,
            concat( de.CT1, "/", de.CT2 ) AS ct,
            de.Dev_Capacity,
            de.Short_Capacity,
            de.Standard_Capacity,
            de.Deal_Capacity,
            de.PT_Type,
            de.Time_Interval,
            dic4.name LoadType,
            dic5.name BusinessType,
            de.Monitor_Id,
            de.Obj_Name,
            de.power_flag,
            de.Stat_Flag,
            dic3.name lineGrade,
            de.Remark
        FROM
            pq_line line
                INNER JOIN pq_line vol ON line.pid = vol.id
                INNER JOIN pq_line dev ON vol.pid = dev.id
                INNER JOIN pq_line sub ON dev.pid = sub.id
                INNER JOIN pq_line gd ON sub.pid = gd.id
                INNER JOIN pq_line pro ON gd.pid = pro.id
                INNER JOIN pq_line pj ON pro.pid = pj.id
                INNER JOIN pq_line_detail de ON line.id = de.id
                INNER JOIN pq_voltage vo ON vol.id = vo.id
                INNER JOIN pq_device d ON dev.id = d.id
                INNER JOIN pq_substation su ON sub.id = su.id
                LEFT JOIN sys_area area ON pro.NAME = area.id
                LEFT JOIN sys_dict_data dic ON su.Scale = dic.id
                LEFT JOIN sys_dict_data dic2 ON vo.Scale = dic2.id
                left join sys_dict_data dic3 on de.Line_Grade = dic3.id
                left join sys_dict_data dic4 on de.Load_Type = dic4.id
                left join sys_dict_data dic5 on de.Business_Type = dic5.id
                left join sys_dict_data dic6 on d.Manufacturer = dic6.id
                left join sys_dict_data dic7 on d.Dev_Type = dic7.id
                left join sys_dict_data dic8 on d.front_type = dic8.id
                LEFT JOIN pq_node node ON d.Node_Id = node.id
        order by pj.sort,pro.sort,gd.sort,sub.sort,dev.sort,vol.sort,line.sort
    </select>

    <select id="getDevIndex" resultType="String">
        select vo.pid
        from pq_line line,
             pq_line vo
        where vo.id = line.pid
          and line.id = #{lineId}
    </select>

    <select id="getVoltageListBySubId" resultType="Line">
        select * from pq_line voltage
        inner join pq_line dev on voltage.pid = dev.id
        inner join pq_line sub on dev.pid = sub.id
        where sub.id = #{subId}
        and voltage.state = 1
        <if test="voltageName!=null and voltageName.size()!=0">
            and voltage.name in
            <foreach collection="voltageName" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="getDeviceList" resultType="string">
        SELECT Id FROM pq_device WHERE Dev_Model = 1 AND Dev_Data_Type IN (1,2) AND Run_Flag = 0
    </select>


    <select id="getOnLineCount" resultType="int">
        SELECT
        count( f.Com_Flag )
        FROM
        pq_line a
        INNER JOIN pq_line_detail b ON a.id = b.id
        INNER JOIN pq_line c ON a.pid = c.id
        INNER JOIN pq_line d ON c.pid = d.id
        INNER JOIN pq_device f ON d.id = f.id
        WHERE
        a.state = 1
        AND f.Com_Flag = 1
        AND a.id in
        <foreach collection="lineIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <select id="getOnOrUnLine" resultType="String">
        select a.id from pq_line a
        inner join pq_line b on a.pid = b.id
        inner join pq_line c on b.pid =c.id
        inner join pq_device d on c.id = d.id
        where a.id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and d.com_flag = #{comFlag}
        and a.state = 1
    </select>


    <select id="getLineBySubStation" resultType="com.njcn.device.biz.pojo.dto.LineDevGetDTO">
        SELECT
        pq_line.id pointId,
        lineDetail.Time_Interval `interval`,
        dev.id devId,
        device.com_flag
        FROM
        pq_line pq_line
        INNER JOIN pq_line_detail lineDetail ON pq_line.id = lineDetail.id
        INNER JOIN pq_line busBar ON pq_line.pid = busBar.id
        INNER JOIN pq_line dev ON busBar.pid = dev.id
        INNER JOIN pq_device device ON dev.id = device.id
        WHERE
        (pq_line.LEVEL = 6
        AND pq_line.state = 1
        AND device.Dev_Model = 1
        AND device.Run_Flag = 0
        )
        and FIND_IN_SET(#{subId}, pq_line.pids );


    </select>
    <select id="getLineByIDs" resultType="java.lang.String">
        SELECT DISTINCT
                line.id
        FROM
            pq_line line,
            pq_line voltage,
            pq_line device,
            pq_line substation,
            pq_line gd

        <where>
            line.pid = voltage.id
            AND voltage.pid = device.id
            AND device.pid = substation.id
            AND substation.pid = gd.id
            <if test="searchValue!=null and searchValue!=''">
                and (
                     gd.name like CONCAT('%', #{searchValue}, '%')
                    or substation.name like CONCAT('%', #{searchValue}, '%')
                    or line.NAME like CONCAT('%', #{searchValue},'%')
                )
            </if>
        </where>

    </select>
    <select id="getSubStations" resultType="com.njcn.device.pq.pojo.po.Line">
        SELECT
        line.*
        FROM
            pq_line line
        INNER JOIN pq_substation sub on  line.id = sub.id
        <where>
             line.Level = 3
            AND line.State = 1
            <if test="subId!=null and subId.size()!=0">
                and line.id in
                <foreach collection="subId" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="scale!=null and scale.size()!=0">
                and sub.Scale in
                <foreach collection="scale" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>

        </where>
    </select>
    <select id="getvoltage" resultType="com.njcn.device.pq.pojo.vo.TerminalTree">
        SELECT
            voltage.id as id,
            sub.id as pid,
            concat( sub.NAME, " ", voltage.NAME )  as name,
            voltage.Sort as sort
        FROM
            pq_line voltage
        INNER JOIN pq_line dev ON voltage.pid = dev.id
        INNER JOIN pq_line sub ON dev.pid = sub.id
        <where>
            voltage.state = 1
            <if test="id != null and id !='' and type == 0">
                and sub.id != #{id}
            </if>
            <if test="id != null and id !='' and type == 1">
                    and sub.id = #{id}
            </if>
            <if test="id != null and id !='' and type == 2">
                and voltage.id = #{id}
            </if>
        </where>
    </select>

</mapper>
