<?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.BusiCableThresholdMapper">
    <!-- 通用查询映射 -->
    <sql id="busiCable">
		SELECT
			id,
			site_id as siteId,
			main_tree_id as mainTreeId,
			sensor_code as sensorCode,
			run_state as runState,
			elect_threshold_val as electThresholdVal,
			elect_prewarning_val as electPrewarningVal,
			relative_growth_day_val as relativeGrowthDayVal,
			relative_growth_month_val as relativeGrowthMonthVal,
			relative_growth_prewarning_day_val as relativeGrowthPrewarningDayVal,
			relative_growth_prewarning_month_val as relativeGrowthPrewarningMonthVal,
			created_time as createdTime,
			creator,
			modified_time as modifiedTime,
			modifier
		FROM
		busi_cable_threshold
	</sql>

	<!-- 查询阈值信息 -->
	<select id="getCableThresholdVo" resultType="com.huatek.busi.vo.cable.BusiCableThresholdVo">
		SELECT
		id,
		site_id as siteId,
		main_tree_id as mainTreeId,
		sensor_code as sensorCode,
		run_state as runState,
		elect_threshold_val as electThresholdVal,
		elect_prewarning_val as electPrewarningVal,
		relative_growth_day_val as relativeGrowthDayVal,
		relative_growth_month_val as relativeGrowthMonthVal,
		relative_growth_prewarning_day_val as relativeGrowthPrewarningDayVal,
		relative_growth_prewarning_month_val as relativeGrowthPrewarningMonthVal,
		created_time as createdTime,
		creator,
		modified_time as modifiedTime,
		modifier
		FROM
		busi_cable_threshold
		<where>
			<if test="mainTreeId != null and mainTreeId != ''">
				AND main_tree_id = #{mainTreeId}
			</if>
			<if test="runState != null and runState != ''">
				AND run_state = #{runState}
			</if>
			<if test="sensorCode != null and sensorCode != ''">
				AND sensor_code = #{sensorCode}
			</if>
		</where>
	</select>

	<select id="getGrowthRate" resultType="com.huatek.busi.vo.cable.BusiCableThresholdVo">
		SELECT
			id,
			site_id as siteId,
			main_tree_id as mainTreeId,
			sensor_code as sensorCode,
			run_state as runState,
			elect_threshold_val as electThresholdVal,
			elect_prewarning_val as electPrewarningVal,
			relative_growth_day_val as relativeGrowthDayVal,
			relative_growth_month_val as relativeGrowthMonthVal,
			relative_growth_prewarning_day_val as relativeGrowthPrewarningDayVal,
			relative_growth_prewarning_month_val as relativeGrowthPrewarningMonthVal,
			created_time as createdTime,
			creator,
			modified_time as modifiedTime,
			modifier
		FROM
			busi_cable_threshold
		where sensor_code = #{sensorCode} and (run_state='' or run_state is null)
	</select>

    <delete id="deleteCableThresholdBySensorCode" parameterType="java.lang.String">
        DELETE FROM busi_cable_threshold
	    WHERE sensor_code = #{sensorCode, jdbcType=VARCHAR}
    </delete>

	<delete id="deleteCableThreshold" parameterType="com.huatek.busi.dto.cable.BusiCableThresholdDto">
		DELETE FROM  busi_cable_threshold
		WHERE sensor_code = #{sensorCode,jdbcType=VARCHAR}
		  and run_state = #{runState,jdbcType=VARCHAR}
	</delete>

	<select id="getCableThreshold" resultType="com.huatek.busi.entity.BusiCableThreshold">
		<include refid="busiCable"></include>
		<where>
			<if test="mainTreeId != null and mainTreeId != ''">
				AND main_tree_id = #{mainTreeId}
			</if>
			<if test="runState != null and runState != ''">
				AND run_state = #{runState}
			</if>
			<if test="runState == null or runState == ''">
				AND run_state is null
			</if>
			<if test="sensorCode != null and sensorCode != ''">
				AND sensor_code = #{sensorCode}
			</if>
		</where>
	</select>

    <!-- 根据sensor编码更新变压器其他阈值 -->
    <update id="updateCableThresholdBySensorCode">
        UPDATE busi_Cable_threshold
        <set>
            <if test="newSensorCode != null and newSensorCode !=''">
                sensor_code = #{newSensorCode,jdbcType=VARCHAR},
            </if>
        </set>
        WHERE sensor_code = #{oldSensorCode,jdbcType=VARCHAR}
    </update>
    <select id="getCableBySensorCodeThreshold" resultType="com.huatek.busi.entity.BusiCableThreshold">
        <include refid="busiCable"></include>
        WHERE
        sensor_code = #{sensorCode} and (run_state='' or run_state is null)
    </select>

	<select id="getCableData" resultType="com.huatek.busi.vo.cable.CableCirculationDataVo">
		SELECT
		t.id,
		sensor.site_id AS siteId,
		t.main_tree_id AS mainTreeId,
		sensor.sensor_code AS sensorCode,
		threshold.run_state AS runState,
		t.phase,
		t.total_core_current AS totalCoreCurrent,
		t.time_stamp AS modifiedTime,
		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,
		sensor.position as position,
		threshold.elect_threshold_val AS electThresholdVal,
		threshold.elect_prewarning_val AS electPrewarningVal,
		threshold.relative_growth_day_val AS relativeGrowthDayVal,
		threshold.relative_growth_month_val AS relativeGrowthMonthVal,
		threshold.relative_growth_prewarning_day_val AS relativeGrowthPrewarningDayVal,
		threshold.relative_growth_prewarning_month_val AS relativeGrowthPrewarningMonthVal,
		MAX(t.time_stamp)
		FROM
		(select * from busi_cable_sensor where 1=1
		<if test="sensorCode != null and sensorCode != ''">
			and sensor_code = #{sensorCode}
		</if>) sensor
		left join busi_cable_circulation  t on  t.sensor_id = sensor.sensor_code
		LEFT JOIN busi_external_equipment ext ON t.sub_tree_id = ext.equipment_id AND ext.external_type = '7'
		LEFT JOIN busi_cable_threshold threshold ON t.sensor_id = threshold.sensor_code
		<if test="runStatus != null and runStatus != '' and runStatus != '0'">
			and threshold.run_state = #{runStatus}
		</if>
		ORDER BY t.time_stamp desc
	</select>

</mapper>