<?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.product.terminal.mysqlTerminal.mapper.LineMapper">


    <select id="getLineIdByIds" resultType="string">
        select
        point.id lineId,
        point.name lineName,
        dev.id devId,
        dev.name devName,
        device.com_flag,
        device.run_flag,
        station.id stationId,
        station.name stationName,
        gd.id gdId,
        gd.name gdName,
        detail.num,
        busBar.scale voltageLevel
        from pq_line point
        inner join pq_line_detail detail on point.id = detail.id
        inner join pq_line voltage on point.pid = voltage.id
        inner join pq_voltage busBar on voltage.id = busBar.id
        inner join pq_line dev on voltage.pid = dev.id
        inner join pq_device device on dev.id = device.id
        inner join pq_line station on dev.pid = station.id
        inner join pq_line gd on station.pid = gd.id
        where point.id in
        <foreach collection="lineIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>


    <select id="getStationList" resultType="TerminalShowVO">

        SELECT DISTINCT
        a.NAME stationName,
        a.id stationId,
        b.Scale stationVoltageLevel,
        b.lng,
        b.lat,
        c.NAME gdName,
        min( detail.run_flag ) runFlag
        FROM
        pq_line line
        INNER JOIN pq_line bus ON line.pid = bus.id
        INNER JOIN pq_line dev ON bus.pid = dev.id
        INNER JOIN pq_device detail ON detail.id = dev.id
        INNER JOIN pq_line a ON dev.pid = a.id
        INNER JOIN pq_substation b ON a.id = b.id
        INNER JOIN pq_line c ON a.pid = c.id
        where line.id in
        <foreach collection="lineIds" open="(" separator="," close=")" item="item">
            #{item}
        </foreach>
        <if test="runFlag!=null">
            and detail.run_flag = #{runFlag}
        </if>
        <if test="searchValue!=null and searchValue!=''">
            and (a.name like CONCAT('%', #{searchValue}, '%')
            or c.name like CONCAT('%', #{searchValue}, '%'))
        </if>
        GROUP BY stationName,stationId,stationVoltageLevel,lng,lat,gdName order by gdName,stationName
    </select>

    <select id="getDevList" resultType="TerminalShowVO">
        SELECT DISTINCT
        dev.NAME devName,
        dev.id devId,
        a.name stationName,
        c.NAME gdName,
        detail.run_flag,
        detail.ip,
        detail.Manufacturer,
        detail.Login_Time
        FROM
        pq_line line
        INNER JOIN pq_line bus ON line.pid = bus.id
        INNER JOIN pq_line dev ON bus.pid = dev.id
        INNER JOIN pq_device detail ON detail.id = dev.id
        INNER JOIN pq_line a ON dev.pid = a.id
        INNER JOIN pq_substation b ON a.id = b.id
        INNER JOIN pq_line c ON a.pid = c.id
        where line.id in
        <foreach collection="lineIds" open="(" separator="," close=")" item="item">
            #{item}
        </foreach>
        <if test="runFlag!=null">
            and detail.run_flag = #{runFlag}
        </if>
        <if test="searchValue!=null and searchValue!=''">
            and (dev.name like CONCAT('%', #{searchValue}, '%')
            or a.name like CONCAT('%', #{searchValue}, '%'))
        </if>
<!--        order by c.name,a.name-->
    </select>

    <select id="getLineList" resultType="TerminalShowVO">
        SELECT DISTINCT
        line.NAME lineName,
        line.id lineId,
        a.name stationName,
        c.NAME gdName,
        detail.run_flag,
        detail.ip,
        detail.Manufacturer,
        detail.Login_Time,
        detail.com_flag,
        users.project_name objName,
        pq_voltage.scale voltageLevel
        from pq_line line
        INNER JOIN pq_line_detail lined ON line.id = lined.id
        INNER JOIN pq_line bus ON line.pid = bus.id
        INNER JOIN pq_voltage ON pq_voltage.id = bus.id
        INNER JOIN pq_line dev ON bus.pid = dev.id
        INNER JOIN pq_device detail ON detail.id = dev.id
        INNER JOIN pq_line a ON dev.pid = a.id
        INNER JOIN pq_substation b ON a.id = b.id
        INNER JOIN pq_line c ON a.pid = c.id
        LEFT JOIN supervision_user_report users ON users.id = lined.obj_id
        where line.id in
        <foreach collection="lineIds" open="(" separator="," close=")" item="item">
            #{item}
        </foreach>
        <if test="comFlag!=null">
            and detail.com_flag = #{comFlag}
        </if>
        <if test="searchValue!=null and searchValue!=''">
            and (line.name like CONCAT('%', #{searchValue}, '%')
            or a.name like CONCAT('%', #{searchValue}, '%')
            or c.name like CONCAT('%', #{searchValue}, '%'))
        </if>
<!--        order by c.name,a.name-->
    </select>

    <select id="getMonitorDetail" resultType="com.njcn.product.terminal.mysqlTerminal.pojo.dto.LineDevGetDTO">
        select a.id pointId,a.name pointName,b.Time_Interval timeInterval,bus.pid devId,b.PT_Type wiringMethod
        from pq_line a
                 inner join pq_line_detail b on a.id=b.id
                 inner join pq_line bus on a.pid =bus.id
        where a.id = #{monitorId}
    </select>

    <select id="getLineByCondition" resultType="Line">
        select
        t1.*
        from
        pq_line t1 ,
        pq_line_detail t2
        where
        t1.state = 1
        and 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.lineRunFlag!=null ">
            and t2.Run_Flag = #{deviceInfoParam.lineRunFlag}
        </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="getVoltageByCondition" resultType="Line">
        select
        t1.*
        from
        pq_line t1 ,
        pq_voltage t2
        <where>
            t1.id = t2.id
            <if test="voltageIds!=null and voltageIds.size()!=0">
                and t1.id in
                <foreach collection="voltageIds" separator="," open="(" close=")" item="item">
                    #{item}
                </foreach>
            </if>
            <if test="scale!=null and scale.size()!=0">
                AND t2.scale in
                <foreach collection="scale" open="(" close=")" item="item" separator=",">
                    #{item.id}
                </foreach>
            </if>
        </where>




    </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="deviceType.comFlag!=null and deviceType.comFlag.size()!=0">
                AND t2.Com_Flag in
                <foreach collection="deviceType.comFlag" 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>
            <if test="devIds!=null and devIds.size()!=0">
                AND t1.id IN
                <foreach collection="devIds" open="(" close=")" item="item" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>

    </select>


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


    <select id="getSubIdByScale" resultType="java.lang.String">
        select
        t1.id
        from
        pq_line t1 ,
        pq_substation t2
        where
        t1.id = t2.id
        and
        t1.id in
        <foreach collection="subIds" 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="getDeviceIdByPowerFlag" 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.Power_Flag = #{powerFlag}
    </select>

    <select id="getLineSubGdDetail" resultType="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="getLineDetail" resultType="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="queryMonitorByUser" resultType="LedgerBaseInfo">
        select line.id lineId,line.name lineName,users.project_name objName,users.user_no userNo
        from pq_line line
        inner join pq_line_detail detail on line.id = detail.id
        inner join supervision_user_report users on detail.obj_id = users.id
        where users.user_no in
        <foreach collection="userIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

</mapper>
