<?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">
<!-- UIWS system 이식: 업무 사용자(tb_uiws_sys_user). password 는 BCrypt, 응답 DTO 에는 미포함. -->
<mapper namespace="com.zioinfo.cms.uiws.system.mapper.SysUserMapper">

    <sql id="searchWhere">
        WHERE 1=1
        <if test="keyword != null and keyword != ''">
            AND (user_id ILIKE '%' || #{keyword} || '%'
                 OR user_nm ILIKE '%' || #{keyword} || '%'
                 OR email   ILIKE '%' || #{keyword} || '%')
        </if>
        <if test="deptId != null and deptId != ''">AND dept_id = #{deptId}</if>
    </sql>

    <select id="search" resultType="com.zioinfo.cms.uiws.system.model.SysUser">
        SELECT * FROM tb_uiws_sys_user <include refid="searchWhere"/>
        ORDER BY user_nm ASC, user_id ASC
        LIMIT #{size} OFFSET #{offset}
    </select>

    <select id="countSearch" resultType="long">
        SELECT COUNT(*) FROM tb_uiws_sys_user <include refid="searchWhere"/>
    </select>

    <select id="searchActive" resultType="com.zioinfo.cms.uiws.system.model.SysUser">
        SELECT * FROM tb_uiws_sys_user
        WHERE use_yn = 'Y'
        <if test="keyword != null and keyword != ''">
            AND (user_id ILIKE '%' || #{keyword} || '%' OR user_nm ILIKE '%' || #{keyword} || '%')
        </if>
        <if test="deptId != null and deptId != ''">AND dept_id = #{deptId}</if>
        ORDER BY user_nm ASC
    </select>

    <select id="findByDeptId" resultType="com.zioinfo.cms.uiws.system.model.SysUser">
        SELECT * FROM tb_uiws_sys_user WHERE dept_id = #{deptId} ORDER BY user_nm ASC
    </select>

    <select id="findById" resultType="com.zioinfo.cms.uiws.system.model.SysUser">
        SELECT * FROM tb_uiws_sys_user WHERE user_id = #{userId}
    </select>

    <select id="existsById" resultType="boolean">
        SELECT EXISTS(SELECT 1 FROM tb_uiws_sys_user WHERE user_id = #{userId})
    </select>

    <insert id="insert" parameterType="com.zioinfo.cms.uiws.system.model.SysUser">
        INSERT INTO tb_uiws_sys_user
            (user_id, user_nm, password, email, grade_cd, dept_id, company_id, role_cd,
             naverworks_id, login_fail_cnt, lock_yn, use_yn, approval_yn, verify_method,
             pw_change_yn, created_by, created_at)
        VALUES
            (#{userId}, #{userNm}, #{password}, #{email}, #{gradeCd}, #{deptId}, #{companyId}, #{roleCd},
             #{naverworksId}, #{loginFailCnt}, #{lockYn}, #{useYn}, #{approvalYn}, #{verifyMethod},
             #{pwChangeYn}, #{createdBy}, now())
    </insert>

    <update id="update" parameterType="com.zioinfo.cms.uiws.system.model.SysUser">
        UPDATE tb_uiws_sys_user SET
            user_nm = #{userNm}, email = #{email}, grade_cd = #{gradeCd},
            dept_id = #{deptId}, company_id = #{companyId}, role_cd = #{roleCd},
            naverworks_id = #{naverworksId}, lock_yn = #{lockYn}, use_yn = #{useYn},
            approval_yn = #{approvalYn},
            <if test="password != null">password = #{password},</if>
            updated_by = #{updatedBy}, updated_at = now()
        WHERE user_id = #{userId}
    </update>
</mapper>
