package com.erp.inventory.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.inventory.entity.StockWarningRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 库存预警记录Repository
 */
@Mapper
public interface StockWarningRecordRepository extends BaseMapper<StockWarningRecord> {

    /**
     * 分页查询预警记录
     */
    IPage<StockWarningRecord> findByConditions(
            Page<StockWarningRecord> page,
            @Param("skuCode") String skuCode,
            @Param("warehouseId") Long warehouseId,
            @Param("ruleType") String ruleType,
            @Param("status") String status,
            @Param("level") String level,
            @Param("startDate") LocalDateTime startDate,
            @Param("endDate") LocalDateTime endDate);

    /**
     * 查询未解决的预警记录
     */
    List<StockWarningRecord> findUnresolved(@Param("skuCode") String skuCode, @Param("warehouseId") Long warehouseId);

    /**
     * 检查是否存在未解决的同类型预警
     */
    Integer countUnresolvedByRuleIdAndSkuCodeAndWarehouse(
            @Param("ruleId") Long ruleId, 
            @Param("skuCode") String skuCode, 
            @Param("warehouseId") Long warehouseId);

    /**
     * 统计指定时间范围内的预警数量
     */
    Integer countByRuleTypeAndCreatedAtBetween(
            @Param("ruleType") String ruleType,
            @Param("startDate") LocalDateTime startDate,
            @Param("endDate") LocalDateTime endDate);

    /**
     * 统计指定时间范围内的预警数量（按状态）
     */
    Integer countByStatusAndCreatedAtBetween(
            @Param("status") String status,
            @Param("startDate") LocalDateTime startDate,
            @Param("endDate") LocalDateTime endDate);
}
