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

    <resultMap type="com.ym.mec.biz.dal.entity.StudentAttendance" id="StudentAttendance">
        <result column="id_" property="id"/>
        <result column="music_group_id_" property="musicGroupId"/>
        <result column="class_group_id_" property="classGroupId"/>
        <result column="course_schedule_id_" property="courseScheduleId"/>
        <result column="user_id_" property="userId"/>
        <result column="teacher_id_" property="teacherId"/>
        <result column="status_" property="status" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="create_time_" property="createTime"/>
        <result column="remark_" property="remark"/>
        <result column="current_class_times_" property="currentClassTimes"/>
    </resultMap>

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

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

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.StudentAttendance" useGeneratedKeys="true"
            keyColumn="id" keyProperty="id">
        <!--
        <selectKey resultClass="int" keyProperty="id" >
        SELECT SEQ_WSDEFINITION_ID.nextval AS ID FROM DUAL
        </selectKey>
        -->
        INSERT INTO student_attendance
        (id_,music_group_id_,class_group_id_,course_schedule_id_,user_id_,teacher_id_,status_,create_time_,remark_,current_class_times_)
        VALUES(#{id},#{musicGroupId},#{classGroupId},#{courseScheduleId},#{userId},#{teacherId},#{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},now(),#{remark},#{currentClassTimes})
    </insert>

    <insert id="addStudentAttendances" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id_">
      INSERT INTO student_attendance ( music_group_id_, class_group_id_, course_schedule_id_, user_id_, teacher_id_, status_, create_time_, remark_ ,current_class_times_)
      VALUES
	  <foreach collection="list" item="studentAttendance" separator=",">
          (#{studentAttendance.musicGroupId},#{studentAttendance.classGroupId},#{studentAttendance.courseScheduleId},#{studentAttendance.userId},#{studentAttendance.teacherId},#{studentAttendance.status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},now(),#{studentAttendance.remark},#{studentAttendance.currentClassTimes})
      </foreach>
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.StudentAttendance">
        UPDATE student_attendance
        <set>
            <if test="status != null">
                status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="userId != null">
                user_id_ = #{userId},
            </if>
            <if test="remark != null">
                remark_ = #{remark},
            </if>
            <if test="classGroupId != null">
                class_group_id_ = #{classGroupId},
            </if>
            <if test="teacherId != null">
                teacher_id_ = #{teacherId},
            </if>
            <if test="courseScheduleId != null">
                course_schedule_id_ = #{courseScheduleId},
            </if>
            <if test="musicGroupId != null">
                music_group_id_ = #{musicGroupId},
            </if>
            <if test="currentClassTimes != null">
                current_class_times_ = #{currentClassTimes},
            </if>
        </set>
        WHERE id_ = #{id}
    </update>

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

    <resultMap id="studentAttendanceViewUtilEntity" type="com.ym.mec.biz.dal.dto.StudentAttendanceViewDto">
        <result property="classGroupId" column="class_group_id_"/>
        <result property="studentId" column="student_id_"/>
        <result property="userName" column="username_"/>
        <result property="musicGroupId" column="music_group_id_"/>
        <result property="subjectId" column="subject_id_"/>
        <result property="subjectName" column="subject_name_"/>
        <result property="status" column="status_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
    </resultMap>

    <sql id="queryPageCondition">
        <where>
            cgsm.class_group_id_=#{classGroupId}
            <if test="filterStatus!=null">
                AND cgsm.status_!=#{filterStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
        </where>
    </sql>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="studentAttendanceViewUtilEntity" parameterType="map">
        SELECT
        cgsm.class_group_id_,
        su.id_ student_id_,
        su.username_,
        mg.id_ music_group_id_,
        s.id_ subject_id_,
        s.name_ subject_name_,
        cgsm.status_
        FROM
        class_group_student_mapper cgsm
        LEFT JOIN class_group cg ON cg.id_ = cgsm.class_group_id_
        LEFT JOIN sys_user su ON cgsm.user_id_=su.id_
        LEFT JOIN music_group mg ON cg.music_group_id_=mg.id_
        LEFT JOIN `subject` s ON cg.subject_id_list_=s.id_
        <include refid="queryPageCondition"/>
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
		SELECT COUNT(*)
		FROM
        class_group cg
        LEFT JOIN class_group_student_mapper cgsm ON cg.id_=cgsm.class_group_id_
        LEFT JOIN sys_user su ON cgsm.user_id_=su.id_
        LEFT JOIN music_group mg ON cg.music_group_id_=mg.id_
        LEFT JOIN `subject` s ON cg.subject_id_list_=s.id_
        <include refid="queryPageCondition"/>
	</select>

    <select id="countStudentStatus" resultType="com.ym.mec.biz.dal.dto.StudentStatusCountUtilEntity">
        SELECT
            status_ studentStatus,
            COUNT(user_id_) numberOfStudent
        FROM
            class_group_student_mapper
        WHERE class_group_id_=#{classId}
        GROUP BY status_
    </select>

    <select id="getStudentAttendanceInfo" parameterType="com.ym.mec.biz.dal.entity.StudentAttendance" resultMap="StudentAttendance">
        SELECT
            cgsm.class_group_id_,
            su.id_ user_id_,
            mg.id_ music_group_id_,
            s.id_ subject_id_,
            #{teacherId} teacher_id_,
            #{status} status_,
            #{remark} remark_,
            #{courseScheduleId} course_schedule_id_
        FROM
            sys_user su
            LEFT JOIN class_group_student_mapper cgsm ON cgsm.user_id_ = su.id_
            LEFT JOIN class_group cg ON cg.id_ = cgsm.class_group_id_
            LEFT JOIN music_group mg ON cg.music_group_id_ = mg.id_
            LEFT JOIN `subject` s ON cg.subject_id_ = s.id_
            LEFT JOIN course_schedule cs ON cg.id_=cs.class_group_id_
        WHERE
            su.id_=#{userId} AND cs.id_=#{courseScheduleId}
    </select>

    <select id="getStudentLatestAttendanceInfo" resultType="com.ym.mec.biz.dal.entity.StudentAttendance" resultMap="StudentAttendance">
        SELECT
            *
        FROM
            student_attendance sa
        WHERE
            sa.music_group_id_ = #{musicGroupId}
            AND sa.user_id_ = #{userId}
        ORDER BY
            sa.create_time_ DESC
            LIMIT 1
    </select>

    <resultMap id="studentPersonalAttendance" type="com.ym.mec.biz.dal.dto.StudentPersonalAttendanceDto">
        <result property="classDate" column="class_date_"/>
        <result property="startClassTime" column="start_class_time_"/>
        <result property="type" column="type_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result property="className" column="name_"/>
        <result property="teacherName" column="username_"/>
        <result property="status" column="status_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
    </resultMap>

    <sql id="queryCondition">
        <where>
            <if test="studentID != null">
                and sa.user_id_ = #{studentID}
            </if>
            <if test="musicGroupId != null">
                and sa.music_group_id_ = #{musicGroupId}
            </if>
            <if test="classGroupId != null">
                and sa.class_group_id_ = #{classGroupId}
            </if>
            <if test="status != null">
                and sa.status_ = #{status, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
        </where>
    </sql>

    <select id="queryStudentPersonalAttendances" resultMap="studentPersonalAttendance">
        SELECT
            cs.class_date_,
            cs.start_class_time_,
            cg.type_,
            cg.name_,
            su.username_,
            sa.status_
        FROM
            student_attendance sa
        LEFT JOIN course_schedule cs ON sa.course_schedule_id_=cs.id_
        LEFT JOIN class_group cg ON sa.class_group_id_=cg.id_
        LEFT JOIN sys_user su ON sa.teacher_id_=su.id_
        <include refid="queryCondition"/>
    </select>

    <select id="queryStudentPersonalAttendancesCount" resultType="int">
        SELECT
        count(*)
        FROM
        student_attendance sa
        LEFT JOIN course_schedule cs ON sa.course_schedule_id_=cs.id_
        LEFT JOIN class_group cg ON sa.class_group_id_=cg.id_
        LEFT JOIN sys_user su ON sa.teacher_id_=su.id_
        <include refid="queryCondition"/>
    </select>
</mapper>
