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


	<select id="findTransformerNumber" parameterType="java.lang.String" resultType="com.huatek.busi.entity.EquipmentSensorNumber">
SELECT
	sensor_id as sensorId,
	equipment_id as equipmentId,
	main_equipment_id as mainEquipmentId,
	sensor_name as sensorName,
	ext_equipment_code as extEquipmentCode,
	time_stamp as timeStamp
FROM
	equipment_temperature_cache
where main_equipment_id = #{mainEquipmentId}
</select>

	<select id="findTransformerSensorCache" parameterType="java.lang.String" resultType="com.huatek.busi.entity.EquipmentSensorNumber">
		SELECT
		t.sensor_id as sensorId,
		t.time_stamp as timeStamp,
		t.sensor_name as sensorName,
		t.ext_equipment_code as extEquipmentCode,
		t.main_equipment_id as mainEquipmentId,
		t.number as number,
		t.normal_number as normalNumber,
		t.fault_number as faultNumber,
		t.online_rate as onlineRate
		FROM
		busi_transformer_sensor_cache t
		left join busi_transformer_sensor sensor
		on t.sensor_id = sensor.sensor_code
		left join busi_transformer_sub_tree stree
		on sensor.equipment_id = stree.id
		left join busi_site_tree tree
		on stree.equipment_id = tree.id
		where
		t.ext_equipment_code = #{extEquipmentCode}
		and sensor.status = '1'
		and stree.status = '1'
		and tree.status = '1'
		group by t.sensor_id
	</select>

	<select id="findArresterSensorCache" parameterType="java.lang.String" resultType="com.huatek.busi.entity.EquipmentSensorNumber">
		SELECT
		t.sensor_id as sensorId,
		t.time_stamp as timeStamp,
		t.sensor_name as sensorName,
		t.ext_equipment_code as extEquipmentCode,
		t.main_equipment_id as mainEquipmentId,
		t.number as number,
		t.normal_number as normalNumber,
		t.fault_number as faultNumber,
		t.online_rate as onlineRate
		FROM
		busi_arrester_sensor_cache t
		left join busi_arrester_sensor sensor
		on t.sensor_id = sensor.sensor_code
		left join busi_arrester_sub_tree stree
		on sensor.equipment_id = stree.id
		left join busi_site_tree tree
		on stree.equipment_id = tree.id
		where
		t.ext_equipment_code = #{extEquipmentCode}
		and sensor.status = '1'
		and stree.status = '1'
		and tree.status = '1'
		group by t.sensor_id
	</select>

	<select id="findGisSensorCache" parameterType="java.lang.String" resultType="com.huatek.busi.entity.EquipmentSensorNumber">
		SELECT
		t.sensor_id as sensorId,
		t.time_stamp as timeStamp,
		t.sensor_name as sensorName,
		t.ext_equipment_code as extEquipmentCode,
		t.main_equipment_id as mainEquipmentId,
		t.number as number,
		t.normal_number as normalNumber,
		t.fault_number as faultNumber,
		t.online_rate as onlineRate
		FROM
		busi_gis_sensor_cache t
		left join busi_gis_sensor sensor
		on t.sensor_id = sensor.sensor_code
		left join busi_gis_sub_tree stree
		on sensor.equipment_id = stree.id
		left join busi_site_tree tree
		on stree.equipment_id = tree.id
		where
		t.ext_equipment_code = #{extEquipmentCode}
		and sensor.status = '1'
		and stree.status = '1'
		and tree.status = '1'
		group by t.sensor_id
	</select>

	<select id="findCwgSensorCache" parameterType="java.lang.String" resultType="com.huatek.busi.entity.EquipmentSensorNumber">
		SELECT
		t.sensor_id as sensorId,
		t.time_stamp as timeStamp,
		t.sensor_name as sensorName,
		t.ext_equipment_code as extEquipmentCode,
		t.main_equipment_id as mainEquipmentId,
		t.number as number,
		t.normal_number as normalNumber,
		t.fault_number as faultNumber,
		t.online_rate as onlineRate
		FROM
		busi_kgg_sensor_cache t
		left join busi_temper_point sensor
		on t.sensor_id = sensor.sensor_code
		left join busi_site_tree tree
		on sensor.equipment_id = tree.id
		where
		1 = 1
		and sensor.status = '1'
		and tree.status = '1'
		group by t.sensor_id
	</select>

	<select id="findCableSensorCache" parameterType="java.lang.String" resultType="com.huatek.busi.entity.EquipmentSensorNumber">
		SELECT
		t.sensor_id as sensorId,
		t.time_stamp as timeStamp,
		t.sensor_name as sensorName,
		t.ext_equipment_code as extEquipmentCode,
		t.main_equipment_id as mainEquipmentId,
		t.number as number,
		t.normal_number as normalNumber,
		t.fault_number as faultNumber,
		t.online_rate as onlineRate
		FROM
		busi_cable_sensor_cache t
		left join busi_cable_sensor sensor
		on t.sensor_id = sensor.sensor_code
		left join busi_cable_sub_tree stree
		on sensor.equipment_id = stree.id
		left join busi_site_tree tree
		on stree.equipment_id = tree.id
		where
		t.ext_equipment_code = #{extEquipmentCode}
		and sensor.status = '1'
		and stree.status = '1'
		and tree.status = '1'
		group by t.sensor_id
	</select>

	<select id="findGeneratorSensorCache" parameterType="java.lang.String" resultType="com.huatek.busi.entity.EquipmentSensorNumber">
		SELECT
		t.sensor_id as sensorId,
		t.time_stamp as timeStamp,
		t.sensor_name as sensorName,
		t.ext_equipment_code as extEquipmentCode,
		t.main_equipment_id as mainEquipmentId,
		t.number as number,
		t.normal_number as normalNumber,
		t.fault_number as faultNumber,
		t.online_rate as onlineRate
		FROM
		busi_generator_sensor_cache t
		left join busi_generator_sensor sensor
		on t.sensor_id = sensor.sensor_code
		left join busi_generator_sub_tree stree
		on sensor.equipment_id = stree.id
		left join busi_site_tree tree
		on stree.equipment_id = tree.id
		where
		t.ext_equipment_code = #{extEquipmentCode}
		and sensor.status = '1'
		and stree.status = '1'
		and tree.status = '1'
		group by t.sensor_id
	</select>


	<select id="findTransformerStatus" parameterType="java.lang.String" resultType="com.huatek.busi.entity.TransformerStatus">
select
	t.site_id as siteId,
	t.main_equipment_id as mainEquipmentId,
	t.`status`,
	t.transformer_name as transformerName,
	w.warning_type as warningType
FROM
	busi_offline_transformer_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="findSumTransformerNumber" parameterType="java.lang.String" resultType="com.huatek.busi.entity.EquipmentSumNumber">
SELECT
		count(*) as sumNumber
	FROM
		busi_offline_transformer_brand t
	LEFT JOIN busi_transformer_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_transformer_brand t
		LEFT JOIN busi_transformer_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 w.warning_type = '1'
		and f.d_time = #{dTime}
	</select>
	<select id="finSumNumber" parameterType="java.lang.String" resultType="java.lang.Integer" >
	select COUNT(*)
FROM
(SELECT
		  bs.`site_name` siteName,
		  '变压器设备'  equipmentType,
		  '1' type,
		  st.`name`  equipmentName,
		  ts.equipment_type pendantType,
		  tst.name pendantName,
		  ts.`sensor_name` sensorName,
		  ts.`sensor_code` sensorCode,
		  ts.sensor_brand sensorBrand,
		  ts.manufactor,
		  ts.position_des positionDes,
		  ts.production_date productionDate,
		  ts.use_date useDate,
		  ts.`status`,
		  ts.equipment_id equipmentId,
		  st.id mainEquipmentId
		FROM
		  busi_transformer_sensor ts
		  LEFT JOIN busi_site bs ON ts.`site_id` = bs.`id`
		  LEFT JOIN busi_transformer_sub_tree tst ON tst.`id` = ts.`equipment_id`
		  LEFT JOIN busi_site_tree st ON st.`id` = tst.`equipment_id`
		WHERE  ts.status = '1'
		AND st.`status` = '1'
		AND ts.`sensor_code` != '' AND ts.`sensor_code` is not null
)temp
	</select>
	<select id="finFaultNumber" parameterType="java.lang.String" resultType="java.lang.Integer" >
	SELECT
		COUNT(*) as sumFault
	  FROM
		busi_offline_transformer_brand t
		LEFT JOIN busi_transformer_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 w.warning_type = '1'
		and f.d_time = #{dTime}
	</select>
	<select id="faultNumber" parameterType="java.lang.String" resultType="java.lang.Integer">
	SELECT

	count(*) AS number

FROM
	busi_offline_transformer_brand t
	LEFT JOIN busi_transformer_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="findIU" parameterType="java.lang.String" resultType="com.huatek.busi.entity.TransformerIU">
	SELECT
		higth_voltage as higthVoltage,
		higth_current as higthCurrent
	FROM iec_bydwork_param
	where eqm_code = #{transformerId}
	order by ins_date desc
	limit 1
	</select>

	<select id="findLoadRate" parameterType="java.lang.String" resultType="com.huatek.busi.entity.TransformerIU">
	SELECT
		load_rate AS loadRate,
		load_time AS loadTime
	FROM
		busi_transformer_load
	WHERE
		main_tree_id = #{transformerId}
	ORDER BY
		load_time DESC
		LIMIT 1
	</select>

	<select id="findTransformerStype" 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_transformer_brand t
	LEFT JOIN iec_bydwork_param i ON t.main_equipment_id = i.eqm_code
	WHERE
		t.main_equipment_id = #{transformerId}
	ORDER BY
		ins_date DESC
	LIMIT 1
	</select>

	<select id="getTime" parameterType="java.lang.String" resultType="com.huatek.busi.entity.SiteUploadCycle">
		SELECT
			*
		FROM `site_upload_cycle`
		WHERE site_id = #{siteId}
	</select>
	<select id="findSiteData" resultType="com.huatek.busi.entity.SiteData">
	SELECT
		id AS id,
		site_name AS siteName,
		create_name AS createName,
		create_time AS createTime
		FROM
	`busi_site`;
	</select>
	<select id="findSesorCodeNumber" resultType="java.math.BigDecimal">
		SELECT
			COUNT(*)
		FROM
			equipment_temperature_cache
		where main_equipment_id = #{mainEquipmentId}
		AND ext_equipment_code = #{sensorCode}
	</select>
	<select id="findSesorCodeNumber1" resultType="java.math.BigDecimal">
		SELECT
		*
		FROM
		equipment_temperature_cache
		where main_equipment_id = #{mainEquipmentId}
		AND ext_equipment_code = #{sensorCode}

	</select>



	<select id="findSenSorCodeId" resultType="com.huatek.busi.entity.EquipmentSensorNumber">
		SELECT
		sensor_id as sensorId,
		equipment_id as equipmentId,
		main_equipment_id as mainEquipmentId,
		sensor_name as sensorName,
		ext_equipment_code as extEquipmentCode,
		time_stamp as timeStamp
	FROM
		equipment_temperature_cache
	where main_equipment_id = #{mainEquipmentId}
	AND ext_equipment_code =#{sensorCode}
	</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_transformer_sensor_cache`;
	</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_transformer_sub_tree t on e.main_equipment_id = t.equipment_id
	where t.status = '1'
	GROUP BY e.main_equipment_id
	</select>
	<select id="findOnlineRate" 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,
		sum(t.normal_number) as normalNumber,
		t.fault_number as faultNumber,
		t.online_rate as onlineRate
	FROM
		busi_transformer_sensor_cache t
	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_transformer_sensor_cache t
		where t.ext_equipment_code = #{extEquipmentCode}
	</select>



	<select id="findCacheType" parameterType="java.lang.String" resultType="com.huatek.busi.entity.EquipmentSumCache">
		SELECT
			id,
			site_id as siteId,
			number,
			normal_number as normalNumber,
			fault_number as faultNumber,
			type
		FROM
			`busi_equipment_sensor_cache`
		where
		type =#{type}
		AND site_id =#{siteId}
	</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,
		normal_ids as normalIds,normal_ids as  faultIds

		FROM
			`busi_equipment_sensor_cache`
		where
		 type='01'
	</select>

	<select id="findSumSensorInfo"  resultType="com.huatek.busi.entity.EquipmentSumSensorCache">
		SELECT
		id,
		site_id as siteId,
		number,
		normal_number as normalNumber,
		fault_number as faultNumber,
		type,
		normal_ids as normalIds,fault_ids as  faultIds

		FROM
		`busi_equipment_sensor_cache`

		<if test="type != null and type != ''" >
			where	type=#{type}
		</if>
	</select>
	<select id="finSumAllNumber" resultType="com.huatek.busi.entity.EquipmentSumSensorCache">
		select
			sum(number) as number,
			sum(normal_number) as normalNumber,
			sum(fault_number) as faultNumber
		FROM
		busi_equipment_sensor_cache
	</select>

	<select id="findMainTreeId" resultType="com.huatek.busi.entity.BusiSiteTree">
		SELECT
			id,
			name
		FROM
			busi_site_tree
		WHERE
			`status` = 1
			AND parent_id = (
			SELECT
				id
			FROM
				busi_site_tree
			WHERE
			`status` = 1
			AND equipment_type = 1)
	</select>

	<select id="findAllLoadRate" resultType="com.huatek.busi.entity.TransformerLoadRate">
		SELECT
			A.load_rate AS loadRate,
			A.load_time AS loadTime,
			A.main_tree_id AS mainTreeId,
			B.`name` AS name
		FROM
			busi_transformer_load A
			LEFT JOIN busi_site_tree B ON A.main_tree_id = B.id
		WHERE
			A.main_tree_id = #{mainEquipmentId}
			AND A.load_time = #{date}
	</select>

	<update id="updateTransformerSensor" parameterType="com.huatek.busi.entity.EquipmentSensorNumber">
		UPDATE busi_transformer_sensor_cache
		<set >
			<if test="sensorId != null" >
				sensor_id = #{sensorId},
			</if>
			<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="addTransformerSensor" parameterType="com.huatek.busi.entity.EquipmentSensorNumber">
		 insert into
         busi_transformer_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>
	<insert id="addNumber" parameterType="com.huatek.busi.entity.EquipmentSumCache">
		 insert into
         busi_equipment_sensor_cache
        (
          `id` ,
          `site_id` ,
          `number`,
          `normal_number` ,
          `fault_number`,
          `type`
        )
        VALUES
        (
        #{id},
        #{siteId},
        #{sumNumber},
        #{sumNormal},
        #{sumFault},
        #{type}
        )
	</insert>
	<update id="updateNumber" parameterType="com.huatek.busi.entity.EquipmentSumNumber">
		UPDATE busi_equipment_sensor_cache t
		<set >
			<if test="sumNumber != null" >
				t.number = #{sumNumber},
			</if>
			<if test="sumNormal != null" >
				t.normal_number = #{sumNormal},
			</if>
			<if test="sumFault != null" >
				t.fault_number = #{sumFault},
			</if>
			<if test="normalIds != null" >
				t.normal_ids = #{normalIds},
			</if>
			<if test="faultIds != null" >
				t.fault_ids = #{faultIds},
			</if>
		</set>
		WHERE
		t.type = #{type}
	</update>

	<select id="selectGeneratorSensorDeviceNumber" resultType="integer">
		select count(*)
		from busi_generator_sensor sensor
		left join busi_generator_sub_tree stree on sensor.equipment_id = stree.id
		left join busi_site_tree tree on stree.equipment_id = tree.id
		where sensor.status = '1' and stree.status = '1' and tree.status = '1'
	</select>
</mapper>
