<?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.BusiEvaMonitorConfigMapper">
	<sql id="Base_Vo_column_list">
		t.id,
		t.monitor_type_id AS monitorTypeId,
		eme.name AS monitorTypeName,
		t.monitor_unit_id AS monitorUnitId,
		emu.name AS monitorUnitName,
		t.sensor_id AS sensorId,
		t.indicators_id AS indicatorsId,
		emui.name AS indicatorsName,
		<!-- t.install_location,
		t.sensor_name,
		t.brand_name, -->
		t.sample_frequency AS sampleFrequency,
		t.mark,
		t.description,
		t.eva_from AS evaFrom,
		t.eva_to AS evaTo,
		t.status,
		t.creator,
		cu.user_name AS creatorName,
		t.created_time AS createdTime,
		t.modifier,
		mu.user_name AS modifierName,
		t.modified_time AS modifiedTime,
		t.type,
		t.equipment_type AS equipmentType,
		CONCAT_WS('', t_sensor.sensor_name, g_sensor.sensor_name, a_sensor.sensor_name, o_sensor.sensor_name, p_sensor.sensor_name) AS sensorName,
		CONCAT_WS('', t_sensor.sensor_brand, g_sensor.sensor_brand, a_sensor.sensor_brand, o_sensor.sensor_brand, p_sensor.sensor_brand) AS brandName,
		CONCAT_WS('', t_sensor.manufactor, g_sensor.manufactor, a_sensor.manufactor, o_sensor.manufactor, p_sensor.manufactor) AS manufactor,
		CONCAT_WS('', t_sensor.position_des, g_sensor.position_des, a_sensor.position_des, o_sensor.position_des, p_sensor.position_des) AS installLocation,
		CONCAT_WS('', t_sensor.use_date, g_sensor.use_date, a_sensor.use_date, o_sensor.use_date, p_sensor.use_date) AS useDate
	</sql>

	<sql id="Base_Vo_column_list4export">
		t.id,
		t.monitor_type_id AS monitorTypeId,
		eme.name AS monitorTypeName,
		t.monitor_unit_id AS monitorUnitId,
		emu.name AS monitorUnitName,
		t.sensor_id AS sensorId,
		t.indicators_id AS indicatorsId,
		emui.name AS indicatorsName,
		<!-- t.install_location,
		t.sensor_name,
		t.brand_name,
		t.sample_frequency AS sampleFrequency, -->
		case t.sample_frequency
                 when 'd' THEN '每天'
                 when 'm' THEN '每月'
                 when 'y' THEN '每年'
            	 else '每半年'
            end as sampleFrequency,
		t.mark,
		t.description,
		t.eva_from AS evaFrom,
		t.eva_to AS evaTo,
		t.status,
		t.creator,
		cu.user_name AS creatorName,
		t.created_time AS createdTime,
		t.modifier,
		mu.user_name AS modifierName,
		t.modified_time AS modifiedTime,
		t.type,
		t.equipment_type AS equipmentType,
		CONCAT_WS('', t_sensor.sensor_name, g_sensor.sensor_name, a_sensor.sensor_name, o_sensor.sensor_name, p_sensor.sensor_name) AS sensorName,
		CONCAT_WS('', t_sensor.sensor_brand, g_sensor.sensor_brand, a_sensor.sensor_brand, o_sensor.sensor_brand, p_sensor.sensor_brand) AS brandName,
		CONCAT_WS('', t_sensor.position_des, g_sensor.position_des, a_sensor.position_des, o_sensor.position_des, p_sensor.position_des) AS installLocation,
		CONCAT_WS('',t_sensor.use_date,g_sensor.use_date,a_sensor.use_date,o_sensor.use_date,p_sensor.use_date) AS useDate 
	</sql>

	<sql id="Base_Table_Ref">
		busi_eva_monitor_config t
		LEFT JOIN busi_transformer_sensor t_sensor ON t.sensor_id = t_sensor.sensor_code AND t_sensor.status = '1'
		LEFT JOIN busi_gis_sensor g_sensor ON t.sensor_id = g_sensor.sensor_code AND g_sensor.status = '1'
		LEFT JOIN busi_arrester_sensor a_sensor ON t.sensor_id = a_sensor.sensor_code AND a_sensor.status = '1'
		LEFT JOIN busi_other_sensor o_sensor ON t.sensor_id = o_sensor.sensor_code AND o_sensor.status = '1'
		LEFT JOIN busi_temper_point p_sensor ON t.sensor_id = p_sensor.sensor_code AND p_sensor.status = '1'
		LEFT JOIN sys_user cu ON t.creator = cu.id
		LEFT JOIN sys_user mu ON t.modifier = mu.id
		LEFT JOIN busi_eva_monitor_equipment eme ON t.monitor_type_id = eme.id AND eme.status = '1'
		LEFT JOIN busi_eva_monitor_unit emu ON t.monitor_unit_id = emu.id AND emu.status = '1'
		LEFT JOIN busi_eva_monitor_unit_indicators emui ON t.indicators_id = emui.id AND emui.status = '1'
	</sql>

	<select id="findBusiEvaMonitorEquipmentList" resultType="com.huatek.busi.vo.BusiEvaMonitorEquipmentVo">
		SELECT
		  t.id AS id,
		  t.name AS name
		FROM busi_eva_monitor_equipment t
		WHERE t.status = '1'
		ORDER BY t.sort ASC, t.id ASC
	</select>

	<select id="findBusiEvaMonitorUnitList" parameterType="java.lang.String" resultType="com.huatek.busi.vo.BusiEvaMonitorUnitVo">
		SELECT
		  t.id AS id,
		  t.eq_type_id AS eqTypeId,
		  t.name AS name
		FROM busi_eva_monitor_unit t
		WHERE t.status = '1'
		AND t.eq_type_id = #{equipTypeId}
		ORDER BY t.sort ASC, t.id ASC
	</select>
	
	<select id="findBusiEvaMonitorUnitIndicators" resultType="com.huatek.busi.vo.BusiEvaMonitorUnitIndicatorsVo">
		SELECT
		  t.id,
		  t.name,
		  t.attr
		FROM busi_eva_monitor_unit_indicators t,
		     busi_eva_monitor_ref ref
		WHERE t.id = ref.indicators_id
		AND ref.unit_id = #{unitId}
		AND t.status = '1'
		ORDER BY t.sort ASC, t.id ASC
	</select>

<!-- 	<select id="selectShowAttr" resultType="java.lang.String">
		SELECT
		  i.attr,
		  i.name
		FROM busi_eva_monitor_config t,
		     busi_eva_monitor_unit u,
		     busi_eva_monitor_unit_indicators i,
		     busi_eva_monitor_ref ref
		WHERE t.monitor_unit_id = u.id
		AND u.id = ref.unit_id
		AND ref.indicators_id = i.id
		AND t.status = '1'
		AND t.sensor_id = #{sensorId}
	</select> -->

	<select id="findIndicatorsBySensorCode" resultType="com.huatek.busi.vo.BusiEvaMonitorUnitIndicatorsVo">
		SELECT
		  t.id,
		  t.name,
		  t.attr
		FROM busi_eva_monitor_unit_indicators t
		WHERE t.status = '1'
		  AND EXISTS (
		    SELECT 
		      1
		    FROM busi_eva_monitor_config c
		    WHERE c.indicators_id = t.id
		      AND c.sensor_id = #{sensorCode}
		      AND c.status = '1')
		ORDER BY t.sort asc
	</select>

	<select id="checkExist" resultType="java.lang.String">
		SELECT
		  t.sensor_id
		FROM busi_eva_monitor_config t
		<where>
			AND t.sensor_id = #{sensorCode}
			AND t.indicators_id = #{unitIndicatorsId}
			AND t.status = '1'
			<if test="id != null and id != ''">
				AND t.id != #{id}
			</if>
		</where>
		LIMIT 1
	</select>

	<sql id="Base_Query_Where">
		t.status = '1'
		and CONCAT_WS('', t_sensor.status, g_sensor.status, a_sensor.status, o_sensor.status, p_sensor.status) = '1'
        <if test="id != null and id != ''">
             and t.id = #{id}
        </if>
        <if test="monitorTypeId != null and monitorTypeId != ''">
             <!-- and t.monitor_type_id = #{monitorTypeId} -->
             and eme.name like CONCAT(CONCAT('%', #{monitorTypeId}), '%')
        </if>
        <if test="monitorUnitId != null and monitorUnitId != ''">
             <!-- and t.monitor_unit_id = #{monitorUnitId} -->
             and emu.name like CONCAT(CONCAT('%', #{monitorUnitId}), '%')
        </if>
        <if test="sensorId != null and sensorId != ''">
             and t.sensor_id = #{sensorId}
        </if>
        <if test="indicatorsId != null and indicatorsId != ''">
             <!-- and t.indicators_id = #{indicatorsId} -->
             and emui.name like CONCAT(CONCAT('%', #{indicatorsId}), '%')
        </if>
        <if test="installLocation != null and installLocation != ''">
             and CONCAT_WS(
						      '',
						      t_sensor.position_des,
						      g_sensor.position_des,
						      a_sensor.position_des,
						      o_sensor.position_des,
						      p_sensor.position_des
						    ) like CONCAT(CONCAT('%', #{installLocation}), '%')
        </if>
        <if test="brandName != null and brandName != ''">
             and t.brand_name  LIKE CONCAT(CONCAT('%', #{brandName}), '%')
        </if>
        <if test="sensorName != null and sensorName != ''">
             and (
             t_sensor.sensor_name  LIKE CONCAT(CONCAT('%', #{sensorName}), '%')
             OR
             g_sensor.sensor_name  LIKE CONCAT(CONCAT('%', #{sensorName}), '%')
             OR 
             a_sensor.sensor_name  LIKE CONCAT(CONCAT('%', #{sensorName}), '%')
             OR
             o_sensor.sensor_name  LIKE CONCAT(CONCAT('%', #{sensorName}), '%')
             OR
             p_sensor.sensor_name  LIKE CONCAT(CONCAT('%', #{sensorName}), '%')
             )
        </if>
        <if test="sampleFrequency != null and sampleFrequency != ''">
             and t.sample_frequency = #{sampleFrequency}
        </if>
        <if test="mark != null and mark != ''">
             and t.mark = #{mark}
        </if>
        <if test="description != null and description != ''">
             and t.description = #{description}
        </if>
        <if test="evaFrom != null">
             and t.eva_from = #{evaFrom}
        </if>
        <if test="evaTo != null">
             and t.eva_to = #{evaTo}
        </if>
	</sql>

	<select id="selectBusiEvaMonitorConfigPage" parameterType="com.huatek.busi.dto.BusiEvaMonitorConfigDto" resultType="com.huatek.busi.vo.BusiEvaMonitorConfigVo">
		SELECT
			<include refid="Base_Vo_column_list"/>
		FROM
			<include refid="Base_Table_Ref"/>
		WHERE
			<include refid="Base_Query_Where"/>
		GROUP BY t.sensor_id
        ORDER BY t.created_time DESC
    </select>

	<!-- 导出考核指标 -->
	<select id="exportBusiEvaMonitorConfigData" parameterType="com.huatek.busi.dto.BusiEvaMonitorConfigDto" resultType="com.huatek.busi.vo.BusiEvaMonitorConfigVo">
		SELECT
			<include refid="Base_Vo_column_list4export"/>
		FROM
			<include refid="Base_Table_Ref"/>
		WHERE
			<include refid="Base_Query_Where"/>
        ORDER BY t.created_time DESC
    </select>

	<select id="findBusiEvaMonitorConfigVo" resultType="com.huatek.busi.vo.BusiEvaMonitorConfigVo">
		SELECT
			<include refid="Base_Vo_column_list"/>
		FROM
			<include refid="Base_Table_Ref"/>
		WHERE
			t.id = #{id}
	</select>
	<!-- 获取监测单元信息 -->
	<select id="findBusiEvaMonitorUnit" resultType="com.huatek.busi.vo.BusiEvaMonitorUnitVo">
		SELECT
		  t.id as id,
		  t.eq_type_id AS eqTypeId
		FROM busi_eva_monitor_unit t
		WHERE t.status = '1'
		 <if test="equipmentType != null and equipmentType != ''">
             and t.equipment_type = #{equipmentType}
        </if>
         <if test="type != null and type != ''">
             and t.type = #{type}
        </if>
	</select>
	<!-- 获取监测单元-考核指标关系信息 -->
	<select id="findBusiEvaMonitorRefList" resultType="com.huatek.busi.vo.BusiEvaMonitorRefVo">
		SELECT
		  t.id as id,
		  t.unit_id AS unitId,
		  t.indicators_id as indicatorsId	  
		FROM busi_eva_monitor_ref t
		<where>
			<if test="unitId !=null and unitId !=''">
				t.unit_id = #{unitId}
			</if>
			<if test="phase !=null and phase !=''">
				and t.phase = #{phase}
			</if>
		</where>
		
	</select>
	<!-- 获取传感器考核指标 -->
	<select id="findBusiEvaMonitorUnitIndicatorsList" resultType="com.huatek.busi.vo.BusiEvaMonitorUnitIndicatorsVo">
		SELECT 
			  mui.name,
			  CONCAT_WS('', t_sensor.sensor_name, g_sensor.sensor_name, a_sensor.sensor_name, o_sensor.sensor_name,p_sensor.sensor_name) AS sensorName,
			  CONCAT_WS('', t_sensor.position_des, g_sensor.position_des, a_sensor.position_des, o_sensor.position_des,p_sensor.position_des) AS sensorInstallLocation
		FROM
			  busi_eva_monitor_config mc 
			  LEFT JOIN busi_eva_monitor_unit_indicators mui ON mc.`indicators_id` = mui.`id`
			  LEFT JOIN busi_transformer_sensor t_sensor ON  mc.sensor_id = t_sensor.sensor_code
			  LEFT JOIN busi_gis_sensor g_sensor ON  mc.sensor_id = g_sensor.sensor_code
			  LEFT JOIN busi_arrester_sensor a_sensor ON  mc.sensor_id = a_sensor.sensor_code
			  LEFT JOIN busi_other_sensor o_sensor ON  mc.sensor_id = o_sensor.sensor_code
			  LEFT JOIN busi_temper_point p_sensor  ON mc.sensor_id = p_sensor.sensor_code 
		WHERE mc.sensor_id = #{sensorId}
	    ORDER BY mui.sort
	</select>
	
	<!-- 删除传感器考核指标 -->
	<delete id="deleteBusiEvaMonitorConfig">
		delete from busi_eva_monitor_config where sensor_id = #{sensorId}
	</delete>
</mapper>