package com.erp.product.repository;

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

import java.util.List;

/**
 * 商品-供应商关联Mapper接口
 */
@Mapper
public interface GoodsSupplierRepository extends BaseMapper<GoodsSupplier> {
    
    /**
     * 根据商品ID查询供应商ID列表
     */
    @Select("SELECT supplier_id FROM goods_supplier WHERE goods_id = #{goodsId}")
    List<Long> findSupplierIdsByGoodsId(@Param("goodsId") Long goodsId);
    
    /**
     * 根据商品ID删除所有关联
     */
    @Delete("DELETE FROM goods_supplier WHERE goods_id = #{goodsId}")
    void deleteByGoodsId(@Param("goodsId") Long goodsId);
    
    /**
     * 批量插入商品-供应商关联
     */
    @Insert("<script>" +
            "INSERT INTO goods_supplier (goods_id, supplier_id) VALUES " +
            "<foreach collection='supplierIds' item='supplierId' separator=','>" +
            "(#{goodsId}, #{supplierId})" +
            "</foreach>" +
            "</script>")
    void insertBatch(@Param("goodsId") Long goodsId, @Param("supplierIds") List<Long> supplierIds);
}
