<?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.system.mapper.UDeviceMapper">

    <resultMap id="UDeviceResult" type="com.ruoyi.system.domain.UDevice">
        <id property="deviceId" column="device_id"/>
        <result property="deviceName" column="device_name"/>
        <result property="deviceSn" column="device_sn"/>
        <result property="deviceType" column="device_type"/>
        <result property="status" column="status"/>
        <result property="ownerUserId" column="owner_user_id"/>
        <result property="bindTime" column="bind_time"/>
        <result property="lastConnectedAt" column="last_connected_at"/>
        <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="remark" column="remark"/>
    </resultMap>

    <select id="selectById" resultMap="UDeviceResult">
        select *
        from u_center.u_device
        where device_id = #{deviceId}
          and del_flag = '0'
        limit 1
    </select>

    <select id="selectBySn" resultMap="UDeviceResult">
        select *
        from u_center.u_device
        where device_sn = #{deviceSn}
          and del_flag = '0'
        limit 1
    </select>

    <select id="selectListByOwner" resultMap="UDeviceResult">
        select *
        from u_center.u_device
        where del_flag = '0'
          and owner_user_id = #{ownerUserId}
        <if test="deviceName != null and deviceName != ''">
            and device_name like concat('%', #{deviceName}, '%')
        </if>
        <if test="deviceType != null and deviceType != ''">
            and device_type = #{deviceType}
        </if>
        <if test="status != null and status != ''">
            and status = #{status}
        </if>
        order by create_time desc
    </select>

    <insert id="insert" parameterType="com.ruoyi.system.domain.UDevice" useGeneratedKeys="true" keyProperty="deviceId">
        insert into u_center.u_device (
            device_name,
            device_sn,
            device_type,
            status,
            owner_user_id,
            bind_time,
            last_connected_at,
            create_by,
            create_time,
            update_by,
            update_time,
            remark,
            del_flag
        ) values (
            #{deviceName},
            #{deviceSn},
            #{deviceType},
            #{status},
            #{ownerUserId},
            #{bindTime},
            #{lastConnectedAt},
            #{createBy},
            sysdate(),
            #{updateBy},
            sysdate(),
            #{remark},
            '0'
        )
    </insert>

    <update id="update" parameterType="com.ruoyi.system.domain.UDevice">
        update u_center.u_device
        <set>
            <if test="deviceName != null">device_name = #{deviceName},</if>
            <if test="deviceSn != null">device_sn = #{deviceSn},</if>
            <if test="deviceType != null">device_type = #{deviceType},</if>
            <if test="status != null">status = #{status},</if>
            <if test="bindTime != null">bind_time = #{bindTime},</if>
            <if test="lastConnectedAt != null">last_connected_at = #{lastConnectedAt},</if>
            <if test="remark != null">remark = #{remark},</if>
            update_by = #{updateBy},
            update_time = sysdate()
        </set>
        where device_id = #{deviceId}
          and owner_user_id = #{ownerUserId}
          and del_flag = '0'
    </update>

    <update id="softDeleteByIds">
        update u_center.u_device
        set del_flag = '2',
            update_by = #{updateBy},
            update_time = sysdate()
        where del_flag = '0'
          and owner_user_id = #{ownerUserId}
          and device_id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

</mapper>
