<?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.czg.service.account.mapper.BkOrderTableMapper">

    <insert id="customInsertBatch">
        insert into bk_order_table (shop_id, book_order_id, table_area_id ,table_area_name,table_id,table_name)
        values
        <foreach collection="bkOrderTables" item="item" separator=",">
            (#{shopId}, #{bookOrderId}, #{item.tableAreaId}, #{item.tableAreaName}, #{item.tableId}, #{item.tableName})
        </foreach>
    </insert>


    <select id="table" resultMap="BkTableVOResultMap">
        SELECT
        `table`.area_id,
        `area`.`name` as area_name,
        `table`.id as table_id,
        `table`.`name` as table_name,
        `table`.`max_capacity` as table_user_num,
        `table`.`status` as table_status,
        `order`.call_phone,
        `order`.call_username,
        `order`.booking_phone,
        `order`.booking_username,
        `order`.booking_time
        FROM
        `tb_shop_table` `table`
        LEFT JOIN tb_shop_table_area `area` on `table`.area_id = `area`.id
        LEFT JOIN bk_order_table oTable on  `table`.id = oTable.table_id
        LEFT JOIN bk_order `order` ON oTable.book_order_id = `order`.id AND DATE(`order`.booking_time) = #{day}
        <if test="seatTimeType != null and seatTimeType != ''">
            AND `order`.seat_time_type = #{seatTimeType}
        </if>
        WHERE
        `table`.shop_id = #{shopId}
        AND `table`.`status` != 'unbound'
        <if test="areaId != null">
            AND `table`.area_id = #{areaId}
        </if>
        ORDER BY `table`.area_id, `table`.id
    </select>

    <resultMap id="BkTableVOResultMap" type="com.czg.account.vo.BkTableVO">
        <!-- 区域级别映射 -->
        <id property="areaId" column="area_id"/>
        <result property="areaName" column="area_name"/>

        <!-- 嵌套集合：桌台列表 -->
        <collection property="tables" ofType="com.czg.account.vo.BkTableVO$TableVO" resultMap="tableVOResultMap"/>
    </resultMap>

    <resultMap id="tableVOResultMap" type="com.czg.account.vo.BkTableVO$TableVO">
        <!-- 桌台级别映射 -->
        <id property="tableId" column="table_id"/>
        <result property="tableName" column="table_name"/>
        <result property="tableUserNum" column="table_user_num"/>
        <result property="tableStatus" column="table_status"/>

        <!-- 嵌套集合：订单列表 -->
        <collection property="bkOrders" ofType="com.czg.account.vo.BkTableVO$BkOrder">
            <result property="callPhone" column="call_phone"/>
            <result property="callUsername" column="call_username"/>
            <result property="bookingPhone" column="booking_phone"/>
            <result property="bookingUsername" column="booking_username"/>
            <result property="bookingTime" column="booking_time"/>
        </collection>
    </resultMap>
</mapper>
