<?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="com.lemon.lifecenter.mapper.StaffMapper">
    
    <select id="selectMemberCount" parameterType="StaffDTO" resultType="int">
        <![CDATA[
            SELECT COUNT(*) AS cnt
              FROM MEMBER
             WHERE 1 = 1
               AND id = #{id}
               AND password = #{password}
        ]]>
    </select>
    
    <select id="selectMemberListCount" parameterType="StaffDTO" resultType="int">
        <![CDATA[
            SELECT COUNT(*) AS total
              FROM MEMBER M
              LEFT OUTER JOIN CENTER_INFO CI
                ON M.CENTER_CODE = CI.CENTER_CODE
             WHERE 1 = 1
        ]]>
        <if test='useYn != null and useYn != ""'>
            <![CDATA[
                AND M.USE_YN = #{useYn}
            ]]>
        </if>
        <if test='selectState == "sId"'>
            <if test="id != null and id != ''">
                <![CDATA[
                    AND M.ID LIKE CONCAT('%', #{id}, '%')
                ]]>
            </if>
        </if>
        <if test='selectState == "sName"'>
            <if test="name != null and name != ''">
                <![CDATA[
                    AND M.NAME LIKE CONCAT('%', #{name}, '%')
                ]]>
            </if>

        </if>
        <if test='selectState == "sCenterName"'>
            <if test="centerName != null and centerName != ''">
                <![CDATA[
                    AND CI.CENTER_NAME LIKE CONCAT('%', #{centerName}, '%')
                ]]>
            </if>
        </if>
    </select>
    
    <select id="selectMemberList" parameterType="StaffDTO" resultType="StaffDTO">
        <![CDATA[
            SELECT ROWNUM                                            AS num,
                   M.ID                                              AS id,
                   M.NAME                                            AS name,
                   CI.CENTER_NAME                                    AS centerName,
                   GROUP_IDX                                         AS groupIdx,
                   DATE_FORMAT(M.LAST_LOGIN_TIME, '%Y-%m-%d %H:%i')  AS lastLoginTime,
                   DATE_FORMAT(M.CREATE_DATE, '%Y-%m-%d %H:%i')      AS createDate,
                   M.USE_YN                                          AS useYn
              FROM MEMBER M
              LEFT OUTER JOIN CENTER_INFO CI
                ON M.CENTER_CODE = CI.CENTER_CODE
             WHERE 1 = 1
        ]]>
        <if test='useYn != null and useYn != ""'>
            <![CDATA[
                AND M.USE_YN = #{useYn}
            ]]>
        </if>
        <if test='selectState == "sId"'>
            <if test="id != null and id != ''">
                <![CDATA[
                    AND M.ID LIKE CONCAT('%', #{id}, '%')
                ]]>
            </if>
        </if>
        <if test='selectState == "sName"'>
            <if test="name != null and name != ''">
                <![CDATA[
                    AND M.NAME LIKE CONCAT('%', #{name}, '%')
                ]]>
            </if>

        </if>
        <if test='selectState == "sCenterName"'>
            <if test="centerName != null and centerName != ''">
                <![CDATA[
                    AND CI.CENTER_NAME LIKE CONCAT('%', #{centerName}, '%')
                ]]>
            </if>
        </if>

        <![CDATA[
            ORDER BY num DESC
            LIMIT ${limit}, ${limitMax}
        ]]>
    </select>
    
    <insert id="insertMember" parameterType="StaffDTO" useGeneratedKeys="true">
        <![CDATA[
            INSERT INTO MEMBER
                        (ID,    PASSWORD,    CREATE_DATE, NAME,    PHONE_NUMBER,   GROUP_IDX,   CENTER_CODE)
                  VALUE (#{id}, #{password}, NOW(),       #{name}, #{phoneNumber}, #{groupIdx}, #{centerCode})
        ]]>
    </insert>
    
    <insert id="insertStaff" parameterType="StaffDTO" useGeneratedKeys="true">
        <selectKey keyProperty="id" resultType="string" order="AFTER">
        <![CDATA[
            SELECT ID AS id FROM MEMBER WHERE ID = #{id}
        ]]>
        </selectKey>
        <![CDATA[
            INSERT INTO MEMBER
                        (ID,    PASSWORD,    CREATE_DATE,   NAME,    PHONE_NUMBER,   USE_YN,   GROUP_IDX,   CENTER_CODE)
                  VALUE (#{id}, #{password}, NOW(),         #{name}, #{phoneNumber}, #{useYn}, #{groupIdx}, #{centerCode})
        ]]>
    </insert>
    
    <update id="updateMember" parameterType="StaffDTO">
        <![CDATA[
            UPDATE MEMBER
               SET NAME = #{name},
        ]]>
        <if test='password != null and password != ""'>
        <![CDATA[
                   PASSWORD = #{password},
        ]]>
        </if>
        <![CDATA[
                   PHONE_NUMBER = #{phoneNumber}
             WHERE id = #{id}
        ]]>
    </update>
    
    <update id="updateMemberPwReset" parameterType="StaffDTO">
        <![CDATA[
            UPDATE MEMBER
               SET PASSWORD = #{password}
             WHERE ID = #{id}
        ]]>
    </update>
    
    <select id="selectMemberInfo" parameterType="StaffDTO" resultType="StaffDTO">
        <![CDATA[
            SELECT M.ID                                             AS id,
                   M.NAME                                           AS name,
                   M.PHONE_NUMBER                                   AS phoneNumber,
                   (SELECT CI.CENTER_NAME
                      FROM CENTER_INFO CI
                     WHERE CI.CENTER_CODE = M.CENTER_CODE)          AS centerName,
                   M.CENTER_CODE                                    AS centerCode,
                   M.GROUP_IDX                                      AS groupIdx,
                   M.USE_YN                                         AS useYn,
                   DATE_FORMAT(M.LAST_LOGIN_TIME, '%Y-%m-%d %H:%i') AS lastLoginTime,
                   DATE_FORMAT(M.CREATE_DATE, '%Y-%m-%d %H:%i')     AS createDate,
                   DATE_FORMAT(M.UPDATE_DATE, '%Y-%m-%d %H:%i')     AS updateDate,
                   M.UPDATE_BY_ID                                   AS updateById
              FROM MEMBER M
             WHERE 1 = 1
               AND id = #{id}
        ]]>
    </select>
    
    <select id="selectCenterList" resultType="CenterInfoDTO">
        <![CDATA[
            SELECT CENTER_CODE AS centerCode,
                   CENTER_NAME AS centerName
              FROM CENTER_INFO
             WHERE 1 = 1
        ]]>
    </select>
</mapper>