<?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.zcxk.dao.DeviceSceneMapper">
    <resultMap type="com.zcxk.entity.DeviceSceneEntity" id="DeviceSceneResult">
        <result property="id" column="id"/>
        <result property="tenantId" column="tenant_id"/>
        <result property="parentSceneId" column="parent_scene_id"/>
        <result property="sceneId" column="scene_id"/>
        <result property="deviceId" column="device_id"/>
        <result property="status" column="status"/>
        <result property="dateCreate" column="date_create"/>
        <result property="createBy" column="create_by"/>
        <result property="dateUpdate" column="date_update"/>
        <result property="updateBy" column="update_by"/>
    </resultMap>

    <!--  实体栏位  -->
    <sql id="deviceSceneColumns">
         a.id as "id" ,
         a.tenant_id as "tenantId" ,
         a.parent_scene_id as "parentSceneId" ,
         a.scene_id as "sceneId" ,
         a.device_id as "deviceId" ,
         a.status as "status" ,
         a.date_create as "dateCreate" ,
         a.create_by as "createBy" ,
         a.date_update as "dateUpdate" ,
         a.update_by as "updateBy" 
     </sql>

    <sql id="deviceInnerJoins">
        inner join sms_scene_type t3 on t1.scene_type_id=t3.id and t3. status = 1
        inner join sms_device_scene t2 on t2.parent_scene_id=t1.id and t2. status = 1
        inner join sms_device t6 on t2.device_id=t6.id and t6. status = 1 and t6.enable_state = 1
        inner join sms_scene t4 on t2.scene_id=t4.id and t4. status = 1
        inner join sms_scene_type t5 on t4.scene_type_id=t5.id and t5. status = 1
    </sql>

    <!--  根据主键获取实体   -->
    <select id="findDeviceSceneById" resultType="com.zcxk.entity.DeviceSceneEntity">
        SELECT
        <include refid="deviceSceneColumns"/>
        FROM sms_device_scene a
        WHERE a.id = #{id} and a.status=1
    </select>

    <!--  根据获取实体List   -->
    <select id="findList" resultType="com.zcxk.entity.DeviceSceneEntity">
        SELECT
        <include refid="deviceSceneColumns"/>
        FROM sms_device_scene a
        <where>
            a.status=1
            <if test="tenantId != null  and tenantId != ''">and a.tenant_id = #{tenantId}</if>
            <if test="parentSceneId != null ">and a.parent_scene_id = #{parentSceneId}</if>
            <if test="sceneId != null ">and a.scene_id = #{sceneId}</if>
            <if test="deviceId != null ">and a.device_id = #{deviceId}</if>
        </where>
    </select>

    <select id="findSceneId" resultType="com.zcxk.dto.scene.SceneIdDto">
        select  t4.id as "sceneId", t4.PARENT_SCENE_ID as "parentSceneId", t4.parent_scene_ids AS "parentSceneIds"
        from sms_scene t1
        <include refid="deviceInnerJoins"/>
        left join sms_device_parm t8 on t8.scene_id = t2.scene_id and t8.device_id=t2.device_id and t8. status = 1
        left join sms_device_attribute t9 on t8.attribute_id=t9.id and t9.status = 1
        where t1.parent_scene_id = 0 and t1. status = 1 and t1. enable_state = 1
        and t3.scene_type_name = #{firstSceneTypeName}
        and t5.scene_type_name = #{secondSceneTypeName}
        and t2.DEVICE_ID = #{deviceId}
        order by t4.scene_name,t8.seq limit 1
    </select>
</mapper>