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

	<select id="selectTotalVacationManagementCount" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*)  AS "totalCount"
		  FROM (
					SELECT MVM.MU_VACATION_MANAGEMENT_ID
						  ,MVM.MU_MEMBER_ID
						  ,IFNULL((SELECT MM.NAME
									 FROM MU_MEMBER AS MM
									WHERE MM.USE_YN       = 'Y'
									  AND MM.MU_MEMBER_ID = MVM.MU_MEMBER_ID
									LIMIT 0, 1
						   ),'')                                  AS MEMBER_NAME
						  ,IFNULL((SELECT MM.LOGIN_ID
									 FROM MU_MEMBER AS MM
									WHERE MM.USE_YN       = 'Y'
									  AND MM.MU_MEMBER_ID = MVM.MU_MEMBER_ID
									LIMIT 0, 1
						   ),'')                                  AS LOGIN_ID
						  ,MVM.MU_GROUP_ID
						  ,MVM.MU_DUTY_ID
						  ,MVM.YEAR
						  ,MVM.DAYS
						  ,MVM.START_DATE
						  ,MVM.END_DATE
						  ,MVM.ETC
					  FROM MU_VACATION_MANAGEMENT AS MVM
					 WHERE MVM.USE_YN    = 'Y'
					   AND MVM.YEAR      = #{vacationManagementSearchYear}
			   ) AS MVM
		 WHERE 1=1
		   <if test="vacationManagementSearchGroupId != null and vacationManagementSearchGroupId != ''">
		   AND MU_GROUP_ID  = #{vacationManagementSearchGroupId}
		   </if>
		   <if test="vacationManagementSearchKeywordParam != null and vacationManagementSearchKeywordParam != ''">
		   AND ((MEMBER_NAME LIKE CONCAT('%',TRIM(#{vacationManagementSearchKeywordParam}),'%'))
			OR (ETC LIKE CONCAT('%',TRIM(#{vacationManagementSearchKeywordParam}),'%')))
		   </if>
	</select>
	
	<select id="selectListVacationManagement" parameterType="hashmap" resultType="hashmap">
		SELECT MVM.*
		  FROM (
					SELECT MVM.*
						  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
					  FROM (
								SELECT MU_VACATION_MANAGEMENT_ID                                  AS "muVacationManagementId"
									  ,MU_MEMBER_ID                                               AS "muMemberId"
									  ,MEMBER_NAME                                                AS "memberName"
									  ,LOGIN_ID                                                   AS "loginId"
									  ,CONCAT(MEMBER_NAME, '(', LOGIN_ID, ')')                    AS "memberNameId"
									  ,MU_GROUP_ID                                                AS "muGroupId"
									  ,IFNULL((SELECT MG.GROUP_NAME
												 FROM MU_GROUP AS MG
												WHERE MG.USE_YN      = 'Y'
									 			  AND MG.MU_GROUP_ID = MVM.MU_GROUP_ID
									 			LIMIT 0, 1
									   ),'')                                                      AS "groupName"
									  ,MU_DUTY_ID                                                 AS "muDutyId"
									  ,IFNULL((SELECT MD.DUTY_NAME
												 FROM MU_DUTY AS MD
												WHERE MD.USE_YN     = 'Y'
									 			  AND MD.MU_DUTY_ID = MVM.MU_DUTY_ID
									 			LIMIT 0, 1
									   ),'')                                                      AS "dutyName"
									  ,YEAR                                                       AS "year"
									  ,DAYS                                                       AS "days"
									  ,DATE_FORMAT(START_DATE, '%Y-%m-%d')                        AS "startDate"
									  ,DATE_FORMAT(END_DATE, '%Y-%m-%d')                          AS "endDate"
									  ,CONCAT(DATE_FORMAT(START_DATE, '%Y-%m-%d')
									   ,'~'
									   ,DATE_FORMAT(END_DATE, '%Y-%m-%d'))                        AS "startEndDate"
									  ,IFNULL(ETC, '')                                            AS "etc"
									  ,DATE_FORMAT(WRITE_DATE, '%Y-%m-%d')                        AS "writeDate"
									  ,CASE
										  WHEN LAPSE>0 AND LEFT_DAYS>0 THEN DAYS - LEFT_DAYS
										  ELSE 0
										END 	 		                                          AS "useAttendanceCount"
									  ,LAPSE                                                      AS "unuseAttendanceCount"
								  FROM (
											SELECT MVM.MU_VACATION_MANAGEMENT_ID
												  ,MVM.MU_MEMBER_ID
												  ,IFNULL((SELECT MM.NAME
															 FROM MU_MEMBER AS MM
															WHERE MM.USE_YN       = 'Y'
															  AND MM.MU_MEMBER_ID = MVM.MU_MEMBER_ID
															LIMIT 0, 1
												   ),'')                                  AS MEMBER_NAME
												  ,IFNULL((SELECT MM.LOGIN_ID
															 FROM MU_MEMBER AS MM
															WHERE MM.USE_YN       = 'Y'
															  AND MM.MU_MEMBER_ID = MVM.MU_MEMBER_ID
															LIMIT 0, 1
												   ),'')                                  AS LOGIN_ID
												  ,MM.MU_GROUP_ID
												  ,MM.MU_DUTY_ID
												  ,MVM.YEAR
												  ,MVM.DAYS
								    			  ,LEFT_DAYS
												  ,CASE
													WHEN END_DATE>=CURDATE() THEN MVM.LEFT_DAYS
													ELSE 0
												   END AS LAPSE
												  ,MVM.START_DATE
												  ,MVM.END_DATE
												  ,MVM.WRITE_DATE
												  ,MVM.ETC
											  FROM MU_VACATION_MANAGEMENT AS MVM
								              JOIN MU_MEMBER MM
								  			  ON MVM.MU_MEMBER_ID = MM.MU_MEMBER_ID
								              AND MM.USE_YN ='Y'
											 WHERE MVM.USE_YN    = 'Y'
											   AND MVM.YEAR      = #{vacationManagementSearchYear}
									   ) AS MVM
								 WHERE 1=1
								   <if test="vacationManagementSearchGroupId != null and vacationManagementSearchGroupId != ''">
								   AND MU_GROUP_ID  = #{vacationManagementSearchGroupId}
								   </if>
								   <if test="vacationManagementSearchKeywordParam != null and vacationManagementSearchKeywordParam != ''">
								   AND ((MEMBER_NAME LIKE CONCAT('%',TRIM(#{vacationManagementSearchKeywordParam}),'%'))
									OR (ETC LIKE CONCAT('%',TRIM(#{vacationManagementSearchKeywordParam}),'%')))
								   </if>
								 <choose>
								 <when test="vacationManagementSort != null and vacationManagementSort != ''">
							  ORDER BY ${vacationManagementSort}
								 </when>
								 <otherwise>
							  ORDER BY MVM.WRITE_DATE DESC
								 </otherwise>
								 </choose>
								 LIMIT 18446744073709551615
						   ) MVM, (SELECT @RNUM:=0) R
					 WHERE 1 = 1
			   ) MVM
		 WHERE 1 = 1
		 LIMIT ${vacationManagementStart}, ${vacationManagementLimit}
	</select>
	
	<select id="selectListVacationManagementExcel" parameterType="hashmap" resultType="hashmap">
		SELECT MVM.*
		  FROM (
					SELECT MVM.*
						  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
					  FROM (
								SELECT MU_VACATION_MANAGEMENT_ID                                  AS "muVacationManagementId"
									  ,MU_MEMBER_ID                                               AS "muMemberId"
									  ,MEMBER_NAME                                                AS "memberName"
									  ,LOGIN_ID                                                   AS "loginId"
									  ,CONCAT(MEMBER_NAME, '(', LOGIN_ID, ')')                    AS "memberNameId"
									  ,MU_GROUP_ID                                                AS "muGroupId"
									  ,IFNULL((SELECT MG.GROUP_NAME
												 FROM MU_GROUP AS MG
												WHERE MG.USE_YN      = 'Y'
									 			  AND MG.MU_GROUP_ID = MVM.MU_GROUP_ID
									 			LIMIT 0, 1
									   ),'')                                                      AS "groupName"
									  ,MU_DUTY_ID                                                 AS "muDutyId"
									  ,IFNULL((SELECT MD.DUTY_NAME
												 FROM MU_DUTY AS MD
												WHERE MD.USE_YN     = 'Y'
									 			  AND MD.MU_DUTY_ID = MVM.MU_DUTY_ID
									 			LIMIT 0, 1
									   ),'')                                                      AS "dutyName"
									  ,YEAR                                                       AS "year"
									  ,DAYS                                                       AS "days"
									  ,START_DATE                                                 AS "startDate"
									  ,END_DATE                                                   AS "endDate"
									  ,CONCAT(DATE_FORMAT(START_DATE, '%Y-%m-%d')
									   ,'~'
									   ,DATE_FORMAT(END_DATE, '%Y-%m-%d'))                        AS "startEndDate"
									  ,IFNULL(ETC, '')                                            AS "etc"
									  ,DATE_FORMAT(WRITE_DATE, '%Y-%m-%d')                        AS "writeDate"
									  ,(DAYS - LEFT_DAYS)                                         AS "useAttendanceCount"
									  ,LEFT_DAYS                                                  AS "unuseAttendanceCount"
								  FROM (
											SELECT MVM.MU_VACATION_MANAGEMENT_ID
												  ,MVM.MU_MEMBER_ID
												  ,IFNULL((SELECT MM.NAME
															 FROM MU_MEMBER AS MM
															WHERE MM.USE_YN       = 'Y'
															  AND MM.MU_MEMBER_ID = MVM.MU_MEMBER_ID
															LIMIT 0, 1
												   ),'')                                                      AS MEMBER_NAME
												  ,IFNULL((SELECT MM.LOGIN_ID
															 FROM MU_MEMBER AS MM
															WHERE MM.USE_YN       = 'Y'
															  AND MM.MU_MEMBER_ID = MVM.MU_MEMBER_ID
															LIMIT 0, 1
												   ),'')                                                      AS LOGIN_ID
												  ,MVM.MU_GROUP_ID
												  ,MVM.MU_DUTY_ID
												  ,MVM.YEAR
												  ,MVM.DAYS
												  ,MVM.LEFT_DAYS
												  ,DATE_FORMAT(MVM.START_DATE, '%Y-%m-%d')                    AS START_DATE
												  ,DATE_FORMAT(MVM.END_DATE, '%Y-%m-%d')                      AS END_DATE
												  ,MVM.ETC
												  ,MVM.WRITE_DATE
											  FROM MU_VACATION_MANAGEMENT AS MVM
											 WHERE MVM.USE_YN    = 'Y'
											   AND MVM.YEAR      = #{vacationManagementSearchYear}
									   ) AS MVM
								 WHERE 1=1
								   <if test="vacationManagementSearchGroupId != null and vacationManagementSearchGroupId != ''">
								   AND MU_GROUP_ID  = #{vacationManagementSearchGroupId}
								   </if>
								   <if test="vacationManagementSearchKeywordParam != null and vacationManagementSearchKeywordParam != ''">
								   AND ((MEMBER_NAME LIKE CONCAT('%',TRIM(#{vacationManagementSearchKeywordParam}),'%'))
									OR (ETC LIKE CONCAT('%',TRIM(#{vacationManagementSearchKeywordParam}),'%')))
								   </if>
								 <choose>
								 <when test="vacationManagementSort != null and vacationManagementSort != ''">
							  ORDER BY ${vacationManagementSort}
								 </when>
								 <otherwise>
							  ORDER BY MVM.WRITE_DATE DESC
								 </otherwise>
								 </choose>
								 LIMIT 18446744073709551615
						   ) MVM, (SELECT @RNUM:=0) R
					 WHERE 1 = 1
			   ) MVM
		 WHERE 1 = 1
	</select>

    <select id="selectVacationManagement" parameterType="hashmap" resultType="hashmap">
		SELECT MU_VACATION_MANAGEMENT_ID                                    AS "muVacationManagementId"
			  ,MU_ATTENDANCE_ID                                             AS "muAttendanceId"
			  ,ATTENDANCE_TYPE                                              AS "attendanceType"
			  ,CASE ATTENDANCE_TYPE
			   WHEN 'D' THEN '연차(전일)'
			   WHEN 'E' THEN '반차(오전)'
			   WHEN 'F' THEN '반차(오후)'
			    END                                                         AS "attendanceTypeKorName"
			  ,DATE_FORMAT(START_DATE, '%Y-%m-%d')                          AS "startDate"
			  ,DATE_FORMAT(END_DATE, '%Y-%m-%d')                            AS "endDate"
			  ,CASE
			   WHEN START_DATE = END_DATE THEN DATE_FORMAT(START_DATE, '%Y-%m-%d')
			   ELSE CONCAT(DATE_FORMAT(START_DATE, '%Y-%m-%d'), '~', DATE_FORMAT(END_DATE, '%Y-%m-%d'))
			    END                                                         AS "startEndDate"
			  ,USE_ATTENDANCE_COUNT                                         AS "useDays"
			  ,SUM(USE_ATTENDANCE_COUNT) OVER (ORDER BY RN)                 AS "sumUseDays"
			  ,(DAYS + SUM(USE_ATTENDANCE_COUNT) OVER (ORDER BY RN))        AS "days"
			  ,ETC                                                          AS "etc"
			  ,CANCEL_ETC                                                   AS "cancelEtc"
			  ,STATUS                                                       AS "status"
			  ,STATUS_TYPE                                                  AS "statusType"
		  FROM (
				SELECT *
				      ,ROW_NUMBER() OVER (ORDER BY START_DATE) AS RN
				  FROM (
				 		SELECT MVM.MU_VACATION_MANAGEMENT_ID
							  ,MA.MU_ATTENDANCE_ID
							  ,MA.ATTENDANCE_TYPE
						      ,MA.START_DATE
					          ,MA.END_DATE
					          ,CASE
						       WHEN MA.ATTENDANCE_TYPE = 'E' THEN -0.5
						       ELSE -(ABS(DATEDIFF(MA.END_DATE, MA.START_DATE)) + 1)
					            END AS USE_ATTENDANCE_COUNT
					          ,MVM.DAYS
					          ,MA.ETC
					          ,MA.MOD_DATE
					          ,'' AS CANCEL_ETC
					          ,CASE
						       WHEN MA.STATUS = 'U' THEN '연차 수정'
						       ELSE '연차 신청'
					            END                                     AS STATUS
				              ,STATUS                                   AS STATUS_TYPE
				              ,MA.WRITE_DATE                            AS WRITE_DATE
				          FROM MU_VACATION_MANAGEMENT AS MVM
				    INNER JOIN MU_ATTENDANCE MA
					        ON MVM.MU_VACATION_MANAGEMENT_ID = MA.MU_VACATION_MANAGEMENT_ID
					       AND MA.USE_YN = 'Y'
					       AND MVM.USE_YN = 'Y'
					       AND MVM.YEAR                      = #{year}
					       AND MVM.MU_VACATION_MANAGEMENT_ID = #{muVacationManagementId}

					 UNION ALL

						SELECT MVM.MU_VACATION_MANAGEMENT_ID
					          ,MA.MU_ATTENDANCE_ID
					          ,MA.ATTENDANCE_TYPE
					          ,MA.START_DATE
					          ,MA.END_DATE
					          ,CASE
						       WHEN MA.ATTENDANCE_TYPE = 'E' THEN 0.5
						       ELSE (ABS(DATEDIFF(MA.END_DATE, MA.START_DATE)) + 1)
					            END AS USE_ATTENDANCE_COUNT
					          ,MVM.DAYS
					          ,MA.ETC
					          ,MA.MOD_DATE
					          ,MA.CANCEL_ETC
					          ,CASE
					 	       WHEN MA.STATUS = 'D' THEN '연차 취소'
						       ELSE ''
					            END                                     AS STATUS
					          ,STATUS                                   AS STATUS_TYPE
					          ,MA.WRITE_DATE                            AS WRITE_DATE
				          FROM MU_VACATION_MANAGEMENT AS MVM
				    INNER JOIN MU_ATTENDANCE MA
					        ON MVM.MU_VACATION_MANAGEMENT_ID = MA.MU_VACATION_MANAGEMENT_ID
						   AND MA.USE_YN = 'Y'
						   AND MVM.USE_YN = 'Y'
						   AND MVM.YEAR                      = #{year}
						   AND MVM.MU_VACATION_MANAGEMENT_ID = #{muVacationManagementId}
						   AND MA.STATUS = 'D'
			      ) AS CombinedData
		  ) AS NumberedData
		<choose>
		<when test="vacationManagementSubSort != null and vacationManagementSubSort != ''">
	  ORDER BY ${vacationManagementSubSort}
		</when>
		<otherwise>
	  ORDER BY RN DESC, START_DATE
		</otherwise>
		</choose>
		 LIMIT 18446744073709551615
	</select>

	<select id="selectVacationManagementMember" parameterType="hashmap" resultType="hashmap">
		SELECT MVM.MU_VACATION_MANAGEMENT_ID                              AS "muVacationManagementId"
			  ,MVM.MU_MEMBER_ID                                           AS "muMemberId"
			  ,IFNULL((SELECT MM.NAME
						 FROM MU_MEMBER AS MM
						WHERE MM.USE_YN       = 'Y'
						  AND MM.MU_MEMBER_ID = MVM.MU_MEMBER_ID
						LIMIT 0, 1
			   ),'')                                                      AS "memberName"
			  ,IFNULL((SELECT MM.LOGIN_ID
						 FROM MU_MEMBER AS MM
						WHERE MM.USE_YN       = 'Y'
						  AND MM.MU_MEMBER_ID = MVM.MU_MEMBER_ID
						LIMIT 0, 1
			   ),'')                                                      AS "loginId"
			  ,MM.MU_GROUP_ID                                            AS "muGroupId"
			  ,IFNULL((SELECT MG.GROUP_NAME
						 FROM MU_GROUP AS MG
						WHERE MG.USE_YN      = 'Y'
			 			  AND MG.MU_GROUP_ID = MVM.MU_GROUP_ID
			 			LIMIT 0, 1
			   ),'')                                                      AS "groupName"
			  ,MM.MU_DUTY_ID                                             AS "muDutyId"
			  ,IFNULL((SELECT MD.DUTY_NAME
						 FROM MU_DUTY AS MD
						WHERE MD.USE_YN     = 'Y'
			 			  AND MD.MU_DUTY_ID = MVM.MU_DUTY_ID
			 			LIMIT 0, 1
			   ),'')                                                      AS "dutyName"
		  FROM MU_VACATION_MANAGEMENT AS MVM
		  JOIN MU_MEMBER AS MM
		  ON MVM.MU_MEMBER_ID = MM.MU_MEMBER_ID
		 WHERE MVM.USE_YN                    = 'Y'
		   AND MVM.YEAR                      = #{year}
		   AND MVM.MU_VACATION_MANAGEMENT_ID = #{muVacationManagementId}
	</select>
	<select id="downloadVacationManagementDetailExcel" parameterType="hashmap" resultType="hashmap">
		SELECT
			 MU_VACATION_MANAGEMENT_ID 									AS "muVacationManagementId"
			,MU_ATTENDANCE_ID 											AS "muAttendanceId"
			,ATTENDANCE_TYPE 											AS "attendanceType"
			,CASE ATTENDANCE_TYPE
				WHEN 'D' THEN '연차(전일)'
				WHEN 'E' THEN '반차(오전)'
				WHEN 'F' THEN '반차(오후)'
			 END 														AS "attendanceTypeKorName"
			,DATE_FORMAT(START_DATE, '%Y-%m-%d') 						AS "startDate"
			,DATE_FORMAT(END_DATE, '%Y-%m-%d') 							AS "endDate"
			,CASE
				WHEN START_DATE = END_DATE THEN DATE_FORMAT(START_DATE, '%Y-%m-%d')
				ELSE CONCAT(DATE_FORMAT(START_DATE, '%Y-%m-%d'), '~', DATE_FORMAT(END_DATE, '%Y-%m-%d'))
			 END 														AS "startEndDate"
			,USE_ATTENDANCE_COUNT 										AS "useDays"
			,SUM(USE_ATTENDANCE_COUNT) OVER (ORDER BY RN) 				AS "sumUseDays"
			,(DAYS + SUM(USE_ATTENDANCE_COUNT) OVER (ORDER BY RN)) 		AS "days"
			,ETC														AS "etc"
			,CANCEL_ETC 												AS "cancelEtc"
		FROM (
			SELECT *,
				   ROW_NUMBER() OVER (ORDER BY START_DATE) AS RN
			FROM (
				SELECT
					 MVM.MU_VACATION_MANAGEMENT_ID
					,MA.MU_ATTENDANCE_ID
					,MA.ATTENDANCE_TYPE
					,MA.START_DATE
					,MA.END_DATE
					,MA.STATUS
					,CASE
						WHEN MA.ATTENDANCE_TYPE = 'E' THEN -0.5
						ELSE -(ABS(DATEDIFF(MA.END_DATE, MA.START_DATE)) + 1)
					 END AS USE_ATTENDANCE_COUNT
					,MVM.DAYS
					,MA.ETC
					,MA.MOD_DATE
					,'' AS CANCEL_ETC
				FROM MU_VACATION_MANAGEMENT MVM
				INNER JOIN MU_ATTENDANCE MA
					ON MVM.MU_VACATION_MANAGEMENT_ID = MA.MU_VACATION_MANAGEMENT_ID
					AND MA.USE_YN = 'Y'
					AND MVM.USE_YN = 'Y'
					AND MVM.YEAR                      = #{vacationManagementSearchYear}
					AND MVM.MU_VACATION_MANAGEMENT_ID = #{muVacationManagementId}
				UNION ALL
				SELECT
					 MVM.MU_VACATION_MANAGEMENT_ID
					,MA.MU_ATTENDANCE_ID
					,MA.ATTENDANCE_TYPE
					,MA.START_DATE
					,MA.END_DATE
					,MA.STATUS
					,CASE
						WHEN MA.ATTENDANCE_TYPE = 'E' THEN 0.5
						ELSE (ABS(DATEDIFF(MA.END_DATE, MA.START_DATE)) + 1)
					 END AS USE_ATTENDANCE_COUNT
					,MVM.DAYS
					,MA.ETC
					,MA.MOD_DATE
					,MA.CANCEL_ETC
				FROM MU_VACATION_MANAGEMENT MVM
				INNER JOIN MU_ATTENDANCE MA
					ON MVM.MU_VACATION_MANAGEMENT_ID = MA.MU_VACATION_MANAGEMENT_ID
					AND MA.USE_YN = 'Y'
					AND MVM.USE_YN = 'Y'
					AND MVM.YEAR                      = #{vacationManagementSearchYear}
					AND MVM.MU_VACATION_MANAGEMENT_ID = #{muVacationManagementId}
					AND MA.STATUS = 'D'
			) CombinedData
		) NumberedData
		<choose>
			<when test="vacationManagementSubSort != null and vacationManagementSubSort != ''">
				ORDER BY ${vacationManagementSubSort}
			</when>
			<otherwise>
				ORDER BY RN DESC, START_DATE
			</otherwise>
		</choose>
		LIMIT 18446744073709551615
	</select>
	<select id="selectListMemberExcel" parameterType="hashmap" resultType="hashmap">
		SELECT
			  NAME 								AS "memberName"
			 ,LOGIN_ID							AS "loginId"
			 ,GROUP_NAME                        AS "groupName"
			 ,DUTY_NAME                         AS "dutyName"
		FROM MU_MEMBER MM
				 JOIN MU_GROUP MG
					  ON MM.MU_GROUP_ID = MG.MU_GROUP_ID
				 JOIN MU_DUTY MD
					  ON MM.MU_DUTY_ID = MD.MU_DUTY_ID
		WHERE MM.USE_YN='Y'
	</select>
	<select id="selectVacationManagementDate" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*)          AS "totalCount"
		  FROM MU_VACATION_MANAGEMENT AS MVM
		 WHERE MVM.USE_YN                                   = 'Y'
		   AND MVM.MU_MEMBER_ID                             = #{muMemberId}
		   AND ((DATE_FORMAT(MVM.START_DATE,'%Y-%m-%d') &lt;= #{startDate} AND DATE_FORMAT(MVM.END_DATE,'%Y-%m-%d')    >= #{startDate})
			OR (DATE_FORMAT(MVM.START_DATE,'%Y-%m-%d')  &lt;= #{endDate}   AND DATE_FORMAT(MVM.END_DATE,'%Y-%m-%d')    >= #{endDate})
			OR (DATE_FORMAT(MVM.START_DATE,'%Y-%m-%d')      > #{startDate} AND DATE_FORMAT(MVM.END_DATE,'%Y-%m-%d')  &lt; #{endDate}))
		 <if test="muVacationManagementId != null and muVacationManagementId != ''">
		   AND MU_VACATION_MANAGEMENT_ID != #{muVacationManagementId}
		 </if>
	</select>
	
	<select id="selectVacationManagementLeftDays" parameterType="hashmap" resultType="hashmap">
		SELECT MVM.MU_VACATION_MANAGEMENT_ID           AS "muVacationManagementId"
			  ,MVM.DAYS                                AS "days"
			  ,MVM.LEFT_DAYS                           AS "leftDays"
			  ,DATE_FORMAT(MVM.START_DATE,'%Y-%m-%d')  AS "startDate"
			  ,DATE_FORMAT(MVM.END_DATE,'%Y-%m-%d')    AS "endDate"
		  FROM MU_VACATION_MANAGEMENT AS MVM
		 WHERE MVM.USE_YN         = 'Y'
		   AND MVM.MU_MEMBER_ID   = #{muMemberId}
		   AND DATE_FORMAT(MVM.START_DATE, '%Y-%m-%d') &lt;= #{startDate}
		   AND DATE_FORMAT(MVM.END_DATE, '%Y-%m-%d')      >= #{endDate}
			LIMIT 0, 1
	</select>

	<select id="selectVacationManagementDays" parameterType="hashmap" resultType="hashmap">
		SELECT MVM.MU_VACATION_MANAGEMENT_ID           AS "muVacationManagementId"
			  ,MVM.DAYS                                AS "days"
			  ,CASE
			  WHEN DATE_FORMAT(MVM.END_DATE, '%Y-%m-%d') &lt; CURDATE() THEN 0
			  ELSE LEFT_DAYS
			  END                                      AS "leftDays"
			  ,DATE_FORMAT(MVM.START_DATE,'%Y-%m-%d')  AS "startDate"
			  ,DATE_FORMAT(MVM.END_DATE,'%Y-%m-%d')    AS "endDate"
		 FROM MU_VACATION_MANAGEMENT AS MVM
		 WHERE MVM.USE_YN         = 'Y'
		   AND MVM.MU_MEMBER_ID   = #{muMemberId}
		   AND MVM.END_DATE       = #{endDate}
		   AND MVM.START_DATE     = #{startDate}
	</select>
	
	<insert id="insertVacationManagement" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
			SELECT CONCAT(#{muVacationManagementId},LPAD((SELECT NEXTVAL(MU_VACATION_MANAGEMENT_SEQ)), 10, 0))
		</selectKey>
		INSERT INTO MU_VACATION_MANAGEMENT(
					 MU_VACATION_MANAGEMENT_ID
					,MU_MEMBER_ID
					,MU_GROUP_ID
					,MU_DUTY_ID
					,YEAR
					,DAYS
					,LEFT_DAYS
					,START_DATE
					,END_DATE
					,ETC
					,WRITE_DATE
					,WRITE_TIME
					,CUD_FLAG
					,USE_YN
					,REG_ID
					,REG_DATE
					,MOD_ID
					,MOD_DATE
					,T_ID
					,T_DATE
		)VALUES(
					 #{id}
					,#{muMemberId}
					,#{muGroupId}
					,#{muDutyId}
					,#{year}
					,#{days}
					,#{days}
					,#{startDate}
					,#{endDate}
					,#{etc, jdbcType=VARCHAR}
					,CURDATE()
					,CURTIME()
					,'C'
					,'Y'
					,#{regId}
					,NOW()
					,#{modId}
					,NOW()
					,#{tId}
					,#{tDate}
		)
	</insert>
	<update id="updateVacationManagement" parameterType="hashmap">
		UPDATE MU_VACATION_MANAGEMENT
		SET MOD_ID        = #{modId}
		  ,MOD_DATE	     = NOW()
		  ,CUD_FLAG      = 'U'
		  ,DAYS			 = #{days}
		  ,START_DATE	 = #{startDate}
		  ,END_DATE		 = #{endDate}
		  ,YEAR		     = #{year}
		  ,DAYS			 = #{days}
		  ,LEFT_DAYS     = #{leftDays}
		  ,ETC     		 = #{etc}
		WHERE USE_YN                     = 'Y'
		  AND MU_VACATION_MANAGEMENT_ID  = #{muVacationManagementId}
		  AND MU_MEMBER_ID               = #{muMemberId}
	</update>
	<update id="updateVacationManagementLeftDays" parameterType="hashmap">
		UPDATE MU_VACATION_MANAGEMENT
		   SET MOD_ID        = #{modId}
			  ,MOD_DATE	     = NOW()
			  ,CUD_FLAG      = 'U'
			  ,LEFT_DAYS     = LEFT_DAYS - #{days}
		 WHERE USE_YN                     = 'Y'
		   AND MU_VACATION_MANAGEMENT_ID  = #{muVacationManagementId}
		   AND MU_MEMBER_ID               = #{muMemberId}
	</update>
	
	<update id="deleteVacationManagement" parameterType="hashmap">
		UPDATE MU_VACATION_MANAGEMENT
		   SET MOD_ID        = #{modId}
			  ,MOD_DATE	     = NOW()
			  ,CUD_FLAG      = 'D'
			  ,USE_YN        = 'N'
		 WHERE USE_YN                     = 'Y'
		   AND MU_VACATION_MANAGEMENT_ID  = #{muVacationManagementId}
	</update>
</mapper>