<?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,
        COALESCE(fc.suffix, '') suffix,
        fc.sort,
        fc.status
    </sql>

    <resultMap id="resultMapFileCategoryList" type="com.backendsys.modules.upload.entity.SysFileCategory">
        <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="suffix" column="suffix" />
        <result property="suffix" column="suffix" />
        <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}
            </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>

</mapper>