<?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.PMeterMapper">
	
        <select id="selectList" parameterType="java.util.HashMap"  resultType="com.elt.ems.common.CaseSensibleHashMap"> 

            select d.plantSeq, p.name as plantName, p.supplyPower,            
                ROUND(sum(case when ivtDay = '01' then actmt*100/energy else 0 end)) as per01,
                ROUND(sum(case when ivtDay = '02' then actmt*100/energy else 0 end)) as per02, 
                ROUND(sum(case when ivtDay = '03' then actmt*100/energy else 0 end)) as per03, 
                ROUND(sum(case when ivtDay = '04' then actmt*100/energy else 0 end)) as per04, 
                ROUND(sum(case when ivtDay = '05' then actmt*100/energy else 0 end)) as per05,            
                ROUND(sum(case when ivtDay = '06' then actmt*100/energy else 0 end)) as per06,
                ROUND(sum(case when ivtDay = '07' then actmt*100/energy else 0 end)) as per07, 
                ROUND(sum(case when ivtDay = '08' then actmt*100/energy else 0 end)) as per08, 
                ROUND(sum(case when ivtDay = '09' then actmt*100/energy else 0 end)) as per09, 
                ROUND(sum(case when ivtDay = '10' then actmt*100/energy else 0 end)) as per10,             
                ROUND(sum(case when ivtDay = '11' then actmt*100/energy else 0 end)) as per11,
                ROUND(sum(case when ivtDay = '12' then actmt*100/energy else 0 end)) as per12, 
                ROUND(sum(case when ivtDay = '13' then actmt*100/energy else 0 end)) as per13, 
                ROUND(sum(case when ivtDay = '14' then actmt*100/energy else 0 end)) as per14, 
                ROUND(sum(case when ivtDay = '15' then actmt*100/energy else 0 end)) as per15,             
                ROUND(sum(case when ivtDay = '16' then actmt*100/energy else 0 end)) as per16,
                ROUND(sum(case when ivtDay = '17' then actmt*100/energy else 0 end)) as per17, 
                ROUND(sum(case when ivtDay = '18' then actmt*100/energy else 0 end)) as per18, 
                ROUND(sum(case when ivtDay = '19' then actmt*100/energy else 0 end)) as per19, 
                ROUND(sum(case when ivtDay = '20' then actmt*100/energy else 0 end)) as per20, 
                ROUND(sum(case when ivtDay = '21' then actmt*100/energy else 0 end)) as per21,
                ROUND(sum(case when ivtDay = '22' then actmt*100/energy else 0 end)) as per22, 
                ROUND(sum(case when ivtDay = '23' then actmt*100/energy else 0 end)) as per23, 
                ROUND(sum(case when ivtDay = '24' then actmt*100/energy else 0 end)) as per24, 
                ROUND(sum(case when ivtDay = '25' then actmt*100/energy else 0 end)) as per25,            
                ROUND(sum(case when ivtDay = '26' then actmt*100/energy else 0 end)) as per26,
                ROUND(sum(case when ivtDay = '27' then actmt*100/energy else 0 end)) as per27, 
                ROUND(sum(case when ivtDay = '28' then actmt*100/energy else 0 end)) as per28, 
                ROUND(sum(case when ivtDay = '29' then actmt*100/energy else 0 end)) as per29, 
                ROUND(sum(case when ivtDay = '30' then actmt*100/energy else 0 end)) as per30,             
                ROUND(sum(case when ivtDay = '31' then actmt*100/energy else 0 end)) as per31,       
                
                ROUND(sum(case when ivtDay = '01' then todayHours else 0 end), 2) as hour01,
                ROUND(sum(case when ivtDay = '02' then todayHours else 0 end), 2) as hour02,
                ROUND(sum(case when ivtDay = '03' then todayHours else 0 end), 2) as hour03,
                ROUND(sum(case when ivtDay = '04' then todayHours else 0 end), 2) as hour04,
                ROUND(sum(case when ivtDay = '05' then todayHours else 0 end), 2) as hour05,
                ROUND(sum(case when ivtDay = '06' then todayHours else 0 end), 2) as hour06,
                ROUND(sum(case when ivtDay = '07' then todayHours else 0 end), 2) as hour07,
                ROUND(sum(case when ivtDay = '08' then todayHours else 0 end), 2) as hour08,
                ROUND(sum(case when ivtDay = '09' then todayHours else 0 end), 2) as hour09,
                ROUND(sum(case when ivtDay = '10' then todayHours else 0 end), 2) as hour10,
                ROUND(sum(case when ivtDay = '11' then todayHours else 0 end), 2) as hour11,
                ROUND(sum(case when ivtDay = '12' then todayHours else 0 end), 2) as hour12,
                ROUND(sum(case when ivtDay = '13' then todayHours else 0 end), 2) as hour13,
                ROUND(sum(case when ivtDay = '14' then todayHours else 0 end), 2) as hour14,
                ROUND(sum(case when ivtDay = '15' then todayHours else 0 end), 2) as hour15,
                ROUND(sum(case when ivtDay = '16' then todayHours else 0 end), 2) as hour16,
                ROUND(sum(case when ivtDay = '17' then todayHours else 0 end), 2) as hour17,
                ROUND(sum(case when ivtDay = '18' then todayHours else 0 end), 2) as hour18,
                ROUND(sum(case when ivtDay = '19' then todayHours else 0 end), 2) as hour19,
                ROUND(sum(case when ivtDay = '20' then todayHours else 0 end), 2) as hour20,
                ROUND(sum(case when ivtDay = '21' then todayHours else 0 end), 2) as hour21,
                ROUND(sum(case when ivtDay = '22' then todayHours else 0 end), 2) as hour22,
                ROUND(sum(case when ivtDay = '23' then todayHours else 0 end), 2) as hour23,
                ROUND(sum(case when ivtDay = '24' then todayHours else 0 end), 2) as hour24,
                ROUND(sum(case when ivtDay = '25' then todayHours else 0 end), 2) as hour25,
                ROUND(sum(case when ivtDay = '26' then todayHours else 0 end), 2) as hour26,
                ROUND(sum(case when ivtDay = '27' then todayHours else 0 end), 2) as hour27,
                ROUND(sum(case when ivtDay = '28' then todayHours else 0 end), 2) as hour28,
                ROUND(sum(case when ivtDay = '29' then todayHours else 0 end), 2) as hour29,
                ROUND(sum(case when ivtDay = '30' then todayHours else 0 end), 2) as hour30,
                ROUND(sum(case when ivtDay = '31' then todayHours else 0 end), 2) as hour31
            from (
            
                select ivt.plantSeq, ivt.ivtDay, ivt.energy, ivt.todayHours, excDay, IFNULL(actmt, 0) as actmt from (
                    select
                        ivt.plantSeq, ivt.inputDate, substring(ivt.inputDate, 9, 10) as ivtDay, TRUNCATE(max(ivt.todayEnergy), 2) as energy, max(ivt.todayHours) as todayHours
                    from t_ivtoverview ivt, t_kesco k
                    where ivt.inputDate between #{startDate} and #{endDate} and ivt.plantSeq = k.plantSeq and k.protoVer = 9
                    group by plantSeq, inputDate
                ) ivt left join (
                    select 
                        ppa.plantSeq, ppa.excYmd, substring(ppa.excYmd, 7, 8) as excDay, sum(ppa.actmtLpVal) as actmt
                    from t_pmeter_ppa_data ppa
                    where ppa.excYmd between #{startYmd} and #{endYmd}
                    group by plantSeq, excYmd
                ) ppa
                on ppa.plantSeq = ivt.plantSeq and ppa.excDay = ivt.ivtDay
            ) d, t_plant p
            where d.plantSeq = p.seq
            group by d.plantSeq
           <!--   
                select ivt.plantSeq, ivt.ivtDay, ivt.energy, excDay, IFNULL(actmt, 0) as actmt from (
                    select
                        ivt.plantSeq, ivt.inputDate, substring(ivt.inputDate, 9, 10) as ivtDay, TRUNCATE(max(ivt.todayEnergy), 2) as energy
                    from t_ivtoverview ivt
                    where ivt.inputDate between  #{startDate} and #{endDate}
                    group by plantSeq, inputDate
                ) ivt left join (
                    select 
                        ppa.plantSeq, ppa.excYmd, substring(ppa.excYmd, 7, 8) as excDay, sum(ppa.actmtLpVal) as actmt
                    from t_pmeter_ppa_data ppa
                    where ppa.excYmd between #{startYmd} and #{endYmd}
                    group by plantSeq, excYmd
                ) ppa
                on ppa.plantSeq = ivt.plantSeq and ppa.excDay = ivt.ivtDay
                where ivt.plantSeq = 98    -->            
        </select>
        	
        <select id="selectMinuteList" parameterType="com.elt.ems.pmeter.vo.PMeterPPAStatisticLpVo"  resultType="com.elt.ems.common.CaseSensibleHashMap"> <!-- 
			select m.* , inputDate, IFNULL(inputHour, 0) as inputHour, IFNULL(todayEnergy, 0) as todayEnergy  from (
			    select p.name, p.supplyPower, m.*,  substring(m.mrHm, 1, 2) as mrHour -->
            select m.* , inputDate, IFNULL(inputHour, 0) as inputHour, IFNULL(todayEnergy, 0) as todayEnergy  from (
                select p.name, p.supplyPower, m.*,  substring(m.mrHm, 1, 2) as mrHour
			    from t_plant p, t_pmeter_ppa_data m 
			    WHERE p.seq = m.plantSeq and m.excYmd = #{excYmd} and m.plantSeq=#{plantSeq}    
			    order by excYmd asc
			) m left join t_ivtoverview_hour i
			on i.inputHour = m.mrHour  <!--  and m.plantSeq = i.plantSeq -->
			and i.inputDate = #{inputDate} and i.plantSeq=#{plantSeq}
            order by excYmd asc, mrHm asc
        </select>
        
		<select id="selectStatisticList" parameterType="com.elt.ems.pmeter.vo.PMeterPPAStatisticLpVo"  resultType="com.elt.ems.common.CaseSensibleHashMap">
			select m.* , inputDate, IFNULL(inputHour, 0) as inputHour, IFNULL(todayEnergy, 0) as todayEnergy from (
			    select p.name, p.supplyPower, m.*
			    from t_plant p, t_pmeter_ppa_hour m 
			    WHERE p.seq = m.plantSeq and m.excYmd = #{excYmd} and m.plantSeq=#{plantSeq}   
			    order by excYmd asc
			) m left join t_ivtoverview_hour i
			on i.inputHour = m.mrHour  <!--  and m.plantSeq = i.plantSeq -->
			and i.inputDate = #{inputDate} and i.plantSeq=#{plantSeq}
            order by excYmd asc, mrHour asc
		</select>
		
	    <select id="selectPlantList" parameterType="java.util.HashMap"  resultType="com.elt.ems.common.CaseSensibleHashMap">
            select p.seq, p.name, k.plantSeq, k.kepcoId, k.protoVer, k.updateDatetime 
            from t_plant p, t_kesco k
            where p.seq = k.plantSeq and k.kepcoId is not null and k.protoVer = 9
            order by p.seq desc
	    </select>	
	    
	    <select id="selectPlant" parameterType="java.util.HashMap"  resultType="com.elt.ems.common.CaseSensibleHashMap">
			select p.seq, p.name, k.plantSeq, k.kepcoId, k.protoVer, k.updateDatetime 
			from t_plant p, t_kesco k
			where p.seq = k.plantSeq 
			and p.seq = #{plantSeq}
        </select>
    
    <!-- 
    <update id="update"  parameterType="com.elt.ems.vo.ClientVo">
      update t_client set 
        name=#{name}, clientType=#{clientType}, memo=#{memo},
        phone1=#{phone1}, phone2=#{phone2}, managerName=#{managerName}, updateDatetime=now()
      where seq=#{seq}
    </update> -->
		
	</mapper>