<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.erp.finance.repository.AccountsPayableRepository">

    <resultMap id="BaseResultMap" type="com.erp.finance.entity.AccountsPayable">
        <id column="id" property="id"/>
        <result column="ap_no" property="apNo"/>
        <result column="purchase_order_no" property="purchaseOrderNo"/>
        <result column="purchase_order_id" property="purchaseOrderId"/>
        <result column="supplier_id" property="supplierId"/>
        <result column="supplier_name" property="supplierName"/>
        <result column="total_amount" property="totalAmount"/>
        <result column="inbound_amount" property="inboundAmount"/>
        <result column="paid_amount" property="paidAmount"/>
        <result column="unpaid_amount" property="unpaidAmount"/>
        <result column="currency" property="currency"/>
        <result column="payment_method" property="paymentMethod"/>
        <result column="due_date" property="dueDate"/>
        <result column="paid_date" property="paidDate"/>
        <result column="status" property="status"/>
        <result column="inbound_status" property="inboundStatus"/>
        <result column="remark" property="remark"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="create_by" property="createBy"/>
        <result column="create_time" property="createTime"/>
        <result column="update_by" property="updateBy"/>
        <result column="update_time" property="updateTime"/>
        <result column="deleted" property="deleted"/>
    </resultMap>

    <select id="selectPageList" resultMap="BaseResultMap">
        SELECT * FROM fin_accounts_payable
        WHERE deleted = 0
        <if test="apNo != null and apNo != ''">
            AND ap_no = #{apNo}
        </if>
        <if test="purchaseOrderNo != null and purchaseOrderNo != ''">
            AND purchase_order_no = #{purchaseOrderNo}
        </if>
        <if test="supplierId != null">
            AND supplier_id = #{supplierId}
        </if>
        <if test="status != null and status != ''">
            AND status = #{status}
        </if>
        <if test="startDate != null">
            AND create_time &gt;= #{startDate}
        </if>
        <if test="endDate != null">
            AND create_time &lt;= #{endDate}
        </if>
        ORDER BY create_time DESC
    </select>

    <select id="findByApNo" resultMap="BaseResultMap">
        SELECT * FROM fin_accounts_payable WHERE ap_no = #{apNo} AND deleted = 0 LIMIT 1
    </select>

    <select id="findByPurchaseOrderNo" resultMap="BaseResultMap">
        SELECT * FROM fin_accounts_payable WHERE purchase_order_no = #{purchaseOrderNo} AND deleted = 0 LIMIT 1
    </select>

    <select id="findByPurchaseOrderId" resultMap="BaseResultMap">
        SELECT * FROM fin_accounts_payable WHERE purchase_order_id = #{purchaseOrderId} AND deleted = 0 LIMIT 1
    </select>

</mapper>
