<?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.zm.order.tool.server.dao.LineUpRecordsCountDao">
    <resultMap id="ResultMap" type="com.zm.order.tool.server.model.LineUpRecordsCount">
		<id column="id" jdbcType="BIGINT" property="id"/>
		<result column="seller_id" jdbcType="BIGINT" property="sellerId"/>
		<result column="store_id" jdbcType="BIGINT" property="storeId"/>
		<result column="queue_order_count" jdbcType="INTEGER" property="queueOrderCount"/>
		<result column="dequeue_order_count" jdbcType="INTEGER" property="dequeueOrderCount"/>
		<result column="queue_goods_count" jdbcType="INTEGER" property="queueGoodsCount"/>
		<result column="dequeue_goods_count" jdbcType="INTEGER" property="dequeueGoodsCount"/>
		<result column="line_up_at" jdbcType="TIMESTAMP" property="lineUpAt"/>
		<result column="version" jdbcType="INTEGER" property="version"/>
		<result column="created_at" jdbcType="TIMESTAMP" property="createdAt"/>
		<result column="updated_at" jdbcType="TIMESTAMP" property="updatedAt"/>
		<result column="is_deleted" jdbcType="INTEGER" property="isDeleted"/>
	</resultMap>

	<sql id="tb">
        order_line_up_records_count
    </sql>

    <sql id="cols_all">
        id,
		seller_id,
		store_id,
		queue_order_count,
		dequeue_order_count,
		queue_goods_count,
		dequeue_goods_count,
		line_up_at,
		version,
		created_at,
		updated_at,
		is_deleted
    </sql>

    <sql id="values">
    	#{id,jdbcType=BIGINT},
		#{sellerId,jdbcType=BIGINT},
		#{storeId,jdbcType=BIGINT},
		#{queueOrderCount,jdbcType=INTEGER},
		#{dequeueOrderCount,jdbcType=INTEGER},
		#{queueGoodsCount,jdbcType=INTEGER},
		#{dequeueGoodsCount,jdbcType=INTEGER},
		#{lineUpAt},
		0,
		now(),
		now(),
		0
    </sql>

    <insert id="insert" parameterType="com.zm.order.tool.server.model.LineUpRecordsCount"
                        keyColumn="id" keyProperty="id" useGeneratedKeys="true">
        insert into
        <include refid="tb"/>
        (<include refid="cols_all"/>)
        values
        (<include refid="values"/>)
    </insert>

    <select id="selectList" resultMap="ResultMap">
        SELECT
        <include refid="cols_all"/>
        FROM
        <include refid="tb"/>
        <where>
			<if test="sellerId != null">
				AND seller_id = #{sellerId}
			</if>
			<if test="storeIdList != null and storeIdList.size > 0">
				AND store_id in
				<foreach collection="storeIdList" item="storeId" index="index" open="(" close=")" separator=",">
					#{storeId}
				</foreach>
			</if>
			<if test="lineUpAt != null">
				AND line_up_at = #{lineUpAt}
			</if>
            and is_deleted = 0
        </where>
    </select>

	<select id="selectOne" resultMap="ResultMap">
		SELECT
		<include refid="cols_all"/>
		FROM
		<include refid="tb"/>
		<where>
			seller_id = #{sellerId}
			AND store_id = #{storeId}
			AND line_up_at = #{lineUpAt}
			AND is_deleted = 0
			limit 1
		</where>
	</select>

    <update id="update" parameterType="com.zm.order.tool.server.model.LineUpRecordsCount">
        UPDATE
        <include refid="tb"/>
        <set>
			<if test="queueOrderCount != null">
				queue_order_count = #{queueOrderCount,jdbcType=INTEGER},
			</if>
			<if test="dequeueOrderCount != null">
				dequeue_order_count = #{dequeueOrderCount,jdbcType=INTEGER},
			</if>
			<if test="queueGoodsCount != null">
				queue_goods_count = #{queueGoodsCount,jdbcType=INTEGER},
			</if>
			<if test="dequeueGoodsCount != null">
				dequeue_goods_count = #{dequeueGoodsCount,jdbcType=INTEGER},
			</if>
			version = version + 1,
			updated_at = now()
        </set>
        where id = #{id}
    </update>

	<update id="updateDequeueGoodsCount">
		update
		<include refid="tb"/>
		set dequeue_goods_count = dequeue_goods_count + 1,
		version = version + 1,
		updated_at = now()
		where id = #{id}
	</update>

</mapper>