<?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.bst.bindRecord.mapper.BindRecordMapper">

    <resultMap type="BindRecordVO" id="BindRecordResult" autoMapping="true">
        <result property="recordId"    column="record_id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="userId"    column="user_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="type"    column="type"    />
    </resultMap>

    <sql id="selectBindRecordVo">
        select
            bbr.record_id,
            bbr.device_id,
            bbr.user_id,
            bbr.create_time,
            bbr.type,
            bd.device_name,
            su.user_name,
            su.nick_name
        from bst_bind_record bbr
        left join bst_device bd on bbr.device_id = bd.device_id
        left join sys_user su on bbr.user_id = su.user_id
    </sql>

    <sql id="searchCondition">
        <if test="query.deviceId != null "> and bbr.device_id = #{query.deviceId}</if>
        <if test="query.userId != null "> and bbr.user_id = #{query.userId}</if>
        <if test="query.type != null  and query.type != ''"> and bbr.type = #{query.type}</if>
        ${query.params.dataScope}
    </sql>

    <select id="selectBindRecordList" parameterType="BindRecordQuery" resultMap="BindRecordResult">
        <include refid="selectBindRecordVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectBindRecordByRecordId" parameterType="Long" resultMap="BindRecordResult">
        <include refid="selectBindRecordVo"/>
        where record_id = #{recordId}
    </select>

    <insert id="insertBindRecord" parameterType="BindRecord" useGeneratedKeys="true" keyProperty="recordId">
        insert into bst_bind_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceId != null">device_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="type != null and type != ''">type,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceId != null">#{deviceId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="type != null and type != ''">#{type},</if>
         </trim>
    </insert>

    <insert id="batchInsert" parameterType="BindRecord" useGeneratedKeys="true" keyProperty="recordId">
        insert into bst_bind_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            device_id,
            user_id,
            create_time,
            type,
        </trim>
        values
        <foreach collection="list" item="i" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="i.deviceId != null ">#{i.deviceId},</if>
                <if test="i.deviceId == null ">default,</if>
                <if test="i.userId != null ">#{i.userId},</if>
                <if test="i.userId == null ">default,</if>
                <if test="i.createTime != null ">#{i.createTime},</if>
                <if test="i.createTime == null ">default,</if>
                <if test="i.type != null  and i.type != ''">#{i.type},</if>
                <if test="i.type == null  or i.type == ''">default,</if>
            </trim>
        </foreach>
    </insert>

    <update id="batchUpdate">
        update bst_bind_record
        <trim prefix="SET" suffixOverrides=",">
            <foreach open="device_id = CASE record_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.deviceId != null ">
                        WHEN #{item.record_id} THEN #{item.deviceId}
                    </when>
                    <otherwise>
                        WHEN #{item.record_id} THEN `device_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="user_id = CASE record_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.userId != null ">
                        WHEN #{item.record_id} THEN #{item.userId}
                    </when>
                    <otherwise>
                        WHEN #{item.record_id} THEN `user_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="create_time = CASE record_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.createTime != null ">
                        WHEN #{item.record_id} THEN #{item.createTime}
                    </when>
                    <otherwise>
                        WHEN #{item.record_id} THEN `create_time`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="type = CASE record_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.type != null  and item.type != ''">
                        WHEN #{item.record_id} THEN #{item.type}
                    </when>
                    <otherwise>
                        WHEN #{item.record_id} THEN `type`
                    </otherwise>
                </choose>
            </foreach>
        </trim>
        where record_id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item.recordId}
        </foreach>
    </update>

    <update id="updateBindRecord" parameterType="BindRecord">
        update bst_bind_record
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where record_id = #{data.recordId}
    </update>

    <sql id="updateColumns">
        <if test="data.deviceId != null">device_id = #{data.deviceId},</if>
        <if test="data.userId != null">user_id = #{data.userId},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.type != null and data.type != ''">type = #{data.type},</if>
    </sql>

    <delete id="deleteBindRecordByRecordId" parameterType="Long">
        delete from bst_bind_record where record_id = #{recordId}
    </delete>

    <delete id="deleteBindRecordByRecordIds" parameterType="String">
        delete from bst_bind_record where record_id in
        <foreach item="recordId" collection="array" open="(" separator="," close=")">
            #{recordId}
        </foreach>
    </delete>
</mapper>
