<?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.jeeplus.finance.projectDict.mapper.ProjectDictMapper">

    <select id="parentList" resultType="com.jeeplus.finance.projectDict.domain.vo.FileDictVo">
        SELECT
            id AS file_id,
            create_by_id,
            create_time,
            update_by_id,
            update_time,
            del_flag,
            remarks,
            parent_id,
            `name`,
            type,
            sort,
            size
        FROM
            program_file_dict
        WHERE
            del_flag = 0
            AND parent_id = 0
            ORDER BY sort
    </select>

    <select id="list" resultType="com.jeeplus.finance.projectDict.domain.vo.FileDictVo">
        SELECT
            a.id,
            a.required_stage,
            b.id AS file_id,
            b.create_by_id,
            b.create_time,
            b.update_by_id,
            b.update_time,
            b.del_flag,
            b.remarks,
            b.parent_id,
            b.`name`,
            b.type,
            b.sort,
            b.size
        FROM
            program_project_dict a
            LEFT JOIN program_file_dict b ON a.file_id = b.id
        WHERE
            a.del_flag = 0
            AND a.attachment_project_type = #{type}
            AND a.attachment_project_sort = #{attachmentProjectSort}
        <if test="projectLevel != null and projectLevel != ''">
            AND a.project_level = #{projectLevel}
        </if>

            ORDER BY b.sort
    </select>

    <select id="getFileParentList" resultType="com.jeeplus.finance.fileDict.domain.ProgramFileDict">
        SELECT
        *
        FROM
        program_file_dict
        WHERE
        del_flag = 0
        AND parent_id = #{id}
        <if test="fileIds != null">
            AND id NOT IN
            <foreach collection="fileIds" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ORDER BY sort
    </select>

    <select id="getFileList" resultType="com.jeeplus.finance.fileDict.domain.ProgramFileDict">
        SELECT
            *
        FROM
            program_file_dict
        WHERE
            del_flag = 0
            AND parent_id = (SELECT parent_id FROM program_file_dict WHERE del_flag = 0 AND id = #{id})
            <if test="fileIds != null">
                AND id NOT IN
                <foreach collection="fileIds" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            ORDER BY sort
    </select>

    <select id="findById" resultType="com.jeeplus.finance.projectDict.domain.vo.FileDictVo">
        SELECT
            a.id,
            a.required_stage,
            b.id AS file_id,
            b.create_by_id,
            b.create_time,
            b.update_by_id,
            b.update_time,
            b.del_flag,
            b.remarks,
            b.parent_id,
            b.`name`,
            b.type,
            b.sort,
            b.size
        FROM
            program_project_dict a
            LEFT JOIN program_file_dict b ON a.file_id = b.id
        WHERE
            a.del_flag = 0
            AND a.id = #{id}
    </select>
    <select id="getParentList" resultType="com.jeeplus.finance.projectDict.domain.vo.FileDictVo">
        SELECT
        id AS file_id,
        create_by_id,
        create_time,
        update_by_id,
        update_time,
        del_flag,
        remarks,
        parent_id,
        `name`,
        type,
        sort,
        size
        FROM
        program_file_dict
        <where>
            del_flag = 0
            AND parent_id = 0
            <if test="sortList != null">
                AND sort IN
                <foreach collection="sortList" item="sort" index="index" open="(" close=")" separator=",">
                    #{sort}
                </foreach>
            </if>
            ORDER BY sort
        </where>
    </select>
    <select id="getFileListByAttachmentAndModuleType"
            resultType="com.jeeplus.sys.service.dto.WorkAttachmentInfoDTO">
        select
        wa.id,
        wa.url,
        wa.attachment_name AS `name`,
        wa.create_by_id AS `createBy.id`,
        su.name AS `createBy.name`,
        wa.create_time,
        wa.type as "type",
        wa.attachment_id as "attachmentId",
        wa.attachment_name as "attachmentName",
        wa.attachment_flag as "attachmentFlag",
        wa.module_type as "moduleType",
        wa.attachment_type as "attachmentType",
        wa.file_size as "fileSize",
        wa.sort as "sort",
        wa.description as "description"
        from
        work_attachment wa
        left join sys_user su on su.id = wa.create_by_id
        <where>
            wa.del_flag = 0
            AND wa.attachment_id = #{attachmentId}
            AND wa.module_type = #{moduleType}
        </where>
    </select>

</mapper>
