<?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.energy.mapper.LogicDeviceLineMapper">


    <select id="getLogicDevList" resultType="EnergyDeviceTreeVO">
        select a.id,a.pid,a.name,2 as level,b.state
        from ele_logic_dev a
        inner join ele_logic_access b on a.did = b.did
        where a.state = 1
        order by a.did
    </select>

    <select id="getEleLineList" resultType="EnergyDeviceTreeVO">
        select id,pid,name,line_id as lineId,3 as level from ele_line
        where state = 1
        order by ele_line.idx
    </select>




    <select id="getEleNoBindsLineList" resultType="EnergyDeviceTreeVO">
        SELECT id,
               pid,
               NAME,
               3 AS LEVEL,
               Line_Id lineId
        FROM ele_line
        WHERE state = 1
          AND Line_Id NOT IN (
            SELECT Line_Id
            FROM ele_load_set
        )
        ORDER BY ele_line.idx
    </select>

    <select id="getEleBindsLineList" resultType="EnergyDeviceTreeVO">
        SELECT
            id,
            pid,
            NAME,
            3 AS LEVEL,
            Line_Id lineId
        FROM
            ele_line
        WHERE
            state = 1
          AND
                Line_Id  IN (
                SELECT
                    Line_Id
                FROM
                    ele_load_set
                WHERE
                    id = #{loadId})

        ORDER BY
            ele_line.idx
    </select>

    <select id="getDevInfo" resultType="LogicDeviceLineInfoVO">
        select a.id,
        a.pid,
        a.did,
        a.name,
        b.name netDevName,
        b.ndid,
        a.address,
        a.position,
        a.create_time,
        a.state from ele_logic_dev a
        inner join ele_net_dev b on a.pid = b.id
        where a.id = #{id}
        and a.state = 1
    </select>



    <select id="getTabTemplateById" resultType="DataSet">
        select
        c.id,
        c.name,
        c.another_name,
        c.idx,
        c.store_flag
        from
        ele_logic_dev a
        inner join ele_data_mould b on a.idx = b.id
        inner join ele_data_set c on b.id = c.pid
        where a.id = #{id}
        order by c.idx
    </select>


    <select id="getGroupDataBySetId" resultType="DataArray">
        SELECT
	    b.id pid,
	    d.id,
	    d.data_id,
	    d.sort,
	    d.class_name
FROM
	ele_data_set a
	INNER JOIN ele_data_group b ON a.id = b.pid
	INNER JOIN ele_group_arr c ON b.id = c.group_id
	INNER JOIN ele_data_array d ON c.arr_id = d.id
WHERE
	a.id = #{id}
    </select>


    <select id="getAllDataBySet" resultType="EnergyTemplateVO">
        SELECT
	    b.id pid,
	    d.id,
	    d.data_id,
	    d.sort,
	    d.phase,
	    d.class_name,
	    d.another_name name,
	    2 as level
FROM
	ele_data_set a
	INNER JOIN ele_data_group b ON a.id = b.pid
	INNER JOIN ele_group_arr c ON b.id = c.group_id
	INNER JOIN ele_data_array d ON c.arr_id = d.id
WHERE
	a.id = #{id}
    </select>




    <select id="getDataList" resultType="EnergyTemplateVO">
        select b.id,
        b.name,
        a.sort,
        b.class_id,
        <if test="className == 'ele_epd_pqd'">
        b.phase,
        b.type,
        b.unit,
        </if>
        <if test="className == 'ele_md'">
            b.phase,
            b.type,
            b.unit,
        </if>
        <if test="className == 'ele_ctrl'">
           1 as operationType,
           b.max_num,
           b.min_num,
           b.ctl_value,
        </if>
        a.another_name
        from ele_data_array a inner join ${className} b on a.data_id = b.id
        where a.id in
        <foreach collection="ids" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        order by sort

    </select>


    <select id="getParseTemplate" resultType="String">
        select
        type
        from ele_data_array
        where pid = #{setId}
        order by sort
    </select>


    <select id="getNetAndDevByLineIds" resultType="NetAndDevByLineDTO">
        select a.line_id,b.did devId,c.ndid,a.name lineName from ele_line a
        inner join ele_logic_dev b on a.pid = b.id
        inner join ele_net_dev c on b.pid = c.id
        where a.state = 1
        and a.line_id in
        <foreach collection="lineIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>


</mapper>
