<?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, createId, updateId, createDateTime, updateDatetime
			) values (
				#{title},
				#{contents},
				#{beginDate},
				#{endDate},
				#{statusCode},
				#{plantSeq},
				#{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>
	        plantSeq=#{plantSeq}, updateId=#{updateId}, updateDatetime=now()
	      where seq=#{seq}
	    </update>     
		
	</mapper>