<?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="plantStatus != null and plantStatus != '' "> and p.plantStatus = #{plantStatus}</if>
			<if test="plantType != null and plantType != '' "> and p.plantType = #{plantType}</if>
			<if test="name != null and name != '' "> and p.name like concat('%', #{name}, '%')</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.*
			from t_plant p
			where 1=1 	 
			<if test="seq > 0"> and p.seq = #{seq}</if>			
			<if test="plantStatus != null and plantStatus != '' "> and p.plantStatus = #{plantStatus}</if>
			<if test="plantType != null and plantType != '' "> and p.plantType = #{plantType}</if>	
			<if test="name != null and name != '' "> and p.name like concat('%', #{name}, '%')</if> 		 
			order by p.seq desc
	      <if test="pgtl != null">
	        <include refid="commonPagingFooter" />
	      </if>
	      
	    </select>
	    
	    <select id="selectListKescoPlant"  parameterType="com.elt.ems.vo.KescoPlantVo" resultType="com.elt.ems.vo.KescoPlantVo">
	
	      <if test="pgtl != null">
	        <include refid="commonPagingHeader" /> 
	      </if>
	      select * from (
				select 
					p.seq as plantSeq, p.name, p.plantType, p.plantStatus, p.supplyPower, p.beginDate, p.omStartDate, p.omEndDate, p.createDatetime,
					k.customerId, k.aesKey, k.kescoStatus, k.protoVer,
				    (select count(d.seq) from t_kesco_timetable d where d.plantSeq = p.seq and d.inputDate = #{inputDate}) as count,
				    (select count(d.seq) from t_kesco_timetable d where d.plantSeq = p.seq and d.inputDate = #{inputDate} and d.sendStatus='01') as successCount,
					(select max(d.createDatetime) from t_kesco_timetable d where d.plantSeq = p.seq and d.inputDate = #{inputDate}) as pmsdataDatetime
				from t_plant p left join t_kesco k
				on p.seq = k.plantSeq
				where 1=1
				<if test="plantSeq > 0"> and p.seq = #{plantSeq}</if>
				<if test="plantStatus != null and plantStatus != '' "> and p.plantStatus = #{plantStatus}</if>
				<if test="kescoStatus != null and kescoStatus != '' "> and k.kescoStatus = #{kescoStatus}</if>
				<if test="name != null and name != '' "> and p.name like concat('%', #{name}, '%')</if>
				order by p.seq desc 
		 ) d
		 <if test="successCount != null and successCount > 0 "> where d.successCount &lt; #{successCount} </if>
			
	      <if test="pgtl != null">
	        <include refid="commonPagingFooter" />
	      </if>
	      
	    </select>
	    
	    <select id="selectListKescoTimetable"  parameterType="com.elt.ems.vo.KescoTimetableVo" resultType="com.elt.ems.vo.KescoTimetableVo">
	
			select
			*
			from t_kesco_timetable k
			where inputDate = #{inputDate} and plantSeq = #{plantSeq}
			order by inputHour desc
			
	    </select>	
	    
	    <select id="selectListKescoData"  parameterType="com.elt.ems.vo.KescoTimetableVo" resultType="com.elt.ems.vo.KescoTimetableVo">
			select 
			k.*,
			d.seq as pmsdataSeq, d.pcsIdx, d.inputDate, d.inputHour,
		    d.chargePower, d.dischargePower, d.pvPower,
			d.data1, d.data2, d.data3, d.data4, d.data5, d.data6, d.data7, d.data8, d.data9, d.data10,
			d.data11, d.data12, d.data13, d.data14, d.data15, d.data16, d.data17, d.data18, d.data19, d.data20,
			d.data21, d.data22, d.data23, d.data24, d.data25, d.data26, d.data27, d.data28, d.data29, d.data30,
			d.data31, d.data32, d.data33,
			d.data51, d.data52, d.data53, d.data54, d.data55, d.data56, d.data57, d.data58, d.data59, d.data60, 
			d.data61, d.data62, d.data63, d.data64, d.data65, d.data66, d.data67, d.data68, d.data69, d.data70
			from t_kesco_timetable k, t_pmsdata_statistic_hour d
			where k.plantSeq = d.plantSeq and k.pmsdataSeq = d.seq and d.pcsIdx=0
			and k.inputDate = #{inputDate} and d.inputDate=#{inputDate}
			and k.plantSeq = #{plantSeq}
			order by d.inputDate desc, d.inputHour desc
			
	    </select>	    
	    
	    
	    <select id="selectListPmsPlantCount"  parameterType="com.elt.ems.vo.PmsPlantVo" resultType="integer">
	
			select count(p.seq) as count 
			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="plantStatus != null and plantStatus != '' "> and p.plantStatus = #{plantStatus}</if>
			<if test="plantType != null and plantType != '' "> and p.plantType = #{plantType}</if>
			<if test="pmsStatus != null and pmsStatus != '' "> and m.pmsStatus = #{pmsStatus}</if>
			<if test="pcsMaker != null and pcsMaker != '' "> and m.pcsMaker = #{pcsMaker}</if>
			<if test="batteryMaker != null and batteryMaker != '' "> and m.batteryMaker = #{batteryMaker}</if> 	
			<if test="name != null and name != '' "> and p.name like concat('%', #{name}, '%')</if>    
	    </select>
	    	    
	    <select id="selectListPmsPlant"  parameterType="com.elt.ems.vo.PmsPlantVo" resultType="com.elt.ems.vo.PmsPlantVo">
	
			select 
				p.*,
				(select max(d.createDatetime) from t_pmsdata_${realtimeYear} d where d.plantSeq = p.seq and d.pcsIdx = 1 and d.inputDate = date_format(now(), '%Y-%m-%d')) as pmsdataDatetime,
				(select count(d.seq) from t_pmsdata_${realtimeYear} d where d.plantSeq = p.seq and d.pcsIdx = 1 and d.inputDate = date_format(now(), '%Y-%m-%d')) as count,
				m.plantSeq, m.ip, m.port, m.pmsStatus, m.startIdx, m.pmsMaker, m.pmsModel, m.pcsMaker, m.pcsVolume, m.pcsQuantity, m.batteryMaker, m.batteryModel, m.batteryVolume, m.batteryQuantity,
				(m.pcsVolume * m.pcsQuantity) as pcsPower, (m.batteryVolume * m.batteryQuantity) as batteryPower
			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="plantStatus != null and plantStatus != '' "> and p.plantStatus = #{plantStatus}</if>
			<if test="plantType != null and plantType != '' "> and p.plantType = #{plantType}</if>
			<if test="pmsStatus != null and pmsStatus != '' "> and m.pmsStatus = #{pmsStatus}</if>
			<if test="pcsMaker != null and pcsMaker != '' "> and m.pcsMaker = #{pcsMaker}</if>
			<if test="batteryMaker != null and batteryMaker != '' "> and m.batteryMaker = #{batteryMaker}</if>	
			<if test="name != null and name != '' "> and p.name like concat('%', #{name}, '%')</if> 
			order by p.seq desc 
	      
	    </select>
	    	    
<!-- 	    	    
	    <select id="selectDetailList"  parameterType="com.elt.ems.vo.PmsVo" resultType="com.elt.ems.vo.PmsVo">
	
	      <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.pmsStatus, m.startIdx, m.pmsMaker, m.pmsModel, m.pcsMaker, m.pcsVolume, m.pcsQuantity, m.batteryMaker, m.batteryModel, m.batteryVolume, m.batteryQuantity,
				(m.pcsVolume * m.pcsQuantity) as pcsPower, (m.batteryVolume * m.batteryQuantity) as batteryPower
			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="plantStatus != null and plantStatus != '' "> and p.plantStatus = #{plantStatus}</if>
			<if test="plantType != null and plantType != '' "> and p.plantType = #{plantType}</if>
			<if test="pmsStatus != null and pmsStatus != '' "> and m.pmsStatus = #{pmsStatus}</if>
			<if test="pcsMaker != null and pcsMaker != '' "> and m.pcsMaker = #{pcsMaker}</if>
			<if test="batteryMaker != null and batteryMaker != '' "> and m.batteryMaker = #{batteryMaker}</if>	
			<if test="name != null and name != '' "> and p.name like concat('%', #{name}, '%')</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>