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

	<select id="findArresterNumber" parameterType="java.lang.String" resultType="com.huatek.busi.entity.EquipmentNumber">
		SELECT
		t.site_id as siteId,
		t.main_equipment_id as mainEquipmentId,
		tt.id as tId,
		tt.equipment_type as equipmentType,
		SUM(f.real_count) as totalRealCount,
		SUM(f.must_count) as totalMustCount,
		name ,
		tt.create_time as createTime,
		f.d_time as dTime,
		count(*) as number,
		w.warning_type as waringType,
		tt.status
	  FROM
		busi_offline_arrester_brand t
		LEFT JOIN busi_arrester_sub_tree tt on t.site_id=tt.site_id and t.main_equipment_id=tt.equipment_id
		LEFT JOIN failure_rate f on f.main_tree_id =tt.equipment_id and tt.id=f.sub_tree_id
		LEFT JOIN busi_eqm_warning w on tt.id = w.sub_equipment_id
		WHERE t.main_equipment_id=#{mainEquipmentId}
		and t.site_id = #{siteId}
		and tt.status='1'
		and  tt.equipment_type LIKE '___'
		and f.d_time = #{dTime}
	  GROUP BY tt.equipment_type
	</select>
	<select id="findArresterStatus" parameterType="java.lang.String" resultType="com.huatek.busi.entity.ArresterStatus">
	select
		t.site_id as siteId,
		t.main_equipment_id as mainEquipmentId,
		t.`status`,
		t.arrester_name as arresterName,
		w.warning_type as warningType
	FROM
		busi_offline_arrester_brand t
	LEFT JOIN busi_site_tree tt on  tt.id =t.main_equipment_id
	LEFT JOIN
		busi_eqm_warning w on t.main_equipment_id = w.sub_equipment_id
	where
		 t.`status` = '1'
			and tt.`status` = '1'
	group by t.main_equipment_id
	</select>
<select id="findSumArresterNumber" parameterType="java.lang.String" resultType="com.huatek.busi.entity.EquipmentSumNumber">
	SELECT
		COUNT(*) AS sumNumber
	FROM
		busi_offline_arrester_brand t
	LEFT JOIN busi_arrester_sub_tree tt ON t.site_id = tt.site_id
	AND t.main_equipment_id = tt.equipment_id
	LEFT JOIN failure_rate f ON f.main_tree_id = tt.equipment_id
	AND tt.id = f.sub_tree_id
	LEFT JOIN busi_eqm_warning w ON tt.id = w.sub_equipment_id
	WHERE
	 tt. STATUS = '1'
	AND tt.equipment_type LIKE '___'
	AND f.d_time = #{dTime}
	UNION ALL
		SELECT
			COUNT(*) AS sumFault
		FROM
		busi_offline_arrester_brand t
		LEFT JOIN busi_arrester_sub_tree tt ON t.site_id = tt.site_id
		AND t.main_equipment_id = tt.equipment_id
		LEFT JOIN failure_rate f ON f.main_tree_id = tt.equipment_id
		AND tt.id = f.sub_tree_id
		LEFT JOIN busi_eqm_warning w ON tt.id = w.sub_equipment_id
		WHERE
		 tt. STATUS = '1'
		AND w.warning_type = '1'
		AND tt.equipment_type LIKE '___'
		AND f.d_time = #{dTime}
</select>
	<select id="findSumNumber" parameterType="java.lang.String" resultType="java.lang.Integer">
		SELECT COUNT(*)
FROM
(SELECT
		  bs.`site_name` siteName,
		  '避雷器设备'  equipmentType,
		  '4' type,
		  st.`name`  equipmentName,
		  bas.equipment_type pendantType,
		  ast.name pendantName,
		  bas.`sensor_name` sensorName,
		  bas.`sensor_code` sensorCode,
		  bas.sensor_brand sensorBrand,
		  bas.manufactor,
		  bas.position_des positionDes,
		  bas.production_date productionDate,
		  bas.use_date useDate
		FROM
		  busi_arrester_sensor bas
		  LEFT JOIN busi_site bs ON bas.`site_id` = bs.`id`
		  LEFT JOIN busi_arrester_sub_tree ast ON ast.`id` = bas.`equipment_id`
		  LEFT JOIN busi_site_tree st ON st.`id` = ast.`equipment_id`
		WHERE bas.`site_id` =  #{siteId}
	AND bas.status = '1'
)temp
	</select>

	<select id="findFaultNumber" parameterType="java.lang.String" resultType="java.lang.Integer">
		SELECT
			COUNT(*) AS sumFault
		FROM
		busi_offline_arrester_brand t
		LEFT JOIN busi_arrester_sub_tree tt ON t.site_id = tt.site_id
		AND t.main_equipment_id = tt.equipment_id
		LEFT JOIN failure_rate f ON f.main_tree_id = tt.equipment_id
		AND tt.id = f.sub_tree_id
		LEFT JOIN busi_eqm_warning w ON tt.id = w.sub_equipment_id
		WHERE
		 tt. STATUS = '1'
		AND w.warning_type = '1'
		AND tt.equipment_type LIKE '___'
		AND f.d_time = #{dTime}
	</select>

	<select id="faultNumber" parameterType="java.lang.String" resultType="java.lang.Integer">
		SELECT
		count(*) AS number
		FROM
		busi_offline_arrester_brand t
		LEFT JOIN busi_arrester_sub_tree tt on t.site_id=tt.site_id and t.main_equipment_id=tt.equipment_id
		LEFT JOIN busi_eqm_warning w on tt.id = w.sub_equipment_id
		WHERE  tt.id = #{tId}
		and tt.status='1'
		and w.warning_type = '1'
		and  tt.equipment_type LIKE '___'
	</select>

	<select id="findTransformerStatus" parameterType="java.lang.String" resultType="com.huatek.busi.entity.TAndTransformerStatus">
		SELECT
			i.site_id AS siteId,
			i.eqm_code AS eqmCode,
			i.stype AS stype,
			i.ins_date AS ins_date
		FROM
			busi_offline_arrester_brand t
		LEFT JOIN busi_equipment_relationship tt on t.main_equipment_id = tt.equipment_id
		LEFT JOIN iec_bydwork_param i ON tt.transformer_id = i.eqm_code
		WHERE
			t.main_equipment_id = #{findArresterId}
		ORDER BY
			ins_date DESC
		LIMIT 1
	</select>
	<select id="findHwChaobiao" parameterType="java.lang.String" resultType="com.huatek.busi.entity.ArresterHwChaobiaoLv">
		SELECT
			site_id AS siteId,
			main_equipment_id AS mainEquipmentId,
			chaobiaolv AS chaobiaolv,
			rtp_time AS rtpTime
		FROM
			`rpt_aret_temper_summary_chaobiaolv`
		WHERE
			site_id = #{siteId}
		AND main_equipment_id = #{mainEquipmentId}
		AND rtp_time = #{rtpTime}
		ORDER BY
			rtp_time DESC
		LIMIT 1
	</select>
	<select id="findEquipmentVoList" resultType="com.huatek.busi.entity.EquipmentSensor">
		SELECT
			e.sensor_id as sensorId,
			e.time_stamp as timeStamp,
			e.sub_equipment_id as subEquipmentId,
			t.sensor_brand as sensorBrand,
			t.sensor_name as sensorName,
			t.equipment_type as equipmentType,
			t.status
		FROM
			`equipment_temperature_cache` e
		LEFT JOIN busi_arrester_sensor t ON e.sub_equipment_id = t.equipment_id
		WHERE
		 t.`status` = '1'
		GROUP BY e.sensor_id
	</select>
    <select id="findEquipment" resultType="com.huatek.busi.entity.EquipmentSensorNumber">
		SELECT
			e.id as id,
			e.sensor_id as sensorId,
			e.main_equipment_id as mainEquipmentId,
			e.sensor_name as sensorName,
			e.ext_equipment_code as extEquipmentCode,
			e.time_stamp as timeStamp
		FROM
			equipment_temperature_cache e
	LEFT JOIN busi_arrester_sub_tree t on e.main_equipment_id = t.equipment_id
	where t.status = '1'
	GROUP BY e.main_equipment_id
	</select>
	<select id="findSensorCache" resultType="com.huatek.busi.entity.EquipmentSensorCache">
		SELECT
		id AS id,
		sensor_id as sensorId,
		time_stamp as timeStamp,
		sensor_name as sensorName,
		ext_equipment_code AS  extEquipmentCode,
		main_equipment_id AS mainEquipmentId,
		number,
		normal_number AS normalNumber,
		fault_number AS faultNumber,
		online_rate AS onlineRate
	FROM
	`busi_arrester_sensor_cache`;
	</select>
    <select id="findArresterSensor" parameterType="java.lang.String" resultType="com.huatek.busi.entity.EquipmentSensorCache">
	SELECT
			t.main_equipment_id as mainEquipmentId,
			t.sensor_name as sensorName,
			t.ext_equipment_code as extEquipmentCode,
			t.time_stamp as timeStamp,
			t.number,
			MAX(t.normal_number) as normalNumber,
			t.fault_number as faultNumber,
			t.online_rate as onlineRate
		FROM
			busi_arrester_sensor_cache t
		LEFT JOIN equipment_temperature_cache e ON t.main_equipment_id = e.main_equipment_id
		where t.main_equipment_id = #{mainEquipmentId}
		GROUP BY t.ext_equipment_code
	</select>
	<select id="findOnlineRateByCode" parameterType="java.lang.String" resultType="com.huatek.busi.vo.EquipmentOnlineRateVo">
		SELECT
		sum(t.number) as number,
		sum(t.normal_number) as normalNumber,
		sum(t.fault_number) as faultNumber
		FROM
		busi_arrester_sensor_cache t
		where t.ext_equipment_code = #{extEquipmentCode}
	</select>
	<select id="finSumNumber" resultType="java.lang.Integer">
		select COUNT(*)
FROM
(SELECT
		  bs.`site_name` siteName,
		  '避雷器设备'  equipmentType,
		  '4' type,
		  st.`name`  equipmentName,
		  bas.equipment_type pendantType,
		  ast.name pendantName,
		  bas.`sensor_name` sensorName,
		  bas.`sensor_code` sensorCode,
		  bas.sensor_brand sensorBrand,
		  bas.manufactor,
		  bas.position_des positionDes,
		  bas.production_date productionDate,
		  bas.use_date useDate,
		  bas.`status`,
		  bas.equipment_id equipmentId,
		  st.id mainEquipmentId
		FROM
		  busi_arrester_sensor bas
		  LEFT JOIN busi_site bs ON bas.`site_id` = bs.`id`
		  LEFT JOIN busi_arrester_sub_tree ast ON ast.`id` = bas.`equipment_id`
		  LEFT JOIN busi_site_tree st ON st.`id` = ast.`equipment_id`
		WHERE  bas.status = '1' AND st.`status` = '1' and bs.status = '1' and ast.status = '1' AND bas.`sensor_code` != '' AND bas.`sensor_code` is not null
)temp
	</select>
    <select id="findSumSensorNumber" resultType="com.huatek.busi.entity.EquipmentSumSensorCache">
		SELECT
		id,
		site_id as siteId,
		number,
		normal_number as normalNumber,
		fault_number as faultNumber,
		type
	FROM
		`busi_equipment_sensor_cache`
	where
	 type='04'
	</select>
    <update id="updateArresterSensor" parameterType="com.huatek.busi.entity.EquipmentSensorNumber">
		UPDATE busi_arrester_sensor_cache
		<set >
			<if test="number != null" >
				number = #{number},
			</if>
			<if test="normalNumber != null" >
				normal_number = #{normalNumber},
			</if>
			<if test="faultNumber != null" >
				fault_number = #{faultNumber},
			</if>
			<if test="onlineRate != null" >
				online_rate = #{onlineRate},
			</if>
		</set>
		WHERE
		sensor_id = #{sensorId}
	</update>
	<insert id="addArresterSensor" parameterType="com.huatek.busi.entity.EquipmentSensorNumber">
		 insert into
         busi_arrester_sensor_cache
        (
          `id` ,
          `sensor_id`,
          `time_stamp` ,
          `sensor_name` ,
          `ext_equipment_code`,
           `main_equipment_id` ,
          `number`,
          `normal_number` ,
          `fault_number`,
          `online_rate`
        )
        VALUES
        (
        #{id},
        #{sensorId},
        #{timeStamp},
        #{sensorName},
        #{extEquipmentCode},
        #{mainEquipmentId},
        #{number},
        #{normalNumber},
        #{faultNumber},
        #{onlineRate}
        )
	</insert>

</mapper>