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

    <sql id="Base_Column_List">
        id,station_id,device_id,col,relation_device_id,relation_col,count_method
    </sql>

    <insert id="insertOne" parameterType="com.ho.business.entity.VirtualDeviceRelation">
        insert into virtual_device_relation
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="stationId != null">station_id,</if>
            <if test="deviceType != null">device_id,</if>
            <if test="col != null">col,</if>
            <if test="colName != null">relation_device_id,</if>
            <if test="offset != null">relation_col,</if>
            <if test="countMethod != null">count_method,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="stationId != null">#{stationId},</if>
            <if test="deviceType != null">#{deviceId},</if>
            <if test="col != null">#{col},</if>
            <if test="colName != null">#{relationDeviceId},</if>
            <if test="offset != null">#{relationCol},</if>
            <if test="countMethod != null">#{countMethod},</if>
        </trim>
    </insert>

    <insert id="insertList" keyColumn="id" keyProperty="id" useGeneratedKeys="true">
        insert into virtual_device_relation (station_id,device_id,col,relation_device_id,relation_col,count_method)
        values
        <foreach item="item" collection="list" index="index" separator=",">
            (
            #{item.stationId},#{item.deviceId},#{item.col},
            #{item.relationDeviceId},#{item.relationCol},#{item.countMethod}
            )
        </foreach>

    </insert>

    <update id="updateById" parameterType="com.ho.business.entity.VirtualDeviceRelation">
        update virtual_device_relation
        <set>
            <if test="stationId != null">station_id = #{stationId},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="col != null">col = #{col},</if>
            <if test="relationDeviceId != null">relation_device_id = #{relationDeviceId},</if>
            <if test="relationCol != null">relation_col = #{relationCol},</if>
            <if test="countMethod != null">count_method = #{countMethod},</if>
        </set>
        where id = #{id}
    </update>

    <select id="selectByParam" resultType="com.ho.business.vo.resp.virtualDeviceRelation.VirtualDeviceRelationResp">
        SELECT vr.id,vr.station_id,vr.device_id,vr.col,vr.relation_device_id,vr.relation_col,vr.count_method,d.device_type as relation_device_type
        FROM virtual_device_relation vr left join device d on vr.relation_device_id = d.src_id
        <where>
            <if test="stationId != null">and vr.station_id = #{stationId}</if>
            <if test="deviceId != null">and vr.device_id = #{deviceId}</if>
            <if test="col != null">and vr.col = #{col}</if>
            <if test="relationDeviceId != null">and vr.relation_device_id = #{relationDeviceId}</if>
            <if test="relationCol != null">and vr.relation_col = #{relationCol}</if>
            <if test="countMethod != null">and vr.count_method = #{countMethod}</if>
        </where>
    </select>

    <delete id="deleteByList">
        delete
        from virtual_device_relation
        <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="deleteStationId">
        delete
        from virtual_device_relation
        where station_id = #{stationId}
    </delete>
</mapper>
