<?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.BusiEqmMalfunctionMapper">
    <sql id="Base_Column_List">
    t.id as id,
    t.warning_id as warningId,
    t.sensor_id as sensorId,
    t.eq_type as eqType,
    t.gz_type as gzType,
    t.malfunction_source as malfunctionSource,
    t.warning_type_code as warningTypeCode,
    t.warning_msg as warningMsg,
    t.warning_time as warningTime,
    t.create_time as createTime,
    t.del_flag as delFlag,
    t.deal_status as dealStatus,
    t.deal_msg as dealMsg,
    t.site_id as siteId,
    t.main_equipment_id as mainEquipmentId,
    t.sub_equipment_id as subEquipmentId,
    t.processing_time as processingTime,
    (SELECT su.user_name FROM sys_user su WHERE su.id = processing_user) as processingUser,
    sub_equipment_install_location as subEquipmentInstallLocation,
    sub_equipment_brand as subEquipmentBrand,
    sub_equipment_manufactor as subEquipmentManufactor,
    sub_equipment_name as subEquipmentName,
    t.sub_tree_id as subTreeId,
    t.main_tree_id as mainTreeId,
    t.run_state as runState,
    t.create_name as createName,
    t.create_user as createUser,
    t.three_ratio as threeRatio
  </sql>
  <sql id="Base_Column_List1">
    t.id AS id,
	bst.NAME AS mainEquipmentName,
	t.warning_id AS warningId,
	t.sensor_id AS sensorId,
	t.eq_type AS eqType,
	t.gz_type AS gzType,
	t.malfunction_source AS malfunctionSource,
	t.warning_type_code AS warningTypeCode,
	t.warning_msg AS warningMsg,
	t.warning_time AS warningTime,
	t.create_time AS createTime,
	t.del_flag AS delFlag,
	t.deal_status AS dealStatus,
	t.deal_msg AS dealMsg,
	t.site_id AS siteId,
	t.main_equipment_id AS mainEquipmentId,
	t.sub_equipment_id AS subEquipmentId,
	t.processing_time AS processingTime,
	( SELECT su.user_name FROM sys_user su WHERE su.id = t.processing_user ) AS processingUser,
	t.sub_equipment_install_location AS subEquipmentInstallLocation,
	t.sub_equipment_brand AS subEquipmentBrand,
	t.sub_equipment_manufactor AS subEquipmentManufactor,
	CONCAT_WS( '', t_sensor.sensor_name, g_sensor.sensor_name, gen_sensor.sensor_name, a_sensor.sensor_name, o_sensor.sensor_name, p_sensor.sensor_name, c_sensor.sensor_name ) AS sensorName,
	CONCAT_WS( '', t_sensor.sensor_brand, g_sensor.sensor_brand, gen_sensor.sensor_brand, a_sensor.sensor_brand, o_sensor.sensor_brand, p_sensor.sensor_brand, c_sensor.sensor_brand ) AS sensorBrand,
	CONCAT_WS( '', t_sensor.manufactor, g_sensor.manufactor, a_sensor.manufactor, gen_sensor.manufactor, o_sensor.manufactor, p_sensor.manufactor, c_sensor.manufactor ) AS sensorManufactor,
	CONCAT_WS( '', t_sensor.position_des, g_sensor.position_des, a_sensor.position_des, gen_sensor.position_des, o_sensor.position_des, p_sensor.position_des, c_sensor.position_des ) AS sensorInstallLocation,
	CONCAT_WS( '', t_sensor.use_date, g_sensor.use_date, gen_sensor.use_date, a_sensor.use_date, o_sensor.use_date, p_sensor.use_date, c_sensor.use_date ) AS sensorUseDate,
	CONCAT_WS( '', t_sensor.position, g_sensor.position, gen_sensor.position, a_sensor.position, o_sensor.position, p_sensor.position, c_sensor.position ) AS sensorPosition,
	t.sub_tree_id AS subTreeId,
	t.main_tree_id AS mainTreeId,
	t.run_state AS runState,
	t.create_name AS createName,
	t.create_user AS createUser,
	t.three_ratio AS threeRatio,
	CONCAT_WS( '', transformer_sub.equipment_type, gis_sub.equipment_type, gen_sub.equipment_type, arrester_sub.equipment_type, other_sub.equipment_type, c_sub.equipment_type ) AS subEquipmentType,
	CONCAT_WS( '', transformer_sub.NAME, gis_sub.NAME, gen_sub.NAME, arrester_sub.NAME, other_sub.NAME, c_sub.NAME ) AS subEquipmentName
  </sql>
  <sql id="Base_Column_List2">
    id as id,
    bst.name AS mainEquipmentName,
    warning_id as warningId,
    sensor_id as sensorId,
    eq_type as eqType,
    gz_type as gzType,
    malfunction_source as malfunctionSource,
    warning_type_code as warningTypeCode,
    warning_msg as warningMsg,
    warning_time as warningTime,
    create_time as createTime,
    del_flag as delFlag,
    deal_status as dealStatus,
    deal_msg as dealMsg,
    site_id as siteId,
    main_equipment_id as mainEquipmentId,
    sub_equipment_id as subEquipmentId,
    processing_time as processingTime,
    (SELECT su.user_name FROM sys_user su WHERE su.id = t.processing_user) as processingUser,
    CASE
	  WHEN eq_type = '1'
	  THEN (SELECT s.position_des FROM busi_transformer_sensor AS s WHERE s.sensor_code = sensor_id)
	  WHEN eq_type = '2'
	  THEN (SELECT s.position_des FROM busi_gis_sensor AS s WHERE s.sensor_code = sensor_id)
	  ELSE
	  (SELECT s.position_des FROM busi_arrester_sensor AS s WHERE s.sensor_code = sensor_id)
	  END
	AS sensorInstallLocation,
    sub_equipment_brand as subEquipmentBrand,
    sub_equipment_manufactor as subEquipmentManufactor,
    sensor_install_location as sensorInstallLocation,
    sensor_brand as sensorBrand,
    sensor_manufactor as sensorManufactor,
    sub_equipment_name as subEquipmentName,
    sensor.position as sensorPosition,
    sensor_name as sensorName,
    sub_tree_id as subTreeId,
    main_tree_id as mainTreeId,
    run_state as runState,
    create_name as createName,
    create_user as createUser,
    three_ratio as threeRatio
  </sql>
  <select id="selectBusiEqmMalfunctionPage" parameterType="com.huatek.busi.dto.BusiEqmMalfunctionDto" resultType="com.huatek.busi.vo.BusiEqmMalfunctionVo">
    select
    <include refid="Base_Column_List1" />
    from busi_eqm_malfunction t
    LEFT JOIN busi_eqm_warning ew ON ew.id = t.warning_id
    LEFT JOIN busi_transformer_sensor t_sensor ON t.sensor_id = t_sensor.sensor_code
	LEFT JOIN busi_gis_sensor g_sensor ON t.sensor_id = g_sensor.sensor_code
    LEFT JOIN busi_generator_sensor gen_sensor ON t.sensor_id = gen_sensor.sensor_code
    LEFT JOIN busi_cable_sensor c_sensor ON t.sensor_id = c_sensor.sensor_code
	LEFT JOIN busi_arrester_sensor a_sensor ON t.sensor_id = a_sensor.sensor_code
	LEFT JOIN busi_other_sensor o_sensor ON t.sensor_id = o_sensor.sensor_code
	LEFT JOIN busi_temper_point p_sensor ON t.sensor_id = p_sensor.sensor_code
	LEFT JOIN busi_external_equipment ext ON t.sub_equipment_id = ext.id
    LEFT JOIN busi_transformer_sub_tree transformer_sub ON t_sensor.equipment_id = transformer_sub.id
    LEFT JOIN busi_gis_sub_tree gis_sub ON g_sensor.equipment_id = gis_sub.id
    LEFT JOIN busi_generator_sub_tree gen_sub ON gen_sensor.equipment_id = gen_sub.id
    LEFT JOIN busi_cable_sub_tree c_sub ON t.sub_tree_id = c_sub.id
    LEFT JOIN busi_arrester_sub_tree arrester_sub ON a_sensor.equipment_id = arrester_sub.id
    LEFT JOIN busi_other_sub_tree other_sub ON o_sensor.equipment_id = other_sub.id
    LEFT JOIN busi_site_tree bst ON  CONCAT_WS('',transformer_sub.equipment_id,gis_sub.equipment_id,arrester_sub.equipment_id,other_sub.equipment_id,gen_sub.equipment_id) = bst.id
    WHERE t.del_flag='1'
    <if test="sensorBrand != null and sensorBrand != ''">
         and CONCAT_WS('', t_sensor.sensor_brand, g_sensor.sensor_brand,gen_sensor.sensor_brand,c_sensor.sensor_brand,a_sensor.sensor_brand, o_sensor.sensor_brand, p_sensor.sensor_brand)  LIKE CONCAT(CONCAT('%', #{sensorBrand}), '%')
    </if>
    <if test="sensorName != null and sensorName != ''">
         and  CONCAT_WS('', t_sensor.sensor_name, g_sensor.sensor_name,gen_sensor.sensor_name,c_sensor.sensor_name,a_sensor.sensor_name, o_sensor.sensor_name, p_sensor.sensor_name) like concat('%', #{sensorName}, '%')
    </if>
    <if test="sensorInstallLocation != null and sensorInstallLocation != ''">
          and CONCAT_WS('', t_sensor.position_des, g_sensor.position_des,gen_sensor.position_des,c_sensor.position_des, a_sensor.position_des, o_sensor.position_des, p_sensor.position_des) LIKE CONCAT(CONCAT('%', #{sensorInstallLocation}), '%')
    </if>
    <if test="sensorManufactor != null and sensorManufactor != ''">
			and CONCAT_WS('', t_sensor.manufactor, g_sensor.manufactor,gen_sensor.manufactor,c_sensor.manufactor, a_sensor.manufactor, o_sensor.manufactor, p_sensor.manufactor) LIKE CONCAT(CONCAT('%', #{sensorManufactor}), '%')
      </if>
     <if test="equipmentType != null and equipmentType != ''">
        and bst.equipment_type = #{equipmentType}
    </if>
    <if test="subEquipmentType != null and subEquipmentType != ''">
    	<choose>
       		<when test="equipmentType == '111'.toString and subEquipmentType == '031'.toString">
       			and transformer_sub.equipment_type in ('081', '031')
       		</when>
       		<otherwise>
       			and CONCAT_WS('',transformer_sub.equipment_type,gis_sub.equipment_type,gen_sub.equipment_type,c_sub.equipment_type,arrester_sub.equipment_type,other_sub.equipment_type) = #{subEquipmentType}
       		</otherwise>
       	</choose>
    </if>
    <if test="mainEquipmentName !=null and mainEquipmentName !=''">
    	and bst.name like concat('%', #{mainEquipmentName}, '%')
    </if>
    <if test="startTime != null">
         and DATE_FORMAT(t.create_time,'%Y-%m-%d') <![CDATA[>= ]]> DATE_FORMAT(#{startTime},'%Y-%m-%d')
    </if>
    <if test="endTime != null">
         and DATE_FORMAT(t.create_time,'%Y-%m-%d') <![CDATA[<= ]]> DATE_FORMAT(#{endTime},'%Y-%m-%d')
    </if>
    <if test="warnStartTime != null">
         and DATE_FORMAT(t.warning_time,'%Y-%m-%d') <![CDATA[>= ]]> DATE_FORMAT(#{warnStartTime},'%Y-%m-%d')
    </if>
    <if test="warnEndTime != null">
         and DATE_FORMAT(t.warning_time,'%Y-%m-%d') <![CDATA[<= ]]> DATE_FORMAT(#{warnEndTime},'%Y-%m-%d')
    </if>
    <if test="dealStatus != null and dealStatus != ''">
		and t.deal_status = #{dealStatus}
	</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="mainEquipmentName != null and mainEquipmentName != ''">
		and bst.name like CONCAT(CONCAT('%', #{mainEquipmentName}), '%')
	</if>
    ORDER BY t.create_time DESC
  </select>

  <select id="lockDealStatus" resultType="java.lang.String">
	SELECT t.deal_status FROM busi_eqm_malfunction t WHERE t.id = #{id} FOR UPDATE
  </select>

  <select id="queryUnprocessedEqmMalfunction" parameterType="java.lang.String" resultType="com.huatek.busi.vo.BusiEqmMalfunctionVo">
    select
    <include refid="Base_Column_List" />
    from busi_eqm_malfunction t
    WHERE deal_status='0' and del_flag='1' and warning_id=#{warningId}
  </select>
  <select id="findMalfunctionById" parameterType="java.lang.String" resultType="com.huatek.busi.vo.BusiEqmMalfunctionVo">
    select
    <include refid="Base_Column_List1" />
    from busi_eqm_malfunction t
      LEFT JOIN busi_transformer_sensor t_sensor ON t.sensor_id = t_sensor.sensor_code
      LEFT JOIN busi_gis_sensor g_sensor ON t.sensor_id = g_sensor.sensor_code
      LEFT JOIN busi_generator_sensor gen_sensor ON t.sensor_id = gen_sensor.sensor_code
      LEFT JOIN busi_cable_sensor c_sensor ON t.sensor_id = c_sensor.sensor_code
      LEFT JOIN busi_arrester_sensor a_sensor ON t.sensor_id = a_sensor.sensor_code
      LEFT JOIN busi_other_sensor o_sensor ON t.sensor_id = o_sensor.sensor_code
      LEFT JOIN busi_temper_point p_sensor ON t.sensor_id = p_sensor.sensor_code
      LEFT JOIN busi_external_equipment ext ON t.sub_equipment_id = ext.id
      LEFT JOIN busi_site_tree bst ON t.main_tree_id = bst.id
      LEFT JOIN busi_transformer_sub_tree transformer_sub ON t.sub_tree_id = transformer_sub.id
      LEFT JOIN busi_gis_sub_tree gis_sub ON t.sub_tree_id = gis_sub.id
      LEFT JOIN busi_generator_sub_tree gen_sub ON t.sub_tree_id = gen_sub.id
      LEFT JOIN busi_cable_sub_tree c_sub ON t.sub_tree_id = c_sub.id
      LEFT JOIN busi_arrester_sub_tree arrester_sub ON t.sub_tree_id = arrester_sub.id
      LEFT JOIN busi_other_sub_tree other_sub ON t.sub_tree_id = other_sub.id
    WHERE t.id=#{id} limit 1
  </select>
</mapper>
