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

    <resultMap type="DmsLiveDetail" id="DmsLiveDetailResult">
        <result property="id"    column="id"    />
        <result property="stuNo"    column="stu_no"    />
        <result property="dormitoryId"    column="dormitory_id"    />
        <result property="duration"    column="duration"    />
        <result property="money"    column="money"    />
        <result property="semesterId"    column="semester_id"    />
    </resultMap>

    <sql id="selectDmsLiveDetailVo">
        select id, stu_no, dormitory_id, duration, money, semester_id from dms_live_detail
    </sql>

    <select id="selectDmsLiveDetailList" parameterType="DmsLiveDetail" resultMap="DmsLiveDetailResult">
        <include refid="selectDmsLiveDetailVo"/>
        <where>
            <if test="stuNo != null  and stuNo != ''"> and stu_no = #{stuNo}</if>
            <if test="dormitoryId != null "> and dormitory_id = #{dormitoryId}</if>
            <if test="duration != null "> and duration = #{duration}</if>
            <if test="money != null "> and money = #{money}</if>
            <if test="semesterId != null "> and semester_id = #{semesterId}</if>
        </where>
    </select>

    <select id="selectDmsLiveDetailById" parameterType="Long" resultMap="DmsLiveDetailResult">
        <include refid="selectDmsLiveDetailVo"/>
        where id = #{id}
    </select>
    <select id="selectRecords" resultType="java.util.Map">
        SELECT
            li.id,
            li.dormitory_id dormitoryId,
            li.duration,
            li.money,
            li.semester_id semesterId,
            li.stu_no stuNo,
            s.`name` stuName,
            s.phone stuPhone,
            c.class_name className,
            ds.is_dormitory_head isDormitoryHead,
            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,
            ds.checkin_time checkinTime
        FROM
            dms_live_detail li
                LEFT JOIN srs_dormitory_student ds ON li.stu_no = ds.stu_no
                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 srs_student s ON s.stu_no = li.stu_no
                LEFT JOIN srs_class c ON s.class_id = c.class_id
                LEFT JOIN cph_teacher t ON t.teacher_id = c.teacher_id
        <where>
            <if test="i==0">AND ds.id IS NOT NULL</if>
            <if test="i==1">AND ds.id IS NULL</if>
            AND li.semester_id = #{semesterId}
        </where>
    </select>

    <insert id="insertDmsLiveDetail" parameterType="DmsLiveDetail" useGeneratedKeys="true" keyProperty="id">
        insert into dms_live_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="stuNo != null">stu_no,</if>
            <if test="dormitoryId != null">dormitory_id,</if>
            <if test="duration != null">duration,</if>
            <if test="money != null">money,</if>
            <if test="semesterId != null">semester_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="stuNo != null">#{stuNo},</if>
            <if test="dormitoryId != null">#{dormitoryId},</if>
            <if test="duration != null">#{duration},</if>
            <if test="money != null">#{money},</if>
            <if test="semesterId != null">#{semesterId},</if>
        </trim>
    </insert>

    <update id="updateDmsLiveDetail" parameterType="DmsLiveDetail">
        update dms_live_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="stuNo != null">stu_no = #{stuNo},</if>
            <if test="dormitoryId != null">dormitory_id = #{dormitoryId},</if>
            <if test="duration != null">duration = #{duration},</if>
            <if test="money != null">money = #{money},</if>
            <if test="semesterId != null">semester_id = #{semesterId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDmsLiveDetailById" parameterType="Long">
        delete from dms_live_detail where id = #{id}
    </delete>

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