<?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.SupplierRepository">

    <resultMap id="BaseResultMap" type="com.erp.supplier.entity.Supplier">
        <id column="id" property="id"/>
        <result column="supplier_code" property="supplierCode"/>
        <result column="supplier_name" property="supplierName"/>
        <result column="supplier_type" property="supplierType"/>
        <result column="industry" property="industry"/>
        <result column="business_license" property="businessLicense"/>
        <result column="registered_capital" property="registeredCapital"/>
        <result column="establishment_date" property="establishmentDate"/>
        <result column="legal_person" property="legalPerson"/>
        <result column="phone" property="phone"/>
        <result column="email" property="email"/>
        <result column="website" property="website"/>
        <result column="province" property="province"/>
        <result column="city" property="city"/>
        <result column="district" property="district"/>
        <result column="address" property="address"/>
        <result column="main_business" property="mainBusiness"/>
        <result column="status" property="status"/>
        <result column="grade" property="grade"/>
        <result column="credit_score" property="creditScore"/>
        <result column="is_internal" property="isInternal"/>
        <result column="cooperation_start_date" property="cooperationStartDate"/>
        <result column="cooperation_end_date" property="cooperationEndDate"/>
        <result column="settlement_method" property="settlementMethod"/>
        <result column="payment_days" property="paymentDays"/>
        <result column="minimum_order_quantity" property="minimumOrderQuantity"/>
        <result column="remark" property="remark"/>
        <result column="attachments" property="attachments"/>
        <result column="audit_status" property="auditStatus"/>
        <result column="audit_comment" property="auditComment"/>
        <result column="auditor_id" property="auditorId"/>
        <result column="audit_time" property="auditTime"/>
        <result column="version" property="version"/>
        <result column="created_at" property="createdAt"/>
        <result column="updated_at" property="updatedAt"/>
        <result column="deleted" property="deleted"/>
    </resultMap>

    <select id="selectSupplierPage" resultMap="BaseResultMap">
        SELECT * FROM suppliers
        WHERE deleted = 0
        <if test="status != null and status != ''">
            AND status = #{status}
        </if>
        <if test="grade != null and grade != ''">
            AND grade = #{grade}
        </if>
        <if test="supplierType != null and supplierType != ''">
            AND supplier_type = #{supplierType}
        </if>
        <if test="industry != null and industry != ''">
            AND industry = #{industry}
        </if>
        <if test="keyword != null and keyword != ''">
            AND (supplier_code LIKE CONCAT('%', #{keyword}, '%')
                 OR supplier_name LIKE CONCAT('%', #{keyword}, '%'))
        </if>
        <if test="startDate != null">
            AND created_at &gt;= #{startDate}
        </if>
        <if test="endDate != null">
            AND created_at &lt;= #{endDate}
        </if>
        ORDER BY created_at DESC
    </select>

</mapper>
