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

    <sql id="Base_Column_List">
        id
        ,station_id,src_id,device_col_id,col,col_name,`type`
    </sql>

    <select id="selectVirtualDeviceCol" resultType="com.ho.business.vo.resp.VirtualDeviceColResp">
        select
        <include refid="Base_Column_List"/>,factor,offset_value
        FROM (select a.*,b.factor,b.offset_value from virtual_device_col a left join device_type_col b on
        a.device_col_id = b.id ) col
        <where>
            <if test="id != null">
                and id = #{id,jdbcType=INTEGER}
            </if>
            <if test="stationId != null">
                and station_id = #{stationId,jdbcType=INTEGER}
            </if>
            <if test="srcId != null">
                and src_id = #{srcId,jdbcType=INTEGER}
            </if>
            <if test="srcIdList != null and srcIdList.size != 0">
                and src_id in
                <foreach collection="srcIdList" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
            <if test="type != null">
                and type = #{type}
            </if>
            <if test="typeList != null and typeList.size != 0">
                and `type` in
                <foreach collection="typeList" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
            <if test="deviceColId != null">
                and device_col_id = #{deviceColId,jdbcType=INTEGER}
            </if>
            <if test="name != null">
                and (col_name LIKE concat('%',#{name},'%') or col LIKE concat('%',#{name},'%'))
            </if>
        </where>
    </select>

    <select id="selectByColId" resultType="com.ho.business.entity.VirtualDeviceCol">
        select
        <include refid="Base_Column_List"/>
        FROM virtual_device_col
        where
        station_id = #{stationId,jdbcType=INTEGER}
        and device_col_id = #{deviceColId,jdbcType=INTEGER}

    </select>

    <select id="selectBySrcId" resultType="com.ho.business.entity.VirtualDeviceCol">
        select
        <include refid="Base_Column_List"/>
        FROM virtual_device_col
        <where>
            station_id = #{stationId}
            <if test="collect != null and collect.size() != 0">
                and src_id in
                <foreach collection="collect" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
            <if test="type != null">
                and (type LIKE concat('%',#{type},'%'))
            </if>
        </where>
    </select>

    <insert id="insertVirtualDeviceCol" keyColumn="id" keyProperty="id"
            parameterType="com.ho.business.entity.VirtualDeviceCol" useGeneratedKeys="true">
        insert into virtual_device_col
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="stationId != null">station_id,</if>
            <if test="srcId != null">src_id,</if>
            <if test="deviceColId != null">device_col_id,</if>
            <if test="col != null">col,</if>
            <if test="colName != null">col_name,</if>
            <if test="type != null">`type`,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id,jdbcType=INTEGER},</if>
            <if test="stationId != null">#{stationId,jdbcType=INTEGER},</if>
            <if test="srcId != null">#{srcId,jdbcType=INTEGER},</if>
            <if test="deviceColId != null">#{deviceColId,jdbcType=INTEGER},</if>
            <if test="col != null">#{col,jdbcType=VARCHAR},</if>
            <if test="colName != null">#{colName,jdbcType=VARCHAR},</if>
            <if test="type != null">#{type,jdbcType=VARCHAR},</if>
        </trim>
    </insert>

    <insert id="insertList" keyColumn="id" keyProperty="id" parameterType="java.util.List" useGeneratedKeys="true">
        insert into virtual_device_col (station_id,src_id,device_col_id,col,col_name,`type`) values
        <foreach collection="virtualDeviceColList" item="item" index="index" separator=",">
            (#{item.stationId},#{item.srcId},#{item.deviceColId},#{item.col},#{item.colName},#{item.type})
        </foreach>
    </insert>

    <update id="updateVirtualDeviceCol" parameterType="com.ho.business.entity.VirtualDeviceCol">
        update virtual_device_col
        <set>
            <if test="stationId != null">
                station_id = #{stationId,jdbcType=INTEGER},
            </if>
            <if test="srcId != null">
                src_id = #{srcId,jdbcType=INTEGER},
            </if>
            <if test="deviceColId != null">
                device_col_id = #{deviceColId,jdbcType=INTEGER},
            </if>
            <if test="col != null">
                col = #{col,jdbcType=VARCHAR},
            </if>
            <if test="colName != null">
                col_name = #{colName,jdbcType=VARCHAR},
            </if>
            <if test="type != null">
                `type` = #{type,jdbcType=VARCHAR},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>

    <update id="updateList" keyColumn="id" keyProperty="id" parameterType="java.util.List" useGeneratedKeys="true">
        <foreach collection="virtualDeviceColList" item="item" index="index" separator=";">
            update virtual_device_col
            <set>
                <if test="stationId != null">
                    station_id = #{stationId,jdbcType=INTEGER},
                </if>
                <if test="srcId != null">
                    src_id = #{srcId,jdbcType=INTEGER},
                </if>
                <if test="deviceColId != null">
                    device_col_id = #{deviceColId,jdbcType=INTEGER},
                </if>
                <if test="col != null">
                    col = #{col,jdbcType=VARCHAR},
                </if>
                <if test="colName != null">
                    col_name = #{colName,jdbcType=VARCHAR},
                </if>
                <if test="type != null">
                    `type` = #{type,jdbcType=VARCHAR},
                </if>
            </set>
            where id = #{id,jdbcType=INTEGER}
        </foreach>
    </update>

    <delete id="deleteByPrimaryKey">
        delete
        from virtual_device_col
        where id = #{id,jdbcType=INTEGER}
    </delete>

    <delete id="deleteByList">
        delete
        from virtual_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>
</mapper>
