<?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.ruoyi.web.modules.farming.mapper.BizServiceAppointmentReviewMapper">

    <resultMap type="com.ruoyi.web.modules.farming.domain.BizServiceAppointmentReview" id="BizServiceAppointmentReviewResult">
        <id     property="id"                column="id"/>
        <result property="appointmentId"     column="appointment_id"/>
        <result property="reviewerName"      column="reviewer_name"/>
        <result property="reviewTime"        column="review_time"/>
        <result property="punctualityScore"  column="punctuality_score"/>
        <result property="attitudeScore"     column="attitude_score"/>
        <result property="guidanceScore"     column="guidance_score"/>
        <result property="summary"           column="summary"/>
        <result property="createTime"        column="create_time"/>
    </resultMap>

    <sql id="selectVo">
        select id, appointment_id, reviewer_name, review_time,
               punctuality_score, attitude_score, guidance_score, summary, create_time
        from biz_service_appointment_review
    </sql>

    <select id="selectReviewListByAppointmentId" resultMap="BizServiceAppointmentReviewResult">
        <include refid="selectVo"/>
        where appointment_id = #{appointmentId}
        order by review_time desc, id desc
    </select>

    <select id="selectLatestReviewByAppointmentId" resultMap="BizServiceAppointmentReviewResult">
        <include refid="selectVo"/>
        where appointment_id = #{appointmentId}
        order by review_time desc, id desc
        limit 1
    </select>

    <select id="selectReviewListByExpertProviderId" resultMap="BizServiceAppointmentReviewResult">
        select r.id, r.appointment_id, r.reviewer_name, r.review_time,
               r.punctuality_score, r.attitude_score, r.guidance_score, r.summary, r.create_time
        from biz_service_appointment_review r
        inner join biz_service_appointment a on a.id = r.appointment_id
        where a.provider_type = 3
          and a.provider_id = #{providerId}
        order by r.review_time desc, r.id desc
    </select>

    <select id="countByAppointmentId" resultType="int">
        select count(1)
        from biz_service_appointment_review
        where appointment_id = #{appointmentId}
    </select>

    <insert id="insertBizServiceAppointmentReview"
            parameterType="com.ruoyi.web.modules.farming.domain.BizServiceAppointmentReview"
            useGeneratedKeys="true" keyProperty="id">
        insert into biz_service_appointment_review (
            appointment_id, reviewer_name, review_time,
            punctuality_score, attitude_score, guidance_score, summary, create_time
        ) values (
            #{appointmentId}, #{reviewerName}, #{reviewTime},
            #{punctualityScore}, #{attitudeScore}, #{guidanceScore}, #{summary}, sysdate()
        )
    </insert>

    <!-- (Σ到场准时 + Σ服务态度 + Σ指导效果) / 3，保留 1 位小数；无评价时无行返回 null -->
    <select id="selectExpertRatingScoreByProviderId" resultType="java.math.BigDecimal">
        select round(
            (sum(r.punctuality_score) + sum(r.attitude_score) + sum(r.guidance_score)) / 3.0,
            1
        )
        from biz_service_appointment_review r
        inner join biz_service_appointment a on a.id = r.appointment_id
        where a.provider_type = 3
          and a.provider_id = #{providerId}
    </select>

    <select id="selectExpertProviderIdsForRatingSync" resultType="long">
        select distinct a.provider_id
        from biz_service_appointment_review r
        inner join biz_service_appointment a on a.id = r.appointment_id
        inner join biz_tech_resource t on t.id = a.provider_id
        where a.provider_type = 3
          and t.resource_type = '004005'
          and t.del_flag = '0'
    </select>
</mapper>
