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

	<select id="selectTotalHolidayManagementCount" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*)  AS "totalCount"
		  FROM (
					SELECT MA.MU_ATTENDANCE_ID
						  ,MA.MU_MEMBER_ID
						  ,IFNULL((SELECT MM.NAME
									 FROM MU_MEMBER AS MM
									WHERE MM.USE_YN       = 'Y'
									  AND MM.MU_MEMBER_ID = MA.MU_MEMBER_ID
									LIMIT 0, 1
						   ),'')                                  AS MEMBER_NAME
						  ,MA.MU_GROUP_ID
						  ,MA.MU_DUTY_ID
						  ,DATE_FORMAT(MA.START_DATE, '%Y-%m-%d') AS START_DATE
						  ,DATE_FORMAT(MA.END_DATE, '%Y-%m-%d')   AS END_DATE
						  ,MA.ETC
						  ,MA.CANCEL_ETC
						  ,MA.WRITE_MEMBER_ID
						  ,IFNULL((SELECT MM.NAME
									 FROM MU_MEMBER AS MM
									WHERE MM.USE_YN       = 'Y'
									  AND MM.MU_MEMBER_ID = MA.WRITE_MEMBER_ID
									LIMIT 0, 1
						   ),'')                                  AS WRITE_MEMBER_NAME
					  FROM MU_ATTENDANCE AS MA
					 WHERE MA.USE_YN          = 'Y'
					   AND MA.ATTENDANCE_TYPE = 'C'
					   AND DATE_FORMAT(MA.START_DATE,'%Y-%m') &lt;= #{holidayManagementSearchDate}
					   AND DATE_FORMAT(MA.END_DATE,'%Y-%m')   &gt;= #{holidayManagementSearchDate}
			   ) AS MA
		 WHERE 1=1
		   <if test="holidayManagementSearchGroupId != null and holidayManagementSearchGroupId != ''">
		   AND MU_GROUP_ID  = #{holidayManagementSearchGroupId}
		   </if>
		   <if test="holidayManagementSearchDutyId != null and holidayManagementSearchDutyId != ''">
		   AND MU_DUTY_ID   = #{holidayManagementSearchDutyId}
		   </if>
		   <if test="holidayManagementSearchKeywordParam != null and holidayManagementSearchKeywordParam != ''">
		   AND ((MEMBER_NAME LIKE CONCAT('%',TRIM(#{holidayManagementSearchKeywordParam}),'%'))
			OR (WRITE_MEMBER_NAME LIKE CONCAT('%',TRIM(#{holidayManagementSearchKeywordParam}),'%'))
			OR (ETC LIKE CONCAT('%',TRIM(#{holidayManagementSearchKeywordParam}),'%'))
			OR (CANCEL_ETC LIKE CONCAT('%',TRIM(#{holidayManagementSearchKeywordParam}),'%')))
		   </if>
	</select>
	
	<select id="selectListHolidayManagement" parameterType="hashmap" resultType="hashmap">
		SELECT MA.*
		  FROM (
					SELECT MA.*
						  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
					  FROM (
								SELECT MU_ATTENDANCE_ID                            AS "muAttendanceId"
									  ,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 = MA.MU_GROUP_ID
									 			LIMIT 0, 1
									   ),'')                                       AS "groupName"
									  ,MA.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 = MA.MU_DUTY_ID
									 			LIMIT 0, 1
									   ),'')                                       AS "dutyName"
									  ,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"
									  ,ABS(DATEDIFF(START_DATE, END_DATE))+1       AS "days"
									  ,MA.STATUS                                   AS "status"
									  ,CASE MA.STATUS
											WHEN 'C' THEN '등록'
											WHEN 'U' THEN '등록(수정)'
											WHEN 'D' THEN '취소'
											ELSE ''
									   END                                         AS "statusKorName"
									  ,IFNULL(ETC, '')                             AS "etc"
									  ,IFNULL(CANCEL_ETC, '')                      AS "cancelEtc"
									  ,DATE_FORMAT(CANCEL_DATE, '%Y-%m-%d')        AS "cancelDate"
									  ,DATE_FORMAT(WRITE_DATE, '%Y-%m-%d')         AS "writeDate"
								  FROM (
											SELECT MA.MU_ATTENDANCE_ID
												  ,MA.MU_MEMBER_ID
												  ,IFNULL((SELECT MM.NAME
															 FROM MU_MEMBER AS MM
															WHERE MM.USE_YN       = 'Y'
															  AND MM.MU_MEMBER_ID = MA.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 = MA.MU_MEMBER_ID
															LIMIT 0, 1
												   ),'')                                  AS LOGIN_ID
												  ,MM.MU_GROUP_ID
												  ,MM.MU_DUTY_ID
												  ,MA.ATTENDANCE_TYPE
												  ,MA.START_DATE
												  ,MA.END_DATE
												  ,MA.ATTENDANCE_TIME
												  ,MA.ETC
												  ,MA.STATUS
												  ,MA.CANCEL_DATE
												  ,MA.CANCEL_ETC
												  ,MA.WRITE_DATE
												  ,MA.WRITE_MEMBER_ID
												  ,IFNULL((SELECT MM.NAME
															 FROM MU_MEMBER AS MM
															WHERE MM.USE_YN       = 'Y'
															  AND MM.MU_MEMBER_ID = MA.WRITE_MEMBER_ID
															LIMIT 0, 1
												   ),'')                                  AS WRITE_MEMBER_NAME
											  FROM MU_ATTENDANCE AS MA
											  JOIN MU_MEMBER MM
											  ON MA.MU_MEMBER_ID = MM.MU_MEMBER_ID
											  AND MM.USE_YN='Y'
											 WHERE MA.USE_YN          = 'Y'
											   AND MA.ATTENDANCE_TYPE = 'C'
											   AND DATE_FORMAT(MA.START_DATE,'%Y-%m') &lt;= #{holidayManagementSearchDate}
											   AND DATE_FORMAT(MA.END_DATE,'%Y-%m')   &gt;= #{holidayManagementSearchDate}
									   ) AS MA
								 WHERE 1=1
								   <if test="holidayManagementSearchGroupId != null and holidayManagementSearchGroupId != ''">
								   AND MU_GROUP_ID  = #{holidayManagementSearchGroupId}
								   </if>
								   <if test="holidayManagementSearchDutyId != null and holidayManagementSearchDutyId != ''">
								   AND MU_DUTY_ID = #{holidayManagementSearchDutyId}
								   </if>
								   <if test="holidayManagementSearchKeywordParam != null and holidayManagementSearchKeywordParam != ''">
								   AND ((MEMBER_NAME LIKE CONCAT('%',TRIM(#{holidayManagementSearchKeywordParam}),'%'))
									OR (WRITE_MEMBER_NAME LIKE CONCAT('%',TRIM(#{holidayManagementSearchKeywordParam}),'%'))
									OR (ETC LIKE CONCAT('%',TRIM(#{holidayManagementSearchKeywordParam}),'%'))
									OR (CANCEL_ETC LIKE CONCAT('%',TRIM(#{holidayManagementSearchKeywordParam}),'%')))
								   </if>
								 <choose>
								 <when test="holidayManagementSort != null and holidayManagementSort != ''">
							  ORDER BY ${holidayManagementSort}
								 </when>
								 <otherwise>
							  ORDER BY MA.WRITE_DATE DESC
								 </otherwise>
								 </choose>
								 LIMIT 18446744073709551615
						   ) MA, (SELECT @RNUM:=0) R
					 WHERE 1 = 1
			   ) MA
		 WHERE 1 = 1
		 LIMIT ${holidayManagementStart}, ${holidayManagementLimit}
	</select>
	
	<select id="selectListHolidayManagementExcel" parameterType="hashmap" resultType="hashmap">
		SELECT MA.*
		  FROM (
					SELECT MA.*
						  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
					  FROM (
								SELECT MU_ATTENDANCE_ID                            AS "muAttendanceId"
									  ,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 = MA.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 = MA.MU_DUTY_ID
									 			LIMIT 0, 1
									   ),'')                                       AS "dutyName"
									  ,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"
									  ,ABS(DATEDIFF(START_DATE, END_DATE))+1       AS "days"
									  ,MA.STATUS                                   AS "status"
									  ,CASE MA.STATUS
											WHEN 'C' THEN '등록'
											WHEN 'U' THEN '등록(수정)'
											WHEN 'D' THEN '취소'
											ELSE ''
									   END                                         AS "statusKorName"
									  ,IFNULL(ETC, '')                             AS "etc"
									  ,IFNULL(CANCEL_ETC, '')                      AS "cancelEtc"
									  ,DATE_FORMAT(CANCEL_DATE, '%Y-%m-%d')        AS "cancelDate"
									  ,DATE_FORMAT(WRITE_DATE, '%Y-%m-%d')         AS "writeDate"
								  FROM (
											SELECT MA.MU_ATTENDANCE_ID
												  ,MA.MU_MEMBER_ID
												  ,IFNULL((SELECT MM.NAME
															 FROM MU_MEMBER AS MM
															WHERE MM.USE_YN       = 'Y'
															  AND MM.MU_MEMBER_ID = MA.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 = MA.MU_MEMBER_ID
															LIMIT 0, 1
												   ),'')                                  AS LOGIN_ID
												  ,MM.MU_GROUP_ID
												  ,MM.MU_DUTY_ID
												  ,MA.ATTENDANCE_TYPE
												  ,MA.START_DATE
												  ,MA.END_DATE
												  ,MA.ATTENDANCE_TIME
												  ,MA.ETC
												  ,MA.STATUS
												  ,MA.CANCEL_DATE
												  ,MA.CANCEL_ETC
												  ,MA.WRITE_DATE
												  ,MA.WRITE_MEMBER_ID
												  ,IFNULL((SELECT MM.NAME
															 FROM MU_MEMBER AS MM
															WHERE MM.USE_YN       = 'Y'
															  AND MM.MU_MEMBER_ID = MA.WRITE_MEMBER_ID
															LIMIT 0, 1
												   ),'')                                  AS WRITE_MEMBER_NAME
											  FROM MU_ATTENDANCE AS MA
											  JOIN MU_MEMBER MM
											  ON MA.MU_MEMBER_ID = MM.MU_MEMBER_ID
											  AND MM.USE_YN='Y'
											 WHERE MA.USE_YN          = 'Y'
											   AND MA.ATTENDANCE_TYPE = 'C'
											   AND DATE_FORMAT(MA.START_DATE,'%Y-%m') &lt;= #{holidayManagementSearchDate}
											   AND DATE_FORMAT(MA.END_DATE,'%Y-%m')   &gt;= #{holidayManagementSearchDate}
									   ) AS MA
								 WHERE 1=1
								   <if test="holidayManagementSearchGroupId != null and holidayManagementSearchGroupId != ''">
								   AND MU_GROUP_ID  = #{holidayManagementSearchGroupId}
								   </if>
								   <if test="holidayManagementSearchDutyId != null and holidayManagementSearchDutyId != ''">
								   AND MU_DUTY_ID = #{holidayManagementSearchDutyId}
								   </if>
								   <if test="holidayManagementSearchKeywordParam != null and holidayManagementSearchKeywordParam != ''">
								   AND ((MEMBER_NAME LIKE CONCAT('%',TRIM(#{holidayManagementSearchKeywordParam}),'%'))
									OR (WRITE_MEMBER_NAME LIKE CONCAT('%',TRIM(#{holidayManagementSearchKeywordParam}),'%'))
									OR (ETC LIKE CONCAT('%',TRIM(#{holidayManagementSearchKeywordParam}),'%'))
									OR (CANCEL_ETC LIKE CONCAT('%',TRIM(#{holidayManagementSearchKeywordParam}),'%')))
								   </if>
								 <choose>
								 <when test="holidayManagementSort != null and holidayManagementSort != ''">
							  ORDER BY ${holidayManagementSort}
								 </when>
								 <otherwise>
							  ORDER BY MA.WRITE_DATE DESC
								 </otherwise>
								 </choose>
								 LIMIT 18446744073709551615
						   ) MA, (SELECT @RNUM:=0) R
					 WHERE 1 = 1
			   ) MA
		 WHERE 1 = 1
	</select>

	<select id="selectHolidayManagement" parameterType="hashmap" resultType="hashmap">
		SELECT MA.MU_ATTENDANCE_ID                                AS "muAttendanceId"
			  ,MA.MU_MEMBER_ID                                    AS "muMemberId"
			  ,IFNULL((SELECT MM.NAME
						 FROM MU_MEMBER AS MM
						WHERE MM.USE_YN       = 'Y'
						  AND MM.MU_MEMBER_ID = MA.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 = MA.MU_MEMBER_ID
						LIMIT 0, 1
			   ),'')                                              AS "loginId"
			  ,MA.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 = MA.MU_GROUP_ID
			 			LIMIT 0, 1
			   ),'')                                              AS "groupName"
			  ,MA.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 = MA.MU_DUTY_ID
			 			LIMIT 0, 1
			   ),'')                                              AS "dutyName"
			  ,DATE_FORMAT(MA.START_DATE, '%Y-%m-%d')             AS "startDate"
			  ,DATE_FORMAT(MA.END_DATE, '%Y-%m-%d')               AS "endDate"
			  ,ABS(DATEDIFF(MA.START_DATE, MA.END_DATE))+1        AS "days"
			  ,MA.STATUS                                          AS "status"
			  ,CASE MA.STATUS
					WHEN 'C' THEN '등록'
					WHEN 'U' THEN '등록(수정)'
					WHEN 'D' THEN '취소'
					ELSE ''
			   END                                                AS "statusKorName"
			  ,IFNULL(MA.ETC, '')                                 AS "etc"
			  ,IFNULL(MA.CANCEL_ETC, '')                          AS "cancelEtc"
			  ,DATE_FORMAT(MA.CANCEL_DATE, '%Y-%m-%d, %H:%i')     AS "cancelDate"
			  ,DATE_FORMAT(MA.WRITE_DATE, '%Y-%m-%d')             AS "writeDate"
			  ,DATE_FORMAT(MA.WRITE_TIME, '%H:%i')                AS "writeTime"
			  ,MA.WRITE_MEMBER_ID                                 AS "writeMemberId"
			  ,IFNULL((SELECT MM.NAME
						 FROM MU_MEMBER AS MM
						WHERE MM.USE_YN       = 'Y'
						  AND MM.MU_MEMBER_ID = MA.WRITE_MEMBER_ID
						LIMIT 0, 1
			   ),'')                                              AS "writeMemberName"
			  ,MA.CANCEL_MEMBER_ID                                 AS "cancelMemberId"
			  ,IFNULL((SELECT MM.NAME
						 FROM MU_MEMBER AS MM
						WHERE MM.USE_YN       = 'Y'
						  AND MM.MU_MEMBER_ID = MA.CANCEL_MEMBER_ID
						LIMIT 0, 1
			   ),'')                                              AS "cancelMemberName"
		  FROM MU_ATTENDANCE AS MA
		 WHERE MA.USE_YN            = 'Y'
		   AND MA.ATTENDANCE_TYPE   = 'C'
		   AND MA.MU_ATTENDANCE_ID  = #{muAttendanceId}
		 LIMIT 0, 1
	</select>

	<insert id="insertHolidayManagement" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
			SELECT CONCAT(#{muAttendanceId},LPAD((SELECT NEXTVAL(MU_ATTENDANCE_SEQ)), 11, 0))
		</selectKey>
		INSERT INTO MU_ATTENDANCE(
					 MU_ATTENDANCE_ID
					,MU_MEMBER_ID
					,MU_GROUP_ID
					,MU_DUTY_ID
					,ATTENDANCE_TYPE
					,START_DATE
					,END_DATE
					,ETC
					,STATUS
					,WRITE_MEMBER_ID
					,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}
					,'C'
					,#{startDate}
					,#{endDate}
					,#{etc, jdbcType=VARCHAR}
					,'C'
					,#{regId}
					,CURDATE()
					,CURTIME()
					,'C'
					,'Y'
					,#{regId}
					,NOW()
					,#{modId}
					,NOW()
					,#{tId}
					,#{tDate}
		)
	</insert>
	
	<update id="updateHolidayManagement" parameterType="hashmap">
		UPDATE MU_ATTENDANCE
		   SET MOD_ID        = #{modId}
			  ,MOD_DATE      = NOW()
			  ,CUD_FLAG      = 'U'
			  ,STATUS        = 'U'
			  ,START_DATE    = #{startDate}
			  ,ETC           = #{etc, jdbcType=VARCHAR}
			  ,END_DATE      = #{endDate}
		 WHERE USE_YN            = 'Y'
		   AND MU_ATTENDANCE_ID  = #{muAttendanceId}
	</update>
	
	<update id="updateHolidayManagementCancel" parameterType="hashmap">
		UPDATE MU_ATTENDANCE
		   SET MOD_ID                  = #{modId}
			  ,MOD_DATE                = NOW()
			  ,CUD_FLAG                = 'U'
			  ,STATUS                  = 'D'
			  ,CANCEL_MEMBER_ID        = #{modId}
			  ,CANCEL_DATE             = NOW()
			  ,CANCEL_ETC              = #{cancelEtc}
		 WHERE USE_YN            = 'Y'
		   AND MU_ATTENDANCE_ID  = #{muAttendanceId}
	</update>
	
	<update id="updateHolidayManagementCancelEtc" parameterType="hashmap">
		UPDATE MU_ATTENDANCE
		   SET MOD_ID        = #{modId}
			  ,MOD_DATE      = NOW()
			  ,CUD_FLAG      = 'U'
			  ,CANCEL_ETC    = #{cancelEtc}
		 WHERE USE_YN            = 'Y'
		   AND MU_ATTENDANCE_ID  = #{muAttendanceId}
	</update>
	
	<update id="deleteHolidayManagement" parameterType="hashmap">
		UPDATE MU_ATTENDANCE
		   SET MOD_ID        = #{modId}
			  ,MOD_DATE	     = NOW()
			  ,CUD_FLAG      = 'D'
			  ,USE_YN        = 'N'
		 WHERE USE_YN            = 'Y'
		   AND MU_ATTENDANCE_ID  = #{muAttendanceId}
	</update>
</mapper>