<?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.generator.mapper.front.FrontProjectFileMapper">

    <resultMap type="FrontProjectFile" id="FrontProjectFileResult">
        <id property="fileId" column="file_id" />
        <result property="projectId" column="project_id" />
        <result property="templateType" column="template_type" />
        <result property="nodeId" column="node_id" />
        <result property="tableId" column="table_id" />
        <result property="fileName" column="file_name" />
        <result property="filePath" column="file_path" />
        <result property="fileContent" column="file_content" />
        <result property="contentHash" column="content_hash" />
        <result property="createTime" column="create_time" />
        <result property="updateTime" column="update_time" />
    </resultMap>

    <sql id="selectFrontProjectFileVo">
        select file_id, project_id, template_type, node_id, table_id, file_name, file_path, file_content, content_hash, create_time, update_time from front_project_file
    </sql>

    <select id="selectFrontProjectFileById" parameterType="Long" resultMap="FrontProjectFileResult">
        <include refid="selectFrontProjectFileVo"/>
        where file_id = #{fileId}
    </select>

    <select id="selectFrontProjectFileByLookup" resultMap="FrontProjectFileResult">
        <include refid="selectFrontProjectFileVo"/>
        where project_id = #{projectId}
          and template_type = #{templateType}
          and node_id = #{nodeId}
        <if test="tableId == null"> and table_id is null</if>
        <if test="tableId != null"> and table_id = #{tableId}</if>
    </select>

    <select id="selectFrontProjectFileList" parameterType="FrontProjectFile" resultMap="FrontProjectFileResult">
        <include refid="selectFrontProjectFileVo"/>
        <where>
            <if test="projectId != null"> and project_id = #{projectId}</if>
            <if test="templateType != null and templateType != ''"> and template_type = #{templateType}</if>
            <if test="tableId != null"> and table_id = #{tableId}</if>
            <if test="fileName != null and fileName != ''"> and file_name like concat('%', #{fileName}, '%')</if>
        </where>
    </select>

    <insert id="insertFrontProjectFile" parameterType="FrontProjectFile" useGeneratedKeys="true" keyProperty="fileId">
        insert into front_project_file
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectId != null">project_id,</if>
            <if test="templateType != null">template_type,</if>
            <if test="nodeId != null">node_id,</if>
            <if test="tableId != null">table_id,</if>
            <if test="fileName != null">file_name,</if>
            <if test="filePath != null">file_path,</if>
            <if test="fileContent != null">file_content,</if>
            <if test="contentHash != null">content_hash,</if>
            create_time,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="projectId != null">#{projectId},</if>
            <if test="templateType != null">#{templateType},</if>
            <if test="nodeId != null">#{nodeId},</if>
            <if test="tableId != null">#{tableId},</if>
            <if test="fileName != null">#{fileName},</if>
            <if test="filePath != null">#{filePath},</if>
            <if test="fileContent != null">#{fileContent},</if>
            <if test="contentHash != null">#{contentHash},</if>
            sysdate(),
        </trim>
    </insert>

    <update id="updateFrontProjectFile" parameterType="FrontProjectFile">
        update front_project_file
        <trim prefix="SET" suffixOverrides=",">
            <if test="fileName != null">file_name = #{fileName},</if>
            <if test="filePath != null">file_path = #{filePath},</if>
            <if test="fileContent != null">file_content = #{fileContent},</if>
            <if test="contentHash != null">content_hash = #{contentHash},</if>
            update_time = sysdate(),
        </trim>
        where file_id = #{fileId}
    </update>

    <delete id="deleteFrontProjectFileById" parameterType="Long">
        delete from front_project_file where file_id = #{fileId}
    </delete>

    <delete id="deleteFilesByProjectId" parameterType="Long">
        delete from front_project_file where project_id = #{projectId}
    </delete>
</mapper>
