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


    <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="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="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="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="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="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="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="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="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>

</mapper>
