<?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.IvtMapper">
			 		   
	    <select id="selectList" parameterType="com.elt.ems.vo.SolarIvtJuncOverviewVo"  resultType="com.elt.ems.common.CaseSensibleHashMap">
			select p.*, d.pvPower, d.dayPvPower, d.createDatetime as pmsDatetime from (
			
				select p.*, s.inputDate as lastday, s.todayEnergy as lastdayEnergy, s.todayHours as lastdayHours from (
			
					select p.*, o.oseq, o.plantSeq, o.inputDate, o.inputHour, o.inputMinute, o.currPower, o.todayEnergy, o.todayHours, o.lifetimeEnergy, o.ovStatus, o.createDatetime as ivtDatetime 
					from (
						select p.seq, p.name, p.supplyPower, p.plantType, v.link, v.url1, v.url2, v.port, v.key1, v.key2, v.ivtQuantity, v.ivtType
						from t_plant p left join t_ivtjunc v
						on p.seq = v.plantSeq
						where p.plantType in ('02', '03') 
						<if test="plantStatus != null and plantStatus != '-1' "> and p.plantStatus = #{plantStatus}</if>   
						<if test="ivtStatus != null and ivtStatus != '-1' "> and v.ivtStatus = #{ivtStatus}</if>
						<if test="plantSeq > 0 "> and p.seq = #{plantSeq}</if>
					) p left join t_ivtoverview o 
					on p.seq = o.plantSeq and o.oseq = ( 
							select max(oseq) as oseq from t_ivtoverview where inputDate=#{inputDate} and plantSeq = p.seq 
							<if test="inputHour != null and inputHour != '' "> and inputHour = #{inputHour}</if> 
					) 
				) p left join t_ivtoverview_day s
				on p.seq = s.plantSeq and s.inputDate = DATE_ADD(p.inputDate, INTERVAL -1 DAY)
			) p left join t_pmsdata_${realtimeYear} d 
			on p.seq = d.plantSeq and d.inputDate=#{inputDate} and d.seq = ( 
				select max(seq) as seq from t_pmsdata_${realtimeYear} where plantSeq = p.seq and inputDate=#{inputDate}
				<if test="inputHour != null and inputHour != '' "> and inputHour = #{inputHour}</if> 
			) order by p.seq desc 				    
   		</select>
   		
	    <select id="selectListOverviewByPlantSeq" parameterType="com.elt.ems.vo.SolarIvtJuncOverviewVo"  resultType="com.elt.ems.vo.SolarIvtJuncOverviewForm">
			select p.*, d.pvPower, d.dayPvPower, d.createDatetime as pmsDatetime from (
			
				select p.*, s.inputDate as lastday, s.todayEnergy as lastdayEnergy, s.todayHours as lastdayHours from (
				
					select p.*, o.oseq, o.plantSeq, o.inputDate, o.inputHour, o.inputMinute, o.currPower, o.todayEnergy, o.todayHours, o.lifetimeEnergy, o.ovStatus, o.createDatetime as lastDatetime 
					from (
						select p.seq, p.name as plantName, p.supplyPower, p.plantType, v.link, v.url1, v.url2, v.ivtQuantity, v.ivtType
						from t_plant p left join t_ivtjunc v
						on p.seq = v.plantSeq
						where p.plantType in ('02', '03')  and p.seq = #{plantSeq}
						<if test="plantStatus != null and plantStatus != '-1' "> and p.plantStatus = #{plantStatus}</if>   
						<if test="ivtStatus != null and ivtStatus != '-1' "> and v.ivtStatus = #{ivtStatus}</if>					
					) p left join t_ivtoverview o 
					on p.seq = o.plantSeq and o.plantSeq = #{plantSeq} and o.inputDate=#{inputDate}
					<if test="oseq > 0 "> and o.oseq = #{oseq}</if> 
				) p left join t_ivtoverview_day s
				on p.seq = s.plantSeq and s.inputDate = DATE_ADD(p.inputDate, INTERVAL -1 DAY)
			) p left join t_pmsdata_${realtimeYear} d 
			on p.seq = d.plantSeq and d.inputDate=#{inputDate} and 
			    d.seq = ( 
				  select max(seq) as seq from t_pmsdata_${realtimeYear} where plantSeq = p.seq and inputDate=#{inputDate} and inputHour = p.inputHour and inputMinute like concat('%', substring(p.inputMinute, 1, 1), '%') and plantSeq = #{plantSeq}
			)
			<if test="orderby == null or orderby == '' "> order by p.seq desc, p.oseq asc</if> 
			<if test="orderby != null and orderby != '' "> order by p.seq desc, p.oseq desc</if>			 			    
   		</select>   
   		
	    <select id="selectListOverviewStaticByPlantSeq" parameterType="com.elt.ems.vo.SolarIvtJuncOverviewForm"  resultType="com.elt.ems.vo.SolarIvtJuncOverviewForm">
			
			select * from t_ivtoverview_day
			where inputDate between #{str1} and #{str2} 
			<if test="plantSeq > 0 "> and plantSeq = #{plantSeq}</if>
			order by inputDate asc 
			 			    
   		</select>  
   		   		
   		<select id="selectListIvtByPlantSeq" parameterType="com.elt.ems.vo.SolarIvtJuncOverviewVo"  resultType="com.elt.ems.common.CaseSensibleHashMap">
   			SELECT R1.* FROM (   		
				select p.*, d.pvPower, d.dayPvPower, d.createDatetime as pmsDatetime from (
					select p.*, o.*  from 
					(
						select p.seq, p.name, p.supplyPower, p.plantType, v.link, v.url1, v.url2, v.ivtQuantity, v.ivtType
						from t_plant p left join t_ivtjunc v
						on p.seq = v.plantSeq
						where p.plantType in ('02', '03')  and p.seq = #{plantSeq}					 
						<if test="ivtStatus != null and ivtStatus != '-1' "> and v.ivtStatus = #{ivtStatus}</if>					
					) p left join t_ivtdata o 
					on p.seq = o.plantSeq and o.plantSeq = #{plantSeq} <if test="oseq > 0 "> and o.oseq = #{oseq}</if> 
				) p left join t_pmsdata_${realtimeYear} d 
				on p.seq = d.plantSeq and d.inputDate=#{inputDate} and d.seq = ( 
					select max(seq) as seq from t_pmsdata_${realtimeYear} where plantSeq = p.seq 
						and inputDate=#{inputDate} and plantSeq = #{plantSeq}
						<if test="inputHour != null and inputHour != '' "> and inputHour = #{inputHour}</if>
				) order by seq desc, ivtIdx asc  
			) R1 LIMIT 0, 50			    
   		</select>  
   		
   		<select id="selectListJuncByPlantSeq" parameterType="com.elt.ems.vo.SolarIvtJuncOverviewVo"  resultType="com.elt.ems.common.CaseSensibleHashMap">
			select p.*, d.pvPower, d.dayPvPower, d.createDatetime as pmsDatetime from (
				select p.*, o.* from 
				(
					select p.seq, p.name, p.supplyPower, p.plantType, v.link, v.url1, v.url2, v.ivtQuantity, v.ivtType
					from t_plant p left join t_ivtjunc v
					on p.seq = v.plantSeq
					where p.plantType in ('02', '03')  and p.seq = #{plantSeq}					 
					<if test="ivtStatus != null and ivtStatus != '-1' "> and v.ivtStatus = #{ivtStatus}</if>					
				) p left join t_juncdata o 
				on p.seq = o.plantSeq and o.plantSeq = #{plantSeq} <if test="oseq > 0 "> and o.oseq = #{oseq}</if> 
			) p left join t_pmsdata_${realtimeYear} d 
			on p.seq = d.plantSeq and d.inputDate=#{inputDate} and d.seq = ( 
				select max(seq) as seq from t_pmsdata_${realtimeYear} where plantSeq = p.seq 
					and inputDate=#{inputDate} and plantSeq = #{plantSeq}
					<if test="inputHour != null and inputHour != '' "> and inputHour = #{inputHour}</if>
			) order by seq desc, ivtIdx asc  			    
   		</select>
	    	      
	    <select id="select" parameterType="com.elt.ems.vo.SolarIvtJuncOverviewVo"  resultType="com.elt.ems.common.CaseSensibleHashMap">
		    select p.*, s.inputDate as lastday, s.todayEnergy as lastdayEnergy, s.todayHours as lastdayHours from (		    
				select  p.*, o.oseq, o.plantSeq, o.inputDate, o.inputHour, o.inputMinute, o.currPower, o.todayEnergy, o.todayHours, o.lifetimeEnergy, o.ovStatus, o.createDatetime as lastDatetime
				from t_plant p left join t_ivtoverview o
				on p.seq = o.plantSeq
				where p.plantType in ('02', '03')
				<if test="plantStatus != null and plantStatus != '-1' "> and p.plantStatus = #{plantStatus}</if>
				<if test="plantSeq > 0 "> and p.seq = #{plantSeq}</if>
				<if test="oseq > 0 "> and o.oseq = #{oseq}</if>
			) p left join t_ivtoverview_day s
			on p.seq = s.plantSeq and s.inputDate = DATE_ADD(p.inputDate, INTERVAL -1 DAY)				
			order by  p.seq desc		        
   		</select>
   		
	    <select id="selectListHour" parameterType="com.elt.ems.vo.SolarIvtJuncOverviewVo"  resultType="com.elt.ems.common.CaseSensibleHashMap">
			select inputHour, inputMinute, count(oseq) as count
			from t_ivtoverview o
			where inputDate=#{inputDate}
			group by inputHour, inputMinute	
            order by oseq desc	        
   		</select>
   		   		
   		
	    <select id="selectListJuncData" parameterType="com.elt.ems.vo.SolarIvtJuncDataVo"  resultType="com.elt.ems.vo.SolarIvtJuncDataVo">
			select d.* from t_juncdata d, (
				select plantSeq, max(oseq) oseq from t_ivtoverview
				where inputDate = #{inputDate}
				<if test="oseq > 0 "> and oseq = #{oseq}</if>
				<if test="plantSeq > 0 "> and plantSeq = #{plantSeq}</if>
				group by plantSeq
			) s
			where d.plantSeq = s.plantSeq and d.oseq = s.oseq	
			order by oseq asc, ivtIdx asc   
				    		        
   		</select>  
   		
  		<select id="selectListIvtData" parameterType="com.elt.ems.vo.SolarIvtDataVo"  resultType="com.elt.ems.vo.SolarIvtDataVo">
			select d.* from t_ivtdata d, (
				select plantSeq, max(oseq) oseq from t_ivtoverview
				where inputDate = #{inputDate}
				<if test="oseq > 0 "> and oseq = #{oseq}</if>
				<if test="plantSeq > 0 "> and plantSeq = #{plantSeq}</if>
				group by plantSeq
			) s
			where d.plantSeq = s.plantSeq and d.oseq = s.oseq	
			order by plantSeq asc, ivtIdx asc        
   		</select> 
   		
   		<select id="selectListIvtDataByPlantSeq" parameterType="com.elt.ems.vo.SolarIvtDataVo"  resultType="com.elt.ems.vo.SolarIvtDataVo">
			select o.inputHour, o.inputMinute, d.*
			from t_ivtoverview o, t_ivtdata d
			where o.oseq = d.oseq            
			and o.plantSeq = #{plantSeq}
			and o.inputDate = #{inputDate}
			<if test="ivtIdx > 0 "> and ivtIdx = #{ivtIdx}</if>
			order by o.oseq asc, d.ivtIdx asc       
   		</select>  
   		
  		<select id="selectMaxOseq" resultType="int">
			select max(oseq) as oseq from t_ivtoverview     
   		</select>  
   		   		
   		<insert id="insertIvtData"  parameterType="com.elt.ems.vo.SolarIvtDataVo">
			insert into t_ivtdata (
				oseq, plantSeq, ivtIdx, acPower, acFreq, apparentPower, reactivePower, acEnergy, dcCurr, dcVolt, dcPower, temp, powerFactor, lifetimeEnergy, ivtStatus, createDatetime
			) values (
				#{oseq},
				#{plantSeq},
				#{ivtIdx},
				#{acPower},
				#{acFreq},
				#{apparentPower},
				#{reactivePower},
				#{acEnergy},
				#{dcCurr},
				#{dcVolt},
				#{dcPower},
				#{temp},
				#{powerFactor},
				#{lifetimeEnergy},
				#{ivtStatus},
				now()
			)
		</insert>
		
   		<insert id="insertOverview"  parameterType="com.elt.ems.vo.SolarIvtJuncOverviewVo">
			insert into t_ivtoverview (
				oseq, plantSeq, inputDate, inputHour, inputMinute, currPower, todayEnergy, todayHours, lifetimeEnergy, ovStatus, createDatetime
			) values (
				#{oseq},
				#{plantSeq},
				#{inputDate},
				#{inputHour},
				#{inputMinute},
				#{currPower},
				#{todayEnergy},
				#{todayHours},
				#{lifetimeEnergy},
				#{ovStatus},
				now()
			)
		</insert>
		
		<update id="updateIvtJunc"  parameterType="com.elt.ems.vo.SolarIvtJuncPlantVo">
			update t_ivtjunc set 
				url1 = #{url1},
				url2 = #{url2},
				port = #{port},
				ivtStatus = #{ivtStatus},
				ivtQuantity = #{ivtQuantity},
				link = #{link},
				key1 = #{key1},
				key2 = #{key2},
				ivtType = #{ivtType},
				syncType = #{syncType},
				updateDatetime = now()
			where plantSeq = #{plantSeq}
		</update>	
		
		
		
		<insert id="insertIvtJunc"  parameterType="com.elt.ems.vo.SolarIvtJuncPlantVo">
			insert into t_ivtjunc (
				plantSeq, url1, url2, port, ivtStatus, ivtQuantity, link,  key1, key2, ivtType, syncType, createDateTime, updateDatetime
			) values (
				#{plantSeq},
				#{url1},
				#{url2},
				#{port},
				#{ivtStatus},
				#{ivtQuantity},
				#{link},
				#{key1},
				#{key2},
				#{ivtType},
				#{syncType},
				now(),
				now()
			)
		</insert>					
		
	</mapper>