<?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.PlantMapper">
			
		<resultMap id="plantResult" type="com.elt.ems.vo.PlantVo">
		  <id property="seq" column="seq" />
		  <id property="name" column="name" />
		  <id property="plantType" column="plantType" />
		  <id property="plantStatus" column="plantStatus" />
		  <id property="supplyPower" column="supplyPower" />
		  <id property="beginDate" column="beginDate" />
		  <id property="geox" column="geox" />
		  <id property="geoy" column="geoy" />
		  <id property="weatherx" column="weatherx" />
		  <id property="weathery" column="weathery" />
		  <id property="omStartDate" column="omStartDate" />
		  <id property="omEndDate" column="omEndDate" />
		  <id property="createDatetime" column="createDatetime" />
		  <id property="updateDatetime" column="updateDatetime" />
		  <id property="pmsdataDatetime" column="pmsdataDatetime" />
		  
		  <association property="pms" column="plantSeq" javaType="com.elt.ems.vo.PmsVo" resultMap="pmsResult"/>
		</resultMap>
			
		<resultMap id="pmsResult" type="com.elt.ems.vo.PmsVo">
		  <id property="plantSeq" column="plantSeq"/>
		  <result property="ip" column="ip"/>
		  <result property="port" column="port"/>
		  <result property="status" column="status"/>
		  <result property="startIdx" column="startIdx"/>
		  <result property="pmsMaker" column="pmsMaker"/>
		  <result property="pcsMaker" column="pcsMaker"/>
		  <result property="batteryMaker" column="batteryMaker"/>
		  <result property="pcsQuantity" column="pcsQuantity"/>
		  <result property="pcsVolume" column="pcsVolume"/>
		</resultMap>        

		<!-- 해당 부분의 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.PlantVo" resultType="integer">
	
			select count(p.seq) as count 
			from t_plant p
			where 1=1  
			<if test="seq > 0"> and p.seq = #{seq}</if> 
			<if test="pms.pcsMaker != null"> and p.seq = #{pms.pcsMaker}</if>
			<if test="pms.batteryMaker != null"> and p.seq = #{pms.batteryMaker}</if>    
	    </select>
	    		
	    <select id="selectList"  parameterType="com.elt.ems.vo.PlantVo" resultType="com.elt.ems.vo.PlantVo">
	
	      <if test="pgtl != null">
	        <include refid="commonPagingHeader" /> 
	      </if>
	
			select 
				p.*,
				(select max(d.createDatetime) from t_pmsdata_2020 d where d.plantSeq = p.seq and d.pcsIdx = 1 and d.inputDate = date_format(now(), '%Y-%m-%d')) as pmsdataDatetime
			from t_plant p
			where 1=1 	  
			order by p.seq desc
	      <if test="pgtl != null">
	        <include refid="commonPagingFooter" />
	      </if>
	      
	    </select>
	    
	    <select id="selectDetailList"  parameterType="com.elt.ems.vo.PlantVo" resultMap="plantResult">
	
	      <if test="pgtl != null">
	        <include refid="commonPagingHeader" /> 
	      </if>
	
			select 
				p.*,
				(select max(d.createDatetime) from t_pmsdata_2020 d where d.plantSeq = p.seq and d.pcsIdx = 1 and d.inputDate = date_format(now(), '%Y-%m-%d')) as pmsdataDatetime,
				m.plantSeq, m.ip, m.port, m.status, m.startIdx, m.pmsMaker, m.pmsModel, m.pcsMaker, m.pcsVolume, m.pcsQuantity, m.batteryMaker, m.batteryModel, m.batteryVolume, m.batteryQuantity
			from t_plant p  left join t_pms m
			on p.seq = m.plantSeq
			where 1=1 
			<if test="seq > 0"> and p.seq = #{seq}</if>
			<if test="pms.pcsMaker != null"> and p.seq = #{pms.pcsMaker}</if>
			<if test="pms.batteryMaker != null"> and p.seq = #{pms.batteryMaker}</if>	 
			order by p.seq desc 
	      <if test="pgtl != null">
	        <include refid="commonPagingFooter" />
	      </if>
	      
	    </select>	    
	    
	<!-- 	    
		<select id="selectBySeq"  parameterType="int" resultMap="plantResult">
	       select * 
	       from t_plant p
	       where seq = #{seq} 		
	
			select 
				seq, name, plantType, plantStatus, beginDate, manageType, manageCompany,
				omStatus, omStartDay,  omEndDay, omPeriod, safetyManagerSeq, clientSeq, regionCode, address, memo, 
				supplyPower, battaryPower, pcsPower, pcsMadeby, battaryMadeby,
				ip, port, pcsStatus, pcsNum, startIdx, geox, geoy, createDateTime, updateDatetime,
        (select c.name from t_code c where c.groupType='battery_madeby' and c.id=p.battaryMadeby) as battaryMadebyName,
        (select c.name from t_code c where c.groupType='pcs_madeby' and c.id=p.pcsMadeby) as pcsMadebyName				
			from t_plant p where seq = #{seq}
 		
		</select>
	    
		<select id="selectDetailBySeq"  parameterType="int" resultType="com.elt.ems.vo.PlantDetailVo">
			select 
				p.*,
				m.ip, m.port 
				m.ip, m.port, m.status, m.startIdx, m.pmsMaker, m.pmsModel, m.pcsMaker, m.pcsVolume, m.pcsQuantity, m.batteryMaker, m.batteryModel, m.batteryVolume, m.batteryQuantity  
			from t_plant p left join t_pms m
			on p.seq = m.plantSeq
			where seq = #{seq} 				
		</select>		    
	-->		    						
		
	</mapper>