<?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.BusiTransformerSensorMapper">
	<sql id="Base_Column_List">
		id AS id,
		sensor_code AS sensorCode,
		sensor_name AS sensorName,
		sensor_brand AS sensorBrand,
		position AS position,
		position_des AS positionDes,
		manufactor AS manufactor,
		production_date AS productionDate,
		use_date AS useDate,
		equipment_id as equipmentId,
		'1' as type,
		equipment_type as equipmentType
	</sql>

	<sql id="Base_Column_List2">
		sensor.id,
		sensor.site_id AS siteId,
		tree.id AS mainTreeId,
		sensor.equipment_id AS subTreeId,
		tree.name AS mainEquipmentName,
		sub.name AS subEquipmentName,
		sensor.sensor_code AS sensorCode,
		sensor.sensor_name AS sensorName,
		sensor.sensor_brand AS sensorBrand,
		sensor.status,
		sensor.`position`,
		sensor.position_des AS positionDes,
		sensor.manufactor,
		sensor.production_date AS productionDate,
		sensor.use_date AS useDate,
		sensor.equipment_type AS equipmentType,
		'1' AS type,
		sensor.type AS oilType,
		sensor.oil_type AS oilDetailType,
		sensor.create_time AS createTime,
		tree.name AS mainTreeName,
		sub.name AS subTreeName,
		sensor.pd_value AS pdValue
	</sql>

	<select id="page" resultType="com.huatek.busi.vo.BusiSensorVo">
		SELECT
			<include refid="Base_Column_List2"/>
		FROM
			busi_transformer_sensor sensor,
			busi_transformer_sub_tree sub,
			busi_site_tree tree
		WHERE sensor.equipment_id = sub.id
			AND sub.equipment_id = tree.id
			AND sensor.equipment_id = #{equipmentId}
			AND sensor.status = '1'
			AND tree.status = '1'
			AND sub.status = '1'
		ORDER BY sensor.create_time DESC
	</select>

	<select id="querySensorByEquipmentId" parameterType="java.lang.String" resultType="com.huatek.busi.vo.BusiSensorVo">
		SELECT
		<include refid="Base_Column_List" />
		FROM
		busi_transformer_sensor t
		WHERE
		t.equipment_id = #{equipmentId}
		AND t.status = '1'
	</select>

	<select id="getSenerCount" resultType="java.lang.Integer">
		SELECT
		  COUNT(*)
		FROM busi_transformer_sensor t
		WHERE t.site_id = #{siteId}
		AND t.equipment_id = #{equipmentId}
		AND t.status = '1'
	</select>

	<select id="checkSensorCode" resultType="java.lang.Integer">
		SELECT
			COUNT(*)
		FROM busi_transformer_sensor t
		WHERE t.sensor_code = #{sensorCode}
			AND t.status = '1'
			AND t.id <![CDATA[!=]]> #{id}
	</select>

	<select id="getBusiSensorVo" resultType="com.huatek.busi.vo.BusiSensorVo">
		SELECT
			<include refid="Base_Column_List2" />
		FROM
			busi_transformer_sensor sensor,
			busi_transformer_sub_tree sub,
			busi_site_tree tree
		WHERE sensor.equipment_id = sub.id
			AND sub.equipment_id = tree.id
			AND sensor.sensor_code = #{sensorCode}
			AND sensor.status = '1'
			AND tree.status = '1'
			AND sub.status = '1'
	</select>
	<select id="getBusiSensorVoById" resultType="com.huatek.busi.vo.BusiSensorVo">
		SELECT
			<include refid="Base_Column_List2" />
		FROM
			busi_transformer_sensor sensor,
			busi_transformer_sub_tree sub,
			busi_site_tree tree
		WHERE sensor.equipment_id = sub.id
			AND sub.equipment_id = tree.id
			AND sensor.id = #{id}
			AND sensor.status = '1'
			AND tree.status = '1'
			AND sub.status = '1'
	</select>

	<select id="findSensorList" resultType="com.huatek.busi.vo.BusiSensorVo">
		SELECT
			<include refid="Base_Column_List2" />
		FROM
			busi_transformer_sensor sensor,
			busi_transformer_sub_tree sub,
			busi_site_tree tree
		<where>
			sensor.equipment_id = sub.id
			AND sub.equipment_id = tree.id
			AND sensor.status = '1'
			AND tree.status = '1'
			AND sub.status = '1'
			AND sensor.sensor_name IS NOT NULL
			AND sensor.sensor_code IS NOT NULL
			<if test="sensorCode != null and sensorCode != ''">
				and sensor.sensor_code = #{sensorCode}
			</if>
			<if test="equipmentType != null and equipmentType != ''">
				and sub.equipment_type = #{equipmentType}
			</if>
			<if test="equipmentTypeList != null and equipmentTypeList.size() > 0">
				AND sensor.equipment_type in
				<foreach collection="equipmentTypeList" index="index" item="item" open="(" separator="," close=")">
					#{item}
				</foreach>
			</if>
			<if test="equipmentId != null and equipmentId != ''">
				and sensor.equipment_id = #{equipmentId}
			</if>
			<if test="mainEquipmentType != null and mainEquipmentType != ''">
				and tree.equipment_type = #{mainEquipmentType}
			</if>
			<if test="mainEquipmentId != null and mainEquipmentId != ''">
				and tree.id = #{mainEquipmentId}
			</if>
		</where>
		ORDER BY sensor.site_id, sensor.equipment_id, sensor.sensor_name
		<if test="sensorCode != null and sensorCode != ''">
			limit 1
		</if>
	</select>

	<select id="findSensorCount" resultType="java.lang.Integer">
		SELECT
			count(*)
		FROM
			busi_transformer_sensor
		WHERE equipment_type = #{equipmentType} AND status = '1'
	</select>

	<select id="findOilSensor" resultType="com.huatek.busi.vo.BusiSensorVo">
		SELECT
			<include refid="Base_Column_List2"/>
		FROM
			busi_transformer_sensor sensor
			LEFT JOIN busi_transformer_sub_tree sub ON sensor.equipment_id = sub.id
			LEFT JOIN busi_site_tree tree ON sub.equipment_id = tree.id
		WHERE
		    sub.id IN ( SELECT id FROM busi_transformer_sub_tree WHERE equipment_id = #{equipmentId} AND `status` = #{status} AND equipment_type = #{oilType} )
		AND sensor.status = #{status}
		AND tree.status = #{status}
		AND sub.status = #{status}
		ORDER BY sensor.create_time DESC
	</select>

	<select id="findSensorType1" resultType="com.huatek.busi.entity.BusiTransformerSensor">
		SELECT
			id,
			sensor_code AS sensorCode,
			sensor_name AS sensorName,
			sensor_brand AS sensorBrand,
			status,
			position,
			position_des AS positionDes,
			manufactor,
			production_date AS productionDate,
			use_date AS useDate,
			site_id AS siteId,
			equipment_id AS equipmentId,
			equipment_type AS equipmentType,
			type,
			oil_type AS oilType,
			create_time AS createTime,
			pd_value AS pdValue
		FROM
			busi_transformer_sensor
		WHERE
			`status` = #{deleted}
			AND equipment_id IN (
			SELECT
				id
			FROM
				busi_transformer_sub_tree
			WHERE
				equipment_id = #{equipmentId}
				AND equipment_type = #{type}
			AND `status` = #{deleted}
			)
			AND type = #{oilType}
	</select>
</mapper>
