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

    <resultMap type="DmsNewRecord" id="DmsNewRecordResult">
        <result property="id"    column="id"    />
        <result property="stuNo"    column="stu_no"    />
        <result property="roomId"    column="room_id"    />
        <result property="startDate"    column="start_date"    />
        <result property="endDate"    column="end_date"    />
        <result property="inMonth"    column="in_month"    />
        <result property="mainMoney"    column="main_money"    />
        <result property="needMoney"    column="need_money"    />
        <result property="bankCard"    column="bank_card"    />
        <result property="stuYearId"    column="stu_year_id"    />
        <result property="stuPhone"    column="stu_phone"    />
        <result property="applyStatus"    column="apply_status"    />
        <result property="isHead"    column="is_head"    />
    </resultMap>

    <sql id="selectDmsNewRecordVo">
        select id,is_head, stu_no, room_id, start_date, end_date, in_month, main_money, need_money, bank_card, stu_year_id, stu_phone, apply_status from dms_new_record
    </sql>

    <select id="selectDmsNewRecordList" parameterType="DmsNewRecord" resultType="DmsNewRecord">
        <if test="searchRole == 'fdy'">
            select t1.* from
            (
            select a1.*
            from view_dms_record as a1
            where a1.apply_status = 1
            union
            select a2.*
            from view_dms_record as a2
            where a2.apply_status != 1
            ) as t1
            <where>
                t1.apply_status = 1 or t1.apply_status = 2 or t1.apply_status = 3
                <if test="stuNo != null  and stuNo != ''">and t1.stu_no = #{stuNo}</if>
                <if test="roomId != null ">and t1.room_id = #{roomId}</if>
                <if test="startDate != null ">and t1.start_date = #{startDate}</if>
                <if test="endDate != null ">and t1.end_date = #{endDate}</if>
                <if test="inMonth != null ">and t1.in_month = #{inMonth}</if>
                <if test="mainMoney != null ">and t1.main_money = #{mainMoney}</if>
                <if test="needMoney != null ">and t1.need_money = #{needMoney}</if>
                <if test="bankCard != null  and bankCard != ''">and t1.bank_card = #{bankCard}</if>
                <if test="stuYearId != null ">and t1.stu_year_id = #{stuYearId}</if>
                <if test="stuPhone != null  and stuPhone != ''">and t1.stu_phone = #{stuPhone}</if>
                <if test="applyStatus != null  and applyStatus != ''">and t1.apply_status = #{applyStatus}</if>
                <if test="tNo != null">and t1.employee_id = #{tNo}</if>
            </where>
            order by t1.id desc
        </if>
        <if test="searchRole == 'jwc'">
            select t1.* from
            (
            select a1.*
            from view_dms_record as a1
            where a1.apply_status = 2
            union
            select a2.*
            from view_dms_record as a2
            where a2.apply_status != 2
            ) as t1
            <where>
                <if test="stuNo != null  and stuNo != ''">and t1.stu_no = #{stuNo}</if>
                <if test="roomId != null ">and t1.room_id = #{roomId}</if>
                <if test="startDate != null ">and t1.start_date = #{startDate}</if>
                <if test="endDate != null ">and t1.end_date = #{endDate}</if>
                <if test="inMonth != null ">and t1.in_month = #{inMonth}</if>
                <if test="mainMoney != null ">and t1.main_money = #{mainMoney}</if>
                <if test="needMoney != null ">and t1.need_money = #{needMoney}</if>
                <if test="bankCard != null  and bankCard != ''">and t1.bank_card = #{bankCard}</if>
                <if test="stuYearId != null ">and t1.stu_year_id = #{stuYearId}</if>
                <if test="stuPhone != null  and stuPhone != ''">and t1.stu_phone = #{stuPhone}</if>
                <if test="applyStatus != null  and applyStatus != ''">and t1.apply_status = #{applyStatus}</if>
                <if test="tNo != null">and t1.employee_id = #{tNo}</if>
            </where>
            order by t1.id desc
        </if>
        <if test="searchRole == null">
            select t1.*
            from view_dms_record as t1
            <where>
                <if test="stuNo != null  and stuNo != ''">and t1.stu_no = #{stuNo}</if>
                <if test="roomId != null ">and t1.room_id = #{roomId}</if>
                <if test="startDate != null ">and t1.start_date = #{startDate}</if>
                <if test="endDate != null ">and t1.end_date = #{endDate}</if>
                <if test="inMonth != null ">and t1.in_month = #{inMonth}</if>
                <if test="mainMoney != null ">and t1.main_money = #{mainMoney}</if>
                <if test="needMoney != null ">and t1.need_money = #{needMoney}</if>
                <if test="bankCard != null  and bankCard != ''">and t1.bank_card = #{bankCard}</if>
                <if test="stuYearId != null ">and t1.stu_year_id = #{stuYearId}</if>
                <if test="stuPhone != null  and stuPhone != ''">and t1.stu_phone = #{stuPhone}</if>
                <if test="applyStatus != null  and applyStatus != ''">and t1.apply_status = #{applyStatus}</if>
                <if test="tNo != null">and t1.employee_id = #{tNo}</if>
            </where>
            order by t1.id desc
        </if>
    </select>

    <select id="selectDmsNewRecordById" parameterType="Long" resultMap="DmsNewRecordResult">
        <include refid="selectDmsNewRecordVo"/>
        where id = #{id}
    </select>

    <insert id="insertDmsNewRecord" parameterType="DmsNewRecord" useGeneratedKeys="true" keyProperty="id">
        insert into dms_new_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="stuNo != null">stu_no,</if>
            <if test="roomId != null">room_id,</if>
            <if test="startDate != null">start_date,</if>
            <if test="endDate != null">end_date,</if>
            <if test="inMonth != null">in_month,</if>
            <if test="mainMoney != null">main_money,</if>
            <if test="needMoney != null">need_money,</if>
            <if test="bankCard != null">bank_card,</if>
            <if test="stuYearId != null">stu_year_id,</if>
            <if test="stuPhone != null">stu_phone,</if>
            <if test="applyStatus != null">apply_status,</if>
            <if test="rangeCode != null">range_code,</if>
            <if test="isHead != null">is_head,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="stuNo != null">#{stuNo},</if>
            <if test="roomId != null">#{roomId},</if>
            <if test="startDate != null">#{startDate},</if>
            <if test="endDate != null">#{endDate},</if>
            <if test="inMonth != null">#{inMonth},</if>
            <if test="mainMoney != null">#{mainMoney},</if>
            <if test="needMoney != null">#{needMoney},</if>
            <if test="bankCard != null">#{bankCard},</if>
            <if test="stuYearId != null">#{stuYearId},</if>
            <if test="stuPhone != null">#{stuPhone},</if>
            <if test="applyStatus != null">#{applyStatus},</if>
            <if test="rangeCode != null">#{rangeCode},</if>
            <if test="isHead != null">#{isHead},</if>
        </trim>
    </insert>

    <update id="updateDmsNewRecord" parameterType="DmsNewRecord">
        update dms_new_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="stuNo != null">stu_no = #{stuNo},</if>
            <if test="roomId != null">room_id = #{roomId},</if>
            <if test="startDate != null">start_date = #{startDate},</if>
            <if test="endDate != null">end_date = #{endDate},</if>
            <if test="inMonth != null">in_month = #{inMonth},</if>
            <if test="mainMoney != null">main_money = #{mainMoney},</if>
            <if test="needMoney != null">need_money = #{needMoney},</if>
            <if test="bankCard != null">bank_card = #{bankCard},</if>
            <if test="stuYearId != null">stu_year_id = #{stuYearId},</if>
            <if test="stuPhone != null">stu_phone = #{stuPhone},</if>
            <if test="applyStatus != null">apply_status = #{applyStatus},</if>
            <if test="rangeCode != null">range_code = #{rangeCode},</if>
            <if test="isHead != null">is_head = #{isHead},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDmsNewRecordById" parameterType="Long">
        delete from dms_new_record where id = #{id}
    </delete>

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

    <select id="listView" resultType="DomInfo">
        select a.*,b.`name` as stu_name,c.class_name,d.`name` as teacher_name ,d.phone_number as teacher_phone
        from view_dms_stu_total_record as a
        left join srs_student as b on a.stu_no = b.stu_no
        left join srs_class as c on b.class_id = c.class_id
        left join cph_teacher as d on c.teacher_id = d.teacher_id
        <where>
            <if test="stuYearId != null  and stuYearId != ''"> and a.stu_year_id = #{stuYearId}</if>
        </where>
        order by b.class_id desc
    </select>

    <!-- 根据年级ID列表查询学生用户ID -->
    <select id="selectStudentIdsByGrades" resultType="Long">
        SELECT DISTINCT u.user_id
        FROM srs_student ss
        left JOIN srs_class sc ON ss.class_id = sc.class_id
        left JOIN sys_user u ON ss.stu_no = u.user_name
        WHERE u.user_id IS NOT NULL
        AND sc.grade_id IN
        <foreach item="gradeId" collection="gradeIds" open="(" separator="," close=")">
            #{gradeId}
        </foreach>
    </select>

    <!-- 根据年级ID列表查询学生学号列表 -->
    <select id="selectStudentNosByGrades" resultType="String">
        SELECT DISTINCT ss.stu_no
        FROM srs_student ss
        left JOIN srs_class sc ON ss.class_id = sc.class_id
        WHERE ss.stu_no IS NOT NULL
        AND sc.grade_id IN
        <foreach item="gradeId" collection="gradeIds" open="(" separator="," close=")">
            #{gradeId}
        </foreach>
    </select>

    <select id="findByParam" resultType="DmsNewRecord" parameterType="DmsSearch">
        select a.*
        from dms_new_record as a
        left join view_stu_info as b on a.stu_no = b.stu_no
        <where>
            <if test="stuYearId != null  and stuYearId != ''"> and a.stu_year_id = #{stuYearId}</if>
            <if test="tNo != null  and tNo != ''"> and b.t_no = #{tNo}</if>
            <if test="classId != null  and classId != ''"> and b.class_id = #{classId}</if>
            <if test="gradeId != null  and classId != ''"> and b.class_id = #{classId}</if>
        </where>
    </select>

    <update id="fastConfirm" >
        UPDATE dms_new_record
        SET apply_status = (
            SELECT dict_value
            FROM sys_dict_data
            WHERE dict_type = 'dms_record_status'
              AND dict_label = '学工已确认'
            LIMIT 1
            )
        WHERE apply_status = (
            SELECT dict_value
            FROM sys_dict_data
            WHERE dict_type = 'dms_record_status'
          AND dict_label = '辅导员已确认'
            LIMIT 1
            )
    </update>

    <update id="confirmUnconfirmedStudents" >
            UPDATE dms_new_record
            SET apply_status = (
                SELECT dict_value
                FROM sys_dict_data
                WHERE dict_type = 'dms_record_status'
                  AND dict_label = '学工已确认'
                LIMIT 1
                )
            WHERE apply_status = (
                SELECT dict_value
                FROM sys_dict_data
                WHERE dict_type = 'dms_record_status'
              AND dict_label = '待确认'
                LIMIT 1
                )
    </update>

    <!-- 批量插入消息 -->
    <insert id="batchInsertCphMsg" parameterType="java.util.List">
        insert into cph_msg (sender, receiver, content, create_by, create_time, update_by, update_time)
        values
        <foreach collection="list" item="item" separator=",">
            (
            #{item.sender},
            #{item.receiver},
            #{item.content},
            #{item.createBy},
            #{item.createTime},
            #{item.updateBy},
            #{item.updateTime}
            )
        </foreach>
    </insert>

</mapper>
