<?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.fastbee.iot.mapper.ModbusParamsMapper">
    
    <resultMap type="com.fastbee.iot.domain.ModbusParams" id="ModbusParamsResult">
        <result property="id"    column="id"    />
        <result property="productId"    column="product_id"    />
        <result property="pollType"    column="poll_type"    />
        <result property="slaveId"    column="slave_id"    />
        <result property="statusDeter"    column="status_deter"    />
        <result property="deterTimer"    column="deter_timer"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="pollLength" column="poll_length" />
    </resultMap>

    <sql id="selectModbusParamsVo">
        select id, product_id, poll_type,poll_length, slave_id, status_deter, deter_timer, create_by, create_time, update_by, update_time, remark from iot_modbus_params
    </sql>

    <select id="selectModbusParamsList" parameterType="ModbusParams" resultMap="ModbusParamsResult">
        <include refid="selectModbusParamsVo"/>
        <where>  
            <if test="productId != null "> and product_id = #{productId}</if>
            <if test="pollType != null "> and poll_type = #{pollType}</if>
            <if test="slaveId != null "> and slave_id = #{slaveId}</if>
            <if test="statusDeter != null "> and status_deter = #{statusDeter}</if>
            <if test="deterTimer != null  and deterTimer != ''"> and deter_timer = #{deterTimer}</if>
        </where>
    </select>
    
    <select id="selectModbusParamsById" parameterType="Long" resultMap="ModbusParamsResult">
        <include refid="selectModbusParamsVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertModbusParams" parameterType="ModbusParams" useGeneratedKeys="true" keyProperty="id">
        insert into iot_modbus_params
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="productId != null">product_id,</if>
            <if test="pollType != null">poll_type,</if>
            <if test="slaveId != null">slave_id,</if>
            <if test="statusDeter != null">status_deter,</if>
            <if test="deterTimer != null">deter_timer,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="pollLength != null">poll_length,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="productId != null">#{productId},</if>
            <if test="pollType != null">#{pollType},</if>
            <if test="slaveId != null">#{slaveId},</if>
            <if test="statusDeter != null">#{statusDeter},</if>
            <if test="deterTimer != null">#{deterTimer},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="pollLength != null">#{pollLength},</if>
         </trim>
    </insert>

    <update id="updateModbusParams" parameterType="ModbusParams">
        update iot_modbus_params
        <trim prefix="SET" suffixOverrides=",">
            <if test="productId != null">product_id = #{productId},</if>
            <if test="pollType != null">poll_type = #{pollType},</if>
            <if test="slaveId != null">slave_id = #{slaveId},</if>
            <if test="statusDeter != null">status_deter = #{statusDeter},</if>
            <if test="deterTimer != null">deter_timer = #{deterTimer},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="pollLength != null">poll_length = #{pollLength},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteModbusParamsById" parameterType="Long">
        delete from iot_modbus_params where id = #{id}
    </delete>

    <delete id="deleteModbusParamsByIds" parameterType="String">
        delete from iot_modbus_params where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="getModbusParamsByProductId" resultMap="ModbusParamsResult">
        <include refid="selectModbusParamsVo"/>
        where product_id = #{product_id}
    </select>

    <select id="getModbusParamsByDeviceId" resultMap="ModbusParamsResult">
        SELECT
            p.id,
            p.product_id,
            p.poll_type,
            p.poll_length,
            p.slave_id,
            p.status_deter,
            p.deter_timer
        FROM
            iot_modbus_params p inner join
                iot_device d on p.product_id = d.product_id
        where  d.device_id = #{deviceId}
    </select>
</mapper>