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

    <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 repaiResult,
            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,
            defect_parts as defectParts,
            defect_repair as defectRepair,
            defect_coefficient as defectCoefficient,
            sub_type as subType,
            fault_shows as faultShows,
            fault_cause as faultCause,
            check_meth as checkMeth,
            sug_measures as sugMeasures,
            fail_description as failDescription,
            deal_status as dealStatus,
            deal_result as dealResult,
            is_delete as isDelete,
            fault_fcode as faultFcode,
            fault_ftype as faultFtype,
            fault_fshow as faultFshow,
            fault_possible_reasons as faultPossibleReasons,
            fault_position as faultPosition,
            tldlq_Maintain as tldlqMaintain,
            fault_notes as faultNotes
    </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.repai_result as repaiResult,
           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,
           (SELECT st.equipment_name FROM busi_external_equipment st WHERE st.id = t.sub_equipment_id) as subEquipmentName,
           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.defect_parts as defectParts,
           t.defect_repair as defectRepair,
           t.defect_coefficient as defectCoefficient,
           t.sub_type as subType,
           t.fault_shows as faultShows,
           t.fault_cause as faultCause,
           t.check_meth as checkMeth,
           t.sug_measures as sugMeasures,
           t.fail_description as failDescription,
           t.deal_status as dealStatus,
           t.deal_result as dealResult,
           t.tldlq_Maintain as tldlqMaintain,
           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,
           t.fault_fcode as faultFcode,
           t.fault_ftype as faultFtype,
           t.fault_fshow as faultFshow,
           t.fault_possible_reasons as faultPossibleReasons,
           t.fault_position as faultPosition,
           t.fault_notes as faultNotes
    </sql>

 <sql id="Base_Column_List3">
  			 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,
             ts.sensor_name AS sensorName,
             t.signage AS signage,
             t.install_location AS installLocation,
             t.discovery_time AS discoveryTime,
             t.defect_content AS defectContent,
             t.defect_category AS defectCategory,
             t.clear_time AS clearTime,
             t.repai_result AS repaiResult,
             t.repair_level AS repairLevel,
             t.effect_level AS effectLevel,
             t.create_user AS createUser,
             t.create_time AS createTime,
             t.create_name AS createName,
             t.tldlq_Maintain as tldlqMaintain,
             t.modified_user AS modifiedUser,
             t.modified_time AS modifiedTime,
             t.modified_name AS modifiedName,
             t.defect_parts AS defectParts,
             t.defect_repair AS defectRepair,
             t.defect_coefficient AS defectCoefficient,
             t.sub_type AS subType,
             t.fault_shows AS faultShows,
             t.fault_cause AS faultCause,
             t.check_meth AS checkMeth,
             t.sug_measures AS sugMeasures,
             t.fail_description AS failDescription,
             t.deal_status AS dealStatus,
             t.deal_result AS dealResult,
             t.is_delete AS isDelete,
             t.fault_ftype as faultFtype,
             t.fault_possible_reasons as faultPossibleReasons,
             t.fault_position as faultPosition
 </sql>

    <select id="selectBusiOfflineGeneratorRepairPage" parameterType="com.huatek.busi.dto.BusiOfflineGeneratorRepairDto" resultType="com.huatek.busi.vo.BusiOfflineGeneratorRepairVo">
        SELECT
        	<include refid="Base_Column_List2"/>,
        	'6' as eqType,
        	em.gz_type as gzType,
			ts.sensor_name as sensorName,
			ts.position_des as installLocation,
			ts.position as sensorPosition,
			ts.manufactor as sensorManufactor,
			ts.use_date as sensorUseDate,
			bst.name AS mainEquipmentName,
			generator_sub.name as subEquipmentName,
            generator_sub.equipment_type as subEquipmentType,
        	bew.create_time as warnTime
        FROM
        	busi_offline_generator_repair t
        LEFT JOIN busi_eqm_malfunction em ON t.warning_id = em.id
		LEFT JOIN busi_generator_sensor ts ON t.sensor_code = ts.sensor_code
		LEFT JOIN busi_generator_sub_tree generator_sub ON ts.equipment_id = generator_sub.id
		LEFT JOIN busi_site_tree bst ON generator_sub.equipment_id = bst.id
		LEFT JOIN busi_eqm_warning bew ON bew.id = em.warning_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="sensorCode != null and sensorCode != ''">
             and t.sensor_code = #{sensorCode}
        </if>
        <if test="equipmentType != null and equipmentType != ''">
	         and bst.equipment_type = #{equipmentType}
	    </if>
	    <if test="dealStatus !=null and dealStatus != ''">
	    	and t.deal_status = #{dealStatus}
	    </if>
   	    <if test="subEquipmentType != null and subEquipmentType != ''">
           and generator_sub.equipment_type = #{subEquipmentType}
       </if>
        <if test="sensorName != null and sensorName != ''">
             and ts.sensor_name like concat('%', #{sensorName}, '%')
        </if>
        <if test="mainEquipmentName !=null and mainEquipmentName !=''">
        	and bst.name like concat('%', #{mainEquipmentName}, '%')
        </if>
        <if test="signage != null and signage != ''">
             and ts.sensor_brand like concat('%', #{signage}, '%')
        </if>
        <if test="installLocation != null and installLocation != ''">
             and ts.position_des like concat('%', #{installLocation}, '%')
        </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>
        <if test="defectContent != null and defectContent != ''">
             and t.defect_content = #{defectContent}
        </if>
        <if test="defectCategory != null and defectCategory != ''">
             and t.defect_category = #{defectCategory}
        </if>
        <if test="clearTime != null">
             and t.clear_time = #{clearTime}
        </if>
        <if test="repaiResult != null and repaiResult != ''">
             and t.repai_result = #{repaiResult}
        </if>
        <if test="repairLevel != null and repairLevel != ''">
             and t.repair_level = #{repairLevel}
        </if>
        <if test="effectLevel != null and effectLevel != ''">
             and t.effect_level = #{effectLevel}
        </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="defectParts != null and defectParts != ''">
             and t.defect_parts = #{defectParts}
        </if>
        <if test="defectRepair != null and defectRepair != ''">
             and t.defect_repair = #{defectRepair}
        </if>
        <if test="defectCoefficient != null and defectCoefficient != ''">
             and t.defect_coefficient = #{defectCoefficient}
        </if>
        <if test="subType != null and subType != ''">
             and t.sub_type = #{subType}
        </if>
        <if test="faultShows != null and faultShows != ''">
             and t.fault_shows = #{faultShows}
        </if>
        <if test="faultCause != null and faultCause != ''">
             and t.fault_cause = #{faultCause}
        </if>
        <if test="checkMeth != null and checkMeth != ''">
             and t.check_meth = #{checkMeth}
        </if>
        <if test="sugMeasures != null and sugMeasures != ''">
             and t.sug_measures = #{sugMeasures}
        </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="getBusiOfflineGeneratorRepairVo"  parameterType="java.lang.String" resultType="com.huatek.busi.vo.BusiOfflineGeneratorRepairVo">
        SELECT
        <include refid="Base_Column_List3"/>
        FROM
        busi_offline_generator_repair t
        LEFT JOIN busi_generator_sensor ts ON ts.sensor_code=t.sensor_code
        WHERE t.id = #{id}
    </select>

    <update id="deleteBusiOfflineGeneratorRepair" parameterType="java.util.List">
        update busi_offline_generator_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="isFinishedOverhaul" parameterType="java.lang.String" resultType="com.huatek.busi.vo.BusiOfflineGeneratorRepairVo">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        busi_offline_generator_repair t
        WHERE t.deal_status='0' and  is_delete='1' and t.warning_id = #{malfunctionId}
    </select>

    <select id="getRepairCount" resultType="java.lang.Integer">
        SELECT
        COUNT(*)
        FROM
        busi_offline_generator_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>

</mapper>