<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="User">

	<statement id="selectList" resultClass="java.util.HashMap">
		SELECT *
		FROM (
		 	<!-- SELECT ROW_NUMBER() OVER (ORDER BY T02.USERID) ROWNUM, -->
		 	SELECT @NO := @NO + 1 AS ROWNO
		 	     ,RM02.USERID                 ,RM02.USERNAME           
				 ,RM02.ROLEIDNFINAME  		  ,'' AS ROLENAME 
                 ,RM02.PAFIARINFOBRNCD        ,RM02.CPHNNO      
                 ,RM02.OFCTELNO               ,RM02.EMAD       
                 ,RM02.JOBCLCD                ,RM02.JOBTLNAME 
                 ,RM02.LASTAMNDYMS            ,RM02.DVSNNAME   
                 ,RM02.INTNLOUTSRCDSTICNAME   ,RM02.EAIGROUPCODSTCD
                 ,RM02.SPOTPRXYNAME           ,RM02.SPOTPRXYEMPID 
                 ,RM02.TEAMNAME
			FROM 
                $schemaId$.TSEAIRM02 RM02, (SELECT @NO := 0) R
			<dynamic prepend="WHERE">
				<isNotEmpty prepend="AND" property="searchUserId">
					UPPER(USERID) LIKE  CONCAT( '%' , UPPER(#searchUserId#) , '%' )
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchUserName">
					UPPER(USERNAME) LIKE  CONCAT( '%' , UPPER(#searchUserName#) , '%' )
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchRoleName">
					USERID IN (SELECT DISTINCT USERID FROM $schemaId$.TSEAIRM04 WHERE ROLEID LIKE CONCAT( '%' , #searchRoleName# , '%') )
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchDepart">
					UPPER(DVSNNAME) LIKE  CONCAT( '%' , UPPER(#searchDepart#) , '%' )
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchTeamName">
					UPPER(TEAMNAME) LIKE  CONCAT( '%' , UPPER(#searchTeamName#) , '%' )
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchPosition">
					UPPER(JOBTLNAME) LIKE  CONCAT( '%' , UPPER(#searchPosition#) , '%' )
				</isNotEmpty>
			</dynamic>				
			) ORGTABLE
		WHERE ROWNO BETWEEN #startNum# AND #endNum#
	</statement>	

	<statement id="selectListCount" resultClass="java.lang.Integer">
		 SELECT  
			COUNT(*) 
        FROM 
        	$schemaId$.TSEAIRM02 RM02
		<dynamic prepend="WHERE">
			<isNotEmpty prepend="AND" property="searchUserId">
				UPPER(USERID) LIKE  CONCAT( '%' , UPPER(#searchUserId#) , '%' )
			</isNotEmpty>
			<isNotEmpty prepend="AND" property="searchUserName">
				UPPER(USERNAME) LIKE  CONCAT( '%' , UPPER(#searchUserName#) , '%' )
			</isNotEmpty>
			<isNotEmpty prepend="AND" property="searchRoleName">
				USERID IN (SELECT DISTINCT USERID FROM $schemaId$.TSEAIRM04 WHERE ROLEID LIKE CONCAT( '%' , #searchRoleName# , '%') )
			</isNotEmpty>
			<isNotEmpty prepend="AND" property="searchDepart">
				UPPER(DVSNNAME) LIKE  CONCAT( '%' , UPPER(#searchDepart#) , '%' )
			</isNotEmpty>
			<isNotEmpty prepend="AND" property="searchTeamName">
				UPPER(TEAMNAME) LIKE  CONCAT( '%' , UPPER(#searchTeamName#) , '%' )
			</isNotEmpty>
			<isNotEmpty prepend="AND" property="searchPosition">
				UPPER(JOBTLNAME) LIKE  CONCAT( '%' , UPPER(#searchPosition#) , '%' )
			</isNotEmpty>
		</dynamic>	
	</statement>	
	
	<statement id="selectDetail" resultClass="java.util.HashMap">
	 	SELECT 
			 T02.USERID					 AS "USERID"
			,T02.USERNAME				 AS "USERNAME"
			,T02.ROLEIDNFINAME			 AS "ROLEIDNFINAME"
			,T02.PAFIARINFOBRNCD		 AS "PAFIARINFOBRNCD"
			,T02.CPHNNO					 AS "CPHNNO"
			,T02.OFCTELNO	             AS "OFCTELNO"
			,T02.EMAD		             AS "EMAD"
			,T02.JOBCLCD	             AS "JOBCLCD"
			,T02.JOBTLNAME	           	 AS "JOBTLNAME"
			,T02.LASTAMNDYMS	         AS "LASTAMNDYMS"
			,T02.DVSNNAME	             AS "DVSNNAME"
			,T02.INTNLOUTSRCDSTICNAME	 AS "INTNLOUTSRCDSTICNAME"
			,T02.EAIGROUPCODSTCD	 	 AS "EAIGROUPCODSTCD"
			,T02.SPOTPRXYNAME	         AS "SPOTPRXYNAME"
			,T02.SPOTPRXYEMPID	 		 AS "SPOTPRXYEMPID"
			,T02.TEAMNAME				 AS "TEAMNAME"
			,T02.SECONDMENTBRNCD		 AS "SECONDMENTBRNCD"
			,T02.SECONDMENTSTDT  		 AS "SECONDMENTSTDT"
			,T02.SECONDMENTENDT			 AS "SECONDMENTENDT"	
		FROM $schemaId$.TSEAIRM02 T02 
		WHERE USERID = #userId#
	</statement>  

	<statement id="selectRole" resultClass="java.util.HashMap">
	 	SELECT ROLEID AS "ROLEID"
				 , ROLENAME AS "ROLENAME"				 
		      FROM $schemaId$.TSEAIRM05
		   WHERE UseYn ='Y'	
	</statement> 
		
	<insert id="insert" parameterClass="java.util.HashMap">
		INSERT INTO $schemaId$.TSEAIRM02(
			 USERID
			,USERNAME
			,ROLEIDNFINAME
			,PAFIARINFOBRNCD
			,CPHNNO
			,OFCTELNO
			,EMAD
			,JOBCLCD
			,JOBTLNAME
			,LASTAMNDYMS
			,DVSNNAME
			,TEAMNAME
			,INTNLOUTSRCDSTICNAME
			,EAIGROUPCODSTCD
			,SPOTPRXYNAME
			,SPOTPRXYEMPID
			,PASSWORD			    
		)
    	VALUES ( 
      		 #userId#					      
			,#userName#							
			,#roleidnfiname#				  
			,#pafiarinfobrncd#	
			,#cphnno#								
			,#ofctelno#	            
			,#emad#		              
			,#jobclcd#	              
			,#jobtlname#	            
			,TO_CHAR(LOCALTIMESTAMP,'YYYYMMDDHH24MISS')          
			,#dvsnname#	
			,#teamName#	             
			,#intnloutsrcdsticname#	
			,#eaigroupcodstcd#
			,#spotprxyname#	        
			,#spotprxyempid#
			,#password#	     
    	 )
	</insert>	
		
	<statement id="update" parameterClass="java.util.HashMap" resultClass="java.lang.Integer">
	 	UPDATE $schemaId$.TSEAIRM02 
	 	    SET USERNAME				  = #userName#				
			,ROLEIDNFINAME            = #roleidnfiname#		
			,PAFIARINFOBRNCD		  = #pafiarinfobrncd#	                          
			,CPHNNO                   = #cphnno#								                             
			,OFCTELNO                 = #ofctelno#	                                         
			,EMAD                     = #emad#		                                           
			,JOBCLCD                  = #jobclcd#	                                         
			,JOBTLNAME                = #jobtlname#	                                       
			,LASTAMNDYMS              = TO_CHAR(LOCALTIMESTAMP,'YYYYMMDDHH24MISS')          
			,DVSNNAME                 = #dvsnname#	                                         
			,TEAMNAME                 = #teamName#	                                         
			,INTNLOUTSRCDSTICNAME     = #intnloutsrcdsticname#	                             
			,EAIGROUPCODSTCD          = #eaigroupcodstcd#	                                 
			,SPOTPRXYNAME             = #spotprxyname#	                                     
			,SPOTPRXYEMPID            = #spotprxyempid#	          
	   WHERE USERID        = #userId#
	</statement>
	
	<statement id="updatePassword" parameterClass="java.util.HashMap" resultClass="java.lang.Integer">
	 	UPDATE $schemaId$.TSEAIRM02 
	 	   SET PASSWORD      = #password#
	   WHERE USERID        = #userId#
	</statement>
	
	<statement id="selectUserRoleList" resultClass="java.util.HashMap">
			SELECT R05.ROLEID ROLEID
				 , R05.ROLENAME ROLENAME
				 , R05.ROLEDESC ROLEDESC
				 , (case when R04.ROLEID = null then 'false' else 'true' end ) CHK
		      FROM $schemaId$.TSEAIRM05 R05
		      LEFT OUTER JOIN $schemaId$.TSEAIRM04 R04 ON 
		       ( R05.ROLEID = R04.ROLEID	AND R04.USERID = #userId# )
		   WHERE R05.UseYn ='Y'		   
	</statement>
		
	<statement id="insertUserRole" parameterClass="java.util.HashMap" resultClass="java.lang.Integer">
	 	INSERT INTO $schemaId$.TSEAIRM04(USERID, ROLEID) 
		VALUES	(#userId#, #roleId#)	
	</statement>
			
	<statement id="selectRoleCnt" parameterClass="java.util.HashMap" resultClass="java.lang.Integer">
	 	SELECT COUNT(USERID) FROM $schemaId$.TSEAIRM04 
	 	WHERE 	USERID = #USERID# 
	</statement>	
	
	<statement id="deleteUserRole" parameterClass="java.util.HashMap" resultClass="java.lang.Integer">
	 	DELETE 	FROM $schemaId$.TSEAIRM04 
	 	WHERE 	USERID = #userId# 
	</statement>	
	
	<statement id="selectUserCnt" parameterClass="java.util.HashMap" resultClass="java.lang.Integer">
	 	SELECT COUNT(USERID) FROM $schemaId$.TSEAIRM02 
	 	WHERE 	USERID = #USERID# 
	</statement>
		
	<insert id="insertHrms" parameterClass="java.util.HashMap">
		INSERT INTO $schemaId$.TSEAIRM02(
			 USERID					 
			,USERNAME							
			,ROLEIDNFINAME				 
			,PAFIARINFOBRNCD			 
			,CPHNNO								
			,OFCTELNO	           
			,EMAD		             
			,JOBCLCD	             
			,JOBTLNAME	           
			,LASTAMNDYMS	         
			,DVSNNAME	           
			,INTNLOUTSRCDSTICNAME	
			,EAIGROUPCODSTCD	     
			,SPOTPRXYNAME	       
			,SPOTPRXYEMPID
			,TEAMNAME	
			,SECONDMENTBRNCD
			,SECONDMENTSTDT  
			,SECONDMENTENDT	  
		    )
    	VALUES ( 
             #USERID#					      
			,#USERNAME#							
			,#ROLEIDNFINAME#				  
			,#PAFIARINFOBRNCD#			  
			,#CPHNNO#								
			,#OFCTELNO#	            
			,#EMAD#		              
			,#JOBCLCD#	              
			,#JOBTLNAME#	            
			,DATE_FORMAT(SYSDATE(), '%Y%m%d%H%i%s')         
			,#DVSNNAME#	            
			,#INTNLOUTSRCDSTICNAME#	
			,#EAIGROUPCODSTCD#	      
			,#SPOTPRXYNAME#	        
			,#SPOTPRXYEMPID#	
			,#TEAMNAME#        
			,#SECONDMENTBRNCD#
			,#SECONDMENTSTDT# 
			,#SECONDMENTENDT#	
           )
	</insert>	
		
	<statement id="updateHrms" parameterClass="java.util.HashMap" resultClass="java.lang.Integer">
	 	UPDATE $schemaId$.TSEAIRM02 
	 	   SET USERNAME				= #USERNAME#				
			,PAFIARINFOBRNCD 		= #PAFIARINFOBRNCD#			                              
			,CPHNNO					= #CPHNNO#								                              
			,OFCTELNO	         	= #OFCTELNO#	                                          
			,EMAD		        	= #EMAD#		                                            
			,JOBCLCD	         	= #JOBCLCD#	                                          
			,JOBTLNAME	         	= #JOBTLNAME#	                                        
			,LASTAMNDYMS			= DATE_FORMAT(SYSDATE(), '%Y%m%d%H%i%s')           
	 		,DVSNNAME	            = #DVSNNAME#	                                          
			,INTNLOUTSRCDSTICNAME	= #INTNLOUTSRCDSTICNAME#	                              
			,EAIGROUPCODSTCD	    = #EAIGROUPCODSTCD#	                                  
			,SPOTPRXYNAME	        = #SPOTPRXYNAME#	                                      
			,SPOTPRXYEMPID	  		= #SPOTPRXYEMPID# 
			,TEAMNAME				= #TEAMNAME# 
			,SECONDMENTBRNCD		= #SECONDMENTBRNCD#
			,SECONDMENTSTDT 		= #SECONDMENTSTDT#
			,SECONDMENTENDT			= #SECONDMENTENDT#                
	   WHERE USERID        = #USERID#
	</statement>
	
	<statement id="mergeRM02" parameterClass="java.util.HashMap">
	 INSERT INTO $schemaId$.TSEAIRM02 
		 (USERID ,USERNAME ,ROLEIDNFINAME, PAFIARINFOBRNCD	,CPHNNO ,OFCTELNO,EMAD	
		,JOBCLCD,JOBTLNAME, LASTAMNDYMS ,DVSNNAME ,INTNLOUTSRCDSTICNAME ,EAIGROUPCODSTCD
		,SPOTPRXYNAME,SPOTPRXYEMPID)
		VALUES (#USERID# ,#USERNAME# ,#ROLEIDNFINAME#, #PAFIARINFOBRNCD#,#CPHNNO# ,#OFCTELNO#, #EMAD#	
		,#JOBCLCD#,#JOBTLNAME#, #LASTAMNDYMS# ,#DVSNNAME# ,#INTNLOUTSRCDSTICNAME# ,#EAIGROUPCODSTCD#
		,#SPOTPRXYNAME#, #SPOTPRXYEMPID#)
	 ON DUPLICATE KEY UPDATE     
		  USERNAME			  =	 #USERNAME#
		,PAFIARINFOBRNCD	  =  #PAFIARINFOBRNCD#
		,CPHNNO				  =  #CPHNNO#
		,OFCTELNO			  =  #OFCTELNO#
		,EMAD				  =  #EMAD#
		,JOBCLCD			  =  #JOBCLCD#
		,JOBTLNAME			  =  #JOBTLNAME#
		,LASTAMNDYMS 		  =  #LASTAMNDYMS#
		,DVSNNAME			  =  #DVSNNAME#
		,INTNLOUTSRCDSTICNAME =  #INTNLOUTSRCDSTICNAME#
		,EAIGROUPCODSTCD	  =  #EAIGROUPCODSTCD#
		,SPOTPRXYNAME		  =  #SPOTPRXYNAME#
		,SPOTPRXYEMPID		  =  #SPOTPRXYEMPID#
	</statement>
	
	<statement id="selectUserInfo" resultClass="java.util.HashMap">
		SELECT  
		  USERID    AS "USERID"           
		 ,DVSNNAME  AS "DVSNNAME"
		 ,TEAMNAME  AS "TEAMNAME"     
		 ,OFCTELNO  AS "OFCTELNO"            
	   FROM $schemaId$.TSEAIRM02
	   WHERE USERID IN ($USERIDS$)
	</statement>
	
	<statement id="selectAdminRoleUser" resultClass="java.util.HashMap">
	 	SELECT
	 		RM02.USERID AS "USERID"
	 	   ,RM04.ROLEID AS "ROLEID"
	    FROM $schemaId$.TSEAIRM02 RM02 ,$schemaId$.TSEAIRM04 RM04
	   WHERE RM02.USERID = RM04.USERID
	    AND RM04.ROLEID = #roleId#
	</statement>  
	
	<statement id="selectBossList" parameterClass="java.util.HashMap" resultClass="java.util.HashMap">
  		SELECT USERID AS "CODE", USERNAME AS "NAME"
			FROM $schemaId$.TSEAIRM02 
		WHERE JOBTLNAME = '팀장' 
		<isEmpty prepend="AND" property="secondMentBrnCd">
			DVSNNAME = (SELECT DVSNNAME FROM $schemaId$.TSEAIRM02 WHERE USERID= #userId# ) 
		</isEmpty>
		<isNotEmpty prepend="AND" property="secondMentBrnCd">
			PAFIARINFOBRNCD = #secondMentBrnCd#
		</isNotEmpty>
 	</statement>
 	
	<statement id="selectAgentList" resultClass="java.util.HashMap">
  		SELECT SPOTPRXYEMPID AS "BOSSNUM", SPOTPRXYNAME AS "BOSSNAME"
		FROM $schemaId$.TSEAIRM02 
		WHERE USERID= #userId#
 	</statement>
 	
 	<statement id="updateChangeRole" parameterClass="java.util.HashMap" resultClass="java.lang.Integer">
		UPDATE $schemaId$.TSEAIRM04
			SET ROLEID = 'dept_developer'
		WHERE USERID = #userId# AND ROLEID IN ('newbie','newboss')
 	</statement>
 	
 	<statement id="selectUserRoleCnt" parameterClass="java.util.HashMap" resultClass="java.lang.Integer">
	 	SELECT COUNT(USERID) FROM $schemaId$.TSEAIRM04 
	 	WHERE 	USERID = #userId# AND ROLEID = #chkRole#
	</statement>
	
	<statement id="updateUserRole" parameterClass="java.util.HashMap" resultClass="java.lang.Integer">
		UPDATE $schemaId$.TSEAIRM04
			SET ROLEID = #roleId#
	    WHERE USERID = #userId# AND ROLEID = #chkRole#
	</statement>
	
	<statement id="selectUsersByIdList" parameterClass="java.util.HashMap" resultClass="java.util.HashMap">
		SELECT * FROM $schemaId$.TSEAIRM02 RM02
		<dynamic prepend="WHERE RM02.USERID IN" open="(" close=")">
			<iterate property="userIdList" conjunction=", ">
                     #userIdList[]#
			</iterate>
		</dynamic>
	</statement>
	
	<statement id="selectUserServiceTypeList" parameterClass="java.util.HashMap" resultClass="java.util.HashMap">
		SELECT 
			USERID,
			USESERVICETYPE
		FROM 
			$schemaId$.TSEAIRM07
		WHERE
			USERID = #userId#
	</statement>

	<statement id="insertUserServiceType" parameterClass="java.util.HashMap" resultClass="java.util.HashMap">
		INSERT INTO
			$schemaId$.TSEAIRM07
		(USERID,
		USESERVICETYPE)
		VALUES
		(#userId#,
		#useServiceName#)
	</statement>
	
	<statement id="deleteUserServiceType" parameterClass="java.util.HashMap" resultClass="java.util.HashMap">
		DELETE
			$schemaId$.TSEAIRM07
		WHERE
			USERID = #userId#
		AND	USESERVICETYPE = #deleteServiceName#
	</statement>
	
	<statement id="insertUserServiceTypeAll" parameterClass="java.util.HashMap">
		INSERT INTO $schemaId$.TSEAIRM07
		(USERID, USESERVICETYPE) 
		VALUES (#userId#, #caseQuery#)
		ON DUPLICATE KEY UPDATE USESERVICETYPE = #caseQuery#
	</statement>
	
	<!-- UserSyncJob -->
	<statement id="selectCompanyList" parameterClass="java.util.HashMap" resultClass="java.util.HashMap">
		SELECT II.EMPNO		AS "EMPNO"
				,II.EMPY_NM		AS "EMPY_NM"
		FROM
		(
			SELECT T.EMPNO, T.EAI_SND_DATA_CRTN_DT, MAX(I.EAI_SND_DATA_CRTN_SEQ_NBR) EAI_SND_DATA_CRTN_SEQ_NBR
			FROM
			(
				SELECT EMPNO, MAX(EAI_SND_DATA_CRTN_DT) AS "EAI_SND_DATA_CRTN_DT"
				FROM $schemaId$.TB_API_ST_PRAR_I
				GROUP BY EMPNO
			) T
			JOIN $schemaId$.TB_API_ST_PRAR_I I ON T.EMPNO = I.EMPNO AND T.EAI_SND_DATA_CRTN_DT = I.EAI_SND_DATA_CRTN_DT
			GROUP BY T.EMPNO, T.EAI_SND_DATA_CRTN_DT
		) TT 
		LEFT JOIN $schemaId$.TB_API_ST_PRAR_I II
			ON TT.EMPNO = II.EMPNO AND TT.EAI_SND_DATA_CRTN_DT = II.EAI_SND_DATA_CRTN_DT AND TT.EAI_SND_DATA_CRTN_SEQ_NBR = II.EAI_SND_DATA_CRTN_SEQ_NBR
		WHERE II.HLOF_STS_CD = '10' AND II.ACS_STS_CD = '1' -- 재직여부:재직 / 계정상태:정상
	</statement>
	
	<statement id="selectDeleteCompanyList" parameterClass="java.util.HashMap" resultClass="java.util.HashMap">
		SELECT II.EMPNO		AS "EMPNO"	
		FROM
		(
			SELECT T.EMPNO, T.EAI_SND_DATA_CRTN_DT, MAX(I.EAI_SND_DATA_CRTN_SEQ_NBR) EAI_SND_DATA_CRTN_SEQ_NBR
			FROM
			(
				SELECT EMPNO, MAX(EAI_SND_DATA_CRTN_DT) AS "EAI_SND_DATA_CRTN_DT"
				FROM $schemaId$.TB_API_ST_PRAR_I
				GROUP BY EMPNO
			) T
			JOIN $schemaId$.TB_API_ST_PRAR_I I ON T.EMPNO = I.EMPNO AND T.EAI_SND_DATA_CRTN_DT = I.EAI_SND_DATA_CRTN_DT
			GROUP BY T.EMPNO, T.EAI_SND_DATA_CRTN_DT
		) TT 
		LEFT JOIN $schemaId$.TB_API_ST_PRAR_I II
			ON TT.EMPNO = II.EMPNO AND TT.EAI_SND_DATA_CRTN_DT = II.EAI_SND_DATA_CRTN_DT AND TT.EAI_SND_DATA_CRTN_SEQ_NBR = II.EAI_SND_DATA_CRTN_SEQ_NBR
		WHERE II.HLOF_STS_CD != '10' OR (II.HLOF_STS_CD = '10' AND (II.ACS_STS_CD = '2' OR II.ACS_STS_CD = '7')) -- 재직여부는 재직이 아니거나 / 재직이지만 계정상태가 정상이 아닌 경우
	</statement>
	
	<statement id="updateUserAccountLock" parameterClass="java.util.HashMap" resultClass="java.util.HashMap">
		UPDATE $schemaId$.TSEAIRM02
			SET STATUS = '2'
		WHERE STR_TO_DATE(IFNULL(LASTLOGINYMS, LASTAMNDYMS), '%Y%m%d%H%i%s') <![CDATA[ <= ]]> DATE_SUB(CURDATE(), INTERVAL 90 DAY) 
 	</statement>
	
    <statement id="updatePortalUserAccountLock" parameterClass="java.util.HashMap" resultClass="java.util.HashMap">
	    UPDATE  $schemaId$.PTL_USER
		SET ACCOUNT_LOCK_YN = 'Y'
			,USER_STATUS = 'DORMANT'
		WHERE LOGIN_ID IN (
				SELECT LOGIN_ID
				FROM
				(
				SELECT LOGIN_ID,
					IFNULL((
					SELECT MAX(LOGIN_TIME) 
					FROM PTL_USER_LOG
					),LAST_MODIFIED_DATE) LAST_LOGIN_TIME
				FROM PTL_USER
				) T
		 	   WHERE STR_TO_DATE(LAST_LOGIN_TIME, '%Y%m%d%H%i%S') <![CDATA[ <= ]]>  DATE_SUB(CURDATE(), INTERVAL 90 DAY) 
		)
  </statement>
		
</sqlMap>
