<?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.DmsNeedCardStuMapper">
    
    <resultMap type="DmsNeedCardStu" id="DmsNeedCardStuResult">
        <result property="id"    column="id"    />
        <result property="needDate"    column="need_date"    />
        <result property="stuNo"    column="stu_no"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectDmsNeedCardStuVo">
        select id, need_date, stu_no, create_by, create_time, update_by, update_time from dms_need_card_stu
    </sql>

    <select id="selectDmsNeedCardStuList" parameterType="DmsNeedCardStu" resultMap="DmsNeedCardStuResult">
        <include refid="selectDmsNeedCardStuVo"/>
        <where>  
            <if test="needDate != null "> and need_date = #{needDate}</if>
            <if test="stuNo != null  and stuNo != ''"> and stu_no = #{stuNo}</if>
        </where>
    </select>
    
    <select id="selectDmsNeedCardStuById" parameterType="Long" resultMap="DmsNeedCardStuResult">
        <include refid="selectDmsNeedCardStuVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertDmsNeedCardStu" parameterType="DmsNeedCardStu" useGeneratedKeys="true" keyProperty="id">
        insert into dms_need_card_stu
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="needDate != null">need_date,</if>
            <if test="stuNo != null">stu_no,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="needDate != null">#{needDate},</if>
            <if test="stuNo != null">#{stuNo},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateDmsNeedCardStu" parameterType="DmsNeedCardStu">
        update dms_need_card_stu
        <trim prefix="SET" suffixOverrides=",">
            <if test="needDate != null">need_date = #{needDate},</if>
            <if test="stuNo != null">stu_no = #{stuNo},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDmsNeedCardStuById" parameterType="Long">
        delete from dms_need_card_stu where id = #{id}
    </delete>

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

    <select id="listView" parameterType="DmsNeedCardStu" resultType="DmsNeedCardStu">
        SELECT
        a.*,
        b.stu_name,
        b.class_name,
        b.dept_name,
        b.major_name,
        b.grade_name,
        d.campus_name,
        d.park_name,
        d.building_name,
        d.floor,
        d.room_no,
        CASE
        WHEN EXISTS (
        SELECT 1
        FROM dms_stu_daily t1
        WHERE t1.stu_no = a.stu_no
        AND t1.submit_time >= DATE(a.need_date)
        AND t1.submit_time &lt; DATE_ADD(DATE(a.need_date), INTERVAL 1 DAY)
        ) THEN '是'
        ELSE '否'
        END AS is_card
        FROM
        dms_need_card_stu a
        LEFT JOIN
        view_stu_info b ON a.stu_no = b.stu_no
        LEFT JOIN
        srs_dormitory_student c ON a.stu_no = c.stu_no
        LEFT JOIN
        view_dorm_info d ON c.dormitory_id = d.id
        <where>
            <!-- 基本查询条件 -->
            <if test="needDate != null">and Date(a.need_date) = Date(#{needDate})</if>
            <if test="stuNo != null and stuNo != ''">and a.stu_no = #{stuNo}</if>

            <!-- 学生信息查询条件 -->
            <if test="stuName != null and stuName != ''">and b.stu_name like concat('%', #{stuName}, '%')</if>
            <if test="className != null and className != ''">and b.class_name like concat('%', #{className}, '%')</if>
            <if test="deptName != null and deptName != ''">and b.dept_name like concat('%', #{deptName}, '%')</if>
            <if test="majorName != null and majorName != ''">and b.major_name like concat('%', #{majorName}, '%')</if>
            <if test="gradeName != null and gradeName != ''">and b.grade_name like concat('%', #{gradeName}, '%')</if>
            <if test="idCard != null and idCard != ''">and b.id_card = #{idCard}</if>
            <if test="teacherName != null and teacherName != ''">and b.teacher_name like concat('%', #{teacherName},
                '%')
            </if>

            <!-- 宿舍信息查询条件 -->
            <if test="campusName != null and campusName != ''">and d.campus_name like concat('%', #{campusName}, '%')
            </if>
            <if test="parkName != null and parkName != ''">and d.park_name like concat('%', #{parkName}, '%')</if>
            <if test="buildingName != null and buildingName != ''">and d.building_name like concat('%', #{buildingName},
                '%')
            </if>
            <if test="floor != null and floor != ''">and d.floor = #{floor}</if>
            <if test="roomNo != null and roomNo != ''">and d.room_no = #{roomNo}</if>

            <!-- ID条件查询 -->
            <if test="campusId != null">and d.campus_id = #{campusId}</if>
            <if test="parkId != null">and d.park_id = #{parkId}</if>
            <if test="buildingId != null">and d.building_id = #{buildingId}</if>
            <if test="floorId != null">and d.floor_id = #{floorId}</if>
            <if test="dormitoryId != null">and c.dormitory_id = #{dormitoryId}</if>
            <if test="deptId != null">and b.dept_id = #{deptId}</if>
            <if test="majorId != null">and b.major_id = #{majorId}</if>
            <if test="classId != null">and b.class_id = #{classId}</if>
            <if test="gradeId != null">and b.grade_id = #{gradeId}</if>

            <if test="fdyNo != null and fdyNo != ''">and b.t_no = #{fdyNo}</if>

            <!-- 新增：是否打卡查询条件 -->
            <if test="isCard != null and isCard != ''">
                <choose>
                    <when test='isCard != null and isCard.equals("是")'>
                        and EXISTS (
                        SELECT 1
                        FROM dms_stu_daily t1
                        WHERE t1.stu_no = a.stu_no
                        AND t1.submit_time >= DATE(a.need_date)
                        AND t1.submit_time &lt; DATE_ADD(DATE(a.need_date), INTERVAL 1 DAY)
                        )
                    </when>
                    <when test='isCard != null and isCard.equals("否")'>
                        and NOT EXISTS (
                        SELECT 1
                        FROM dms_stu_daily t1
                        WHERE t1.stu_no = a.stu_no
                        AND t1.submit_time >= DATE(a.need_date)
                        AND t1.submit_time &lt; DATE_ADD(DATE(a.need_date), INTERVAL 1 DAY)
                        )
                    </when>
                </choose>
            </if>
        </where>
        order by a.id desc
    </select>

    <select id="callAutoSetDailyCheckTask" statementType="CALLABLE">
        {CALL auto_set_daily_check_task()}
    </select>

</mapper>