package com.test.bijihoudaun.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.test.bijihoudaun.entity.MarkdownFile;
import com.test.bijihoudaun.entity.MarkdownFileVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface MarkdownFileMapper extends BaseMapper<MarkdownFile> {

    /**
     * 查询最近更新的笔记列表（不包含content大字段，提高性能）
     */
    @Select("SELECT mf.id, mf.grouping_id as groupingId, mf.`title`, mf.file_name, null as `content`, mf.created_at, mf.updated_at, mf.is_deleted, mf.deleted_at, mf.deleted_by, mf.is_private, g.`grouping` as groupingName " +
            "FROM `markdown_file` mf " +
            "LEFT JOIN `grouping` g ON mf.grouping_id = g.id " +
            "WHERE mf.is_deleted = 0 " +
            "ORDER BY mf.updated_at DESC " +
            "LIMIT #{limit}")
    List<MarkdownFileVO> selectRecentWithGrouping(@Param("limit") int limit);

    /**
     * 根据分类ID查询笔记列表（不包含content大字段，提高性能）
     */
    @Select("SELECT mf.id, mf.grouping_id as groupingId, mf.`title`, mf.file_name, null as `content`, mf.created_at, mf.updated_at, mf.is_deleted, mf.deleted_at, mf.deleted_by, mf.is_private, g.`grouping` as groupingName " +
            "FROM `markdown_file` mf " +
            "LEFT JOIN `grouping` g ON mf.grouping_id = g.id " +
            "WHERE mf.grouping_id = #{groupingId} AND mf.is_deleted = 0 " +
            "ORDER BY mf.updated_at DESC")
    List<MarkdownFileVO> selectByGroupingIdWithGrouping(@Param("groupingId") Long groupingId);

    /**
     * 查询已删除的笔记（不包含content大字段）
     */
    @Select("SELECT id, grouping_id, `title`, file_name, null as `content`, created_at, updated_at, is_deleted, deleted_at, deleted_by, is_private FROM `markdown_file` WHERE is_deleted = 1")
    List<MarkdownFile> selectDeleted();

    @Delete("DELETE FROM `markdown_file` WHERE id = #{id}")
    void physicalDeleteById(@Param("id") Long id);

    @Delete("DELETE FROM `markdown_file` WHERE grouping_id = #{groupingId}")
    void physicalDeleteByGroupingId(@Param("groupingId") Long groupingId);

    @Update("UPDATE `markdown_file` SET is_deleted = 0, deleted_at = NULL, deleted_by = NULL WHERE id = #{id}")
    void restoreById(@Param("id") Long id);
    @Delete("DELETE FROM `markdown_file` WHERE is_deleted = 1")
    void physicalDeleteByIsDeleted();
    
    /**
     * 获取所有笔记ID
     * @return 所有笔记ID列表
     */
    @Select("SELECT id FROM `markdown_file` WHERE is_deleted = 0")
    List<Long> findAllIds();

    /**
     * 获取所有笔记的ID和content（用于图片清理任务）
     * @return 所有笔记的ID和content
     */
    @Select("SELECT id, `content` FROM `markdown_file` WHERE is_deleted = 0")
    List<MarkdownFile> selectAllIdsAndContent();

    @Select("SELECT mf.id, mf.grouping_id as groupingId, mf.`title`, mf.file_name, mf.`content`, mf.created_at, mf.updated_at, mf.is_deleted, mf.deleted_at, mf.deleted_by, mf.is_private, g.`grouping` as groupingName " +
            "FROM `markdown_file` mf " +
            "LEFT JOIN `grouping` g ON mf.grouping_id = g.id " +
            "WHERE mf.id = #{id} AND mf.is_deleted = 0")
    MarkdownFileVO selectByIdWithGrouping(@Param("id") Long id);
}