<?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="avatar_" property="avatar"/>
        <result column="teacher_avatar_" property="teacherAvatar"/>
        <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="visitFlag" property="visitFlag" 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="normal_remind_" property="normalRemind"/>
        <result column="current_class_times_" property="currentClassTimes"/>
        <result column="current_class_times_" property="currentClassTimes"/>
        <result column="current_schedule_id_" property="currentScheduleId"/>
        <result column="organ_name_" property="courseSchedule.organization.name"/>
        <result column="teacher_name_" property="courseSchedule.teacherName"/>
        <result column="teach_mode_" property="courseSchedule.teachMode"/>
        <result column="course_type_" property="courseSchedule.type"/>
        <result column="course_status_" property="courseSchedule.status"/>
        <result column="current_schedule_id_" property="courseSchedule.id"/>
        <result column="course_schedule_name_" property="courseSchedule.name"/>
        <result column="class_date_" property="courseSchedule.classDate"/>
        <result column="start_class_time_" property="courseSchedule.startClassTime"/>
        <result column="end_class_time_" property="courseSchedule.endClassTime"/>
        <result column="new_course_id_" property="courseSchedule.newCourseId"/>
    </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_,current_schedule_id_,visit_flag_,normal_remind_)
        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},#{currentScheduleId},#{visitFlag,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
        #{normalRemind})
    </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_,current_schedule_id_,visit_flag_,normal_remind_)
      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},#{studentAttendance.currentScheduleId},#{studentAttendance.visitFlag,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
          #{studentAttendance.normalRemind})
      </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="visitFlag != null">
                visit_flag_ = #{visitFlag,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="currentScheduleId != null">
                current_schedule_id_ = #{currentScheduleId},
            </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>
            <if test="normalRemind != null">
                normal_remind_ = #{normalRemind},
            </if>
            update_time_ = NOW()
        </set>
        WHERE id_ = #{id}
    </update>
    
    <update id="cleanCourseStudentSignOut">
        UPDATE student_attendance
        SET sign_out_time_= NULL
    	WHERE course_schedule_id_=#{courseId} AND user_id_ = #{userId}
    </update>
    <update id="updateLeaveVisitFlag">
        UPDATE student_attendance SET leave_visit_flag_ = 1 WHERE user_id_ = #{userId} AND status_ = 'LEAVE'
    </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="beMerged" column="be_merged_"/>
        <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) AND cs.pre_course_flag_ = 0
            <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 cs.pre_course_flag_ = 0
            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>
            AND remark_ IS NULL
        GROUP BY
            course_schedule_id_
    </select>
    <select id="countStudentAttendancesByCoursesAndStatus" resultType="java.util.Map">
        SELECT
        course_schedule_id_ AS 'key',
        COUNT( id_ ) AS 'value'
        FROM
        student_attendance
        WHERE
        status_=#{status, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
        AND 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 cssp.id_,cssp.course_schedule_id_,cssp.user_id_,cssp.group_type_,cssp.music_group_id_,sa.sign_in_time_,sa.sign_out_time_,
               CASE WHEN sa.status_ IS NULL THEN 'TRUANT' ELSE sa.status_ END status_,
        su.username_,su.phone_,su.avatar_,cs.teach_mode_,cs.type_ course_type_,o.name_ organ_name_,tu.real_name_ teacher_name_,tu.avatar_ teacher_avatar_,
        cs.name_ course_schedule_name_,cs.status_ course_status_,
        cs.actual_teacher_id_ teacher_id_,cs.class_date_ ,cs.start_class_time_,cs.end_class_time_ ,cs.new_course_id_,
        CASE WHEN sa.visit_flag_ IS NULL THEN 0 ELSE sa.visit_flag_ END visitFlag,
        CASE WHEN sa.status_ IS NULL OR sa.status_ = 'TRUANT' THEN '1LEAVE' ELSE sa.status_ END status_1
        FROM course_schedule_student_payment cssp
        LEFT JOIN course_schedule cs on cs.id_ = cssp.course_schedule_id_
        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 sys_user tu on tu.id_ = cs.actual_teacher_id_
        LEFT JOIN organization o on o.id_ = cs.organ_id_
        <include refid="findStudentAttendanceSql"/>
        GROUP BY cssp.id_
        ORDER BY
        <if test="orderFlag == 1 and search != null and search != ''">
            status_1,visitFlag,cssp.id_,
        </if>
        CONCAT(cs.class_date_, ' ', cs.start_class_time_) ASC
        <include refid="global.limit"/>
    </select>
    <sql id="findStudentAttendanceSql">
        <where>
            cs.del_flag_ = 0 AND cs.pre_course_flag_ = 0
            <if test="visitFlag != null">
                <if test="visitFlag == 1">
                    AND sa.visit_flag_ = 1
                </if>
                <if test="visitFlag == 0">
                    AND (sa.visit_flag_ = 0 OR sa.visit_flag_ IS NULL)
                </if>
            </if>
            <if test="courseScheduleId != null">
                AND cssp.course_schedule_id_ = #{courseScheduleId}
            </if>
            <if test="search != null">
                AND cssp.course_schedule_id_ = #{search}
            </if>
            <if test="studentID != null">
                AND cssp.user_id_ = #{studentID}
            </if>
            <if test="startDateOfCourse != null">
                AND cs.class_date_ &gt;= #{startDateOfCourse}
            </if>
            <if test="endDateOfCourse != null">
                AND cs.class_date_ &lt;= #{endDateOfCourse}
            </if>
            <if test="courseScheduleType != null">
                AND cs.type_ = #{courseScheduleType}
            </if>
            <if test="teacherId != null">
                AND cs.actual_teacher_id_ = #{teacherId}
            </if>
            <if test="courseStatus != null">
                AND cs.status_ = #{courseStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="status != null">
                AND (sa.status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
                <if test="status.code == 'TRUANT'">
                    OR sa.id_ IS NULL
                </if>
                )
            </if>
            <if test="groupType != null">
                and cssp.group_type_ = #{groupType}
            </if>
            <if test="musicGroupId != null">
                and cssp.music_group_id_ = #{musicGroupId}
            </if>
            <if test="organId != null and organId != ''">
                AND FIND_IN_SET(cs.organ_id_,#{organId})
            </if>
            <if test="ids != null and ids != ''">
                AND FIND_IN_SET(cssp.id_,#{ids})
            </if>
        </where>
    </sql>
    
    <select id="countStudentAttendance" resultType="java.lang.Integer">
        SELECT count(DISTINCT cssp.id_)
        FROM course_schedule_student_payment cssp
        left join course_schedule cs on cs.id_ = cssp.course_schedule_id_
        left join student_attendance sa on cssp.course_schedule_id_ = sa.course_schedule_id_ and cssp.user_id_ = sa.user_id_
        <include refid="findStudentAttendanceSql"/>
    </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>

    <select id="getWithCoursesAndExcludeQuitStudent" resultMap="StudentAttendance">
        SELECT sa.* FROM student_attendance sa
        LEFT JOIN class_group_student_mapper cgsm ON sa.class_group_id_=cgsm.class_group_id_ AND sa.user_id_=cgsm.user_id_
        WHERE cgsm.status_ NOT IN ('QUIT', 'QUIT_SCHOOL') AND course_schedule_id_ IN
        <foreach collection="courseIds" item="courseId" open="(" close=")" separator=",">
            #{courseId}
        </foreach>
    </select>
    <select id="findFirstSign" resultType="java.lang.Long">
        SELECT sa.course_schedule_id_ FROM student_attendance sa
        WHERE sa.current_schedule_id_ = #{courseScheduleId} AND sa.user_id_ = #{userId}
        AND sa.sign_in_time_ IS NOT NULL ORDER BY sa.sign_in_time_ ASC LIMIT 1
    </select>
    <resultMap id="ExportStudentAttendanceDtoMap" type="com.ym.mec.biz.dal.dto.ExportStudentAttendanceDto">
        <result property="classDate" column="class_date_"/>
        <result property="courseScheduleId" column="course_schedule_id_"/>
        <result property="courseScheduleName" column="course_schedule_name_"/>
        <result property="educationalTeacherId" column="educational_teacher_id_"/>
        <result property="educationalTeacherName" column="educational_teacher_name_"/>
        <result property="actualTeacherId" column="actual_teacher_id_"/>
        <result property="actualTeacherName" column="actual_teacher_name_"/>
        <result property="endClassTime" column="end_class_time_"/>
        <result property="groupId" column="music_group_id_"/>
        <result property="groupName" column="music_group_name_"/>
        <result property="groupType" column="group_type_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result property="guideTeacherId" column="guide_teacher_id_"/>
        <result property="guideTeacherName" column="guide_teacher_name_"/>
        <result property="organId" column="organ_id_"/>
        <result property="organName" column="organ_name_"/>
        <result property="startClassTime" column="start_class_time_"/>
        <result property="studentAttendanceStatus" column="attendance_status_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result property="userId" column="user_id_"/>
        <result property="username" column="username_"/>
    </resultMap>
    <select id="countExportStudentAttendancesQueryPage" resultType="java.lang.Integer">
        SELECT COUNT(cssp.id_)
        FROM course_schedule_student_payment cssp
        LEFT JOIN course_schedule cs ON cs.id_ = cssp.course_schedule_id_
        <include refid="exportStudentAttendancesQueryPageSql"/>
    </select>
    <sql id="exportStudentAttendancesQueryPageSql">
        <where>
                cs.pre_course_flag_ = 0
            <if test="organId != null and organId != ''">
                AND FIND_IN_SET(cs.organ_id_,#{organId})
            </if>
            <if test="groupType != null">
                AND cssp.group_type_ = #{groupType}
            </if>
            <if test="classStartDate != null and classStartDate != ''">
                AND cs.class_date_ >= #{classStartDate}
            </if>
            <if test="classEndDate != null and classEndDate != ''">
                AND cs.class_date_ &lt;= #{classEndDate}
            </if>
        </where>
    </sql>
    <select id="exportStudentAttendancesQueryPage"
            resultMap="ExportStudentAttendanceDtoMap">
        SELECT MAX(o.name_) organ_name_,MAX(o.id_) organ_id_,MAX(cs.music_group_id_) music_group_id_,MAX(mg.name_) music_group_name_,MAX(su.username_) username_,MAX(cssp.user_id_) user_id_,
        MAX(cs.name_) course_schedule_name_,MAX(cs.id_) course_schedule_id_,
        MAX(cs.class_date_) class_date_,MAX(cs.start_class_time_) start_class_time_,MAX(cs.end_class_time_) end_class_time_,
        MAX(su2.real_name_) guide_teacher_name_,MAX(st.teacher_id_) guide_teacher_id_
        ,MAX(su1.real_name_) educational_teacher_name_,MAX(mg.educational_teacher_id_) educational_teacher_id_,MAX(cs.group_type_) group_type_,
        MAX(cs.actual_teacher_id_) actual_teacher_id_,MAX(su3.real_name_) actual_teacher_name_,
        CASE WHEN MAX(sa.id_) IS NULL THEN 'TRUANT' ELSE MAX(sa.status_) END attendance_status_
        FROM course_schedule_student_payment cssp
        LEFT JOIN course_schedule cs ON cs.id_ = cssp.course_schedule_id_
        LEFT JOIN student_attendance sa ON cssp.course_schedule_id_ = sa.course_schedule_id_ AND sa.user_id_ = cssp.user_id_
        <if test="groupType == 'MUSIC'">
            LEFT JOIN music_group mg ON mg.id_ = cssp.music_group_id_
        </if>
        <if test="groupType == 'VIP'">
            LEFT JOIN vip_group mg ON mg.id_ = cssp.music_group_id_
        </if>
        <if test="groupType == 'PRACTICE'">
            LEFT JOIN practice_group mg ON mg.id_ = cssp.music_group_id_
        </if>
        LEFT JOIN sys_user su1 ON su1.id_ = mg.educational_teacher_id_
        LEFT JOIN organization o ON o.id_ = cs.organ_id_
        LEFT JOIN student st ON st.user_id_ = cssp.user_id_
        LEFT JOIN sys_user su ON su.id_ = cssp.user_id_
        LEFT JOIN sys_user su2 ON su2.id_ = st.teacher_id_
        LEFT JOIN sys_user su3 ON su3.id_ = cs.actual_teacher_id_
        <include refid="exportStudentAttendancesQueryPageSql"/>
        GROUP BY cssp.id_
        ORDER BY cssp.music_group_id_ DESC,cssp.id_ DESC
        <include refid="global.limit"/>
    </select>
    <select id="countErrorAttendance" resultType="java.lang.Integer">
        SELECT COUNT(cssp.id_) FROM course_schedule_student_payment cssp
        LEFT JOIN student_attendance sa ON sa.course_schedule_id_ = cssp.course_schedule_id_ AND cssp.user_id_ = sa.user_id_
        WHERE (sa.status_ != 'NORMAL' OR sa.status_ IS NULL) AND cssp.course_schedule_id_ = #{courseScheduleId}
    </select>
    <select id="getLeaveNum" resultType="java.lang.Integer">
        SELECT COUNT(sa.id_) FROM student_attendance sa
        LEFT JOIN course_schedule cs ON cs.id_ = sa.course_schedule_id_
        WHERE DATE_FORMAT(cs.class_date_,'%Y-%m') = #{month} AND sa.user_id_ = #{userId} AND sa.status_ = 'LEAVE' AND cs.pre_course_flag_ = 0
        <if test="attendanceId != null">
            AND sa.id_ != #{attendanceId}
        </if>
    </select>
    <select id="queryLeaveNum" resultType="java.util.Map">
        SELECT sa.user_id_ 'key',COUNT(sa.id_) 'value' FROM student_attendance sa
        LEFT JOIN course_schedule cs ON cs.id_ = sa.course_schedule_id_
        WHERE sa.status_ = 'LEAVE' AND DATE_FORMAT(cs.class_date_,'%Y-%m') = #{month} AND cs.pre_course_flag_ = 0
        AND sa.user_id_ IN
        <foreach collection="userIds" item="userId" separator="," open="(" close=")">
            #{userId}
        </foreach>
        GROUP BY sa.user_id_
    </select>
</mapper>
