<?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.getLoginInfo" parameterType="String" resultType="loginUserVO">
	<![CDATA[
	/* Security.getLoginInfo */
		SELECT 
			 T2.CC_CODE_NAME AS place1Str
			, T3.CC_CODE_NAME AS place2Str
			, T4.CC_CODE_NAME AS dutyStr
  			, T1.USR_ID  AS userid
  			, T1.USR_NAME AS name
  			, T1.USR_PWD AS passwd
  			, T1.USR_PWD_CHG loginchk
  			, T1.USR_DUTY duties
  			, T1.USR_PLACE1 AS place1
  			, T1.USR_PLACE2 AS place2
 			, IFNULL(T1.USR_RPT_AUTH, 0) as authCode
			, IFNULL(T1.USR_RPT_AUTH, 0) & 1 AS authSearch
			, IFNULL(T1.USR_RPT_AUTH, 0) & 2 AS authStatistics
			, IFNULL(T1.USR_RPT_AUTH, 0) & 4 AS authWaitList
			, IFNULL(T1.USR_RPT_AUTH, 0) & 8 AS authManage
			, IFNULL(T1.USR_RPT_AUTH, 0) & 16 AS authProxySection
			, IFNULL(T1.USR_RPT_AUTH, 0) & 32 AS authProxyHead
  			, T1.USR_LV USEGRADE
  			, T5.ROLE_LEVEL AS ROLELEVEL
		FROM  
  			H2_USER_INFO T1
  			LEFT JOIN H2_COMMON_CODE T2 ON T1.USR_PLACE1 = T2.CC_CODE 
			LEFT JOIN H2_COMMON_CODE T3 ON T1.USR_PLACE2 = T3.CC_CODE AND T1.USR_PLACE1 = T3.CC_GROUP
			LEFT JOIN H2_COMMON_CODE T4 ON T1.USR_DUTY = T4.CC_CODE AND T4.CC_GROUP = 'DUTY'
  			LEFT JOIN T_SSO_AUTH_CODE T5 ON T5.CODE_ID = T1.USR_LV 
  	WHERE  
		USR_ID = #{userId}
	]]>
	</select>
	
	<select id="Security.getCmmnLoginInfo" parameterType="String" resultType="loginUserVO">
	<![CDATA[
	/* Security.getCmmnLoginInfo */
		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.getAuthorities" parameterType="String" resultType="securityRolesVO">
	<![CDATA[
	/* Security.getAuthorities */
		SELECT  
			A.USR_ID AS userid,
      		B.CODE_NM AUTHORITY
		FROM  
			H2_USER_INFO A, 
			T_SSO_AUTH_CODE B
		WHERE 
			B.CODE_ID = A.USR_LV 
		AND A.USR_ID = #{userId}
	]]>
	</select>
	
	<select id="Security.getRolesAndUrl" resultType="map">
	<![CDATA[	
	/* Security.getRolesAndUrl */
		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">
	/* Security.getHierarchyRoles*/
			  select
			  	 @ROWNUM := @ROWNUM + 1 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
			     WHERE
			     (@ROWNUM := 0) = 0
			   order by RNUM desc;
	</select>
	
	<update id="Security.updateLoginInfo"  parameterType="loginUserVO">
	/* Security.updateLoginInfo*/
		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[
	/* Security.getReportAgentAuCode*/	
		SELECT  
			A.AU_ID AS ID,
			A.AU_CODE AS CODE
		FROM  
			RPT_AGENT_AUTH A
		WHERE  
			A.AU_ID = #{userId}
	]]>
	</select>
	
</mapper>
