<?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.GeneratorNumberMapper">
	<select id="findSumGeneratorNumber" parameterType="java.lang.String" resultType="com.huatek.busi.entity.EquipmentSumNumber">
	SELECT
		COUNT(*) AS sumNumber
	FROM
		busi_offline_generator_brand t
	LEFT JOIN busi_generator_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.equipment_id = w.sub_equipment_id
	WHERE
		tt. STATUS = '1'
	AND tt.equipment_type LIKE '___'
	AND f.d_time = #{dTime}
	UNION ALL
	SELECT
		COUNT(*) AS sumNumber
	FROM
		busi_offline_generator_brand t
	LEFT JOIN busi_generator_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.equipment_id = w.sub_equipment_id
	WHERE
		tt. STATUS = '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(*) AS sumNumber
	FROM
		busi_offline_generator_brand t
	LEFT JOIN busi_generator_sub_tree tt ON t.site_id = tt.site_id
	AND t.main_equipment_id = tt.equipment_id
	LEFT JOIN busi_generator_sensor s on tt.id = s.equipment_id
	WHERE
		tt. STATUS = '1'
	and	s.`status` = '1'
	AND tt.equipment_type LIKE '___'
	</select>

	<select id="findFaultNumber" parameterType="java.lang.String" resultType="java.lang.Integer">
	SELECT
		COUNT(*) AS sumNumber
	FROM
		busi_offline_generator_brand t
	LEFT JOIN busi_generator_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.equipment_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="findGeneratorStatus" parameterType="java.lang.String" resultType="com.huatek.busi.entity.GeneratorStatus">
		select
		t.site_id as siteId,
		t.main_equipment_id as mainEquipmentId,
		t.`status`,
		t.generator_name as generatorName,
		w.warning_type as warningType
		FROM
		busi_offline_generator_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="findOtherEquNumber" parameterType="java.lang.String" resultType="int">
		select
		count(*)
		FROM
		busi_offline_generator_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'
	</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_generator_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 = #{findGeneratorId}
		ORDER BY
		ins_date DESC
		LIMIT 1
	</select>

	<select id="findGeneratorSenSor" 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_generator_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="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='06'
	</select>

	<select id="findOtherSensorSumNumber" resultType="int">
		SELECT
		count(*)
		FROM
		`busi_other_sensor` t
		left join busi_other_sub_tree stree
		on t.equipment_id  = stree.id
		left join busi_site_tree tree
		on stree.equipment_id = tree.id
		WHERE
		t.`status` = '1'
		and stree.status = '1'
		and tree.status = '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_generator_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_generator_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_generator_sensor_cache`;
	</select>

	<update id="updatGeneratorSensor" parameterType="com.huatek.busi.entity.EquipmentSensorNumber">
		UPDATE busi_generator_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="addGeneratorSensor" parameterType="com.huatek.busi.entity.EquipmentSensorNumber">
		insert into
		busi_generator_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>

	<select id="finSumNumber" resultType="java.lang.Integer">
		select COUNT(*)
		FROM
		(SELECT
		bs.`site_name` siteName,
		'发电机设备'  equipmentType,
		'6' type,
		st.`name`  equipmentName,
		gs.equipment_type pendantType,
		gst.name pendantName,
		gs.`sensor_name` sensorName,
		gs.`sensor_code` sensorCode,
		gs.sensor_brand sensorBrand,
		gs.manufactor,
		gs.position_des positionDes,
		gs.production_date productionDate,
		gs.use_date useDate,
		gs.`status`,
		gs.equipment_id equipmentId,
		st.id mainEquipmentId
		FROM
		busi_generator_sensor gs
		LEFT JOIN busi_site bs ON gs.`site_id` = bs.`id`
		LEFT JOIN busi_generator_sub_tree gst ON gst.`id` = gs.`equipment_id`
		LEFT JOIN busi_site_tree st ON st.`id` = gst.`equipment_id`
		WHERE  gs.status = '1' AND st.`status` = '1' AND gs.sensor_code!='' AND gs.`sensor_code` is not null
		)temp
	</select>


	<select id="findSesorCodeNumber" resultType="java.math.BigDecimal">
		SELECT
		COUNT(*)
		FROM
		equipment_temperature_cache
		where main_equipment_id = #{mainEquipmentId}
		AND ext_equipment_code = #{extEquipmentCode}
	</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_generator_sensor_cache t
		where t.ext_equipment_code = #{extEquipmentCode}
	</select>
</mapper>