<?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.KescoMapper">

		<!-- 해당 부분의 id는 MapperClass의 함수 이름과 유사하여야 합니다. -->
	    <sql id="commonPagingHeader"  >
	      SELECT R1.* FROM (
	    </sql>
	    
	    <sql id="commonPagingFooter"  >
	      ) R1 LIMIT #{pgtl.startNo}, #{pgtl.listPerPage}
	    </sql>    
	     		
	     		
	    <select id="selectList"  parameterType="com.elt.ems.vo.KescoPlantVo" resultType="com.elt.ems.vo.KescoPlantVo">
	
	      <if test="pgtl != null">
	        <include refid="commonPagingHeader" /> 
	      </if>
	
			select p.name as name, k.*
			from t_kesco k, t_plant p
			where k.plantSeq = p.seq
			<if test="name != null and name != '' "> and p.name like concat('%', #{name}, '%')</if>
			<if test="plantSeq > 0"> and p.seq = #{plantSeq}</if>			
			<if test="kescoStatus != null and kescoStatus != '' "> and k.kescoStatus = #{kescoStatus} </if>
			order by p.seq desc	
			
	      <if test="pgtl != null">
	        <include refid="commonPagingFooter" />
	      </if>
	      
	    </select>
	    	     		
	    <select id="selectListKescoDataCount"  parameterType="com.elt.ems.vo.KescoDataVo" resultType="integer">
	
			select count(k.seq) as count 
			from t_kesco_data k, t_plant p
			where k.plantSeq = p.seq
			and k.inputDate between #{str1} and #{str2}
			<if test="inputHour != null and inputHour != '' and inputHour != '-1'"> and k.inputHour = #{inputHour}</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="sendStatus != null and sendStatus != '' "> and k.sendStatus = #{sendStatus} </if>
			
	    </select>
	    		
	    <select id="selectListKescoData"  parameterType="com.elt.ems.vo.KescoDataVo" resultType="com.elt.ems.vo.KescoDataVo">
	
	      <if test="pgtl != null">
	        <include refid="commonPagingHeader" /> 
	      </if>
	
			select p.name as plantName, k.*
			from t_kesco_data k, t_plant p
			where k.plantSeq = p.seq
			and k.inputDate between #{str1} and #{str2}
			<if test="inputHour != null and inputHour != '' and inputHour != '-1'"> and k.inputHour = #{inputHour}</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="sendStatus != null and sendStatus != '' "> and k.sendStatus = #{sendStatus} </if>
			order by k.inputDate desc, k.inputHour desc, k.plantSeq asc	
			
	      <if test="pgtl != null">
	        <include refid="commonPagingFooter" />
	      </if>
	      
	    </select>
	    
	    <select id="selectListKescoDataDetailCount"  parameterType="com.elt.ems.vo.KescoDataVo" resultType="integer">
	    
			select 
				count(k.seq) as count
			from (
				select k.*, m.pcsMaker, m.batteryMaker, p.name as plantName from t_kesco_data k, t_pms m, t_plant p 
			    where k.plantSeq = p.seq and m.plantSeq = p.seq and k.inputDate=#{inputDate} 
			    <if test="inputHour != null and inputHour != '' "> and k.inputHour=#{inputHour} </if>     
			) k left join t_pmsdata_statistic_hour d
			on k.plantSeq = d.plantSeq and k.pmsdataSeq = d.seq and d.pcsIdx=0 and d.inputDate=#{inputDate} 
			<if test="inputHour != null and inputHour != '' "> and d.inputHour=#{inputHour} </if> 
			<if test="plantSeq > 0"> and d.plantSeq = #{plantSeq} </if>
			where k.inputDate = #{inputDate} 
			<if test="inputHour != null and inputHour != '' "> and k.inputHour=#{inputHour} </if>
			<if test="plantSeq > 0"> and k.plantSeq = #{plantSeq} </if>
	    </select>
	    	    

	    <select id="selectListKescoDataDetail"  parameterType="com.elt.ems.vo.KescoDataVo" resultType="com.elt.ems.vo.KescoDataVo">
	    
	      <if test="pgtl != null">
	        <include refid="commonPagingHeader" /> 
	      </if>
	      	    
			select 
			k.*, d.pcsIdx, 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 (
				select k.*, m.pcsMaker, m.batteryMaker, p.name as plantName from t_kesco_data k, t_pms m, t_plant p 
			    where k.plantSeq = p.seq and m.plantSeq = p.seq and k.inputDate=#{inputDate} 
			    <if test="inputHour != null and inputHour != '' "> and k.inputHour=#{inputHour} </if>     
			) k left join t_pmsdata_statistic_hour d
			on k.plantSeq = d.plantSeq and k.pmsdataSeq = d.seq and d.pcsIdx=0 and d.inputDate=#{inputDate} 
			<if test="inputHour != null and inputHour != '' "> and d.inputHour=#{inputHour} </if> 
			<if test="plantSeq > 0"> and d.plantSeq = #{plantSeq} </if>
			where k.inputDate = #{inputDate} 
			<if test="inputHour != null and inputHour != '' "> and k.inputHour=#{inputHour} </if>
			<if test="plantSeq > 0"> and k.plantSeq = #{plantSeq} </if>
			order by k.timetableSeq desc, k.inputDate desc, k.inputHour desc,  k.plantSeq asc
			
	      <if test="pgtl != null">
	        <include refid="commonPagingFooter" />
	      </if>
	      			
	    </select>
	    
	    <select id="selectListTimetable"  parameterType="com.elt.ems.vo.TimetableKescoVo" resultType="com.elt.ems.vo.TimetableKescoVo">
	
	      <if test="pgtl != null">
	        <include refid="commonPagingHeader" /> 
	      </if>
	
			select *
			from t_timetable_kesco k
			where k.inputDate = #{inputDate} 
			order by k.inputDate desc, k.inputHour desc	
			
	      <if test="pgtl != null">
	        <include refid="commonPagingFooter" />
	      </if>
	      
	    </select>	
	    
	    <select id="selectMaxInputHour"  parameterType="com.elt.ems.vo.KescoDataVo" resultType="String">
			select max(inputHour) as inputHour
			from t_timetable_kesco k
			where k.inputDate = #{inputDate} 
	      
	    </select>	
	    			
		<update id="update"  parameterType="com.elt.ems.vo.KescoPlantVo">
			update t_kesco set 
				customerId = #{customerId},
				kescoStatus = #{kescoStatus},
				rpsId = #{rpsId},
				kepcoId = #{kepcoId},
				specialStatus = #{specialStatus},
				aesKey = #{aesKey},
				protoVer = #{protoVer},
				updateDatetime = now()
			where plantSeq = #{plantSeq}
		</update>	
		
		
		
		<insert id="insert"  parameterType="com.elt.ems.vo.KescoPlantVo">
			insert into t_kesco (
				plantSeq, customerId, kescoStatus, rpsId, kepcoId, specialStatus,  aesKey, protoVer, createDateTime, updateDatetime
			) values (
				#{plantSeq},
				#{customerId},
				#{kescoStatus},
				#{rpsId},
				#{kepcoId},
				#{specialStatus},
				#{aesKey},
				#{protoVer},
				now(),
				now()
			)
		</insert>	
		
	</mapper>