package com.erp.order.settlement.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.erp.order.settlement.entity.SettlementReport;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

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

/**
 * 结算报表Repository
 */
@Mapper
public interface SettlementReportRepository extends BaseMapper<SettlementReport> {

    /**
     * 根据报表编号查询
     */
    @Select("SELECT * FROM settlement_report WHERE report_no = #{reportNo} AND deleted_at IS NULL LIMIT 1")
    SettlementReport findByReportNo(@Param("reportNo") String reportNo);

    /**
     * 根据类型和日期范围查询报表
     */
    @Select("<script>" +
            "SELECT * FROM settlement_report WHERE report_type = #{reportType} AND deleted_at IS NULL " +
            "<if test='startDate != null'> AND start_date &gt;= #{startDate} </if>" +
            "<if test='endDate != null'> AND end_date &lt;= #{endDate} </if>" +
            "ORDER BY created_at DESC" +
            "</script>")
    List<SettlementReport> findByTypeAndDateRange(@Param("reportType") String reportType,
                                                   @Param("startDate") LocalDate startDate,
                                                   @Param("endDate") LocalDate endDate);

    /**
     * 根据店铺ID查询报表
     */
    @Select("SELECT * FROM settlement_report WHERE shop_id = #{shopId} AND deleted_at IS NULL ORDER BY created_at DESC")
    List<SettlementReport> findByShopId(@Param("shopId") Long shopId);
}
