<?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.DmsNewRegMapper">
    
    <resultMap type="DmsNewReg" id="DmsNewRegResult">
        <result property="id"    column="id"    />
        <result property="dormitoryId"    column="dormitory_id"    />
        <result property="stuNo"    column="stu_no"    />
        <result property="ksh"    column="ksh"    />
        <result property="regStatus"    column="reg_status"    />
        <result property="status"    column="status"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectDmsNewRegVo">
        select id, dormitory_id, stu_no, ksh, reg_status, status, remark, create_by, create_time, update_by, update_time, del_flag from dms_new_reg
    </sql>

    <select id="selectDmsNewRegList" parameterType="DmsNewReg" resultMap="DmsNewRegResult">
        <include refid="selectDmsNewRegVo"/>
        <where>  
            <if test="dormitoryId != null "> and dormitory_id = #{dormitoryId}</if>
            <if test="stuNo != null  and stuNo != ''"> and stu_no = #{stuNo}</if>
            <if test="ksh != null  and ksh != ''"> and ksh = #{ksh}</if>
            <if test="regStatus != null  and regStatus != ''"> and reg_status = #{regStatus}</if>
            <if test="status != null "> and status = #{status}</if>
        </where>
    </select>
    
    <select id="selectDmsNewRegById" parameterType="Long" resultMap="DmsNewRegResult">
        <include refid="selectDmsNewRegVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertDmsNewReg" parameterType="DmsNewReg">
        insert into dms_new_reg
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="dormitoryId != null">dormitory_id,</if>
            <if test="stuNo != null">stu_no,</if>
            <if test="ksh != null">ksh,</if>
            <if test="regStatus != null">reg_status,</if>
            <if test="status != null">status,</if>
            <if test="remark != null">remark,</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>
            <if test="delFlag != null and delFlag != ''">del_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="dormitoryId != null">#{dormitoryId},</if>
            <if test="stuNo != null">#{stuNo},</if>
            <if test="ksh != null">#{ksh},</if>
            <if test="regStatus != null">#{regStatus},</if>
            <if test="status != null">#{status},</if>
            <if test="remark != null">#{remark},</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>
            <if test="delFlag != null and delFlag != ''">#{delFlag},</if>
         </trim>
    </insert>

    <update id="updateDmsNewReg" parameterType="DmsNewReg">
        update dms_new_reg
        <trim prefix="SET" suffixOverrides=",">
            <if test="dormitoryId != null">dormitory_id = #{dormitoryId},</if>
            <if test="stuNo != null">stu_no = #{stuNo},</if>
            <if test="ksh != null">ksh = #{ksh},</if>
            <if test="regStatus != null">reg_status = #{regStatus},</if>
            <if test="status != null">status = #{status},</if>
            <if test="remark != null">remark = #{remark},</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>
            <if test="delFlag != null and delFlag != ''">del_flag = #{delFlag},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDmsNewRegById" parameterType="Long">
        delete from dms_new_reg where id = #{id}
    </delete>

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

    <select id="listStuInDorm" parameterType="DmsSearch" resultType="SrsStuReg">
        select b.*
            from dms_new_reg as a
        left join srs_stu_reg as b on a.ksh = b.KSH
        <where>
            <if test="dormitoryId != null">and a.dormitory_id = #{dormitoryId}</if>
            <if test="classCode != null">and b.BJDM = #{classCode}</if>
        </where>
        order by a.ksh asc
    </select>

    <select id="getStuDorm" resultType="DomInfo" parameterType="DmsSearch">
        select b.campus_name,b.park_name,b.building_name,b.floor,b.room_no
        from dms_new_reg as a
        left join view_dorm_info as b on a.dormitory_id = b.id
        left join dms_dormitory_class as c on b.id = c.dormitory_id
        <where>
            c.is_mixed = 1
            <if test="ksh != null">and a.ksh = #{ksh}</if>
        </where>
    </select>

</mapper>