<?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.EquipmentMapper">
    
    <resultMap type="com.ics.admin.domain.meeting.Equipment" id="EquipmentResult">
        <result property="id"    column="id"    />
        <result property="type"    column="type"    />
        <result property="equipmentName"    column="equipment_name"    />
        <result property="status"    column="status"    />
        <result property="ip"    column="ip"    />
        <result property="equipmentNum"    column="equipment_num"    />
        <result property="pic"    column="pic"    />
        <result property="deleteFlag"    column="delete_flag"    />
        <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="parkId"    column="park_id"    />
        <result property="buildingId"    column="building_id"    />
        <result property="tenantId"    column="tenant_id"    />
    </resultMap>

    <sql id="selectEquipmentVo">
        SELECT id, type, equipment_name, status,  equipment_num,ip, pic, delete_flag, create_by,park_id,building_id,tenant_id, create_time, update_by, update_time FROM tb_equipment
    </sql>

    <select id="selectEquipmentList" parameterType="Equipment" resultMap="EquipmentResult">
        SELECT e.id, e.type, e.equipment_name, e.status,  e.equipment_num,e.ip,e.park_id,e.building_id,e.tenant_id, e.pic, e.delete_flag, e.create_by, e.create_time, e.update_by,
               e.update_time,e.park_id,e.tenant_id FROM tb_equipment e
        left join tb_room_equipment re on re.equipment_id = e.id
        <where>
            <if test="equipmentName != null  and equipmentName != ''"> AND e.equipment_name LIKE CONCAT('%', #{equipmentName}, '%')</if>
            <if test="parkId != null  and parkId != ''"> AND e.park_id = #{parkId}</if>
            <if test="tenantId != null  and tenantId != ''"> AND e.tenant_id = #{tenantId}</if>
            <if test="roomId != null  and roomId != ''"> AND re.room_id = #{roomId}</if>
            <if test="roomIds != null and roomIds.size() > 0">
                AND re.room_id IN
                <foreach collection="roomIds" item="roomId" open="(" separator="," close=")">
                    #{roomId}
                </foreach>
            </if>
            <if test="buildingDetailIds != null and buildingDetailIds.size() > 0">
                AND re.building_detail_id IN
                <foreach collection="buildingDetailIds" item="buildingDetailId" open="(" separator="," close=")">
                    #{buildingDetailId}
                </foreach>
            </if>
        </where>
    </select>
    
    <select id="selectEquipmentById" parameterType="Long" resultMap="EquipmentResult">
        <include refid="selectEquipmentVo"/>
        WHERE id = #{id}
    </select>
        
    <insert id="insertEquipment" parameterType="Equipment">
        INSERT INTO tb_equipment
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null ">id,</if>
            <if test="type != null ">type,</if>
            <if test="equipmentName != null  and equipmentName != ''">equipment_name,</if>
            <if test="status != null ">status,</if>
            <if test="createDate != null ">create_date,</if>
            <if test="equipmentNum != null  and equipmentNum != ''">equipment_num,</if>
            <if test="pic != null  and pic != ''">pic,</if>
            <if test="ip != null  and ip != ''">ip,</if>
            <if test="deleteFlag != null ">delete_flag,</if>
            <if test="createBy != null  and createBy != ''">create_by,</if>
            <if test="createTime != null ">create_time,</if>
            <if test="updateBy != null  and updateBy != ''">update_by,</if>
            <if test="updateTime != null ">update_time,</if>
            <if test="parkId != null and parkId != '' ">park_id,</if>
            <if test="buildingId != null and buildingId != '' ">building_id,</if>
            <if test="tenantId != null and tenantId != '' ">tenant_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null ">#{id},</if>
            <if test="type != null ">#{type},</if>
            <if test="equipmentName != null  and equipmentName != ''">#{equipmentName},</if>
            <if test="status != null ">#{status},</if>
            <if test="createDate != null ">#{createDate},</if>
            <if test="equipmentNum != null  and equipmentNum != ''">#{equipmentNum},</if>
            <if test="pic != null  and pic != ''">#{pic},</if>
            <if test="ip != null  and ip != ''">#{ip},</if>
            <if test="deleteFlag != null ">#{deleteFlag},</if>
            <if test="createBy != null  and createBy != ''">#{createBy},</if>
            <if test="createTime != null ">#{createTime},</if>
            <if test="updateBy != null  and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null ">#{updateTime},</if>
            <if test="parkId != null and parkId != '' ">parkId,</if>
            <if test="buildingId != null and buildingId != '' ">buildingId,</if>
            <if test="tenantId != null and tenantId != '' ">tenantId,</if>
         </trim>
    </insert>

    <update id="updateEquipment" parameterType="Equipment">
        UPDATE tb_equipment
        <trim prefix="SET" suffixOverrides=",">
            <if test="type != null ">type = #{type},</if>
            <if test="equipmentName != null  and equipmentName != ''">equipment_name = #{equipmentName},</if>
            <if test="status != null ">status = #{status},</if>
            <if test="equipmentNum != null  and equipmentNum != ''">equipment_num = #{equipmentNum},</if>
            <if test="parkId != null  and parkId != ''">park_id = #{parkId},</if>
            <if test="buildingId != null  and buildingId != ''">building_id = #{buildingId},</if>
            <if test="tenantId != null  and tenantId != ''">tenant_id = #{tenantId},</if>
            <if test="pic != null  and pic != ''">pic = #{pic},</if>
            <if test="deleteFlag != null ">delete_flag = #{deleteFlag},</if>
            <if test="ip != null ">ip = #{ip},</if>
        </trim>
        WHERE id = #{id}
    </update>

    <delete id="deleteEquipmentById" parameterType="Long">
        DELETE FROM tb_equipment WHERE id = #{id}
    </delete>

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