<?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.PmsDataMapper">
	
		<!-- 해당 부분의 id는 MapperClass의 함수 이름과 유사하여야 합니다. -->
	    <sql id="commonPagingHeader"  >
	      SELECT R1.* FROM (
	    </sql>
	    
	    <sql id="commonPagingFooter"  >
	      ) R1 LIMIT #{pgtl.startNo}, #{pgtl.listPerPage}
	    </sql> 
	    <sql id="includePlantSeqs"  >
	      and p.seq in ( ${plantSeqs} )
	    </sql>
	    <sql id="tableName"> t_weather_2021 </sql>
	    	
	    <select id="selectListCount"  parameterType="com.elt.ems.vo.PmsDataVo" resultType="integer">
	
	       select count(d.seq) as count 
	       from t_pmsdata_${realtimeYear} d
	       where 1=1  
	       <if test="plantSeq > 0"> and d.plantSeq = #{plantSeq} </if>  
	       <if test="plantSeqs != null and plantSeqs != '' ">and d.plantSeq in ( ${plantSeqs} ) </if>
	       <if test="pcsIdx >= 0"> and d.pcsIdx = #{pcsIdx} </if>
	       <if test="inputDate != null"> and d.inputDate = #{inputDate} </if> 
			<if test="seq > 0">and d.seq = #{seq}</if>   
			<if test="timetableSeq > 0"> and d.timetableSeq = #{timetableSeq} </if>
			<if test="str1 != null"> and d.inputDate between #{str1} and #{str2} </if> 
	    </select>
	    		
	    <select id="selectList"  parameterType="com.elt.ems.vo.PmsDataVo" resultType="com.elt.ems.vo.PmsDataVo">
	      <if test="pgtl != null">
	        <include refid="commonPagingHeader" /> 
	      </if>
	      	    
			select d.*, m.pcsMaker, m.pcsQuantity, m.pcsQuantity * m.pcsVolume as supplyPcsPower, m.batteryMaker, m.batteryQuantity * m.batteryVolume as supplyBatteryPower, p.name as plantName, p.supplyPower, p.weatherCode
			<!-- 						  
			<if test="plantSeq > 0">
			, (select max(todayPvPower) from t_pmsdata_statistic_hour h where h.inputDate = #{inputDate} and h.plantSeq = d.plantSeq and h.pcsIdx = d.pcsIdx and h.plantSeq=#{plantSeq} and h.pcsIdx=#{pcsIdx} and h.inputHour &lt;= d.inputHour ) as todayPvPower
			</if> --> 
			from (	       
				select d.*, s.y1ChargePower, s.y1DischargePower, s.y1PvPower, s.y2ChargePower, s.y2DischargePower, s.y2PvPower				
			    from t_pmsdata_${realtimeYear} d left join (
					select 
						s1.plantSeq, s1.pcsIdx,
						s1.chargePower as y1ChargePower, s1.dischargePower as y1DischargePower, s1.pvPower as y1PvPower,
						s2.chargePower as y2ChargePower, s2.dischargePower as y2DischargePower, s2.pvPower as y2PvPower
					from t_pmsdata_statistic_day s1 , t_pmsdata_statistic_day s2
					where s1.plantSeq = s2.plantSeq and s1.pcsIdx = s2.pcsIdx  
					<if test="plantSeq > 0"> and s1.plantSeq = #{plantSeq} and s2.plantSeq = #{plantSeq}  </if> 
					<if test="plantSeqs != null and plantSeqs != '' ">and s1.plantSeq in ( ${plantSeqs} ) </if>
					<if test="pcsIdx >= 0"> and s1.pcsIdx = #{pcsIdx} and s2.pcsIdx = #{pcsIdx} </if> 
					<if test="str3 != null"> and s1.inputDate = #{str3} </if>
					<if test="str4 != null"> and s2.inputDate = #{str4} </if>	  
					) s
				on d.plantSeq = s.plantSeq and d.pcsIdx = s.pcsIdx 
			    where 1=1 
			    <if test="seq > 0"> and d.seq = #{seq}</if>
			    <if test="plantSeq > 0"> and d.plantSeq = #{plantSeq}</if>
			    <if test="plantSeqs != null and plantSeqs != '' ">and d.plantSeq in ( ${plantSeqs} ) </if>
			    <if test="pcsIdx >= 0"> and d.pcsIdx = #{pcsIdx} </if>
			    <if test="inputDate != null"> and d.inputDate = #{inputDate}  </if>
			    <if test="inputHour != null and inputHour != '' "> and d.inputHour = #{inputHour}  </if>
			    <if test="inputMinute != null and inputMinute != '' "> and d.inputMinute = #{inputMinute}  </if>
				<if test="timetableSeq > 0"> and d.timetableSeq = #{timetableSeq} </if>
				<if test="str1 != null and str1 != '' "> and d.inputDate  between #{str1} and #{str2} </if> 
			) d, t_pms m, t_plant p
			where d.plantSeq = p.seq and d.plantSeq = m.plantSeq	
			<if test="orderby == 'soc' "> order by d.data15 desc, p.seq desc</if>	
			<if test="orderby == 'soh' "> order by d.data16 desc, p.seq desc</if>
			<if test="orderby == 'beginDate' "> order by p.beginDate desc, p.seq desc</if>
			<if test="orderby == 'pvPower' "> order by d.pvPower desc, p.seq desc</if>	
			<if test="orderby == 'inputDate' ">order by d.plantSeq asc, d.pcsIdx asc, d.inputDate desc, d.inputHour desc, d.inputMinute desc</if>
			<if test="orderby == 'asc' ">order by d.plantSeq asc, d.pcsIdx asc, d.inputDate asc, d.inputHour asc, d.inputMinute asc</if>
			<if test="orderby == null || orderby == '' || orderby == 'desc' ">order by d.plantSeq desc, d.pcsIdx asc, d.inputDate asc, d.inputHour asc, d.inputMinute asc</if> 
	       
	      <if test="pgtl != null">
	        <include refid="commonPagingFooter" />
	      </if>
	      	  
	    </select>
	    
	    <select id="selectByPlantSeq"  parameterType="com.elt.ems.vo.PmsDataVo" resultType="com.elt.ems.vo.PmsDataVo">
	      <if test="pgtl != null">
	        <include refid="commonPagingHeader" /> 
	      </if>
	      	    
			select d.*, m.pcsQuantity, m.pcsMaker, m.batteryMaker, p.name as plantName   			
			from (	       
				select d.*, s.y1ChargePower, s.y1DischargePower, s.y1PvPower, s.y2ChargePower, s.y2DischargePower, s.y2PvPower				
			    from t_pmsdata_${realtimeYear} d left join (
					select 
						s1.plantSeq, s1.pcsIdx,
						s1.chargePower as y1ChargePower, s1.dischargePower as y1DischargePower, s1.pvPower as y1PvPower,
						s2.chargePower as y2ChargePower, s2.dischargePower as y2DischargePower, s2.pvPower as y2PvPower
					from t_pmsdata_statistic_day s1 , t_pmsdata_statistic_day s2
					where s1.plantSeq = s2.plantSeq and s1.pcsIdx = s2.pcsIdx  
					and s1.plantSeq = #{plantSeq} 
					<if test="pcsIdx >= 0"> and s1.pcsIdx = #{pcsIdx} </if> 
					<if test="str3 != null"> and s1.inputDate = #{str3} </if>
					<if test="plantSeq > 0"> and s2.plantSeq = #{plantSeq} </if> 
					<if test="pcsIdx >= 0"> and s2.pcsIdx = #{pcsIdx} </if>
					<if test="str4 != null"> and s2.inputDate = #{str4} </if>	  
					) s
				on d.plantSeq = s.plantSeq and d.pcsIdx = s.pcsIdx 
			    where 1=1 
			    <if test="seq > 0"> and d.seq = #{seq}</if>
			    and d.plantSeq = #{plantSeq}
			    <if test="pcsIdx >= 0"> and d.pcsIdx = #{pcsIdx} </if>
			    <if test="inputDate != null"> and d.inputDate = #{inputDate}  </if>
			    <!--   if test="inputHour != null"> and d.inputHour = #{inputHour}  </if -->
			    <if test="inputMinute != null"> and d.inputMinute = #{inputMinute}  </if>
				<if test="timetableSeq > 0"> and d.timetableSeq = #{timetableSeq} </if>
				<if test="str1 != null"> and d.inputDate  between #{str1} and #{str2} </if> 
			) d, t_pms m, t_plant p
			where d.plantSeq = p.seq and d.plantSeq = m.plantSeq	
			order by d.seq desc
	       
	      <if test="pgtl != null">
	        <include refid="commonPagingFooter" />
	      </if>
	      	  
	    </select>
	    	    
	    	  
	    	    	    
	    <select id="selectListFaultCount"  parameterType="com.elt.ems.vo.PmsDataVo" resultType="integer">
	      	       
			select count(d.seq) as count
			from t_pmsdata_${realtimeYear} d, t_plant p, t_pms m
			where  p.seq = d.plantSeq and p.seq = m.plantSeq and d.pcsIdx &gt; 0
			<if test="data25 == -1"> and (d.data25 &gt; 0 or d.data62 &gt; 0 or d.data63 &gt; 0 or d.data64 &gt; 0 or d.data28 &gt; 0 or d.data29 &gt; 0 or d.data69 &gt; 0 or d.data70 &gt; 0 ) </if>			
			<if test="data26 == -1"> and (d.data26 &gt; 0 or d.data66 &gt; 0 or d.data67 &gt; 0 or d.data68 &gt; 0 ) </if>
			<if test="data51 == -1 or data56 == -1"> and d.data52 &lt;&gt; 0 and d.data53 &lt;&gt; 0 and d.data57 &lt;&gt; 0 and d.data58 &lt;&gt; 0
							and ( d.data52 &lt; #{data61} or d.data52 &gt; #{data62} or d.data53 &lt; #{data61} or d.data53 &gt; #{data62} or 
								  (d.data54 &lt;&gt; 0 and d.data54 &lt; #{data61} ) or d.data54 &gt; #{data62} or (d.data55 &lt;&gt; 0 and d.data55 &lt; #{data61}) or d.data55 &gt; #{data62} or
							 	  d.data57 &lt; #{data63} or d.data57 &gt; #{data64} or d.data58 &lt; #{data63} or d.data58 &gt; #{data64} ) 
			</if>
			<if test="data20 > 0 and data21 > 0">
							and ( d.data20 &lt; #{data21} or d.data20 &gt; #{data20} or d.data21 &lt; #{data21} or d.data21 &gt; #{data20}) 
			</if>
			<if test="plantSeq > 0"> and d.plantSeq = #{plantSeq} </if>
			<if test="pcsIdx > 0"> and d.pcsIdx = #{pcsIdx} </if>
			<if test="inputDate != null"> and d.inputDate = #{inputDate} </if> 
			<if test="seq > 0">and d.seq = #{seq}</if>
			<if test="str1 != null"> and d.inputDate between #{str1} and #{str2} </if> 
			<if test="plantStatus != null and plantStatus != '-1' "> and p.plantStatus = #{plantStatus}</if>
			<!-- 
			<if test="pmsStatus != null and pmsStatus != '-1' "> and m.pmsStatus = #{pmsStatus}</if> -->
	      	  
	    </select>
	    
	    	    
	    <select id="selectListFault"  parameterType="com.elt.ems.vo.PmsDataVo" resultType="com.elt.ems.vo.PmsDataVo">
	       
	      <if test="pgtl != null">
	        <include refid="commonPagingHeader" /> 
	      </if>
	      	       
			select p.name as plantName, d.*, 
			m.pcsMaker as 'plant.pcsMaker', m.batteryMaker as 'plant.batteryMaker', m.msgGroupPcs as 'plant.msgGroupPcs', m.msgGroupBattery as 'plant.msgGroupBattery', m.pcsQuantity as 'plant.pcsQuantity', m.batteryQuantity as 'plant.batteryQuantity'
			from t_pmsdata_${realtimeYear} d, t_plant p, t_pms m
			where  p.seq = d.plantSeq and p.seq = m.plantSeq and d.pcsIdx &gt; 0
			<if test="data25 == -1"> and (d.data25 &gt; 0 or d.data62 &gt; 0 or d.data63 &gt; 0 or d.data64 &gt; 0 or d.data28 &gt; 0 or d.data29 &gt; 0 or d.data69 &gt; 0 or d.data70 &gt; 0 ) </if>	
			<if test="data26 == -1"> and (d.data26 &gt; 0 or d.data66 &gt; 0 or d.data67 &gt; 0 or d.data68 &gt; 0 ) </if>		
			<if test="data51 == -1 or data56 == -1"> and d.data52 &lt;&gt; 0 and d.data53 &lt;&gt; 0 and d.data57 &lt;&gt; 0 and d.data58 &lt;&gt; 0
							and ( d.data52 &lt; #{data61} or d.data52 &gt; #{data62} or d.data53 &lt; #{data61} or d.data53 &gt; #{data62} or 
								  (d.data54 &lt;&gt; 0 and d.data54 &lt; #{data61} ) or d.data54 &gt; #{data62} or (d.data55 &lt;&gt; 0 and d.data55 &lt; #{data61}) or d.data55 &gt; #{data62} or
							 	  d.data57 &lt; #{data63} or d.data57 &gt; #{data64} or d.data58 &lt; #{data63} or d.data58 &gt; #{data64} ) 
			</if>
			<if test="data20 > 0 and data21 > 0">
							and ( d.data20 &lt; #{data21} or d.data20 &gt; #{data20} or d.data21 &lt; #{data21} or d.data21 &gt; #{data20}) 
			</if>
			<if test="plantSeq > 0"> and d.plantSeq = #{plantSeq} </if>
			<if test="pcsIdx > 0"> and d.pcsIdx = #{pcsIdx} </if>
			<if test="inputDate != null"> and d.inputDate = #{inputDate} </if> 
			<if test="seq > 0">and d.seq = #{seq}</if>
			<if test="str1 != null"> and d.inputDate between #{str1} and #{str2} </if> 
			<if test="plantStatus != null and plantStatus != '-1' "> and p.plantStatus = #{plantStatus}</if>
			<!-- 
			<if test="pmsStatus != null and pmsStatus != '-1' "> and m.pmsStatus = #{pmsStatus}</if>  -->
			order by d.inputDate desc, d.inputHour desc, d.inputMinute desc, d.plantSeq asc
	  
	      <if test="pgtl != null">
	        <include refid="commonPagingFooter" />
	      </if>
	      	  
	    </select>
	    	    
	    
	    <select id="selectPlantPmsDataList"  parameterType="com.elt.ems.vo.PmsDataVo" resultType="com.elt.ems.vo.PmsDataVo">
	
			select 
				p.seq as plantSeq, p.plantName, p.plantStatus, p.supplyPower, p.weatherCode, p.pmsStatus, p.pcsMaker, p.pcsQuantity, 
			    d.seq, d.pcsIdx, d.timetableSeq, d.inputDate, d.inputHour, d.inputMinute,
			    d.chargePower, d.dischargePower, d.pvPower, d.dayPvPower, (p.pcsVolume * p.pcsQuantity) as supplyPcsPower, (p.batteryVolume * p.batteryQuantity) as supplyBatteryPower,
				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.data34, d.data35, d.data36,
				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,
			    d.createDatetime, d.count
			from (
				select p.seq, p.name as plantName, p.plantStatus, p.supplyPower, p.weatherCode, pm.pmsStatus, pm.pcsMaker, pm.pcsQuantity, pm.pcsVolume, pm.batteryQuantity, pm.batteryVolume 
				from t_plant p, t_pms pm
				where p.seq = pm.plantSeq
				<if test="plantStatus != null and plantStatus != '-1' "> and p.plantStatus = #{plantStatus}</if>
				<if test="pmsStatus != null and pmsStatus != '-1' "> and pm.pmsStatus = #{pmsStatus}</if>		
				<if test="plantName != null and plantName != '' "> and p.name like concat('%', #{plantName}, '%')</if>	
				<if test="plantSeq > 0 "> and p.seq = #{plantSeq}</if>	
				<if test="plantSeqs != null and plantSeqs != '' "><include refid="includePlantSeqs" /></if>
			) p left join (
			
				select  
					d.*, d2.count
				from t_pmsdata_${realtimeYear} d, 
				(
					select plantSeq, max(seq) as seq, max(timetableSeq) as timetableSeq, count(seq) as count
					from t_pmsdata_${realtimeYear} 
					where inputDate = #{inputDate}
					<if test="plantSeq > 0 "> and plantSeq = #{plantSeq}</if>
					<if test="pcsIdx >= 0 ">and pcsIdx=#{pcsIdx} </if>
					<if test="pcsIdx == -1 ">and pcsIdx &gt; 0 </if> 
					group by plantSeq, pcsIdx
				) d2
				where d.plantSeq = d2.plantSeq	and d.timetableSeq = d2.timetableSeq and d.seq = d2.seq and d.inputDate = #{inputDate}
				<if test="plantSeq > 0 ">and d.plantSeq = #{plantSeq} </if> 
				<if test="pcsIdx >= 0 ">and d.pcsIdx=#{pcsIdx} </if>
				<if test="pcsIdx == -1 ">and d.pcsIdx &gt; 0 </if>
				group by d.plantSeq, d.pcsIdx							
			) d
			on p.seq = d.plantSeq
			order by p.seq desc, d.pcsIdx asc
	    </select>
	    
	    
	    <select id="selectList4Weather"  parameterType="com.elt.ems.vo.PmsDataVo" resultType="com.elt.ems.vo.PmsDataReportVo">
	
			select p.*, w.weatherHour, w.cloud from ( 
			    select d.*, m.pcsMaker, m.pcsQuantity, m.pcsQuantity * m.pcsVolume as supplyPcsPower, m.batteryMaker, m.batteryQuantity * m.batteryVolume as supplyBatteryPower, p.name as plantName, p.supplyPower, p.weatherCode
				from (	       
					select d.* 
			        from t_pmsdata_${realtimeYear} d 
					where d.plantSeq = #{plantSeq} and d.inputDate = #{inputDate} and d.pcsIdx = 0					
				) d, t_pms m, t_plant p
				where d.plantSeq = p.seq and d.plantSeq = m.plantSeq	
				) p left join ( 
					select inputHour as weatherHour, weatherCode, cloud from <include refid="tableName" /> where inputYmd = #{inputDate} 
				) w on p.weatherCode = w.weatherCode and p.inputHour = w.weatherHour
			order by p.seq asc 
						
	    </select>
	    	    
	    <select id="selectList4Expect"  parameterType="com.elt.ems.vo.PmsDataVo" resultType="com.elt.ems.common.CaseSensibleHashMap">
	
			select * from (
				select *, d.pvPower/p.supplyPower as pvPercent from (
					select p.seq as pSeq, p.name as plantName, p.plantStatus, p.supplyPower, p.geox, p.geoy, p.weatherCode, p.prate, p.drate, p.beginDate, pm.pmsStatus, pm.pcsMaker, pm.pcsQuantity, pm.pcsVolume, pm.batteryQuantity, pm.batteryVolume 
					from t_plant p, t_pms pm
					where p.seq = pm.plantSeq
					and p.plantStatus='01' and pm.pmsStatus='01'
				) p left join (
			
					select  d.*, d2.count
					from t_pmsdata_${realtimeYear} d, (
						
						select plantSeq, pcsIdx, max(seq) as seq, max(timetableSeq) as timetableSeq, count(seq) as count
						from t_pmsdata_${realtimeYear}
						where inputDate = #{inputDate} and pcsIdx=0 
						<if test="inputMinute == null or inputMinute == ''"> 
						    and inputHour=#{inputHour} 
						</if>
						<if test="inputMinute != null and inputMinute != ''">
							and inputHour=#{inputHour} and inputMinute = #{inputMinute}
						</if>						
						group by plantSeq, pcsIdx
						
					) d2
					where d.plantSeq = d2.plantSeq	and d.timetableSeq = d2.timetableSeq and d.seq = d2.seq and d.inputDate = #{inputDate} and d.pcsIdx = d2.pcsIdx 
					group by d.plantSeq, d.pcsIdx
				) d
				on p.pSeq = d.plantSeq
			) p left join (
				select inputHour as weatherHour, weatherCode, cloud from <include refid="tableName" /> where inputYmd = #{inputDate} and inputHour = ( select max(inputHour) from <include refid="tableName" /> where inputYmd = #{inputDate})
			) w
			on p.weatherCode = w.weatherCode
			<if test="orderby == 'plantSeq' ">order by p.pSeq desc</if>
			<if test="orderby == 'data15'">order by p.data15 desc</if>
			<if test="orderby == 'data1'">order by p.data1 desc</if>
			<if test="orderby == null or orderby == 'pvPower' ">order by p.pvPercent desc</if>	
						
	    </select>
	    	    
	    <select id="selectPmsDataReport"  parameterType="com.elt.ems.vo.PmsDataReportVo" resultType="com.elt.ems.vo.PmsDataReportVo">
			select * from ( 
				select 
					p.seq as plantSeq, p.plantName, p.plantStatus, p.supplyPower, p.geox, p.geoy, p.weatherCode, p.prate, p.drate, p.beginDate, p.pmsStatus, p.pcsMaker, p.pcsQuantity, 
				    d.seq, d.pcsIdx, d.timetableSeq, d.inputDate, d.inputHour, d.inputMinute,
				    d.chargePower, d.dischargePower, d.pvPower, d.pvPower/p.supplyPower as pvPercent, 
				    (p.pcsVolume * p.pcsQuantity) as supplyPcsPower, (p.batteryVolume * p.batteryQuantity) as supplyBatteryPower,
					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,
				    d.createDatetime, d.count
				from (
					select p.seq, p.name as plantName, p.plantStatus, p.supplyPower, p.geox, p.geoy, p.weatherCode,  p.prate, p.drate, p.beginDate, pm.pmsStatus, pm.pcsMaker, pm.pcsQuantity, pm.pcsVolume, pm.batteryQuantity, pm.batteryVolume 
					from t_plant p, t_pms pm
					where p.seq = pm.plantSeq
					<if test="plantStatus != null and plantStatus != '-1' "> and p.plantStatus = #{plantStatus}</if>
					<if test="pmsStatus != null and pmsStatus != '-1' "> and pm.pmsStatus = #{pmsStatus}</if>		
					<if test="plantName != null and plantName != '' "> and p.name like concat('%', #{plantName}, '%')</if>	
					<if test="plantSeq > 0 "> and p.seq = #{plantSeq}</if>	
				) p left join (
				
				<if test="timetableSeq > 0 ">
					select  
						d.*, d2.count
					from t_pmsdata_${realtimeYear} d, 
					(
						select plantSeq, max(timetableSeq) as timetableSeq, count(seq) as count
						from t_pmsdata_${realtimeYear} 
						where pcsIdx=0 	and inputDate = #{inputDate}
						<if test="plantSeq > 0 "> and plantSeq = #{plantSeq}</if> 
						group by plantSeq
					) d2
					where d.plantSeq = d2.plantSeq
					and d.timetableSeq = ${timetableSeq}			
				</if>
				<if test="timetableSeq == 0 ">
					select  
						d.*, d2.count
					from t_pmsdata_${realtimeYear} d, 
					(
						<!-- select plantSeq, max(timetableSeq) as timetableSeq, count(seq) as count  -->
						select plantSeq, max(seq) as seq, count(seq) as count  
						from t_pmsdata_${realtimeYear} 
						where pcsIdx=0 	and inputDate = #{inputDate}
						<if test="plantSeq > 0 "> and plantSeq = #{plantSeq}</if> 
						group by plantSeq
					) d2
					where d.plantSeq = d2.plantSeq
					<!--  and d.timetableSeq = d2.timetableSeq   -->
					and d.seq = d2.seq
				</if>
					and d.inputDate = #{inputDate}
					<if test="plantSeq > 0 ">and d.plantSeq = #{plantSeq} </if> 
					<if test="pcsIdx >= 0 ">and d.pcsIdx=#{pcsIdx} </if>
					<if test="pcsIdx == -1 ">and d.pcsIdx &gt; 0 </if>								
				) d
				on p.seq = d.plantSeq					
			) p left join ( 
				select inputHour as weatherHour, weatherCode, cloud from <include refid="tableName" /> where inputYmd = #{inputDate} and inputHour = ( select max(inputHour) from <include refid="tableName" /> where inputYmd = #{inputDate}) 
			) w on p.weatherCode = w.weatherCode 
		
			<if test="orderby == 'plantSeq' ">order by p.plantSeq desc</if>
			<if test="orderby == 'data15'">order by p.data15 desc</if>
			<if test="orderby != 'plantSeq' and orderby != 'data15' ">order by p.pvPercent desc</if>							
						
	    </select>
	    	    
	    
	    <select id="selectListCompare"  parameterType="com.elt.ems.vo.PmsDataVo" resultType="com.elt.ems.vo.PmsDataVo">
	      	       
			select d.*, m.pcsQuantity, m.pcsVolume, m.pcsMaker, (p.pcsVolume * p.pcsQuantity) as supplyPcsPower, m.batteryMaker, p.name as plantName
			from (	       
				select d.*
			    from t_pmsdata_${realtimeYear} d 
			    <if test="plantSeq > 0"> and d.plantSeq = #{plantSeq}</if>
			    <if test="pcsIdx >= 0"> and d.pcsIdx = #{pcsIdx} </if>
			    <if test="inputDate != null"> and d.inputDate = #{inputDate}  </if>
				<if test="timetableSeq > 0"> and d.timetableSeq = #{timetableSeq} </if>
			) d, t_pms m, t_plant p
			where d.plantSeq = p.seq and d.plantSeq = m.plantSeq		
			<if test="orderby != null and orderby == 'asc' ">order by d.plantSeq asc, d.pcsIdx asc, d.inputDate asc, d.inputHour asc, d.inputMinute asc</if>
			<if test="orderby == null || orderby != 'asc'">order by d.plantSeq asc, d.pcsIdx asc, d.inputDate desc, d.inputHour desc, d.inputMinute desc</if> 
	  
	    </select>
	    	    
	    	   
	    <select id="getWarningBscList"  parameterType="com.elt.ems.vo.PmsDataVo" resultType="com.elt.ems.vo.PmsDataVo">
			select 
				p.name as plantName, d.* 
			from t_pmsdata_${realtimeYear} d, t_plant p
			where d.plantSeq = p.seq and d.inputDate=#{inputDate} and d.pcsIdx > 0 and ( d.data26  &gt; 0 or d.data66  &gt; 0 or d.data67  &gt; 0 or d.data68  &gt; 0)  
				and d.timetableSeq = (select currSeq from t_sequence where seq=10)   
			order by p.seq asc, d.pcsIdx asc	               
	    </select>
	    	    	    
	    <select id="getFaultBscList"  parameterType="com.elt.ems.vo.PmsDataVo" resultType="com.elt.ems.vo.PmsDataVo">
	    	select * from (
				select 
					p.name as plantName, '1' as str1, 'warning' as str2, d.* 
				from t_pmsdata_${realtimeYear} d, t_plant p
				where d.plantSeq = p.seq and d.inputDate=#{inputDate} and d.pcsIdx > 0 and ( d.data26  &gt; 0 or d.data66  &gt; 0 or d.data67  &gt; 0 or d.data68  &gt; 0)  
					and d.timetableSeq = (select currSeq from t_sequence where seq=10)   
				union all	    
				select 
					p.name as plantName, '2' as str1, 'danger' as str2, d.* 
				from t_pmsdata_${realtimeYear} d, t_plant p
				where d.plantSeq = p.seq and d.inputDate=#{inputDate} and d.pcsIdx > 0 and ( d.data25  &gt; 0 or d.data62  &gt; 0 or d.data63  &gt; 0 or d.data64  &gt; 0)  
					and d.timetableSeq = (select currSeq from t_sequence where seq=10)   
			
			) d
			order by d.str1 asc, d.plantSeq asc, d.pcsIdx asc	               
	    </select>
	    
	    <select id="getFaultPcsList"  parameterType="com.elt.ems.vo.PmsDataVo" resultType="com.elt.ems.vo.PmsDataVo">
			select 
				p.name as plantName, d.* 
			from t_pmsdata_${realtimeYear} d, t_plant p
			where d.plantSeq = p.seq and d.inputDate=#{inputDate} and d.pcsIdx > 0 and ( d.data28 &gt; 0 or d.data29 &gt; 0 or d.data69 &gt; 0 or d.data70 &gt; 0) 
				and d.timetableSeq = (select currSeq from t_sequence where seq=10)  
			order by p.seq asc, d.pcsIdx asc	 
	    </select> 	    	    
	    
	    <select id="selectListTimetable"  parameterType="com.elt.ems.vo.TimetablePmsVo" resultType="com.elt.ems.vo.TimetablePmsVo">
	
	      <if test="pgtl != null">
	        <include refid="commonPagingHeader" /> 
	      </if>
	
			select *
			from t_timetable_pms k
			where k.inputDate = #{inputDate} 
			order by k.inputDate desc, k.inputHour desc, k.inputMinute desc	
			
	      <if test="pgtl != null">
	        <include refid="commonPagingFooter" />
	      </if>
	      
	    </select>   
	   
	    <select id="selectMaxTimetable"  parameterType="com.elt.ems.vo.TimetablePmsVo" resultType="com.elt.ems.vo.TimetablePmsVo">
	
			select *
			from t_timetable_pms k
			where seq =  (select max(seq) from t_timetable_pms k where k.inputDate = #{inputDate} )
	      
	    </select> 
	    	   
	   
	<select id="listNotificationAlarmCount"  parameterType="com.elt.ems.vo.PmsDataVo" resultType="integer">
		select count(d.seq) as count
		from t_pmsdata_${realtimeYear} d, t_plant p, t_pms m
		where d.plantSeq = p.seq  and p.seq = m.plantSeq
		and d.inputDate=#{inputDate}  and d.data51 &lt;&gt; 0 and 
				(d.data51 &lt; #{data61} or d.data51 &gt; #{data62}  or  d.data52 &lt; #{data61} or d.data52 &gt; #{data62} or
       	         d.data56 &lt; #{data63} or d.data56 &gt; #{data64}  or  d.data57 &lt; #{data63} or d.data57 &gt; #{data64})
      <if test="plantSeq > 0">
        and d.plantSeq = #{plantSeq}
      </if>              
        <if test="inputHour != null and inputHour != '' ">
       and d.inputHour = #{inputHour}
        </if>
            
    </select>   
    	    
	<select id="listNotificationAlarm"  parameterType="com.elt.ems.vo.PmsDataVo" resultType="com.elt.ems.vo.PmsDataVo">
	
      <if test="pgtl != null">
        <include refid="commonPagingHeader" /> 
      </if>
      	
		select d.*, p.name as plantName, m.pcsMaker, m.pcsVolume, m.pcsQuantity, m.batteryMaker, m.batteryModel, m.batteryVolume, m.batteryQuantity
		from t_pmsdata_${realtimeYear} d, t_plant p, t_pms m
		where d.plantSeq = p.seq  and p.seq = m.plantSeq
		and d.inputDate=#{inputDate}  and d.data51 &lt;&gt; 0 and 
				(d.data51 &lt; #{data61} or d.data51 &gt; #{data62}  or  d.data52 &lt; #{data61} or d.data52 &gt; #{data62} or
       	         d.data56 &lt; #{data63} or d.data56 &gt; #{data64}  or  d.data57 &lt; #{data63} or d.data57 &gt; #{data64})
       	     
      <if test="plantSeq > 0">
        and d.plantSeq = #{plantSeq}
      </if>              
        <if test="inputHour != null and inputHour != '' ">
       and d.inputHour = #{inputHour}
        </if>
      order by d.inputHour desc, d.plantSeq desc
      
      <if test="pgtl != null">
        <include refid="commonPagingFooter" /> 
      </if>
            
    </select>  
    
    
	    <select id="listInspectDayByTime"  parameterType="com.elt.ems.vo.PmsStatisticDataVo" resultType="com.elt.ems.common.CaseSensibleHashMap">
	
			select p.seq as plantSeq, p.name, m.pcsQuantity, s.*     
			from t_plant p, t_pms m  left join (
			select  s.plantSeq as seq, s.pcsIdx, s.inputYear, s.inputMonth, s.inputDay, 
				max(input01) as input01, max(input02) as input02, max(input03) as input03, max(input04) as input04, max(input05) as input05, 
				max(input06) as input06, max(input07) as input07, max(input08) as input08, max(input09) as input09, max(input10) as input10,  
				max(input11) as input11, max(input12) as input12, max(input13) as input13, max(input14) as input14, max(input15) as input15, 
				max(input16) as input16, max(input17) as input17, max(input18) as input18, max(input19) as input19, max(input20) as input20,  
				max(input21) as input21, max(input22) as input22, max(input23) as input23, max(input24) as input24, max(input25) as input25, 
				max(input26) as input26, max(input27) as input27, max(input28) as input28, max(input29) as input29, max(input30) as input30,  
				max(input31) as input31,
			    sum(total) as total
			from (
				select s.plantSeq, s.pcsIdx, s.inputYear, s.inputMonth, s.inputDay, s.seq,
				(CASE WHEN inputDay = '01' THEN cnt END) as input01,
				(CASE WHEN inputDay = '02' THEN cnt END) as input02,
				(CASE WHEN inputDay = '03' THEN cnt END) as input03,
				(CASE WHEN inputDay = '04' THEN cnt END) as input04,
				(CASE WHEN inputDay = '05' THEN cnt END) as input05,
				(CASE WHEN inputDay = '06' THEN cnt END) as input06,
				(CASE WHEN inputDay = '07' THEN cnt END) as input07,
				(CASE WHEN inputDay = '08' THEN cnt END) as input08,
				(CASE WHEN inputDay = '09' THEN cnt END) as input09,
				(CASE WHEN inputDay = '10' THEN cnt END) as input10,
				(CASE WHEN inputDay = '11' THEN cnt END) as input11,
				(CASE WHEN inputDay = '12' THEN cnt END) as input12,
				(CASE WHEN inputDay = '13' THEN cnt END) as input13,
				(CASE WHEN inputDay = '14' THEN cnt END) as input14,
				(CASE WHEN inputDay = '15' THEN cnt END) as input15,
				(CASE WHEN inputDay = '16' THEN cnt END) as input16,
				(CASE WHEN inputDay = '17' THEN cnt END) as input17,
				(CASE WHEN inputDay = '18' THEN cnt END) as input18,
				(CASE WHEN inputDay = '19' THEN cnt END) as input19,
				(CASE WHEN inputDay = '20' THEN cnt END) as input20,
				(CASE WHEN inputDay = '21' THEN cnt END) as input21,
				(CASE WHEN inputDay = '22' THEN cnt END) as input22,
				(CASE WHEN inputDay = '23' THEN cnt END) as input23,
				(CASE WHEN inputDay = '24' THEN cnt END) as input24,
				(CASE WHEN inputDay = '25' THEN cnt END) as input25,
				(CASE WHEN inputDay = '26' THEN cnt END) as input26,
				(CASE WHEN inputDay = '27' THEN cnt END) as input27,
				(CASE WHEN inputDay = '28' THEN cnt END) as input28,
				(CASE WHEN inputDay = '29' THEN cnt END) as input29,
				(CASE WHEN inputDay = '30' THEN cnt END) as input30,
				(CASE WHEN inputDay = '31' THEN cnt END) as input31,
				sum(s.cnt) total
				from (
					select s.plantSeq, s.pcsIdx, s.inputDate,  substr(s.inputDate, 1, 4) as inputYear, substr(s.inputDate, 6, 2) as inputMonth, substr(s.inputDate, 9) as inputDay, s.seq, count(s.seq) as cnt
					from t_pmsdata_${realtimeYear} s
					where inputDate like concat(#{inputYear}, '-', #{inputMonth}, '-__') and pcsIdx=#{pcsIdx}
					group by s.plantSeq, s.inputDate
					order by plantSeq asc
				) s
				group by s.plantSeq, s.inputDate 
			) s 
			group by s.plantSeq, s.pcsIdx
			) s
			on m.plantSeq = s.seq
			where m.pmsStatus='01' and p.plantStatus='01' and p.seq = m.plantSeq
			<if test="plantSeq > 0 "> and m.plantSeq = #{plantSeq} </if>
			order by m.plantSeq desc, s.pcsIdx asc, s.inputDay asc
									
	    </select>	
	    	    
	    	        
    
	<insert id="insert"  parameterType="com.elt.ems.vo.PmsDataVo">
		insert into t_pmsdata_${realtimeYear} (
			plantSeq, pcsIdx, timetableSeq, inputDate, inputHour, inputMinute, chargePower, dischargePower, pvPower, 
			data1, data2, data3, data4, data5, data6, data7, data8, data9, data10, 
			data11, data12, data13, data14, data15, data16, data17, data18, data19, data20,
			data21, data22, data23, data24, data25, data26, data27, data28, data29, data30,
			data31, data32, data33, 
			data51, data52, data53, data54, data55, data56, data57, data58, data59, data60,
			data61, data62, data63, data64, data65, data66, data67, data68, data69, data70,
			createDateTime
		) values (
			#{plantSeq}, #{pcsIdx}, #{timetableSeq}, #{inputDate}, #{inputHour}, #{inputMinute}, #{chargePower}, #{dischargePower}, #{pvPower},
			#{data1}, #{data2}, #{data3}, #{data4}, #{data5}, #{data6}, #{data7}, #{data8}, #{data9}, #{data10},  
			#{data11}, #{data12}, #{data13}, #{data14}, #{data15}, #{data16}, #{data17}, #{data18}, #{data19}, #{data20},  
			#{data21}, #{data22}, #{data23}, #{data24}, #{data25}, #{data26}, #{data27}, #{data28}, #{data29}, #{data30},  
			#{data31}, #{data32}, #{data33},   
			#{data51}, #{data52}, #{data53}, #{data54}, #{data55}, #{data56}, #{data57}, #{data58}, #{data59}, #{data60},  
			#{data61}, #{data62}, #{data63}, #{data64}, #{data65}, #{data66}, #{data67}, #{data68}, #{data69}, #{data70},  
			now()
		)
	</insert>	
	
	<update id="updateTimetableSeq"  parameterType="integer">
		update t_sequence set 
			currSeq = currSeq+1
		where seq = #{seq}	
	</update>	
	
			    	    
    <select id="getTimetableSeq"  resultType="integer" parameterType="integer">
		select currSeq			
		from t_sequence
		where seq = #{seq}	               
    </select>
    
    
		
	<!-- 
	<update id="updatePushTimetableSeq"  parameterType="integer">
		update t_sequence set 
			currSeq = currSeq+1
		where seq = 10
	</update>	
			
    <select id="getPushTimetableSeq"  resultType="integer">
		select currSeq			
		from t_sequence
		where seq = 30	               
    </select>
     -->	
     
	<select id="selectListTransPower"  parameterType="com.elt.ems.vo.PmsDataVo" resultType="com.elt.ems.common.CaseSensibleHashMap">
		select * from (
			select p.seq as plantSeq, p.name as plantName,  p1.seq, p1.inputDate, p1.inputHour, p1.inputMinute, p1.timetableSeq, 
		    p.supplyPower,  p1.pvPower, round(p1.pvPower/100, 2) as scpv, round(p1.data1/10, 2) as scpw,
			(round(p1.pvPower/100, 2) + round(p1.data1/10, 2)) as transPw,
		    round((round(p1.pvPower/100, 2) + round(p1.data1/10, 2))*100/p.supplyPower, 2) as transPercent
			from t_pmsdata_${realtimeYear} p1, t_plant p
			where p1.inputDate like concat(#{inputDate} , '%')
			and p.seq = p1.plantSeq and p1.pcsIdx = 0 
			and (p1.data1/10) &lt; p.supplyPower 
			and p1.inputHour &gt; #{inputHour}
		    and p.plantStatus='01'
		) p
		where p.transPercent > #{count}
		order by p.plantSeq asc, p.inputDate desc, p.inputHour desc, p.inputMinute desc
    </select>       		
		
	</mapper>