<?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.microservices.pms.document.mapper.PmsDocumentMapper">

    <resultMap type="com.microservices.pms.document.domain.PmsDocument" id="PmsDocumentResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="content"    column="content"    />
        <result property="docType"    column="doc_type"    />
        <result property="parentId"    column="parent_id"    />
        <result property="pmsProjectId"    column="pms_project_id"    />
        <result property="pmsEnterpriseId"    column="pms_enterprise_id"    />
        <result property="wikiGitlinkRepoId"    column="wiki_gitlink_repo_id"    />
        <result property="attachmentIdentifier"    column="attachment_identifier"    />
        <result property="isTop"    column="is_top"    />
        <result property="permission"    column="permission"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
    </resultMap>

    <sql id="selectPmsDocumentVo">
        select id, name, content, doc_type, parent_id, pms_project_id, pms_enterprise_id, wiki_gitlink_repo_id, attachment_identifier, is_top, permission, create_time, create_by, update_time, update_by from pms_document
    </sql>

    <sql id="selectWhereBySearchVo">
        <where>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="docType != null "> and doc_type = #{docType}</if>
            <if test="pmsProjectId != null "> and pms_project_id = #{pmsProjectId}</if>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="isCreatedOnly"> and create_by = #{currentUserName}</if>
            <if test="enterpriseAdmin">
                and pms_enterprise_id = #{pmsEnterpriseId}
            </if>
            <if test="!enterpriseAdmin">
                <include refid="selectNotPrivateOrCreateByCurrentUser"/>
                and pms_enterprise_id = #{pmsEnterpriseId}
            </if>
            ${params.dataScope}
        </where>
    </sql>
    
    <sql id="selectNotPrivateOrCreateByCurrentUser">
        and (permission > 1 or create_by = #{currentUserName})
    </sql>

    <sql id="selectWritableOrCreateByCurrentUser">
        and (permission > 2 or create_by = #{currentUserName})
    </sql>

    <select id="selectPmsDocumentList" parameterType="com.microservices.pms.document.domain.PmsDocument"
            resultMap="PmsDocumentResult">
        <include refid="selectPmsDocumentVo"/>
        <where>  
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="content != null  and content != ''"> and content = #{content}</if>
            <if test="docType != null "> and doc_type = #{docType}</if>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="pmsProjectId != null "> and pms_project_id = #{pmsProjectId}</if>
            <if test="pmsEnterpriseId != null "> and pms_enterprise_id = #{pmsEnterpriseId}</if>
            <if test="wikiGitlinkRepoId != null "> and wiki_gitlink_repo_id = #{wikiGitlinkRepoId}</if>
            <if test="attachmentIdentifier != null  and attachmentIdentifier != ''"> and attachment_identifier = #{attachmentIdentifier}</if>
            <if test="isTop != null "> and is_top = #{isTop}</if>
            <if test="permission != null "> and permission = #{permission}</if>
        </where>
    </select>

    <select id="selectPmsDocumentByIdAndEnterpriseId" parameterType="Long" resultMap="PmsDocumentResult">
        <include refid="selectPmsDocumentVo"/>
        where id = #{id} and pms_enterprise_id = #{enterpriseId}
        <if test="!enterpriseAdmin">
            <include refid="selectNotPrivateOrCreateByCurrentUser"/>
        </if>
    </select>
    <select id="selectPmsDocumentListBySearchInput"
            parameterType="com.microservices.pms.document.domain.vo.PmsDocumentSearchVo" resultMap="PmsDocumentResult">
        <include refid="selectPmsDocumentVo"/>
        <include refid="selectWhereBySearchVo"/>
        ORDER BY is_top DESC, doc_type ASC, update_time DESC
    </select>

    <select id="selectPmsFavoriteDocumentList"
            parameterType="com.microservices.pms.document.domain.vo.PmsDocumentFavoriteSearchVo"
            resultMap="PmsDocumentResult">
        select pd.id, pd.name, pd.doc_type, pd.parent_id, pd.pms_project_id, pd.pms_enterprise_id, pd.wiki_gitlink_repo_id,
        pd.attachment_identifier, pd.is_top, pd.permission, pd.create_time, pd.create_by, pd.update_time, pd.update_by
        from pms_document as pd
        left join pms_document_user_favorite as pduf on pduf.pms_document_id = pd.id
        <where>
            pduf.user_id = #{currentUserId}
            <if test="name != null  and name != ''"> and pd.name like concat('%', #{name}, '%')</if>
            <if test="docType != null "> and pd.doc_type = #{docType}</if>
            ${params.dataScope}
        </where>
        ORDER BY is_top DESC, doc_type ASC, update_time DESC
    </select>

    <select id="selectPmsDocumentById" resultMap="PmsDocumentResult">
        <include refid="selectPmsDocumentVo"/>
        where id = #{id}
    </select>

    <insert id="insertPmsDocument" parameterType="com.microservices.pms.document.domain.PmsDocument"
            useGeneratedKeys="true" keyProperty="id">
        insert into pms_document
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="content != null">content,</if>
            <if test="docType != null">doc_type,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="pmsProjectId != null">pms_project_id,</if>
            <if test="pmsEnterpriseId != null">pms_enterprise_id,</if>
            <if test="wikiGitlinkRepoId != null">wiki_gitlink_repo_id,</if>
            <if test="attachmentIdentifier != null">attachment_identifier,</if>
            <if test="isTop != null">is_top,</if>
            <if test="permission != null">permission,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="content != null">#{content},</if>
            <if test="docType != null">#{docType},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="pmsProjectId != null">#{pmsProjectId},</if>
            <if test="pmsEnterpriseId != null">#{pmsEnterpriseId},</if>
            <if test="wikiGitlinkRepoId != null">#{wikiGitlinkRepoId},</if>
            <if test="attachmentIdentifier != null">#{attachmentIdentifier},</if>
            <if test="isTop != null">#{isTop},</if>
            <if test="permission != null">#{permission},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
         </trim>
    </insert>

    <insert id="batchInsertPmsDocument" parameterType="java.util.List">
        insert into pms_document
        (name, content, doc_type, parent_id, pms_project_id, pms_enterprise_id, wiki_gitlink_repo_id,
        attachment_identifier, is_top, permission, create_time, create_by, update_time, update_by)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.name}, #{item.content}, #{item.docType}, #{item.parentId}, #{item.pmsProjectId},
            #{item.pmsEnterpriseId}, #{item.wikiGitlinkRepoId}, #{item.attachmentIdentifier}, #{item.isTop},
            #{item.permission}, #{item.createTime}, #{item.createBy}, #{item.updateTime}, #{item.updateBy})
        </foreach>
    </insert>

    <update id="updatePmsDocument">
        update pms_document
        <trim prefix="SET" suffixOverrides=",">
            <if test="pmsDocument.name != null and pmsDocument.name != ''">`name` = #{pmsDocument.name},</if>
            <if test="pmsDocument.content != null">content = #{pmsDocument.content},</if>
            <if test="pmsDocument.docType != null">doc_type = #{pmsDocument.docType},</if>
            <if test="pmsDocument.parentId != null">parent_id = #{pmsDocument.parentId},</if>
            <if test="pmsDocument.pmsProjectId != null">pms_project_id = #{pmsDocument.pmsProjectId},</if>
            <if test="pmsDocument.pmsEnterpriseId != null">pms_enterprise_id = #{pmsDocument.pmsEnterpriseId},</if>
            <if test="pmsDocument.wikiGitlinkRepoId != null">wiki_gitlink_repo_id = #{pmsDocument.wikiGitlinkRepoId},</if>
            <if test="pmsDocument.attachmentIdentifier != null">attachment_identifier = #{pmsDocument.attachmentIdentifier},</if>
            <if test="pmsDocument.isTop != null">is_top = #{pmsDocument.isTop},</if>
            <if test="pmsDocument.permission != null">permission = #{pmsDocument.permission},</if>
            <if test="pmsDocument.createTime != null">create_time = #{pmsDocument.createTime},</if>
            <if test="pmsDocument.createBy != null">create_by = #{pmsDocument.createBy},</if>
            <if test="pmsDocument.updateTime != null">update_time = #{pmsDocument.updateTime},</if>
            <if test="pmsDocument.updateBy != null">update_by = #{pmsDocument.updateBy},</if>
        </trim>
        where id = #{pmsDocument.id}
        <if test="!enterpriseAdmin">
            <include refid="selectWritableOrCreateByCurrentUser"/>
        </if>
    </update>

    <delete id="deletePmsDocumentById" parameterType="Long">
        delete from pms_document where id = #{id}
        <if test="!enterpriseAdmin">
            <include refid="selectWritableOrCreateByCurrentUser"/>
        </if>
    </delete>

    <delete id="deletePmsDocumentByIds" parameterType="String">
        delete from pms_document where id in 
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
        <if test="!enterpriseAdmin">
            <include refid="selectWritableOrCreateByCurrentUser"/>
        </if>
    </delete>
    <delete id="deletePmsDocumentByIdWithoutAuthCheck">
        delete
        from pms_document
        where id = #{docId}
    </delete>

    <select id="countChildDocByIds" resultType="java.lang.Integer">
        select count(*)
        from pms_document
        where parent_id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
    <select id="countDocumentByNameAndTypeAndParenId" resultType="java.lang.Integer">
        select count(*)
        from pms_document
        where `name` = #{name}
          and doc_type = #{docType}
          and pms_project_id = #{pmsProjectId}
        <if test="docType != 4">
            and parent_id = #{parenId}
        </if>
    </select>
    <select id="selectLinkedRepoIds" resultType="java.lang.Long">
        select distinct(wiki_gitlink_repo_id)
        from pms_document
        where
            pms_enterprise_id = #{enterpriseId}
          and pms_project_id = #{projectId}
          and wiki_gitlink_repo_id is not null
    </select>
</mapper>