<?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.cable.BusiOtherThresholdMapper">
    <!-- 通用查询映射 -->
    <sql id="busiOther">
		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_other_threshold
	</sql>

    <sql id="Base_Column_List">
		bos.position_des as positionDes,
		bos.position as position,
		bos.sensor_code as sensorCode,
		bos.sensor_brand as sensorBrand,
		bos.manufactor,
		bos.use_date as useDate,
		bot.run_state as runState,
		bot.elect_threshold_val as thresholdVal
	</sql>
    <!-- 查询阈值信息 -->
    <select id="getOtherThresholdVo" resultType="com.huatek.busi.vo.cable.BusiOtherThresholdVo">
        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_other_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.BusiOtherThresholdVo">
        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_other_threshold
        where sensor_code = #{sensorCode} and (run_state='' or run_state is null)
    </select>

    <select id="getOtherThreshold" resultType="com.huatek.busi.entity.BusiOtherThreshold">
        <include refid="busiOther"></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="sensorCode != null and sensorCode != ''">
                AND sensor_code = #{sensorCode}
            </if>
        </where>
    </select>

    <select id="getCableThreshold" resultType="com.huatek.busi.entity.BusiOtherThreshold">
        <include refid="busiOther"></include>
        WHERE
        sensor_code = #{sensorCode} and (run_state='' or run_state is null)
    </select>

    <select id="selectBySensorCode" resultType="com.huatek.busi.vo.cable.BusiOtherThresholdVo">
        <include refid="busiOther"></include>
        WHERE
        sensor_code = #{sensorCode} and (run_state='' or run_state is null)
    </select>


    <!-- 根据sensor编码更新变压器其他阈值 -->
    <update id="updateOtherThresholdBySensorCode">
        UPDATE busi_other_threshold
        <set>
            <if test="newSensorCode != null and newSensorCode !=''">
                sensor_code = #{newSensorCode,jdbcType=VARCHAR},
            </if>
        </set>
        WHERE sensor_code = #{oldSensorCode,jdbcType=VARCHAR}
    </update>

    <!-- 根据sensor编码删除其他阈值 -->
    <delete id="delOtherThresholdBySensorCode" parameterType="java.lang.String">
	  	DELETE FROM  busi_other_threshold
	    WHERE sensor_code = #{sensorCode,jdbcType=VARCHAR}
     </delete>

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

    <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_other_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 = '5'
			LEFT JOIN busi_other_threshold threshold ON t.sensor_id = threshold.sensor_code
                <if test="runStatus != null and runStatus != '' and runStatus != '0'">
                    and threshold.run_state = #{runStatus}
                </if>

<!--                <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.modified_time, "%Y-%m-%d") &gt;= DATE_FORMAT(#{dTime},"%Y-%m-%d")-->
<!--                </if>-->
<!--                <if test="dTimeEnd != null">-->
<!--                    and DATE_FORMAT(t.modified_time, "%Y-%m-%d") &lt;= DATE_FORMAT(#{dTimeEnd},"%Y-%m-%d")-->
<!--                </if>-->
			ORDER BY t.time_stamp desc
    </select>

    <select id="getCableDataForCable" 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>

        <!--                <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.modified_time, "%Y-%m-%d") &gt;= DATE_FORMAT(#{dTime},"%Y-%m-%d")-->
        <!--                </if>-->
        <!--                <if test="dTimeEnd != null">-->
        <!--                    and DATE_FORMAT(t.modified_time, "%Y-%m-%d") &lt;= DATE_FORMAT(#{dTimeEnd},"%Y-%m-%d")-->
        <!--                </if>-->
        ORDER BY t.time_stamp desc
    </select>


</mapper>
