package com.erp.product.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.erp.product.entity.Goods;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 商品Mapper接口
 */
@Mapper
public interface GoodsRepository extends BaseMapper<Goods> {
    
    /**
     * 根据编码查询商品
     */
    @Select("SELECT * FROM goods WHERE code = #{code} AND deleted_at IS NULL LIMIT 1")
    Goods findByCode(@Param("code") String code);
    
    /**
     * 根据编码查询商品（排除指定ID）
     */
    @Select("SELECT * FROM goods WHERE code = #{code} AND id != #{excludeId} AND deleted_at IS NULL LIMIT 1")
    Goods findByCodeExcludingId(@Param("code") String code, @Param("excludeId") Long excludeId);
    
    /**
     * 查询今日最大的商品编码
     */
    @Select("SELECT code FROM goods WHERE code LIKE #{prefix} AND deleted_at IS NULL ORDER BY code DESC LIMIT 1")
    String findMaxCodeWithPrefix(@Param("prefix") String prefix);
}
