<?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="cokr.xit.base.security.authentication.dao.PolicyMapper">

<resultMap id="policyRow" type="cokr.xit.base.security.authentication.AuthenticationPolicy">
	<result property="userID" column="USER_ID"/>
	<result property="ipAddress" column="IP_ADRS"/>
	<result property="duplicateYN" column="DPLCT_YN"/>
	<result property="limitYN" column="LIMIT_YN"/>
	<result property="createdBy" column="RGTR"/>
	<result property="createdAt" column="REG_DT"/>
	<result property="modifiedBy" column="MDFR"/>
	<result property="lastModified" column="MDFCN_DT"/>
</resultMap>

<select id="getPolicyList" parameterType="map" resultType="dataobject">/* 로그인 정책 목록 조회(policyMapper.getPolicyList) */
<include refid="utility.paging-prefix"/>
SELECT A.USER_ID
	 , USER_NM
	 , IP_ADRS
	 , DPLCT_YN
	 , LIMIT_YN
	 , MDFR
	 , MDFCN_DT
  FROM TB_USER A LEFT OUTER JOIN TB_LOGIN_POLICY B ON A.USER_ID = B.USER_ID
<if test="term != null">WHERE A.${by} LIKE CONCAT('%', #{term}, '%')</if>
 <include refid="utility.orderBy" />
<include refid="utility.paging-suffix"/></select>

<select id="getPolicies" parameterType="map" resultMap="policyRow">/* 로그인 정책 가져오기(policyMapper.getPolicies) */
SELECT *
  FROM TB_LOGIN_POLICY
 <if test="userIDs != null">WHERE USER_ID IN (<foreach collection="userIDs" item="userID" separator=",">#{userID}</foreach>)</if>
 ORDER BY USER_ID</select>

<insert id="insertPolicy" parameterType="map">/* 로그인 정책 등록(policyMapper.insertPolicy) */
INSERT INTO TB_LOGIN_POLICY (
       USER_ID
     , IP_ADRS
     , DPLCT_YN
     , LIMIT_YN
     , REG_DT
     , RGTR
     , MDFCN_DT
     , MDFR
) VALUES (
       #{policy.userID}
     , #{policy.ipAddress}
     , #{policy.duplicateYN}
     , #{policy.limitYN}
     ,<include refid="utility.now" />
     , #{currentUser.id}
     ,<include refid="utility.now" />
     , #{currentUser.id}
)</insert>

<update id="updatePolicy" parameterType="map">/* 로그인 정책 수정(policyMapper.updatePolicy) */
UPDATE TB_LOGIN_POLICY SET
       IP_ADRS = #{policy.ipAddress}
     , DPLCT_YN = #{policy.duplicateYN}
     , LIMIT_YN = #{policy.limitYN}
     , MDFR = #{currentUser.id}
     , MDFCN_DT =<include refid="utility.now" />
WHERE USER_ID = #{policy.userID}</update>

<delete id="removePolicy" parameterType="map">/* 로그인 정책 삭제(policyMapper.removePolicy) */
DELETE FROM TB_LOGIN_POLICY
 WHERE USER_ID IN (<foreach collection="userIDs" item="userID" separator=",">#{userID}</foreach>)</delete>

</mapper>