<?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.ruoyi.business.mapper.HwSceneMapper">

    <resultMap type="HwScene" id="HwSceneResult">
        <result property="sceneId"    column="scene_id"    />
        <result property="sceneName"    column="scene_name"    />
        <result property="tenantId"    column="tenant_id"    />
        <result property="sceneModeId"    column="scene_mode_id"    />
        <result property="scenePic"    column="scene_pic"    />
        <result property="defaultFlag"    column="default_flag"    />
        <result property="sceneStatus"    column="scene_status"    />
        <result property="authMode"    column="auth_mode"    />
        <result property="modeAccount"    column="mode_account"    />
        <result property="modeKey"    column="mode_key"    />
        <result property="modeSecret"    column="mode_secret"    />
        <result property="preserveTime"    column="preserve_time"    />
        <result property="testPreserveTime"    column="test_preserve_time"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="sceneEnvironment"    column="scene_environment"    />
        <result property="sceneField"    column="scene_field"    />
    </resultMap>
    <resultMap type="HwSceneVo" id="HwSceneVoResult">
        <result property="sceneId"    column="scene_id"    />
        <result property="sceneName"    column="scene_name"    />
        <result property="tenantId"    column="tenant_id"    />
        <result property="tenantName"    column="tenant_name"    />
        <result property="sceneModeId"    column="scene_mode_id"    />
        <result property="sceneModeName"    column="scene_mode_name"    />
        <result property="scenePic"    column="scene_pic"    />
        <result property="defaultFlag"    column="default_flag"    />
        <result property="sceneStatus"    column="scene_status"    />
        <result property="authMode"    column="auth_mode"    />
        <result property="modeAccount"    column="mode_account"    />
        <result property="modeKey"    column="mode_key"    />
        <result property="modeSecret"    column="mode_secret"    />
        <result property="preserveTime"    column="preserve_time"    />
        <result property="testPreserveTime"    column="test_preserve_time"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="sceneEnvironment"    column="scene_environment"    />
        <result property="sceneField"    column="scene_field"    />
        <result property="contactName"    column="contact_name"    />
        <result property="contactPhone"    column="contact_phone"    />
    </resultMap>

    <sql id="selectHwSceneVo">
        select scene_id, scene_name, tenant_id, scene_mode_id, scene_pic, default_flag, scene_status, auth_mode, mode_account, mode_key, mode_secret, preserve_time, test_preserve_time, remark, create_by, create_time, update_by, update_time, scene_environment, scene_field from hw_scene
    </sql>

    <select id="selectHwSceneVoList" parameterType="HwSceneVo" resultMap="HwSceneVoResult">
        select a.*,b.tenant_name,c.scene_mode_name from hw_scene as a
        left join hw_tenant as b on a.tenant_id = b.tenant_id
        left join hw_scene_mode as c on a.scene_mode_id = c.scene_mode_id
        <where>
            <if test="sceneName != null  and sceneName != ''"> and a.scene_name like concat('%', #{sceneName}, '%')</if>
            <if test="contactName != null  and contactName != ''"> and b.contact_name like concat('%', #{contactName}, '%')</if>
            <if test="contactPhone != null  and contactPhone != ''"> and b.contact_phone like concat('%', #{contactPhone}, '%')</if>
            <if test="sceneModeId != null  and sceneModeId != ''"> and a.scene_mode_id = #{sceneModeId}</if>
            <if test="defaultFlag != null  and defaultFlag != ''"> and a.default_flag = #{defaultFlag}</if>
            <if test="tenantId != null  and tenantId != ''"> and a.tenant_id = #{tenantId}</if>
           </where>
    </select>
    <select id="selectHwSceneList" parameterType="HwScene" resultMap="HwSceneResult">
        <include refid="selectHwSceneVo"/>
        <where>
            <if test="sceneName != null  and sceneName != ''"> and scene_name like concat('%', #{sceneName}, '%')</if>
            <if test="tenantId != null "> and tenant_id = #{tenantId}</if>
            <if test="sceneModeId != null "> and scene_mode_id = #{sceneModeId}</if>
            <if test="scenePic != null  and scenePic != ''"> and scene_pic = #{scenePic}</if>
            <if test="defaultFlag != null  and defaultFlag != ''"> and default_flag = #{defaultFlag}</if>
            <if test="sceneStatus != null  and sceneStatus != ''"> and scene_status = #{sceneStatus}</if>
            <if test="authMode != null  and authMode != ''"> and auth_mode = #{authMode}</if>
            <if test="modeAccount != null  and modeAccount != ''"> and mode_account = #{modeAccount}</if>
            <if test="modeKey != null  and modeKey != ''"> and mode_key = #{modeKey}</if>
            <if test="modeSecret != null  and modeSecret != ''"> and mode_secret = #{modeSecret}</if>
            <if test="preserveTime != null "> and preserve_time = #{preserveTime}</if>
            <if test="testPreserveTime != null "> and test_preserve_time = #{testPreserveTime}</if>
            <if test="sceneEnvironment != null  and sceneEnvironment != ''"> and scene_environment = #{sceneEnvironment}</if>
            <if test="sceneField != null  and sceneField != ''"> and scene_field = #{sceneField}</if>
        </where>
    </select>
    <select id="selectHwSceneByTenantId0" parameterType="Long" resultMap="HwSceneResult">
        <include refid="selectHwSceneVo"/>
         order by update_time desc
    </select>
    <select id="selectHwSceneByTenantIdNot0" parameterType="Long" resultMap="HwSceneResult">
        <include refid="selectHwSceneVo"/>
        where tenant_id = #{tenantId}

        order by update_time desc
    </select>
    <select id="selectHwSceneBySceneId" parameterType="Long" resultMap="HwSceneResult">
        <include refid="selectHwSceneVo"/>
        where scene_id = #{sceneId}
    </select>
    <select id="selectHwSceneBySceneModeId" parameterType="Long" resultMap="HwSceneResult">
        <include refid="selectHwSceneVo"/>
        from hw_scene_mode  where scene_mode_id = #{SceneModeId}
    </select>
    <insert id="insertHwScene" parameterType="HwScene" useGeneratedKeys="true" keyProperty="sceneId">
        insert into hw_scene
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sceneName != null and sceneName != ''">scene_name,</if>
            <if test="tenantId != null">tenant_id,</if>
            <if test="sceneModeId != null">scene_mode_id,</if>
            <if test="scenePic != null">scene_pic,</if>
            <if test="defaultFlag != null and defaultFlag != ''">default_flag,</if>
            <if test="sceneStatus != null and sceneStatus != ''">scene_status,</if>
            <if test="authMode != null and authMode != ''">auth_mode,</if>
            <if test="modeAccount != null">mode_account,</if>
            <if test="modeKey != null and modeKey != ''">mode_key,</if>
            <if test="modeSecret != null and modeSecret != ''">mode_secret,</if>
            <if test="preserveTime != null">preserve_time,</if>
            <if test="testPreserveTime != null">test_preserve_time,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="sceneEnvironment != null">scene_environment,</if>
            <if test="sceneField != null">scene_field,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="sceneName != null and sceneName != ''">#{sceneName},</if>
            <if test="tenantId != null">#{tenantId},</if>
            <if test="sceneModeId != null">#{sceneModeId},</if>
            <if test="scenePic != null">#{scenePic},</if>
            <if test="defaultFlag != null and defaultFlag != ''">#{defaultFlag},</if>
            <if test="sceneStatus != null and sceneStatus != ''">#{sceneStatus},</if>
            <if test="authMode != null and authMode != ''">#{authMode},</if>
            <if test="modeAccount != null">#{modeAccount},</if>
            <if test="modeKey != null and modeKey != ''">#{modeKey},</if>
            <if test="modeSecret != null and modeSecret != ''">#{modeSecret},</if>
            <if test="preserveTime != null">#{preserveTime},</if>
            <if test="testPreserveTime != null">#{testPreserveTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="sceneEnvironment != null">#{sceneEnvironment},</if>
            <if test="sceneField != null">#{sceneField},</if>
         </trim>
    </insert>
    <update id="updateDefaultFlag" parameterType="Long">
         update hw_scene
 set default_flag = '0'
        where tenant_id = #{tenantId}
    </update>
    <update id="updateHwScene" parameterType="HwScene">
        update hw_scene
        <trim prefix="SET" suffixOverrides=",">
            <if test="sceneName != null and sceneName != ''">scene_name = #{sceneName},</if>
            <if test="tenantId != null">tenant_id = #{tenantId},</if>
            <if test="sceneModeId != null">scene_mode_id = #{sceneModeId},</if>
            <if test="scenePic != null">scene_pic = #{scenePic},</if>
            <if test="defaultFlag != null and defaultFlag != ''">default_flag = #{defaultFlag},</if>
            <if test="sceneStatus != null and sceneStatus != ''">scene_status = #{sceneStatus},</if>
            <if test="authMode != null and authMode != ''">auth_mode = #{authMode},</if>
            <if test="modeAccount != null">mode_account = #{modeAccount},</if>
            <if test="modeKey != null and modeKey != ''">mode_key = #{modeKey},</if>
            <if test="modeSecret != null and modeSecret != ''">mode_secret = #{modeSecret},</if>
            <if test="preserveTime != null">preserve_time = #{preserveTime},</if>
            <if test="testPreserveTime != null">test_preserve_time = #{testPreserveTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="sceneEnvironment != null">scene_environment = #{sceneEnvironment},</if>
            <if test="sceneField != null">scene_field = #{sceneField},</if>
        </trim>
        where scene_id = #{sceneId}
    </update>

    <delete id="deleteHwSceneBySceneId" parameterType="Long">
        update hw_scene set scene_status = '9' where scene_id = #{sceneId}
    </delete>

    <delete id="deleteHwSceneBySceneIds" parameterType="String">
        update hw_scene set scene_status = '9' where scene_id = in
        <foreach item="sceneId" collection="array" open="(" separator="," close=")">
            #{sceneId}
        </foreach>
    </delete>
</mapper>
