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

    <resultMap id="BaseResultMap" type="com.ho.business.entity.ModelDeviceCol">
        <id property="id" column="id" jdbcType="INTEGER"/>
        <result property="deviceType" column="device_type" jdbcType="VARCHAR"/>
        <result property="deviceColId" column="device_col_id" jdbcType="INTEGER"/>
        <result property="modelType" column="model_type" jdbcType="VARCHAR"/>
        <result property="modelColId" column="model_col_id" jdbcType="INTEGER"/>
    </resultMap>

    <sql id="Base_Column_List">
        id
        ,device_type,device_col_id,
        model_type,model_col_id
    </sql>

    <insert id="insert">
        insert into model_device_col
        (id, device_type, device_col_id,
         model_type, model_col_id)
        values ( #{id}, #{device_type}, #{device_col_id}
               , #{model_type}, #{model_col_id})
    </insert>

    <insert id="insertSelective">
        insert into model_device_col
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="deviceType != null">device_type,</if>
            <if test="deviceColId != null">device_col_id,</if>
            <if test="modelType != null">model_type,</if>
            <if test="modelColId != null">model_col_id</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="deviceType != null">#{deviceType},</if>
            <if test="deviceColId != null">#{deviceColId},</if>
            <if test="modelType != null">#{modelType},</if>
            <if test="modelColId != null">#{modelColId}</if>
        </trim>
    </insert>

    <insert id="addBatch" keyColumn="id" keyProperty="id" useGeneratedKeys="true">
        insert into model_device_col (<include refid="Base_Column_List"/>)
        values
        <foreach item="item" collection="list" index="index" separator=",">
            (
            #{item.id},
            #{item.deviceType},#{item.deviceColId},
            #{item.modelType},#{item.modelColId}
            )
        </foreach>
    </insert>


    <insert id="insertList" keyColumn="id" keyProperty="id" parameterType="java.util.List" useGeneratedKeys="true">
        insert into model_device_col (device_type,device_col_id,model_type,model_col_id) values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.deviceType},#{item.deviceColId},#{item.modelType},#{item.modelColId})
        </foreach>
    </insert>

    <update id="updateByPrimaryKeySelective">
        update model_device_col
        <set>
            <if test="deviceType != null">
                device_type=#{deviceType}
            </if>
            <if test="deviceTypeId != null">
                device_col_id= #{deviceColId}
            </if>
            <if test="modelType != null">
                model_type = #{modelType}
            </if>
            <if test="modelColId != null">
                model_col_id = #{modelColId}
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>

    <delete id="deleteByPrimaryKey">
        delete
        from model_device_col
        where id = #{id}
    </delete>
    <delete id="deleteBatchDevice">
        delete
        from model_device_col
        <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="deleteByDeviceType">
        delete
        from model_device_col
        where device_type like concat('%',#{deviceType})
    </delete>

    <select id="selectByDeviceType" resultType="com.ho.business.entity.ModelDeviceCol">
        select
        <include refid="Base_Column_List"/>
        from model_device_col
        where device_type=#{deviceType}
        ORDER BY id desc
    </select>

    <select id="selectByIds" resultType="com.ho.business.entity.ModelDeviceCol">
        select
        <include refid="Base_Column_List"/>
        from device_type_col
        <where>
            <if test="ids != null and ids.size !=0">
                and id in
                <foreach collection="ids" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>


    <select id="selectByDeviceIds" resultType="com.ho.business.entity.ModelDeviceCol">
        select
        <include refid="Base_Column_List"/>
        from model_device_col
        <where>
            <if test="deviceType != null">
                and device_type=#{deviceType}
            </if>
            <if test="ids != null and ids.size !=0">
                and device_col_id in
                <foreach collection="ids" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectByModelIds" resultType="com.ho.business.entity.ModelDeviceCol">
        select
        <include refid="Base_Column_List"/>
        from model_device_col
        <where>
            <if test="deviceType != null">
                and device_type=#{deviceType}
            </if>
            <if test="ids != null and ids.size !=0">
                and model_col_id in
                <foreach collection="ids" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectByDeviceTypeList" resultType="com.ho.business.entity.ModelDeviceCol">
        select
        <include refid="Base_Column_List"/>
        from model_device_col
        <where>
            <if test="list != null and list.size !=0">
                and device_type in
                <foreach collection="list" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectByModelType" resultType="com.ho.business.entity.ModelDeviceCol">
        select
        <include refid="Base_Column_List"/>
        from model_device_col
        <where>
            <if test="type != null">
                and model_type=#{type}
            </if>
            <if test="ids != null and ids.size !=0">
                and model_col_id in
                <foreach collection="ids" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectByType" resultType="com.ho.business.entity.ModelDeviceCol">
        select
        <include refid="Base_Column_List"/>
        from model_device_col
        <where>
            <if test="deviceType != null">
                and device_type=#{deviceType}
            </if>
            <if test="modelType != null">
                and model_type =#{modelType}
            </if>
            <if test="deviceColId != null">
                and device_col_id= #{deviceColId}
            </if>
            <if test="modelColId != null">
                and model_col_id = #{modelColId}
            </if>
        </where>
    </select>

    <select id="selectByModelTypes" resultType="com.ho.business.entity.ModelDevice">
        select * from model_device
        <where>
            <if test="modelType != null">
                and model_type =#{modelType}
            </if>
        </where>
    </select>

    <select id="selectByModelId" resultType="com.ho.business.entity.ModelDeviceCol">
        select *
        from model_device_col
        where
              device_type = #{deviceType}
             and model_col_id = #{modelColId}
    </select>

    <select id="selectByParam" resultType="com.ho.business.vo.resp.deviceModel.ModelDeviceColRespVo">
        select m.id,m.device_type,m.device_col_id,m.model_type,m.model_col_id,d.col as deviceCol,d.col_name as deviceColIdName
        from model_device_col m left join device_type_col d on m.device_col_id = d.id
        where m.device_type like concat('%',#{deviceType})
    </select>


    <select id="selectDeviceColsByIds" resultType="com.ho.business.entity.ModelDeviceCol">
        select
        <include refid="Base_Column_List"/>
        from model_device_col
        <where>
            <if test="ids != null and ids.size !=0">
                and model_col_id in
                <foreach collection="ids" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="deviceTypeList != null and deviceTypeList.size !=0">
                and device_type in
                <foreach collection="deviceTypeList" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectModelData" resultType="com.ho.business.vo.resp.modelType.ModelColData">
        SELECT d.id,d.device_type,t.col FROM `model_device_col` d left join model_type_col t on d.model_col_id = t.id
        <where>
            <if test="ids != null and ids.size !=0">
                and d.id in
                <foreach collection="ids" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getStationModelColCount" resultType="java.lang.Integer">
            select count(0) from model_device_col t1,device t2
            where t1.device_type = t2.device_type
              and t2.station_id = #{stationId} and t1.model_col_id = #{modelId}
    </select>
</mapper>
