<?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.srs.routine.mapper.RtEnlistmentReserveAttachMapper">

    <resultMap type="RtEnlistmentReserveAttach" id="RtEnlistmentReserveAttachResult">
        <result property="id" column="id"/>
        <result property="applyId" column="apply_id"/>
        <result property="fileName" column="file_name"/>
        <result property="filePath" column="file_path"/>
        <result property="fileSize" column="file_size"/>
        <result property="fileType" column="file_type"/>
        <result property="createTime" column="create_time"/>
        <result property="applyNo" column="apply_no"/>
        <result property="studentName" column="student_name"/>
        <result property="studentNo" column="student_no"/>
        <!--入伍保留学籍申请表 （多条件查询column里传入了多条件【{studentName = student_name, studentNo = student_no}】javaType里面写了list表明你有多条件 studentName student_name字段）-->
        <collection property="rtEnlistmentReserves"
                    column="{studentName = student_name, studentNo = student_no, id = apply_id}"
                    javaType="java.util.ArrayList" select="selectRtEnlistmentReserveByStuName"/>
    </resultMap>

    <resultMap type="RtEnlistmentReserve" id="RtEnlistmentReserveResult">
        <result property="id" column="id"/>
        <result property="applyNo" column="apply_no"/>
        <result property="studentId" column="student_id"/>
        <result property="teacherName" column="teacher_name"/>
        <result property="studentName" column="student_name"/>
        <result property="gender" column="gender"/>
        <result property="nation" column="nation"/>
        <result property="grade" column="grade"/>
        <result property="studentNo" column="student_no"/>
        <result property="className" column="class_name"/>
        <result property="major" column="major"/>
        <result property="familyAddress" column="family_address"/>
        <result property="parentPhone" column="parent_phone"/>
        <result property="applyReason" column="apply_reason"/>
        <result property="applyStatus" column="apply_status"/>
        <result property="processInstanceId" column="process_instance_id"/>
        <result property="reserveNo" column="reserve_no"/>
        <result property="reserveStartDate" column="reserve_start_date"/>
        <result property="reserveEndDate" column="reserve_end_date"/>
        <result property="approvalNo" column="approval_no"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="affixId" column="affix_id"/>
        <result property="deployId"    column="deploy_id"    />
        <result property="deptName" column="dept_name"/>
        <!--入伍保留学籍申请表-审核记录 （多条件查询column里传入了多条件【{studentName = student_name, studentNo = student_no}】javaType里面写了list表明你有多条件 studentName student_name字段）-->
        <collection property="enlistmentReserveApprovalList"
                    column="{studentName = student_name, studentNo = student_no}"
                    javaType="java.util.ArrayList" select="selectRtEnlistmentReserveApprovalByStuName"/>
    </resultMap>

    <!--  入伍保留申请表 -->
    <select id="selectRtEnlistmentReserveByStuName" resultMap="RtEnlistmentReserveResult">
        select *
        from rt_enlistment_reserve
        <where>
            <if test="id != null and id != ''">
                and id = #{id}
            </if>
            <if test="studentName != null and studentName != ''">
                and student_name = #{studentName}
            </if>
            <if test="studentNo != null and studentNo != ''">
                and student_no = #{studentNo}
            </if>
        </where>
    </select>

    <!--入伍保留学籍申请表-审核记录子表的映射 -->
    <resultMap type="EnlistmentReserveApproval" id="RtEnlistmentReserveApprovalResult">
        <result property="id" column="id"/>
        <result property="applyId" column="apply_id"/>
        <result property="processInstanceId" column="process_instance_id"/>
        <result property="taskId" column="task_id"/>
        <result property="nodeName" column="node_name"/>
        <result property="approverId" column="approver_id"/>
        <result property="approverName" column="approver_name"/>
        <result property="approvalOpinion" column="approval_opinion"/>
        <result property="approvalResult" column="approval_result"/>
        <result property="approvalTime" column="approval_time"/>
        <result property="studentName" column="student_name"/>
        <result property="studentNo" column="student_no"/>
    </resultMap>

    <!--  入伍保留申请表-审核记录子表，根据学生姓名、学号查询  -->
    <select id="selectRtEnlistmentReserveApprovalByStuName" resultMap="RtEnlistmentReserveApprovalResult">
        select *
        from rt_enlistment_reserve_approval
        <where>
            <if test="studentName != null and studentName != ''">
                and student_name = #{studentName}
            </if>
            <if test="studentNo != null and studentNo != ''">
                and student_no = #{studentNo}
            </if>
        </where>
    </select>

    <sql id="selectRtEnlistmentReserveAttachVo">
        select id,
               apply_id,
               file_name,
               file_path,
               file_size,
               file_type,
               create_time,
               apply_no,
               student_name,
               student_no
        from rt_enlistment_reserve_attach
    </sql>

    <select id="selectRtEnlistmentReserveAttachList" parameterType="RtEnlistmentReserveAttach"
            resultMap="RtEnlistmentReserveAttachResult">
        <include refid="selectRtEnlistmentReserveAttachVo"/>
        <where>
            <if test="applyId != null ">and apply_id = #{applyId}</if>
            <if test="fileName != null  and fileName != ''">and file_name like concat('%', #{fileName}, '%')</if>
            <if test="filePath != null  and filePath != ''">and file_path = #{filePath}</if>
            <if test="fileSize != null ">and file_size = #{fileSize}</if>
            <if test="fileType != null  and fileType != ''">and file_type = #{fileType}</if>
            <if test="applyNo != null  and applyNo != ''">and apply_no = #{applyNo}</if>
            <if test="studentName != null  and studentName != ''">and student_name like concat('%', #{studentName},
                '%')
            </if>
            <if test="studentNo != null  and studentNo != ''">and student_no = #{studentNo}</if>
        </where>
    </select>

    <select id="selectRtEnlistmentReserveAttachById" parameterType="Long" resultMap="RtEnlistmentReserveAttachResult">
        <include refid="selectRtEnlistmentReserveAttachVo"/>
        where id = #{id}
    </select>

    <insert id="insertRtEnlistmentReserveAttach" parameterType="RtEnlistmentReserveAttach" useGeneratedKeys="true"
            keyProperty="id">
        insert into rt_enlistment_reserve_attach
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="applyId != null">apply_id,</if>
            <if test="fileName != null and fileName != ''">file_name,</if>
            <if test="filePath != null and filePath != ''">file_path,</if>
            <if test="fileSize != null">file_size,</if>
            <if test="fileType != null and fileType != ''">file_type,</if>
            <if test="createTime != null">create_time,</if>
            <if test="applyNo != null and applyNo != ''">apply_no,</if>
            <if test="studentName != null and studentName != ''">student_name,</if>
            <if test="studentNo != null and studentNo != ''">student_no,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="applyId != null">#{applyId},</if>
            <if test="fileName != null and fileName != ''">#{fileName},</if>
            <if test="filePath != null and filePath != ''">#{filePath},</if>
            <if test="fileSize != null">#{fileSize},</if>
            <if test="fileType != null and fileType != ''">#{fileType},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="applyNo != null and applyNo != ''">#{applyNo},</if>
            <if test="studentName != null and studentName != ''">#{studentName},</if>
            <if test="studentNo != null and studentNo != ''">#{studentNo},</if>
        </trim>
    </insert>

    <update id="updateRtEnlistmentReserveAttach" parameterType="RtEnlistmentReserveAttach">
        update rt_enlistment_reserve_attach
        <trim prefix="SET" suffixOverrides=",">
            <if test="applyId != null">apply_id = #{applyId},</if>
            <if test="fileName != null and fileName != ''">file_name = #{fileName},</if>
            <if test="filePath != null and filePath != ''">file_path = #{filePath},</if>
            <if test="fileSize != null">file_size = #{fileSize},</if>
            <if test="fileType != null and fileType != ''">file_type = #{fileType},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="applyNo != null and applyNo != ''">apply_no = #{applyNo},</if>
            <if test="studentName != null and studentName != ''">student_name = #{studentName},</if>
            <if test="studentNo != null and studentNo != ''">student_no = #{studentNo},</if>
        </trim>
        where id = #{id}
    </update>

    <!-- 批量插入（复用单条的动态字段逻辑） -->
    <insert id="batchInsertRtEnlistmentReserveAttach" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
        insert into rt_enlistment_reserve_attach
        <!-- 提取公共字段列表（与单条插入的trim逻辑一致） -->
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="list != null and list.size() > 0">
                <!-- 取列表第一个元素判断字段是否需要包含（避免循环判断，提升性能） -->
                <if test="list[0].applyId != null">apply_id,</if>
                <if test="list[0].fileName != null and list[0].fileName != ''">file_name,</if>
                <if test="list[0].filePath != null and list[0].filePath != ''">file_path,</if>
                <if test="list[0].fileSize != null">file_size,</if>
                <if test="list[0].fileType != null and list[0].fileType != ''">file_type,</if>
                <if test="list[0].createTime != null">create_time,</if>
                <if test="list[0].applyNo != null and list[0].applyNo != ''">apply_no,</if>
                <if test="list[0].studentName != null and list[0].studentName != ''">student_name,</if>
                <if test="list[0].studentNo != null and list[0].studentNo != ''">student_no,</if>
            </if>
        </trim>
        values
        <!-- 循环列表项，生成多个values -->
        <foreach collection="list" item="item" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="item.applyId != null">#{item.applyId},</if>
                <if test="item.fileName != null and item.fileName != ''">#{item.fileName},</if>
                <if test="item.filePath != null and item.filePath != ''">#{item.filePath},</if>
                <if test="item.fileSize != null">#{item.fileSize},</if>
                <if test="item.fileType != null and item.fileType != ''">#{item.fileType},</if>
                <if test="item.createTime != null">#{item.createTime},</if>
                <if test="item.applyNo != null and item.applyNo != ''">#{item.applyNo},</if>
                <if test="item.studentName != null and item.studentName != ''">#{item.studentName},</if>
                <if test="item.studentNo != null and item.studentNo != ''">#{item.studentNo},</if>
            </trim>
        </foreach>
    </insert>

    <!-- 批量修改（根据ID更新，支持动态字段） -->
    <update id="batchUpdateRtEnlistmentReserveAttach" parameterType="java.util.List">
        <foreach collection="list" item="item" separator=";">
            update rt_enlistment_reserve_attach
            <set>
                <!-- 动态更新字段（仅更新非空/非空字符串的字段） -->
                <if test="item.applyId != null">apply_id = #{item.applyId},</if>
                <if test="item.fileName != null and item.fileName != ''">file_name = #{item.fileName},</if>
                <if test="item.filePath != null and item.filePath != ''">file_path = #{item.filePath},</if>
                <if test="item.fileSize != null">file_size = #{item.fileSize},</if>
                <if test="item.fileType != null and item.fileType != ''">file_type = #{item.fileType},</if>
                <if test="item.createTime != null">create_time = #{item.createTime},</if>
                <if test="item.applyNo != null and item.applyNo != ''">apply_no = #{item.applyNo},</if>
                <if test="item.studentName != null and item.studentName != ''">student_name = #{item.studentName},</if>
                <if test="item.studentNo != null and item.studentNo != ''">student_no = #{item.studentNo},</if>
                <!-- 可选：添加更新时间字段 -->
                <if test="item.updateTime != null">update_time = #{item.updateTime},</if>
            </set>
            <!-- 必须包含主键ID，否则无法定位要修改的记录 -->
            where id = #{item.id}
        </foreach>
    </update>

    <delete id="deleteRtEnlistmentReserveAttachById" parameterType="Long">
        delete
        from rt_enlistment_reserve_attach
        where id = #{id}
    </delete>

    <delete id="deleteRtEnlistmentReserveAttachByFileNameAndStuName" parameterType="String">
        delete
        from rt_enlistment_reserve_attach
        where file_name = #{fileName} and student_name = #{studentName}
    </delete>

    <delete id="deleteRtEnlistmentReserveAttachByIds" parameterType="String">
        delete from rt_enlistment_reserve_attach where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>