<?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.charge.core.dao.DeviceSetupRecordMapper">


    <!--  实体栏位  -->
    <sql id="deviceSetupRecordColumns">
         a.id as "id" ,
         a.scene_id as "sceneId" ,
         a.TENANT_ID as "tenantId",
         a.device_id as "deviceId" ,
         a.attribute_id as "attributeId" ,
         a.setup_method_id as "setupMethodId" ,
         a.state as "state" ,
         a.params as "params" ,
         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>


    <!--  根据主键获取实体   -->
    <select id="findDeviceSetupRecordById" resultType="com.zcxk.charge.core.entity.DeviceSetupRecordEntity">
        SELECT
        <include refid="deviceSetupRecordColumns"/>
        FROM sms_device_setup_record a

        WHERE a.id = #{id}
    </select>

    <!--  根据获取实体List   -->
    <select id="findList" resultType="com.zcxk.charge.core.entity.DeviceSetupRecordEntity">
        SELECT
        <include refid="deviceSetupRecordColumns"/>
        FROM sms_device_setup_record a

        <where>
            <if test="tenantId != null  and tenantId != ''">and a.tenant_id = #{tenantId}</if>
            <if test="sceneId != null ">and a.scene_id = #{sceneId}</if>
            <if test="deviceId != null ">and a.device_id = #{deviceId}</if>
            <if test="attributeId != null ">and a.attribute_id = #{attributeId}</if>
            <if test="setupMethodId != null ">and a.setup_method_id = #{setupMethodId}</if>
            <if test="state != null  and state != ''">and a.state = #{state}</if>
        </where>
    </select>

    <!--  根据获取实体List   -->
    <select id="selectLastOne" resultType="com.zcxk.charge.core.entity.DeviceSetupRecordEntity">
        SELECT
        <include refid="deviceSetupRecordColumns"/>
        FROM sms_device_setup_record a
        inner join sms_device_setup_method b on a.setup_method_id=b.id
        <where>
            <if test="tenantId != null  and tenantId != ''">and a.tenant_id = #{tenantId}</if>
            <if test="sceneId != null ">and a.scene_id = #{sceneId}</if>
            <if test="deviceId != null ">and a.device_id = #{deviceId}</if>
            <if test="attributeId != null ">and a.attribute_id = #{attributeId}</if>
            <if test="setupMethodId != null ">and a.setup_method_id = #{setupMethodId}</if>
            <if test="state != null  and state != ''">and a.state = #{state}</if>
            <if test="type != null">and b.type = #{type}</if>
        </where>
        order by a.date_create desc
        limit 1
    </select>
</mapper>