<?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.BusiArresterSensorMapper">
	<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,
		'4' as type,
		sensor.create_time AS createTime,
		tree.name as mainTreeName,
		sub.name as subTreeName
	</sql>

	<select id="page" resultType="com.huatek.busi.vo.BusiSensorVo">
		SELECT
			<include refid="Base_Column_List2" />
		FROM
			busi_arrester_sensor sensor,
			busi_arrester_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="checkSensorCode" resultType="java.lang.Integer">
		SELECT
			COUNT(*)
		FROM busi_arrester_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_arrester_sensor sensor,
			busi_arrester_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_arrester_sensor sensor,
			busi_arrester_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_arrester_sensor sensor,
			busi_arrester_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_arrester_sensor 
		WHERE equipment_type = #{equipmentType} AND status = '1'
	</select>
</mapper>