<?xml version="1.0" encoding="UTF-8"?><!--Converted at: Wed May 11 15:49:38 KST 2016-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="nlib.col.service.impl.CollectionDAO">

    <select id="selectMngOrgInfo" parameterType="String" resultType="CollectionVO">
        SELECT 
              A.MNG_ORG_CD 
            , B.DOMAIN_HOST_NM
            , B.DEPT_NM AS MNG_ORG_NM 
        FROM RG_MASTER A
        LEFT OUTER JOIN SM_DEPT B ON A.MNG_ORG_CD = B.ORG_CD  
        WHERE A.MASTER_ID = #{masterId}
        LIMIT 1 
    </select>
    
    <insert id="insertDataInquiryLog" parameterType="CollectionVO">
        INSERT INTO MB_VIEW_LOG(
              MNG_ORG_CD
            , MASTER_ID
            , MB_INFO_ID
            , REG_DD
        ) VALUES (
              #{mngOrgCd}
            , #{masterId}
            , #{mbInfoId}
            , SYSDATE()
        )
    </insert>
    
    <select id="selectSeeTogetherList" parameterType="CollectionVO" resultType="CollectionVO">
    /* CollectionDAO.selectSeeTogetherList 함께 볼만한 자료 리스트*/
    <![CDATA[
        SELECT 
                    M.MASTER_ID
                  , M.TITLE
                  , M.TYPE_DIV_CD
                  , C1.S_CODE_NM AS TYPE_DIV_NM 
                  , M.DTLS_TYPE_DIV_CD
                  , C2.S_CODE_NM AS DTLS_TYPE_DIV_NM
                  , M.SUBJECT_CODE 
                  , (  SELECT CLSF_NM 
                       FROM CT_CLSF
                       WHERE CLSF_ID = (
                           SELECT UP_CLSF_ID FROM CT_CLSF
                           WHERE CLSF_ID = CAST(M.SUBJECT_CODE AS CHAR(35)) )
                       AND UP_CLSF_ID = '2') AS SUBJECT_NM_UP
                  , (SELECT CLSF_NM FROM CT_CLSF WHERE CLSF_ID = CAST(M.SUBJECT_CODE AS CHAR(35))) AS SUBJECT_NM
                  , M.MOD_DD 
                  , M.ORG_NM
                  , M.CREAT_YYYY
                  , M.RPRS_THUMB_URL
                FROM
                (SELECT
                         TT2.*
                    FROM (
                        SELECT
                            MASTER_ID,
                            COUNT(KEY_NAME) AS CNT
                        FROM
                            (
                            SELECT
                                DISTINCT RM.MASTER_ID,
                                TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(replace(RM.KEYWORD, ',', ';'), ';', NUMBERS.N), ';', -1)) AS KEY_NAME
                            FROM (
                                WITH RECURSIVE CTE AS (
                                     SELECT 1 AS N
                                     UNION ALL
                                     SELECT 1+CTE.N AS N
                                     FROM CTE
                                     WHERE CTE.N < 500
                                )
                                SELECT N FROM CTE       
                            ) NUMBERS
                            INNER JOIN RG_MASTER RM
                                ON CHAR_LENGTH(RM.KEYWORD)-CHAR_LENGTH(REPLACE(replace(RM.KEYWORD, ',', ';'), ';', '')) >= NUMBERS.N-1
                                AND RM.MNG_ORG_CD = #{mngOrgCd}
                                AND RM.OPEN_DIV_CD != '3'
                                AND RM.REG_STATUS = 'REG_OK'
                                AND RM.USE_FLAG = 'Y'
                                AND RM.MASTER_ID != #{masterId}
                                AND IFNULL(RM.PE_STATUS, '') = ''     /* 보존상태 */
                                AND ( RM.OPER_OUT_RANGE_CD = '01' OR RM.OPER_READ_RANGE_CD = '01' OR RM.DIGITAL_READ_YN = 'Y')/* 대출 OR 열람 OR 온라인열람 가능 */
                                ) T
                        WHERE 1=1
                            AND T.KEY_NAME != '-'
                            AND T.KEY_NAME IN (
                            SELECT  TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(replace(RM.KEYWORD, ',', ';'), ';', NUMBERS.N), ';', -1)) AS KEY_NAME
                            FROM (
                                WITH RECURSIVE CTE AS (
                                     SELECT 1 AS N
                                     UNION ALL
                                     SELECT 1+CTE.N AS N
                                     FROM CTE
                                     WHERE CTE.N < 500
                                )
                                SELECT N FROM CTE       
                            ) NUMBERS
                            INNER JOIN RG_MASTER RM 
                                ON CHAR_LENGTH(RM.KEYWORD)-CHAR_LENGTH(REPLACE(replace(RM.KEYWORD, ',', ';'), ';', '')) >= NUMBERS.N-1 
                                AND RM.MASTER_ID = #{masterId}
                        )
                        GROUP BY MASTER_ID
                    ) TT1
                    INNER JOIN RG_MASTER TT2 ON TT1.MASTER_ID = TT2.MASTER_ID
                    ORDER BY TT1.CNT DESC, TT2.MOD_DD DESC, TT2.REG_DD DESC
                    LIMIT 10) M
                    LEFT OUTER JOIN SM_CODE_S C1 ON M.TYPE_DIV_CD = C1.S_CODE_ID AND C1.L_CODE_ID = 'MASTER_TYPE_DIV_CD'
                    LEFT OUTER JOIN SM_CODE_S C2 ON M.DTLS_TYPE_DIV_CD = C2.S_CODE_ID AND C2.L_CODE_ID = 'DTLS_TYPE_DIV_CD'
                    ]]>
    </select>
    
    
    <select id="selectSimilarList" parameterType="CollectionVO" resultType="CollectionVO">
    /* CollectionDAO.selectSimilarList 유사한자료 리스트*/
       SELECT 
            M.MASTER_ID
          , M.TITLE
          , M.TYPE_DIV_CD
          , C1.S_CODE_NM AS TYPE_DIV_NM 
          , M.DTLS_TYPE_DIV_CD
          , C2.S_CODE_NM DTLS_TYPE_DIV_NM
          , M.SUBJECT_CODE 
          , (  SELECT CLSF_NM 
               FROM CT_CLSF
               WHERE CLSF_ID = (
                   SELECT UP_CLSF_ID FROM CT_CLSF
                   WHERE CLSF_ID = CAST(M.SUBJECT_CODE AS CHAR(35)) )
               AND UP_CLSF_ID = '2') AS SUBJECT_NM_UP
          , (SELECT CLSF_NM FROM CT_CLSF WHERE CLSF_ID = CAST(M.SUBJECT_CODE AS CHAR(35))) AS SUBJECT_NM
          , M.MOD_DD 
          , M.ORG_NM
          , M.CREAT_YYYY
          , M.RPRS_THUMB_URL
          , IFNULL(M.M_INTERFACE_ID, '') AS INTERFACE_ID
          , B.DEPT_NM
          , B.DOMAIN_HOST_NM
        FROM RG_MASTER M 
        LEFT OUTER JOIN SM_CODE_S C1 ON M.TYPE_DIV_CD = C1.S_CODE_ID AND C1.L_CODE_ID = 'MASTER_TYPE_DIV_CD'
        LEFT OUTER JOIN SM_CODE_S C2 ON M.DTLS_TYPE_DIV_CD = C2.S_CODE_ID AND C2.L_CODE_ID = 'DTLS_TYPE_DIV_CD'
        LEFT OUTER JOIN SM_DEPT B ON M.MNG_ORG_CD = B.ORG_CD
        WHERE 1=1
        AND M.OPEN_DIV_CD = '1'             /* 공개구분 : 공개 */
        AND M.REG_STATUS = 'REG_OK'         /* 등록상태 : 등록완료 */
        AND M.USE_FLAG = 'Y'
        AND IFNULL(M.PE_STATUS, '') = ''    /* 보존상태 */ 
        AND ( M.OPER_OUT_RANGE_CD = '01' OR M.OPER_READ_RANGE_CD = '01' OR M.DIGITAL_READ_YN = 'Y')/* 대출 OR 열람 OR 온라인열람 가능 */
        AND REPLACE(M.TITLE,' ','') = REPLACE(#{title},' ','')
        AND M.CREAT_YYYY LIKE CONCAT(#{creatYyyy},'%') 
        AND M.ORG_NM = REPLACE(#{orgNm},' ','')
        AND M.MASTER_ID != #{masterId}
        ORDER BY M.MOD_DD DESC 
        LIMIT 10
    </select>
</mapper>