package com.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.ResourceRating;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface ResourceRatingMapper extends BaseMapper<ResourceRating> {
    
    /**
     * 根据资源ID查询评分记录，包含用户名信息
     */
    @Select("SELECT r.*, p.name as patient_name " +
            "FROM resource_rating r " +
            "LEFT JOIN patient p ON r.patient_id = p.id " +
            "WHERE r.resource_id = #{resourceId} " +
            "ORDER BY r.create_time DESC")
    List<ResourceRating> selectByResourceId(@Param("resourceId") Integer resourceId);
    
    /**
     * 查询用户是否已对某资源评分
     */
    @Select("SELECT COUNT(*) FROM resource_rating WHERE resource_id = #{resourceId} AND patient_id = #{patientId}")
    int countByResourceIdAndPatientId(@Param("resourceId") Integer resourceId, @Param("patientId") Integer patientId);
    
    /**
     * 获取资源的平均评分
     */
    @Select("SELECT IFNULL(AVG(rating), 0) FROM resource_rating WHERE resource_id = #{resourceId}")
    double getAverageRating(@Param("resourceId") Integer resourceId);
    
    /**
     * 获取资源的评价数量
     */
    @Select("SELECT COUNT(*) FROM resource_rating WHERE resource_id = #{resourceId}")
    Integer getRatingCount(@Param("resourceId") Integer resourceId);
    
    /**
     * 根据资源ID和患者ID查询评分记录
     */
    @Select("SELECT * FROM resource_rating WHERE resource_id = #{resourceId} AND patient_id = #{patientId} LIMIT 1")
    ResourceRating findByResourceAndPatient(@Param("resourceId") Integer resourceId, @Param("patientId") Integer patientId);
} 