<?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.RtEnlistmentReserveMapper">

    <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"    />
        <!--入伍保留学籍申请表-审核记录 （多条件查询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"/>
        <!--入伍保留学籍申请表-附件记录 （多条件查询column里传入了多条件【{studentName = student_name, studentNo = student_no}】javaType里面写了list表明你有多条件 studentName student_name字段）-->
        <collection property="enlistmentReserveAttachList"
                    column="{studentName = student_name, studentNo = student_no, applyNo = apply_no}"
                    javaType="java.util.ArrayList" select="selectRtEnlistmentReserveAttachByStuName"/>
    </resultMap>
    <!--入伍保留学籍申请表-审核记录子表的映射 -->
    <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>

    <!--入伍保留学籍申请表-申请附件子表的映射 -->
    <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"/>
    </resultMap>

    <!--  入伍保留申请表-申请附件记录子表，根据学生姓名、学号，申请编号查询  -->
    <select id="selectRtEnlistmentReserveAttachByStuName" resultMap="RtEnlistmentReserveAttachResult">
        select *
        from rt_enlistment_reserve_attach
        <where>
            <if test="studentName != null and studentName != ''">
                and student_name = #{studentName}
            </if>
            <if test="studentNo != null and studentNo != ''">
                and student_no = #{studentNo}
            </if>
            <if test="applyNo != null and applyNo != ''">
                and apply_no = #{applyNo}
            </if>
        </where>
    </select>


    <sql id="selectRtEnlistmentReserveVo">
        select id,
               apply_no,
               student_id,
               teacher_name,
               student_name,
               gender,
               nation,
               grade,
               student_no,
               class_name,
               major,
               family_address,
               parent_phone,
               apply_reason,
               apply_status,
               process_instance_id,
               reserve_no,
               reserve_start_date,
               reserve_end_date,
               approval_no,
               create_time,
               update_time,
               affix_id,
               deploy_id
        from rt_enlistment_reserve
    </sql>

    <select id="selectRtEnlistmentReserveList" parameterType="RtEnlistmentReserve"
            resultMap="RtEnlistmentReserveResult">
        <include refid="selectRtEnlistmentReserveVo"/>
        <where>
            <if test="applyNo != null  and applyNo != ''">and apply_no = #{applyNo}</if>
            <if test="studentId != null ">and student_id = #{studentId}</if>
            <if test="teacherName != null  and teacherName != ''">and teacher_name like concat('%', #{teacherName},
                '%')
            </if>
            <if test="studentName != null  and studentName != ''">and student_name like concat('%', #{studentName},
                '%')
            </if>
            <if test="gender != null  and gender != ''">and gender = #{gender}</if>
            <if test="nation != null  and nation != ''">and nation = #{nation}</if>
            <if test="grade != null  and grade != ''">and grade = #{grade}</if>
            <if test="studentNo != null  and studentNo != ''">and student_no = #{studentNo}</if>
            <if test="className != null  and className != ''">and class_name like concat('%', #{className}, '%')</if>
            <if test="major != null  and major != ''">and major = #{major}</if>
            <if test="familyAddress != null  and familyAddress != ''">and family_address = #{familyAddress}</if>
            <if test="parentPhone != null  and parentPhone != ''">and parent_phone = #{parentPhone}</if>
            <if test="applyReason != null  and applyReason != ''">and apply_reason = #{applyReason}</if>
            <if test="applyStatus != null ">and apply_status = #{applyStatus}</if>
            <if test="processInstanceId != null  and processInstanceId != ''">and process_instance_id =
                #{processInstanceId}
            </if>
            <if test="reserveNo != null  and reserveNo != ''">and reserve_no = #{reserveNo}</if>
            <if test="reserveStartDate != null ">and reserve_start_date = #{reserveStartDate}</if>
            <if test="reserveEndDate != null ">and reserve_end_date = #{reserveEndDate}</if>
            <if test="approvalNo != null  and approvalNo != ''">and approval_no = #{approvalNo}</if>
            <if test="affixId != null  and affixId != ''">and affix_id = #{affixId}</if>
            <if test="deployId != null  and deployId != ''"> and deploy_id = #{deployId}</if>
        </where>
    </select>

    <select id="selectRtEnlistmentReserveById" parameterType="Long" resultMap="RtEnlistmentReserveResult">
        <include refid="selectRtEnlistmentReserveVo"/>
        where id = #{id}
    </select>

    <!--根据学生姓名和学号查询申请表-->
    <select id="getEnlistmentReserveByStuNameAndStuNo" resultMap="RtEnlistmentReserveResult">
        select *
        from rt_enlistment_reserve
        <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>

    <!--  根据流程编号查询申请记录  -->
    <select id="selectRtEnlistmentReserveByProcessInstanceId" parameterType="String"
            resultMap="RtEnlistmentReserveResult">
        <include refid="selectRtEnlistmentReserveVo"/>
        where process_instance_id = #{processInstanceId}
    </select>

    <!--  根据学号查询辅导员信息  -->
    <select id="getCounselorInfo" resultType="com.srs.common.doman.vo.TeacherVo">
        SELECT d.user_id userId, d.dept_id deptId, d.user_name userName, d.nick_name nickName
        FROM srs_student a
                 LEFT JOIN srs_class b ON a.class_id = b.class_id
                 LEFT JOIN cph_teacher c ON b.teacher_id = c.teacher_id
                 LEFT JOIN sys_user d ON c.employee_id = d.user_name
        WHERE a.stu_no = #{stuNo}
    </select>

    <insert id="insertRtEnlistmentReserve" parameterType="RtEnlistmentReserve" useGeneratedKeys="true" keyProperty="id">
        insert into rt_enlistment_reserve
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="applyNo != null and applyNo != ''">apply_no,</if>
            <if test="studentId != null">student_id,</if>
            <if test="teacherName != null and teacherName != ''">teacher_name,</if>
            <if test="studentName != null and studentName != ''">student_name,</if>
            <if test="gender != null and gender != ''">gender,</if>
            <if test="nation != null and nation != ''">nation,</if>
            <if test="grade != null and grade != ''">grade,</if>
            <if test="studentNo != null and studentNo != ''">student_no,</if>
            <if test="className != null and className != ''">class_name,</if>
            <if test="major != null and major != ''">major,</if>
            <if test="familyAddress != null and familyAddress != ''">family_address,</if>
            <if test="parentPhone != null and parentPhone != ''">parent_phone,</if>
            <if test="applyReason != null and applyReason != ''">apply_reason,</if>
            <if test="applyStatus != null">apply_status,</if>
            <if test="processInstanceId != null">process_instance_id,</if>
            <if test="reserveNo != null">reserve_no,</if>
            <if test="reserveStartDate != null">reserve_start_date,</if>
            <if test="reserveEndDate != null">reserve_end_date,</if>
            <if test="approvalNo != null">approval_no,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="affixId != null">affix_id,</if>
            <if test="deployId != null">deploy_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="applyNo != null and applyNo != ''">#{applyNo},</if>
            <if test="studentId != null">#{studentId},</if>
            <if test="teacherName != null and teacherName != ''">#{teacherName},</if>
            <if test="studentName != null and studentName != ''">#{studentName},</if>
            <if test="gender != null and gender != ''">#{gender},</if>
            <if test="nation != null and nation != ''">#{nation},</if>
            <if test="grade != null and grade != ''">#{grade},</if>
            <if test="studentNo != null and studentNo != ''">#{studentNo},</if>
            <if test="className != null and className != ''">#{className},</if>
            <if test="major != null and major != ''">#{major},</if>
            <if test="familyAddress != null and familyAddress != ''">#{familyAddress},</if>
            <if test="parentPhone != null and parentPhone != ''">#{parentPhone},</if>
            <if test="applyReason != null and applyReason != ''">#{applyReason},</if>
            <if test="applyStatus != null">#{applyStatus},</if>
            <if test="processInstanceId != null">#{processInstanceId},</if>
            <if test="reserveNo != null">#{reserveNo},</if>
            <if test="reserveStartDate != null">#{reserveStartDate},</if>
            <if test="reserveEndDate != null">#{reserveEndDate},</if>
            <if test="approvalNo != null">#{approvalNo},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="affixId != null">#{affixId},</if>
            <if test="deployId != null">#{deployId},</if>
        </trim>
    </insert>

    <update id="updateRtEnlistmentReserve" parameterType="RtEnlistmentReserve">
        update rt_enlistment_reserve
        <trim prefix="SET" suffixOverrides=",">
            <if test="applyNo != null and applyNo != ''">apply_no = #{applyNo},</if>
            <if test="studentId != null">student_id = #{studentId},</if>
            <if test="teacherName != null and teacherName != ''">teacher_name = #{teacherName},</if>
            <if test="studentName != null and studentName != ''">student_name = #{studentName},</if>
            <if test="gender != null and gender != ''">gender = #{gender},</if>
            <if test="nation != null and nation != ''">nation = #{nation},</if>
            <if test="grade != null and grade != ''">grade = #{grade},</if>
            <if test="studentNo != null and studentNo != ''">student_no = #{studentNo},</if>
            <if test="className != null and className != ''">class_name = #{className},</if>
            <if test="major != null and major != ''">major = #{major},</if>
            <if test="familyAddress != null and familyAddress != ''">family_address = #{familyAddress},</if>
            <if test="parentPhone != null and parentPhone != ''">parent_phone = #{parentPhone},</if>
            <if test="applyReason != null and applyReason != ''">apply_reason = #{applyReason},</if>
            <if test="applyStatus != null">apply_status = #{applyStatus},</if>
            <if test="processInstanceId != null">process_instance_id = #{processInstanceId},</if>
            <if test="reserveNo != null">reserve_no = #{reserveNo},</if>
            <if test="reserveStartDate != null">reserve_start_date = #{reserveStartDate},</if>
            <if test="reserveEndDate != null">reserve_end_date = #{reserveEndDate},</if>
            <if test="approvalNo != null">approval_no = #{approvalNo},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="affixId != null">affix_id = #{affixId},</if>
            <if test="deployId != null">deploy_id = #{deployId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteRtEnlistmentReserveById" parameterType="Long">
        delete
        from rt_enlistment_reserve
        where id = #{id}
    </delete>

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