<?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.ics.admin.mapper.meeting.UserEquipmentMapper">
    
    <resultMap type="com.ics.admin.domain.meeting.UserEquipment" id="UserEquipmentResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="equipmentId"    column="equipment_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="startTime"    column="start_time"    />
        <result property="endDate"    column="end_date"    />
    </resultMap>

    <sql id="selectUserEquipmentVo">
        SELECT id, user_id, equipment_id, create_time,start_time,end_date FROM tb_user_equipment
    </sql>

    <select id="selectUserEquipmentList" parameterType="UserEquipment" resultMap="UserEquipmentResult">
        <include refid="selectUserEquipmentVo"/>
        <where>  
        </where>
    </select>
    
    <select id="selectUserEquipmentById" parameterType="Long" resultMap="UserEquipmentResult">
        <include refid="selectUserEquipmentVo"/>
        WHERE id = #{id}
    </select>
    <select id="selectCountByUserIdAndDeviceId" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM tb_user_equipment
        WHERE user_id = #{userId} AND equipment_id = #{equipmentId}
        AND now() &gt;  DATE_SUB(start_time, INTERVAL 15 MINUTE)
        AND now() &lt;  end_date
    </select>

    <insert id="insertUserEquipment" parameterType="UserEquipment">
        INSERT INTO tb_user_equipment
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null ">id,</if>
            <if test="userId != null ">user_id,</if>
            <if test="equipmentId != null ">equipment_id,</if>
            <if test="createTime != null ">create_time,</if>
            <if test="startTime != null ">start_time,</if>
            <if test="endDate != null ">end_date,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null ">#{id},</if>
            <if test="userId != null ">#{userId},</if>
            <if test="equipmentId != null ">#{equipmentId},</if>
            <if test="createTime != null ">#{createTime},</if>
            <if test="startTime != null ">#{startTime},</if>
            <if test="endDate != null ">#{endDate},</if>
         </trim>
    </insert>

    <update id="updateUserEquipment" parameterType="UserEquipment">
        UPDATE tb_user_equipment
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null ">user_id = #{userId},</if>
            <if test="equipmentId != null ">equipment_id = #{equipmentId},</if>
            <if test="createTime != null ">create_time = #{createTime},</if>
            <if test="startTime != null ">start_time = #{startTime},</if>
            <if test="endDate != null ">end_date = #{endDate},</if>
        </trim>
        WHERE id = #{id}
    </update>

    <delete id="deleteUserEquipmentById" parameterType="Long">
        DELETE FROM tb_user_equipment WHERE id = #{id}
    </delete>

    <delete id="deleteUserEquipmentByIds" parameterType="String">
        DELETE FROM tb_user_equipment where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    
</mapper>