<?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.cmm.service.impl.MainDAO">

    <!-- 추천키워드 목록 조회  -->
 	<select id="listKeywords" parameterType="String" resultType="EgovMap">
        <![CDATA[ 	  
		SELECT T.KEY_NAME, COUNT(KEY_NAME) AS CNT 
        FROM (
            SELECT          
                 TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(replace(RM.KEYWORD,',',';'), ';', NUMBERS.N), ';', -1)) AS KEY_NAME    
            FROM ( /* 키워드 길이 1000 이므로, 최대 가능 키워드수 500이나 속도 개선으로 10개로 제한 */ 
                 WITH RECURSIVE W1 AS (
	                 SELECT 1 AS N
	                 UNION ALL
	                 SELECT 1+W1.N AS N
	                 FROM W1
	                 WHERE W1.N < 10
                 )
                 SELECT N FROM W1
            ) NUMBERS       
            INNER JOIN RG_MASTER RM
            ON CHAR_LENGTH(RM.KEYWORD)-CHAR_LENGTH(REPLACE(replace(RM.KEYWORD,',',';'), ';', '')) >= NUMBERS.N-1 
	            AND RM.USE_FLAG = 'Y'
	            AND RM.MNG_ORG_CD = #{mngOrgCd} 
	            AND RM.OPEN_DIV_CD != '3'
	            AND RM.REG_STATUS = 'REG_OK'
	            AND IFNULL(RM.PE_STATUS, '') = ''     /* 보존상태 */
	            AND RM.OPER_READ_RANGE_CD != '03' /* 전체열람, 목록열람으로 한정 : 열람불가 제외 */  
        ) T
        WHERE KEY_NAME != '-'
        GROUP BY T.KEY_NAME
        ORDER BY CNT DESC, T.KEY_NAME
        LIMIT 20
		]]>
 	</select>
 	
    <!-- 문화원 PICK -->
    <select id="listOrgPicks" parameterType="String" resultType="CollectionVO">
        <![CDATA[     
		SELECT 
		    M1.MASTER_ID
		  , M1.TITLE
		  , M1.TYPE_DIV_CD
		  , TDC.S_CODE_NM AS TYPE_DIV_NM
		  , M1.RPRS_THUMB_URL 
		  , M1.REG_DD
		  , M1.MOD_DD
		FROM BD_NLIB_RECOM_MASTER R
		INNER JOIN RG_MASTER M1 
		ON R.MASTER_ID = M1.MASTER_ID 
		JOIN SM_CODE_S TDC ON M1.TYPE_DIV_CD = TDC.S_CODE_ID AND TDC.L_CODE_ID = 'MASTER_TYPE_DIV_CD'
		WHERE R.MNG_ORG_CD = #{mngOrgCd} 
           AND M1.OPEN_DIV_CD = '1'             /* 공개구분 : 공개 */
           AND M1.REG_STATUS = 'REG_OK'         /* 등록상태 : 등록완료 */
           AND IFNULL(M1.PE_STATUS, '') = ''     /* 보존상태 */ 
           AND M1.USE_FLAG = 'Y'
           ORDER BY M1.MOD_DD DESC, M1.REG_DD DESC
           LIMIT 10
        ]]>
    </select>
     
    <!-- 유형별인기자료 -->
    <select id="listPops" parameterType="CollectionVO" resultType="CollectionVO">
        /* listPops */   
        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 
          , CONCAT(
              NVL((
               SELECT CONCAT(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') , '')
               , (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
        FROM RG_MASTER M 
        INNER JOIN (
            /* 자료 상세조회 이력 현황 */ 
	        SELECT A.MASTER_ID 
	             , COUNT(1) AS CNT 
	        FROM MB_VIEW_LOG A
	        INNER JOIN RG_MASTER B ON (A.MASTER_ID = B.MASTER_ID AND A.MNG_ORG_CD = B.MNG_ORG_CD)
	        WHERE A.MNG_ORG_CD = #{mngOrgCd}
	        AND B.USE_FLAG = 'Y'
            AND B.OPEN_DIV_CD != '3'            /* 공개구분 : 공개, 부분공개 */
            AND B.REG_STATUS = 'REG_OK'         /* 등록상태 : 등록완료 */
            AND IFNULL(B.PE_STATUS, '') = ''    /* 보존상태 */ 
	        GROUP BY A.MASTER_ID 
            ORDER BY COUNT(1) DESC
            LIMIT 4) M2 ON (M.MASTER_ID = M2.MASTER_ID)        
        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'
		ORDER BY M2.CNT DESC, M.MOD_DD DESC, M.REG_DD DESC  
		LIMIT 4
    </select>
          
    <!-- 온라인자료관 -->
    <select id="listOnline" parameterType="String" resultType="CollectionVO">
        <![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 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
		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'
		WHERE M.MNG_ORG_CD = #{mngOrgCd}
        AND M.USE_FLAG = 'Y'
		AND M.OPEN_DIV_CD != '3'               /* 공개구분 : 공개 */
		AND M.REG_STATUS = 'REG_OK'            /* 등록상태 : 등록완료 */
		AND IFNULL(M.PE_STATUS, '') = ''       /* 보존상태 */ 
		AND M.DIGITAL_READ_YN = 'Y'            /* 디지탈열람여부 */ 
		AND IFNULL(M.M_INTERFACE_ID, '') != '' /* IF존재하는 건 */
		AND IFNULL(M.RPRS_THUMB_URL, '') != '' /* 썸네일 존재하는 자료에 한정 : 2021.11.15 엑티버 김혜준대리 내부근무 과장님통해 요청 */
		ORDER BY M.MOD_DD DESC, M.REG_DD DESC
		LIMIT 10
        ]]>
    </select>
    
    <!-- 메인 배너 롤링 : 2021.11.23 추가  -->
    <select id="listBannerInfos" parameterType="String" resultType="egovMap">
    <![CDATA[     
		SELECT  
		      A.ORDER_SEQ
		    , A.BANNER_ID
		    , A.MNG_ORG_CD
		    , A.TITLE
		    , A.BANNER_DESC
		    , A.URL
		    , A.ATTACH_FILE_ID
		    , B.FILE_STRE_PATH
		    , B.STRE_FILE_NM
		    , B.ORIGNL_FILE_NM
		FROM BD_NLIB_BANNER A
		LEFT OUTER JOIN BD_FILEDETAIL B ON (A.ATTACH_FILE_ID = B.ATTACH_FILE_ID AND IFNULL(B.USE_AT,'Y') = 'Y')
		WHERE A.MNG_ORG_CD = #{mngOrgCd}
		AND DATE_FORMAT(NOW(), '%Y%m%d%H') BETWEEN IFNULL(A.POST_START_DATE, '1901010100') AND IFNULL(A.POST_END_DATE  , '9999123124')
		AND IFNULL(USE_YN, 'Y') = 'Y'
		AND DELETE_YN = 'N'
		ORDER BY ORDER_SEQ    
    ]]>
    </select>


    <!-- 메인 배너 롤링 : 2021.11.24 추가  -->
    <select id="getBannerInfo" parameterType="String" resultType="egovMap">
    <![CDATA[     
        SELECT  
              A.ORDER_SEQ
            , A.BANNER_ID
            , A.MNG_ORG_CD
            , A.TITLE
            , A.BANNER_DESC
            , A.URL
            , A.ATTACH_FILE_ID
            , B.FILE_STRE_PATH
            , B.STRE_FILE_NM
            , B.ORIGNL_FILE_NM
        FROM BD_NLIB_BANNER A
        LEFT OUTER JOIN BD_FILEDETAIL B ON (A.ATTACH_FILE_ID = B.ATTACH_FILE_ID AND IFNULL(B.USE_AT,'Y') = 'Y')
        WHERE A.BANNER_ID = #{bannerId}
        AND IFNULL(USE_YN, 'Y') = 'Y'
        ORDER BY B.FILE_SN DESC 
        LIMIT 1
    ]]>
    </select>

</mapper>