package com.erp.purchase.mapper;

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.purchase.entity.Supplier;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface SupplierMapper extends BaseMapper<Supplier> {

    @Select("SELECT * FROM supplier WHERE deleted = 0 AND (#{keyword} IS NULL OR name LIKE CONCAT('%', #{keyword}, '%') OR supplier_code LIKE CONCAT('%', #{keyword}, '%')) AND (#{status} IS NULL OR status = #{status}) ORDER BY create_time DESC")
    IPage<Supplier> selectPageList(Page<Supplier> page,
                                    @Param("keyword") String keyword,
                                    @Param("status") String status);

    @Select("SELECT * FROM supplier WHERE deleted = 0 AND supplier_code = #{supplierCode} LIMIT 1")
    Supplier findByCode(@Param("supplierCode") String supplierCode);

    @Select("SELECT COUNT(*) FROM supplier WHERE deleted = 0 AND name = #{name} AND id != #{id}")
    int countByName(@Param("name") String name, @Param("id") Long id);
}
