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

    <resultMap id="BaseResultMap" type="com.erp.supplier.entity.SupplierRating">
        <id column="id" property="id"/>
        <result column="supplier_id" property="supplierId"/>
        <result column="rating_period" property="ratingPeriod"/>
        <result column="rating_year" property="ratingYear"/>
        <result column="rating_month" property="ratingMonth"/>
        <result column="quality_score" property="qualityScore"/>
        <result column="delivery_score" property="deliveryScore"/>
        <result column="price_score" property="priceScore"/>
        <result column="service_score" property="serviceScore"/>
        <result column="overall_score" property="overallScore"/>
        <result column="grade" property="grade"/>
        <result column="evaluator_id" property="evaluatorId"/>
        <result column="evaluator_name" property="evaluatorName"/>
        <result column="evaluation_time" property="evaluationTime"/>
        <result column="strengths" property="strengths"/>
        <result column="weaknesses" property="weaknesses"/>
        <result column="suggestions" property="suggestions"/>
        <result column="remark" property="remark"/>
        <result column="created_at" property="createdAt"/>
        <result column="updated_at" property="updatedAt"/>
        <result column="deleted" property="deleted"/>
    </resultMap>

    <select id="selectRatingPage" resultMap="BaseResultMap">
        SELECT * FROM supplier_ratings
        WHERE deleted = 0
        <if test="supplierId != null">
            AND supplier_id = #{supplierId}
        </if>
        <if test="grade != null and grade != ''">
            AND grade = #{grade}
        </if>
        <if test="ratingYear != null">
            AND rating_year = #{ratingYear}
        </if>
        <if test="ratingPeriod != null and ratingPeriod != ''">
            AND rating_period = #{ratingPeriod}
        </if>
        ORDER BY evaluation_time DESC
    </select>

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

    <select id="selectLatestBySupplierId" resultMap="BaseResultMap">
        SELECT * FROM supplier_ratings
        WHERE supplier_id = #{supplierId} AND deleted = 0
        ORDER BY evaluation_time DESC
        LIMIT 1
    </select>

</mapper>
