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

	<select id="selectTotalWorkCount" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*)  AS "totalCount"
		  FROM MU_WORK AS MW
		 WHERE MW.USE_YN = 'Y'
		   <if test="workSearchGroupId != null and workSearchGroupId != ''">
		   AND MW.MU_GROUP_ID  = #{workSearchGroupId}
		   </if>
		   <if test="workSearchDutyId != null and workSearchDutyId != ''">
		   AND MW.MU_DUTY_ID = #{workSearchDutyId}
		   </if>
		   <if test="workSearchKeywordParam != null and workSearchKeywordParam != ''">
		   AND ((MW.WORK_NAME LIKE CONCAT('%',TRIM(#{workSearchKeywordParam}),'%'))
			OR (MW.ETC LIKE CONCAT('%',TRIM(#{workSearchKeywordParam}),'%')))
		   </if>
	</select>
	
	<select id="selectListWork" parameterType="hashmap" resultType="hashmap">
		SELECT MW.*
		  FROM (
					SELECT MW.*
						  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
					  FROM (
								SELECT MW.MU_WORK_ID                          AS "muWorkId"
									  ,MW.WORK_NAME                           AS "workName"
									  ,DATE_FORMAT(MW.START_TIME, '%H:%i')    AS "startTime"
									  ,DATE_FORMAT(MW.END_TIME, '%H:%i')      AS "endTime"
									  ,CONCAT(DATE_FORMAT(MW.START_TIME, '%H:%i')
									   ,'~'
									   ,DATE_FORMAT(MW.END_TIME, '%H:%i'))    AS "startEndTime"
									  ,MW.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 = MW.MU_GROUP_ID
									 			LIMIT 0, 1
									   ),'')                                  AS "groupName"
									  ,MW.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 = MW.MU_DUTY_ID
									 			LIMIT 0, 1
									   ),'')                                  AS "dutyName"
									  ,IFNULL(MW.ETC, '')                     AS "etc"
									  ,DATE_FORMAT(MW.WRITE_DATE, '%Y-%m-%d') AS "writeDate"
								  FROM MU_WORK AS MW
								 WHERE MW.USE_YN = 'Y'
								   <if test="workSearchGroupId != null and workSearchGroupId != ''">
								   AND MW.MU_GROUP_ID  = #{workSearchGroupId}
								   </if>
								   <if test="workSearchDutyId != null and workSearchDutyId != ''">
								   AND MW.MU_DUTY_ID = #{workSearchDutyId}
								   </if>
								   <if test="workSearchKeywordParam != null and workSearchKeywordParam != ''">
								   AND ((MW.WORK_NAME LIKE CONCAT('%',TRIM(#{workSearchKeywordParam}),'%'))
									OR (MW.ETC LIKE CONCAT('%',TRIM(#{workSearchKeywordParam}),'%')))
								   </if>
								 <choose>
								 <when test="workSort != null and workSort != ''">
							  ORDER BY ${workSort}
								 </when>
								 <otherwise>
							  ORDER BY MW.WRITE_DATE DESC
								 </otherwise>
								 </choose>
								 LIMIT 18446744073709551615
						   ) MW, (SELECT @RNUM:=0) R
					 WHERE 1 = 1
			   ) MW
		 WHERE 1 = 1
		 LIMIT ${workStart}, ${workLimit}
	</select>

	<select id="selectWork" parameterType="hashmap" resultType="hashmap">
		SELECT MW.MU_WORK_ID                          AS "muWorkId"
			  ,MW.WORK_NAME                           AS "workName"
			  ,DATE_FORMAT(MW.START_TIME, '%H:%i')    AS "startTime"
			  ,DATE_FORMAT(MW.END_TIME, '%H:%i')      AS "endTime"
			  ,MW.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 = MW.MU_GROUP_ID
						LIMIT 0, 1
			   ),'')                                  AS "groupName"
			  ,MW.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 = MW.MU_DUTY_ID
			 			LIMIT 0, 1
			   ),'')                                  AS "dutyName"
			  ,IFNULL(MW.ETC, '')                     AS "etc"
			  ,DATE_FORMAT(MW.WRITE_DATE, '%Y-%m-%d') AS "writeDate"
		  FROM MU_WORK AS MW
		 WHERE MW.USE_YN        = 'Y'
		   AND MW.MU_WORK_ID  = #{muWorkId}
		 LIMIT 0, 1
	</select>

	<insert id="insertWork" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
			SELECT CONCAT(#{muWorkId},LPAD((SELECT NEXTVAL(MU_WORK_SEQ)), 11, 0))
		</selectKey>
		INSERT INTO MU_WORK(
					 MU_WORK_ID
					,WORK_NAME
					,START_TIME
					,END_TIME
					,MU_GROUP_ID
					,MU_DUTY_ID
					,ETC
					,WRITE_DATE
					,WRITE_TIME
					,CUD_FLAG
					,USE_YN
					,REG_ID
					,REG_DATE
					,MOD_ID
					,MOD_DATE
					,T_ID
					,T_DATE
		)VALUES(
					 #{id}
					,#{workName}
					,#{startTime}
					,#{endTime}
					,#{muGroupId}
					,#{muDutyId}
					,#{etc, jdbcType=VARCHAR}
					,CURDATE()
					,CURTIME()
					,'C'
					,'Y'
					,#{regId}
					,NOW()
					,#{modId}
					,NOW()
					,#{tId}
					,#{tDate}
		)
	</insert>
	
	<update id="updateWork" parameterType="hashmap">
		UPDATE MU_WORK
		   SET MOD_ID              = #{modId}
			  ,MOD_DATE            = NOW()
			  ,CUD_FLAG            = 'U'
			  ,WORK_NAME           = #{workName}
			  ,START_TIME          = #{startTime}
			  ,END_TIME            = #{endTime}
			  ,MU_GROUP_ID         = #{muGroupId}
			  ,MU_DUTY_ID          = #{muDutyId}
			  ,ETC                 = #{etc, jdbcType=VARCHAR}
		 WHERE USE_YN        = 'Y'
		   AND MU_WORK_ID  = #{muWorkId}
	</update>
	
	<update id="deleteWork" parameterType="hashmap">
		UPDATE MU_WORK
		   SET MOD_ID            = #{modId}
			  ,MOD_DATE	         = NOW()
			  ,CUD_FLAG          = 'D'
			  ,USE_YN            = 'N'
		 WHERE USE_YN      = 'Y'
		   AND MU_WORK_ID  = #{muWorkId}
	</update>
</mapper>