package com.czg.service.order.mapper;

import com.czg.order.entity.ShopTableOrderStatistic;
import com.czg.order.param.TableSummaryParam;
import com.czg.order.vo.TableSummaryExportVo;
import com.mybatisflex.core.BaseMapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;

/**
 * 台桌订单统计表 映射层。
 *
 * @author ww
 * @since 2025-11-21
 */
public interface ShopTableOrderStatisticMapper extends BaseMapper<ShopTableOrderStatistic> {



    /**
     * 获取指定天的在线数据台桌
     *
     * @param shopId 店铺ID
     * @param tradeDay    时间  yyyy-MM-dd
     */
    @Select("SELECT " +
            "  `table`.id AS tableId, " +
            "  #{shopId}  AS shopId, " +
            "  #{tradeDay}  AS createDay, " +
            "  `table`.table_code AS tableCode, " +
            "  `table`.`name` AS tableName, " +
            "  `area`.`name` AS areaName, " +
            "  count( `order`.id ) AS orderCount, " +
            "  SUM( CASE WHEN `order`.pay_type = 'free_pay' THEN `order`.order_amount ELSE `order`.pay_amount END ) AS orderAmount, " +
            "  SUM( CASE WHEN `order`.refund_amount > 0 THEN 1 ELSE 0 END ) AS refundCount, " +
            "  SUM( `order`.refund_amount ) AS refundAmount  " +
            "FROM " +
            "  tb_order_info `order` " +
            "  INNER JOIN tb_shop_table `table` ON `order`.shop_id = `table`.shop_id  " +
            "  AND `order`.table_code = `table`.table_code " +
            "  LEFT JOIN tb_shop_table_area `area` ON `table`.area_id = `area`.id  " +
            "  AND `table`.shop_id = `area`.shop_id  " +
            "WHERE " +
            "  `order`.shop_id = #{shopId} " +
            "  AND `order`.trade_day = #{tradeDay} " +
            "  AND `order`.paid_time IS NOT NULL  " +
            "  AND `order`.pay_mode != 'no-table'" +
            " group by `table`.id")
    List<ShopTableOrderStatistic> getOnlineData(Long shopId, LocalDate tradeDay);

    /**
     * 获取指定天的台桌订单统计数据
     *
     * @param shopId 店铺ID
     * @param tradeDay    时间  yyyy-MM-dd
     */
    @Select("SELECT  " +
            "   *   " +
            "FROM  " +
            "   `tb_shop_table_order_statistic`   " +
            "WHERE  " +
            "   shop_id = #{shopId}   " +
            "   AND create_day = #{tradeDay}")
    List<ShopTableOrderStatistic> getStatSingleDate(Long shopId, LocalDate tradeDay);


    /**
     * 获取指定时间范围内的台桌订单统计数据
     *
     * @param shopId 店铺ID
     * @param startDate 开始时间  yyyy-MM-dd
     * @param endDate   结束时间  yyyy-MM-dd
     */
    @Select("SELECT" +
            " id,table_id,table_code,table_name,area_name," +
            " sum(order_count) as orderCount," +
            " sum(order_amount) as orderAmount," +
            " sum(refund_count) as refundCount," +
            " sum(refund_amount) as refundAmount" +
            " FROM" +
            " tb_shop_table_order_statistic " +
            "WHERE" +
            " shop_id = #{shopId} " +
            " AND create_day >=#{startDate} " +
            " AND create_day <=#{endDate} " +
            " GROUP BY table_id")
    List<ShopTableOrderStatistic> getStatDateRange(Long shopId, LocalDate startDate, LocalDate endDate);


    List<TableSummaryExportVo> findSummaryExportList(TableSummaryParam param);
}
