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

	<sql id="Base_Column_List2">
		t.id AS id,
		t.site_id AS siteId,
		t.system_code AS systemCode,
		t.sensor_code AS sensorCode,
		sensor.sensor_name AS sensorName,
		t.time_stamp AS timeStamp,
		t.work_state AS workState,
		t.dataval AS dlval,
		t.picpath AS picpath,
		tree.name as equipmentName
	</sql>

	<sql id="Base_Query_And_Export">
		SELECT
			<include refid="Base_Column_List2"/>
		FROM 
			gis_read_table t,
			busi_site_tree tree,
			busi_gis_sub_tree sub,
			busi_gis_sensor sensor
		WHERE t.main_tree_id = tree.id
			AND sub.equipment_id = tree.id
			AND sensor.equipment_id = sub.id
			AND sensor.sensor_code = t.sensor_code
			AND sensor.status = '1'
			<if test="mainTreeId != null and mainTreeId != ''">
				AND t.main_tree_id = #{mainTreeId}
			</if>
			<if test="subTreeId != null and subTreeId != ''">
				AND sub.id = #{subTreeId}
			</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>
		ORDER BY t.time_stamp DESC
	</sql>

	<!-- 数据输入 GIS读表、拍照原始数据查询 -->
	<select id="findGisReadTablePageData" resultType="java.util.Map">
		<include refid="Base_Query_And_Export"/>
	</select>

	<!-- 数据输入 GIS读表、拍照原始数据查询 -->
	<select id="exportPointTableData" resultType="com.huatek.busi.vo.GisReadTableVo">
		<include refid="Base_Query_And_Export"/>
	</select>

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

	<select id="findDaylyPointList" resultType="com.huatek.busi.vo.curve.PointItemVo">
		SELECT
			t.sensor_code as sensorCode,
			t.time_stamp as timeStamp,
			CAST(t.${subType} AS decimal(20, 4)) as val
		FROM gis_read_table t
		WHERE t.main_tree_id = #{dto.mainTreeId}
		<if test="dto.sensorCode != null and dto.sensorCode != ''">
			AND t.sensor_code = #{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_code 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 gis_read_table t
		WHERE t.main_tree_id = #{mainTreeId}
		<if test="sensorCode != null and sensorCode != ''">
			AND t.sensor_code = #{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_code AS sensorCode,
		  t.main_tree_id AS mainTreeId,
		  t.work_state AS runState,
		  t.site_id as siteId,
		  CAST(AVG(t.dataval) AS decimal(20, 4)) AS dataval
		FROM gis_read_table t
		WHERE t.time_stamp &gt;= #{startTime}
			AND t.time_stamp &lt;= #{endTime}
		GROUP BY t.sensor_code,
		         t.main_tree_id,
		         t.work_state,
		         t.site_id
	</select>

	<select id="getListEquInfo" resultType="com.huatek.busi.vo.ReadTableCountVo">
		SELECT
			t.site_id AS siteId,
			t.main_tree_id AS mainTreeId,
			t.sensor_code AS sensorCode,
			COUNT(*) AS dataCount,
			CAST(AVG(t.dataval) AS decimal(20, 4)) AS avgDataVal
		FROM
			gis_read_table t
		WHERE
			t.site_id = #{siteId}
			AND t.sensor_code = #{sensorCode}
			AND t.time_stamp &gt;= #{startTime}
			AND t.time_stamp &lt; #{endTime}
	</select>
	
	<select id="queryHistory" resultType="java.util.Map">
		SELECT
		    t.id AS id,
			t.site_id AS siteId,
			t.system_code AS systemCode,
			t.sensor_code AS sensorCode,
			DATE_FORMAT(t.time_stamp, '%Y-%m-%d %H:%i:%S') AS timeStamp,
			t.work_state AS workState,
			t.dataval AS dlval,
			t.work_state as workState,
			t.picpath AS picpath
		FROM gis_read_table t
		WHERE sensor_code=#{sensorCode}
		<if test="startTime != null">
			AND t.time_stamp &gt;= #{startTime}
		</if>
		<if test="endTime != null">
			AND t.time_stamp &lt;= #{endTime}
		</if>
	    order by t.time_stamp desc
	</select>
</mapper>