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

    <sql id="Base_Column_List">
            t.id as id,
            t.site_id as siteId,
            t.main_equipment_id as mainEquipmentId,
            t.sub_equipment_id as subEquipmentId,
            t.warning_id as warningId,
            t.discovery_time as discoveryTime,
            t.clear_time as clearTime,
            t.defect_category as defectCategory,
            t.repair_result as repairResult,
            t.defect_content as defectContent,
            t.leakage_current as leakageCurrent,
            t.capacitance as capacitance,
            t.loss_factor as lossFactor,
            t.total_current as totalCurrent,
            t.resistive_current as resistiveCurrent,
            t.create_user as createUser,
            t.create_time as createTime,
            t.create_name as createName,
            t.modified_user as modifiedUser,
            t.modified_time as modifiedTime,
            t.modified_name as modifiedName,
            t.fail_description as failDescription,
            t.deal_status as dealStatus,
            t.deal_result as dealResult,
            t.is_delete as isDelete
    </sql>
    <sql id="Base_Column_List2">
           t.id as id,
           t.site_id as siteId,
           t.main_equipment_id as mainEquipmentId,
           t.sub_equipment_id as subEquipmentId,
           t.warning_id as warningId,
           t.sensor_code as sensorCode,
           t.signage as signage,
           t.discovery_time as discoveryTime,
           t.clear_time as clearTime,
           t.defect_category as defectCategory,
           t.repair_result as repairResult,
           t.defect_content as defectContent,
           t.leakage_current as leakageCurrent,
           t.capacitance as capacitance,
           t.loss_factor as lossFactor,
           t.total_current as totalCurrent,
           t.resistive_current as resistiveCurrent,
           (SELECT su.user_name FROM sys_user su WHERE su.id = t.create_user) as createUser,
           t.create_time as createTime,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
           t.create_name as createName,
           (SELECT su.user_name FROM sys_user su WHERE su.id = t.modified_user) AS modifiedUser,
		   t.modified_time AS modifiedTime,
		   t.modified_name AS modifiedName,
           t.fail_description as failDescription,
           t.deal_status as dealStatus,
           t.deal_result as dealResult,
           t.is_delete as isDelete,
           (SELECT su.user_name FROM sys_user su WHERE su.id = t.handle_user) as handleUser,
           t.handle_time as handleTime,
           t.handle_name as handleName
    </sql>
    <select id="selectBusiOfflineArresterRepairPage" parameterType="com.huatek.busi.dto.BusiOfflineArresterRepairDto" resultType="com.huatek.busi.vo.BusiOfflineArresterRepairVo">
        SELECT
        	<include refid="Base_Column_List2"/>,
		  	'3' as eqType,
			bas.sensor_name as sensorName,
			bas.position_des as installLocation,
			bas.position as sensorPosition,
			bas.manufactor as sensorManufactor,
			bas.use_date as sensorUseDate,
			bst.name AS mainEquipmentName,
			arrester_sub.name as subEquipmentName,
        	arrester_sub.equipment_type as subEquipmentType,
        	bew.create_time as warnTime
        FROM
        	busi_offline_arrester_repair t
        LEFT JOIN busi_arrester_sensor bas ON t.sensor_code = bas.sensor_code
		LEFT JOIN busi_eqm_malfunction em ON t.warning_id = em.id
		LEFT JOIN busi_eqm_warning bew ON bew.id = em.warning_id
		LEFT JOIN busi_arrester_sub_tree arrester_sub ON bas.equipment_id = arrester_sub.id 
  		LEFT JOIN busi_site_tree bst ON arrester_sub.equipment_id = bst.id 
        WHERE t.is_delete = '1'
        <if test="id != null and id != ''">
             and t.id = #{id}
        </if>
        <if test="siteId != null and siteId != ''">
             and t.site_id = #{siteId}
        </if>
        <if test="mainEquipmentId != null and mainEquipmentId != ''">
             and t.main_equipment_id = #{mainEquipmentId}
        </if>
        <if test="subEquipmentId != null and subEquipmentId != ''">
             and t.sub_equipment_id = #{subEquipmentId}
        </if>
        <if test="warningId != null and warningId != ''">
             and t.warning_id = #{warningId}
        </if>
        <if test="warningCode != null and warningCode != ''">
             and w.warning_code like  CONCAT("%",#{warningCode},"%")
        </if>
        <if test="sensorCode != null and sensorCode != ''">
             and t.sensor_code = #{sensorCode}
        </if>
        <if test="mainEquipmentName !=null and mainEquipmentName !=''">
        	and bst.name like concat('%', #{mainEquipmentName}, '%')
        </if>
        <if test="subEquipmentType != null and subEquipmentType != ''">
           and arrester_sub.equipment_type = #{subEquipmentType}
       </if>
        <if test="sensorName != null and sensorName != ''">
             and bas.sensor_name like concat('%', #{sensorName}, '%')
        </if>
        <if test="signage != null and signage != ''">
             and bas.sensor_brand like concat('%', #{signage}, '%')
        </if>
        <if test="installLocation != null and installLocation != ''">
             and bas.position_des like concat('%', #{installLocation}, '%')
        </if>
        <if test="sensorManufactor != null and sensorManufactor != ''">
            and bas.manufactor like concat('%', #{sensorManufactor}, '%')
       	</if>
       	<if test="warnStartTime != null">
             and DATE_FORMAT(bew.warning_time,"%Y-%m-%d") <![CDATA[ >= ]]> DATE_FORMAT(#{warnStartTime},"%Y-%m-%d")
        </if>
        <if test="warnEndTime != null">
             and DATE_FORMAT(bew.warning_time,"%Y-%m-%d") <![CDATA[ <= ]]> DATE_FORMAT(#{warnEndTime},"%Y-%m-%d")
        </if>
        <if test="discoveryTime != null">
             and DATE_FORMAT(t.discovery_time,"%Y-%m-%d") <![CDATA[ >= ]]> DATE_FORMAT(#{discoveryTime},"%Y-%m-%d")
        </if>
        <if test="discoveryTimeAfter != null">
             and DATE_FORMAT(t.discovery_time,"%Y-%m-%d") <![CDATA[ <= ]]> DATE_FORMAT(#{discoveryTimeAfter},"%Y-%m-%d")
        </if>
        <if test="clearTime != null">
             and t.clear_time = #{clearTime}
        </if>
        <if test="defectCategory != null and defectCategory != ''">
             and t.defect_category = #{defectCategory}
        </if>
        <if test="repairResult != null and repairResult != ''">
             and t.repair_result = #{repairResult}
        </if>
        <if test="defectContent != null and defectContent != ''">
             and t.defect_content = #{defectContent}
        </if>
        <if test="leakageCurrent != null and leakageCurrent != ''">
             and t.leakage_current = #{leakageCurrent}
        </if>
        <if test="capacitance != null and capacitance != ''">
             and t.capacitance = #{capacitance}
        </if>
        <if test="lossFactor != null and lossFactor != ''">
             and t.loss_factor = #{lossFactor}
        </if>
        <if test="totalCurrent != null and totalCurrent != ''">
             and t.total_current = #{totalCurrent}
        </if>
        <if test="resistiveCurrent != null and resistiveCurrent != ''">
             and t.resistive_current = #{resistiveCurrent}
        </if>
        <if test="createUser != null and createUser != ''">
             and t.create_user = #{createUser}
        </if>
        <if test="createTime != null">
             and t.create_time = #{createTime}
        </if>
        <if test="createName != null and createName != ''">
             and t.create_name = #{createName}
        </if>
        <if test="modifiedUser != null and modifiedUser != ''">
             and t.modified_user = #{modifiedUser}
        </if>
        <if test="modifiedTime != null">
             and t.modified_time = #{modifiedTime}
        </if>
        <if test="modifiedName != null and modifiedName != ''">
             and t.modified_name = #{modifiedName}
        </if>
        <if test="failDescription != null and failDescription != ''">
             and t.fail_description = #{failDescription}
        </if>
        <if test="dealStatus != null and dealStatus != ''">
             and t.deal_status = #{dealStatus}
        </if>
        <if test="dealResult != null and dealResult != ''">
             and t.deal_result = #{dealResult}
        </if>
	     ORDER BY t.create_time DESC, t.id ASC
    </select>

	<select id="getBusiOfflineArresterRepairVo" parameterType="java.lang.String" resultType="com.huatek.busi.vo.BusiOfflineArresterRepairVo">
		SELECT
        <include refid="Base_Column_List"/>,
        bas.sensor_code as sensorCode,
        bas.sensor_name as sensorName,
        bas.sensor_brand as signage,
        bas.position_des as installLocation
        FROM
        busi_offline_arrester_repair t
        LEFT JOIN busi_arrester_sensor bas ON t.sensor_code = bas.sensor_code
        WHERE t.id = #{id}
	</select>

	<select id="getRepairCountForArrester" resultType="java.lang.Integer">
		SELECT
			COUNT(*)
		FROM 
			busi_offline_arrester_repair t
		WHERE
			t.is_delete = '1' and t.deal_status = '1'
		<if test="subEquipmentId != null and subEquipmentId != ''">
			and t.sub_equipment_id = #{subEquipmentId}
		</if>
		<if test="sensorCode != null and sensorCode != ''">
			and t.sensor_code = #{sensorCode}
		</if>
		<if test="startTime != null">
			and t.handle_time &gt;= #{startTime}
		</if>
		<if test="endTime != null">
			and t.handle_time &lt;= #{endTime}
		</if>
    </select>

    <select id="isFinishedOverhaul" parameterType="java.lang.String" resultType="com.huatek.busi.vo.BusiOfflineArresterRepairVo">
		SELECT
        <include refid="Base_Column_List"/>
        FROM
        busi_offline_arrester_repair t
        WHERE t.deal_status='0' and  is_delete='1' and t.warning_id = #{malfunctionId}
	</select>
	
</mapper>