<?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.ho.business.mapper.ModelTypeMapper">

    <resultMap id="BaseResultMap" type="com.ho.business.entity.ModelType">
        <id property="id" column="id" jdbcType="INTEGER"/>
        <result property="modelType" column="model_type" jdbcType="VARCHAR"/>
        <result property="modelName" column="model_name" jdbcType="INTEGER"/>
    </resultMap>

    <sql id="Base_Column_List">
        id
        ,model_type,model_name
    </sql>

    <insert id="insertModelType" keyColumn="id" keyProperty="id" parameterType="com.ho.business.entity.ModelType"
            useGeneratedKeys="true">
        insert into model_type
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="modelType != null">model_type,</if>
            <if test="modelName != null">model_name,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id,jdbcType=INTEGER},</if>
            <if test="modelType != null">#{modelType,jdbcType=VARCHAR},</if>
            <if test="modelName != null">#{modelName,jdbcType=VARCHAR},</if>
        </trim>
    </insert>

    <select id="getModelTypeList" resultType="com.ho.business.entity.ModelType">
        select
        <include refid="Base_Column_List"/>
        from model_type
        <where>
            <if test="modelType.modelType != null">
                model_type = #{modelType.modelType}
            </if>
            <if test="modelType.modelName != null">
                model_name LIKE concat('%',#{modelType.modelName},'%')
            </if>
        </where>
    </select>

    <select id="selectByModelType" resultType="com.ho.business.entity.ModelType">
        select
        <include refid="Base_Column_List"/>
        from model_type
        where model_type=#{model_type}
    </select>

    <select id="getModelType" resultType="com.ho.business.entity.ModelType">
        select
        <include refid="Base_Column_List"/>
        from model_type
        <where>
            <if test="modelType != null">
                model_type = #{modelType}
            </if>
        </where>
    </select>

    <select id="getDeviceType" resultType="com.ho.business.entity.ModelDevice">
        select *
        from model_device
        where model_type = #{model_type}
    </select>

    <select id="selectByDict" resultType="com.ho.business.entity.ModelDevice">
        select *
        from model_device
        where model_type = #{modelType}
          and device_type = #{deviceType}

    </select>

    <select id="selectByDeviceType" resultType="com.ho.business.entity.ModelDevice">
        select * from model_device where `device_type` like concat('%',#{deviceType})
    </select>

    <insert id="insertModelDevice" keyColumn="id" keyProperty="id" parameterType="com.ho.business.entity.ModelDevice"
            useGeneratedKeys="true">
        insert into model_device
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="modelType != null">model_type,</if>
            <if test="deviceType != null">device_type,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id,jdbcType=INTEGER},</if>
            <if test="modelType != null">#{modelType,jdbcType=VARCHAR},</if>
            <if test="deviceType != null">#{deviceType,jdbcType=VARCHAR},</if>
        </trim>
    </insert>

    <insert id="insertModelDeviceList" keyColumn="id" keyProperty="id" useGeneratedKeys="true">
        insert into model_device (`model_type`,`device_type`)
        values
        <foreach item="item" collection="list" index="index" separator=",">
            (
            #{item.modelType},#{item.deviceType}
            )
        </foreach>
    </insert>

    <update id="updateModelDevice" parameterType="com.ho.business.entity.ModelDevice">
        update `model_device`
        <set>
            <if test="modelType != null">
                model_type = #{modelType},
            </if>
            <if test="deviceType != null">
                device_type = #{deviceType},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>

    <delete id="deleteModelDevice">
        delete from `model_device`
        <where>
            <if test="ids != null and ids.size !=0">
                and id in
                <foreach collection="ids" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
        </where>
    </delete>

    <delete id="deleteModelDeviceByDeviceType">
        delete from `model_device` where `device_type` like concat('%',#{deviceType})
    </delete>

</mapper>
