<?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.project.business.mapper.TtFileDatabaseMapper">

    <resultMap id="BaseResultMap" type="com.ruoyi.project.business.domain.train.TtFileDatabase">
        <id property="id" column="id" jdbcType="BIGINT"/>
        <result property="fileName" column="file_name" jdbcType="VARCHAR"/>
        <result property="fileType" column="file_type" jdbcType="VARCHAR"/>
        <result property="directoryId" column="directory_id" jdbcType="BIGINT"/>
        <result property="visibleRange1" column="visible_range1" jdbcType="VARCHAR"/>
        <result property="visibleRange2" column="visible_range2" jdbcType="VARCHAR"/>
        <result property="visibleRange3" column="visible_range3" jdbcType="VARCHAR"/>
        <result property="briefIntroduction" column="brief_introduction" jdbcType="VARCHAR"/>
        <result property="delFlag" column="del_flag" jdbcType="CHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="createBy" column="create_by" jdbcType="BIGINT"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="updateBy" column="update_by" jdbcType="BIGINT"/>
        <result property="remark" column="remark" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,file_name,file_type,
        directory_id,visible_range1,visible_range2,
        visible_range3,brief_introduction,del_flag,
        create_time,create_by,update_time,
        update_by,remark
    </sql>
    <update id="editVisibleRange">
        update tt_file_database
        <set>
            <if test="visibleRange1 != null">
                visible_range1 = #{visibleRange1},
            </if>
            <if test="visibleRange2 != null">
                visible_range2 = #{visibleRange2},
            </if>
            <if test="visibleRange3 != null">
                visible_range3 = #{visibleRange3},
            </if>
            update_time = now(),
            update_by = #{updateBy}
        </set>
        where id in <foreach collection="ids" item="id" open="(" close=")" separator=",">#{id}</foreach>

        <if test="roleKeys != null and roleKeys.size() > 0 or areaIds != null and areaIds.size() > 0">
            and (
            <trim prefixOverrides="or">
                <if test="roleKeys != null and roleKeys.size() > 0">
                    <foreach collection="roleKeys" item="roleKey" separator="or">
                        find_in_set(#{roleKey}, visible_range1)
                    </foreach>
                </if>
                or
                <if test="areaIds != null and areaIds.size() > 0">
                    <foreach collection="areaIds" item="areaId" separator="or">
                        find_in_set(#{areaId}, visible_range2)
                    </foreach>
                </if>
            </trim>
            )
        </if>
        
        
    </update>
    
    <!--<if test="roleIds != null and roleIds.size() > 0">
            and (
            <foreach collection="roleIds" item="roleId" separator="or">
                find_in_set(#{roleId}, tfd.visible_range1)
            </foreach>
            )
        </if>

        <if test="areaIds != null and areaIds.size() > 0">
            and (
            <foreach collection="areaIds" item="areaId" separator="or">
                find_in_set(#{areaId}, tfd.visible_range2)
            </foreach>
            )
        </if>-->

    <select id="selectTtFileDatabaseList" resultType="com.ruoyi.project.business.domain.vo.train.FileDatabaseRes">
        SELECT tfd.*, u1.user_name AS create_by_name, u2.user_name AS update_by_name
        
        FROM tt_file_database tfd
        LEFT JOIN sys_user u1 ON u1.user_id = tfd.create_by
        LEFT JOIN sys_user u2 ON u2.user_id = tfd.update_by
        WHERE tfd.del_flag = '0'

        <if test="directoryId != null">
            and tfd.directory_id = #{directoryId}
        </if>
        <if test="fileName != null and fileName != ''">
            and tfd.file_name like concat('%',#{fileName},'%')
        </if>
        <if test="fileType != null and fileType != ''">
            and tfd.file_type = #{fileType}
        </if>
        <if test="briefIntroduction != null and briefIntroduction != ''">
            and tfd.brief_introduction like concat('%',#{briefIntroduction},'%')
        </if>
        <if test="createByName != null">
            and u1.user_name like concat('%',#{createByName},'%')
        </if>
        <if test="createStart != null and createEnd != null">
            and tfd.create_time between #{createStart} and #{createEnd}
        </if>
          
        <if test="fileTypes != null and fileTypes.size() > 0">
            and tfd.file_type in 
            <foreach collection="fileTypes" item="fileType" open="(" close=")" separator=",">
                #{fileType}
            </foreach>
        </if>
        
        
        <if test="roleKeys != null and roleKeys.size() > 0 or areaIds != null and areaIds.size() > 0">
            and (
            <trim prefixOverrides="or">
                <if test="roleKeys != null and roleKeys.size() > 0">
                    <foreach collection="roleKeys" item="roleKey" separator="or">
                        find_in_set(#{roleKey}, tfd.visible_range1)
                    </foreach>
                </if>
                or
                <if test="areaIds != null and areaIds.size() > 0">
                    <foreach collection="areaIds" item="areaId" separator="or">
                        find_in_set(#{areaId}, tfd.visible_range2)
                    </foreach>
                </if>
            </trim>
            )
        </if>

    </select>
    <select id="selectTtFileDatabaseById" resultType="com.ruoyi.project.business.domain.train.TtFileDatabase">
        SELECT * FROM tt_file_database WHERE id = #{id}
    </select>
</mapper>
