<?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.ms.dal.mapper.DsMessageConsumerQueueMapper">

    <resultMap id="BaseResultMap" type="com.ms.dal.entity.DsMessageConsumerQueue">
            <id property="dsMessageConsumerQueueId" column="ds_message_consumer_queue_id" jdbcType="BIGINT"/>
            <result property="shopId" column="shop_id" jdbcType="BIGINT"/>
            <result property="dsMessageId" column="ds_message_id" jdbcType="BIGINT"/>
            <result property="locked" column="locked" jdbcType="BIGINT"/>
            <result property="gmtLocked" column="gmt_locked" jdbcType="TIMESTAMP"/>
            <result property="hostname" column="hostname" jdbcType="VARCHAR"/>
            <result property="gmtLastHeartbeat" column="gmt_last_heartbeat" jdbcType="TIMESTAMP"/>
            <result property="gmtCreate" column="gmt_create" jdbcType="TIMESTAMP"/>
            <result property="gmtModified" column="gmt_modified" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        ds_message_consumer_queue_id,shop_id,ds_message_id,
        locked,gmt_locked,hostname,
        gmt_last_heartbeat,gmt_create,gmt_modified
    </sql>

    <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from ds_message_consumer_queue
        where  ds_message_consumer_queue_id = #{dsMessageConsumerQueueId,jdbcType=BIGINT} 
    </select>
    <select id="getCurrentQueueCount" resultType="java.lang.Integer">
        select count(*) from ds_message_consumer_queue
    </select>
    <select id="queryFirstRowByQueueId" resultType="com.ms.dal.entity.DsMessageConsumerQueue">
        select * from ds_message_consumer_queue where `locked` = 0 and ds_message_consumer_queue_id = #{queueId}
    </select>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
        delete from ds_message_consumer_queue
        where  ds_message_consumer_queue_id = #{dsMessageConsumerQueueId,jdbcType=BIGINT} 
    </delete>
    <insert id="insert" keyColumn="ds_message_consumer_queue_id" keyProperty="dsMessageConsumerQueueId" parameterType="com.ms.dal.entity.DsMessageConsumerQueue" useGeneratedKeys="true">
        insert into ds_message_consumer_queue
        ( ds_message_consumer_queue_id,shop_id,ds_message_id
        ,locked,gmt_locked,hostname
        ,gmt_last_heartbeat,gmt_create,gmt_modified
        )
        values (#{dsMessageConsumerQueueId,jdbcType=BIGINT},#{shopId,jdbcType=BIGINT},#{dsMessageId,jdbcType=BIGINT}
        ,#{locked,jdbcType=BIGINT},#{gmtLocked,jdbcType=TIMESTAMP},#{hostname,jdbcType=VARCHAR}
        ,#{gmtLastHeartbeat,jdbcType=TIMESTAMP},#{gmtCreate,jdbcType=TIMESTAMP},#{gmtModified,jdbcType=TIMESTAMP}
        )
    </insert>
    <insert id="insertSelective" keyColumn="ds_message_consumer_queue_id" keyProperty="dsMessageConsumerQueueId" parameterType="com.ms.dal.entity.DsMessageConsumerQueue" useGeneratedKeys="true">
        insert into ds_message_consumer_queue
        <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="dsMessageConsumerQueueId != null">ds_message_consumer_queue_id,</if>
                <if test="shopId != null">shop_id,</if>
                <if test="dsMessageId != null">ds_message_id,</if>
                <if test="locked != null">locked,</if>
                <if test="gmtLocked != null">gmt_locked,</if>
                <if test="hostname != null">hostname,</if>
                <if test="gmtLastHeartbeat != null">gmt_last_heartbeat,</if>
                <if test="gmtCreate != null">gmt_create,</if>
                <if test="gmtModified != null">gmt_modified,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="dsMessageConsumerQueueId != null">#{dsMessageConsumerQueueId,jdbcType=BIGINT},</if>
                <if test="shopId != null">#{shopId,jdbcType=BIGINT},</if>
                <if test="dsMessageId != null">#{dsMessageId,jdbcType=BIGINT},</if>
                <if test="locked != null">#{locked,jdbcType=BIGINT},</if>
                <if test="gmtLocked != null">#{gmtLocked,jdbcType=TIMESTAMP},</if>
                <if test="hostname != null">#{hostname,jdbcType=VARCHAR},</if>
                <if test="gmtLastHeartbeat != null">#{gmtLastHeartbeat,jdbcType=TIMESTAMP},</if>
                <if test="gmtCreate != null">#{gmtCreate,jdbcType=TIMESTAMP},</if>
                <if test="gmtModified != null">#{gmtModified,jdbcType=TIMESTAMP},</if>
        </trim>
    </insert>
    <insert id="insertBatch" useGeneratedKeys="true" keyColumn="ds_message_consumer_queue_id" keyProperty="list.dsMessageConsumerQueueId" parameterType="java.util.List">
        insert into ds_message_consumer_queue
        ( ds_message_consumer_queue_id,shop_id,ds_message_id
        ,locked,gmt_locked,hostname
        ,gmt_last_heartbeat,gmt_create,gmt_modified
        )
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.dsMessageConsumerQueueId,jdbcType=BIGINT},#{item.shopId,jdbcType=BIGINT},#{item.dsMessageId,jdbcType=BIGINT}
            ,#{item.locked,jdbcType=BIGINT},#{item.gmtLocked,jdbcType=TIMESTAMP},#{item.hostname,jdbcType=VARCHAR}
            ,#{item.gmtLastHeartbeat,jdbcType=TIMESTAMP},#{item.gmtCreate,jdbcType=TIMESTAMP},#{item.gmtModified,jdbcType=TIMESTAMP}
            )
        </foreach>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.ms.dal.entity.DsMessageConsumerQueue">
        update ds_message_consumer_queue
        <set>
                <if test="shopId != null">
                    shop_id = #{shopId,jdbcType=BIGINT},
                </if>
                <if test="dsMessageId != null">
                    ds_message_id = #{dsMessageId,jdbcType=BIGINT},
                </if>
                <if test="locked != null">
                    locked = #{locked,jdbcType=BIGINT},
                </if>
                <if test="gmtLocked != null">
                    gmt_locked = #{gmtLocked,jdbcType=TIMESTAMP},
                </if>
                <if test="hostname != null">
                    hostname = #{hostname,jdbcType=VARCHAR},
                </if>
                <if test="gmtLastHeartbeat != null">
                    gmt_last_heartbeat = #{gmtLastHeartbeat,jdbcType=TIMESTAMP},
                </if>
                <if test="gmtCreate != null">
                    gmt_create = #{gmtCreate,jdbcType=TIMESTAMP},
                </if>
                <if test="gmtModified != null">
                    gmt_modified = #{gmtModified,jdbcType=TIMESTAMP},
                </if>
        </set>
        where   ds_message_consumer_queue_id = #{dsMessageConsumerQueueId,jdbcType=BIGINT} 
    </update>
    <update id="updateByPrimaryKey" parameterType="com.ms.dal.entity.DsMessageConsumerQueue">
        update ds_message_consumer_queue
        set 
            shop_id =  #{shopId,jdbcType=BIGINT},
            ds_message_id =  #{dsMessageId,jdbcType=BIGINT},
            locked =  #{locked,jdbcType=BIGINT},
            gmt_locked =  #{gmtLocked,jdbcType=TIMESTAMP},
            hostname =  #{hostname,jdbcType=VARCHAR},
            gmt_last_heartbeat =  #{gmtLastHeartbeat,jdbcType=TIMESTAMP},
            gmt_create =  #{gmtCreate,jdbcType=TIMESTAMP},
            gmt_modified =  #{gmtModified,jdbcType=TIMESTAMP}
        where   ds_message_consumer_queue_id = #{dsMessageConsumerQueueId,jdbcType=BIGINT} 
    </update>
</mapper>
