<?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.supplier.repository.ProcurementInquiryRepository">

    <resultMap id="BaseResultMap" type="com.erp.supplier.entity.ProcurementInquiry">
        <id column="id" property="id"/>
        <result column="inquiry_no" property="inquiryNo"/>
        <result column="supplier_id" property="supplierId"/>
        <result column="supplier_name" property="supplierName"/>
        <result column="inquiry_type" property="inquiryType"/>
        <result column="product_id" property="productId"/>
        <result column="product_name" property="productName"/>
        <result column="product_spec" property="productSpec"/>
        <result column="unit" property="unit"/>
        <result column="quantity" property="quantity"/>
        <result column="expected_delivery_date" property="expectedDeliveryDate"/>
        <result column="inquiry_status" property="inquiryStatus"/>
        <result column="inquiry_date" property="inquiryDate"/>
        <result column="quote_deadline" property="quoteDeadline"/>
        <result column="quoted_price" property="quotedPrice"/>
        <result column="currency" property="currency"/>
        <result column="quote_valid_until" property="quoteValidUntil"/>
        <result column="delivery_cycle" property="deliveryCycle"/>
        <result column="quality_standard" property="qualityStandard"/>
        <result column="payment_terms" property="paymentTerms"/>
        <result column="remark" property="remark"/>
        <result column="is_selected" property="isSelected"/>
        <result column="selection_reason" property="selectionReason"/>
        <result column="creator_id" property="creatorId"/>
        <result column="creator_name" property="creatorName"/>
        <result column="created_at" property="createdAt"/>
        <result column="updated_at" property="updatedAt"/>
        <result column="deleted" property="deleted"/>
    </resultMap>

    <select id="selectInquiryPage" resultMap="BaseResultMap">
        SELECT * FROM procurement_inquiries
        WHERE deleted = 0
        <if test="supplierId != null">
            AND supplier_id = #{supplierId}
        </if>
        <if test="inquiryStatus != null and inquiryStatus != ''">
            AND inquiry_status = #{inquiryStatus}
        </if>
        <if test="inquiryType != null and inquiryType != ''">
            AND inquiry_type = #{inquiryType}
        </if>
        <if test="keyword != null and keyword != ''">
            AND (inquiry_no LIKE CONCAT('%', #{keyword}, '%')
                 OR product_name LIKE CONCAT('%', #{keyword}, '%')
                 OR supplier_name LIKE CONCAT('%', #{keyword}, '%'))
        </if>
        <if test="startDate != null and startDate != ''">
            AND inquiry_date &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            AND inquiry_date &lt;= #{endDate}
        </if>
        ORDER BY created_at DESC
    </select>

    <select id="selectBySupplierId" resultMap="BaseResultMap">
        SELECT * FROM procurement_inquiries
        WHERE supplier_id = #{supplierId} AND deleted = 0
        ORDER BY created_at DESC
    </select>

    <select id="selectByInquiryNo" resultMap="BaseResultMap">
        SELECT * FROM procurement_inquiries
        WHERE inquiry_no = #{inquiryNo} AND deleted = 0
        LIMIT 1
    </select>

</mapper>
