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

    <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,
            repai_result as repairResult,
            repair_level as repairLevel,
            create_user as createUser,
            create_time as createTime,
            create_name as createName,
            modified_user as modifiedUser,
            modified_time as modifiedTime,
            modified_name as modifiedName,
            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_level as repairLevel,
		   t.repai_result as repairResult,
		   t.effect_level as effectLevel,
		   t.tldlq_maintain as tldlqMaintain,
		   t.qx_xz as qxXz,
		   t.ztjg_appearance as ztjgAppearance,
		   t.dust_corrosion as dustCorrosion,
		   t.support_insulation as supportInsulation,
           (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="selectBusiOfflineOtherRepairPage" parameterType="com.huatek.busi.dto.BusiOfflineOtherRepairDto" resultType="com.huatek.busi.vo.BusiOfflineOtherRepairVo">
    	SELECT
			<include refid="Base_Column_List2"/>,
        	'5' as eqType,
        	em.gz_type as gzType,
			other.sensor_name as sensorName,
			other.position_des as installLocation,
			other.position as sensorPosition,
			other.manufactor as sensorManufactor,
			other.use_date as sensorUseDate,
			bst.name AS mainEquipmentName,
			other_sub.name as subEquipmentName,
        	other_sub.equipment_type as subEquipmentType,
        	bew.create_time as warnTime
		FROM busi_offline_other_repair t
		  LEFT JOIN busi_other_sensor other ON t.sensor_code = other.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_other_sub_tree other_sub ON other.equipment_id = other_sub.id
		  LEFT JOIN busi_site_tree bst ON other_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 other.sensor_name like concat('%', #{sensorName}, '%')
	        </if>
	        <if test="signage != null and signage != ''">
	             and other.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 other_sub.equipment_type = #{subEquipmentType}
	       </if>
	       <if test="installLocation != null and installLocation != ''">
             and other.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 other.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="getBusiOfflineOtherRepairVo" parameterType="java.lang.String" resultType="com.huatek.busi.vo.BusiOfflineOtherRepairVo">
		SELECT
        <include refid="Base_Column_List2"/>
        FROM
        busi_offline_other_repair t
        WHERE t.id = #{id}
	</select>

	 <!-- 逻辑删除 -->
	 <update id="deleteBusiOfflineOtherRepair" parameterType="java.util.List">
    	update busi_offline_other_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="getRepairCountForOther" resultType="java.lang.Integer">
		SELECT
			COUNT(*)
		FROM
			busi_offline_other_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_other_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 <![CDATA[ >= ]]> #{startTime}
		</if>
		<if test="endTime != null">
			and t.create_time <![CDATA[ <= ]]> #{endTime}
		</if>
    </select>

     <select id="findBusiOfflineOtherRepairList" parameterType="com.huatek.busi.dto.BusiOfflineOtherRepairDto" resultType="com.huatek.busi.vo.BusiOfflineOtherRepairVo">
    	SELECT
		  	<include refid="Base_Column_List2"/>,
		  	w.warning_code AS warningCode
		FROM busi_offline_other_repair t LEFT JOIN busi_eqm_warning w ON t.warning_id = w.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="discoveryTime != null">
	             and t.discovery_time &gt;= #{discoveryTime}
	        </if>
	        <if test="discoveryTimeEnd != null">
	             and t.discovery_time &lt;= #{discoveryTimeEnd}
	        </if>
    	</where>
	     ORDER BY t.create_time DESC, t.id ASC
    </select>

      <select id="findBusiOfflineOtherRepair" parameterType="java.lang.String" resultType="com.huatek.busi.vo.BusiOfflineOtherRepairVo">
    	SELECT
		  	<include refid="Base_Column_List2"/>,
		  	w.warning_code AS warningCode
		FROM busi_offline_other_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
		limit 1
    </select>


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

</mapper>
