<?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.style24.admin.biz.dao.TsaLoginDao">

	<!-- ID로 사용자 조회 -->
	<select id="getUserById" parameterType="String" resultType="User">
		/* TsaLogin.getUserById */
		SELECT USER_NO                                                     /*사용자명*/
		     , USER_ID                                                     /*사용자ID*/
		     , USER_NM                                                     /*사용자명*/
		     , PASSWD                                                      /*비밀번호*/
		     , ROLE_CD                                                     /*권한코드*/
		     , FN_GET_CODE_NM('G001',ROLE_CD)            AS ROLE_NM        /*권한명*/
		     , ROLE_REF_VAL                                                /*권한관련값*/
		     , CASE WHEN SUBSTR(REPLACE(ROLE_CD,'G001_', ''),1,1) IN ('A','B','E') THEN
		                ROLE_REF_VAL
		            END                                  AS SUPPLY_COMP_CD /*공급업체코드
		     , CASE WHEN SUBSTRING(REPLACE(ROLE_CD,'G001_', ''),1,1) IN ('A','B','E') THEN
		                (SELECT SUPPLY_COMP_NM
		                 FROM   TB_SUPPLY_COMPANY
		                 WHERE  SUPPLY_COMP_CD = A.ROLE_REF_VAL
		                )
		       END                                       AS SUPPLY_COMP_NM /*공급업체명*/
		     , CASE WHEN SUBSTRING(REPLACE(ROLE_CD,'G001_', ''),1,1) = 'C' THEN
		                ROLE_REF_VAL
		       END                                       AS VENDOR_ID      /*벤더ID*/
		     , CASE WHEN SUBSTRING(REPLACE(ROLE_CD,'G001_', ''),1,1) = 'C' THEN
		                FN_GET_CODE_NM('G003',ROLE_REF_VAL)
		       END                                       AS VENDOR_NM      /*벤더명*/
		     , CASE WHEN SUBSTRING(REPLACE(ROLE_CD,'G001_', ''),1,1) = 'D' THEN
		                ROLE_REF_VAL
		       END                                       AS AF_LINK_CD     /*제휴링크코드*/
		     , CASE WHEN SUBSTRING(REPLACE(ROLE_CD,'G001_', ''),1,1) = 'D' THEN
		                (SELECT AF_LINK_NM
		                 FROM   TB_AF_LINK
		                 WHERE  AF_LINK_CD = A.ROLE_REF_VAL
		                )
		       END                                       AS AF_LINK_NM     /*제휴링크명*/
		     , CASE WHEN SUBSTRING(REPLACE(ROLE_CD,'G001_', ''),1,1) = 'D' THEN
		                (SELECT AF_CHANNEL
		                 FROM   TB_AF_LINK
		                 WHERE  AF_LINK_CD = A.ROLE_REF_VAL
		                )
		       END                                       AS AF_CHANNEL     /*제휴채널*/
		     , CASE WHEN SUBSTRING(REPLACE(ROLE_CD,'G001_', ''),1,1) = 'D' THEN
		                (SELECT FN_GET_CODE_NM('G053',AL.AF_CHANNEL)
		                 FROM   TB_AF_LINK AL
		                 WHERE  AL.AF_LINK_CD = A.ROLE_REF_VAL
		                )
		       END                                       AS AF_CHANNEL_NM  /*제휴채널명*/
		     , EMAIL                                                       /*이메일*/
		     , CELL_PHNNO                                                  /*휴대전화번호*/
		     , PNT_ASSIGN_AMT                                              /*포인트부여가능금액*/
		     , IP_ADDR                                                     /*IP주소*/
		     , IP_CHK_YN                                                   /*IP체크여부*/
		     , DATE_FORMAT(PASSWD_CHG_DT,'%Y%m%d%H%i%S') AS PASSWD_CHG_DT  /*비밀번호변경일시*/
		     , MASKING_YN                                                  /*마스킹여부*/
		FROM   TB_USER A
		WHERE  USER_ID = #{userId}
		AND    NOT (ROLE_CD LIKE 'G001_B%' OR ROLE_CD LIKE 'G001_E%') /*입점업체담당자, 촬영업체담당자*/
		AND    USE_YN = 'Y'
	</select>
	
	<!-- 로그인실패 남기기 -->
	<insert id="createLoginFail" parameterType="User">
		/* TsaLogin.createLoginFail */
		INSERT INTO TB_USER_LOGIN_FAIL (
		       USER_NO
		     , IP_ADDR
		     , LOGIN_FAIL_CNT
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
		VALUES (
		       #{userNo}
		     , #{ipAddr}
		     , 1
		     , #{userNo}
		     , NOW()
		     , #{userNo}
		     , NOW()
		)
		ON DUPLICATE KEY UPDATE
		       LOGIN_FAIL_CNT = CASE WHEN #{loginFailYn} = 'Y' THEN LOGIN_FAIL_CNT + 1 ELSE 0 END
		     , UPD_NO = #{userNo}
		     , UPD_DT = NOW()
	</insert>

	<!-- 로그인 실패건수 조회 -->
	<select id="getLoginFailCount" parameterType="User" resultType="int">
		/* TsaLogin.getLoginFailCount */
		SELECT IFNULL((SELECT LOGIN_FAIL_CNT
		               FROM   TB_USER_LOGIN_FAIL
		               WHERE  USER_NO = #{userNo}
		               AND    IP_ADDR = #{ipAddr}
		              ),0) AS CNT
		FROM   DUAL
	</select>

	<!-- 최종로그인일시 Update -->
	<update id="updateLastLoginDate" parameterType="Integer">
		/* TsaLogin.updateLastLoginDate */
		UPDATE TB_USER
		SET    LOGIN_LDT = NOW()
		     , UPD_NO = #{userNo}
		     , UPD_DT = NOW()
		WHERE  USER_NO = #{userNo}
	</update>

	<!-- 로그인이력 남기기 -->
	<insert id="createLoginHistory" parameterType="User">
		/* TsaLogin.createLoginHistory */
		INSERT INTO TB_USER_LOGIN_HST (
		       USER_LOGIN_SQ
		     , USER_NO
		     , IP_ADDR
		     , LOGIN_DT
		     , REG_NO
		     , REG_DT
		)
		VALUES (
		       NULL
		     , #{userNo}
		     , #{ipAddr}
		     , NOW()
		     , #{userNo}
		     , NOW()
		)
	</insert>

	<!-- 로그인 메뉴 목록 -->
	<select id="getLoginMenuList" parameterType="Integer" resultType="Menu">
		/* TsaLogin.getLoginMenuList */
		WITH RECURSIVE CONNECTBY2 AS (
		    WITH RECURSIVE CONNECTBY1 AS (
		        SELECT 1                                                  AS MENU_LVL
		             , PMENU_ID
		             , MENU_ID
		             , MENU_NM
		             , MENU_NM                                            AS NAVIGATION
		             , MENU_GB
		             , MENU_DESC
		             , MENU_URL
		             , CONCAT(MENU_ID,LPAD(CAST(DISP_ORD AS CHAR),4,'0')) AS DISP_ORD
		        FROM   TB_MENU
		        WHERE  PMENU_ID = 'ROOT'
		        AND    USE_YN = 'Y'
		        
		        UNION ALL
		        
		        SELECT A.MENU_LVL + 1                                          AS MENU_LVL
		             , B.PMENU_ID
		             , B.MENU_ID
		             , B.MENU_NM
		             , CONCAT(A.NAVIGATION,' > ',B.MENU_NM)                    AS NAVIGATION
		             , B.MENU_GB
		             , B.MENU_DESC
		             , B.MENU_URL
		             , CONCAT(A.DISP_ORD,LPAD(CAST(B.DISP_ORD AS CHAR),4,'0')) AS DISP_ORD
		        FROM   CONNECTBY1 A
		        INNER JOIN TB_MENU B ON A.MENU_ID = B.PMENU_ID
		                            AND B.USE_YN = 'Y'
		    )
		    SELECT MENU_LVL
		         , PMENU_ID
		         , MENU_ID
		         , MENU_NM
		         , NAVIGATION
		         , MENU_GB
		         , MENU_DESC
		         , MENU_URL
		         , DISP_ORD
		    FROM   CONNECTBY1
		    WHERE  MENU_ID IN (SELECT MENU_ID
		                       FROM   TB_USER_MENU
		                       WHERE  USER_NO = #{userNo}
		                       AND    USE_ROLE != 'NNNN'
		                      )
		    
		    UNION ALL
		    
		    SELECT B.MENU_LVL
		         , B.PMENU_ID
		         , B.MENU_ID
		         , B.MENU_NM
		         , B.NAVIGATION
		         , B.MENU_GB
		         , B.MENU_DESC
		         , B.MENU_URL
		         , B.DISP_ORD
		    FROM   CONNECTBY2 A
		    INNER JOIN CONNECTBY1 B ON A.PMENU_ID = B.MENU_ID
		)
		SELECT DISTINCT
		       PMENU_ID               /*상위메뉴ID*/
		     , MENU_ID                /*메뉴ID*/
		     , MENU_NM                /*메뉴명*/
		     , NAVIGATION             /*네비게이션*/
		     , MENU_DESC              /*메뉴설명*/
		     , MENU_URL               /*메뉴URL*/
		     , MENU_LVL               /*메뉴레벨*/
		     , CASE WHEN IFNULL((SELECT MAX(MENU_LVL)
		                         FROM   CONNECTBY2
		                         WHERE  PMENU_ID = A.MENU_ID
		                        ),0) > 0 THEN 1
		            ELSE 0
		       END        AS LEAF_LVL /*말단레벨*/
		     , RANK() OVER(ORDER BY DISP_ORD) AS RK
		FROM   CONNECTBY2 A
		WHERE  MENU_ID NOT LIKE 'P%' /*팝업메뉴제외*/
	</select>

</mapper>