package com.aiotagro.cattletrade.business.mapper;

import com.aiotagro.cattletrade.business.entity.WarehouseIn;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 进仓管理 Mapper 接口
 *
 * @author System
 * @date 2025-01-20
 */
@Mapper
public interface WarehouseInMapper extends BaseMapper<WarehouseIn> {

    /**
     * 根据进仓单号查询进仓记录（唯一性校验）
     * @param inNumber 进仓单号
     * @return 进仓记录
     */
    @Select("SELECT * FROM warehouse_in WHERE in_number = #{inNumber} AND is_delete = 0")
    WarehouseIn selectByInNumber(@Param("inNumber") String inNumber);

    /**
     * 获取当天最大流水号
     * @param datePrefix 日期前缀（如：IN20250120）
     * @return 最大流水号（4位数字）
     */
    @Select("SELECT COALESCE(MAX(CAST(SUBSTRING(in_number, 11) AS UNSIGNED)), 0) FROM warehouse_in WHERE in_number LIKE CONCAT(#{datePrefix}, '%') AND is_delete = 0")
    Integer getMaxSequenceByDate(@Param("datePrefix") String datePrefix);
}

