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

	<select id="selectHospital" parameterType="hashmap" resultType="hashmap">
		SELECT MH.MU_HOSPITAL_ID                                   AS "muHospitalId"
			  ,MH.HOSPITAL_NAME                                    AS "hospitalName"
			  ,MH.MON_OPEN_YN                                      AS "monOpenYn"
			  ,DATE_FORMAT(MH.MON_OPEN_START_TIME , '%H:%i')       AS "monOpenStartTime"
			  ,DATE_FORMAT(MH.MON_OPEN_END_TIME   , '%H:%i')       AS "monOpenEndTime"
			  ,DATE_FORMAT(MH.MON_BREAK_START_TIME, '%H:%i')       AS "monBreakStartTime"
			  ,DATE_FORMAT(MH.MON_BREAK_END_TIME  , '%H:%i')       AS "monBreakEndTime"
			  ,MH.TUE_OPEN_YN                                      AS "tueOpenYn"
			  ,DATE_FORMAT(MH.TUE_OPEN_START_TIME , '%H:%i')       AS "tueOpenStartTime"
			  ,DATE_FORMAT(MH.TUE_OPEN_END_TIME   , '%H:%i')       AS "tueOpenEndTime"
			  ,DATE_FORMAT(MH.TUE_BREAK_START_TIME, '%H:%i')       AS "tueBreakStartTime"
			  ,DATE_FORMAT(MH.TUE_BREAK_END_TIME  , '%H:%i')       AS "tueBreakEndTime"
			  ,MH.WED_OPEN_YN                                      AS "wedOpenYn"
			  ,DATE_FORMAT(MH.WED_OPEN_START_TIME , '%H:%i')       AS "wedOpenStartTime"
			  ,DATE_FORMAT(MH.WED_OPEN_END_TIME   , '%H:%i')       AS "wedOpenEndTime"
			  ,DATE_FORMAT(MH.WED_BREAK_START_TIME, '%H:%i')       AS "wedBreakStartTime"
			  ,DATE_FORMAT(MH.WED_BREAK_END_TIME  , '%H:%i')       AS "wedBreakEndTime"
			  ,MH.THU_OPEN_YN                                      AS "thuOpenYn"
			  ,DATE_FORMAT(MH.THU_OPEN_START_TIME , '%H:%i')       AS "thuOpenStartTime"
			  ,DATE_FORMAT(MH.THU_OPEN_END_TIME   , '%H:%i')       AS "thuOpenEndTime"
			  ,DATE_FORMAT(MH.THU_BREAK_START_TIME, '%H:%i')       AS "thuBreakStartTime"
			  ,DATE_FORMAT(MH.THU_BREAK_END_TIME  , '%H:%i')       AS "thuBreakEndTime"
			  ,MH.FRI_OPEN_YN                                      AS "friOpenYn"
			  ,DATE_FORMAT(MH.FRI_OPEN_START_TIME , '%H:%i')       AS "friOpenStartTime"
			  ,DATE_FORMAT(MH.FRI_OPEN_END_TIME   , '%H:%i')       AS "friOpenEndTime"
			  ,DATE_FORMAT(MH.FRI_BREAK_START_TIME, '%H:%i')       AS "friBreakStartTime"
			  ,DATE_FORMAT(MH.FRI_BREAK_END_TIME  , '%H:%i')       AS "friBreakEndTime"
			  ,MH.SAT_OPEN_YN                                      AS "satOpenYn"
			  ,DATE_FORMAT(MH.SAT_OPEN_START_TIME , '%H:%i')       AS "satOpenStartTime"
			  ,DATE_FORMAT(MH.SAT_OPEN_END_TIME   , '%H:%i')       AS "satOpenEndTime"
			  ,DATE_FORMAT(MH.SAT_BREAK_START_TIME, '%H:%i')       AS "satBreakStartTime"
			  ,DATE_FORMAT(MH.SAT_BREAK_END_TIME  , '%H:%i')       AS "satBreakEndTime"
			  ,MH.SUN_OPEN_YN                                      AS "sunOpenYn"
			  ,DATE_FORMAT(MH.SUN_OPEN_START_TIME , '%H:%i')       AS "sunOpenStartTime"
			  ,DATE_FORMAT(MH.SUN_OPEN_END_TIME   , '%H:%i')       AS "sunOpenEndTime"
			  ,DATE_FORMAT(MH.SUN_BREAK_START_TIME, '%H:%i')       AS "sunBreakStartTime"
			  ,DATE_FORMAT(MH.SUN_BREAK_END_TIME  , '%H:%i')       AS "sunBreakEndTime"
			  ,MH.PUBLIC_HOLIDAY_USE_YN                            AS "publicHolidayUseYn"
			  ,IFNULL((SELECT CONCAT(MM.NAME,'(',MM.LOGIN_ID,')')  AS MEMBER_NAME
						 FROM MU_MEMBER AS MM
						WHERE MM.USE_YN = 'Y'
						  AND MM.MU_MEMBER_ID = MH.MU_MEMBER_ID
						LIMIT 0, 1
			   ),'')                                               AS "memberName"
		  FROM MU_HOSPITAL AS MH
		 WHERE MH.USE_YN        = 'Y'
		 LIMIT 0, 1
	</select>

	<select id="selectHospitalWeek" parameterType="hashmap" resultType="hashmap">
		SELECT MH.MU_HOSPITAL_ID                                   AS "muHospitalId"
			  ,MH.HOSPITAL_NAME                                    AS "hospitalName"
			  ,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"
			  ,CASE DAYOFWEEK(DATE_FORMAT(#{searchDate}, '%Y-%m-%d'))
					WHEN '2' THEN MH.MON_OPEN_START_TIME
					WHEN '3' THEN MH.TUE_OPEN_START_TIME
					WHEN '4' THEN MH.WED_OPEN_START_TIME
					WHEN '5' THEN MH.THU_OPEN_START_TIME
					WHEN '6' THEN MH.FRI_OPEN_START_TIME
					WHEN '7' THEN MH.SAT_OPEN_START_TIME
					WHEN '1' THEN MH.SUN_OPEN_START_TIME
			   END                                                 AS "openStartTime"
			  ,CASE DAYOFWEEK(DATE_FORMAT(#{searchDate}, '%Y-%m-%d'))
					WHEN '2' THEN MH.MON_OPEN_END_TIME
					WHEN '3' THEN MH.TUE_OPEN_END_TIME
					WHEN '4' THEN MH.WED_OPEN_END_TIME
					WHEN '5' THEN MH.THU_OPEN_END_TIME
					WHEN '6' THEN MH.FRI_OPEN_END_TIME
					WHEN '7' THEN MH.SAT_OPEN_END_TIME
					WHEN '1' THEN MH.SUN_OPEN_END_TIME
			   END                                                 AS "openEndTime"
			  ,CASE DAYOFWEEK(DATE_FORMAT(#{searchDate}, '%Y-%m-%d'))
					WHEN '2' THEN MH.MON_BREAK_START_TIME
					WHEN '3' THEN MH.TUE_BREAK_START_TIME
					WHEN '4' THEN MH.WED_BREAK_START_TIME
					WHEN '5' THEN MH.THU_BREAK_START_TIME
					WHEN '6' THEN MH.FRI_BREAK_START_TIME
					WHEN '7' THEN MH.SAT_BREAK_START_TIME
					WHEN '1' THEN MH.SUN_BREAK_START_TIME
			   END                                                 AS "breakStartTime"
			  ,CASE DAYOFWEEK(DATE_FORMAT(#{searchDate}, '%Y-%m-%d'))
					WHEN '2' THEN MH.MON_BREAK_END_TIME
					WHEN '3' THEN MH.TUE_BREAK_END_TIME
					WHEN '4' THEN MH.WED_BREAK_END_TIME
					WHEN '5' THEN MH.THU_BREAK_END_TIME
					WHEN '6' THEN MH.FRI_BREAK_END_TIME
					WHEN '7' THEN MH.SAT_BREAK_END_TIME
					WHEN '1' THEN MH.SUN_BREAK_END_TIME
			   END                                                 AS "breakEndTime"
		  FROM MU_HOSPITAL AS MH
		 WHERE MH.USE_YN        = 'Y'
		 LIMIT 0, 1
	</select>

	<select id="selectHospitalWeek2" parameterType="hashmap" resultType="hashmap">
        WITH RECURSIVE T_TEMP_DATES AS (
            SELECT #{startDate} AS RESULT_DATE

            UNION ALL

            SELECT DATE_ADD(RESULT_DATE, INTERVAL 1 DAY)
              FROM T_TEMP_DATES
             WHERE RESULT_DATE &lt; #{endDate}
        )
        SELECT TTD.OPEN_YN      AS "openYn"
              ,TTD.HOLIDAY_YN   AS "holidayYn"
          FROM (
                SELECT TTD.RESULT_DATE                    AS RESULT_DATE
                      ,TTD.DAY_OF_WEEK                    AS DAY_OF_WEEK
                      ,CASE
                       WHEN TTD.DAY_OF_WEEK = '1'
                       THEN MH.SUN_OPEN_YN
                       WHEN TTD.DAY_OF_WEEK = '2'
                       THEN MH.MON_OPEN_YN
                       WHEN TTD.DAY_OF_WEEK = '3'
                       THEN MH.TUE_OPEN_YN
                       WHEN TTD.DAY_OF_WEEK = '4'
                       THEN MH.WED_OPEN_YN
                       WHEN TTD.DAY_OF_WEEK = '5'
                       THEN MH.THU_OPEN_YN
                       WHEN TTD.DAY_OF_WEEK = '6'
                       THEN MH.FRI_OPEN_YN
                       WHEN TTD.DAY_OF_WEEK = '7'
                       THEN MH.SAT_OPEN_YN
                       ELSE 'N'
                       END                                AS OPEN_YN
                      ,CASE
                       WHEN EXISTS(
                                   SELECT 1
                                     FROM MU_HOSPITAL_HOLIDAY AS MH
                                    WHERE MH.USE_YN = 'Y'
                                      AND ((MH.REPEAT_YN = 'Y' AND DATE_FORMAT(MH.LOC_DATE, '%m-%d') = DATE_FORMAT(TTD.RESULT_DATE, '%m-%d'))
                                            OR
                                           (MH.REPEAT_YN = 'N' AND MH.LOC_DATE = TTD.RESULT_DATE))
                            )
                         OR EXISTS(
                                   SELECT 1
                                     FROM MU_PUBLIC_HOLIDAY AS MPH
                                     JOIN MU_HOSPITAL
                                       ON PUBLIC_HOLIDAY_USE_YN='Y'
                                    WHERE MPH.USE_YN = 'Y'
                                      AND MPH.LOC_DATE = TTD.RESULT_DATE
                            )
                       THEN 'Y'
                       ELSE 'N'
                       END                                AS HOLIDAY_YN
                  FROM (
                        SELECT TTD.RESULT_DATE            AS RESULT_DATE
                              ,DAYOFWEEK(TTD.RESULT_DATE) AS DAY_OF_WEEK
                          FROM T_TEMP_DATES AS TTD
                      ORDER BY TTD.RESULT_DATE ASC
                  ) AS TTD
             LEFT JOIN MU_HOSPITAL AS MH
                    ON 1 = 1
          ) AS TTD
	</select>

	<select id="selectHospitalHoliday" parameterType="hashmap" resultType="hashmap">
		SELECT 'hospitalHoliday'                 AS dateType
			 , DATE_FORMAT(LOC_DATE, '%Y.%m.%d') AS locDate
		  FROM MU_HOSPITAL_HOLIDAY MH
		 WHERE MH.USE_YN = 'Y'
		   AND (
			 (MH.REPEAT_YN = 'Y')
				 OR
			 (MH.REPEAT_YN = 'N' AND DATE_FORMAT( MH.LOC_DATE, '%Y' ) >= DATE_FORMAT( CURDATE( ), '%Y' ))
			 )
		 UNION
		(
			SELECT 'publicHoliday'                   AS dateType
				 , DATE_FORMAT(LOC_DATE, '%Y.%m.%d') AS locDate
			  FROM MU_PUBLIC_HOLIDAY MPH
			  JOIN MU_HOSPITAL
                ON PUBLIC_HOLIDAY_USE_YN='Y'
			 WHERE MPH.USE_YN = 'Y'
			   AND DATE_FORMAT( MPH.LOC_DATE, '%Y' ) >= DATE_FORMAT( CURDATE( ), '%Y' )
		)
	</select>
	
	<update id="updateHospital" parameterType="hashmap">
		UPDATE MU_HOSPITAL
		   SET MOD_ID              = #{modId}
			  ,MOD_DATE            = NOW()
			  ,CUD_FLAG            = 'U'
			  ,MON_OPEN_YN             = #{monOpenYn}
			  ,MON_OPEN_START_TIME     = #{monOpenStartTime}
			  ,MON_OPEN_END_TIME       = #{monOpenEndTime}
			  ,MON_BREAK_START_TIME    = #{monBreakStartTime}
			  ,MON_BREAK_END_TIME      = #{monBreakEndTime}
			  ,TUE_OPEN_YN             = #{tueOpenYn}
			  ,TUE_OPEN_START_TIME     = #{tueOpenStartTime}
			  ,TUE_OPEN_END_TIME       = #{tueOpenEndTime}
			  ,TUE_BREAK_START_TIME    = #{tueBreakStartTime}
			  ,TUE_BREAK_END_TIME      = #{tueBreakEndTime}
			  ,WED_OPEN_YN             = #{wedOpenYn}
			  ,WED_OPEN_START_TIME     = #{wedOpenStartTime}
			  ,WED_OPEN_END_TIME       = #{wedOpenEndTime}
			  ,WED_BREAK_START_TIME    = #{wedBreakStartTime}
			  ,WED_BREAK_END_TIME      = #{wedBreakEndTime}
			  ,THU_OPEN_YN             = #{thuOpenYn}
			  ,THU_OPEN_START_TIME     = #{thuOpenStartTime}
			  ,THU_OPEN_END_TIME       = #{thuOpenEndTime}
			  ,THU_BREAK_START_TIME    = #{thuBreakStartTime}
			  ,THU_BREAK_END_TIME      = #{thuBreakEndTime}
			  ,FRI_OPEN_YN             = #{friOpenYn}
			  ,FRI_OPEN_START_TIME     = #{friOpenStartTime}
			  ,FRI_OPEN_END_TIME       = #{friOpenEndTime}
			  ,FRI_BREAK_START_TIME    = #{friBreakStartTime}
			  ,FRI_BREAK_END_TIME      = #{friBreakEndTime}
			  ,SAT_OPEN_YN             = #{satOpenYn}
			  ,SAT_OPEN_START_TIME     = #{satOpenStartTime}
			  ,SAT_OPEN_END_TIME       = #{satOpenEndTime}
			  ,SAT_BREAK_START_TIME    = #{satBreakStartTime}
			  ,SAT_BREAK_END_TIME      = #{satBreakEndTime}
			  ,SUN_OPEN_YN             = #{sunOpenYn}
			  ,SUN_OPEN_START_TIME     = #{sunOpenStartTime}
			  ,SUN_OPEN_END_TIME       = #{sunOpenEndTime}
			  ,SUN_BREAK_START_TIME    = #{sunBreakStartTime}
			  ,SUN_BREAK_END_TIME      = #{sunBreakEndTime}
			  ,PUBLIC_HOLIDAY_USE_YN   = #{publicHolidayUseYn}
		 WHERE USE_YN        = 'Y'
		   AND MU_HOSPITAL_ID  = #{muHospitalId}
	</update>
</mapper>