package com.aiotagro.cattletrade.business.mapper;

import com.aiotagro.cattletrade.business.entity.WarehouseOut;
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 WarehouseOutMapper extends BaseMapper<WarehouseOut> {

    /**
     * 根据出仓单号查询出仓记录（唯一性校验）
     * @param outNumber 出仓单号
     * @return 出仓记录
     */
    @Select("SELECT * FROM warehouse_out WHERE out_number = #{outNumber} AND is_delete = 0")
    WarehouseOut selectByOutNumber(@Param("outNumber") String outNumber);

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

