<?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.yeejoin.amos.knowledgebase.face.orm.dao.DocContentMapper">


    <select id="searchForDocIds" resultType="map">

         select
            DISTINCT d.docSeq
        from
            (
            select
                t.TARGET_SEQ docSeq
            from
                knowledge_tag_instance t
            where
                t.TAG_NAME like CONCAT('%',#{queryStr},'%'
                )
                and t.AGENCY_CODE = #{agencyCode}
                and
                EXISTS(select 1 from knowledge_doc_content dc
		                where dc.SEQUENCE_NBR = t.TAG_SEQ
        <if test="docStatus!=null and docStatus.length>0">
            and dc.DOC_STATUS in
            <foreach collection="docStatus" item="name" index="index" open="(" close=")" separator=",">
                #{name}
            </foreach>
        </if>


        <if test="auditStatus!=null and auditStatus.length>0">
            and dc.AUDIT_STATUS in
            <foreach collection="auditStatus" item="name" index="index" open="(" close=")" separator=",">
                #{name}
            </foreach>
        </if>
        <if test="userId != null and userId !=''">
            and dc.USER_ID = #{userId}
        </if>
        <if test="orgCode != null and orgCode !=''">
            and dc.ORG_CODE like '%${orgCode}%'
        </if>
                )
        UNION ALL
            SELECT
                n.INSTANCE_ID docSeq
            FROM
                knowledge_dynamics_value n
            where
                n.AGENCY_CODE = #{agencyCode}
                and n.FIELD_NAME = 'docTitle'
                and n.FIELD_VALUE like  CONCAT('%',#{queryStr},'%' )
                EXISTS(select 1 from knowledge_doc_content dc
		                where dc.SEQUENCE_NBR = t.TAG_SEQ
        <if test="docStatus!=null and docStatus.length>0">
            and dc.DOC_STATUS in
            <foreach collection="docStatus" item="name" index="index" open="(" close=")" separator=",">
                 #{name}
            </foreach>
        </if>


        <if test="auditStatus!=null and auditStatus.length>0">
            and dc.AUDIT_STATUS in
            <foreach collection="auditStatus" item="name" index="index" open="(" close=")" separator=",">
                #{name}
            </foreach>
        </if>
        <if test="userId != null and userId !=''">
		                and dc.USER_ID = #{userId}
        </if>
        <if test="orgCode != null and orgCode !=''">
		                and dc.ORG_CODE like '%${orgCode}%'
        </if>
                )
                )d LIMIT #{offset},#{length}
    </select>


    <select id="queryDocBaseInfoList" parameterType="map" resultType="map">
        SELECT
          SEQUENCE_NBR sequenceNbr, REC_DATE recDate, DOC_STATUS docStatus, CREATE_TIME createTime, USER_ID userId,
          AUDIT_STATUS auditStatus, REJECTION_COMMENT rejectionComment, AUDITOR_USER_ID auditorUserId, ORG_CODE orgCode,
          DOC_TITLE docTitle, DIRECTORY_ID directoryId,
          (SELECT kdc.CATEGORY_NAME FROM knowledge_doc_category kdc WHERE kdc.SEQUENCE_NBR = DIRECTORY_ID) directoryName,
          <if test="extraFields != null and extraFields.size > 0">
            <foreach collection="extraFields" item="_field" >
                ${_field},
            </foreach>
          </if>
          IFNULL(collectNum, 0) collectNum, IFNULL(quoteNum, 0) quoteNum, IFNULL(collect, "UNCOLLECT") collect
        FROM knowledge_doc_content doct LEFT JOIN (
            SELECT INSTANCE_ID
              <if test="extraFields != null and extraFields.size > 0">
                  <foreach collection="extraFields" item="_field" >
                      , MAX(CASE FIELD_NAME WHEN #{_field} THEN FIELD_VALUE ELSE NULL END) AS ${_field}
                  </foreach>
              </if>
            FROM knowledge_dynamics_value
            GROUP BY INSTANCE_ID
          ) fieldt ON doct.SEQUENCE_NBR = fieldt.INSTANCE_ID
          LEFT JOIN (
              SELECT ENTITY_ID, COUNT(1) collectNum FROM knowledge_interaction_record WHERE OPERATE_TYPE = "COLLECT" GROUP BY ENTITY_ID
          ) coll ON doct.SEQUENCE_NBR = coll.ENTITY_ID
          LEFT JOIN (
              SELECT ENTITY_ID, OPERATE_COUNT quoteNum FROM knowledge_interaction_count WHERE OPERATE_TYPE = "PUBLISH" AND ENTITY_TYPE = "DOC"
          ) quot ON doct.SEQUENCE_NBR = quot.ENTITY_ID
          LEFT JOIN (
              SELECT ENTITY_ID, OPERATE_TYPE collect FROM knowledge_interaction_record WHERE OPERATE_TYPE = "COLLECT" AND USER_ID = #{userId}
          ) favor ON doct.SEQUENCE_NBR = favor.ENTITY_ID
        <where>
            <if test="commonFilters != null">
                <if test="commonFilters.directoryIdList != null and commonFilters.directoryIdList.size > 0">
                    AND DIRECTORY_ID IN
                    <foreach collection="commonFilters.directoryIdList" item="directoryId" separator=", " open="(" close=")">
                        #{directoryId}
                    </foreach>
                </if>
                <if test="commonFilters.docTitle != null and commonFilters.docTitle.length > 0">
                    AND DOC_TITLE LIKE CONCAT("%", #{commonFilters.docTitle}, "%")
                </if>
                <if test="commonFilters.docStatus != null and commonFilters.docStatus.length > 0">
                    AND DOC_STATUS = #{commonFilters.docStatus}
                </if>
                <if test="commonFilters.auditStatus != null and commonFilters.auditStatus.size > 0">
                    AND AUDIT_STATUS IN
                    <foreach collection="commonFilters.auditStatus" item="_status" separator=", " open="(" close=")">
                        #{_status}
                    </foreach>
                </if>
                <if test="commonFilters.createTimeLeft != null">
                    AND CREATE_TIME <![CDATA[>=]]> #{commonFilters.createTimeLeft}
                </if>
                <if test="commonFilters.createTimeRight != null">
                    AND CREATE_TIME <![CDATA[<=]]> #{commonFilters.createTimeRight}
                </if>
            </if>
            <if test="collect != null">
                AND collect = #{collect}
            </if>
            <if test="permissionFilters != null">
                <if test="permissionFilters.userId != null or permissionFilters.orgCode != null">
                    AND (
                    <if test="permissionFilters.userId != null">
                        REC_USER_ID = #{permissionFilters.userId}
                    </if>
                    <if test="permissionFilters.userId != null and permissionFilters.orgCode != null">
                        OR
                    </if>
                    <if test="permissionFilters.orgCode != null">
                        (
                        ORG_CODE LIKE CONCAT(#{permissionFilters.orgCode}, "%")
                        AND AUDIT_STATUS IN
                        <foreach collection="permissionFilters.auditStatusList" item="auditStatus" open="(" close=")" separator=", ">
                            #{auditStatus}
                        </foreach>
                        )
                    </if>
                    )
                </if>
            </if>
            <if test="extraStrFilters != null and extraStrFilters.size > 0">
                <foreach collection="extraStrFilters" item="str">
                    AND ${str}
                </foreach>
            </if>
        </where>
        ORDER BY FIELD(audit_status, 'SAVED', 'SUBMITTED', 'REJECTED', 'PASSED') ASC, create_time DESC
        LIMIT #{start}, #{limit}
    </select>

    <select id="queryDocBaseInfoTotal" parameterType="map" resultType="int">
        SELECT
          COUNT(1)
        FROM knowledge_doc_content doct LEFT JOIN (
            SELECT INSTANCE_ID
                <if test="extraFields != null and extraFields.size > 0">
                    <foreach collection="extraFields" item="_field" >
                        , MAX(CASE FIELD_NAME WHEN #{_field} THEN FIELD_VALUE ELSE NULL END) AS ${_field}
                    </foreach>
                </if>
            FROM knowledge_dynamics_value
            GROUP BY INSTANCE_ID
        ) fieldt ON doct.SEQUENCE_NBR = fieldt.INSTANCE_ID
        LEFT JOIN (
            SELECT ENTITY_ID, COUNT(1) collectNum FROM knowledge_interaction_record WHERE OPERATE_TYPE = "COLLECT" GROUP BY ENTITY_ID
        ) coll ON doct.SEQUENCE_NBR = coll.ENTITY_ID
        LEFT JOIN (
            SELECT ENTITY_ID, OPERATE_COUNT quoteNum FROM knowledge_interaction_count WHERE OPERATE_TYPE = "PUBLISH" AND ENTITY_TYPE = "DOC"
        ) quot ON doct.SEQUENCE_NBR = quot.ENTITY_ID
        LEFT JOIN (
            SELECT ENTITY_ID, OPERATE_TYPE collect FROM knowledge_interaction_record WHERE OPERATE_TYPE = "COLLECT" AND USER_ID = #{userId}
        ) favor ON doct.SEQUENCE_NBR = favor.ENTITY_ID
        <where>
            <if test="commonFilters != null">
                <if test="commonFilters.directoryIdList != null and commonFilters.directoryIdList.size > 0">
                    AND DIRECTORY_ID IN
                    <foreach collection="commonFilters.directoryIdList" item="directoryId" separator=", " open="(" close=")">
                        #{directoryId}
                    </foreach>
                </if>
                <if test="commonFilters.docTitle != null and commonFilters.docTitle.length > 0">
                    AND DOC_TITLE LIKE CONCAT("%", #{commonFilters.docTitle}, "%")
                </if>
                <if test="commonFilters.docStatus != null and commonFilters.docStatus.length > 0">
                    AND DOC_STATUS = #{commonFilters.docStatus}
                </if>
                <if test="commonFilters.auditStatus != null and commonFilters.auditStatus.size > 0">
                    AND AUDIT_STATUS IN
                    <foreach collection="commonFilters.auditStatus" item="_status" separator=", " open="(" close=")">
                        #{_status}
                    </foreach>
                </if>
                <if test="commonFilters.createTimeLeft != null">
                    AND CREATE_TIME <![CDATA[>=]]> #{commonFilters.createTimeLeft}
                </if>
                <if test="commonFilters.createTimeRight != null">
                    AND CREATE_TIME <![CDATA[<=]]> #{commonFilters.createTimeRight}
                </if>
            </if>
            <if test="collect != null">
                AND collect = #{collect}
            </if>
            <if test="permissionFilters != null">
                <if test="permissionFilters.userId != null or permissionFilters.orgCode != null">
                    AND (
                    <if test="permissionFilters.userId != null">
                        REC_USER_ID = #{permissionFilters.userId}
                    </if>
                    <if test="permissionFilters.userId != null and permissionFilters.orgCode != null">
                        OR
                    </if>
                    <if test="permissionFilters.orgCode != null">
                        (
                            ORG_CODE LIKE CONCAT(#{permissionFilters.orgCode}, "%")
                            AND AUDIT_STATUS IN
                            <foreach collection="permissionFilters.auditStatusList" item="auditStatus" open="(" close=")" separator=", ">
                                #{auditStatus}
                            </foreach>
                        )
                    </if>
                    )
                </if>
            </if>
            <if test="extraStrFilters != null and extraStrFilters.size > 0">
                <foreach collection="extraStrFilters" item="str">
                    AND ${str}
                </foreach>
            </if>
        </where>
    </select>
    
    <select id="getAllPublishedDocIds" resultType="long">
        SELECT SEQUENCE_NBR FROM knowledge_doc_content WHERE DOC_STATUS = "PUBLISHED"
    </select>
</mapper>

