<!-- AdminMapper.xml -->

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.caliverse.admin.domain.dao.admin.AdminMapper">
    <!-- resultMap 설정 -->
    <resultMap id="UserResultMap" type="com.caliverse.admin.domain.entity.Admin">
        <!-- 일반 필드들은 column과 property를 매핑하여 추가합니다. -->
        <id property="id" column="id"/>
        <result property="rowNum" column="row_num"/>
        <result property="name" column="name"/>
        <result property="password" column="password"/>
        <result property="email" column="email"/>
        <result property="status" column="status"/>
        <result property="groupId" column="group_id"/>
        <result property="groupNm" column="group_nm"/>
        <result property="pwUpdateDt" column="pw_update_dt"/>
    </resultMap>

    <select id="findByEmail" resultMap="UserResultMap" parameterType="java.lang.String">
        SELECT * FROM admin WHERE email = #{email} and deleted = 0
    </select>

    <select id="existsByEmail" resultType="boolean" parameterType="java.lang.String">
        SELECT COUNT(*) > 0 FROM admin WHERE email = #{email} and deleted = 0
    </select>

    <select id="findPwdById" resultType="String" parameterType="java.lang.Long">
        SELECT password FROM admin WHERE id = #{id}
    </select>
    <select id="findPwdHistoryById" resultType="String" parameterType="java.lang.Long">
        SELECT password FROM admin_history WHERE admin_id = #{id}
    </select>

    <insert id="save" parameterType="com.caliverse.admin.domain.entity.Admin">
        INSERT INTO admin (name, password, email, status, group_id)
        VALUES (#{name}, #{password}, #{email}, #{status}, ${groupId} )
    </insert>

    <update id="initPwd" parameterType="map">
        UPDATE admin SET password = #{password}
                            ,status = 'INIT'
                            ,update_by = #{updateBy}
                            ,pw_update_Dt = NOW()
                            ,update_dt = NOW()
        WHERE id = #{id}
    </update>

    <update id="updatePwd" parameterType="map">
        UPDATE admin SET password = #{password}
                            ,status = #{status}
                            ,update_by = #{updateBy}
                            ,pw_update_Dt = NOW()
                            ,update_dt = NOW()
        WHERE id = #{updateBy}
    </update>

    <insert id="saveHistoryPwd" parameterType="map">
        INSERT INTO admin_history (admin_id,password) VALUES (#{adminId}, #{password})
    </insert>


    <!--운영자 리스트 조회-->
    <select id="getAdminList" resultMap="UserResultMap" parameterType="map">
        SELECT * FROM
        (
            SELECT (@row_number:=@row_number + 1) AS row_num
            , a.id
            , a.name
            , a.email
            , a.status
            , a.group_id
            , a.pw_update_dt AS pw_update_dt
            FROM admin a
            , (SELECT @row_number:=0) AS t
            WHERE a.deleted = 0
            <if test="join_check == 'true'">AND a.status = 'ROLE_NOT_PERMITTED'
            </if>
            <if test="
            group_id != null and group_id != ''">AND a.group_id = #{group_id}
            </if>

                <choose>
                    <when test="search_type == 'NAME' ||  search_type == 'name'">
                        <if test="search_key != null and search_key != ''">
                            AND a.name LIKE CONCAT('%',#{search_key},'%')
                        </if>
                    </when>
                    <otherwise>
                        <if test="search_key != null and search_key != ''">
                            AND a.email LIKE CONCAT('%',#{search_key},'%')
                        </if>
                    </otherwise>
                </choose>
            ORDER BY a.create_dt
        )c
        <if test="orderby != null and orderby != ''">
                ORDER BY c.row_num ${orderby}
            </if>
        <if test="pageSize != null and pageSize != ''">
            LIMIT ${pageSize} OFFSET ${offset}
        </if>
    </select>
    <select id="getTotal" resultType="java.lang.Integer" parameterType="map">
        SELECT count(*) FROM admin  WHERE deleted = 0
    </select>
    <select id="getAllCnt" resultType="java.lang.Integer" parameterType="map">
        SELECT count(*) FROM admin a WHERE a.deleted = 0
        <if test="join_check == 'true'">
            AND a.status = 'ROLE_NOT_PERMITTED'
        </if>
        <if test="group_id != null and group_id != ''">
            AND a.group_id = #{group_id}
        </if>

        <choose>
            <when test="search_type == 'NAME' ||  search_type == 'name'">
                <if test="search_key != null and search_key != ''">
                    AND a.name LIKE CONCAT('%',#{search_key},'%')
                </if>
            </when>
            <otherwise>
                <if test="search_key != null and search_key != ''">
                    AND a.email LIKE CONCAT('%',#{search_key},'%')
                </if>
            </otherwise>
        </choose>
    </select>

    <!--로그인 승인/불가-->
    <update id="updateStatus" parameterType="map">
        UPDATE admin SET status = #{status} , update_by = #{id} , update_dt = NOW() WHERE email = #{email}
    </update>
    <!--운영자 그룹 저장-->
    <update id="updateGroup" parameterType="map">
        UPDATE admin SET group_id = #{group_id} , update_by = #{id} , update_dt = NOW() WHERE email = #{email}
    </update>
    <!--운영자 선택 삭제-->
    <update id="deleteAdmin" parameterType="map">
        UPDATE admin SET deleted = ${deleted} WHERE email = #{email}
    </update>

</mapper>
