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

    <resultMap id="BaseResultMap" type="com.ho.business.entity.DeviceTypeCol">
        <id property="id" column="id" jdbcType="INTEGER"/>
        <result property="deviceTypeId" column="device_type_id" jdbcType="INTEGER"/>
        <result property="deviceType" column="device_type" jdbcType="VARCHAR"/>
        <result property="deviceTypeName" column="device_type_name" jdbcType="VARCHAR"/>
        <result property="col" column="col" jdbcType="VARCHAR"/>
        <result property="colName" column="col_name" jdbcType="VARCHAR"/>
        <result property="sensType" column="sens_type" jdbcType="INTEGER"/>
        <result property="isShow" column="is_show" jdbcType="INTEGER"/>
        <result property="isSave" column="is_save" jdbcType="INTEGER"/>
        <result property="maxValue" column="max_value"/>
        <result property="minValue" column="min_value"/>
        <result property="specifiedValue" column="specified_value"/>
        <result property="offsetValue" column="offset_value"/>
        <result property="factor" column="factor"/>
        <result property="unit" column="unit"/>
        <result property="dataType" column="data_type" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id
        ,device_type_id,device_type,
        device_type_name,col,col_name,col_en,sens_type,is_show,is_save,
        max_value,min_value,specified_value,offset_value,factor,unit,
        data_type,filter_type
    </sql>

    <sql id="Base_Column_List1">
        id
        ,device_type_id,device_type,
        device_type_name,col,col_en colName,col_en,sens_type,is_show,is_save,
        max_value,min_value,specified_value,offset_value,factor,unit,
        data_type,filter_type
    </sql>

    <sql id="Base_List">
        id
        ,device_type_id,device_type,
        device_type_name,col,col_name,sens_type,is_show,is_save,
        max_value,min_value,specified_value,offset_value,factor,unit
    </sql>


    <delete id="deleteByPrimaryKey">
        delete
        from device_type_col
        where id = #{id}
    </delete>

    <delete id="deleteBatchDevice">
        delete
        from device_type_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="deleteBatchModel">
        delete
        from model_type_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>

    <select id="selectByDeviceType" resultType="com.ho.business.entity.DeviceTypeCol">
        select
        <choose>
            <when test="lang != null and lang=='en_US' ">
                <include refid="Base_Column_List1"/>
            </when>
            <otherwise>
                <include refid="Base_Column_List"/>
            </otherwise>
        </choose>
        from device_type_col
        <where>
            <if test="deviceType != null">
                and device_type=#{deviceType}
            </if>
        </where>
        ORDER BY id asc
    </select>

    <select id="selectByParam" resultType="com.ho.business.entity.DeviceTypeCol">
        select
        <include refid="Base_Column_List"/>
        from device_type_col
        <where>
            <if test="deviceTypeCol.deviceType != null">
                and device_type=#{deviceTypeCol.deviceType}
            </if>
            <if test="deviceTypeCol.col != null">
                and col LIKE concat('%',#{deviceTypeCol.col},'%')
            </if>
            <if test="deviceTypeCol.colName != null">
                and col_name LIKE concat('%',#{deviceTypeCol.colName},'%')
            </if>
        </where>
        ORDER BY id asc
    </select>

    <select id="selectByDeviceTypeList" resultType="com.ho.business.entity.DeviceTypeCol">
        select
        <choose>
            <when test="deviceTypeColReqVO.lang != null and deviceTypeColReqVO.lang=='en_US' ">
                <include refid="Base_Column_List1"/>
            </when>
            <otherwise>
                <include refid="Base_Column_List"/>
            </otherwise>
        </choose>
        from device_type_col
        <where>
            <if test="deviceTypeColReqVO.deviceTypeList != null and deviceTypeColReqVO.deviceTypeList.size !=0">
                and device_type in
                <foreach collection="deviceTypeColReqVO.deviceTypeList" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="deviceTypeColReqVO.isSave != null">
                and is_save = #{deviceTypeColReqVO.isSave}
            </if>
            <if test="deviceTypeColReqVO.name != null">
                and (col_name LIKE concat('%',#{deviceTypeColReqVO.name},'%') or col_en LIKE concat('%',#{deviceTypeColReqVO.name},'%') or col LIKE
                concat('%',#{deviceTypeColReqVO.name},'%'))
            </if>
            <if test="deviceTypeColReqVO.sensType != null">
                and sens_type = #{deviceTypeColReqVO.sensType}
            </if>
        </where>
    </select>

    <select id="selectByDeviceTypeAndIsShow" resultType="com.ho.business.entity.DeviceTypeCol">
        select
        <include refid="Base_Column_List"/>
        from device_type_col
        <where>
            <if test="deviceType != null">
                and device_type = #{deviceType}
            </if>
            <if test="isShow != null">
                and is_show = #{isShow}
            </if>
        </where>
    </select>

    <select id="selectByIds" resultType="com.ho.business.entity.DeviceTypeCol">
        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="getCompListByType" resultType="com.ho.business.entity.ModelDeviceColComp">
        select r.id, r.device_col_id,r.device_type, d.col as device_col, d.col_name as device_col_name,
        r.model_col_id ,r.model_type, m.col as model_col, m.col_name as model_col_name,d.unit,d.sens_type
        from model_device_col r
        left join
        device_type_col d
        on r.device_col_id = d.id
        left join
        model_type_col m
        on r.model_col_id = m.id
        <where>
            <if test="modelCol !=null">
                and m.col = #{modelCol}
            </if>
            <choose>
                <when test="type =='device'">
                    and r.device_type = #{typeName}
                </when>
                <when test="type =='model'">
                    and r.model_type = #{typeName}
                </when>
            </choose>
        </where>
    </select>

    <select id="selectSameCol" resultType="com.ho.business.entity.DeviceTypeCol">
        select
        <choose>
            <when test="lang != null and lang=='en_US' ">
                <include refid="Base_Column_List1"/>
            </when>
            <otherwise>
                <include refid="Base_Column_List"/>
            </otherwise>
        </choose>
        from device_type_col
        <where>
            <if test="deviceType != null">
                and device_type=#{deviceType}
            </if>
            <if test="list != null and list.size !=0">
                and col in
                <foreach collection="list" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
    <select id="selectTypeAndCol" resultType="com.ho.business.entity.DeviceTypeCol">
        select
        <choose>
            <when test="lang != null and lang=='en_US' ">
                <include refid="Base_Column_List1"/>
            </when>
            <otherwise>
                <include refid="Base_Column_List"/>
            </otherwise>
        </choose>
        from device_type_col
        <where>
            <if test="deviceType != null">
                and device_type = #{deviceType}
            </if>
            <if test="col != null">
                and col = #{col}
            </if>
        </where>
    </select>

    <select id="selectCountByDeviceType" resultType="java.lang.Integer">
        select count(1)
        from device_type_col
        <where>
            <if test="deviceTypeCol.deviceType != null">
                and device_type = #{deviceTypeCol.deviceType}
            </if>
        </where>
    </select>

    <select id="selectDimType" resultType="com.ho.business.entity.DeviceTypeCol">
        select
        <include refid="Base_Column_List"/>
        from device_type_col
        <where>
            <if test="deviceType != null">
                and device_type LIKE concat('%',#{deviceType},'%')
            </if>
        </where>
    </select>

    <select id="selectType" resultType="java.lang.String">
        SELECT device_type
        FROM `device_type_col`
        where
        <if test="collect != null and collect.size != 0 ">
            device_type in
            <foreach collection="collect" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
        group by device_type
    </select>

    <select id="selectById" resultType="com.ho.business.entity.DeviceTypeCol">
        SELECT
        <include refid="Base_Column_List"/>
        FROM `device_type_col`
        where id = #{id}
    </select>

    <select id="selectAllData" resultType="com.ho.business.entity.DeviceTypeCol">
        SELECT
        <include refid="Base_Column_List"/>
        from device_type_col
        where id >= #{colBeginId} limit #{allNum}
    </select>

    <select id="selectByBatchId" resultType="com.ho.business.entity.DeviceTypeCol">
        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>


    <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.ho.business.entity.DeviceTypeCol"
            useGeneratedKeys="true">
        insert into device_type_col
        (id, device_type_id, device_type,
         device_type_name, col, col_name, sens_type, is_show, is_save,
         max_value, min_value, specified_value, offset_value, factor, unit
             data_type)
        values ( #{id}, #{device_type_id}, #{device_type}
               , #{device_type_name},
               , #{col}, #{col_name}, #{sens_type}, #{is_show}, #{is_save}
               , #{max_value}, #{min_value}, #{specified_value}, #{offset_value}, #{factor}, #{unit}, #{data_type})
    </insert>

    <insert id="insertList" keyColumn="id" keyProperty="id" parameterType="java.util.List" useGeneratedKeys="true">
        insert into device_type_col
        (device_type_id,device_type,device_type_name,col,col_name,col_en,sens_type,is_show,is_save,max_value,min_value,offset_value,factor,unit)
        values
        <foreach collection="deviceTypeColList" item="item" index="index" separator=",">
            (#{item.deviceTypeId},#{item.deviceType},#{item.deviceTypeName},#{item.col},#{item.colName},#{item.colEn},#{item.sensType},#{item.isShow},#{item.isSave},#{item.maxValue},#{item.minValue},#{item.offsetValue},#{item.factor},#{item.unit})
        </foreach>
    </insert>

    <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.ho.business.entity.DeviceTypeCol"
            useGeneratedKeys="true">
        insert into device_type_col
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="deviceTypeId != null">device_type_id,</if>
            <if test="deviceType != null">device_type,</if>
            <if test="deviceTypeName != null">device_type_name,</if>
            <if test="col != null">col,</if>
            <if test="colName != null">col_name,</if>
            <if test="colEn != null">col_en,</if>
            <if test="sensType != null">sens_type,</if>
            <if test="isShow != null">is_show,</if>
            <if test="isSave != null">is_save,</if>
            <if test="maxValue != null">max_value,</if>
            <if test="minValue != null">min_value,</if>
            <if test="specifiedValue != null">specified_value,</if>
            <if test="offsetValue != null">offset_value,</if>
            <if test="factor != null">factor,</if>
            <if test="unit != null">unit,</if>
            <if test="dataType != null">data_type</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="deviceTypeId != null">#{deviceTypeId},</if>
            <if test="deviceType != null">#{deviceType},</if>
            <if test="deviceTypeName != null">#{deviceTypeName},</if>
            <if test="col != null">#{col},</if>
            <if test="colName != null">#{colName},</if>
            <if test="colEn != null">#{colEn},</if>
            <if test="sensType != null">#{sensType},</if>
            <if test="isShow != null">#{isShow},</if>
            <if test="isSave != null">#{isSave},</if>
            <if test="maxValue != null">#{maxValue},</if>
            <if test="minValue != null">#{minValue},</if>
            <if test="specifiedValue != null">#{specifiedValue},</if>
            <if test="offsetValue != null">#{offsetValue},</if>
            <if test="factor != null">#{factor},</if>
            <if test="unit != null">#{unit},</if>
            <if test="dataType != null">#{dataType},</if>

        </trim>
    </insert>

    <insert id="addBatchDevice" keyColumn="id" keyProperty="id" useGeneratedKeys="true">
        insert into device_type_col (<include refid="Base_List"/>)
        values
        <foreach item="item" collection="list" index="index" separator=",">
            (
            #{item.id},
            #{item.deviceTypeId},#{item.deviceType},
            #{item.deviceTypeName},#{item.col},#{item.colName},#{item.sensType}, #{item.isShow}, #{item.isSave},
            #{item.maxValue}, #{item.minValue}, #{item.specifiedValue}, #{item.offsetValue}, #{item.factor},
            #{item.unit}
            )
        </foreach>

    </insert>


    <update id="updateByPrimaryKeySelective" parameterType="com.ho.business.entity.DeviceTypeCol">
        update device_type_col
        <set>
            <if test="deviceTypeId != null">
                device_type_id= #{deviceTypeId},
            </if>
            <if test="deviceType != null">
                device_type=#{deviceType},
            </if>
            <if test="deviceTypeName != null">
                device_type_name = #{deviceTypeName},
            </if>
            <if test="col != null">
                col = #{col},
            </if>
            <if test="colName != null">
                col_name = #{colName},
            </if>
            <if test="sensType != null">
                sens_type = #{sensType},
            </if>
            <if test="isShow != null">
                is_show = #{isShow},
            </if>
            <if test="isSave != null">
                is_save = #{isSave},
            </if>
            <if test="maxValue != null">
                max_value = #{maxValue},
            </if>
            <if test="minValue != null">
                min_value = #{minValue},
            </if>
            <if test="specifiedValue != null">
                specified_value = #{specifiedValue},
            </if>
            <if test="offsetValue != null">
                offset_value = #{offsetValue},
            </if>
            <if test="offsetValue != null">
                offset_value = #{offsetValue},
            </if>
            <if test="factor != null">
                factor = #{factor},
            </if>
            <if test="unit != null">
                unit = #{unit},
            </if>
            <if test="dataType != null">
                data_type = #{dataType},
            </if>

        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>


    <update id="updateByPrimaryKey">
        update device_type_col
        set device_type_id= #{deviceTypeId},
            device_type=#{deviceType},
            device_type_name = #{deviceTypeName},
            col = #{col},
            col_name = #{colName},
            sens_type = #{sensType},
            is_show = #{isShow},
            is_save = #{isSave},
            max_value = #{maxValue},
            min_value = #{minValue},
            specified_value = #{specifiedValue},
            offset_value = #{offsetValue},
            offset_value = #{offsetValue},
            factor = #{factor},
            unit = #{unit},
            data_type = #{dataType}
        where id = #{id}
    </update>

    <update id="updateList" keyColumn="id" keyProperty="id" parameterType="java.util.List" useGeneratedKeys="true">
        <foreach collection="deviceTypeColList" item="item" index="index" separator=";">
            update device_type_col set device_type_id = #{item.deviceTypeId},device_type = #{item.deviceType},
            device_type_name = #{item.deviceTypeName},
            col = #{item.col},col_name = #{item.colName},col_en = #{item.colEn},sens_type = #{item.sensType}, is_show =
            #{item.isShow}, is_save = #{item.isSave},
            max_value = #{item.maxValue}, min_value = #{item.minValue}, unit = #{item.unit}
            where id = #{item.id}
        </foreach>
    </update>

    <select id="countByDeviceType" resultType="com.ho.business.vo.resp.deviceTypeCol.DeviceTypeColCountResp">
        select count(1) as num,device_type
        from device_type_col
        <where>
            <if test="typeList != null and typeList.size !=0">
                and device_type in
                <foreach collection="typeList" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY device_type
    </select>

</mapper>
