<?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.user.dao.UserMapper">

<resultMap id="userRow" type="cokr.xit.base.user.ManagedUser">
	<result property="id"                 column="USER_ID"/>
	<result property="account"            column="USER_ACNT"/>
	<result property="name"               column="USER_NM"/>
	<result property="password"           column="PASSWD"/>
	<result property="passwordHint"       column="PASSWD_HINT"/>
	<result property="passwordHintAnswer" column="PASSWD_NSR"/>
	<result property="empNo"              column="EMP_NO"/>
	<result property="residentRegNo"      column="RSDNT_NO"/>
	<result property="gender"             column="GENDER"/>
	<result property="birthday"           column="BRDT"/>
	<result property="areaNo"             column="AREA_NO"/>
	<result property="zipCode"            column="ZIP"/>
	<result property="address"            column="ADDR"/>
	<result property="addressDetail"      column="DADDR"/>
	<result property="phoneNo"            column="TELNO"/>
	<result property="mobilePhoneNo"      column="MBL_TELNO"/>
	<result property="faxNo"              column="FXNO"/>
	<result property="emailAddress"       column="EML_ADRS"/>
	<result property="positionName"       column="POS_NM"/>
	<result property="groupID"            column="GRP_ID"/>
	<result property="orgID"              column="ORG_ID"/>
	<result property="deptCode"           column="DEPT_CD"/>
	<result property="institute"          column="NSTT_CD"/>
	<result property="certificateDn"      column="CRTFC_DN"/>
	<result property="locked"             column="LOCK_YN"/>
	<result property="lockCount"          column="LOCK_CNT"/>
	<result property="lockedDate"         column="LOCK_DT"/>
	<result property="createdAt"          column="REG_DT"/>
	<result property="createdBy"          column="RGTR"/>
	<result property="lastModified"       column="MDFCN_DT"/>
	<result property="modifiedBy"         column="MDFR"/>
	<result property="useYN"              column="USE_YN"/>
	<result property="status"             column="STTS"/>
</resultMap>

<sql id="selectUsers"><include refid="utility.paging-prefix" />
SELECT USER_ID
     , USER_ACNT
     , USER_NM
     , PASSWD_HINT
     , PASSWD_NSR
     , EMP_NO
     , RSDNT_NO
     , GENDER
     , BRDT
     , AREA_NO
     , ZIP
     , ADDR
     , DADDR
     , TELNO
     , MBL_TELNO
     , FXNO
     , EML_ADRS
     , POS_NM
     , GRP_ID
     , ORG_ID
     , DEPT_CD
     , (CASE 
        WHEN A.DEPT_CD = 'default'
        THEN '기본 부서'
        ELSE (SELECT DEPT_NM FROM TB_DEPT_INFO WHERE DEPT_CD = A.DEPT_CD)
        END
       ) AS DEPT_NM
     , NSTT_CD
     , (CASE
        WHEN A.NSTT_CD = 'default'
        THEN '기본 기관'
        ELSE (SELECT INST_NM FROM TB_SGG_INFO WHERE INST_CD = A.NSTT_CD) 
        END
       ) AS NSTT_NM
     , CRTFC_DN
     , LOCK_YN
     , LOCK_CNT
     , LOCK_DT
     , REG_DT
     , STTS
  FROM TB_USER A
<where><if test="by != null and term != null">AND ${by} LIKE CONCAT('%', #{term}, '%')</if>
	<if test="userIDs != null">USER_ID IN (<foreach collection="userIDs" item="userID" separator=",">#{userID}</foreach>)</if>
    <if test="status == null and userIDs == null">AND STTS != 'D'</if>
	<if test="status != null">AND STTS = #{status}</if></where>
	<include refid="utility.orderBy" />
<include refid="utility.paging-suffix" /></sql>

<select id="getUserList" parameterType="map" resultType="dataobject">/* 사용자 목록 조회(userMapper.getUserList) */
<include refid="selectUsers"/></select>

<select id="getUsers" parameterType="map" resultMap="userRow">/* 사용자 가져오기(userMapper.getUsers) */
<include refid="selectUsers"/></select>

<select id="getUser" parameterType="map" resultMap="userRow">/* 사용자 계정 가져오기(userMapper.getUser) */
SELECT *
  FROM TB_USER
 WHERE USER_ACNT = #{account}
   AND NSTT_CD = #{institute}</select>

<insert id="insertUser" parameterType="cokr.xit.base.user.ManagedUser">
<selectKey resultType="string" keyProperty="id" keyColumn="NEW_ID" order="BEFORE">SELECT LPAD(IFNULL(MAX(USER_ID) + 1, 1), 10, '0') NEW_ID FROM TB_USER</selectKey>
/* 사용자 정보 등록(userMapper.insertUser) */
INSERT INTO TB_USER (
       USER_ID
     , USER_ACNT
     , USER_NM
     , PASSWD
     , PASSWD_HINT
     , PASSWD_NSR
     , EMP_NO
     , RSDNT_NO
     , GENDER
     , BRDT
     , AREA_NO
     , ZIP
     , ADDR
     , DADDR
     , TELNO
     , MBL_TELNO
     , FXNO
     , EML_ADRS
     , POS_NM
     , GRP_ID
     , ORG_ID
     , NSTT_CD
     , CRTFC_DN
     , LOCK_YN
     , LOCK_CNT
     , LOCK_DT
     , REG_DT
     , RGTR
     , MDFCN_DT
     , MDFR
     , USE_YN
     , STTS
) VALUES (
       #{id}
     , #{account}
     , #{name}
     , #{password}
     , #{passwordHint}
     , #{passwordHintAnswer}
     , #{empNo}
     , #{residentRegNo}
     , #{gender}
     , #{birthday}
     , #{areaNo}
     , #{zipCode}
     , #{address}
     , #{addressDetail}
     , #{phoneNo}
     , #{mobilePhoneNo}
     , #{faxNo}
     , #{emailAddress}
     , #{positionName}
     , #{groupID}
     , #{orgID}
     , #{institute}
     , #{certificateDn}
     , 'N'
     , 0
     , NULL
     ,<include refid="utility.now" />
     , #{createdBy}
     ,<include refid="utility.now" />
     , #{createdBy}
     , 'Y'
     , #{status}
)</insert>

<update id="updateUser" parameterType="cokr.xit.base.user.ManagedUser">/* 사용자 정보 수정(userMapper.updateUser) */
UPDATE TB_USER SET
       USER_NM = #{name}
     , PASSWD_HINT = #{passwordHint}
     , PASSWD_NSR = #{passwordHintAnswer}
     , EMP_NO = #{empNo}
     , RSDNT_NO = #{residentRegNo}
     , GENDER = #{gender}
     , BRDT = #{birthday}
     , AREA_NO = #{areaNo}
     , ZIP = #{zipCode}
     , ADDR = #{address}
     , DADDR = #{addressDetail}
     , TELNO = #{phoneNo}
     , MBL_TELNO = #{mobilePhoneNo}
     , FXNO = #{faxNo}
     , EML_ADRS = #{emailAddress}
     , POS_NM = #{positionName}
     , GRP_ID = #{groupID}
     , ORG_ID = #{orgID}
     , NSTT_CD = #{institute}
     , DEPT_CD = #{deptCode}
     , CRTFC_DN = #{certificateDn}
     , MDFCN_DT =<include refid="utility.now" />
     , MDFR = #{modifiedBy}
 WHERE USER_ID = #{id}</update>

<update id="changePassword" parameterType="map">/* 비밀번호 변경(userMapper.changePassword) */
UPDATE TB_USER SET
       PASSWD = CASE USER_ID<foreach collection="userPasswords" item="userPassword" separator=" ">
                WHEN #{userPassword.userID} THEN #{userPassword.password}</foreach>
	            ELSE PASSWD END
     , MDFCN_DT =<include refid="utility.now" />
     , MDFR = #{currentUser.id}
 WHERE USER_ID IN (<foreach collection="userIDs" item="userID" separator=",">#{userID}</foreach>)
</update>

<update id="lockUsers" parameterType="map">/* 사용자 잠김 해제(userMapper.lockUsers) */
UPDATE TB_USER SET
    <if test='lock == true'>  LOCK_YN = 'Y'
     , LOCK_CNT = LOCK_CNT + 1
     , LOCK_DT =<include refid="utility.now" /></if>
    <if test='lock == false'>  LOCK_YN = 'N'
     , LOCK_CNT = 0
     , LOCK_DT = NULL</if>
     , MDFCN_DT =<include refid="utility.now" />
     , MDFR = #{currentUser.id}
 WHERE USER_ID IN (<foreach collection="userIDs" item="userID" separator=",">#{userID}</foreach>) 
</update>

<update id="setStatus" parameterType="map">/* 사용자 상태 변경(userMapper.setStatus) */
UPDATE TB_USER SET
       STTS = #{status}
    <if test='"D" == status'>, USE_YN = 'N'</if>
     , MDFCN_DT =<include refid="utility.now" />
     , MDFR = #{currentUser.id}
 WHERE USER_ID IN (<foreach collection="userIDs" item="userID" separator=",">#{userID}</foreach>)
   AND STTS != #{status}
</update>

</mapper>