<?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.ruoyi.file.mapper.DownloadRecordMapper">

    <resultMap type="DownloadRecord" id="DownloadRecordResult">
        <result property="recordId"    column="record_id"    />
        <result property="userId"    column="user_id"    />
        <result property="fileNames"    column="file_names"    />
        <result property="downloadTime"    column="download_time"    />
        <result property="ipaddr"    column="ipaddr"    />
        <result property="location"    column="location"    />
        <result property="browser"    column="browser"    />
        <result property="os"    column="os"    />
        <result property="nickName"    column="nick_name"    />
    </resultMap>

    <resultMap id="DownloadRecordDownloadRecordItemResult" type="DownloadRecord" extends="DownloadRecordResult">
        <collection property="downloadRecordItemList" notNullColumn="sub_item_id" javaType="java.util.List" resultMap="DownloadRecordItemResult" />
    </resultMap>

    <resultMap type="DownloadRecordItem" id="DownloadRecordItemResult">
        <result property="itemId"    column="sub_item_id"    />
        <result property="recordId"    column="sub_record_id"    />
        <result property="userFileId"    column="sub_userFileId"    />
        <association property="userFile" javaType="com.ruoyi.file.domain.UserFile"
                     select="com.ruoyi.file.mapper.UserFileMapper.selectByUserFileId" column="sub_userFileId"/>
    </resultMap>

    <sql id="selectDownloadRecordVo">
        select record_id, user_id, file_names, download_time, ipaddr, location, browser, os from download_record
    </sql>

    <select id="selectDownloadRecordList" parameterType="DownloadRecord" resultMap="DownloadRecordResult">
        select a.record_id, a.user_id, a.file_names, a.download_time, a.ipaddr, a.location, a.browser, a.os, u.nick_name as nick_name
        from download_record a
        left join sys_user u on u.user_id = a.user_id
        <where>
            <if test="params.beginDownloadTime != null and params.beginDownloadTime != '' and params.endDownloadTime != null and params.endDownloadTime != ''"> and download_time between #{params.beginDownloadTime} and #{params.endDownloadTime}</if>
            <if test="location != null  and location != ''"> and location like concat('%', #{location}, '%')</if>
        </where>
        order by a.download_time desc
    </select>

    <select id="selectDownloadRecordByRecordId" parameterType="Long" resultMap="DownloadRecordDownloadRecordItemResult">
        select a.record_id, a.user_id, a.file_names, a.download_time, a.ipaddr, a.location, a.browser, a.os,
 b.item_id as sub_item_id, b.record_id as sub_record_id, b.userFileId as sub_userFileId
        from download_record a
        left join download_record_item b on b.record_id = a.record_id
        where a.record_id = #{recordId}
    </select>

    <insert id="insertDownloadRecord" parameterType="DownloadRecord" useGeneratedKeys="true" keyProperty="recordId">
        insert into download_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="fileNames != null">file_names,</if>
            <if test="downloadTime != null">download_time,</if>
            <if test="ipaddr != null">ipaddr,</if>
            <if test="location != null">location,</if>
            <if test="browser != null">browser,</if>
            <if test="os != null">os,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="fileNames != null">#{fileNames},</if>
            <if test="downloadTime != null">#{downloadTime},</if>
            <if test="ipaddr != null">#{ipaddr},</if>
            <if test="location != null">#{location},</if>
            <if test="browser != null">#{browser},</if>
            <if test="os != null">#{os},</if>
         </trim>
    </insert>

    <update id="updateDownloadRecord" parameterType="DownloadRecord">
        update download_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="fileNames != null">file_names = #{fileNames},</if>
            <if test="downloadTime != null">download_time = #{downloadTime},</if>
            <if test="ipaddr != null">ipaddr = #{ipaddr},</if>
            <if test="location != null">location = #{location},</if>
            <if test="browser != null">browser = #{browser},</if>
            <if test="os != null">os = #{os},</if>
        </trim>
        where record_id = #{recordId}
    </update>

    <delete id="deleteDownloadRecordByRecordId" parameterType="Long">
        delete from download_record where record_id = #{recordId}
    </delete>

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

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

    <delete id="deleteDownloadRecordItemByRecordId" parameterType="Long">
        delete from download_record_item where record_id = #{recordId}
    </delete>

    <insert id="batchDownloadRecordItem">
        insert into download_record_item( item_id, record_id, userFileId) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.itemId}, #{item.recordId}, #{item.userFileId})
        </foreach>
    </insert>
</mapper>
