package com.haha.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.haha.entity.Order;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.time.LocalDateTime;

public interface OrderMapper extends BaseMapper<Order> {

    /**
     * 统计已完成订单的销售总额
     * 
     * @param startDate 开始时间（null表示统计全部）
     * @return 销售总额
     */
    @Select("<script>" +
            "SELECT COALESCE(SUM(total_amount), 0) FROM t_order " +
            "WHERE status = 2 AND pay_status = 'PAID' " +
            "<if test='startDate != null'>" +
            "AND create_time &gt;= #{startDate} " +
            "</if>" +
            "</script>")
    BigDecimal sumCompletedOrderAmount(@Param("startDate") LocalDateTime startDate);
}
