<?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.SysDeviceMapper">

    <resultMap id="SysDeviceResult" type="SysDevice">
        <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="ownerName" column="owner_name"/>
        <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>

    <sql id="selectDeviceVo">
        select device_id,
               device_name,
               device_sn,
               device_type,
               status,
               owner_name,
               last_connected_at,
               create_by,
               create_time,
               update_by,
               update_time,
               remark
        from sys_device
    </sql>

    <select id="selectDeviceById" parameterType="Long" resultMap="SysDeviceResult">
        <include refid="selectDeviceVo"/>
        where device_id = #{deviceId}
    </select>

    <select id="selectDeviceList" parameterType="SysDevice" resultMap="SysDeviceResult">
        <include refid="selectDeviceVo"/>
        <where>
            <if test="deviceName != null and deviceName != ''">
                and device_name like concat('%', #{deviceName}, '%')
            </if>
            <if test="deviceSn != null and deviceSn != ''">
                and device_sn like concat('%', #{deviceSn}, '%')
            </if>
            <if test="deviceType != null and deviceType != ''">
                and device_type = #{deviceType}
            </if>
            <if test="status != null and status != ''">
                and status = #{status}
            </if>
            <if test="ownerName != null and ownerName != ''">
                and owner_name like concat('%', #{ownerName}, '%')
            </if>
        </where>
        order by create_time desc
    </select>

    <insert id="insertDevice" parameterType="SysDevice" useGeneratedKeys="true" keyProperty="deviceId">
        insert into sys_device (
            device_name,
            device_sn,
            device_type,
            status,
            owner_name,
            last_connected_at,
            create_by,
            create_time,
            remark
        ) values (
            #{deviceName},
            #{deviceSn},
            #{deviceType},
            #{status},
            #{ownerName},
            #{lastConnectedAt},
            #{createBy},
            sysdate(),
            #{remark}
        )
    </insert>

    <update id="updateDevice" parameterType="SysDevice">
        update sys_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="ownerName != null">owner_name = #{ownerName},</if>
            <if test="lastConnectedAt != null">last_connected_at = #{lastConnectedAt},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            update_time = sysdate()
        </set>
        where device_id = #{deviceId}
    </update>

    <delete id="deleteDeviceById" parameterType="Long">
        delete from sys_device where device_id = #{deviceId}
    </delete>

    <delete id="deleteDeviceByIds" parameterType="Long">
        delete from sys_device where device_id in
        <foreach collection="array" item="deviceId" open="(" separator="," close=")">
            #{deviceId}
        </foreach>
    </delete>

    <select id="checkDeviceSnUnique" parameterType="String" resultMap="SysDeviceResult">
        <include refid="selectDeviceVo"/>
        where device_sn = #{deviceSn}
        limit 1
    </select>

</mapper>
