<?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.dormitory.mapper.DmsOutsideAccommodationAttachmentMapper">

    <resultMap type="DmsOutsideAccommodationAttachment" id="DmsOutsideAccommodationAttachmentResult">
        <result property="id" column="id"/>
        <result property="applyId" column="apply_id"/>
        <result property="attachmentName" column="attachment_name"/>
        <result property="attachmentUrl" column="attachment_url"/>
        <result property="attachmentType" column="attachment_type"/>
        <result property="fileSize" column="file_size"/>
        <result property="fileSuffix" column="file_suffix"/>
        <result property="createTime" column="create_time"/>
        <result property="processInstanceId" column="process_instance_id"/>
        <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="outsideAccommodationApplies"
                    column="{studentName = student_name, studentNo = student_no, id = apply_id}"
                    javaType="java.util.ArrayList" select="selectOutsideAccommodationApplyByStuName"/>
    </resultMap>

    <!--外宿申请表-->
    <resultMap type="DmsOutsideAccommodationApply" id="DmsOutsideAccommodationApplyResult">
        <result property="id" column="id"/>
        <result property="applyNo" column="apply_no"/>
        <result property="studentId" column="student_id"/>
        <result property="studentNo" column="student_no"/>
        <result property="studentName" column="student_name"/>
        <result property="gender" column="gender"/>
        <result property="birthDate" column="birth_date"/>
        <result property="deptId" column="dept_id"/>
        <result property="deptName" column="dept_name"/>
        <result property="majorId" column="major_id"/>
        <result property="majorName" column="major_name"/>
        <result property="classId" column="class_id"/>
        <result property="className" column="class_name"/>
        <result property="originalDormitory" column="original_dormitory"/>
        <result property="accommodationFee" column="accommodation_fee"/>
        <result property="accommodationFeeStatus" column="accommodation_fee_status"/>
        <result property="applyReason" column="apply_reason"/>
        <result property="outsideAddress" column="outside_address"/>
        <result property="address" column="address"/>
        <result property="emergencyPhone" column="emergency_phone"/>
        <result property="emergencyContact" column="emergency_contact"/>
        <result property="parentOpinion" column="parent_opinion"/>
        <result property="parentSignAttachment" column="parent_sign_attachment"/>
        <result property="parentPhone" column="parent_phone"/>
        <result property="parentAddress" column="parent_address"/>
        <result property="parentDetailAddress" column="parent_detail_address"/>
        <result property="studentPromiseSign" column="student_promise_sign"/>
        <result property="promiseDate" column="promise_date"/>
        <result property="startDate" column="start_date"/>
        <result property="endDate" column="end_date"/>
        <result property="status" column="status"/>
        <result property="rejectReason" column="reject_reason"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="isValid" column="is_valid"/>
        <result property="teacherName" column="teacher_name"/>
        <result property="affixId" column="affix_id"/>
        <result property="deployId" column="deploy_id"/>
        <result property="processInstanceId" column="process_instance_id"/>
        <result property="idCard" column="idCard"/>
        <result property="studentPhone" column="studentPhone"/>
        <result property="studentSignature" column="student_signature"/>
        <result property="promiseContent"    column="promise_content"    />
        <!--外宿申请表-审核记录 （多条件查询column里传入了多条件【{studentName = student_name, studentNo = student_no}】javaType里面写了list表明你有多条件 studentName student_name字段）-->
        <collection property="outsideAccommodationApprovals"
                    column="{studentName = student_name, studentNo = student_no}"
                    javaType="java.util.ArrayList" select="selectOutsideAccommodationApprovalByStuName"/>
    </resultMap>

    <!--  外宿申请表 -->
    <select id="selectOutsideAccommodationApplyByStuName" resultMap="DmsOutsideAccommodationApplyResult">
        select *
        from dms_outside_accommodation_apply
        <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="DmsOutsideAccommodationApproval" id="DmsOutsideAccommodationApprovalResult">
        <result property="id" column="id"/>
        <result property="applyId" column="apply_id"/>
        <result property="applyNo" column="apply_no"/>
        <result property="approvalNode" column="approval_node"/>
        <result property="approverId" column="approver_id"/>
        <result property="approverName" column="approver_name"/>
        <result property="approverRole" column="approver_role"/>
        <result property="approvalOpinion" column="approval_opinion"/>
        <result property="approvalResult" column="approval_result"/>
        <result property="approvalTime" column="approval_time"/>
        <result property="processInstanceId" column="process_instance_id"/>
        <result property="studentName" column="student_name"/>
        <result property="studentNo" column="student_no"/>
    </resultMap>

    <!--  外宿申请表-审核记录子表，根据学生姓名、学号查询  -->
    <select id="selectOutsideAccommodationApprovalByStuName" resultMap="DmsOutsideAccommodationApprovalResult">
        select *
        from dms_outside_accommodation_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="selectDmsOutsideAccommodationAttachmentVo">
        select id,
               apply_id,
               attachment_name,
               attachment_url,
               attachment_type,
               file_size,
               file_suffix,
               create_time,
               process_instance_id,
               student_name,
               student_no
        from dms_outside_accommodation_attachment
    </sql>

    <select id="selectDmsOutsideAccommodationAttachmentList" parameterType="DmsOutsideAccommodationAttachment"
            resultMap="DmsOutsideAccommodationAttachmentResult">
        <include refid="selectDmsOutsideAccommodationAttachmentVo"/>
        <where>
            <if test="applyId != null ">and apply_id = #{applyId}</if>
            <if test="attachmentName != null  and attachmentName != ''">and attachment_name like concat('%',
                #{attachmentName}, '%')
            </if>
            <if test="attachmentUrl != null  and attachmentUrl != ''">and attachment_url = #{attachmentUrl}</if>
            <if test="attachmentType != null ">and attachment_type = #{attachmentType}</if>
            <if test="fileSize != null ">and file_size = #{fileSize}</if>
            <if test="fileSuffix != null  and fileSuffix != ''">and file_suffix = #{fileSuffix}</if>
            <if test="processInstanceId != null  and processInstanceId != ''">and process_instance_id =
                #{processInstanceId}
            </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="selectDmsOutsideAccommodationAttachmentById" parameterType="Long"
            resultMap="DmsOutsideAccommodationAttachmentResult">
        <include refid="selectDmsOutsideAccommodationAttachmentVo"/>
        where id = #{id}
    </select>

    <!-- 新增：批量插入方法 -->
    <insert id="batchInsertDmsOutsideAccommodationAttachment" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
        insert into dms_outside_accommodation_attachment
        (
        apply_id,
        attachment_name,
        attachment_url,
        attachment_type,
        file_size,
        file_suffix,
        create_time,
        process_instance_id,
        student_name,
        student_no
        )
        values
        <foreach collection="list" item="item" separator=",">
            (
            #{item.applyId},
            #{item.attachmentName},
            #{item.attachmentUrl},
            #{item.attachmentType},
            #{item.fileSize},
            #{item.fileSuffix},
            #{item.createTime},
            #{item.processInstanceId},
            #{item.studentName},
            #{item.studentNo}
            )
        </foreach>
    </insert>

    <insert id="insertDmsOutsideAccommodationAttachment" parameterType="DmsOutsideAccommodationAttachment"
            useGeneratedKeys="true" keyProperty="id">
        insert into dms_outside_accommodation_attachment
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="applyId != null">apply_id,</if>
            <if test="attachmentName != null and attachmentName != ''">attachment_name,</if>
            <if test="attachmentUrl != null and attachmentUrl != ''">attachment_url,</if>
            <if test="attachmentType != null">attachment_type,</if>
            <if test="fileSize != null">file_size,</if>
            <if test="fileSuffix != null and fileSuffix != ''">file_suffix,</if>
            <if test="createTime != null">create_time,</if>
            <if test="processInstanceId != null">process_instance_id,</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="attachmentName != null and attachmentName != ''">#{attachmentName},</if>
            <if test="attachmentUrl != null and attachmentUrl != ''">#{attachmentUrl},</if>
            <if test="attachmentType != null">#{attachmentType},</if>
            <if test="fileSize != null">#{fileSize},</if>
            <if test="fileSuffix != null and fileSuffix != ''">#{fileSuffix},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="processInstanceId != null">#{processInstanceId},</if>
            <if test="studentName != null and studentName != ''">#{studentName},</if>
            <if test="studentNo != null and studentNo != ''">#{studentNo},</if>
        </trim>
    </insert>

    <update id="updateDmsOutsideAccommodationAttachment" parameterType="DmsOutsideAccommodationAttachment">
        update dms_outside_accommodation_attachment
        <trim prefix="SET" suffixOverrides=",">
            <if test="applyId != null">apply_id = #{applyId},</if>
            <if test="attachmentName != null and attachmentName != ''">attachment_name = #{attachmentName},</if>
            <if test="attachmentUrl != null and attachmentUrl != ''">attachment_url = #{attachmentUrl},</if>
            <if test="attachmentType != null">attachment_type = #{attachmentType},</if>
            <if test="fileSize != null">file_size = #{fileSize},</if>
            <if test="fileSuffix != null and fileSuffix != ''">file_suffix = #{fileSuffix},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="processInstanceId != null">process_instance_id = #{processInstanceId},</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>

    <delete id="deleteDmsOutsideAccommodationAttachmentById" parameterType="Long">
        delete
        from dms_outside_accommodation_attachment
        where id = #{id}
    </delete>

    <delete id="deleteOutsideAccommodationAttachmentNameAndStuName" parameterType="String">
        delete
        from dms_outside_accommodation_attachment
        where attachment_name = #{attachmentName} and student_name = #{studentName}
    </delete>

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