package com.erp.order.settlement.repository;

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

import java.util.List;

/**
 * 结算周期Repository
 */
@Mapper
public interface SettlementPeriodRepository extends BaseMapper<SettlementPeriod> {

    /**
     * 根据周期编号查询
     */
    @Select("SELECT * FROM settlement_period WHERE period_no = #{periodNo} AND deleted_at IS NULL LIMIT 1")
    SettlementPeriod findByPeriodNo(@Param("periodNo") String periodNo);

    /**
     * 根据类型和状态查询周期列表
     */
    @Select("SELECT * FROM settlement_period WHERE period_type = #{periodType} AND period_status = #{status} AND deleted_at IS NULL ORDER BY start_date DESC")
    List<SettlementPeriod> findByTypeAndStatus(@Param("periodType") String periodType, @Param("status") String status);

    /**
     * 查询最近一个开放周期
     */
    @Select("SELECT * FROM settlement_period WHERE period_type = #{periodType} AND period_status = 'open' AND deleted_at IS NULL ORDER BY start_date DESC LIMIT 1")
    SettlementPeriod findLatestOpenPeriod(@Param("periodType") String periodType);
}
