<?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.CubicleNumberMapper">
<select id="findCubicleStart"  parameterType="java.lang.String" resultType="com.huatek.busi.entity.CubicleStatus">
SELECT
	t.site_id AS siteId,
	t.main_equipment_id AS mainEquipmentId,
	t.switchgear_name AS switchgearName,
	w.warning_type AS warningType
FROM
	busi_offline_switchgear_brand t
LEFT JOIN failure_rate f ON f.main_equipment_id = t.main_equipment_id
LEFT JOIN busi_site_tree tt ON tt.id = t.main_equipment_id
LEFT JOIN busi_eqm_warning w ON t.id = w.sub_equipment_id
WHERE
	t.`status` = '1'
AND tt.`status` = '1'




</select>

	<select id="findCubucleNumber" parameterType="java.lang.String" resultType="com.huatek.busi.entity.CubicleStatus">
SELECT
	t.site_id AS siteId,
	t.main_equipment_id AS mainEquipmentId,
	tt.id AS tId,
	p.sensor_name AS sensorName,
	SUM(f.real_count) AS totalRealCount,
	SUM(f.must_count) AS totalMustCount,
	w.warning_type AS warningType,
	count(*) as NUMBER
FROM
	busi_offline_switchgear_brand t
LEFT JOIN busi_temper_point p on t.main_equipment_id = p.middle_id
LEFT JOIN failure_rate f on f.sensor_id = p.sensor_code
LEFT JOIN busi_site_tree tt on tt.id = t.main_equipment_id
LEFT JOIN busi_eqm_warning w on t.id = w.sub_equipment_id
where
	t.site_id = #{siteId}
AND t.main_equipment_id = #{mainEquipmentId}
AND t.`status` = '1'
AND p.status ='1'
AND f.d_time = #{dTime}
GROUP BY p.sensor_name
	</select>

	<select id="findSumCubucleNumber" parameterType="java.lang.String" resultType="com.huatek.busi.entity.EquipmentSumNumber">
	SELECT
		count(*) as sumFault
	FROM
		busi_temper_point t
	LEFT JOIN failure_rate f on f.sensor_id =t.sensor_code
	LEFT JOIN busi_site_tree tt on tt.id = t.equipment_id
	LEFT JOIN busi_eqm_warning w on t.id = w.sub_equipment_id
	where
		t.`status` = '1'
	AND f.d_time =#{dTime}
	UNION ALL
	SELECT
		count(*) as NUMBER
	FROM
		busi_temper_point t
	LEFT JOIN failure_rate f on f.sensor_id =t.sensor_code
	LEFT JOIN busi_site_tree tt on tt.id = t.equipment_id
	LEFT JOIN busi_eqm_warning w on t.id = w.sub_equipment_id
	where
	t.`status` = '1'
	AND w.warning_type='1'
	AND f.d_time =#{dTime}
	</select>

	<select id="findSumNumber"  parameterType="java.lang.String" resultType="java.lang.Integer">
SELECT
		count(*) as sumFault
	FROM
		busi_temper_point
	where
		`status` = '1'
 AND sensor_code !=''
</select>
	<select id="findFaultNumber"  parameterType="java.lang.String" resultType="java.lang.Integer">
	SELECT
		count(*) as NUMBER
	FROM
		busi_temper_point t
	LEFT JOIN failure_rate f on f.sensor_id =t.sensor_code
	LEFT JOIN busi_site_tree tt on tt.id = t.middle_id
	LEFT JOIN busi_eqm_warning w on t.id = w.sub_equipment_id
	where
	t.`status` = '1'
	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_temper_point t
	LEFT JOIN failure_rate f on f.sensor_id =t.sensor_code
	LEFT JOIN busi_site_tree tt on tt.id = t.middle_id
	LEFT JOIN busi_eqm_warning w on t.id = w.sub_equipment_id
		where
		tt.id = #{tId}
	AND	t.`status` = '1'
	AND w.warning_type = '1'
	AND f.d_time =#{dTime}
	</select>

	<select id="findCubiclePoint" parameterType="java.lang.String" resultType="com.huatek.busi.entity.RptTermperRecord">
		SELECT
			site_id AS siteId,
			eqm_id AS eqmId,
			sensor_id AS sensorId,
			temper AS temper,
			phase AS phase,
			position AS position,
			d_time AS dTime
		FROM
			`rpt_termper_record`
		WHERE
		 site_id = #{siteId}
		AND eqm_id = #{eqmId}
		ORDER BY temper DESC
	</select>
	<select id="findTemperRate" parameterType="com.huatek.busi.entity.RptTemperRate" resultType="com.huatek.busi.entity.RptTemperRate">
		SELECT
		site_id as siteId,
		eqm_id as eqmId,
		bad_rate as badRate,
		fault_rate as faultRate,
		rate_time as rateTime,
		create_time as createTime
		FROM
			rpt_temper_rate
		WHERE
			site_id = #{siteId}
		AND eqm_id = #{eqmId}
		AND rate_time =#{dBefore}
	</select>

	<select id="findTemper" parameterType="java.lang.String" resultType="com.huatek.busi.entity.RptTemper">
		SELECT
			site_id as siteId,
			eqm_id AS eqmid,
			box_temper as boxTemper,
			box_sensor_id as boxSensorId,
			box_outside_temper as boxOutsideTemper,
			box_outside_sensor_id as boxOutsideSensorId,
			a_i_temper as aITemper,
			a_i_sensor_id AS aISensorId,
			a_o_temper AS aOTemper,
			a_o_sensor_id AS aOSensorId,
			b_i_temper AS bITemper,
			b_i_sensor_id AS bISensorId,
			b_o_temper AS bOTemper,
			b_o_sensor_id AS bOSensorId,
			c_i_temper AS cITemper,
			c_i_sensor_id AS cISensorId,
			c_o_temper AS cOTemper,
			c_o_sensor_id AS cOSensorId,
			create_time AS createTime
		FROM
			rpt_temper
		WHERE
			site_id = #{siteId}
		AND eqm_id = #{eqmId}
		ORDER BY
			create_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_temper_point 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_temper_point 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_kgg_sensor_cache`;
	</select>
	<select id="findCubicleSensor" 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_kgg_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_kgg_sensor_cache t
		where t.ext_equipment_code = #{extEquipmentCode}
	</select>

	<select id="findOnlineRate" 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_kgg_sensor_cache t
	</select>

	<select id="findAllSesorCodeNumber" parameterType="java.lang.String" resultType="java.math.BigDecimal">

	SELECT
			COUNT(*)
		FROM
			equipment_temperature_cache
		where main_equipment_id = #{mainEquipmentId}
	</select>
    <select id="finSumNumber" resultType="java.lang.Integer">
		select COUNT(*)
FROM
(SELECT
		  bs.`site_name` siteName,
		  '开关柜设备'  equipmentType,
		  '3' type,
		  st.`name`  equipmentName,
		  ss.equipment_type pendantType,
		  '开关柜测温点' pendantName,
		  ss.`sensor_name` sensorName,
		  ss.`sensor_code` sensorCode,
		  ss.sensor_brand sensorBrand,
		  ss.manufactor,
		  ss.position_des positionDes,
		  ss.production_date productionDate,
		  ss.use_date useDate,
		  ss.`status`,
		  ss.equipment_id equipmentId,
		  st.id mainEquipmentId
		FROM
		  busi_temper_point ss
		  LEFT JOIN busi_site bs ON ss.`site_id` = bs.`id`
		  LEFT JOIN busi_site_tree st ON st.`id` = ss.`equipment_id`
		WHERE  ss.status = '1' AND st.`status` = '1' AND ss.sensor_code is not null AND ss.sensor_code != ''
)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='05'
	</select>
    <update id="updateCubliceSensor" parameterType="com.huatek.busi.entity.EquipmentSensorNumber">
		UPDATE busi_kgg_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="addCubliceSensor" parameterType="com.huatek.busi.entity.EquipmentSensorNumber">
		 insert into
         busi_kgg_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>