<?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.ygtx.worklog.mapper.ReportAttachmentMapper">

    <resultMap type="ReportAttachment" id="ReportAttachmentResult">
        <result property="id"    column="id"    />
        <result property="reportId"    column="report_id"    />
        <result property="reportType"    column="report_type"    />
        <result property="fileName"    column="file_name"    />
        <result property="filePath"    column="file_path"    />
        <result property="fileSize"    column="file_size"    />
        <result property="fileType"    column="file_type"    />
        <result property="uploadBy"    column="upload_by"    />
        <result property="uploadTime"    column="upload_time"    />
    </resultMap>

    <sql id="selectReportAttachmentVo">
        select id, report_id, report_type, file_name, file_path, file_size,
               file_type, upload_by, upload_time
        from wl_report_attachment
    </sql>

    <select id="selectReportAttachmentList" parameterType="ReportAttachment" resultMap="ReportAttachmentResult">
        <include refid="selectReportAttachmentVo"/>
        <where>
            <if test="reportId != null "> and report_id = #{reportId}</if>
            <if test="reportType != null  and reportType != ''"> and report_type = #{reportType}</if>
            <if test="fileName != null  and fileName != ''"> and file_name like concat('%', #{fileName}, '%')</if>
        </where>
        order by upload_time desc
    </select>

    <select id="selectReportAttachmentById" parameterType="Long" resultMap="ReportAttachmentResult">
        <include refid="selectReportAttachmentVo"/>
        where id = #{id}
    </select>

    <select id="selectAttachmentsByReport" resultMap="ReportAttachmentResult">
        <include refid="selectReportAttachmentVo"/>
        where report_id = #{reportId} and report_type = #{reportType}
        order by upload_time asc
    </select>

    <insert id="insertReportAttachment" parameterType="ReportAttachment" useGeneratedKeys="true" keyProperty="id">
        insert into wl_report_attachment
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="reportId != null">report_id,</if>
            <if test="reportType != null and reportType != ''">report_type,</if>
            <if test="fileName != null and fileName != ''">file_name,</if>
            <if test="filePath != null and filePath != ''">file_path,</if>
            <if test="fileSize != null">file_size,</if>
            <if test="fileType != null and fileType != ''">file_type,</if>
            <if test="uploadBy != null and uploadBy != ''">upload_by,</if>
            <if test="uploadTime != null">upload_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="reportId != null">#{reportId},</if>
            <if test="reportType != null and reportType != ''">#{reportType},</if>
            <if test="fileName != null and fileName != ''">#{fileName},</if>
            <if test="filePath != null and filePath != ''">#{filePath},</if>
            <if test="fileSize != null">#{fileSize},</if>
            <if test="fileType != null and fileType != ''">#{fileType},</if>
            <if test="uploadBy != null and uploadBy != ''">#{uploadBy},</if>
            <if test="uploadTime != null">#{uploadTime},</if>
         </trim>
    </insert>

    <insert id="batchInsertReportAttachment" parameterType="java.util.List">
        insert into wl_report_attachment(report_id, report_type, file_name, file_path,
                                         file_size, file_type, upload_by, upload_time)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.reportId}, #{item.reportType}, #{item.fileName}, #{item.filePath},
             #{item.fileSize}, #{item.fileType}, #{item.uploadBy}, now())
        </foreach>
    </insert>

    <update id="updateReportAttachment" parameterType="ReportAttachment">
        update wl_report_attachment
        <trim prefix="SET" suffixOverrides=",">
            <if test="reportId != null">report_id = #{reportId},</if>
            <if test="reportType != null and reportType != ''">report_type = #{reportType},</if>
            <if test="fileName != null and fileName != ''">file_name = #{fileName},</if>
            <if test="filePath != null and filePath != ''">file_path = #{filePath},</if>
            <if test="fileSize != null">file_size = #{fileSize},</if>
            <if test="fileType != null and fileType != ''">file_type = #{fileType},</if>
            <if test="uploadBy != null and uploadBy != ''">upload_by = #{uploadBy},</if>
            <if test="uploadTime != null">upload_time = #{uploadTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteReportAttachmentById" parameterType="Long">
        delete from wl_report_attachment where id = #{id}
    </delete>

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

    <delete id="deleteAttachmentsByReport">
        delete from wl_report_attachment
        where report_id = #{reportId} and report_type = #{reportType}
    </delete>
</mapper>
