<?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">
<mapper namespace="com.huatek.busi.mapper.RptAretMonitorSummaryMapper">
	<sql id="Base_Column_List">
            id as id,
            site_id as siteId,
            sub_equipment_id as subEquipmentId,
            main_equipment_id as mainEquipmentId,
            arrester_count as arresterCount,
            spring_count as springCount,
            summer_count as summerCount,
            autumn_count as autumnCount,
            winter_count as winterCount,
            dangtian_count as dangtianCount,
            last_time as lastTime,
            a_senser_id as aSenserId,
            a_full_alc as aFullAlc,
            a_hindrance_alc as aHindranceAlc,
            b_senser_id as bSenserId,
            b_full_alc as bFullAlc,
            b_hindrance_alc as bHindranceAlc,
            c_senser_id as cSenserId,
            c_full_alc as cFullAlc,
            c_hindrance_alc as cHindranceAlc,
            rtp_time as rtpTime,
            create_time as createTime,
            remark as remark
    </sql>
    <delete id="deleteOneDayDate">
    	delete from rpt_aret_monitor_summary where rtp_time = #{day}
    </delete>
    <select id="findOneDayData" resultType="com.huatek.busi.vo.rpt.RptAretMonitorSummaryQueryVo">
    	SELECT
		  a.site_id AS siteId,
		  a.main_tree_id AS mainTreeId,
		  a.sub_tree_id AS subTreeId,
		  a.main_equipment_id AS mainEquipmentId,
		  a.sub_equipment_id AS subEquipmentId,
		  SUM(a.actionCountA + a.actionCountB + a.actionCountC) AS totalActionCount,
		
		  a.phaseA,
		  SUM(a.totalCurrentA) AS totalCurrentA,
		  SUM(a.resistiveCurrentA) AS resistiveCurrentA,
		
		  a.phaseB,
		  SUM(a.totalCurrentB) AS totalCurrentB,
		  SUM(a.resistiveCurrentB) AS resistiveCurrentB,
		
		  a.phaseC,
		  SUM(a.totalCurrentC) AS totalCurrentC,
		  SUM(a.resistiveCurrentC) AS resistiveCurrentC
		FROM (SELECT
		    t.site_id,
		    t.main_tree_id,
		    t.sub_tree_id,
		    t.main_equipment_id,
		    t.sub_equipment_id,
		    t.sensor_id,
		
		    'A' AS phaseA,
		    SUM(t.action_count) AS actionCountA,
		    AVG(t.total_current) AS totalCurrentA,
		    AVG(t.resistive_current) AS resistiveCurrentA,
		
		    'B' AS phaseB,
		    0 AS actionCountB,
		    0 AS totalCurrentB,
		    0 AS resistiveCurrentB,
		
		    'C' AS phaseC,
		    0 AS actionCountC,
		    0 AS totalCurrentC,
		    0 AS resistiveCurrentC
		  FROM lightningrod_im t
		  WHERE t.phase LIKE 'A%'
		  		AND t.time_stamp &gt;= #{startTime}
		  		AND t.time_stamp &lt; #{endTime}
		  GROUP BY t.site_id,
		           t.sensor_id,
		           t.phase
		
		  UNION ALL
		
		  SELECT
		    t.site_id,
		    t.main_tree_id,
		    t.sub_tree_id,
		    t.main_equipment_id,
		    t.sub_equipment_id,
		    t.sensor_id,
		
		    'A' AS phaseA,
		    0 AS actionCountA,
		    0 AS totalCurrentA,
		    0 AS resistiveCurrentA,
		
		    'B' AS phaseB,
		    SUM(t.action_count) AS actionCountB,
		    AVG(t.total_current) AS totalCurrentB,
		    AVG(t.resistive_current) AS resistiveCurrentB,
		
		    'C' AS phaseC,
		    0 AS actionCountC,
		    0 AS totalCurrentC,
		    0 AS resistiveCurrentC
		  FROM lightningrod_im t
		  WHERE t.phase LIKE 'B%'
		  		AND t.time_stamp &gt;= #{startTime}
		  		AND t.time_stamp &lt; #{endTime}
		  GROUP BY t.site_id,
		           t.sensor_id,
		           t.phase
		
		  UNION ALL
		
		  SELECT
		    t.site_id,
		    t.main_tree_id,
		    t.sub_tree_id,
		    t.main_equipment_id,
		    t.sub_equipment_id,
		    t.sensor_id,
		
		    'A' AS phaseA,
		    0 AS actionCountA,
		    0 AS totalCurrentA,
		    0 AS resistiveCurrentA,
		
		    'B' AS phaseB,
		    0 AS actionCountB,
		    0 AS totalCurrentB,
		    0 AS resistiveCurrentB,
		
		    'C' AS phaseC,
		    SUM(t.action_count) AS actionCountC,
		    AVG(t.total_current) AS totalCurrentC,
		    AVG(t.resistive_current) AS resistiveCurrentC
		  FROM lightningrod_im t
		  WHERE t.phase LIKE 'C%'
		  		AND t.time_stamp &gt;= #{startTime}
		  		AND t.time_stamp &lt; #{endTime}
		  GROUP BY t.site_id,
		           t.sensor_id,
		           t.phase) a
    </select>
</mapper>