<?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.order.mapper.KitchenDetailMapper">


    <select id="getKitchenFood" resultMap="KitchenFoodVOResultMap">
        SELECT detail.product_id                                                             AS productId,
               detail.is_temporary                                                           AS isTemporary,
               detail.product_name                                                           AS productName,
               detail.sku_name                                                               AS skuName,
               `order`.id                                                                    AS orderId,
               detail.id                                                                     AS orderDetailId,
               `category`.id                                                                 AS categoryId,
               `category`.name                                                               AS categoryName,
               COALESCE(`table`.`name`, '无台桌')                                             AS tableName,
               CASE WHEN `table`.id IS NULL THEN '吧台' ELSE COALESCE(`area`.`name`, '') END  AS areaName,
               `staff`.`name`                                                                AS staffName,
               sum(detail.num - detail.return_num)                                           AS num,
               detail.sub_status                                                             AS subStatus,
               detail.create_time                                                             AS orderTime,
               detail.start_order_time                                                       AS startOrderTime,
               detail.dish_out_time                                                          AS dishOutTime,
               detail.food_serve_time                                                        AS foodServeTime
        FROM `tb_order_detail` detail
                 INNER JOIN `tb_order_info` `order` ON detail.order_id = `order`.id AND `order`.`status` = 'unpaid'
                 LEFT JOIN `tb_shop_staff` `staff` ON staff.id = `order`.staff_id and staff.shop_id = #{shopId}
                 LEFT JOIN `tb_product` `product` ON `product`.id = `detail`.product_id and product.shop_id = #{shopId}
                 INNER JOIN `tb_shop_prod_category` `category` ON category.id = `product`.category_id and category.shop_id = #{shopId}
                 LEFT JOIN `tb_shop_table` `table` ON `order`.table_code = `table`.table_code
                 LEFT JOIN `tb_shop_table_area` `area` ON `table`.area_id = `area`.id
        WHERE detail.shop_id = #{shopId}
            AND detail.`status` = 'wait-pay'
            <if test="categoryId != null">
                AND `category`.id = #{categoryId}
            </if>
            <if test="productName != null and productName != ''">
                AND detail.product_name like concat('%',#{productName},'%')
            </if>
        GROUP BY `order`.id, detail.product_id, detail.sku_id, detail.id
        ORDER BY
            CASE detail.sub_status
            WHEN 'READY_TO_SERVE' THEN 1
            WHEN 'PENDING_PREP' THEN 2
            WHEN 'SENT_OUT' THEN 3
            WHEN 'DELIVERED' THEN 4
            ELSE 5
            END ASC,
        CASE WHEN detail.sub_status = 'READY_TO_SERVE' THEN detail.start_order_time END ASC
    </select>


    <select id="getKitchenTable" resultType="com.czg.order.vo.KitchenTableVO">
        SELECT
        `order`.id AS orderId,
        `table`.id AS tableId,
        `staff`.`name` AS staffName,
        detail.order_time AS orderTime,
        CASE WHEN `table`.id IS NULL THEN '' ELSE COALESCE (`order`.table_code, '') END AS tableCode,
        COALESCE(`table`.`name`, '无台桌') AS tableName,
        `table`.`area_id` AS areaId,
        CASE WHEN `table`.id IS NULL THEN '吧台' ELSE COALESCE(`area`.`name`, '') END AS areaName,
        sum(case when detail.sub_status = 'READY_TO_SERVE' then detail.num - detail.return_num else 0 end) AS pendingDishCount
        FROM
        `tb_order_detail` detail
        INNER JOIN `tb_order_info` `order` ON detail.order_id = `order`.id AND `order`.`status` = 'unpaid'
        LEFT JOIN `tb_shop_staff` `staff` ON staff.id = `order`.staff_id and staff.shop_id = #{shopId}
        LEFT JOIN `tb_shop_table` `table` ON `order`.table_code = `table`.table_code
        LEFT JOIN `tb_shop_table_area` `area` ON `table`.area_id = `area`.id
        WHERE
        detail.shop_id = #{shopId}
        AND detail.`status` = 'wait-pay'
        <if test="tableName != null and tableName != ''">
            AND `table`.`name` like concat('%',#{tableName},'%')
        </if>
        <if test="areaId != null">
            AND `table`.`area_id` = #{areaId}
        </if>
        GROUP BY detail.order_id,tableName
    </select>

    <select id="getKitchenTableFoods" resultType="com.czg.order.vo.KitchenTableFoodVO">
        SELECT
            detail.product_id                                                             AS productId,
            detail.product_name                                                           AS productName,
            detail.is_temporary                                                           AS isTemporary,
            `staff`.`name`                                                                AS staffName,
            detail.id                                                                     AS orderDetailId,
            detail.sku_name                                                               AS skuName,
            sum(detail.num - detail.return_num)                                           AS num,
            detail.sub_status                                                             AS subStatus,
            detail.create_time                                                             AS orderTime,
            detail.start_order_time                                                       AS startOrderTime,
            detail.dish_out_time                                                          AS dishOutTime,
            detail.food_serve_time                                                        AS foodServeTime
        FROM `tb_order_detail` detail
        INNER JOIN `tb_order_info` `order` ON detail.order_id = `order`.id AND `order`.`status` = 'unpaid'
        LEFT JOIN `tb_shop_table` `table` ON `order`.table_code = `table`.table_code
        LEFT JOIN `tb_shop_staff` `staff` ON staff.id = `order`.staff_id and staff.shop_id = #{shopId}
        WHERE detail.shop_id = #{shopId}
        AND detail.`status` = 'wait-pay'
        <if test="orderId != null">
            AND detail.order_id = #{orderId}
        </if>
        <if test="tableCode != null and tableCode != ''">
            AND `table`.`table_code` = #{tableCode}
        </if>
        <if test="isNoTable != null">
            AND `table`.`table_code` is null
        </if>
        GROUP BY `order`.id, detail.product_id, detail.sku_id, detail.id
        ORDER BY
            CASE detail.sub_status
                WHEN 'READY_TO_SERVE' THEN 1
                WHEN 'PENDING_PREP' THEN 2
                WHEN 'SENT_OUT' THEN 3
                WHEN 'DELIVERED' THEN 4
                ELSE 5
            END ASC,
        CASE WHEN detail.sub_status = 'READY_TO_SERVE' THEN detail.start_order_time END ASC
    </select>


    <resultMap id="KitchenFoodVOResultMap" type="com.czg.order.vo.KitchenFoodVO">
        <result property="productId" column="productId"/>
        <result property="isTemporary" column="isTemporary"/>
        <result property="productName" column="productName"/>
        <result property="categoryId" column="categoryId"/>
        <result property="categoryName" column="categoryName"/>
        <collection property="foodItems" ofType="com.czg.order.vo.KitchenFoodVO$KitchenFoodItemVO">
            <constructor>
                <arg column="orderId" javaType="java.lang.Long"/>
                <arg column="staffName" javaType="java.lang.String"/>
                <arg column="tableName" javaType="java.lang.String"/>
                <arg column="areaName" javaType="java.lang.String"/>
                <arg column="orderDetailId" javaType="java.lang.Long"/>
                <arg column="num" javaType="java.lang.Long"/>
                <arg column="subStatus" javaType="java.lang.String"/>
                <arg column="orderTime" javaType="java.lang.String"/>
                <arg column="startOrderTime" javaType="java.lang.String"/>
                <arg column="dishOutTime" javaType="java.lang.String"/>
                <arg column="foodServeTime" javaType="java.lang.String"/>
            </constructor>
        </collection>
    </resultMap>

</mapper>
