<?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.StatisticDataMapper">
	
	    <select id="selectListHour"  parameterType="com.elt.ems.vo.PmsStatisticDataVo" resultType="com.elt.ems.vo.PmsStatisticDataVo">
	    
	    	<!--  
	       select d.* , s1.chargePower as y1ChargePower, s1.dischargePower as y1DischargePower
	       from t_pmsdata_statistic_hour 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 inputDate != ''"> 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.inputHour desc
	       -->
	       select d.* , s1.chargePower as y1ChargePower, s1.dischargePower as y1DischargePower
	       from t_pmsdata_statistic_hour 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 inputDate != ''"> 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 inputDate != ''"> and d.inputDate = #{inputDate} </if>
	       order by d.plantSeq desc, d.inputHour desc
	  
	    </select>
	    
	    <select id="selectListDay"  parameterType="com.elt.ems.vo.PmsStatisticDataVo" resultType="com.elt.ems.vo.PmsStatisticDataVo">
	
			select d.*, m.chargePower as y1ChargePower, m.dischargePower as y1DischargePower
			from t_pmsdata_statistic_day d left join t_pmsdata_statistic_month m
			on d.plantSeq = m.plantSeq and d.pcsIdx = m.pcsIdx
			<if test="inputDate != null and inputDate != ''"> and m.inputDate = concat(date_format(date_add(#{inputDate}, INTERVAL -1 MONTH), '%Y-%m'), '-01') </if>
			<if test="plantSeq > 0"> and m.plantSeq = #{plantSeq} </if>
			<if test="pcsIdx >= 0"> and m.pcsIdx = #{pcsIdx} </if>
			where 1=1 
			<if test="inputYear != null and inputYear != ''"> and d.inputYear = #{inputYear} and d.inputMonth = #{inputMonth} </if>
			<if test="plantSeq > 0"> and d.plantSeq = #{plantSeq} </if>
			<if test="pcsIdx >= 0"> and d.pcsIdx = #{pcsIdx} </if>
			order by d.inputYear desc, d.inputMonth desc, d.inputDay desc
	
		  <!-- 
	       select * 
	       from t_pmsdata_statistic_day d
	       where 1=1 
	       <if test="plantSeq > 0"> and d.plantSeq = #{plantSeq}</if>
	       <if test="inputYear != null and inputYear != ''"> and d.inputYear = #{inputYear}</if>
	       <if test="inputMonth != null and inputMonth != ''"> and d.inputMonth = #{inputMonth}</if>
	       order by inputYear desc, inputMonth desc, inputDay desc
	        -->
	  
	    </select>
	    
	    <select id="selectListMonth"  parameterType="com.elt.ems.vo.PmsStatisticDataVo" resultType="com.elt.ems.vo.PmsStatisticDataVo">
		<!--  
	       select * 
	       from t_pmsdata_statistic_month d
	       where 1=1 
	       <if test="plantSeq > 0"> and d.plantSeq = #{plantSeq}</if>
	       <if test="inputYear != null and inputYear != ''"> and d.inputYear = #{inputYear}</if>
	       order by inputYear desc, inputMonth desc
	     -->
	       
			select d1.*,  d2.inputDate
			from t_pmsdata_statistic_month d1 left join t_pmsdata_statistic_month d2
			on d1.plantSeq = d2.plantSeq and d1.pcsIdx = d2.pcsIdx and d1.inputDate = date_add(d2.inputDate, INTERVAL 1 MONTH)
			<if test="plantSeq > 0"> and d2.plantSeq = #{plantSeq} </if>
			<if test="pcsIdx >= 0"> and d2.pcsIdx = #{pcsIdx} </if>
			where 1=1 
			<if test="plantSeq > 0"> and d1.plantSeq = #{plantSeq} </if>
			<if test="pcsIdx >= 0"> and d1.pcsIdx = #{pcsIdx} </if>
			<if test="inputYear != null and inputYear != ''"> and d1.inputYear = #{inputYear} </if>
			order by d1.inputYear desc, d1.inputMonth desc
	  
	    </select>	    
	    

	    
    						
		
	</mapper>