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

    <update id="deptChangeBindLine" >
    update pq_dept_line set id = #{id} where Line_Id = #{lineId}
    </update>

    <select id="getAllBindLine" resultType="String">
        select line_id from pq_dept_line
    </select>

    <select id="getOtherBindLine" resultType="String">
        select line_id from pq_dept_line where Id !=#{deptId}
    </select>

    <select id="getMyselfBindLine" resultType="String">
        select line_id from pq_dept_line where Id =#{deptId}
    </select>

    <select id="lineDevGet" resultType="com.njcn.device.biz.pojo.dto.LineDevGetDTO">
        select
        <!--监测点-->
        <if test="type == 1">
            pq_dept_line.id unitId,
            point.id pointId,
            lineDetail.Time_Interval as `interval`,
            lineDetail.load_type lineTag,
            dic.id voltageLevel,
            dev.id devId,
            device.com_flag comFlag,
            1 as lineType,
            0 as type
        </if>
        <!--母线-->
        <if test="type == 2">
            DISTINCT
            pq_dept_line.id unitId,
            dic.id voltageLevel,
            voltage.id pointId,
            point.id devId
        </if>
        <!--装置-->
        <if test="type == 3">
            DISTINCT
            pq_dept_line.id unitId,
            dev.id devId,
            dic.id voltageLevel,
            1 as type  ,
            device.update_time,
            device.Com_Flag as comFlag
        </if>
        from pq_dept_line pq_dept_line
        inner join pq_line point on pq_dept_line.line_id = point.id
        inner join pq_line_detail lineDetail on point.id = lineDetail.id
        inner join pq_line voltage on point.pid = voltage.id
        inner join pq_voltage pq_voltage on voltage.id = pq_voltage.id
        inner join sys_dict_data dic on pq_voltage.Scale= dic.id
        inner join pq_line dev on voltage.pid = dev.id
        inner join pq_device device on dev.id = device.id
        where device.Dev_Model = 1
        and point.state = 1
        and device.Dev_Data_Type in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        and device.Run_Flag = 0

    </select>


    <select id="orgSubStationGet" resultType="com.njcn.device.biz.pojo.dto.TerminalGetBase">
        select
        DISTINCT
        pq_dept_line.id unitId,
        substation.id ledgerId,
        point.id lineId
        from pq_dept_line pq_dept_line
        inner join pq_line point on pq_dept_line.line_id = point.id
        inner join pq_line_detail lineDetail on point.id = lineDetail.id
        inner join pq_line voltage on point.pid = voltage.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 substation on dev.pid = substation.id
        where device.Dev_Model = 1
        and point.state = 1
        and device.Dev_Data_Type in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>


    <select id="getLineIdByDeptIds" resultType="string">
        select
        DISTINCT
        pq_dept_line.line_id
        from pq_dept_line pq_dept_line
        inner join pq_line point on pq_dept_line.line_id = point.id
        inner join pq_line_detail lineDetail on point.id = lineDetail.id
        inner join pq_line voltage on point.pid = voltage.id
        inner join pq_line dev on voltage.pid = dev.id
        inner join pq_device device on dev.id = device.id
        where device.Dev_Model = 1
        and device.Dev_Data_Type in
        <foreach collection="dataType" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        and device.run_flag in
        <foreach collection="runFlag" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        and pq_dept_line.id in
        <foreach collection="deptIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>
</mapper>
