package com.ruoyi.system.mapper.flash;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.system.domain.flash.FlashDeliveryPricing;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/** 闪送时段运价持久化接口。 */
public interface FlashDeliveryPricingMapper extends BaseMapper<FlashDeliveryPricing> {
    @Select("""
            select lock_key
            from flash_delivery_pricing_lock
            where lock_key = 'GLOBAL'
            for update
            """)
    String lockPricing();

    @Select("""
            select id, start_time, end_time, starting_distance, starting_fare,
                   distance, freight, urgent_rate, minimum_urgent_fee,
                   config_version, updated_by, create_time, update_time
            from flash_delivery_pricing
            where start_time <= #{targetTime}
              and end_time > #{targetTime}
            order by start_time desc
            """)
    List<FlashDeliveryPricing> selectAtTime(@Param("targetTime") String targetTime);

    @Select("""
            select count(1)
            from flash_delivery_pricing
            where start_time < #{endTime}
              and end_time > #{startTime}
              and (#{excludedId} is null or id <> #{excludedId})
            """)
    int countOverlapping(@Param("startTime") String startTime,
                         @Param("endTime") String endTime,
                         @Param("excludedId") Long excludedId);
}
