package com.example.mapper;

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

import java.util.List;

public interface TreatmentResourceMapper extends BaseMapper<TreatmentResource> {
    
    @Select("SELECT r.*, c.name as category_name " +
            "FROM treatment_resource r " +
            "LEFT JOIN resource_category c ON r.category_id = c.id " +
            "WHERE r.audit_status = '已通过' " +
            "AND (r.title LIKE CONCAT('%', #{keyword}, '%') " +
            "OR r.applicable_symptoms LIKE CONCAT('%', #{keyword}, '%')) " +
            "ORDER BY r.create_time DESC")
    Page<TreatmentResource> searchResourcesForPatient(Page<TreatmentResource> page, @Param("keyword") String keyword);
    
    @Select("SELECT r.*, c.name as category_name " +
            "FROM treatment_resource r " +
            "LEFT JOIN resource_category c ON r.category_id = c.id " +
            "WHERE (r.title LIKE CONCAT('%', #{keyword}, '%') " +
            "OR r.applicable_symptoms LIKE CONCAT('%', #{keyword}, '%')) " +
            "ORDER BY r.create_time DESC")
    Page<TreatmentResource> searchResourcesForAdmin(Page<TreatmentResource> page, @Param("keyword") String keyword);
    
    @Select("SELECT r.*, c.name as category_name " +
            "FROM treatment_resource r " +
            "LEFT JOIN resource_category c ON r.category_id = c.id " +
            "WHERE (r.title LIKE CONCAT('%', #{keyword}, '%') " +
            "OR r.applicable_symptoms LIKE CONCAT('%', #{keyword}, '%')) " +
            "ORDER BY r.create_time DESC")
    List<TreatmentResource> selectAll(@Param("keyword") String keyword);
    
    @Select("SELECT r.*, c.name as category_name " +
            "FROM treatment_resource r " +
            "LEFT JOIN resource_category c ON r.category_id = c.id " +
            "WHERE r.audit_status = '已通过' " +
            "AND (r.title LIKE CONCAT('%', #{keyword}, '%') " +
            "OR r.applicable_symptoms LIKE CONCAT('%', #{keyword}, '%')) " +
            "ORDER BY r.create_time DESC")
    List<TreatmentResource> selectAllAudited(@Param("keyword") String keyword);
    
    @Select("SELECT r.*, c.name as category_name " +
            "FROM treatment_resource r " +
            "LEFT JOIN resource_category c ON r.category_id = c.id " +
            "WHERE r.category_id = #{categoryId} " +
            "AND r.audit_status = '已通过' " +
            "ORDER BY r.create_time DESC")
    List<TreatmentResource> selectByCategoryId(@Param("categoryId") Integer categoryId);
    
    @Update("UPDATE treatment_resource SET visit_count = visit_count + 1 WHERE id = #{id}")
    int incrementVisitCount(@Param("id") Integer id);
    
    @Update("UPDATE treatment_resource SET download_count = download_count + 1 WHERE id = #{id}")
    int incrementDownloadCount(@Param("id") Integer id);

    /**
     * 根据发布者ID查询所有资源
     */
    @Select("SELECT * FROM treatment_resource WHERE publisher_id = #{publisherId} AND (title LIKE CONCAT('%', #{keyword}, '%') OR applicable_symptoms LIKE CONCAT('%', #{keyword}, '%'))")
    List<TreatmentResource> selectByPublisher(@Param("publisherId") Integer publisherId, @Param("keyword") String keyword);
} 