<?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.flow.mapper.DeviceColDefineMapper">


    <sql id="Base_Column_List">
        `id`
        , `device_type_id`, `device_name`, `device_type`, `col`,
        `col_name`, `sens_type`,info0,info1
    </sql>


    <select id="selectPage" resultType="com.ho.flow.vo.DeviceColDefine">
        select
        <include refid="Base_Column_List"/>
        from device_col_define
        <where>
            <!--<if test="deviceType != null">
                and `device_type` LIKE CONCAT('%',#{deviceType},'%')
            </if>-->
            <if test="deviceType != null">
                and `device_type` = #{deviceType}
            </if>
            <if test="sensType != null">
                and sens_type = #{sensType}
            </if>
            <if test="col != null and col!=''">
                and `col` LIKE CONCAT('%',#{col},'%')
            </if>
            <if test="colName != null and colName!=''">
                and `col_name` LIKE CONCAT('%',#{colName},'%')
            </if>
            <if test="colNameList != null and colNameList.size >0 ">
                <foreach collection="colNameList" item="item" open=" " close=" " separator=" ">
                    and `col_name` LIKE CONCAT('%',#{item},'%')
                </foreach>
            </if>
            <if test="name != null and name!=''">
                and (col_name LIKE concat('%',#{name},'%') or col LIKE concat('%',#{name},'%'))
            </if>
            <if test="deviceTypeList != null and deviceTypeList.size > 0">
                and device_type in
                <foreach collection="deviceTypeList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="deviceTypeSuffix !=null and deviceTypeSuffix !='' ">
                and device_type like concat('%',#{deviceTypeSuffix})
            </if>
        </where>
    </select>

    <select id="selectById" resultType="com.ho.flow.vo.DeviceColDefine">
        select
        <include refid="Base_Column_List"/>
        from device_col_define
        where id = #{id}
    </select>

    <select id="selectByIdList" resultType="com.ho.flow.vo.DeviceColDefine">
        select
        <include refid="Base_Column_List"/>
        from device_col_define
        <where>
            <if test="deviceType!=null">
                and device_type = #{deviceType}
            </if>
            <if test="ids!=null and ids.size()!=0">
                and id in
                <foreach collection="ids" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>

    </select>

    <select id="selectByIdAndDeviceTypeId" resultType="com.ho.flow.vo.DeviceColDefine">
        select
        <include refid="Base_Column_List"/>
        from device_col_define
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="deviceTypeId != null">
                and device_type_id = #{deviceTypeId}
            </if>
        </where>
    </select>

    <select id="selectByColAndDeviceType" resultType="com.ho.flow.vo.DeviceColDefine">
        select
        <include refid="Base_Column_List"/>
        from device_col_define
        <where>
            <if test="col != null">
                and col = #{col}
            </if>
            <if test="deviceType != null">
                and device_type = #{deviceType}
            </if>

        </where>
    </select>
    <select id="selectByDeviceType" resultType="com.ho.flow.vo.DeviceColDefine">
        select
        <include refid="Base_Column_List"/>
        from device_col_define
        where
        `device_type` LIKE CONCAT('%',#{deviceType},'%')
    </select>

    <insert id="insertList" keyColumn="id" keyProperty="id" parameterType="java.util.List" useGeneratedKeys="true">
        insert into device_col_define (device_type_id,device_name,device_type,col,col_name,col_en,lang,sens_type,info0,info1) values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.deviceTypeId},#{item.deviceName},#{item.deviceType},#{item.col},#{item.colName},#{item.colEn},#{item.lang},#{item.sensType},#{item.info0},#{item.info1})
        </foreach>
    </insert>

    <update id="updateList" keyColumn="id" keyProperty="id" parameterType="java.util.List" useGeneratedKeys="true">
        <foreach collection="list" item="item" index="index" separator=";">
            update device_col_define set device_type_id = #{item.deviceTypeId}, device_name = #{item.deviceName},
            device_type = #{item.deviceType}, col = #{item.col},col_name = #{item.colName},
            col_en = #{item.colEn},lang = #{item.lang}, sens_type = #{item.sensType}, info0 = #{item.info0}, info1 = #{item.info1}
            where id = #{item.id}
        </foreach>
    </update>

    <insert id="insertDeviceColDefine" keyColumn="id" keyProperty="id" parameterType="com.ho.flow.vo.DeviceColDefine"
            useGeneratedKeys="true">
        insert into device_col_define
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceTypeId != null">device_type_id,</if>
            <if test="deviceName != null">device_name,</if>
            <if test="deviceType != null">device_type,</if>
            <if test="col != null">col,</if>
            <if test="colName != null">col_name,</if>
            <if test="sensType != null">sens_type,</if>
            <if test="info0 != null">info0,</if>
            <if test="info1 != null">info1,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceTypeId != null">#{deviceTypeId},</if>
            <if test="deviceName != null">#{deviceName},</if>
            <if test="deviceType != null">#{deviceType},</if>
            <if test="col != null">#{col},</if>
            <if test="colName != null">#{colName},</if>
            <if test="sensType != null">#{sensType},</if>
            <if test="info0 != null">#{info0},</if>
            <if test="info1 != null">#{info1},</if>
        </trim>
    </insert>

    <update id="updateDeviceColDefine" parameterType="com.ho.flow.vo.DeviceColDefine">
        update device_col_define
        <set>
            <if test="deviceName != null">
                device_name = #{deviceName},
            </if>
            <if test="deviceType != null">
                device_type = #{deviceType},
            </if>
            <if test="colName != null">
                col_name = #{colName},
            </if>
            <if test="sensType != null">
                sens_type = #{sensType},
            </if>
            <if test="info0 != null">
                info0 = #{info0},
            </if>
            <if test="info1 != null">
                info1 =#{info1},
            </if>
        </set>
        where id = #{id}
    </update>

    <delete id="deleteDeviceColDefine">
        delete
        from device_col_define
        <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>

</mapper>
