<?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> 
	    
	    	
	    <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="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 &gt;= #{str1} and d.inputDate &lt;= #{str2} </if> 
	    </select>
	    		
	    <select id="selectList"  parameterType="com.elt.ems.vo.PmsDataVo" resultType="com.elt.ems.vo.PmsDataVo">
	    <!-- 
	       select d.*, s1.chargePower as y1ChargePower, s1.dischargePower as y1DischargePower
	       from t_pmsdata_${realtimeYear} d, t_pmsdata_statistic_day s1
	       where d.plantSeq = s1.plantSeq and d.pcsIdx = s1.pcsIdx
	       <if test="plantSeq > 0"> and d.plantSeq = #{plantSeq} and s1.plantSeq = #{plantSeq} </if>
	       <if test="inputDate != null"> and d.inputDate = #{inputDate} and s1.inputDate = date_format(date_add(#{inputDate}, INTERVAL -1 DAY), '%Y-%m-%d') </if>
	       order by d.plantSeq desc, d.inputDate desc, d.inputHour desc, d.inputMinute desc
	       
			select d.*, m.pcsMaker, m.batteryMaker, p.name as plantName
			from (	       
				select d.*, s1.chargePower as y1ChargePower, s1.dischargePower as y1DischargePower 
				from t_pmsdata_${realtimeYear} d left join t_pmsdata_statistic_day s1 
				on d.plantSeq = s1.plantSeq and d.pcsIdx = s1.pcsIdx 
				<if test="plantSeq > 0"> and s1.plantSeq = #{plantSeq} </if>
				<if test="pcsIdx >= 0"> and s1.pcsIdx = #{pcsIdx} </if>
				<if test="inputDate != null"> and s1.inputDate = date_format(date_add(#{inputDate}, INTERVAL -1 DAY), '%Y-%m-%d') </if>
				where  1=1 
				<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="timetableSeq > 0"> and d.timetableSeq = #{timetableSeq} </if>
				<if test="str1 != null"> and d.inputDate &gt;= #{str1} and d.inputDate &lt;= #{str2} </if> 
			) d, t_pms m, t_plant p
			where d.plantSeq = p.seq and d.plantSeq = m.plantSeq	
				       
 		-->	       
	       
			select d.*, 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_2020 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 1=1 
					<if test="plantSeq > 0"> and s1.plantSeq = #{plantSeq} </if> 
					<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 = 0 </if>
					<if test="str4 != null"> and s2.inputDate = #{str4} </if>
					and s1.plantSeq = s2.plantSeq and s1.pcsIdx = s2.pcsIdx    
					) s
				on d.plantSeq = s.plantSeq and d.pcsIdx = s.pcsIdx 
			    where 1=1 
			    <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>
				<if test="str1 != null"> and d.inputDate &gt;= #{str1} and d.inputDate &lt;= #{str2} </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.inputDate asc, d.inputHour asc, d.inputMinute asc</if>
			<if test="orderby == null || orderby != 'asc'">order by d.plantSeq desc, d.inputDate desc, d.inputHour desc, d.inputMinute desc</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
			where  p.seq = d.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="data51 == -1"> and d.data51 &lt;&gt; 0 and d.data56 &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>
			<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 &gt;= #{str1} and d.inputDate &lt;= #{str2} </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.*
			from t_pmsdata_${realtimeYear} d, t_plant p
			where  p.seq = d.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="data51 == -1"> and d.data51 &lt;&gt; 0 and d.data56 &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>
			<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 &gt;= #{str1} and d.inputDate &lt;= #{str2} </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.pmsStatus, p.pcsMaker, p.pcsQuantity, 
			    d.seq, d.pcsIdx, d.timetableSeq, d.inputDate, d.inputHour, d.inputMinute,
			    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,
			    d.createDatetime, d.count
			from (
				select p.seq, p.name as plantName, p.plantStatus, p.supplyPower, pm.pmsStatus, pm.pcsMaker, pm.pcsQuantity
				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="plantStatus != null and plantStatus != '-1' "> and p.plantStatus = #{plantStatus}</if>
				<if test="plantSeq > 0 "> and p.seq = #{plantSeq}</if>	
			) p left join (
			
				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=1 	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.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
			order by p.seq desc, d.pcsIdx asc
	    </select>
    
	    
	    <select id="getFaultBscList"  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.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)   
			order by p.seq 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	
			
	      <if test="pgtl != null">
	        <include refid="commonPagingFooter" />
	      </if>
	      
	    </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>   	     						
		
	</mapper>