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

	<sql id="Base_Column_List">
		id as id,
		sensor_id as sensorId,
		site_id as siteId,
		main_tree_id as mainTreeId,
		sub_tree_id as subTreeId,
		sub_equipment_id as subEquipmentId,
		main_equipment_id as mainEquipmentId,
		eq_type as eqType,
		gz_type as gzType,
		must_count as mustCount,
		real_count as realCount,
		rate as rate,
		d_time as dTime,
		created_time as createdTime,
		modified_time as modifiedTime
	</sql>

	<select id="getFailureRate" resultType="com.huatek.busi.entity.FailureRate">
		select
			<include refid="Base_Column_List"/>
		from
			failure_rate
		where site_id = #{siteId}
			AND main_tree_id = #{mainTreeId}
			AND sensor_id = #{sensorCode}
			AND d_time = #{date}
		limit 1
	</select>

	<select id="getLossRateCount" resultType="java.util.Map">
		SELECT
		  SUM(t.real_count) AS totalRealCount,
		  SUM(t.must_count) AS totalMustCount
		FROM failure_rate t
		<where>
			<if test="siteId != null and siteId != ''">
				AND t.site_id = #{siteId}
			</if>
			<if test="mainTreeId != null and mainTreeId != ''">
				AND t.main_tree_id = #{mainTreeId}
			</if>
			<if test="subTreeId != null and subTreeId != ''">
				AND t.sub_tree_id = #{subTreeId}
			</if>
			<if test="mainEquipmentId != null and mainEquipmentId != ''">
				AND t.main_equipment_id = #{mainEquipmentId}
			</if>
			<if test="subEquipmentId != null and subEquipmentId != ''">
				AND t.sub_equipment_id = #{subEquipmentId}
			</if>
			<if test="eqType != null and eqType != ''">
				AND t.eq_type = #{eqType}
			</if>
			<if test="gzType != null and gzType != ''">
				AND t.gz_type = #{gzType}
			</if>
			<if test="sensorCode != null and sensorCode != ''">
				AND t.sensor_id = #{sensorCode}
			</if>
			<if test="startTime != null">
				AND t.d_time &gt;= #{startTime}
			</if>
			<if test="endTime != null">
				AND t.d_time &lt; #{endTime}
			</if>
		</where>
	</select>
</mapper>