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> {

    @Select("SELECT mf.*, 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);

    @Select("SELECT mf.*, 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") String groupingId);

    @Select("SELECT * 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<Integer> findAllIds();
}
