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

    <sql id="Base_Column_List">
            id as id,
            site_id as siteId,
            main_equipment_id as mainEquipmentId,
            sub_equipment_id as subEquipmentId,
            warning_id as warningId,
            sensor_code as sensorCode,
            sensor_name as sensorName,
            signage as signage,
            install_location as installLocation,
            discovery_time as discoveryTime,
            defect_content as defectContent,
            defect_category as defectCategory,
            clear_time as clearTime,
            repair_result as repairResult,
            repair_level as repairLevel,
            effect_level as effectLevel,
            create_user as createUser,
            create_time as createTime,
            create_name as createName,
            modified_user as modifiedUser,
            modified_time as modifiedTime,
            modified_name as modifiedName,
            czjg_lubrication as czjgLubrication,
            ztjg_appearance as ztjgAppearance,
            support_insulation as supportInsulation,
            main_loop_terminal as mainLoopTerminal,
            tldlq_maintain as tldlqMaintain,
            dlq_maintenance as dlqMaintenance,
            dust_corrosion as dustCorrosion,
            qx_xz as qxXz,
            fail_description as failDescription,
            deal_status as dealStatus,
            deal_result as dealResult,
            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.defect_content as defectContent,
           t.defect_category as defectCategory,
           t.clear_time as clearTime,
		   t.repair_result as repairResult,
           t.repair_level as repairLevel,
           t.effect_level as effectLevel,
           (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.czjg_lubrication as czjgLubrication,
           t.ztjg_appearance as ztjgAppearance,
           t.support_insulation as supportInsulation,
           t.main_loop_terminal as mainLoopTerminal,
           t.tldlq_maintain as tldlqMaintain,
           t.dlq_maintenance as dlqMaintenance,
           t.dust_corrosion as dustCorrosion,
           t.qx_xz as qxXz,
           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="selectBusiOfflineGisRepairPage" parameterType="com.huatek.busi.dto.BusiOfflineGisRepairDto" resultType="com.huatek.busi.vo.BusiOfflineGisRepairVo">
    	SELECT
			<include refid="Base_Column_List2"/>,
        	'2' as eqType,
        	em.gz_type as gzType,
			gs.sensor_name as sensorName,
			gs.position_des as installLocation,
			gs.position as sensorPosition,
			gs.manufactor as sensorManufactor,
			gs.use_date as sensorUseDate,
			bst.name AS mainEquipmentName,
			gis_sub.name as subEquipmentName,
        	gis_sub.equipment_type as subEquipmentType,
        	bew.create_time as warnTime
		FROM busi_offline_gis_repair t
		  LEFT JOIN busi_gis_sensor gs ON t.sensor_code = gs.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_gis_sub_tree gis_sub ON gs.equipment_id = gis_sub.id 
		  LEFT JOIN busi_site_tree bst ON gis_sub.equipment_id = bst.id 
    	<where>
    		and t.is_delete = '1'
    		<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="warningCode != null and warningCode != ''">
	             and w.warning_code like concat('%', #{warningCode}, '%')
	        </if>
	        
	        <if test="sensorName != null and sensorName != ''">
	             and gs.sensor_name like concat('%', #{sensorName}, '%')
	        </if>
	        <if test="signage != null and signage != ''">
	             and gs.sensor_brand like concat('%', #{signage}, '%')
	        </if>
	        <if test="mainEquipmentName !=null and mainEquipmentName !=''">
        		and bst.name like concat('%', #{mainEquipmentName}, '%')
        	</if>
        	<if test="subEquipmentType != null and subEquipmentType != ''">
	           and gis_sub.equipment_type = #{subEquipmentType}
	       </if>
	       <if test="installLocation != null and installLocation != ''">
             and gs.position_des like concat('%', #{installLocation}, '%')
        	</if>
        	<if test="dealStatus !=null and dealStatus != ''">
		    	and t.deal_status = #{dealStatus}
		    </if>
        	<if test="sensorManufactor != null and sensorManufactor != ''">
             	and gs.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="discoveryTimeEnd != null">
	             and DATE_FORMAT(t.discovery_time,"%Y-%m-%d") <![CDATA[ <= ]]> DATE_FORMAT(#{discoveryTimeEnd},"%Y-%m-%d")
	        </if>
    	</where>
	     ORDER BY t.create_time DESC, t.id ASC
    </select>

	<select id="getBusiOfflineGisRepairVo" parameterType="java.lang.String" resultType="com.huatek.busi.vo.BusiOfflineGisRepairVo">
		SELECT
        <include refid="Base_Column_List"/>
        FROM
        busi_offline_gis_repair t 
        WHERE t.id = #{id}
	</select>
	
	 <!-- 逻辑删除 -->
	 <update id="deleteBusiOfflineGisRepair" parameterType="java.util.List">
    	update busi_offline_gis_repair set is_delete='2' 
    	where id in
    	<foreach collection="idList" index="index" item="id" separator="," open="(" close=")">
            #{id,jdbcType=VARCHAR}
        </foreach>
    </update>

    <select id="getRepairCountForGis" resultType="java.lang.Integer">
		SELECT
			COUNT(*)
		FROM 
			busi_offline_gis_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="getRepairCount" resultType="java.lang.Integer">
		SELECT
			COUNT(*)
		FROM 
			busi_offline_gis_repair t
		WHERE
			t.is_delete = '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.create_time &gt;= #{startTime}
		</if>
		<if test="endTime != null">
			and t.create_time &lt;= #{endTime}
		</if>
    </select>
    
      <select id="findBusiOfflineGisRepair" parameterType="java.lang.String" resultType="com.huatek.busi.vo.BusiOfflineGisRepairVo">
    	SELECT
		  	<include refid="Base_Column_List2"/>,
		  	w.warning_code AS warningCode
		FROM busi_offline_gis_repair t LEFT JOIN busi_eqm_warning w ON t.warning_id = w.id
    	WHERE t.main_equipment_id = #{mainTreeId}
		AND t.site_id = #{siteId}
		ORDER BY t.create_time DESC
    </select>
    
    
     <select id="isFinishedOverhaul" parameterType="java.lang.String" resultType="com.huatek.busi.vo.BusiOfflineGisRepairVo">
		SELECT
        <include refid="Base_Column_List"/>
        FROM
        busi_offline_gis_repair t
        WHERE t.deal_status='0' and  is_delete='1' and t.warning_id = #{malfunctionId}
	</select>
	
</mapper>