<?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.backendsys.modules.upload.dao.SysFileCategoryDao">

    <sql id="includeFileCategory">
        fc.id,
        fc.user_id,
        fc.category_name,
        fc.sort,
        fc.status
    </sql>

<!--    type="com.backendsys.modules.upload.entity.SysFileCategory"-->
    <resultMap id="resultMapFileCategoryList" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="user_id" column="user_id" javaType="java.lang.Long" />
        <result property="category_name" column="category_name" />
        <result property="category_file_count" column="category_file_count" javaType="java.lang.Integer" />
        <result property="sort" column="sort" javaType="java.lang.Integer" />
        <result property="status" column="status" javaType="java.lang.Integer" />
    </resultMap>

    <select id="selectUploadFileCategoryList" resultMap="resultMapFileCategoryList">
        SELECT <include refid="includeFileCategory" />, COUNT(f.id) AS category_file_count
        FROM sys_file_category fc
        LEFT JOIN sys_file f ON fc.id = f.category_id
        <where>
            <if test="user_id != null and user_id != ''">
                AND fc.user_id = #{user_id}
                AND (f.user_id = #{user_id} OR f.user_id IS NULL)
            </if>
            <if test="category_name != null and category_name != ''">
                AND fc.category_name LIKE CONCAT('%', #{category_name}, '%')
            </if>
            <if test="status != null and status != ''">
                AND fc.status = #{status}
            </if>
        </where>
        GROUP BY fc.id
        ORDER BY fc.sort DESC
    </select>

    <!-- 批量插入 -->
    <insert id="insertBatch" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
        <foreach collection="list" item="item" separator=";">
            INSERT INTO sys_file_category
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="item.user_id != null">user_id,</if>
                <if test="item.category_name != null and item.category_name != ''">category_name,</if>
                <if test="item.sort != null">sort,</if>
                <if test="item.status != null">status,</if>
            </trim>
            VALUES
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="item.user_id != null">#{item.user_id},</if>
                <if test="item.category_name != null and item.category_name != ''">#{item.category_name},</if>
                <if test="item.sort != null">#{item.sort},</if>
                <if test="item.status != null">#{item.status},</if>
            </trim>
        </foreach>
    </insert>

    <!-- 批量更新 -->
    <update id="updateByIdBatch" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
        <foreach collection="list" item="item" separator=";">
            UPDATE sys_file_category SET
            <trim suffixOverrides="," suffix=" ">
                <if test="item.category_name != null and item.category_name != ''">category_name = #{item.category_name},</if>
                <if test="item.sort != null and item.sort != ''">sort = #{item.sort},</if>
                <if test="item.status != null and item.status != ''">status = #{item.status},</if>
            </trim>
            WHERE id = #{item.id}
        </foreach>
    </update>


</mapper>