<?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="kr.co.jinwoosi.clfd.framework.security.mapper.UserMapper">

	<select id="selectList" parameterType="userSearch" resultType="user">
		SELECT  *
		FROM (
			SELECT ROWNUM RNUM , LIST.*
			FROM (
			    SELECT USER_ID, USER_LOGIN_ID, USER_PWD, USER_NM, GENDER, BIRTH, PHONE, EMAIL, USER_ST_CD
			         , FN_CD_DTL_NM('userStCd', USER_ST_CD) AS USER_ST_CD_NM, USER_TY_CD, REG_ID, REG_DT, FAIL_CNT
				FROM JCMS_USER
				WHERE 1 = 1
					<if test="empty(userTyCd)" > AND USER_TY_CD != 'S' </if>
					<if test="!empty(userTyCd)" > AND USER_TY_CD = #{userTyCd} </if>
					<if test="!empty(userLoginIdLike)" > AND USER_LOGIN_ID LIKE '%'|| #{userLoginIdLike} ||'%' </if>
					<if test="!empty(userNmLike)" > AND USER_NM LIKE '%'|| #{userNmLike} ||'%' </if>
					<if test="!empty(userStCd)" > AND USER_ST_CD = #{userStCd} </if>
					<if test="!empty(userLoginId)" > AND USER_LOGIN_ID = #{userLoginId} </if>
					<if test="!empty(email)" > AND EMAIL = #{email} </if>
					<if test="findIdByPhoneFlag == true" >
						AND PHONE = #{phone}
					</if>
					<if test="findPwByPhoneAndIdFlag == true" >
				  		AND USER_LOGIN_ID = #{userLoginId}
						AND PHONE = #{phone}
					</if>
				<if test="!empty(sort) and !empty(sortOrd)">
				ORDER BY ${sort} ${sortOrd}
	 			</if>
	       	) LIST
		)
	    <if test="pagingYn == true" >
	    <![CDATA[			    
			 WHERE RNUM > #{start} AND RNUM <= #{start} + #{length}
		]]>
		</if>
	</select>
	
	<select id="checkIdDuplicate" parameterType="userSearch" resultType="java.lang.Integer">
		SELECT COUNT(USER_ID)
		FROM JCMS_USER
		WHERE USER_LOGIN_ID = #{userLoginId}
	</select>
	
	<select id="login" parameterType="user" resultType="user">
		SELECT USER_ID, USER_LOGIN_ID, USER_PWD, USER_NM, USER_ST_CD, USER_TY_CD, FAIL_CNT, AUTH_ST_CD
		FROM JCMS_USER
		WHERE USER_LOGIN_ID = #{userLoginId} AND USER_ST_CD = '1'
	</select>
	
	<select id="select" parameterType="userSearch" resultType="user">
		SELECT JU.USER_ID, JU.USER_LOGIN_ID, JU.USER_PWD, JU.USER_NM, JU.GENDER, JU.BIRTH, JU.PHONE, JU.EMAIL, JU.REG_DT, JU.FAIL_CNT, JU.USER_OUT_RSN, rl.ROLE_AUTH, rl.ROLE_NM
		FROM JCMS_USER JU
		LEFT JOIN JCMS_USER_ROLE ur ON JU.USER_ID = ur.USER_ID
		LEFT JOIN JCMS_ROLE rl ON rl.ROLE_AUTH = ur.ROLE_AUTH
		<where>	
			<if test="!empty(userLoginId)" > JU.USER_LOGIN_ID = #{userLoginId} </if>
			<if test="!empty(userId)" > AND JU.USER_ID = #{userId} </if>
			<if test="!empty(userNm)" > AND JU.USER_NM = #{userNm} </if>
			<if test="!empty(email)" > AND JU.EMAIL = #{email} </if>
		</where>			
	</select>
	
	<select id="count" parameterType="userSearch" resultType="java.lang.Integer">
		SELECT COUNT(USER_ID)
		FROM JCMS_USER
		WHERE 1 = 1
			<if test="empty(userTyCd)" > AND USER_TY_CD != 'S' </if>
			<if test="!empty(userTyCd)" > AND USER_TY_CD = #{userTyCd} </if>
			<if test="!empty(userLoginIdLike)" > AND USER_LOGIN_ID LIKE '%'|| #{userLoginIdLike} ||'%' </if>
			<if test="!empty(userNmLike)" > AND USER_NM LIKE '%'|| #{userNmLike} ||'%' </if>
			<if test="!empty(userStCd)" > AND USER_ST_CD = #{userStCd} </if>
			<if test="!empty(userLoginId)" > AND USER_LOGIN_ID = #{userLoginId} </if>
			<if test="!empty(email)" > AND EMAIL = #{email} </if>
			<if test="findIdByPhoneFlag == true" >
				AND PHONE = #{phone}
			</if>
			<if test="findPwByPhoneAndIdFlag == true" >
				AND USER_LOGIN_ID = #{userLoginId}
				AND PHONE = #{phone}
			</if>
	</select>
	
	<select id="userPwdCheck" parameterType="userSearch" resultType="java.lang.Integer">
		SELECT COUNT(USER_ID)
		FROM JCMS_USER
		WHERE USER_ID = #{userId} AND USER_PWD = #{userPwd}
	</select>
	
	<insert id="insert" parameterType="user">
		<selectKey keyProperty="userId" resultType="java.lang.Long" order="BEFORE" >
	    	SELECT USER_ID_SEQ.NEXTVAL FROM DUAL
	  	</selectKey>
  		INSERT INTO JCMS_USER (
  		    USER_ID, USER_LOGIN_ID, USER_PWD, USER_NM, GENDER, BIRTH, PHONE, EMAIL,
  		    FAIL_CNT, USER_ST_CD, USER_TY_CD, REG_ID, REG_DT, REC_ST,
  		    USER_DI, AUTH_KEY, AUTH_ST_CD
		) VALUES (
  			#{userId}, #{userLoginId}, #{userPwd}, #{userNm}, #{gender}, #{birth}, #{phone}, #{email},
		    0, 1, #{userTyCd}, #{userId}, SYSDATE, 'Y',
		    #{userDi}, #{authKey}, #{authStCd}
		)
	</insert>
	
	<update id="update" parameterType="user">
		UPDATE JCMS_USER
		SET
			UPD_ID = #{updId}
			, UPD_DT = SYSDATE
			<if test="!empty(userPwd)" > , USER_PWD = #{userPwd} </if>
			<if test="!empty(userNm)" > , USER_NM = #{userNm} </if>
			<if test="!empty(gender)" > , GENDER = #{gender} </if>
			<if test="!empty(birth)" > , BIRTH = #{birth} </if>
			<if test="!empty(phone)" > , PHONE = #{phone} </if>
			<if test="!empty(email)" > , EMAIL = #{email} </if>

			<if test="!empty(userStCd)" > , USER_ST_CD = #{userStCd} </if>
			<if test="!empty(authStCd)" > , AUTH_ST_CD = #{authStCd} </if>
		WHERE
			USER_ID = #{userId}
	</update>
	
	<update id="delete" parameterType="user">
		UPDATE JCMS_USER
		SET
			DEL_ID = #{delId},
			DEL_DT = SYSDATE
		WHERE
			USER_ID = #{userId} 
	</update>
	
	<update id="updateUserLoginFail" parameterType="user">
		UPDATE JCMS_USER
		SET
			FAIL_CNT = #{failCnt}
		WHERE
			USER_ID = #{userId} 
	</update>
	
	<update id="updateInitUserLoginFailCnt" parameterType="user">
		UPDATE JCMS_USER
		SET
			FAIL_CNT = 0
		WHERE
			USER_ID = #{userId} 
	</update>
	
	<update id="updatePwd" parameterType="user">
		UPDATE JCMS_USER
		SET
			USER_PWD = #{userPwd}
		WHERE
			USER_ID = #{userId}
	</update>
	
	<select id="thisWeekInUserCount" resultType="java.lang.Integer">
		SELECT COUNT(USER_ID)
		FROM JCMS_USER
		WHERE REG_DT > SYSDATE - 7
	</select>
	
	<select id="thisWeekOutUserCount" resultType="java.lang.Integer">
		SELECT COUNT(USER_ID)
		FROM JCMS_USER
		WHERE USER_ST_CD = '9' AND UPD_DT > SYSDATE - 7
	</select>
	
	<select id="thisWeekInUser" resultType="user">	
		SELECT USER_ID, USER_LOGIN_ID, USER_NM, USER_ST_CD, REG_DT
		FROM JCMS_USER
		WHERE DEL_DT IS NULL AND REG_DT > SYSDATE - 7
		ORDER BY REG_DT DESC
	</select>
	
	<select id="thisWeekOutUser" resultType="java.lang.Integer">
		SELECT COUNT(USER_ID)
		FROM JCMS_USER
		WHERE USER_ST_CD = '9' AND UPD_DT > SYSDATE - 7
	</select>
	
	<select id="userTotalOutCount" resultType="java.lang.Integer">
		SELECT COUNT(USER_ID)
		FROM JCMS_USER
		WHERE USER_ST_CD = '9'
	</select>
	
</mapper>
