<?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="group_type_" property="groupType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <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="username_" property="username"/>
        <result column="subject_name_" property="subjectName"/>
        <result column="phone_" property="phone"/>
        <result column="teacher_id_" property="teacherId"/>
        <result column="sign_in_time_" property="signInTime"/>
        <result column="sign_out_time_" property="signOutTime"/>
        <result column="status_" property="status" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <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">
        INSERT INTO student_attendance
        (group_type_,music_group_id_,class_group_id_,course_schedule_id_,user_id_,teacher_id_,status_,create_time_,update_time_,remark_,current_class_times_,sign_in_time_,sign_out_time_)
        VALUES(#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{musicGroupId},#{classGroupId},
        #{courseScheduleId},#{userId},#{teacherId},#{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},now(),now(),
        #{remark},#{currentClassTimes},#{signInTime},#{signOutTime})
    </insert>

    <insert id="addStudentAttendances" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id_">
      INSERT INTO student_attendance (group_type_, music_group_id_, class_group_id_, course_schedule_id_,
      user_id_, teacher_id_, status_, create_time_,update_time_, remark_ ,current_class_times_,sign_in_time_,sign_out_time_)
      VALUES
	  <foreach collection="list" item="studentAttendance" separator=",">
          (#{studentAttendance.groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
          #{studentAttendance.musicGroupId},#{studentAttendance.classGroupId},#{studentAttendance.courseScheduleId},
          #{studentAttendance.userId},#{studentAttendance.teacherId},
          #{studentAttendance.status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},now(),#{studentAttendance.updateTime},
          #{studentAttendance.remark},#{studentAttendance.currentClassTimes},
          #{studentAttendance.signInTime},#{studentAttendance.signOutTime})
      </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="signOutTime != null">
                sign_out_time_ = #{signOutTime},
            </if>
            <if test="signInTime != null">
                sign_in_time_ = #{signInTime},
            </if>
            <if test="userId != null">
                user_id_ = #{userId},
            </if>
            <if test="remark != null">
                remark_ = #{remark},
            </if>
            <if test="groupType != null">
                group_type_ = #{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </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>
            update_time_=NOW()
        </set>
        WHERE id_ = #{id}
    </update>
    
    <update id="batchUpdate"  parameterType="java.util.List">  
    	<foreach collection="list" item="item" index="index" open="" close="" separator=";">
	        update student_attendance
	        <set>
	            <if test="item.status != null">
	                status_ = #{item.status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
	            </if>
                <if test="item.signOutTime != null">
                    sign_out_time_ = #{item.signOutTime},
                </if>
                <if test="item.signInTime != null">
                    sign_in_time_ = #{item.signInTime},
                </if>
	            <if test="item.userId != null">
	                user_id_ = #{item.userId},
	            </if>
	            <if test="item.remark != null">
	                remark_ = #{item.remark},
	            </if>
	            <if test="item.classGroupId != null">
	                class_group_id_ = #{item.classGroupId},
	            </if>
	            <if test="item.groupType != null">
	                group_type_ = #{item.groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
	            </if>
	            <if test="item.teacherId != null">
	                teacher_id_ = #{item.teacherId},
	            </if>
	            <if test="item.courseScheduleId != null">
	                course_schedule_id_ = #{item.courseScheduleId},
	            </if>
	            <if test="item.musicGroupId != null">
	                music_group_id_ = #{item.musicGroupId},
	            </if>
	            <if test="item.currentClassTimes != null">
	                current_class_times_ = #{item.currentClassTimes},
	            </if>
                update_time_=NOW()
        	</set>
	        where id_ = #{item.id}
	    </foreach>      
	</update>

    <!-- 根据主键删除一条记录 -->
    <delete id="delete">
		DELETE FROM student_attendance WHERE id_ = #{id} 
	</delete>
    <delete id="deleteByCourseSchedules">
        DELETE FROM student_attendance WHERE course_schedule_id_ IN
        <foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
            #{courseScheduleId}
        </foreach>
    </delete>
    <delete id="deleteByCourseAndUser">
        DELETE FROM student_attendance WHERE course_schedule_id_ IN
        <foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
            #{courseScheduleId}
        </foreach>
        AND user_id_=#{userId}
    </delete>
    <delete id="deleteStudentAttendancesByCourseAndUsers">
        DELETE FROM student_attendance WHERE course_schedule_id_ = #{courseScheduleId}
        AND user_id_ IN
        <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
            #{userId}
        </foreach>
    </delete>
    <delete id="deleteByGroup">
        DELETE FROM student_attendance WHERE music_group_id_=#{groupId} AND group_type_=#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
    </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="groupType" column="group_type_"/>
        <result property="subjectId" column="subject_id_"/>
        <result property="subjectName" column="subject_name_"/>
        <result property="avatar" column="avatar_"/>
        <result property="phone" column="phone_"/>
        <result property="status" column="status_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
    </resultMap>

    <sql id="queryPageCondition">
        <where>
            cgsm.class_group_id_=#{classGroupId} AND cgsm.status_ != 'QUIT'
            <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} AND status_ != 'QUIT'
        GROUP BY status_
    </select>

    <select id="getStudentAttendanceInfo" parameterType="com.ym.mec.biz.dal.entity.StudentAttendance" resultMap="StudentAttendance">
        SELECT
            cs.class_group_id_,
            #{userId} user_id_,
            mg.id_ music_group_id_,
            #{teacherId} teacher_id_,
            #{status} status_,
            #{remark} remark_,
            #{courseScheduleId} course_schedule_id_,
            #{currentClassTimes} current_class_times_
        FROM
            course_schedule cs
            LEFT JOIN class_group cg ON cg.id_ = cs.class_group_id_
            LEFT JOIN music_group mg ON cg.music_group_id_ = mg.id_
        WHERE 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>
            (cs.del_flag_ != 1 OR cs.del_flag_ IS NULL)
            <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>
    <select id="findLatestAttendanceDate" resultType="date">
      SELECT MAX(create_time_) FROM student_attendance WHERE course_schedule_id_=#{courseScheduleId}
    </select>

    <resultMap id="StudentAttendanceResponse" type="com.ym.mec.biz.dal.dto.StudentAttendanceResponse">
        <result property="userId" column="user_id_"/>
        <result property="classDate" column="class_date_"/>
        <result property="status" column="status_"/>
        <result property="courseScheduleId" column="course_schedule_id_"/>
    </resultMap>

    <select id="findByClassGroupAndUsers" resultMap="StudentAttendanceResponse">
        SELECT
            cs.id_ course_schedule_id_,
            cs.class_date_,
            sa.user_id_,
            sa.status_
        FROM
            student_attendance sa
            LEFT JOIN course_schedule cs ON sa.course_schedule_id_=cs.id_
            WHERE
            (cs.del_flag_ != 1 OR cs.del_flag_ IS NULL)
            AND sa.class_group_id_=#{classGroupId}
            AND sa.user_id_ IN
            <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
                #{userId}
            </foreach>
    </select>

    <resultMap id="StudentAttendanceStatusCountDto" type="com.ym.mec.biz.dal.dto.StudentAttendanceStatusCountDto">
        <result property="userId" column="user_id_"/>
        <result property="status" column="status_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result property="num" column="num_"/>
    </resultMap>

    <select id="countStudentAttendanceStatus" resultMap="StudentAttendanceStatusCountDto">
        SELECT
            user_id_,
            status_,
            COUNT(course_schedule_id_) num_
        FROM
            student_attendance
            WHERE class_group_id_=#{classGroupId}
            AND user_id_ IN
            <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
                #{userId}
            </foreach>
        GROUP BY user_id_,status_
    </select>

    <select id="getCurrentClassTimesByCourses" resultType="map">
        SELECT
            course_schedule_id_ AS 'key',
            current_class_times_ AS 'value'
        FROM
            student_attendance
        WHERE
            course_schedule_id_ IN
            <foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
                #{courseScheduleId}
            </foreach>
            GROUP BY course_schedule_id_,current_class_times_
    </select>
    <select id="findLatestSignInDate" resultType="java.util.Date">
        SELECT
            MAX(create_time_)
        FROM
            student_attendance
            WHERE class_group_id_=#{classGroupId}
    </select>
    <select id="findByStatusAndCourseScheduleId" resultMap="StudentAttendance">
        SELECT * FROM student_attendance sa WHERE course_schedule_id_ = #{courseScheduleId} AND user_id_ = #{userId} LIMIT 1
    </select>

    <select id="queryNoSignStudentRecord" resultMap="StudentAttendance">
        SELECT cg.music_group_id_,cg.id_ class_group_id_,cssp.course_schedule_id_,
        cssp.user_id_,cg.current_class_times_,cg.group_type_
        FROM course_schedule_student_payment cssp
        LEFT JOIN class_group cg ON cg.id_ = cssp.class_group_id_
        WHERE cssp.user_id_ NOT IN
        (SELECT DISTINCT sa.user_id_ FROM student_attendance sa WHERE course_schedule_id_ = #{courseScheduleId})
        AND cssp.course_schedule_id_ = #{courseScheduleId}
    </select>
    <select id="countStudentAttendenceNum" resultType="int">
        SELECT
            COUNT( sa.user_id_ )
        FROM
            student_attendance sa
            LEFT JOIN course_schedule cs ON cs.id_ = sa.course_schedule_id_
        WHERE
            sa.create_time_ &gt; DATE_ADD( CONCAT( cs.class_date_, ' ', cs.start_class_time_ ), INTERVAL - 4 HOUR )
            AND sa.course_schedule_id_ = #{courseScheduleId}
            AND (sa.update_time_!='1970-01-01 00:00:00' OR sa.update_time_ IS NULL)
    </select>
    <select id="countStudentAttendancesByCourses" resultType="java.util.Map">
        SELECT
            course_schedule_id_ AS 'key',
            COUNT( id_ ) AS 'value'
        FROM
            student_attendance
        WHERE
            course_schedule_id_ IN
            <foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
                #{courseScheduleId}
            </foreach>
        GROUP BY
            course_schedule_id_
    </select>
    <select id="countCourseLeaveStudentNumWithFourHoursAgo" resultType="java.util.Map">
        SELECT
            sa.course_schedule_id_ AS 'key',
            COUNT( DISTINCT sa.user_id_ ) AS 'value'
        FROM
            student_attendance sa
            LEFT JOIN course_schedule cs ON cs.id_ = sa.course_schedule_id_
        WHERE
            sa.status_ = 'LEAVE'
            AND sa.create_time_ &lt; DATE_ADD( CONCAT( cs.class_date_, ' ', cs.start_class_time_ ), INTERVAL -#{hours} HOUR )
            AND sa.course_schedule_id_ IN
            <foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
                #{courseScheduleId}
            </foreach>
            GROUP BY sa.course_schedule_id_
    </select>
    <select id="findStudentAttendance" resultMap="StudentAttendance">
        SELECT sa.*,su.username_,su.phone_,IF(s.name_ IS NULL,s1.name_,s.name_) subject_name_
        FROM course_schedule_student_payment cssp
        LEFT JOIN student_attendance sa ON cssp.course_schedule_id_ = sa.course_schedule_id_ AND cssp.user_id_ = sa.user_id_
        LEFT JOIN sys_user su ON cssp.user_id_ = su.id_
        LEFT JOIN class_group cg ON cg.id_ = cssp.class_group_id_ AND cg.group_type_ = 'VIP'
        LEFT JOIN student_registration sr ON sr.user_id_ = cssp.user_id_ AND cssp.music_group_id_ = sr.music_group_id_
        LEFT JOIN `subject` s ON s.id_ = cg.subject_id_list_
        LEFT JOIN `subject` s1 ON s1.id_ = sr.actual_subject_id_
        WHERE cssp.course_schedule_id_ = #{search}
        ORDER BY sa.id_ DESC
        <include refid="global.limit"/>
    </select>
    <select id="countStudentAttendance" resultType="java.lang.Integer">
        SELECT COUNT(id_)
        FROM course_schedule_student_payment
        WHERE course_schedule_id_ = #{search}
    </select>
    <select id="findByCourseId" resultMap="StudentAttendance">
        SELECT * FROM student_attendance WHERE course_schedule_id_=#{courseId}
    </select>
    <select id="findByCourseIds" resultMap="StudentAttendance">
        SELECT * FROM student_attendance WHERE course_schedule_id_ IN
        <foreach collection="courseIds" item="courseId" open="(" close=")" separator=",">
            #{courseId}
        </foreach>
    </select>
</mapper>
