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

	<sql id="Base_Column_List">
		id as id,
		d_time as dTime,
		phase as phase,
		discharge_capacity as dischargeCapacity,
		discharge_position as dischargePosition,
		dichange_count as dichangeCount,
		cac_id as cacId,
		sensor_id as sensorId,
		peak as peak,
		jf_id as jfId,
		site_id as siteId,
		site_eqm_code as siteEqmCode,
		created_time as createdTime,
		modified_time as modifiedTime,
		main_tree_id as mainTreeId,
		sub_tree_id as subTreeId
	</sql>

	<select id="selectTransformerPdCachePage" resultType="com.huatek.busi.vo.TransformerPdCacheVo">
		SELECT
			t.id,
			t.d_time AS dTime,
			t.phase,
			t.discharge_capacity AS dischargeCapacity,
			t.discharge_position AS dischargePosition,
			t.dichange_count AS dichangeCount,
			t.cac_id AS cacId,
			t.sensor_id AS sensorId,
			t.peak,
			t.dsch_type as dschType,
			t.site_id AS siteId,
			t.site_eqm_code AS siteEqmCode,
			t.created_time AS createdTime,
			t.modified_time AS modifiedTime,
			t.main_tree_id AS mainTreeId,
			t.sub_tree_id AS subTreeId,
			ext.id as equipmentId,
			ext.equipment_name AS equipmentName,
			sensor.sensor_brand AS equipmentBrand,
			sensor.manufactor,
			sensor.use_date AS useDate,
			sensor.position_des AS installLocation,
			pd.pic_file as matlabImage
		FROM
			transformer_pd_cache t
				LEFT JOIN busi_transformer_sensor sensor ON t.sensor_id = sensor.sensor_code AND sensor.status = '1'
				LEFT JOIN busi_external_equipment ext ON t.sub_tree_id = ext.equipment_id AND ext.external_type = '1'
				LEFT JOIN iec_pd_file_header pd ON t.transformer_pd_id = pd.pd_id
		<where>
			<if test="equipmentName != null and equipmentName != ''">
				and ext.equipment_name like CONCAT("%",#{equipmentName},"%")
			</if>
			<if test="equipmentBrand != null and equipmentBrand != ''">
				and ext.equipment_brand like CONCAT("%",#{equipmentBrand},"%")
			</if>
			<if test="dTime != null">
				and DATE_FORMAT(t.d_time,'%Y-%m-%d') &gt;= DATE_FORMAT(#{dTime},'%Y-%m-%d')
			</if>
			<if test="dTimeEnd != null">
				and DATE_FORMAT(t.d_time,'%Y-%m-%d') &lt;= DATE_FORMAT(#{dTimeEnd},'%Y-%m-%d')
			</if>
		</where>
		ORDER BY t.created_time DESC
	</select>

	<select id="getTransformerPdCache" resultType="com.huatek.busi.entity.TransformerPdCache">
		SELECT
        	<include refid="Base_Column_List"/>
        FROM
        	transformer_pd_cache t
        <where>
        	<if test="sensorId != null and sensorId != ''">
	             and t.sensor_id = #{sensorId}
	        </if>
	        <if test="siteId != null and siteId != ''">
	             and t.site_id = #{siteId}
	        </if>
	        <if test="subTreeId != null and subTreeId != ''">
	             and t.sub_tree_id = #{subTreeId}
	        </if>
        </where>
        limit 1
	</select>
</mapper>