<?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.goomood.phm.mapper.FaultMonitorMapper">
    
    <resultMap type="FaultMonitor" id="FaultMonitorResult">
        <result property="id"    column="id"    />
        <result property="incidentDate"    column="incident_date"    />
        <result property="militaryRegion"    column="military_region"    />
        <result property="army"    column="army"    />
        <result property="division"    column="division"    />
        <result property="regiment"    column="regiment"    />
        <result property="squadron"    column="squadron"    />
        <result property="aircraftNo"    column="aircraft_no"    />
        <result property="aircraftType"    column="aircraft_type"    />
        <result property="factoryNo"    column="factory_no"    />
        <result property="partCategory"    column="part_category"    />
        <result property="partNo"    column="part_no"    />
        <result property="partModel"    column="part_model"    />
        <result property="partName"    column="part_name"    />
        <result property="landingCount"    column="landing_count"    />
        <result property="specialty"    column="specialty"    />
        <result property="faultyPartName"    column="faulty_part_name"    />
        <result property="faultyPartModel"    column="faulty_part_model"    />
        <result property="faultyPartNo"    column="faulty_part_no"    />
        <result property="engineType"    column="engine_type"    />
        <result property="engineNo"    column="engine_no"    />
        <result property="engineSerialNo"    column="engine_serial_no"    />
        <result property="faultLocation"    column="fault_location"    />
        <result property="faultDescription"    column="fault_description"    />
        <result property="faultLocationSite"    column="fault_location_site"    />
        <result property="detectionTiming"    column="detection_timing"    />
        <result property="useSystem"    column="use_system"    />
        <result property="timingType"    column="timing_type"    />
        <result property="manufacturer"    column="manufacturer"    />
        <result property="repairFacility"    column="repair_facility"    />
        <result property="workHoursCurrent"    column="work_hours_current"    />
        <result property="workHoursTotal"    column="work_hours_total"    />
        <result property="workHoursAfterRepair"    column="work_hours_after_repair"    />
        <result property="replacementPartModel"    column="replacement_part_model"    />
        <result property="replacementPartNo"    column="replacement_part_no"    />
        <result property="replacementManufacturer"    column="replacement_manufacturer"    />
        <result property="replacementWorkHoursTotal"    column="replacement_work_hours_total"    />
        <result property="replacementRepairFacility"    column="replacement_repair_facility"    />
        <result property="replacementHoursAfterRepair"    column="replacement_hours_after_repair"    />
        <result property="faultCode"    column="fault_code"    />
        <result property="installationDate"    column="installation_date"    />
        <result property="faultCause"    column="fault_cause"    />
        <result property="faultNature"    column="fault_nature"    />
        <result property="faultResponsibility"    column="fault_responsibility"    />
        <result property="faultConsequence"    column="fault_consequence"    />
        <result property="handlingSuggestion"    column="handling_suggestion"    />
        <result property="impactCount"    column="impact_count"    />
        <result property="missedFlightCount"    column="missed_flight_count"    />
        <result property="detectionMethod"    column="detection_method"    />
        <result property="discoverer"    column="discoverer"    />
        <result property="resolutionMethod"    column="resolution_method"    />
        <result property="maintainer"    column="maintainer"    />
        <result property="reviewer"    column="reviewer"    />
        <result property="reviewTime"    column="review_time"    />
        <result property="resolutionDate"    column="resolution_date"    />
        <result property="maintenanceHours"    column="maintenance_hours"    />
        <result property="maintenanceStaffCount"    column="maintenance_staff_count"    />
        <result property="maintenanceTime"    column="maintenance_time"    />
        <result property="meritAwarded"    column="merit_awarded"    />
        <result property="testFlightRequired"    column="test_flight_required"    />
        <result property="remarks"    column="remarks"    />
        <result property="filePath"    column="file_path"    />
        <result property="status"    column="status"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectFaultMonitorVo">
        select id, incident_date, military_region, army, division, regiment, squadron, aircraft_no, aircraft_type, factory_no, part_category, part_no, part_model, part_name, landing_count, specialty, faulty_part_name, faulty_part_model, faulty_part_no, engine_type, engine_no, engine_serial_no, fault_location, fault_description, fault_location_site, detection_timing, use_system, timing_type, manufacturer, repair_facility, work_hours_current, work_hours_total, work_hours_after_repair, replacement_part_model, replacement_part_no, replacement_manufacturer, replacement_work_hours_total, replacement_repair_facility, replacement_hours_after_repair, fault_code, installation_date, fault_cause, fault_nature, fault_responsibility, fault_consequence, handling_suggestion, impact_count, missed_flight_count, detection_method, discoverer, resolution_method, maintainer, reviewer, review_time, resolution_date, maintenance_hours, maintenance_staff_count, maintenance_time, merit_awarded, test_flight_required, remarks, file_path, status, create_by, create_time, update_by, update_time from fault_monitor
    </sql>

    <select id="selectFaultMonitorList" parameterType="FaultMonitor" resultMap="FaultMonitorResult">
        <include refid="selectFaultMonitorVo"/>
        <where>  
            <if test="aircraftType != null  and aircraftType != ''"> and aircraft_type like concat('%', #{aircraftType}, '%')</if>
        </where>
    </select>
    
    <select id="selectFaultMonitorById" parameterType="Long" resultMap="FaultMonitorResult">
        <include refid="selectFaultMonitorVo"/>
        where id = #{id}
    </select>

    <insert id="insertFaultMonitor" parameterType="FaultMonitor" useGeneratedKeys="true" keyProperty="id">
        insert into fault_monitor
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="incidentDate != null">incident_date,</if>
            <if test="militaryRegion != null">military_region,</if>
            <if test="army != null">army,</if>
            <if test="division != null">division,</if>
            <if test="regiment != null">regiment,</if>
            <if test="squadron != null">squadron,</if>
            <if test="aircraftNo != null">aircraft_no,</if>
            <if test="aircraftType != null">aircraft_type,</if>
            <if test="factoryNo != null">factory_no,</if>
            <if test="partCategory != null">part_category,</if>
            <if test="partNo != null">part_no,</if>
            <if test="partModel != null">part_model,</if>
            <if test="partName != null">part_name,</if>
            <if test="landingCount != null">landing_count,</if>
            <if test="specialty != null">specialty,</if>
            <if test="faultyPartName != null">faulty_part_name,</if>
            <if test="faultyPartModel != null">faulty_part_model,</if>
            <if test="faultyPartNo != null">faulty_part_no,</if>
            <if test="engineType != null">engine_type,</if>
            <if test="engineNo != null">engine_no,</if>
            <if test="engineSerialNo != null">engine_serial_no,</if>
            <if test="faultLocation != null">fault_location,</if>
            <if test="faultDescription != null">fault_description,</if>
            <if test="faultLocationSite != null">fault_location_site,</if>
            <if test="detectionTiming != null">detection_timing,</if>
            <if test="useSystem != null">use_system,</if>
            <if test="timingType != null">timing_type,</if>
            <if test="manufacturer != null">manufacturer,</if>
            <if test="repairFacility != null">repair_facility,</if>
            <if test="workHoursCurrent != null">work_hours_current,</if>
            <if test="workHoursTotal != null">work_hours_total,</if>
            <if test="workHoursAfterRepair != null">work_hours_after_repair,</if>
            <if test="replacementPartModel != null">replacement_part_model,</if>
            <if test="replacementPartNo != null">replacement_part_no,</if>
            <if test="replacementManufacturer != null">replacement_manufacturer,</if>
            <if test="replacementWorkHoursTotal != null">replacement_work_hours_total,</if>
            <if test="replacementRepairFacility != null">replacement_repair_facility,</if>
            <if test="replacementHoursAfterRepair != null">replacement_hours_after_repair,</if>
            <if test="faultCode != null">fault_code,</if>
            <if test="installationDate != null">installation_date,</if>
            <if test="faultCause != null">fault_cause,</if>
            <if test="faultNature != null">fault_nature,</if>
            <if test="faultResponsibility != null">fault_responsibility,</if>
            <if test="faultConsequence != null">fault_consequence,</if>
            <if test="handlingSuggestion != null">handling_suggestion,</if>
            <if test="impactCount != null">impact_count,</if>
            <if test="missedFlightCount != null">missed_flight_count,</if>
            <if test="detectionMethod != null">detection_method,</if>
            <if test="discoverer != null">discoverer,</if>
            <if test="resolutionMethod != null">resolution_method,</if>
            <if test="maintainer != null">maintainer,</if>
            <if test="reviewer != null">reviewer,</if>
            <if test="reviewTime != null">review_time,</if>
            <if test="resolutionDate != null">resolution_date,</if>
            <if test="maintenanceHours != null">maintenance_hours,</if>
            <if test="maintenanceStaffCount != null">maintenance_staff_count,</if>
            <if test="maintenanceTime != null">maintenance_time,</if>
            <if test="meritAwarded != null">merit_awarded,</if>
            <if test="testFlightRequired != null">test_flight_required,</if>
            <if test="remarks != null">remarks,</if>
            <if test="filePath != null">file_path,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="incidentDate != null">#{incidentDate},</if>
            <if test="militaryRegion != null">#{militaryRegion},</if>
            <if test="army != null">#{army},</if>
            <if test="division != null">#{division},</if>
            <if test="regiment != null">#{regiment},</if>
            <if test="squadron != null">#{squadron},</if>
            <if test="aircraftNo != null">#{aircraftNo},</if>
            <if test="aircraftType != null">#{aircraftType},</if>
            <if test="factoryNo != null">#{factoryNo},</if>
            <if test="partCategory != null">#{partCategory},</if>
            <if test="partNo != null">#{partNo},</if>
            <if test="partModel != null">#{partModel},</if>
            <if test="partName != null">#{partName},</if>
            <if test="landingCount != null">#{landingCount},</if>
            <if test="specialty != null">#{specialty},</if>
            <if test="faultyPartName != null">#{faultyPartName},</if>
            <if test="faultyPartModel != null">#{faultyPartModel},</if>
            <if test="faultyPartNo != null">#{faultyPartNo},</if>
            <if test="engineType != null">#{engineType},</if>
            <if test="engineNo != null">#{engineNo},</if>
            <if test="engineSerialNo != null">#{engineSerialNo},</if>
            <if test="faultLocation != null">#{faultLocation},</if>
            <if test="faultDescription != null">#{faultDescription},</if>
            <if test="faultLocationSite != null">#{faultLocationSite},</if>
            <if test="detectionTiming != null">#{detectionTiming},</if>
            <if test="useSystem != null">#{useSystem},</if>
            <if test="timingType != null">#{timingType},</if>
            <if test="manufacturer != null">#{manufacturer},</if>
            <if test="repairFacility != null">#{repairFacility},</if>
            <if test="workHoursCurrent != null">#{workHoursCurrent},</if>
            <if test="workHoursTotal != null">#{workHoursTotal},</if>
            <if test="workHoursAfterRepair != null">#{workHoursAfterRepair},</if>
            <if test="replacementPartModel != null">#{replacementPartModel},</if>
            <if test="replacementPartNo != null">#{replacementPartNo},</if>
            <if test="replacementManufacturer != null">#{replacementManufacturer},</if>
            <if test="replacementWorkHoursTotal != null">#{replacementWorkHoursTotal},</if>
            <if test="replacementRepairFacility != null">#{replacementRepairFacility},</if>
            <if test="replacementHoursAfterRepair != null">#{replacementHoursAfterRepair},</if>
            <if test="faultCode != null">#{faultCode},</if>
            <if test="installationDate != null">#{installationDate},</if>
            <if test="faultCause != null">#{faultCause},</if>
            <if test="faultNature != null">#{faultNature},</if>
            <if test="faultResponsibility != null">#{faultResponsibility},</if>
            <if test="faultConsequence != null">#{faultConsequence},</if>
            <if test="handlingSuggestion != null">#{handlingSuggestion},</if>
            <if test="impactCount != null">#{impactCount},</if>
            <if test="missedFlightCount != null">#{missedFlightCount},</if>
            <if test="detectionMethod != null">#{detectionMethod},</if>
            <if test="discoverer != null">#{discoverer},</if>
            <if test="resolutionMethod != null">#{resolutionMethod},</if>
            <if test="maintainer != null">#{maintainer},</if>
            <if test="reviewer != null">#{reviewer},</if>
            <if test="reviewTime != null">#{reviewTime},</if>
            <if test="resolutionDate != null">#{resolutionDate},</if>
            <if test="maintenanceHours != null">#{maintenanceHours},</if>
            <if test="maintenanceStaffCount != null">#{maintenanceStaffCount},</if>
            <if test="maintenanceTime != null">#{maintenanceTime},</if>
            <if test="meritAwarded != null">#{meritAwarded},</if>
            <if test="testFlightRequired != null">#{testFlightRequired},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="filePath != null">#{filePath},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateFaultMonitor" parameterType="FaultMonitor">
        update fault_monitor
        <trim prefix="SET" suffixOverrides=",">
            <if test="incidentDate != null">incident_date = #{incidentDate},</if>
            <if test="militaryRegion != null">military_region = #{militaryRegion},</if>
            <if test="army != null">army = #{army},</if>
            <if test="division != null">division = #{division},</if>
            <if test="regiment != null">regiment = #{regiment},</if>
            <if test="squadron != null">squadron = #{squadron},</if>
            <if test="aircraftNo != null">aircraft_no = #{aircraftNo},</if>
            <if test="aircraftType != null">aircraft_type = #{aircraftType},</if>
            <if test="factoryNo != null">factory_no = #{factoryNo},</if>
            <if test="partCategory != null">part_category = #{partCategory},</if>
            <if test="partNo != null">part_no = #{partNo},</if>
            <if test="partModel != null">part_model = #{partModel},</if>
            <if test="partName != null">part_name = #{partName},</if>
            <if test="landingCount != null">landing_count = #{landingCount},</if>
            <if test="specialty != null">specialty = #{specialty},</if>
            <if test="faultyPartName != null">faulty_part_name = #{faultyPartName},</if>
            <if test="faultyPartModel != null">faulty_part_model = #{faultyPartModel},</if>
            <if test="faultyPartNo != null">faulty_part_no = #{faultyPartNo},</if>
            <if test="engineType != null">engine_type = #{engineType},</if>
            <if test="engineNo != null">engine_no = #{engineNo},</if>
            <if test="engineSerialNo != null">engine_serial_no = #{engineSerialNo},</if>
            <if test="faultLocation != null">fault_location = #{faultLocation},</if>
            <if test="faultDescription != null">fault_description = #{faultDescription},</if>
            <if test="faultLocationSite != null">fault_location_site = #{faultLocationSite},</if>
            <if test="detectionTiming != null">detection_timing = #{detectionTiming},</if>
            <if test="useSystem != null">use_system = #{useSystem},</if>
            <if test="timingType != null">timing_type = #{timingType},</if>
            <if test="manufacturer != null">manufacturer = #{manufacturer},</if>
            <if test="repairFacility != null">repair_facility = #{repairFacility},</if>
            <if test="workHoursCurrent != null">work_hours_current = #{workHoursCurrent},</if>
            <if test="workHoursTotal != null">work_hours_total = #{workHoursTotal},</if>
            <if test="workHoursAfterRepair != null">work_hours_after_repair = #{workHoursAfterRepair},</if>
            <if test="replacementPartModel != null">replacement_part_model = #{replacementPartModel},</if>
            <if test="replacementPartNo != null">replacement_part_no = #{replacementPartNo},</if>
            <if test="replacementManufacturer != null">replacement_manufacturer = #{replacementManufacturer},</if>
            <if test="replacementWorkHoursTotal != null">replacement_work_hours_total = #{replacementWorkHoursTotal},</if>
            <if test="replacementRepairFacility != null">replacement_repair_facility = #{replacementRepairFacility},</if>
            <if test="replacementHoursAfterRepair != null">replacement_hours_after_repair = #{replacementHoursAfterRepair},</if>
            <if test="faultCode != null">fault_code = #{faultCode},</if>
            <if test="installationDate != null">installation_date = #{installationDate},</if>
            <if test="faultCause != null">fault_cause = #{faultCause},</if>
            <if test="faultNature != null">fault_nature = #{faultNature},</if>
            <if test="faultResponsibility != null">fault_responsibility = #{faultResponsibility},</if>
            <if test="faultConsequence != null">fault_consequence = #{faultConsequence},</if>
            <if test="handlingSuggestion != null">handling_suggestion = #{handlingSuggestion},</if>
            <if test="impactCount != null">impact_count = #{impactCount},</if>
            <if test="missedFlightCount != null">missed_flight_count = #{missedFlightCount},</if>
            <if test="detectionMethod != null">detection_method = #{detectionMethod},</if>
            <if test="discoverer != null">discoverer = #{discoverer},</if>
            <if test="resolutionMethod != null">resolution_method = #{resolutionMethod},</if>
            <if test="maintainer != null">maintainer = #{maintainer},</if>
            <if test="reviewer != null">reviewer = #{reviewer},</if>
            <if test="reviewTime != null">review_time = #{reviewTime},</if>
            <if test="resolutionDate != null">resolution_date = #{resolutionDate},</if>
            <if test="maintenanceHours != null">maintenance_hours = #{maintenanceHours},</if>
            <if test="maintenanceStaffCount != null">maintenance_staff_count = #{maintenanceStaffCount},</if>
            <if test="maintenanceTime != null">maintenance_time = #{maintenanceTime},</if>
            <if test="meritAwarded != null">merit_awarded = #{meritAwarded},</if>
            <if test="testFlightRequired != null">test_flight_required = #{testFlightRequired},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="filePath != null">file_path = #{filePath},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteFaultMonitorById" parameterType="Long">
        delete from fault_monitor where id = #{id}
    </delete>

    <delete id="deleteFaultMonitorByIds" parameterType="String">
        delete from fault_monitor where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>


    <select id="countUnprocessedFaults" resultType="int">
        SELECT COUNT(*)
        FROM fault_monitor
        WHERE
            status = #{status}
          AND incident_date >= DATE_SUB(CURDATE(), INTERVAL 3 MONTH)
    </select>
</mapper>