<?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.xinda.iot.mapper.DeviceModelMapper">

    <resultMap type="DeviceModel" id="DeviceModelResult">
        <result property="id"    column="id"    />
        <result property="productModel"    column="product_model"    />
        <result property="displayOrder"    column="display_order"    />
        <result property="createdAt"    column="created_at"    />
        <result property="remarks"    column="remarks"    />
    </resultMap>

    <sql id="selectDeviceModelVo">
        select id, product_model, display_order, created_at, remarks from iot_device_model
    </sql>

    <select id="selectDeviceModelList" parameterType="DeviceModel" resultMap="DeviceModelResult">
        <include refid="selectDeviceModelVo"/>
        <where>
            <if test="productModel != null  and productModel != ''"> and product_model = #{productModel}</if>
            <if test="displayOrder != null "> and display_order = #{displayOrder}</if>
            <if test="createdAt != null "> and created_at = #{createdAt}</if>
            <if test="remarks != null  and remarks != ''"> and remarks = #{remarks}</if>
        </where>
        order by display_order asc
    </select>

    <select id="selectDeviceModelById" parameterType="Long" resultMap="DeviceModelResult">
        <include refid="selectDeviceModelVo"/>
        where id = #{id}
    </select>

    <insert id="insertDeviceModel" parameterType="DeviceModel" useGeneratedKeys="true" keyProperty="id">
        insert into iot_device_model
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="productModel != null and productModel != ''">product_model,</if>
            <if test="displayOrder != null">display_order,</if>
            <if test="createdAt != null">created_at,</if>
            <if test="remarks != null">remarks,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="productModel != null and productModel != ''">#{productModel},</if>
            <if test="displayOrder != null">#{displayOrder},</if>
            <if test="createdAt != null">#{createdAt},</if>
            <if test="remarks != null">#{remarks},</if>
         </trim>
    </insert>

    <update id="updateDeviceModel" parameterType="DeviceModel">
        update iot_device_model
        <trim prefix="SET" suffixOverrides=",">
            <if test="productModel != null and productModel != ''">product_model = #{productModel},</if>
            <if test="displayOrder != null">display_order = #{displayOrder},</if>
            <if test="createdAt != null">created_at = #{createdAt},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDeviceModelById" parameterType="Long">
        delete from iot_device_model where id = #{id}
    </delete>

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