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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.srs.dormitory.domain.SrsDormitoryStudent">
        <id column="id" property="id" />
        <result column="dormitory_id" property="dormitoryId" />
        <result column="bed_id" property="bedId" />
        <result column="stu_no" property="stuNo" />
        <result column="checkin_time" property="checkinTime" />
        <result column="in_status" property="inStatus" />
    </resultMap>

    <select id="findByParam" parameterType="DmsSearch" resultType="DomInfo">
        select a.*,b.bank_card,b.stu_phone,c.charge_standard
        from srs_dormitory_student as a
        left join view_stu_info as b on a.stu_no = b.stu_no
        left join view_dorm_info as c on a.dormitory_id = c.id
        <where>
            <if test="gradeId != null">and b.grade_id = #{gradeId}</if>
        </where>

    </select>

    <select id="listView" parameterType="CphSearch" resultType="StuClass">
        select a.stu_no,a.`name` as stu_name,b.class_name
        from srs_dormitory_student as t1
        left join srs_student as a on a.stu_no = t1.stu_no
        left join srs_class as b on a.class_id = b.class_id
        left join srs_majors as c on b.major_id = c.major_id
        left join sys_dept as d on c.college_id = d.dept_id
        left join cph_teacher as f on b.teacher_id = f.teacher_id
        <where>
            <if test="classId != null">and a.class_id = #{classId}</if>
            <if test="majorId != null">and b.major_id = #{majorId}</if>
            <if test="deptId != null">and d.dept_id = #{deptId}</if>
            <if test="tNo != null">and f.employee_id = #{tNo}</if>
            <if test="stuNo != null">and a.stu_no = #{stuNo}</if>
            <if test="gradeId != null">and b.grade_id = #{gradeId}</if>
            <if test="stuName != null">and a.`name` like concat('%',#{stuName},'%') </if>
        </where>
        order by b.class_id desc
    </select>

    <update id="updateSrsDormitoryStudent" parameterType="SrsDormitoryStudent">
        update srs_dormitory_student
        <trim prefix="SET" suffixOverrides=",">
            <if test="dormitoryId != null">dormitory_id = #{dormitoryId},</if>
            <if test="dormitoryId == null">dormitory_id =NULL,</if>
            <if test="bedId != null">bed_id = #{bedId},</if>
            <if test="stuNo != null and stuNo!=''">stu_no = #{stuNo},</if>
            <if test="isDormitoryHead != null">is_dormitory_head = #{isDormitoryHead},</if>
            <if test="status != null">status =#{status},</if>
            <if test="checkinTime!=null"> checkin_time = #{checkinTime},</if>
            <if test="checkinTime==null"> checkin_time =NULL,</if>
            <if test="inStatus != null and inStatus!=''">in_status = #{inStatus},</if>
            <if test="isInStudentsleep != null and isInStudentsleep!=''">isInStudentsleep = #{isInStudentsleep},</if>
        </trim>
        <where>
            <if test="id!=null"> and id = #{id}</if>
        </where>
    </update>
    <select id="list" resultType="SrsDormitoryStudentVo" parameterType="DmsSearch">
        select a.*,b.stu_name,c.room_no,c.floor as floor_name,c.building_name,
        c.park_name,c.campus_name,b.class_name,
        c.floor_id,c.building_id,c.park_id,c.campus_id,
        b.dept_name,d.phone,b.teacher_name
        from srs_dormitory_student as a
        left join view_stu_info as b on a.stu_no = b.stu_no
        left join view_dorm_info as c on a.dormitory_id = c.id
        left join srs_student as d on d.stu_no=a.stu_no
        <where>
            <if test="stuName != null">and b.stu_name like concat('%',#{stuName},'%')</if>
            <if test="stuNo != null">and a.stu_no = #{stuNo}</if>
            <if test="classId != null">and b.class_id = #{classId}</if>
            <if test="majorId != null">and b.major_id = #{majorId}</if>
            <if test="deptId != null">and b.dept_id = #{deptId}</if>
            <if test="gradeId != null">and b.grade_id = #{gradeId}</if>
            <if test="campusId != null">and c.campus_id = #{campusId}</if>
            <if test="parkId != null">and c.park_id = #{parkId}</if>
            <if test="buildingId != null">and c.building_id = #{buildingId}</if>
            <if test="floorId != null">and c.floor_id = #{floorId}</if>
            <if test="dormitoryId != null">and c.id = #{dormitoryId}</if>
            <if test="teacherName != null">and b.teacher_name = #{teacherName}</if>
            <if test="idCard != null and idCard != ''">and AES_DECRYPT(UNHEX(b.id_card),'zhxg') = #{idCard}</if>
        </where>
        order by c.park_name,c.campus_name,c.building_name,c.floor,c.room_no
    </select>

    <select id="doExport" parameterType="DmsSearch" resultType="DomInfo">
        select c.campus_name,c.park_name,c.building_name,c.room_no,
               b.stu_no,b.stu_name,b.bank_card,d.mz,d.zzmm,b.gender,b.id_card,
               b.dept_name,b.major_name,b.class_name,b.grade_name,b.teacher_name
        from srs_dormitory_student as a
                 left join view_stu_info as b on a.stu_no = b.stu_no
                 left join view_dorm_info as c on a.dormitory_id = c.id
                 left join cph_stu_extra_info as d on b.stu_no = d.stu_no
        <where>
            <if test="classId != null">and b.class_id = #{classId}</if>
            <if test="majorId != null">and b.major_id = #{majorId}</if>
            <if test="deptId != null">and b.dept_id = #{deptId}</if>
            <if test="gradeId != null">and b.grade_id = #{gradeId}</if>
            <if test="campusId != null">and c.campus_id = #{campusId}</if>
            <if test="parkId != null">and c.park_id = #{parkId}</if>
            <if test="buildingId != null">and c.building_id = #{buildingId}</if>
            <if test="floorId != null">and c.floor_id = #{floorId}</if>
            <if test="dormitoryId != null">and c.id = #{dormitoryId}</if>
            <if test="teacherName != null">and b.teacher_name = #{teacherName}</if>
        </where>
        order by c.id

    </select>


    <select id="selectStuRoomInfoByKsh" resultType="com.srs.dormitory.domain.Dto.StuRoomInfo">
        select * from view_dorm_xh_ksh_room_info
        where KSH = #{ksh}
    </select>

    <select id="selectBedInfoByDormId" resultType="com.srs.dormitory.domain.Dto.BedInfo">
        select * from view_dorm_bed_info
        where DormId = #{dormId}
    </select>

    <select id="selectStudentNoBed" resultType="com.srs.stureg.domain.SrsStuReg">
        SELECT r.*
        FROM srs_stu_reg r
                 LEFT JOIN srs_dormitory_student s ON r.XH = s.stu_no
        WHERE  s.id IS NULL
          AND r.ZYDM = #{major}
          AND r.XB = #{gender}
    </select>
    <select id="selectBedNoStudent" resultType="com.srs.dormitory.domain.DmsDormitoryBed">
        SELECT b.*
        FROM dms_dormitory_bed b
                 LEFT JOIN dms_dormitory d ON b.dormitory_id = d.id
                 LEFT JOIN dms_dormitory_class dc ON dc.dormitory_id = d.id
                 LEFT JOIN srs_dormitory_student ds ON ds.bed_id = b.id
                 LEFT JOIN dms_dormitory_unit u ON u.id = d.unit_id
                 LEFT JOIN dms_dormitory_building_info info ON info.building_id = u.building_id
        WHERE dc.BJDM = #{major}
          AND ds.id IS NULL
          AND info.gender = #{gender}
    </select>
    <select id="selectNoPayReg" resultType="com.srs.stureg.domain.SrsStuReg">
        SELECT r.*
        FROM srs_stu_reg r
                 LEFT JOIN srs_dormitory_student s ON r.XH = s.stu_no
        WHERE TIMESTAMPDIFF(HOUR,s.checkin_time,NOW()) >= #{time}
          AND s.`status` = 0
    </select>
    <select id="getNoBedStudent" resultType="com.srs.stureg.domain.SrsStuReg">
        SELECT *
        FROM `srs_stu_reg` s
                 LEFT JOIN srs_dormitory_student d ON s.XH = d.stu_no
        WHERE  d.id IS NULL
    </select>
    <select id="selectStuNo" resultType="java.lang.String">
        SELECT s.stu_no FROM `srs_student` s
        LEFT JOIN srs_class c ON c.class_id = s.class_id
        LEFT JOIN srs_grade g ON g.grade_id = c.grade_id
        LEFT JOIN srs_dormitory_student ds ON ds.stu_no = s.stu_no
        LEFT JOIN dms_delay_application a ON a.stu_no = s.stu_no
        <where>
            AND g.grade_id = 10
            AND ds.id IS NOT NULL
            AND (a.delay_time &lt; NOW() OR a.id IS NULL)
            <if test="deptId !=null">AND s.dept_id = #{deptId}</if>
            <if test="majorId != null">AND s.major_id = #{majorId}</if>
        </where>
    </select>
    <select id="getList" resultType="java.util.Map">
        SELECT ds.id,ds.stu_no stuNo,d.room_no roomNo,b.`name` buildingName,p.`name` parkName,ds.dormitory_id dormitoryId,
               c.`name` campusName,ds.checkin_time checkInTime,ds.in_status

        FROM `srs_dormitory_student` ds
                 LEFT JOIN dms_dormitory d ON ds.dormitory_id = d.id
                 LEFT JOIN dms_dormitory_floor f ON f.id = d.floor_id
                 LEFT JOIN dms_dormitory_building b ON b.id = f.building_id
                 LEFT JOIN dms_dormitory_park p ON p.id = b.park_id
                 LEFT JOIN dms_campus c ON c.id = p.campus_id
        <where>
            <if test="campusId != null">and c.id = #{campusId}</if>
            <if test="parkId != null">and c.id = #{parkId}</if>
            <if test="buildingId != null">and c.id = #{buildingId}</if>
            <if test="floorId != null">and c.id = #{floorId}</if>
            <if test="dormitoryId != null">and c.id = #{dormitoryId}</if>
            <if test="stuNo != null">and ds.stu_no = #{stuNo}</if>
        </where>

    </select>
    <select id="selectStudentByStuNoList" resultType="java.util.Map">
        select s.stu_id stuId,s.stu_no stuNo,s.`name` stuName,s.gender,c.class_name className,
        m.major_name majorName,d.dept_name deptName,ds.dormitory_id dormitoryId
        from srs_student s
        LEFT JOIN srs_class c ON s.class_id = c.class_id
        LEFT JOIN srs_majors m ON c.major_id = m.major_id
        LEFT JOIN sys_dept d ON d.dept_id = m.college_id
        LEFT JOIN srs_dormitory_student ds ON ds.stu_no = s.stu_no
            <where>
                and s.stu_no in
                <foreach collection="stuNoList" item="stuNo" open="(" separator="," close=")">
                    #{stuNo}
                </foreach>
            </where>
    </select>
    <select id="selectSrsStuRegByStuNoList" resultType="java.util.Map">
        SELECT r.id stuId,r.XSXM stuName,r.XH stuNo,r.XB gender,r.BJ className,
               r.ZY majorName,ds.dormitory_id dormitoryId
        FROM srs_stu_reg r
        LEFT JOIN srs_dormitory_student ds ON r.XH = ds.stu_no
        <where>
            r.XH in
        <foreach collection="stuNoList" open="(" separator="," close=")" item="stuNo">
            #{stuNo}
        </foreach>
        </where>
    </select>
    <select id="selectDetailById" resultType="java.util.Map">
        SELECT ds.id,ds.stu_no stuNo,d.room_no roomNo,b.`name` buildingName,p.`name` parkName,ds.dormitory_id dormitoryId,
               c.`name` campusName,ds.checkin_time as checkinTime,ds.in_status as inStatus,
               ds.is_dormitory_head as isDormitoryHead,ds.isInStudentsleep as isInStudentsleep
        FROM `srs_dormitory_student` ds
                 LEFT JOIN dms_dormitory d ON ds.dormitory_id = d.id
                 LEFT JOIN dms_dormitory_floor f ON f.id = d.floor_id
                 LEFT JOIN dms_dormitory_building b ON b.id = f.building_id
                 LEFT JOIN dms_dormitory_park p ON p.id = b.park_id
                 LEFT JOIN dms_campus c ON c.id = p.campus_id
        <where>
            and ds.id = #{id}
        </where>
    </select>
    <select id="selectAll" resultType="java.util.Map">
        SELECT ds.id,d.room_no roomNo,b.`name` buildingName,p.`name` parkName,ds.is_dormitory_head isHead,
               ds.stu_no stuNo,s.`name` stuName,s.phone stuPhone,t.`name` teacherName,t.phone_number teacherPhone,
               d.bed_num bedNum,d.occupancy,d.charge_standard chargeStandard,c.class_name className,ds.in_status as inStatus
        FROM `srs_dormitory_student` ds
                 LEFT JOIN dms_dormitory d ON ds.dormitory_id = d.id
                 LEFT JOIN dms_dormitory_floor f ON f.id = d.floor_id
                 LEFT JOIN dms_dormitory_building b ON b.id = f.building_id
                 LEFT JOIN dms_dormitory_park p ON p.id = b.park_id
                 LEFT JOIN srs_student s ON s.stu_no = ds.stu_no
                 LEFT JOIN srs_class c ON c.class_id = s.class_id
                 LEFT JOIN cph_teacher t ON t.teacher_id = c.teacher_id
        <where>
            <if test="stuNo != null and stuNo != ''">and ds.stu_no = #{stuNo}</if>
        </where>
    </select>
    <select id="selectRecords" resultType="java.util.Map">
        SELECT ds.id,ds.dormitory_id dormitoryId,ds.stu_no stuNo,ds.is_dormitory_head isDormitoryHead,s.`name` stuName,
               c.class_name className,IF(park.`name`='default',CONCAT(building.`name`,d.`room_no`),CONCAT(park.`name`,building.`name`,d.room_no)) roomNo,
               d.bed_num bedNum,d.occupancy,d.charge_standard chargeStandard,s.phone stuPhone,t.`name` teacherName,t.phone_number teacherPhone,DATE(ds.checkin_time) checkinTime,
               ds.in_status as inStatus
        FROM `srs_dormitory_student` ds
                 LEFT JOIN srs_student s ON ds.stu_no = s.stu_no
                 LEFT JOIN srs_class c ON s.class_id = c.class_id
                 LEFT JOIN dms_dormitory d ON d.id = ds.dormitory_id
                 LEFT JOIN dms_dormitory_floor f ON d.floor_id = f.id
                 LEFT JOIN dms_dormitory_building building ON f.building_id = building.id
                 LEFT JOIN dms_dormitory_park park ON park.id = building.park_id
                 LEFT JOIN cph_teacher t ON t.teacher_id = c.teacher_id
    </select>
    <select id="rankingCount" resultType="java.util.Map">
        SELECT
            dept_name AS deptName,
            COUNT(CASE WHEN ranking = '一等奖' THEN 1 END) AS firstCount,
            COUNT(CASE WHEN ranking = '二等奖' THEN 1 END) AS thirdCount,
            COUNT(CASE WHEN ranking = '三等奖' THEN 1 END) AS secondCount
        FROM dms_new_wenming
        GROUP BY dept_name
        ORDER BY dept_name;
    </select>



    <select id="listSmokeEarly" parameterType="String" resultType="SrsStuReg">
        select a.BJDM,a.XB, a.KSH,a.is_smoke,a.is_early
        from srs_stu_reg as a
                 left join srs_class as b on a.BJDM = b.class_code
                 left join cph_teacher as c on b.teacher_id = c.teacher_id
        where c.employee_id = #{fdyNo}
        order by a.BJDM asc,a.XB asc, a.is_smoke asc, a.is_early desc;
    </select>


    <select id="listFdyCanDorm" parameterType="String" resultType="DmsDormitoryClass">
        select a.*
        from dms_dormitory_class as a
        where a.t_no = #{fdyNo} and is_lock = 1
    </select>

</mapper>
