<?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.TransformerRunStatusMapper">
  <sql id="Base_Column_List">
    t.id, 
    t.site_id as siteId, 
    t.main_tree_id as mainTreeId, 
    t.work_status as workStatus, 
    t.d_time as jcTime, 
    t.oil_temperature as oilTemperature, 
    t.oil_position as oilPosition, 
    t.cooling_entry_pressure as coolingEntryPressure, 
    t.cooling_exit_pressure as coolingExitPressure,
    t.system_code as systemCode,
    s.name as equipmentName,
    t.top_oil_temperature as topOilTemperature,
    t.oilpillow_oillevel as oilpillowOillevel,
    t.circulating_oil_inlet_pressure as circulatingOilInletPressure,
    t.circulating_oil_outlet_pressure as circulatingOilOutletPressure,
    t.coolingwater_inlet_temperature as coolingwaterInletTemperature,
    t.coolingwater_outlet_temperature as coolingwaterOutletTemperature,
    t.circulating_oil_inlet_temperature as circulatingOilInletTemperature,
    t.circulating_oil_out_temperature as circulatingOilOutTemperature,
    t.winding_temperature as windingTemperature
  </sql>
  
  <sql id="Base_Column_List2">
    t.id as id,
    t.site_id as siteId, 
    bs.site_name as siteName, 
    tree.name as mainTreeName,
    t.main_tree_id as mainTreeId, 
    t.work_status as workStatus, 
    t.d_time as timeStamp, 
    t.oil_temperature as oilTemperature, 
    t.oil_position as oilPosition, 
    t.cooling_entry_pressure as coolingEntryPressure, 
    t.cooling_exit_pressure as coolingExitPressure,
    threshold.oil_temperature as oilTemperatureVal,
    threshold.oilpillow_oillevel as oilPositionVal,
    threshold.coolingwater_inlet_pressure as coolingEntryPressureVal,
    threshold.coolingwater_outlet_pressure as coolingExitPressureVal,
    threshold.coolingwater_inlet_temperature as coolingEntryTemperatureVal,
	threshold.coolingwater_outlet_temperature as coolingExitTemperatureVal,
	threshold.winding_temperature as windingTemperatureVal,
	threshold.circulating_oil_inlet_pressure as circulatingEntryPressureVal,
	threshold.circulating_oil_inlet_temperature as circulatingOilInletTemperature,
    threshold.circulating_oil_out_temperature as circulatingOilOutTemperature,
	threshold.transformer_Ontology_oillevel as transformerOntologyOillevelVal,
	threshold.top_oil_temperature as topOilTemperatureVal
  </sql>
  
  
  <select id="findTransformerRunStatusPage" resultType="java.util.Map">
	    select 
	    	<include refid="Base_Column_List" />
	    from transformer_run_status t
	    	left join busi_site_tree s  ON t.main_tree_id = s.id
	    <where>
	    	s.status = '1'
	   		<if test="mainTreeId != null and mainTreeId != ''">
			    and t.main_tree_id = #{mainTreeId}
			</if>
			<if test="workStatus != null and workStatus != ''">
			    and t.work_status = #{workStatus}
			</if>
			<if test="timeStamp != null and timeStamp != ''">
				and DATE_FORMAT(t.d_time,'%Y-%m-%d') &gt;= #{timeStamp}
			</if>
			<if test="timeStampEnd != null and timeStampEnd != ''">
				and DATE_FORMAT(t.d_time,'%Y-%m-%d') &lt;= #{timeStampEnd}
			</if>
		</where>
		ORDER BY t.d_time DESC 
  </select>
  <select id="exportPointTableData" resultType="com.huatek.busi.vo.TransformerRunStatusVo">
	    select 
	    <include refid="Base_Column_List" />
	    from transformer_run_status t
	    left join busi_site_tree s ON t.system_code = s.system_code
	    <where>
	   		<if test="mainTreeId != null and mainTreeId != ''">
			    t.main_tree_id = #{mainTreeId}
			</if>
			<if test="timeStamp != null">
				and DATE_FORMAT(t.d_time,'%Y-%m-%d') &gt;= #{timeStamp}
			</if>
			<if test="timeStampEnd != null">
				and DATE_FORMAT(t.d_time,'%Y-%m-%d')  &lt;= #{timeStampEnd}
			</if>
		</where>
		ORDER BY t.d_time DESC
  </select>

	<select id="findSensorCodeList" resultType="com.huatek.busi.dto.curve.BaseCurveQueryDto">
		SELECT DISTINCT
			t.main_tree_id as mainTreeId,
			t.main_tree_id as sensorCode,
			count(*) as count
		FROM transformer_run_status t
		WHERE t.d_time &gt;= #{startTime} and t.d_time &lt; #{endTime}
		GROUP BY t.main_tree_id
	</select>

	<select id="findDaylyPointList" resultType="com.huatek.busi.vo.curve.PointItemVo">
		SELECT
			t.main_tree_id as sensorCode,
			t.d_time as timeStamp,
			CAST(t.${subType} AS decimal(20, 4)) as val
		FROM transformer_run_status t
		WHERE t.main_tree_id = #{dto.mainTreeId}
			AND t.d_time &gt;= #{startTime}
			AND t.d_time &lt;= #{endTime}
		ORDER BY t.d_time ASC
	</select>

	<select id="getDaylyPointStatistics" resultType="com.huatek.busi.dto.curve.PointItemDto">
		SELECT
			t.main_tree_id AS mainTreeId,
			COUNT(*) AS count,
			CAST(SUM(t.${subType}) AS decimal(20, 4)) AS total,
			MAX(CAST(t.${subType} AS decimal(20, 4))) AS max,
			MIN(CAST(t.${subType} AS decimal(20, 4))) AS min
		FROM transformer_run_status t
		WHERE t.main_tree_id = #{mainTreeId}
			AND t.d_time &gt;= #{startTime} 
			AND t.d_time &lt;= #{endTime}
	</select>

	<select id="transformerRunStatusCurrent" parameterType="com.huatek.busi.dto.TransformerRunStatusDto" resultType="com.huatek.busi.vo.TransformerRunStatusVo">
	    select 
	    <include refid="Base_Column_List2" />
	    from (
	      SELECT 
			  run.* 
			FROM
			  transformer_run_status run,
			  (SELECT 
			    rs.main_tree_id,
			    MAX(rs.d_time) AS max_time 
			  FROM
			    transformer_run_status rs
			  GROUP BY main_tree_id DESC) tmp 
			WHERE tmp.main_tree_id = run.main_tree_id 
			  AND run.d_time = tmp.max_time 
		   	<if test="dTime != null">
            		and DATE_FORMAT(tmp.max_time,'%Y-%m-%d') &gt;= DATE_FORMAT(#{dTime},'%Y-%m-%d')
	        </if>
	        <if test="dTimeEnd != null">
	             and DATE_FORMAT(tmp.max_time,'%Y-%m-%d') &lt;= DATE_FORMAT(#{dTimeEnd},'%Y-%m-%d')
	        </if>
			GROUP BY run.main_tree_id,
			  run.d_time 
	    ) t
	    LEFT JOIN busi_site_tree tree ON t.main_tree_id = tree.id 
	    LEFT JOIN busi_site bs ON t.site_id = bs.id 
	    LEFT JOIN transformer_runing_status_threshold threshold ON t.main_tree_id = threshold.main_tree_id AND t.work_status = threshold.work_status	    
	    <where>
	        <if test="equipmentName != null and equipmentName != '' ">
	         	tree.name like concat("%",#{equipmentName},"%") 
	        </if>      
	        <!-- <if test="dTime != null">
             	and DATE_FORMAT(t.d_time,'%Y-%m-%d') &gt;= DATE_FORMAT(#{dTime},'%Y-%m-%d')
	        </if>
	        <if test="dTimeEnd != null">
	             and DATE_FORMAT(t.d_time,'%Y-%m-%d') &lt;= DATE_FORMAT(#{dTimeEnd},'%Y-%m-%d')
	        </if> -->
       </where>
     </select>

	<select id="getListEquInfo" resultType="com.huatek.busi.vo.TransformerRunStatusCountVo">
		SELECT
			t.site_id AS siteId,
			t.main_tree_id AS mainTreeId,
			CAST(AVG(t.oil_temperature) AS decimal(20, 4)) AS avgOilTemperature,
			CAST(AVG(t.oil_position) AS decimal(20, 4)) AS avgOilPosition,
			CAST(AVG(t.cooling_entry_pressure) AS decimal(20, 4)) AS avgCoolingEntryPressure,
			CAST(AVG(t.cooling_exit_pressure) AS decimal(20, 4)) AS avgCoolingExitPressure,
			CAST(AVG(t.coolingwater_outlet_temperature) AS decimal(20, 4)) AS avgCoolingWaterOutletTemperature,
			CAST(AVG(t.coolingwater_inlet_temperature) AS decimal(20, 4)) AS avgCoolingWaterInletTemperature,
			COUNT(*) AS dataCount
		FROM
			transformer_run_status t
		WHERE
			t.d_time &gt;= #{startTime}
			AND t.d_time &lt;= #{endTime}
		GROUP BY t.site_id,
				t.main_tree_id
	</select>
	<select id="getList" resultType="com.huatek.busi.entity.TransformerRunStatus">
		select
		<include refid="Base_Column_List" />
		from transformer_run_status t
		left join busi_site_tree s  ON t.main_tree_id = s.id
		<where>
			s.status = '1'
			<if test="mainTreeId != null and mainTreeId != ''">
				and t.main_tree_id = #{mainTreeId}
			</if>
			<if test="day != null and day != ''">
				and DATE_FORMAT(t.d_time,'%Y-%m-%d') = #{day}
			</if>

		</where>
		ORDER BY t.d_time DESC
	</select>


</mapper>