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

    <select id="selectListReservationCount" parameterType="hashmap" resultType="hashmap">
        SELECT COUNT(*)  AS "totalCount"
          FROM MU_RESERVE MR
         WHERE MU_USER_ID = #{muUserId}
           AND DATE_FORMAT(MR.RESERVE_DATE, '%Y-%m-%d') &gt;= CURRENT_DATE
    </select>

    <select id="selectListReservation" parameterType="hashmap" resultType="hashmap">
        SELECT USER_NAME                                AS 'userName'
             , MU_RESERVE_ID                            AS 'muReserveId'
             , DATE_FORMAT(MR.RESERVE_DATE, '%Y-%m-%d') AS 'reserveDate'
             , DATE_FORMAT(MR.RESERVE_TIME, '%H:%i')    AS 'reserveTime'
             , CASE
					WHEN MR.TREATMENT_NAME IN ('피부', '쁘띠', '비만')
						 THEN MR.TREATMENT_NAME
					ELSE '기타'
			   END                                      AS "treatmentName"
             , MR.TREATMENT_PROCEDURE_NAME              AS 'treatmentProcedureName'
             , MR.STATUS                                AS 'status'
             , (
                   SELECT COUNT(*)
                     FROM MU_RESERVE MR2
                    WHERE MR2.STATUS = 'R'
                      AND MR.STATUS='R'
                      AND MR2.RESERVE_MEMBER_ID=MR.RESERVE_MEMBER_ID
                      AND MR2.RESERVE_DATE = MR.RESERVE_DATE
                      AND (
                          MR2.RESERVE_TIME &lt; MR.RESERVE_TIME
                          OR (MR2.RESERVE_TIME = MR.RESERVE_TIME
                              AND MR2.RECEPTION_DATE &lt; MR.RECEPTION_DATE)
                      )
               )                                        AS 'waitingCount'
        FROM MU_RESERVE MR
        WHERE MU_USER_ID = #{muUserId}
        AND DATE_FORMAT(MR.RESERVE_DATE, '%Y-%m-%d') &gt;= CURRENT_DATE
        ORDER BY MR.RESERVE_DATE, MR.RESERVE_TIME
    </select>

    <select id="selectListReservationHistoryCount" parameterType="hashmap" resultType="hashmap">
        SELECT COUNT(*)  AS "totalCount"
          FROM MU_RESERVE MR
         WHERE MU_USER_ID =#{muUserId}
           AND DATE_FORMAT(RESERVE_DATE, '%Y-%m') = #{searchDate}
           AND CONCAT(RESERVE_DATE,' ',RESERVE_TIME) &lt; CURRENT_TIMESTAMP()
    </select>

    <select id="selectListReservationHistory" parameterType="hashmap" resultType="hashmap">
        SELECT USER_NAME                                AS 'userName'
             , DATE_FORMAT(RESERVE_DATE, '%Y-%m-%d')    AS 'reserveDate'
             , DATE_FORMAT(RESERVE_TIME, '%H:%i')       AS 'reserveTime'
             , CASE
                   WHEN MR.TREATMENT_NAME IN ('피부', '쁘띠', '비만')
                       THEN MR.TREATMENT_NAME
                   ELSE '기타'
               END                                   AS "treatmentName"
             , TREATMENT_PROCEDURE_NAME              AS 'treatmentProcedureName'
             , STATUS                                AS 'status'
          FROM MU_RESERVE MR
         WHERE MU_USER_ID =#{muUserId}
           AND DATE_FORMAT(RESERVE_DATE, '%Y-%m') = #{searchDate}
           AND CONCAT(RESERVE_DATE,' ',RESERVE_TIME) &lt; CURRENT_TIMESTAMP()
         ORDER BY MR.RESERVE_DATE, MR.RESERVE_TIME
    </select>

    <select id="selectTreatmentProcedure" parameterType="hashmap" resultType="hashmap">
        SELECT MTP.MU_TREATMENT_PROCEDURE_ID         AS "muTreatmentProcedureId"
             ,MTP.TREATMENT_PROCEDURE_NAME          AS "treatmentProcedureName"
             ,MT.MU_TREATMENT_ID                    AS "muTreatmentId"
             ,MT.TREATMENT_NAME                     AS "treatmentName"
          FROM MU_TREATMENT AS MT
          INNER JOIN MU_TREATMENT_PROCEDURE AS MTP
          ON MT.MU_TREATMENT_ID            = MTP.MU_TREATMENT_ID
              AND MT.USE_YN                     = 'Y'
              AND MTP.USE_YN                    = 'Y'
              AND MTP.MU_TREATMENT_PROCEDURE_ID = #{muTreatmentProcedureId}
         LIMIT 0, 1
    </select>

    <select id="selectHospitalHoliday" parameterType="hashmap" resultType="hashmap">
        SELECT CASE DAYOFWEEK(DATE_FORMAT(#{searchDate}, '%Y-%m-%d'))
                  WHEN '2' THEN MH.MON_OPEN_YN
                  WHEN '3' THEN MH.TUE_OPEN_YN
                  WHEN '4' THEN MH.WED_OPEN_YN
                  WHEN '5' THEN MH.THU_OPEN_YN
                  WHEN '6' THEN MH.FRI_OPEN_YN
                  WHEN '7' THEN MH.SAT_OPEN_YN
                  WHEN '1' THEN MH.SUN_OPEN_YN
              END                                                 AS "openYn"
             ,CASE
                  WHEN EXISTS (
                                  SELECT 1
                                    FROM MU_HOSPITAL_HOLIDAY MH
                                   WHERE MH.USE_YN = 'Y'
                                     AND (
                                       (MH.REPEAT_YN = 'Y' AND DATE_FORMAT(MH.LOC_DATE, '%m-%d') = DATE_FORMAT(#{searchDate}, '%m-%d'))
                                           OR
                                       (MH.REPEAT_YN = 'N' AND MH.LOC_DATE = #{searchDate})
                                       )) OR
                       EXISTS (
                                  SELECT 1
                                    FROM MU_PUBLIC_HOLIDAY MPH
                                    JOIN MU_HOSPITAL
                                    ON PUBLIC_HOLIDAY_USE_YN='Y'
                                   WHERE MPH.USE_YN = 'Y'
                                     AND MPH.LOC_DATE = #{searchDate}
                              )
                      THEN 'Y'
                  ELSE 'N'
              END 												  AS "holidayYn"
          FROM MU_HOSPITAL AS MH
         WHERE MH.USE_YN        = 'Y'
         LIMIT 0, 1
    </select>

    <select id="selectReserveCount" parameterType="hashmap" resultType="Integer">
        SELECT COUNT(1)       AS "totalCount"
          FROM MU_RESERVE MR
         WHERE MR.USE_YN     = 'Y'
           AND MR.STATUS NOT IN ('TN', 'RN')
           AND MR.RESERVE_DATE                       = #{reserveDate}
           AND DATE_FORMAT(MR.RESERVE_TIME, '%H:%i') = #{reserveTime}
    </select>

    <select id="selectListReserveYn" parameterType="hashmap" resultType="hashmap">
        SELECT MR.MU_RESERVE_ID        AS "muReserveId"
          FROM MU_RESERVE MR
         WHERE MR.USE_YN     = 'Y'
           AND MR.STATUS NOT IN ('TN', 'RN')
           AND MR.RESERVE_DATE                       = #{reserveDate}
           AND DATE_FORMAT(MR.RESERVE_TIME, '%H:%i') = #{reserveTime}
           AND MR.USER_NAME                          = #{userName}
           AND MR.PHONE_NUMBER                       = #{phoneNumber}
           AND MR.MU_TREATMENT_PROCEDURE_ID          = #{muTreatmentProcedureId}
    </select>

    <select id="selectReserve" parameterType="hashmap" resultType="hashmap">
        SELECT MR.MU_RESERVE_ID                            AS "muReserveId"
			  ,MR.USER_NAME                                AS "userName"
			  ,MR.PHONE_NUMBER                             AS "phoneNumber"
			  ,MR.MU_USER_ID                               AS "muUserId"
			  ,DATE_FORMAT(MR.RESERVE_DATE,'%Y-%m-%d')     AS "reserveDate"
			  ,DATE_FORMAT(MR.RESERVE_TIME, '%H:%i')       AS "reserveTime"
			  ,MR.MU_TREATMENT_ID                          AS "muTreatmentId"
			  ,MR.TREATMENT_NAME                           AS "treatmentName"
			  ,MR.MU_TREATMENT_PROCEDURE_ID                AS "muTreatmentProcedureId"
			  ,MR.TREATMENT_PROCEDURE_NAME                 AS "treatmentProcedureName"
          FROM MU_RESERVE AS MR
	 LEFT JOIN MU_USER AS MU
			ON MR.MU_USER_ID = MU.MU_USER_ID
		   AND MU.USE_YN   = 'Y'
		 WHERE MR.USE_YN         = 'Y'
		   AND MR.MU_RESERVE_ID  = #{muReserveId}
         LIMIT 0, 1
    </select>

    <select id="selectReservationHeader" parameterType="hashmap" resultType="hashmap">
        SELECT MR.MU_RESERVE_ID                            AS "muReserveId"
              ,MR.MU_USER_ID                               AS "muUserId"
              ,DATE_FORMAT(MR.RESERVE_DATE,'%m월 %d일')     AS "reserveDate"
              ,DATE_FORMAT(MR.RESERVE_TIME, '%H:%i')       AS "reserveTime"
          FROM MU_RESERVE AS MR
         WHERE MR.USE_YN         = 'Y'
           AND MR.MU_USER_ID     = #{muUserId}
           AND MR.USER_CHECK_YN  = 'N'
      ORDER BY MR.WRITE_DATE DESC
              ,MR.WRITE_TIME DESC
         LIMIT 0, 1
    </select>

    <insert id="insertReserve" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            SELECT CONCAT(#{muReserveId},LPAD((SELECT NEXTVAL(MU_RESERVE_SEQ)), 11, 0))
        </selectKey>
        INSERT INTO MU_RESERVE(
        MU_RESERVE_ID
        ,USER_NAME
        ,MU_USER_ID
        ,RESERVE_DATE
        ,RESERVE_TIME
        ,RESERVE_CATEGORY_ITEM_ID
        ,RESERVE_CATEGORY_ITEM_NAME
        ,RESERVE_APP_YN
        ,MU_TREATMENT_ID
        ,TREATMENT_NAME
        ,MU_TREATMENT_PROCEDURE_ID
        ,TREATMENT_PROCEDURE_NAME
        ,ETC
        ,STATUS
        ,STATUS2
        ,WRITE_DATE
        ,WRITE_TIME
        ,CUD_FLAG
        ,USE_YN
        ,REG_ID
        ,REG_DATE
        ,MOD_ID
        ,MOD_DATE
        ,T_ID
        ,T_DATE
        ,PHONE_NUMBER
        )VALUES(
        #{id}
        ,#{userName}
        ,#{muUserId,jdbcType=VARCHAR}
        ,#{reserveDate}
        ,#{reserveTime}
        ,(SELECT MCI.MU_CATEGORY_ITEM_ID
            FROM MU_CATEGORY_ITEM AS MCI
           WHERE MCI.USE_YN = 'Y'
             AND MCI.CATEGORY_ITEM_NAME LIKE '%앱%'
        LIMIT 0, 1)
        ,'앱 예약'
        ,'Y'
        ,#{muTreatmentId}
        ,#{treatmentName}
        ,#{muTreatmentProcedureId}
        ,#{treatmentProcedureName}
        ,#{etc,jdbcType=VARCHAR}
        ,#{status,jdbcType=VARCHAR}
        ,#{status2}
        ,CURDATE()
        ,CURTIME()
        ,'C'
        ,'Y'
        ,#{regId}
        ,NOW()
        ,#{modId}
        ,NOW()
        ,#{tId}
        ,#{tDate}
        ,(
            SELECT PHONE_NUMBER
              FROM MU_USER
             WHERE MU_USER_ID = #{muUserId}
        )
        )
    </insert>

    <update id="updateReserveStatus" parameterType="hashmap">
        UPDATE MU_RESERVE
           SET MOD_ID              = #{modId}
              ,MOD_DATE            = NOW()
              ,CUD_FLAG            = 'U'
              ,STATUS              = #{status}
         WHERE USE_YN         = 'Y'
           AND MU_RESERVE_ID  = #{muReserveId}
    </update>

    <update id="updateReserveUserCheckYn" parameterType="hashmap">
        UPDATE MU_RESERVE
           SET MOD_ID         = #{modId}
              ,MOD_DATE       = NOW()
              ,CUD_FLAG       = 'U'
              ,USER_CHECK_YN  = #{userCheckYn}
         WHERE USE_YN         = 'Y'
           AND USER_CHECK_YN  = 'N'
           AND MU_USER_ID     = #{muUserId}
    </update>
</mapper>