<?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.CableNumberMapper">
<select id="findCableNumber" 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(name) AS number,
	w.warning_type as waringType,
	tt.status
FROM
	busi_offline_cable_brand t
	LEFT JOIN busi_cable_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="findCableStatus" parameterType="java.lang.String" resultType="com.huatek.busi.entity.CableStatus">
select
	t.site_id as siteId,
	t.main_equipment_id as mainEquipmentId,
	t.`status`,
	t.cable_name as cableName,
	w.warning_type as warningType
FROM
	busi_offline_cable_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="findCableSumNumber" parameterType="java.lang.String" resultType="com.huatek.busi.entity.EquipmentSumNumber" >
		SELECT
		COUNT(*) as sumNumber
		  FROM
			busi_offline_cable_brand t
			LEFT JOIN busi_cable_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 sumNumber
		  FROM
			busi_offline_cable_brand t
			LEFT JOIN busi_cable_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,
		  '7' type,
		  st.`name`  equipmentName,
		  cs.equipment_type pendantType,
		  cst.name pendantName,
		  cs.`sensor_name` sensorName,
		  cs.`sensor_code` sensorCode,
		  cs.sensor_brand sensorBrand,
		  cs.manufactor,
		  cs.position_des positionDes,
		  cs.production_date productionDate,
		  cs.use_date useDate
		FROM
		  busi_cable_sensor cs
		  LEFT JOIN busi_site bs ON cs.`site_id` = bs.`id`
		  LEFT JOIN busi_cable_sub_tree cst ON cst.`id` = cs.`equipment_id`
		  LEFT JOIN busi_site_tree st ON st.`id` = cst.`equipment_id`
		WHERE cs.`site_id` =  #{siteId}
		AND cs.status = '1')temp
	</select>
	<select id="findFaultNumber" parameterType="java.lang.String" resultType="java.lang.Integer">
			SELECT
		COUNT(*) as sumNumber
		  FROM
			busi_offline_cable_brand t
			LEFT JOIN busi_cable_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_cable_brand t
		LEFT JOIN busi_cable_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_cable_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 = #{findCableId}
		ORDER BY
			ins_date 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_cable_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_cable_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_cable_sensor_cache`;
	</select>
    <select id="findCableSenSor" 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_cable_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_cable_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,
		  '7' type,
		  st.`name`  equipmentName,
		  cs.equipment_type pendantType,
		  cst.name pendantName,
		  cs.`sensor_name` sensorName,
		  cs.`sensor_code` sensorCode,
		  cs.sensor_brand sensorBrand,
		  cs.manufactor,
		  cs.position_des positionDes,
		  cs.production_date productionDate,
		  cs.use_date useDate,
		  cs.`status`,
		  cs.equipment_id equipmentId,
		  st.id mainEquipmentId
		FROM
		  busi_cable_sensor cs
		  LEFT JOIN busi_site bs ON cs.`site_id` = bs.`id`
		  LEFT JOIN busi_cable_sub_tree cst ON cst.`id` = cs.`equipment_id`
		  LEFT JOIN busi_site_tree st ON st.`id` = cst.`equipment_id`
		WHERE cs.status = '1' AND st.`status` = '1' AND cs.sensor_code != '' AND cs.`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='03'
	</select>
    <update id="updateCableSensor" parameterType="com.huatek.busi.entity.EquipmentSensorNumber">
		UPDATE busi_cable_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="addCableSensor" parameterType="com.huatek.busi.entity.EquipmentSensorNumber">
		 insert into
         busi_cable_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>