<?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.bicloud.mapper.DocumentMapper">

    <!-- 新增文献：写入 biology_document_info -->
    <insert id="insertDocument" parameterType="com.bicloud.pojo.dto.DocumentCreateDto" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO biology_document_info
        (doc_type, doc_label, doc_title, doc_icon, doc_desp, doc_info,
         doc_collect, position, author, publication, impact_factor,
         publish_time, techniques, addtime, updtime, status, if_content, removed)
        VALUES
            (#{docType}, #{docLabel}, #{docTitle}, #{docIcon}, #{docDesp}, #{docInfo},
             #{docCollect}, #{position}, #{author}, #{publication}, #{impactFactor},
             #{publishTime}, #{techniques}, NOW(), NOW(), #{status}, #{ifContent}, 0)
    </insert>

    <!-- 软删除：removed=1 -->
    <update id="softDeleteById" parameterType="long">
        UPDATE biology_document_info
        SET removed = 1, updtime = NOW()
        WHERE id = #{id}
    </update>

    <!-- 分页查询：列表页7字段 + id
         说明：
         1) YEAR 取 publish_time
         2) 文献分类名来自 biology_document_type.name（join）
         3) IF范围：impact_factor 是varchar，先用 REGEXP 过滤“纯数字/小数”，再 CAST 比较
    -->
    <select id="pageQuery" resultType="com.bicloud.pojo.vo.DocumentListVo">
        SELECT
        d.id,
        YEAR(d.publish_time) AS year,
        d.doc_title     AS docTitle,
        d.publication   AS publication,
        d.impact_factor AS impactFactor,
        d.techniques    AS techniques,
        t.name          AS typeName,
        d.doc_url       AS docUrl
        FROM biology_document_info d
        LEFT JOIN biology_document_type t
        ON t.id = d.doc_type
        WHERE d.removed = 0

        <if test="year != null">
            AND d.publish_time IS NOT NULL
            AND YEAR(d.publish_time) = #{year}
        </if>

        <if test="docTitle != null and docTitle != ''">
            AND d.doc_title LIKE CONCAT('%', #{docTitle}, '%')
        </if>

        <if test="publication != null and publication != ''">
            AND d.publication LIKE CONCAT('%', #{publication}, '%')
        </if>

        <if test="techniques != null and techniques != ''">
            AND d.techniques LIKE CONCAT('%', #{techniques}, '%')
        </if>

        <if test="docType != null">
            AND d.doc_type = #{docType}
        </if>

        <!-- 关键字：可按需扩大范围（这里覆盖标题/期刊/作者/简介） -->
        <if test="keyword != null and keyword != ''">
            AND (
            d.doc_title LIKE CONCAT('%', #{keyword}, '%')
            OR d.publication LIKE CONCAT('%', #{keyword}, '%')
            OR d.author LIKE CONCAT('%', #{keyword}, '%')
            OR d.doc_desp LIKE CONCAT('%', #{keyword}, '%')
            )
        </if>

        <!-- IF 下限：仅对可转换为数值的 IF 参与比较 -->
        <if test="impactFactorMin != null">
            AND d.impact_factor IS NOT NULL
            AND d.impact_factor != ''
            AND d.impact_factor REGEXP '^[0-9]+(\\\\.[0-9]+)?$'
            AND CAST(d.impact_factor AS DECIMAL(10, 3)) &gt;= #{impactFactorMin}
        </if>

        <!-- IF 上限 -->
        <if test="impactFactorMax != null">
            AND d.impact_factor IS NOT NULL
            AND d.impact_factor != ''
            AND d.impact_factor REGEXP '^[0-9]+(\\\\.[0-9]+)?$'
            AND CAST(d.impact_factor AS DECIMAL(10, 3)) &lt;= #{impactFactorMax}
        </if>

        ORDER BY d.id DESC
    </select>

    <!-- 文献分类（document_type）下拉列表 -->
    <select id="listTypes" resultType="com.bicloud.pojo.vo.DocumentTypeVo">
        SELECT
            id,
            name
        FROM biology_document_type
        WHERE removed = '0'
        ORDER BY position ASC, id ASC
    </select>



</mapper>
