<?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.ym.mec.biz.dal.dao.TeacherAttendanceDao">

    <resultMap type="com.ym.mec.biz.dal.entity.TeacherAttendance" id="TeacherAttendance">
        <result column="id_" property="id"/>
        <result column="teacher_id_" property="teacherId"/>
        <result column="music_group_id_" property="musicGroupId"/>
        <result column="class_group_id_" property="classGroupId"/>
        <result column="course_schedule_id_" property="courseScheduleId"/>
        <result column="sign_in_time_" property="signInTime"/>
        <result column="sign_in_status_" property="signInStatus" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="sign_out_time_" property="signOutTime"/>
        <result column="sign_out_status_" property="signOutStatus" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="remark_" property="remark"/>
        <result column="create_time_" property="createTime"/>
    </resultMap>

    <!-- 根据主键查询一条记录 -->
    <select id="get" resultMap="TeacherAttendance">
		SELECT * FROM teacher_attendance WHERE id_ = #{id} 
	</select>

    <!-- 全查询 -->
    <select id="findAll" resultMap="TeacherAttendance">
		SELECT * FROM teacher_attendance ORDER BY id_
	</select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.TeacherAttendance" useGeneratedKeys="true"
            keyColumn="id" keyProperty="id">
        <!--
        <selectKey resultClass="int" keyProperty="id" >
        SELECT SEQ_WSDEFINITION_ID.nextval AS ID FROM DUAL
        </selectKey>
        -->
        INSERT INTO teacher_attendance
        (id_,teacher_id_,music_group_id_,class_group_id_,course_schedule_id_,sign_in_time_,sign_in_status_,remark_,create_time_,sign_out_time_,sign_out_status_)
        VALUES(#{id},#{teacherId},#{musicGroupId},#{classGroupId},#{courseScheduleId},#{signInTime},#{signInStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{remark},now(),#{signOutTime},#{signOutStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler})
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.TeacherAttendance">
        UPDATE teacher_attendance
        <set>
            <if test="signInStatus != null">
                sign_in_status_ = #{signInStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="remark != null">
                remark_ = #{remark},
            </if>
            <if test="signInTime != null">
                sign_in_time_ = #{signInTime},
            </if>
            <if test="teacherId != null">
                teacher_id_ = #{teacherId},
            </if>
            <if test="classGroupId != null">
                class_group_id_ = #{classGroupId},
            </if>
            <if test="courseScheduleId != null">
                course_schedule_id_ = #{courseScheduleId},
            </if>
            <if test="musicGroupId != null">
                music_group_id_ = #{musicGroupId},
            </if>
            <if test="signOutTime != null">
                sign_out_time_ = #{signOutTime},
            </if>
            <if test="signOutStatus != null">
                sign_out_status_ = #{signOutStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
        </set>
        WHERE id_ = #{id}
    </update>

    <!-- 根据主键删除一条记录 -->
    <delete id="delete">
		DELETE FROM teacher_attendance WHERE id_ = #{id} 
	</delete>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="TeacherAttendance" parameterType="map">
        SELECT * FROM teacher_attendance ORDER BY id_
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM teacher_attendance
	</select>

    <resultMap id="teacherPersonalAttendance" type="com.ym.mec.biz.dal.dto.TeacherPersonalAttendanceDto">
        <result column="class_date_" property="classDate"/>
        <result column="start_class_time_" property="startClassTime"/>
        <result column="type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="name_" property="name"/>
        <result column="status_" property="status" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="remark_" property="remark"/>
    </resultMap>

    <sql id="queryCondition">
        <where>
            <if test="teacherId != null">
                and ta.teacher_id_ = #{teacherId}
            </if>
            <if test="musicGroupId != null">
                and ta.music_group_id_ = #{musicGroupId}
            </if>
            <if test="classGroupId != null">
                and ta.class_group_id_ = #{classGroupId}
            </if>
            <if test="attendanceStatus != null">
                and ta.sign_in_status_ = #{attendanceStatus, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="classGroupType != null">
                and cg.type_ = #{classGroupType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="classGroupName != null">
                and cg.name_ like CONCAT('%',#{classGroupName},'%')
            </if>
        </where>
    </sql>

    <select id="getTeacherPersonalAttendances" parameterType="map" resultMap="teacherPersonalAttendance">
        SELECT
            cs.class_date_,
            cs.start_class_time_,
            cg.name_,
            cg.type_,
            ta.status_,
            ta.remark_
        FROM
            teacher_attendance ta
        LEFT JOIN course_schedule cs ON ta.course_schedule_id_=cs.id_
        LEFT JOIN class_group cg ON ta.class_group_id_=cg.id_
        <include refid="queryCondition"/>
        ORDER BY class_date_ DESC
        <include refid="global.limit"/>
    </select>

    <select id="getTeacherPersonalAttendancesCount" resultType="int">
        SELECT
            count(ta.id_)
        FROM
          teacher_attendance ta
        LEFT JOIN course_schedule cs ON ta.course_schedule_id_=cs.id_
        LEFT JOIN class_group cg ON ta.class_group_id_=cg.id_
        <include refid="queryCondition"/>
    </select>

    <select id="countClassTime" resultType="java.lang.Integer">
        select count(*) from teacher_attendance where class_group_id_=#{classGroupId} for update
    </select>
    <select id="findByTeacherAttendanceInfo" resultMap="TeacherAttendance">
        SELECT * FROM teacher_attendance WHERE teacher_id_=#{teacherId} AND course_schedule_id_=#{courseScheduleId}
    </select>


</mapper>
