<?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.ruoyi.project.mapper.BizDocMapper">
    
    <resultMap type="BizDoc" id="BizDocResult">
        <result property="docId"    column="doc_id"    />
        <result property="projectId"    column="project_id"    />
        <result property="moduleId"    column="module_id"    />
        <result property="kindType"    column="kind_type"    />
        <result property="docName"    column="doc_name"    />
        <result property="docPath"    column="doc_path"    />
        <result property="docSize"    column="doc_size"    />
        <result property="docType"    column="doc_type"    />
        <result property="docContent"    column="doc_content"    jdbcType="BLOB"/>
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="projectName"    column="project_name"    />
    </resultMap>

    <!-- 轻量字段：用于列表，排除大字段 doc_content -->
    <sql id="selectBizDocListVo">
        select 
            d.doc_id, 
            d.project_id, 
            d.module_id, 
            d.kind_type,
            d.file_name as doc_name,
            d.file_url as doc_path,
            null as doc_size,
            null as doc_type,
            d.del_flag, 
            d.upload_by as create_by,
            d.upload_time as create_time,
            null as update_by,
            null as update_time,
            p.project_name as project_name
        from biz_doc d
        left join biz_project p on p.project_id = d.project_id
    </sql>

    <!-- 详情字段：下载/单条查询专用，含 doc_content -->
    <sql id="selectBizDocDetailVo">
        select 
            d.doc_id, 
            d.project_id, 
            d.module_id, 
            d.kind_type,
            d.file_name as doc_name,
            d.file_url as doc_path,
            null as doc_size,
            null as doc_type,
            d.doc_content,
            d.del_flag, 
            d.upload_by as create_by,
            d.upload_time as create_time,
            null as update_by,
            null as update_time,
            p.project_name as project_name
        from biz_doc d
        left join biz_project p on p.project_id = d.project_id
    </sql>

    <select id="selectBizDocList" parameterType="BizDoc" resultMap="BizDocResult">
        <include refid="selectBizDocListVo"/>
        <where>
            <if test="docId != null  and docId != ''"> and doc_id = #{docId}</if>
            <if test="projectId != null  and projectId != ''"> and d.project_id = #{projectId}</if>
            <if test="moduleId != null  and moduleId != ''"> and module_id = #{moduleId}</if>
            <if test="kindType != null  and kindType != ''"> and kind_type = #{kindType}</if>
            <if test="docName != null  and docName != ''"> and d.file_name like concat('%', #{docName}, '%')</if>
            <if test="docPath != null  and docPath != ''"> and d.file_url = #{docPath}</if>
            and d.del_flag = '0'
            <!-- 权限控制（最终口径）：
                 - 项目文档(kind_type='0')：项目创建者可见全部；普通用户仅能看到自己上传的项目文档
                 - 模块文档(kind_type='1')：项目创建者可见全部；普通用户可见：
                      a) 自己上传的模块文档
                      b) 自己是该模块接取人（assignee = 当前用户名 或 = 当前用户ID）
                      c) 自己被指派（designated_user = 当前用户ID）
            -->
            <if test="params != null and params.currentUserId != null">
                and (
                    -- 项目文档：仅项目创建者可见 或 自己上传的项目文档
                    (kind_type = '0' and (
                        exists (
                            select 1 from biz_project p 
                            where p.project_id = d.project_id 
                              and p.owner_id = #{params.currentUserId}
                        )
                        or d.upload_by = #{params.currentUsername}
                    ))
                    -- 模块文档：项目创建者可见全部；普通用户可见与自己强相关的
                    or (kind_type = '1' and (
                        exists (
                            select 1 from biz_project p 
                            where p.project_id = d.project_id 
                              and p.owner_id = #{params.currentUserId}
                        )
                        or d.upload_by = #{params.currentUsername}
                        or exists (
                            select 1 from biz_module m 
                            where m.module_id = d.module_id 
                              and m.del_flag = '0'
                              and (
                                  m.designated_user = #{params.currentUserId}
                                  or CAST(m.assignee AS CHAR) = CAST(#{params.currentUserId} AS CHAR)
                                  or m.assignee = #{params.currentUsername}
                              )
                        )
                    ))
                )
            </if>
        </where>
    </select>
    
    <select id="selectBizDocByDocId" parameterType="Long" resultMap="BizDocResult">
        <include refid="selectBizDocDetailVo"/>
        where doc_id = #{docId}
    </select>
        
    <insert id="insertBizDoc" parameterType="BizDoc" useGeneratedKeys="true" keyProperty="docId">
        insert into biz_doc
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectId != null">project_id,</if>
            <if test="moduleId != null">module_id,</if>
            <if test="kindType != null">kind_type,</if>
            <if test="docName != null">file_name,</if>
            <if test="docPath != null">file_url,</if>
            <if test="docContent != null">doc_content,</if>
            <if test="createBy != null">upload_by,</if>
            <if test="createTime != null">upload_time,</if>
            <if test="delFlag != null">del_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="projectId != null">#{projectId},</if>
            <if test="moduleId != null">#{moduleId},</if>
            <if test="kindType != null">#{kindType},</if>
            <if test="docName != null">#{docName},</if>
            <if test="docPath != null">#{docPath},</if>
            <if test="docContent != null">#{docContent,jdbcType=BLOB},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
         </trim>
    </insert>

    <update id="updateBizDoc" parameterType="BizDoc">
        update biz_doc
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="moduleId != null">module_id = #{moduleId},</if>
            <if test="kindType != null">kind_type = #{kindType},</if>
            <if test="docName != null">file_name = #{docName},</if>
            <if test="docPath != null">file_url = #{docPath},</if>
            <if test="docContent != null">doc_content = #{docContent,jdbcType=BLOB},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where doc_id = #{docId}
    </update>

    <update id="deleteBizDocByDocId" parameterType="Long">
        update biz_doc set del_flag = '2' where doc_id = #{docId}
    </update>

    <update id="deleteBizDocByDocIds" parameterType="Long">
        update biz_doc set del_flag = '2' where doc_id in 
        <foreach collection="array" item="docId" open="(" separator="," close=")">
            #{docId}
        </foreach>
    </update>
</mapper>