<?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.yushu.system.mapper.SysFileShareMapper">
    
    <resultMap type="SysFileShare" id="SysFileShareResult">
        <result property="shareId"    column="share_id"    />
        <result property="fileId"    column="file_id"    />
        <result property="folderId"    column="folder_id"    />
        <result property="shareCode"    column="share_code"    />
        <result property="sharePassword"    column="share_password"    />
        <result property="shareType"    column="share_type"    />
        <result property="resourceType"    column="resource_type"    />
        <result property="expireTime"    column="expire_time"    />
        <result property="viewCount"    column="view_count"    />
        <result property="downloadCount"    column="download_count"    />
        <result property="maxDownload"    column="max_download"    />
        <result property="userId"    column="user_id"    />
        <result property="status"    column="status"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectSysFileShareVo">
        select share_id, file_id, folder_id, share_code, share_password, share_type, resource_type, expire_time, view_count, download_count, max_download, user_id, status, create_by, create_time from sys_file_share
    </sql>

    <select id="selectSysFileShareList" parameterType="SysFileShare" resultMap="SysFileShareResult">
        <include refid="selectSysFileShareVo"/>
        <where>  
            <if test="fileId != null "> and file_id = #{fileId}</if>
            <if test="shareCode != null  and shareCode != ''"> and share_code = #{shareCode}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectSysFileShareByShareId" parameterType="Long" resultMap="SysFileShareResult">
        <include refid="selectSysFileShareVo"/>
        where share_id = #{shareId}
    </select>
    
    <select id="selectSysFileShareByShareCode" parameterType="String" resultMap="SysFileShareResult">
        <include refid="selectSysFileShareVo"/>
        where share_code = #{shareCode}
    </select>
        
    <insert id="insertSysFileShare" parameterType="SysFileShare" useGeneratedKeys="true" keyProperty="shareId">
        insert into sys_file_share
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="fileId != null">file_id,</if>
            <if test="folderId != null">folder_id,</if>
            <if test="shareCode != null and shareCode != ''">share_code,</if>
            <if test="sharePassword != null">share_password,</if>
            <if test="shareType != null">share_type,</if>
            <if test="resourceType != null">resource_type,</if>
            <if test="expireTime != null">expire_time,</if>
            <if test="viewCount != null">view_count,</if>
            <if test="downloadCount != null">download_count,</if>
            <if test="maxDownload != null">max_download,</if>
            <if test="userId != null">user_id,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="fileId != null">#{fileId},</if>
            <if test="folderId != null">#{folderId},</if>
            <if test="shareCode != null and shareCode != ''">#{shareCode},</if>
            <if test="sharePassword != null">#{sharePassword},</if>
            <if test="shareType != null">#{shareType},</if>
            <if test="resourceType != null">#{resourceType},</if>
            <if test="expireTime != null">#{expireTime},</if>
            <if test="viewCount != null">#{viewCount},</if>
            <if test="downloadCount != null">#{downloadCount},</if>
            <if test="maxDownload != null">#{maxDownload},</if>
            <if test="userId != null">#{userId},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateSysFileShare" parameterType="SysFileShare">
        update sys_file_share
        <trim prefix="SET" suffixOverrides=",">
            <if test="fileId != null">file_id = #{fileId},</if>
            <if test="shareCode != null and shareCode != ''">share_code = #{shareCode},</if>
            <if test="sharePassword != null">share_password = #{sharePassword},</if>
            <if test="shareType != null">share_type = #{shareType},</if>
            <if test="expireTime != null">expire_time = #{expireTime},</if>
            <if test="viewCount != null">view_count = #{viewCount},</if>
            <if test="downloadCount != null">download_count = #{downloadCount},</if>
            <if test="maxDownload != null">max_download = #{maxDownload},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where share_id = #{shareId}
    </update>

    <delete id="deleteSysFileShareByShareId" parameterType="Long">
        delete from sys_file_share where share_id = #{shareId}
    </delete>

    <delete id="deleteSysFileShareByShareIds" parameterType="String">
        delete from sys_file_share where share_id in 
        <foreach item="shareId" collection="array" open="(" separator="," close=")">
            #{shareId}
        </foreach>
    </delete>
    
    <update id="increaseViewCount" parameterType="Long">
        update sys_file_share set view_count = view_count + 1 where share_id = #{shareId}
    </update>
    
    <update id="increaseDownloadCount" parameterType="Long">
        update sys_file_share set download_count = download_count + 1 where share_id = #{shareId}
    </update>
    
    <select id="selectSysFileShareByFileIdAndUserId" resultMap="SysFileShareResult">
        <include refid="selectSysFileShareVo"/>
        where file_id = #{fileId} and user_id = #{userId} and status = '0'
        and (expire_time is null or expire_time > now())
        limit 1
    </select>
    
    <select id="selectSysFileShareByFolderIdAndUserId" resultMap="SysFileShareResult">
        <include refid="selectSysFileShareVo"/>
        where folder_id = #{folderId} and user_id = #{userId} and status = '0'
        and (expire_time is null or expire_time > now())
        limit 1
    </select>
    
    <delete id="deleteSysFileShareByFileId" parameterType="Long">
        delete from sys_file_share where file_id = #{fileId}
    </delete>

</mapper>
