package com.erp.tenant.repository.finance;

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.finance.Reconciliation;
import org.apache.ibatis.annotations.*;

/**
 * 对账记录数据访问
 */
@Mapper
public interface ReconciliationRepository extends BaseMapper<Reconciliation> {

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

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