<?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.CwgTemperatureMapper">

    <sql id="Base_Column_List">
            phase as phase,
            site_id as siteId,
            tmp as tmp,
            run_state as runState
    </sql>
    
    <!-- 数据输入测温原始数据查询 -->
	<select id="findCACPageData" resultType="java.util.Map">
		SELECT
		t.sensor_id as sensorId,
		s.sensor_name as sensorName,
		t.tmp as tmp,
		t.time_stamp as timeStamp,
		t.phase as phase,
		(select tree.system_code from busi_site_tree tree where tree.id = t.main_tree_id) as systemCode,
		(select tree.name from busi_site_tree tree where tree.id = t.main_tree_id) as equipmentName
		FROM
		<if test="outTask != null and outTask != ''">
		(	select
				id,sensor_id,tmp,time_stamp,main_tree_id,phase,cac_monitor_type_code
			from
				cwg_temperature
			where (sensor_id, time_stamp) in (select sensor_id, max(time_stamp) from cwg_temperature group by sensor_id )
		) t
		</if>
		<if test='outTask == null or outTask == ""'>
			cwg_temperature t
		</if>
		left join busi_temper_point s on t.sensor_id = s.sensor_code and s.status = '1'
		<where>
			and t.tmp != '-999'
			<if test="mainTreeId != null and mainTreeId != ''">
				and t.main_tree_id = #{mainTreeId}
			</if>
			<if test="cacMonitorTypeCode != null and cacMonitorTypeCode != ''">
				and t.cac_monitor_type_code = #{cacMonitorTypeCode}
			</if>
			<if test="timeStamp != null and timeStamp != ''">
				and DATE_FORMAT(t.time_stamp,'%Y-%m-%d') &gt;= #{timeStamp}
			</if>
			<if test="timeStampEnd != null and timeStampEnd != ''">
				and DATE_FORMAT(t.time_stamp,'%Y-%m-%d') &lt;= #{timeStampEnd}
			</if>
			<if test="sensorCode != null and sensorCode != ''">
				AND t.sensor_id = #{sensorCode}
			</if>
		</where>
		<if test="outTask != null and outTask != ''">
			GROUP BY t.sensor_id
		</if>
		<if test='outTask == null or outTask == ""'>
			ORDER BY t.time_stamp DESC
		</if>
	</select>
<!--	<select id="findCACPageData" resultType="java.util.Map">-->
<!--		SELECT-->
<!--		t.sensor_id as sensorId,-->
<!--		s.sensor_name as sensorName,-->
<!--		t.tmp as tmp,-->
<!--		<if test="outTask != null and outTask != ''">-->
<!--			max(t.time_stamp) as timeStamp,-->
<!--		</if>-->
<!--		<if test='outTask == null or outTask == ""'>-->
<!--			t.time_stamp as timeStamp,-->
<!--		</if>-->
<!--		t.phase as phase,-->
<!--		(select tree.system_code from busi_site_tree tree where tree.id = t.main_tree_id) as systemCode,-->
<!--		(select tree.name from busi_site_tree tree where tree.id = t.main_tree_id) as equipmentName-->
<!--		FROM-->
<!--		cwg_temperature t-->
<!--		left join busi_temper_point s on t.sensor_id = s.sensor_code and s.status = '1'-->
<!--		<where>-->
<!--			and t.tmp != '-999'-->
<!--			<if test="mainTreeId != null and mainTreeId != ''">-->
<!--				and t.main_tree_id = #{mainTreeId}-->
<!--			</if>-->
<!--			<if test="cacMonitorTypeCode != null and cacMonitorTypeCode != ''">-->
<!--				and t.cac_monitor_type_code = #{cacMonitorTypeCode}-->
<!--			</if>-->
<!--			<if test="timeStamp != null and timeStamp != ''">-->
<!--				and DATE_FORMAT(t.time_stamp,'%Y-%m-%d') &gt;= #{timeStamp}-->
<!--			</if>-->
<!--			<if test="timeStampEnd != null and timeStampEnd != ''">-->
<!--				and DATE_FORMAT(t.time_stamp,'%Y-%m-%d') &lt;= #{timeStampEnd}-->
<!--			</if>-->
<!--			<if test="sensorCode != null and sensorCode != ''">-->
<!--				AND t.sensor_id = #{sensorCode}-->
<!--			</if>-->
<!--		</where>-->
<!--		<if test="outTask != null and outTask != ''">-->
<!--			GROUP BY t.sensor_id-->
<!--		</if>-->
<!--		<if test='outTask == null or outTask == ""'>-->
<!--			ORDER BY t.time_stamp DESC-->
<!--		</if>-->
<!--	</select>-->
<!--    <select id="findCACPageData" resultType="java.util.Map">-->
<!--		SELECT-->
<!--			t.sensor_id as sensorId,-->
<!--			s.sensor_name as sensorName,-->
<!--			t.tmp as tmp,-->
<!--			t.time_stamp as timeStamp,-->
<!--			t.phase as phase,-->
<!--			(select tree.system_code from busi_site_tree tree where tree.id = t.main_tree_id) as systemCode,-->
<!--			(select tree.name from busi_site_tree tree where tree.id = t.main_tree_id) as equipmentName-->
<!--        FROM-->
<!--        	cwg_temperature t,-->
<!--        	busi_temper_point s-->
<!--       <where>-->
<!--       		t.sensor_id = s.sensor_code and s.status = '1'-->
<!--	        <if test="mainTreeId != null and mainTreeId != ''">-->
<!--	             and t.main_tree_id = #{mainTreeId}-->
<!--	        </if>-->
<!--	        <if test="cacMonitorTypeCode != null and cacMonitorTypeCode != ''">-->
<!--	             and t.cac_monitor_type_code = #{cacMonitorTypeCode}-->
<!--	        </if>-->
<!--	        <if test="timeStamp != null and timeStamp != ''">-->
<!--             	and DATE_FORMAT(t.time_stamp,'%Y-%m-%d') &gt;= #{timeStamp}-->
<!--	        </if>-->
<!--	        <if test="timeStampEnd != null and timeStampEnd != ''">-->
<!--	             and DATE_FORMAT(t.time_stamp,'%Y-%m-%d') &lt;= #{timeStampEnd}-->
<!--	        </if>-->
<!--       </where>-->
<!--       ORDER BY t.time_stamp DESC-->
<!--    </select>-->
    <!-- 数据输入测温原始数据查询 -->
    <select id="exportCacMonitorData" resultType="com.huatek.busi.vo.CwgTemperatureVo">
        SELECT
        t.sensor_id as sensorId,
        s.sensor_name as sensorName,
        t.tmp as tmp,
        DATE_FORMAT( t.time_stamp,'%Y-%m-%d %H:%m:%s') as timeStamp,
        t.phase as phase,
        (select tree.system_code from busi_site_tree tree where tree.id = t.main_tree_id) as systemCode,
        (select tree.name from busi_site_tree tree where tree.id = t.main_tree_id) as equipmentName
        FROM
       cwg_temperature t left join busi_temper_point s ON t.sensor_id = s.sensor_code
       <where>
	        <if test="mainTreeId != null and mainTreeId != ''">
	             and t.main_tree_id = #{mainTreeId}
	        </if>
	        <if test="cacMonitorTypeCode != null and cacMonitorTypeCode != ''">
	             and t.cac_monitor_type_code = #{cacMonitorTypeCode}
	        </if>
	        <if test="timeStamp != null and timeStamp != ''">
             	and DATE_FORMAT(t.time_stamp,'%Y-%m-%d') &gt;= #{timeStamp}
	        </if>
	        <if test="timeStampEnd != null and timeStampEnd != ''">
	             and DATE_FORMAT(t.time_stamp,'%Y-%m-%d') &lt;= #{timeStampEnd}
	        </if>
		   <if test="sensorId !=null and sensorId !=''">
			   AND t.sensor_id = #{sensorId}
		   </if>
       </where>
    </select>

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

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

	<select id="getDaylyPointStatistics" resultType="com.huatek.busi.dto.curve.PointItemDto">
		SELECT
			t.main_tree_id AS mainTreeId,
			t.sensor_id AS sensorCode,
			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 cwg_temperature t
		WHERE t.main_tree_id = #{mainTreeId}
		<if test="sensorCode != null and sensorCode != ''">
			AND t.sensor_id = #{sensorCode}
		</if>
			AND t.time_stamp &gt;= #{startTime} 
			AND t.time_stamp &lt;= #{endTime}
	</select>

	<select id="queryMonitorDataList" resultType="java.util.Map">
		SELECT
		  t.sensor_id AS sensorCode,
		  t.main_tree_id AS mainTreeId,
		  t.main_equipment_id AS mainEquipmentId,
		  t.sub_tree_id AS subTreeId,
		  t.sub_equipment_id AS subEquipmentId,
		  t.run_state AS runState,
		  t.site_id AS siteId,
		  CAST(AVG(t.tmp) AS decimal(20, 4)) AS tmp
		FROM cwg_temperature t
		WHERE t.time_stamp &gt;= #{startTime}
			AND t.time_stamp &lt;= #{endTime}
		GROUP BY t.sensor_id,
		         t.main_tree_id,
		         t.main_equipment_id,
		         t.sub_tree_id,
		         t.sub_equipment_id,
		         t.run_state,
		         t.site_id
	</select>

	<select id="getListEquInfo" resultType="com.huatek.busi.vo.CwgTemperatureStatisticsVo">
		SELECT
			t.site_id AS siteId,
			t.main_tree_id AS mainTreeId,
			t.sub_tree_id AS subTreeId,
			t.main_equipment_id AS mainEquipmentId,
			t.sub_equipment_id AS subEquipmentId,
			t.sensor_id AS sensorCode,
			t.cac_monitor_type_code AS cacMonitorTypeCode,
			CAST(AVG(t.tmp) AS decimal(20, 4)) AS tmp,
			COUNT(*) AS dataCount
		FROM
			cwg_temperature t
		WHERE 
			t.cac_monitor_type_code = #{cacMonitorTypeCode}
			AND t.time_stamp &gt;= #{startTime}
			AND t.time_stamp &lt; #{endTime}
			AND t.tmp != '-999'
			<if test="sensorCode != null and sensorCode != ''">
				AND t.sensor_id = #{sensorCode}
			</if>
			<if test="mainTreeId != null and mainTreeId != ''">
				AND t.main_tree_id = #{mainTreeId}
			</if>
		GROUP BY t.site_id,
				t.main_tree_id,
				t.sub_tree_id,
				t.sensor_id,
				t.cac_monitor_type_code
	</select>
</mapper>