<?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.pnkx.system.mapper.SysFileMapper">

    <resultMap type="com.pnkx.system.domain.SysFile" id="SysFileResult">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="url" column="url"/>
        <result property="path" column="path"/>
        <result property="thumbnail" column="thumbnail"/>
        <result property="port" column="port"/>
        <result property="type" column="type"/>
        <result property="thumb" column="thumb"/>
        <result property="browse" column="browse"/>
        <result property="version" column="version"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectSysFileVo">
        select id,
               name,
               url,
               path,
               thumbnail,
               port,
               browse,
               type,
               thumb,
               version,
               create_by,
               create_time,
               update_by,
               update_time,
               remark
        from sys_file
    </sql>

    <select id="selectSysFileList" parameterType="com.pnkx.system.domain.SysFile" resultMap="SysFileResult">
        <include refid="selectSysFileVo"/>
        <where>
            <if test="name != null  and name != ''">and (`name` like concat('%', #{name}, '%') or remark like concat('%', #{name}, '%'))</if>
            <if test="port != null  and port != ''">and port like concat('%', #{port}, '%')</if>
            <if test="type != null  and type != ''">and `type` = #{type}</if>
            <if test="path != null  and path != ''">and `path` = #{path}</if>
            <if test="path != null  and path != ''">and thumbnail = #{thumbnail}</if>
            <if test="version != null  and version != ''">and version = #{version}</if>
        </where>
    </select>

    <select id="selectSysFileById" parameterType="Long" resultMap="SysFileResult">
        <include refid="selectSysFileVo"/>
        where id = #{id}
    </select>
    <select id="selectSysFileByIds" parameterType="string" resultMap="SysFileResult">
        <include refid="selectSysFileVo"/>
        where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <!-- 获取随机图片 -->
    <select id="getRandomPicture" resultType="com.pnkx.system.domain.SysFile">
        <include refid="selectSysFileVo"/>
         where type = 'tk' ORDER BY RAND() LIMIT #{number};
    </select>

    <insert id="insertSysFile" parameterType="com.pnkx.system.domain.SysFile" useGeneratedKeys="true" keyProperty="id">
        insert into sys_file
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="name != null">`name`,</if>
            <if test="url != null">url,</if>
            <if test="path != null">`path`,</if>
            <if test="thumbnail != null">thumbnail,</if>
            <if test="port != null">port,</if>
            <if test="type != null">`type`,</if>
            <if test="thumb != null">thumb,</if>
            <if test="browse != null">`browse`,</if>
            <if test="version != null">version,</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>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="name != null">#{name},</if>
            <if test="url != null">#{url},</if>
            <if test="path != null">#{path},</if>
            <if test="thumbnail != null">#{thumbnail},</if>
            <if test="port != null">#{port},</if>
            <if test="type != null">#{type},</if>
            <if test="thumb != null">#{thumb},</if>
            <if test="browse != null">#{browse},</if>
            <if test="version != null">#{version},</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>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateSysFile" parameterType="com.pnkx.system.domain.SysFile">
        update sys_file
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="url != null">url = #{url},</if>
            <if test="path != null">`path` = #{path},</if>
            <if test="thumbnail != null">thumbnail = #{thumbnail},</if>
            <if test="port != null">port = #{port},</if>
            <if test="thumb != null">thumb = #{thumb},</if>
            <if test="browse != null">`browse` = #{browse},</if>
            <if test="version != null">version = #{version},</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>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <!--浏览+1-->
    <update id="browse">
        update sys_file
        set browse = browse + 1
        where id = #{id}
    </update>

    <!--点赞+1-->
    <update id="like">
        update sys_file
        set thumb = thumb + 1
        where id = #{id}
    </update>

    <delete id="deleteSysFileById" parameterType="Long">
        delete
        from sys_file
        where id = #{id}
    </delete>

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