<?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">
	 
	<!-- 해당 부분의 namespace는 project package + Mapper Package + Mapper Interface 이름입니다. -->
	<mapper namespace="com.elt.ems.mapper.BoardMapper">
	
		<!-- 해당 부분의 id는 MapperClass의 함수 이름과 유사하여야 합니다. -->
		
	    <sql id="commonPagingHeader"  >
	      SELECT R1.* FROM (
	    </sql>
	    
	    <sql id="commonPagingFooter"  >
	      ) R1 LIMIT #{pgtl.startNo}, #{pgtl.listPerPage}
	    </sql>  
	    		
		<select id="selectListCount" parameterType="com.elt.ems.vo.BoardVo"  resultType="int">
			select 
			  count(a.seq) as count
			from t_board a left join t_plant p 
			on a.plantSeq = p.seq
			where 1=1
			<if test="plantSeq > 0 ">and ( a.plantSeq = #{plantSeq} or a.plantSeq = 0 ) </if>
			<if test="statusCode != null and statusCode != '' ">and a.statusCode = #{statusCode} </if>
			<if test="todayDate != null and todayDate != '' ">and a.beginDate &gt;= #{todayDate} </if>
			<if test="beginDate != null and beginDate != '' ">and a.beginDate &lt;= #{beginDate} </if>
			<if test="endDate != null and endDate != '' ">and a.endDate &gt;= #{endDate} </if>
		</select>
				
		<select id="selectList" parameterType="com.elt.ems.vo.BoardVo"  resultType="com.elt.ems.vo.BoardVo">
	      <if test="pgtl != null">
	        <include refid="commonPagingHeader" /> 
	      </if>
	      		
			select 
			  a.*, p.name as plantName
			from t_board a left join t_plant p 
			on a.plantSeq = p.seq
			where 1=1
			<if test="plantSeq > 0 ">and ( a.plantSeq = #{plantSeq} or a.plantSeq = 0 )  </if>
			<if test="statusCode != null and statusCode != '' ">and a.statusCode = #{statusCode} </if>	
			<if test="todayDate != null and todayDate != '' ">and a.beginDate &gt;= #{todayDate} </if>		
			<if test="beginDate != null and beginDate != '' ">and a.beginDate &lt;= #{beginDate} </if>
			<if test="endDate != null and endDate != '' ">and a.endDate &gt;= #{endDate} </if>
	     		order by a.beginDate desc, a.seq desc 
	      <if test="pgtl != null">
	        <include refid="commonPagingFooter" />
	      </if>
	      	     		
		</select>
			
	    <select id="select" parameterType="int"  resultType="com.elt.ems.vo.BoardVo">
	      select a.*, p.name as plantName
	      from t_board a left join t_plant p 
	      on a.plantSeq = p.seq
	      where a.seq = #{seq}       
	    </select>		    
	    
	    
		<insert id="insert"  parameterType="com.elt.ems.vo.BoardVo">
			insert into t_board (
				title, contents, beginDate, endDate, statusCode, plantSeq, serverName, createId, updateId, createDateTime, updateDatetime
			) values (
				#{title},
				#{contents},
				#{beginDate},
				#{endDate},
				#{statusCode},
				#{plantSeq},
				#{serverName},
				#{createId},
				#{updateId},
				now(),
				now()
			)
		</insert>
			    
	    <update id="update"  parameterType="com.elt.ems.vo.BoardVo">
	      update t_board set  
	        <if test="title != null and title != '' "> title=#{title}, </if> 
	        <if test="contents != null and contents != '' "> contents=#{contents}, </if>
	        <if test="beginDate != null and beginDate != '' "> beginDate=#{beginDate},  </if>
	        <if test="endDate != null and endDate != '' "> endDate=#{endDate}, </if> 
	        <if test="statusCode != null and statusCode != '' "> statusCode=#{statusCode}, </if>
	        <if test="attach != null and attach != '' "> attach=#{attach}, </if>
	        <if test="serverName != null and serverName != '' "> serverName=#{serverName}, </if>
	        plantSeq=#{plantSeq}, updateId=#{updateId}, updateDatetime=now()
	      where seq=#{seq}
	    </update>   
	    	      	    	
		<select id="selectListNoticeCount" parameterType="java.util.HashMap"  resultType="int">
			select count(a.seq) as count
			from t_notice a, ( 
				select seq, 
					CONCAT(#{todayYy}, substr(startDate, 5, 6)) as startDate01, 
			        CONCAT(#{todayYm}, substr(startDate, 8, 3)) as startDate12, 
			        date_add(CONCAT(#{todayYy}, substr(startDate, 5, 6)), INTERVAL datediff(endDate, startDate) DAY) as endDate01,
			        date_add(CONCAT(#{todayYm}, substr(startDate, 8, 3)), INTERVAL datediff(endDate, startDate) DAY) as endDate12
				from t_notice
			) b
			where a.seq = b.seq 
			<if test="statusCode != null and statusCode != '' ">and a.statusCode = #{statusCode} </if>
			<if test="repeatCode != null and repeatCode != '' ">
			and (
				(a.repeatCode ='01' and #{todayDate} between b.startDate01 and b.endDate01) or
			    (a.repeatCode ='12' and #{todayDate} between b.startDate12 and b.endDate12) or
			    (a.repeatCode ='00' and #{todayDate} between a.startDate and a.endDate) ) 
			</if>
		</select>
				
<!-- 				
		<select id="selectListNotice" parameterType="java.util.HashMap"  resultType="com.elt.ems.vo.NoticeVo">  -->
		<select id="selectListNotice" parameterType="java.util.HashMap"  resultType="com.elt.ems.common.CaseSensibleHashMap">
	      <if test="pgtl != null">
	        <include refid="commonPagingHeader" /> 
	      </if>	      		
			select b.startDate01, b.startDate12, b.endDate01, b.endDate12, a.*
			from t_notice a, ( 
				select seq, 
					CONCAT(#{todayYy}, substr(startDate, 5, 6)) as startDate01, 
			        CONCAT(#{todayYm}, substr(startDate, 8, 3)) as startDate12, 
			        date_add(CONCAT(#{todayYy}, substr(startDate, 5, 6)), INTERVAL datediff(endDate, startDate) DAY) as endDate01,
			        date_add(CONCAT(#{todayYm}, substr(startDate, 8, 3)), INTERVAL datediff(endDate, startDate) DAY) as endDate12
				from t_notice
			) b
			where a.seq = b.seq 
			<if test="statusCode != null and statusCode != '' ">and a.statusCode = #{statusCode} </if>
			<if test="repeatCode != null and repeatCode != '' ">
			and (
				(a.repeatCode ='01' and #{todayDate} between b.startDate01 and b.endDate01) or
			    (a.repeatCode ='12' and #{todayDate} between b.startDate12 and b.endDate12) or
			    (a.repeatCode ='00' and #{todayDate} between a.startDate and a.endDate) ) 
			</if>
	     		order by a.seq desc 
	      <if test="pgtl != null">
	        <include refid="commonPagingFooter" />
	      </if>	      	     	
		</select>
		
	    <select id="selectNotice" parameterType="int"  resultType="com.elt.ems.vo.NoticeVo">
	      select a.*
	      from t_notice a 
	      where a.seq = #{seq}       
	    </select>		    
	    
	    <update id="updateNotice"  parameterType="com.elt.ems.vo.NoticeVo">
	      update t_notice set  
	        <if test="title != null and title != '' "> title=#{title}, </if> 
	        <if test="contents != null and contents != '' "> contents=#{contents}, </if>
	        <if test="link != null and link != '' "> link=#{link}, </if>
	        <if test="syear != null and syear != '' "> syear=#{syear},  </if>
	        <if test="smonth != null and smonth != '' "> smonth=#{smonth}, </if> 	        
	        <if test="eyear != null and eyear != '' "> eyear=#{eyear},  </if>
	        <if test="emonth != null and emonth != '' "> emonth=#{emonth}, </if> 	        
	        <if test="startDate != null and startDate != '' "> startDate=#{startDate}, </if>
	        <if test="endDate != null and endDate != '' "> endDate=#{endDate}, </if> 
	        <if test="repeatCode != null and repeatCode != '' "> repeatCode=#{repeatCode}, </if>
	        <if test="statusCode != null and statusCode != '' "> statusCode=#{statusCode}, </if>
	        updateId=#{updateId}, updateDatetime=now()
	      where seq=#{seq}
	    </update>  
	    	    		
		<insert id="insertNotice"  parameterType="com.elt.ems.vo.NoticeVo">
			insert into t_notice (
				title, contents, link, startDate, endDate, syear, smonth, eyear, emonth, repeatCode, statusCode, createId, updateId, createDateTime, updateDatetime
			) values (
				#{title},
				#{contents},
				#{link},
				#{startDate},
				#{endDate},
				#{syear},
				#{smonth},				
				#{eyear},
				#{emonth},				
				#{repeatCode},
				#{statusCode},
				#{createId},
				#{updateId},
				now(),
				now()
			)
		</insert>
				
	</mapper>