<?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_plant p left join t_kesco k
			on p.seq = k.plantSeq 
			where p.plantStatus='01' 
			<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 != '-1' "> and k.kescoStatus = #{kescoStatus} </if>		
			order by p.seq desc				
	      
	<!-- 
			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>
			<if test="plantStatus != null and plantStatus != '' "> and p.plantStatus = #{plantStatus} </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="selectMaxTimetable"  parameterType="com.elt.ems.vo.TimetableKescoVo" resultType="com.elt.ems.vo.TimetableKescoVo">
	
			select *
			from t_timetable_kesco k
			where seq =  (select max(seq) from t_timetable_kesco k where k.inputDate = #{inputDate} )
	      
	    </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>	
		
	    <select id="reportByPlantSeq"  parameterType="com.elt.ems.vo.KescoDataReportVo" resultType="com.elt.ems.vo.KescoDataReportVo">
	
			select inputDate, count(seq) as totalCount, 
				SUM(CASE WHEN sendStatus ='01' THEN 1 ELSE 0 END)  as succCount, 
				SUM(CASE WHEN sendStatus ='01' THEN 0 ELSE 1 END)  as failCount
			from t_kesco_data 
			where inputDate between #{startDate} and #{endDate} and plantSeq = #{plantSeq}
			group by inputDate
			order by inputDate asc	

	    </select>
	    
	    <select id="listInspectHour"  parameterType="com.elt.ems.vo.KescoDataReportVo" resultType="com.elt.ems.common.CaseSensibleHashMap">
	
			select p.seq as plantSeq, p.name as plantName, s.*     
			from t_plant p  left join (
				select  
					s.plantSeq, s.inputdate, s.inputHour, max(input00) as input00, 
					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, 
					count(plantSeq) as total
				from ( 			
					select 
						s.plantSeq, s.inputdate, s.inputHour,
						SUM(CASE WHEN (inputHour = '00') THEN 1 ELSE 0 END) as input00,
						SUM(CASE WHEN (inputHour = '01') THEN 1 ELSE 0 END) as input01,
						SUM(CASE WHEN (inputHour = '02') THEN 1 ELSE 0 END) as input02,
						SUM(CASE WHEN (inputHour = '03') THEN 1 ELSE 0 END) as input03,
						SUM(CASE WHEN (inputHour = '04') THEN 1 ELSE 0 END) as input04,
						SUM(CASE WHEN (inputHour = '05') THEN 1 ELSE 0 END) as input05,
						SUM(CASE WHEN (inputHour = '06') THEN 1 ELSE 0 END) as input06,
						SUM(CASE WHEN (inputHour = '07') THEN 1 ELSE 0 END) as input07,
						SUM(CASE WHEN (inputHour = '08') THEN 1 ELSE 0 END) as input08,
						SUM(CASE WHEN (inputHour = '09') THEN 1 ELSE 0 END) as input09,
						SUM(CASE WHEN (inputHour = '10') THEN 1 ELSE 0 END) as input10,
						SUM(CASE WHEN (inputHour = '11') THEN 1 ELSE 0 END) as input11,
						SUM(CASE WHEN (inputHour = '12') THEN 1 ELSE 0 END) as input12,
						SUM(CASE WHEN (inputHour = '13') THEN 1 ELSE 0 END) as input13,
						SUM(CASE WHEN (inputHour = '14') THEN 1 ELSE 0 END) as input14,
						SUM(CASE WHEN (inputHour = '15') THEN 1 ELSE 0 END) as input15,
						SUM(CASE WHEN (inputHour = '16') THEN 1 ELSE 0 END) as input16,
						SUM(CASE WHEN (inputHour = '17') THEN 1 ELSE 0 END) as input17,
						SUM(CASE WHEN (inputHour = '18') THEN 1 ELSE 0 END) as input18,
						SUM(CASE WHEN (inputHour = '19') THEN 1 ELSE 0 END) as input19,
						SUM(CASE WHEN (inputHour = '20') THEN 1 ELSE 0 END) as input20,
						SUM(CASE WHEN (inputHour = '21') THEN 1 ELSE 0 END) as input21,
						SUM(CASE WHEN (inputHour = '22') THEN 1 ELSE 0 END) as input22,
						SUM(CASE WHEN (inputHour = '23') THEN 1 ELSE 0 END) as input23   
					from t_kesco_data s
					where s.inputDate=#{inputDate}
					group by s.plantSeq, s.inputDate, s.inputHour
				) s 
				group by s.plantSeq
			) s 			
			on p.seq = s.plantSeq
			where p.plantStatus='01' and plantType in ('01', '03')
			<if test="plantSeq > 0 "> and p.seq = #{plantSeq} </if>
			order by p.seq desc
						
	    </select>
	    	    
	    	
	    <select id="listInspectDay"  parameterType="com.elt.ems.vo.KescoDataReportVo" resultType="com.elt.ems.common.CaseSensibleHashMap">
	
			select p.seq as plantSeq, p.name as plantName, s.*     
			from t_plant p  left join (
				select  s.plantSeq, s.inputDate, 
					sum(input01) as input01, sum(input02) as input02, sum(input03) as input03, sum(input04) as input04, max(input05) as input05, 
					sum(input06) as input06, sum(input07) as input07, sum(input08) as input08, sum(input09) as input09, max(input10) as input10,  
					sum(input11) as input11, sum(input12) as input12, sum(input13) as input13, sum(input14) as input14, max(input15) as input15, 
					sum(input16) as input16, sum(input17) as input17, sum(input18) as input18, sum(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.inputDate, s.seq, count(s.seq) as total,
						(CASE WHEN SUBSTRING(inputDate, 9) = '01' THEN COUNT(SEQ) END) as input01,
						(CASE WHEN SUBSTRING(inputDate, 9) = '02' THEN COUNT(SEQ) END) as input02,
						(CASE WHEN SUBSTRING(inputDate, 9) = '03' THEN COUNT(SEQ) END) as input03,
						(CASE WHEN SUBSTRING(inputDate, 9) = '04' THEN COUNT(SEQ) END) as input04,
						(CASE WHEN SUBSTRING(inputDate, 9) = '05' THEN COUNT(SEQ) END) as input05,
						(CASE WHEN SUBSTRING(inputDate, 9) = '06' THEN COUNT(SEQ) END) as input06,
						(CASE WHEN SUBSTRING(inputDate, 9) = '07' THEN COUNT(SEQ) END) as input07,
						(CASE WHEN SUBSTRING(inputDate, 9) = '08' THEN COUNT(SEQ) END) as input08,
						(CASE WHEN SUBSTRING(inputDate, 9) = '09' THEN COUNT(SEQ) END) as input09,
						(CASE WHEN SUBSTRING(inputDate, 9) = '10' THEN COUNT(SEQ) END) as input10,
						(CASE WHEN SUBSTRING(inputDate, 9) = '11' THEN COUNT(SEQ) END) as input11,
						(CASE WHEN SUBSTRING(inputDate, 9) = '12' THEN COUNT(SEQ) END) as input12,
						(CASE WHEN SUBSTRING(inputDate, 9) = '13' THEN COUNT(SEQ) END) as input13,
						(CASE WHEN SUBSTRING(inputDate, 9) = '14' THEN COUNT(SEQ) END) as input14,
						(CASE WHEN SUBSTRING(inputDate, 9) = '15' THEN COUNT(SEQ) END) as input15,
						(CASE WHEN SUBSTRING(inputDate, 9) = '16' THEN COUNT(SEQ) END) as input16,
						(CASE WHEN SUBSTRING(inputDate, 9) = '17' THEN COUNT(SEQ) END) as input17,
						(CASE WHEN SUBSTRING(inputDate, 9) = '18' THEN COUNT(SEQ) END) as input18,
						(CASE WHEN SUBSTRING(inputDate, 9) = '19' THEN COUNT(SEQ) END) as input19,
						(CASE WHEN SUBSTRING(inputDate, 9) = '20' THEN COUNT(SEQ) END) as input20,
						(CASE WHEN SUBSTRING(inputDate, 9) = '21' THEN COUNT(SEQ) END) as input21,
						(CASE WHEN SUBSTRING(inputDate, 9) = '22' THEN COUNT(SEQ) END) as input22,
						(CASE WHEN SUBSTRING(inputDate, 9) = '23' THEN COUNT(SEQ) END) as input23,
						(CASE WHEN SUBSTRING(inputDate, 9) = '24' THEN COUNT(SEQ) END) as input24,
						(CASE WHEN SUBSTRING(inputDate, 9) = '25' THEN COUNT(SEQ) END) as input25,
						(CASE WHEN SUBSTRING(inputDate, 9) = '26' THEN COUNT(SEQ) END) as input26,
						(CASE WHEN SUBSTRING(inputDate, 9) = '27' THEN COUNT(SEQ) END) as input27,
						(CASE WHEN SUBSTRING(inputDate, 9) = '28' THEN COUNT(SEQ) END) as input28,
						(CASE WHEN SUBSTRING(inputDate, 9) = '29' THEN COUNT(SEQ) END) as input29,
						(CASE WHEN SUBSTRING(inputDate, 9) = '30' THEN COUNT(SEQ) END) as input30,
						(CASE WHEN SUBSTRING(inputDate, 9) = '31' THEN COUNT(SEQ) END) as input31
					from t_kesco_data s
					where s.inputdate between #{startDate} and #{endDate}	
					<if test="sendStatus != null and sendStatus != '' ">
                    	and sendStatus = #{sendStatus}
                    </if>
					group by s.plantSeq, s.inputDate
				) s 
			    group by s.plantSeq
								
			) s
			on p.seq = s.plantSeq
			where p.plantStatus='01' and plantType in ('01', '03')
			<if test="plantSeq > 0 "> and p.seq = #{plantSeq} </if>
			order by p.seq desc
									
	    </select>		    		
		
	</mapper>