<?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="WebAppUser">

    <select id="selectTotalAppUserCount" parameterType="hashmap" resultType="Integer">
        SELECT COUNT(*)                         AS "totalCount"
        FROM MU_USER MU
       WHERE MU.USE_YN='Y'
         AND MU.APP_USE_YN ='Y'
         <if test="appIntroType != null and appIntroType != ''">
         AND MU.APP_INTRO_TYPE = #{appIntroType}
         </if>
         <if test="appUserSearchKeywordParam != null and appUserSearchKeywordParam != ''">
         AND (MU.USER_NAME LIKE CONCAT('%',TRIM(#{appUserSearchKeywordParam}),'%')
          OR MU.PHONE_NUMBER LIKE CONCAT('%',TRIM(#{appUserSearchKeywordParam}),'%')
          OR MU.MEMBERSHIP_ACCOUNT LIKE CONCAT('%',TRIM(#{appUserSearchKeywordParam}),'%'))
         </if>
    </select>

    <select id="selectListAppUser" parameterType="hashmap" resultType="hashmap">
        SELECT MU.MU_USER_ID                                        AS muUserId
              ,MU.USER_NAME                                         AS userName
              ,MU.MEMBERSHIP_ACCOUNT                                AS membershipAccount
              ,MU.PHONE_NUMBER                                      AS phoneNumber
              ,IF(APP_INTRO_TYPE='C','신규 가입회원','기존 내원회원')    AS appIntroType
              ,(SELECT CASE
	                   WHEN MD.PHONE_TYPE='A' THEN 'Android'
		               WHEN MD.PHONE_TYPE='I' THEN 'iOS'
		               ELSE '기타'
		                END
                  FROM MU_DEVICE AS MD
                 WHERE MD.USE_YN='Y'
                   AND MD.MU_USER_ID=MU.MU_USER_ID
              ORDER BY MD.REG_DATE DESC
                 LIMIT 0,1)					                        AS phoneType
              ,DATE_FORMAT(APP_INTRO_DATE, '%Y-%m-%d')              AS appIntroDate
              ,IFNULL((SELECT RESERVE_DATE
                         FROM MU_RESERVE MR
                        WHERE MU.MU_USER_ID = MR.MU_USER_ID
                          AND RESERVE_APP_YN='Y'
                     ORDER BY RESERVE_DATE DESC
                        LIMIT 0,1),'')                              AS reserveDate
          FROM MU_USER MU
         WHERE MU.USE_YN='Y'
           AND MU.APP_USE_YN ='Y'
           <if test="appIntroType != null and appIntroType != ''">
           AND MU.APP_INTRO_TYPE = #{appIntroType}
           </if>
           <if test="appUserSearchKeywordParam != null and appUserSearchKeywordParam != ''">
           AND (MU.USER_NAME LIKE CONCAT('%',TRIM(#{appUserSearchKeywordParam}),'%')
            OR MU.PHONE_NUMBER LIKE CONCAT('%',TRIM(#{appUserSearchKeywordParam}),'%')
            OR MU.MEMBERSHIP_ACCOUNT LIKE CONCAT('%',TRIM(#{appUserSearchKeywordParam}),'%'))
           </if>
        <choose>
            <when test="appUserSort != null and appUserSort != ''">
                ORDER BY ${appUserSort}
            </when>
            <otherwise>
                ORDER BY MU.APP_INTRO_DATE DESC
            </otherwise>
        </choose>
        LIMIT ${appUserStart}, ${appUserLimit}
    </select>

    <select id="selectListAppUserExcel" parameterType="hashmap" resultType="hashmap">
        SELECT MU.MU_USER_ID                                        AS muUserId
             , MU.USER_NAME                                         AS userName
             , MEMBERSHIP_ACCOUNT                                   AS membershipAccount
             , MU.PHONE_NUMBER                                      AS phoneNumber
             , IF(APP_INTRO_TYPE='C','신규 가입회원','기존 내원회원')   AS appIntroType
             , CASE
                 WHEN PHONE_TYPE='A' THEN 'Android'
                 WHEN PHONE_TYPE='I' THEN 'iOS'
                 ELSE '기타'
                END                                                 AS phoneType
             , DATE_FORMAT(APP_INTRO_DATE, '%Y-%m-%d')              AS appIntroDate
             , IFNULL((SELECT RESERVE_DATE
                      FROM MU_RESERVE MR
                     WHERE MU.MU_USER_ID = MR.MU_USER_ID
                       AND RESERVE_APP_YN='Y'
                     ORDER BY RESERVE_DATE LIMIT 1),'')           AS reserveDate
          FROM MU_USER MU
          JOIN MU_DEVICE MD
            ON MU.MU_USER_ID = MD.MU_USER_ID
         WHERE MU.USE_YN='Y'
           AND MU.APP_USE_YN ='Y'
           AND MD.USE_YN='Y'
           <if test="appIntroType != null and appIntroType != ''">
           AND MU.APP_INTRO_TYPE = #{appIntroType}
           </if>
           <if test="appUserSearchKeywordParam != null and appUserSearchKeywordParam != ''">
           AND (MU.USER_NAME LIKE CONCAT('%',TRIM(#{appUserSearchKeywordParam}),'%')
            OR MU.PHONE_NUMBER LIKE CONCAT('%',TRIM(#{appUserSearchKeywordParam}),'%')
            OR MU.MEMBERSHIP_ACCOUNT LIKE CONCAT('%',TRIM(#{appUserSearchKeywordParam}),'%'))
           </if>
    </select>

</mapper>