<?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="Security">

	<select id="Security.getFaLoginInfo" parameterType="String" resultType="loginUserVO">
	<![CDATA[
		SELECT  
  			A.USERID, A.PASSWD, A.IDNO, A.NAME, 
  			A.PLACE1, A.PLACE2, A.POSITION, A.DUTIES, 
  			A.LOGINCHK, A.USEGRADE, B.ROLE_LEVEL AS ROLELEVEL, A.CHKCNT
		FROM  
  			TAGENT A, 
  			T_SSO_AUTH_CODE B
		WHERE  
		  	B.CODE_ID = A.USEGRADE AND
		  	A.USERID = #{userId}
	]]>
	</select>
	
	<select id="Security.getCmmnLoginInfo" parameterType="String" resultType="loginUserVO">
	<![CDATA[	
		SELECT  
			A.USERID, A.PASSWD, A.IDNO, A.NAME, 
			A.PLACE1, A.PLACE2, A.POSITION,
			A.LOGINCHK, (SELECT ROLE_LEVEL FROM T_SSO_AUTH_CODE WHERE CODE_NM = 'NFA_USER' ) AS ROLELEVEL, A.CHKCNT
		FROM  
			TUSER A
		WHERE  
			A.USERID = #{userId}
	]]>
	</select>
	
	<select id="Security.getAuthoritiesFa" parameterType="String" resultType="securityRolesVO">
	<![CDATA[	
		SELECT  
			A.USERID,
      		B.CODE_NM AUTHORITY
		FROM  
			TAGENT A, 
			T_SSO_AUTH_CODE B
		WHERE 
			B.CODE_ID = A.USEGRADE AND
			A.USERID = #{userId}
	]]>
	</select>
	
	<select id="Security.getAuthoritiesCmmnUser" parameterType="String" resultType="securityRolesVO">
	<![CDATA[	
		SELECT  
			A.USERID,
            'NFA_USER' AUTHORITY
		FROM  
			TUSER A
		WHERE 
			A.USERID = #{userId}
	]]>
	</select>
	
	<select id="Security.getRolesAndUrl" resultType="map">
	<![CDATA[	
		SELECT 
			A.URL URL,
			B.CODE_NM AUTHORITY
		FROM 
			T_SSO_AUTH_URL A, 
			T_SSO_AUTH_CODE B
		WHERE 
			B.CODE_ID = A.AUTH_CODE
		ORDER BY
			A.PRIORITY
	]]>
	</select>
	
	<select id="Security.getHierarchyRoles" resultType="map">
	<![CDATA[
    SELECT
      (ROW_NUMBER() OVER()) AS RNUM, A.CHILDREN_ROLE CHILD, A.PARENTS_ROLE PARENT
    FROM
      T_SSO_HIERARCHY_ROLES A
        LEFT JOIN T_SSO_HIERARCHY_ROLES B
                  ON (A.CHILDREN_ROLE = B.PARENTS_ROLE)
    order by RNUM desc
	]]>
	</select>
	
	<update id="Security.updateLoginInfo"  parameterType="loginUserVO">
		UPDATE
		<choose>
			<when test='type == 1'>
				TAGENT
			</when>
			<otherwise>
				TUSER
			</otherwise>
		</choose>
	<![CDATA[
		SET		
			LOGINCHK = #{loginchk}	
		WHERE
			USERID = #{userid}
	]]>
	</update>
	
	<select id="Security.getReportAgentAuCode" parameterType="String" resultType="map">
	<![CDATA[	
		SELECT  
			A.AU_ID AS ID,
			A.AU_CODE AS CODE
		FROM  
			RPT_AGENT_AUTH A
		WHERE  
			A.AU_ID = #{userId}
	]]>
	</select>
	
	<select id="Security.getReportAgentApprove" parameterType="String" resultType="map">
	<![CDATA[	
		SELECT  
			A.AP_PLACE1 AS PLACE1, 
			A.AP_SECTION_ID AS SECTION, 
			A.AP_HEAD_ID AS HEAD
		FROM  
			RPT_AGENT_APPROVE A
		WHERE  
			A.AP_SECTION_ID = #{userId} OR
			A.AP_HEAD_ID = #{userId}
	]]>
	</select>
	
</mapper>
