<?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.hw.dms.mapper.DmsFaultInstanceFileMapper">

    <resultMap type="DmsFaultInstanceFile" id="DmsFaultInstanceFileResult">
        <result property="repairInstanceFileId"    column="repair_instance_file_id"    />
        <result property="targetType"    column="target_type"    />
        <result property="targetId"    column="target_id"    />
        <result property="faultFile"    column="fault_file"    />
    </resultMap>

    <sql id="selectDmsFaultInstanceFileVo">
        select repair_instance_file_id, target_type, target_id, fault_file from dms_fault_instance_file
    </sql>

    <select id="selectDmsFaultInstanceFileList" parameterType="DmsFaultInstanceFile" resultMap="DmsFaultInstanceFileResult">
        <include refid="selectDmsFaultInstanceFileVo"/>
        <where>
            <if test="targetType != null  and targetType != ''"> and target_type = #{targetType}</if>
            <if test="targetId != null "> and target_id = #{targetId}</if>
            <if test="faultFile != null  and faultFile != ''"> and fault_file = #{faultFile}</if>
        </where>
    </select>

    <select id="selectDmsFaultInstanceFileByRepairInstanceFileId" parameterType="Long" resultMap="DmsFaultInstanceFileResult">
        <include refid="selectDmsFaultInstanceFileVo"/>
        where repair_instance_file_id = #{repairInstanceFileId}
    </select>

    <insert id="insertDmsFaultInstanceFile" parameterType="DmsFaultInstanceFile" useGeneratedKeys="true" keyProperty="repairInstanceFileId">
        insert into dms_fault_instance_file
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="targetType != null and targetType != ''">target_type,</if>
            <if test="targetId != null">target_id,</if>
            <if test="faultFile != null and faultFile != ''">fault_file,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="targetType != null and targetType != ''">#{targetType},</if>
            <if test="targetId != null">#{targetId},</if>
            <if test="faultFile != null and faultFile != ''">#{faultFile},</if>
         </trim>
    </insert>

    <update id="updateDmsFaultInstanceFile" parameterType="DmsFaultInstanceFile">
        update dms_fault_instance_file
        <trim prefix="SET" suffixOverrides=",">
            <if test="targetType != null and targetType != ''">target_type = #{targetType},</if>
            <if test="targetId != null">target_id = #{targetId},</if>
            <if test="faultFile != null and faultFile != ''">fault_file = #{faultFile},</if>
        </trim>
        where repair_instance_file_id = #{repairInstanceFileId}
    </update>

    <delete id="deleteDmsFaultInstanceFileByRepairInstanceFileId" parameterType="Long">
        delete from dms_fault_instance_file where repair_instance_file_id = #{repairInstanceFileId}
    </delete>

    <delete id="deleteDmsFaultInstanceFileByRepairInstanceFileIds" parameterType="String">
        delete from dms_fault_instance_file where repair_instance_file_id in
        <foreach item="repairInstanceFileId" collection="array" open="(" separator="," close=")">
            #{repairInstanceFileId}
        </foreach>
    </delete>
    <select id="selectDmsFaultInstanceFileByFaultId" parameterType="Long" resultMap="DmsFaultInstanceFileResult">
        select * from dms_fault_instance_file where target_id = (
        select min(instance_activity_id) from dms_fault_instance_activity where repair_instance_id = #{repairInstanceId}
        )
    </select>
</mapper>
