package com.erp.tenant.repository.supplier;

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.tenant.entity.supplier.Supplier;
import org.apache.ibatis.annotations.*;

/**
 * 供应商数据访问
 */
@Mapper
public interface SupplierRepository extends BaseMapper<Supplier> {

    @Select("SELECT * FROM suppliers WHERE id = #{id} AND tenant_id = #{tenantId} AND deleted_at IS NULL")
    Supplier findById(@Param("id") Long id, @Param("tenantId") Long tenantId);

    @Select("<script>" +
            "SELECT s.* FROM suppliers s " +
            "WHERE s.tenant_id = #{tenantId} AND s.deleted_at IS NULL " +
            "<if test='keyword != null'> AND (s.name LIKE CONCAT('%', #{keyword}, '%') OR s.code LIKE CONCAT('%', #{keyword}, '%')) </if>" +
            "<if test='status != null'> AND s.status = #{status} </if>" +
            "ORDER BY s.updated_at DESC" +
            "</script>")
    IPage<Supplier> findPage(Page<Supplier> page,
                              @Param("tenantId") Long tenantId,
                              @Param("keyword") String keyword,
                              @Param("status") Integer status);
}
