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

    <resultMap id="LineUpRecordMap" type="com.zm.order.tool.server.model.LineUpRecord">
        <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="floor_code" jdbcType="VARCHAR" property="floorCode"/>
        <result column="stall_code" jdbcType="VARCHAR" property="stallCode"/>
        <result column="order_no" jdbcType="VARCHAR" property="orderNo"/>
        <result column="user_id" jdbcType="BIGINT" property="userId"/>
        <result column="goods_id" jdbcType="BIGINT" property="goodsId"/>
        <result column="num" jdbcType="INTEGER" property="num"/>
        <result column="pay_at" jdbcType="TIMESTAMP" property="payAt"/>
        <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
    </sql>

    <sql id="cols_all">
        id,
        <include refid="cols_exclude_id"/>
    </sql>

    <sql id="cols_exclude_id">
        seller_id, store_id, floor_code, stall_code, order_no, user_id, goods_id, num, pay_at, created_at, updated_at,
        is_deleted
    </sql>

    <sql id="values">
        #{id}, #{sellerId}, #{storeId},
        #{floorCode}, #{stallCode}, #{orderNo},
        #{userId}, #{goodsId}, #{num}, #{payAt}, now(), now(),
        0
    </sql>



    <select id="selectList" parameterType="map" resultMap="LineUpRecordMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            seller_id = #{sellerId}
            and store_id =#{storeId}
            <if test="orderNo != null">
                and order_no=#{orderNo}
            </if>
            <if test="isDeleted != null">
                and is_deleted = #{isDeleted}
            </if>
        </where>


    </select>

    <resultMap id="GoodsCountMap" type="com.zm.order.tool.server.model.LineUpCount">
        <result column="order_num" jdbcType="BIGINT" property="orderNum"/>
        <result column="goods_num" jdbcType="INTEGER" property="goodsNum"/>
    </resultMap>

    <select id="countOrder" parameterType="map" resultMap="GoodsCountMap">
        select
        count(distinct order_no) as order_num, sum(num) as goods_num
        from
        <include refid="tb"/>
        <where>
            seller_id = #{sellerId}
            and store_id =#{storeId}
            <![CDATA[ AND pay_at >= #{payAtStart} ]]>
            <![CDATA[ AND pay_at <= #{payAtEnd} ]]>
            <if test="isDeleted != null">
                and is_deleted = #{isDeleted}
            </if>
        </where>
    </select>

</mapper>