<?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="group_type_" property="groupType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <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="update_attendance_type_" property="updateAttendanceEnum" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="remark_" property="remark"/>
        <result column="create_time_" property="createTime"/>
        <result column="current_class_times_" property="currentClassTimes"/>
        <result column="sign_in_longitude_latitude_" property="signInLongitudeLatitude"/>
        <result column="sign_out_longitude_latitude_" property="signOutLongitudeLatitude"/>
        <result column="longitude_latitude_" property="schoolLongitudeLatitude"/>
        <result column="complaints_type_" property="complaintsType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="is_complaints_" property="isComplaints"/>
        <result column="complaints_status_" property="complaintsStatus" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="complaints_content_" property="complaintsContent"/>
        <result column="dispose_content_" property="disposeContent"/>
        <result column="operator_" property="operator"/>
        <result column="complaints_time_" property="complaintsTime"/>
        <result column="current_schedule_id_" property="currentScheduleId"/>
        <result column="sign_out_attachments_" property="signOutAttachments"/>
        <result column="sign_out_remark_" property="signOutRemark"/>
        <result column="url_" property="url"/>
        <result column="sign_in_device_no_" property="signInDeviceNo"/>
        <result column="sign_out_device_no_" property="signOutDeviceNo"/>
        <result column="tenant_id_" property="tenantId"/>
    </resultMap>
    
    <resultMap type="com.ym.mec.biz.dal.dto.Mapper" id="Mapper">
    	<result column="key_" property="key"/>
    	<result column="value_" property="value"/>
    	<result column="course_schedule_id_" property="courseScheduleId"/>
    </resultMap>

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

    <!-- 全查询 -->
    <select id="findAll" resultMap="TeacherAttendance">
		SELECT * FROM teacher_attendance whre tenant_id_ = #{tenantId} ORDER BY id_
	</select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.TeacherAttendance" useGeneratedKeys="true"
            keyColumn="id" keyProperty="id">
        INSERT INTO teacher_attendance
        (group_type_,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_,current_class_times_,sign_in_longitude_latitude_,sign_out_longitude_latitude_,current_schedule_id_,sign_out_attachments_,
        sign_out_remark_,update_attendance_type_,url_,sign_in_device_no_,sign_out_device_no_,tenant_id_)
        VALUES(#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{teacherId},
        #{musicGroupId},#{classGroupId},#{courseScheduleId},#{signInTime},#{signInStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{remark},
        now(),#{signOutTime},#{signOutStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
        #{currentClassTimes},#{signInLongitudeLatitude},#{signOutLongitudeLatitude},#{currentScheduleId},#{signOutAttachments},#{signOutRemark},
        #{updateAttendanceEnum,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{url},#{signInDeviceNo},#{signOutDeviceNo},#{tenantId})
    </insert>

    <insert id="batchInsert" parameterType="com.ym.mec.biz.dal.entity.TeacherAttendance" useGeneratedKeys="true"
            keyColumn="id" keyProperty="id">
        INSERT INTO teacher_attendance
        (group_type_,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_,sign_in_longitude_latitude_,sign_out_longitude_latitude_,current_schedule_id_,sign_out_attachments_,
        sign_out_remark_,update_attendance_type_,url_,sign_in_device_no_,sign_out_device_no_,tenant_id_)
        VALUE
        <foreach collection="teacherAttendances" item="teacherAttendance" separator=",">
            (#{teacherAttendance.groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            #{teacherAttendance.teacherId},#{teacherAttendance.musicGroupId},#{teacherAttendance.classGroupId},
            #{teacherAttendance.courseScheduleId},#{teacherAttendance.signInTime},
            #{teacherAttendance.signInStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            #{teacherAttendance.remark},now(),#{teacherAttendance.signOutTime},
            #{teacherAttendance.signOutStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            #{teacherAttendance.signInLongitudeLatitude},#{teacherAttendance.signOutLongitudeLatitude},#{teacherAttendance.currentScheduleId},
            #{teacherAttendance.signOutAttachments},#{teacherAttendance.signOutRemark},
            #{teacherAttendance.updateAttendanceEnum,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{teacherAttendance.url},
            #{teacherAttendance.signInDeviceNo},#{teacherAttendance.signOutDeviceNo},#{teacherAttendance.tenantId})
        </foreach>
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.TeacherAttendance">
        UPDATE teacher_attendance
        <set>
            <if test="signInDeviceNo != null and signInDeviceNo != ''">
                sign_in_device_no_ = #{signInDeviceNo},
            </if>
            <if test="signOutDeviceNo != null and signOutDeviceNo != ''">
                sign_out_device_no_ = #{signOutDeviceNo},
            </if>
            <if test="url != null and url != ''">
                url_ = #{url},
            </if>
            <if test="updateAttendanceEnum != null">
                update_attendance_type_ = #{updateAttendanceEnum,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="signInStatus != null">
                sign_in_status_ = #{signInStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="currentScheduleId != null">
                current_schedule_id_ = #{currentScheduleId},
            </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="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>
            <if test="currentClassTimes != null">
                current_class_times_ = #{currentClassTimes},
            </if>
            <if test="signInLongitudeLatitude != null">
                sign_in_longitude_latitude_ = #{signInLongitudeLatitude},
            </if>
            <if test="signOutLongitudeLatitude != null">
                sign_out_longitude_latitude_ = #{signOutLongitudeLatitude},
            </if>
            <if test="isComplaints != null">
                is_complaints_ = #{isComplaints},
            </if>
            <if test="complaintsType != null">
                complaints_type_ = #{complaintsType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="complaintsStatus != null">
                complaints_status_ = #{complaintsStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="complaintsContent != null and complaintsContent != ''">
                complaints_content_ = #{complaintsContent},
            </if>
            <if test="disposeContent != null and disposeContent != ''">
                dispose_content_ = #{disposeContent},
            </if>
            <if test="operator != null">
                operator_ = #{operator},
            </if>
            <if test="complaintsTime != null">
                complaints_time_ = #{complaintsTime},
            </if>
            <if test="signOutAttachments != null">
                sign_out_attachments_ = #{signOutAttachments},
            </if>
            <if test="signOutRemark != null">
                sign_out_remark_ = #{signOutRemark},
            </if>
            <if test="signInDeviceNo != null and signInDeviceNo != ''">
                sign_in_device_no_ = #{signInDeviceNo},
            </if>
            <if test="signOutDeviceNo != null and signOutDeviceNo != ''">
                sign_out_device_no_ = #{signOutDeviceNo},
            </if>
        </set>
        WHERE id_ = #{id} and tenant_id_ = #{tenantId}
    </update>

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

    <delete id="batchDeleteByCourseSchedules">
        DELETE FROM teacher_attendance WHERE course_schedule_id_ IN
        <foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
            #{courseScheduleId}
        </foreach>
    </delete>

    <delete id="batchDeleteByCourseAndUsers">
        DELETE FROM teacher_attendance WHERE course_schedule_id_ = #{courseScheduleId}
        AND teacher_id_ IN
        <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
            #{userId}
        </foreach>
    </delete>

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

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM teacher_attendance where tenant_id_ = #{tenantId}
	</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="class_date_" property="classDate"/>
        <result column="type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="course_status_" property="courseStatus" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="name_" property="name"/>
        <result column="sign_in_status_" property="signInStatus"/>
        <result column="sign_out_status_" property="signOutStatus"/>
        <result column="remark_" property="remark"/>
        <result column="id_" property="courseScheduleId"/>
        <result column="end_class_time_" property="endClassTime"/>
    </resultMap>

    <resultMap id="TeacherAttendancesDto" type="com.ym.mec.biz.dal.dto.TeacherAttendancesDto">
        <result column="class_date_" property="classDate"/>
        <result column="teacher_attendance_id_" property="teacherAttendanceId"/>
        <result column="start_class_time_" property="startClassTime"/>
        <result column="end_class_time_" property="endClassTime"/>
        <result column="course_schedule_type_" property="courseScheduleType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="course_schedule_status_" property="courseScheduleStatus" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="course_schedule_name_" property="courseScheduleName"/>
        <result column="dispose_content_" property="disposeContent"/>
        <result column="teacher_name_" property="teacherName"/>
        <result column="job_nature_" property="jobNature" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="organ_name_" property="organName"/>
        <result column="teacher_id_" property="teacherId"/>
        <result column="sign_in_status_" property="signInStatus" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="sign_out_status_" property="signOutStatus" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="sign_in_time_" property="signInTime"/>
        <result column="sign_out_time_" property="signOutTime"/>
        <result column="complaints_status_" property="complaintsStatus" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="remark_" property="remark"/>
        <result column="course_schedule_id_" property="courseScheduleId"/>
    </resultMap>

    <sql id="queryCondition">
        <where>
            (cs.del_flag_ != 1 OR cs.del_flag_ IS NULL) AND cs.actual_teacher_id_ = ta.teacher_id_ AND cs.pre_course_flag_ = 0 and cs.tenant_id_ = #{tenantId}
            <if test="teacherId != null">
                AND cs.actual_teacher_id_ = #{teacherId}
            </if>
            <if test="courseStartDate != null">
                AND cs.class_date_ &gt;= #{courseStartDate}
            </if>
            <if test="courseEndDate != null">
                AND cs.class_date_ &lt;= #{courseEndDate}
            </if>
            <if test="signInStatus != null and signInStatus != '' and signInStatus != 3">
                AND ta.sign_in_status_ = #{signInStatus}
            </if>
            <if test="signOutStatus != null and signOutStatus != '' and signOutStatus != 3">
                AND ta.sign_out_status_ = #{signOutStatus}
            </if>
            <if test="signOutStatus != null and signOutStatus != '' and signOutStatus == 3">
                AND ta.sign_out_status_ IS NULL
            </if>
            <if test="signInStatus != null and signInStatus != '' and signInStatus == 3">
                AND ta.sign_in_status_ IS NULL
            </if>
            <if test="courseScheduleType != null">
                AND cs.type_ = #{courseScheduleType}
            </if>
            <if test="classGroupName != null">
                AND cs.name_ LIKE CONCAT('%',#{classGroupName},'%')
            </if>
        </where>
    </sql>
    <sql id="queryTeacherAttendancesCondition">
        <where>
            cs.del_flag_ = 0 AND cs.pre_course_flag_ = 0 and cs.tenant_id_ = #{tenantId}
            AND (cs.new_course_id_ IS NULL OR cs.new_course_id_=cs.id_)
            <if test="search != null and search != ''">
                AND (su.id_ = #{search} OR su.real_name_ LIKE CONCAT('%',#{search},'%') OR cs.id_ = #{search} OR cs.name_ LIKE CONCAT('%',#{search},'%'))
            </if>
            <if test="courseStartDate != null and courseStartDate != ''">
                AND cs.class_date_ &gt;= #{courseStartDate}
            </if>
            <if test="courseEndDate != null and courseEndDate != ''">
                AND cs.class_date_ &lt;= #{courseEndDate}
            </if>
            <if test="signInStatus != null and signInStatus != '' and signInStatus != 3">
                AND ta.sign_in_status_ = #{signInStatus}
            </if>
            <if test="signOutStatus != null and signOutStatus != '' and signOutStatus != 3">
                AND ta.sign_out_status_ = #{signOutStatus}
            </if>
            <if test="signOutStatus != null and signOutStatus != '' and signOutStatus == 3">
                AND ta.sign_out_status_ IS NULL
            </if>
            <if test="signInStatus != null and signInStatus != '' and signInStatus == 3">
                AND ta.sign_in_status_ IS NULL
            </if>
            <if test="courseScheduleType != null  and courseScheduleType != ''">
                AND cs.type_ = #{courseScheduleType}
            </if>
            <if test="courseScheduleStatus != null  and courseScheduleStatus != ''">
                AND cs.status_ = #{courseScheduleStatus}
            </if>
            <if test="organId != null and organId != ''">
                AND FIND_IN_SET(cs.organ_id_,#{organId})
            </if>
            <if test="teacherId != null">
                AND ta.teacher_id_ = #{teacherId}
            </if>
            <if test="teacherAttendanceId != null and teacherAttendanceId != ''">
                AND FIND_IN_SET(ta.id_,#{teacherAttendanceId})
            </if>
            <if test="jobNature != null">
                AND t.job_nature_ = #{jobNature,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
        </where>
    </sql>

    <select id="getTeacherPersonalAttendances" parameterType="map" resultMap="teacherPersonalAttendance">
        SELECT
            cs.class_date_,cs.start_class_time_,cs.name_,cs.type_,cs.status_ course_status_,
            IF(ta.sign_in_status_ IS NULL,3,ta.sign_in_status_) sign_in_status_,
            IF(ta.sign_out_status_ IS NULL,3,ta.sign_out_status_) sign_out_status_,
            ta.remark_,cs.id_,cs.end_class_time_
        FROM
            course_schedule cs
            LEFT JOIN teacher_attendance ta  ON ta.course_schedule_id_=cs.id_
        <include refid="queryCondition"/>
        ORDER BY cs.class_date_ ASC,cs.start_class_time_ ASC
        <include refid="global.limit"/>
    </select>

    <select id="queryTeacherAttendances" parameterType="map" resultMap="TeacherAttendancesDto">
        SELECT su.real_name_ teacher_name_,su.id_ teacher_id_,csts.course_schedule_id_,cs.name_ course_schedule_name_,
        cs.status_ course_schedule_status_,
        cs.class_date_,cs.start_class_time_,cs.end_class_time_,cs.type_ course_schedule_type_,
        IF(ta.sign_in_status_ IS NULL,3,ta.sign_in_status_) sign_in_status_,
        IF(ta.sign_out_status_ IS NULL,3,ta.sign_out_status_) sign_out_status_,
        ta.sign_in_time_,ta.sign_out_time_,ta.remark_,o.name_ organ_name_,t.job_nature_,
        ta.complaints_status_,ta.id_ teacher_attendance_id_,ta.dispose_content_
        FROM course_schedule_teacher_salary csts
        LEFT JOIN course_schedule cs ON cs.id_ = csts.course_schedule_id_
        LEFT JOIN teacher_attendance ta ON ta.course_schedule_id_ = csts.course_schedule_id_ AND ta.teacher_id_ = csts.user_id_
        LEFT JOIN sys_user su ON su.id_ = csts.user_id_
        LEFT JOIN teacher t ON t.id_ = csts.user_id_
        LEFT JOIN organization o ON o.id_ = cs.organ_id_
        <include refid="queryTeacherAttendancesCondition"/>
        ORDER BY cs.class_date_ ASC,cs.start_class_time_ ASC
        <include refid="global.limit"/>
    </select>

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

    <select id="countTeacherAttendances" resultType="int">
        SELECT COUNT(csts.id_)
        FROM course_schedule_teacher_salary csts
        LEFT JOIN course_schedule cs ON cs.id_ = csts.course_schedule_id_
        LEFT JOIN teacher_attendance ta ON ta.course_schedule_id_ = csts.course_schedule_id_ AND ta.teacher_id_ = csts.user_id_
        LEFT JOIN sys_user su ON su.id_ = csts.user_id_
        LEFT JOIN teacher t ON t.id_ = csts.user_id_
        <include refid="queryTeacherAttendancesCondition"/>
    </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} LIMIT 1
    </select>

    <resultMap id="TeacherAbnormalAttendanceInfoDto" type="com.ym.mec.biz.dal.dto.TeacherAbnormalAttendanceInfoDto">
        <result property="teacherAttendanceId" column="teacher_attendance_id_"/>
        <result property="courseScheduleId" column="course_schedule_id_"/>
        <result property="courseScheduleName" column="course_schedule_name_"/>
        <result property="classDate" column="class_date_"/>
        <result property="startClassTime" column="start_class_time_"/>
        <result property="endClassTime" column="end_class_time_"/>
        <result property="signInTime" column="sign_in_time_"/>
        <result property="teacherId" column="teacher_id_"/>
        <result property="teacherName" column="teacher_name_"/>
        <result property="remark" column="remark_"/>
    </resultMap>

    <select id="findTeacherAbnormalSignInCourseScheduleRecord" resultMap="TeacherAbnormalAttendanceInfoDto">
        SELECT
            ta.id_ teacher_attendance_id_,
            cs.id_ course_schedule_id_,
            cs.name_ course_schedule_name_,
            cs.class_date_,
            CONCAT( cs.class_date_, ' ', cs.start_class_time_ ) start_class_time_,
            CONCAT( cs.class_date_, ' ', cs.end_class_time_ ) end_class_time_,
            ta.sign_in_time_,
            ta.teacher_id_,
            su.username_ teacher_name_,
            ta.remark_
        FROM
            teacher_attendance ta
            LEFT JOIN course_schedule cs ON ta.course_schedule_id_ = cs.id_
            LEFT JOIN music_group mg ON ta.music_group_id_ = mg.id_
            LEFT JOIN sys_user su ON su.id_ = ta.teacher_id_
        WHERE
            (cs.del_flag_ != 1 OR cs.del_flag_ IS NULL)
            AND mg.organ_id_ = #{organId}
            AND ta.sign_in_status_ = #{signInStatus}
            <if test="date==null">
                AND DATE_FORMAT( ta.sign_in_time_, '%Y%m%d' ) = DATE_FORMAT( NOW(), '%Y%m%d' )
            </if>
            <if test="date!=null">
                AND DATE_FORMAT( ta.sign_in_time_, '%Y%m%d' ) = DATE_FORMAT( #{date}, '%Y%m%d' )
            </if>
    </select>
    <select id="findTeacherAbnormalSignInDates" resultType="java.util.Date">
        SELECT
            DATE_FORMAT(ta.sign_in_time_,'%Y%m%d') sign_date_
        FROM
            teacher_attendance ta
            LEFT JOIN music_group mg ON ta.music_group_id_ = mg.id_
        WHERE
            mg.organ_id_ = #{organId}
            AND ta.sign_in_status_ != 1
            <if test="date==null">
                AND DATE_FORMAT( ta.sign_in_time_, '%Y%m' ) = DATE_FORMAT( NOW(), '%Y%m' )
            </if>
            <if test="date!=null">
                AND DATE_FORMAT( ta.sign_in_time_, '%Y%m' ) = DATE_FORMAT( #{date}, '%Y%m' )
            </if>
        GROUP BY DATE_FORMAT(ta.sign_in_time_,'%Y%m%d')
    </select>
    <select id="countTeacherAbnormalSignInClassTimes" resultType="int">
        SELECT
            COUNT(*)
        FROM
          teacher_attendance ta
        LEFT JOIN music_group mg ON ta.music_group_id_ = mg.id_
        WHERE
          mg.organ_id_ = #{organId}
        AND ta.sign_in_status_ = #{signInStatus}
        <if test="date==null">
            AND DATE_FORMAT( ta.sign_in_time_, '%Y%m%d' ) = DATE_FORMAT( NOW(), '%Y%m%d' )
        </if>
        <if test="date!=null">
            AND DATE_FORMAT( ta.sign_in_time_, '%Y%m%d' ) = DATE_FORMAT( #{date}, '%Y%m%d' )
        </if>
    </select>

    <select id="queryNoSignOutListByOverMinutes" resultMap="Mapper">
        SELECT ta.teacher_id_ key_,u.real_name_ value_,cs.id_ course_schedule_id_
        FROM course_schedule cs RIGHT JOIN teacher_attendance ta on ta.course_schedule_id_ = cs.id_
        LEFT JOIN sys_user u on ta.teacher_id_ = u.id_
        WHERE ta.sign_out_status_ IS NULL and cs.tenant_id_ = #{tenantId}
        AND (cs.new_course_id_ IS NULL OR cs.new_course_id_=cs.id_)
        AND CONCAT( cs.class_date_, ' ', cs.end_class_time_ ) &lt;= DATE_SUB(now(),INTERVAL #{minutes} MINUTE)
        AND cs.class_date_ = DATE_FORMAT(NOW(),'%Y-%m-%d') AND cs.del_flag_ = 0
        AND cs.group_type_ = 'MUSIC'
    </select>
    <select id="findTeacherIdByCourseSchedule" resultMap="TeacherAttendance">
        SELECT
          *
        FROM teacher_attendance
        WHERE course_schedule_id_ IN
        <foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
            #{courseScheduleId}
        </foreach>
    </select>
    <select id="findByCourseSchedules" resultMap="TeacherAttendance">
        SELECT
        *
        FROM teacher_attendance
        WHERE course_schedule_id_ IN
        <foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
            #{courseScheduleId}
        </foreach>
    </select>
    <select id="findCourseTimeByClassGroup" resultType="java.util.Map">
        SELECT
            course_schedule_id_ AS 'key',
            current_class_times_ AS 'value'
        FROM
            `teacher_attendance`
        WHERE
            class_group_id_ = #{classGroupId}
    </select>
    <select id="findCourseTimeByCourseSchedules" resultType="java.util.Map">
        SELECT
            course_schedule_id_ AS 'key',
            current_class_times_ AS 'value'
        FROM
            `teacher_attendance`
        WHERE
            course_schedule_id_ IN
            <foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
                #{courseScheduleId}
            </foreach>
    </select>
    <select id="getMainTeacherAttendance" resultMap="TeacherAttendance">
        SELECT ta.*,s.longitude_latitude_ FROM teacher_attendance ta
        LEFT JOIN course_schedule cs ON ta.course_schedule_id_ = cs.id_
        LEFT JOIN school s ON cs.schoole_id_ = s.id_
        WHERE ta.course_schedule_id_ = #{courseScheduleId} and ta.teacher_id_ = cs.actual_teacher_id_
    </select>
    <select id="getTotalMinutes" resultType="java.lang.Integer">
        SELECT ROUND((UNIX_TIMESTAMP(CONCAT(ta.sign_out_time_)) - UNIX_TIMESTAMP(CONCAT(cs.class_date_,' ',cs.start_class_time_))) / 60)
        FROM teacher_attendance ta
        LEFT JOIN course_schedule cs ON cs.id_ = ta.course_schedule_id_
        WHERE ta.course_schedule_id_ = #{courseId} AND ta.teacher_id_ = #{teacherId}
    </select>
    <select id="queryTeacherExceptionAttendance" resultType="java.lang.Integer">
        SELECT DISTINCT ta.teacher_id_ FROM teacher_attendance ta
        LEFT JOIN course_schedule cs ON cs.id_ = ta.course_schedule_id_
        WHERE cs.del_flag_ = 0 AND cs.class_date_ = #{format}
        AND (cs.new_course_id_ IS NULL OR cs.new_course_id_=cs.id_)
        AND ta.teacher_id_ = cs.actual_teacher_id_ AND CONCAT(cs.class_date_,' ',cs.end_class_time_) &lt; NOW()
        AND (ta.sign_in_status_ IS NULL OR ta.sign_in_status_ = 0 OR ta.sign_out_status_ IS NULL OR ta.sign_out_status_ = 0)
    </select>
    <resultMap id="TeacherAttendanceComplaintsDtoMap" type="com.ym.mec.biz.dal.dto.TeacherAttendanceComplaintsDto">
        <result property="organName" column="organ_name_"/>
        <result property="courseScheduleId" column="course_schedule_id_"/>
        <result property="courseScheduleName" column="course_schedule_name_"/>
        <result property="courseScheduleTime" column="course_schedule_time_"/>
        <result property="courseScheduleStartTime" column="start_class_time_"/>
        <result property="courseScheduleEndTime" column="end_class_time_"/>
        <result property="signInStatus" column="sign_in_status_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result property="signOutStatus" column="sign_out_status_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result property="teacherName" column="teacher_name_"/>
        <result property="complaintsStatusEnum" column="complaints_status_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="complaints_type_" property="complaintsType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result property="operatorName" column="operator_name_"/>
        <result property="signInTime" column="sign_in_time_"/>
        <result property="signOutTime" column="sign_out_time_"/>
        <result property="complaintsContent" column="complaints_content_"/>
        <result property="disposeContent" column="dispose_content_"/>
        <result property="teacherAttendanceId" column="teacher_attendance_id_"/>
        <result property="url" column="url_"/>
        <result property="teachMode" column="teach_mode_"/>
        <result property="courseType" column="course_type_"/>
        <result property="updateAttendanceEnum" column="update_attendance_type_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
    </resultMap>
    <select id="queryTeacherAttendanceComplaints" resultMap="TeacherAttendanceComplaintsDtoMap">
        SELECT o.name_ organ_name_,ta.course_schedule_id_,cs.name_ course_schedule_name_,cs.class_date_ course_schedule_time_,
        IF(ta.sign_in_status_ IS NULL,3,ta.sign_in_status_) sign_in_status_,
        IF(ta.sign_out_status_ IS NULL,3,ta.sign_out_status_) sign_out_status_,
        suo.real_name_ operator_name_,su.real_name_ teacher_name_,ta.complaints_status_,ta.sign_in_time_,ta.sign_out_time_
        ,ta.complaints_content_,ta.dispose_content_,cs.end_class_time_,cs.start_class_time_,
        ta.id_ teacher_attendance_id_,ta.update_attendance_type_,ta.url_,ta.complaints_type_,cs.teach_mode_,cs.type_ course_type_
        FROM teacher_attendance ta
        LEFT JOIN sys_user su ON ta.teacher_id_ = su.id_
        LEFT JOIN teacher t ON t.id_ = su.id_
        LEFT JOIN organization o ON o.id_ = t.organ_id_
        LEFT JOIN sys_user suo ON suo.id_ = ta.operator_
        LEFT JOIN course_schedule cs ON cs.id_ = ta.course_schedule_id_
        <include refid="queryTeacherAttendanceComplaintsSql"/>
        ORDER BY cs.class_date_ DESC,cs.start_class_time_ DESC
        <include refid="global.limit"/>
    </select>
    <sql id="queryTeacherAttendanceComplaintsSql">
        <where>
            ta.is_complaints_ = 1 and ta.tenant_id_ = #{tenantId}
            <if test="organId != null and organId != ''">
                AND FIND_IN_SET(t.organ_id_, #{organId})
            </if>
            <if test="complaintsTime != null and complaintsTime != ''">
                AND ta.complaints_time_ = #{complaintsTime}
            </if>
            <if test="complaintsStatus != null and complaintsStatus != ''">
                AND ta.complaints_status_ = #{complaintsStatus}
            </if>
            <if test="search != null and search != ''">
                AND (ta.course_schedule_id_ = #{search} OR su.real_name_ LIKE CONCAT('%',#{search},'%') OR ta.teacher_id_ = #{search})
            </if>
            <if test="courseScheduleStartDate != null and courseScheduleStartDate != ''">
                AND cs.class_date_ >= #{courseScheduleStartDate}
            </if>
            <if test="courseScheduleEndDate != null and courseScheduleEndDate != ''">
                AND cs.class_date_ &lt;= #{courseScheduleEndDate}
            </if>
            <if test="settlemented!=null and settlemented==true">
                AND EXISTS (SELECT id_ FROM course_schedule_teacher_salary WHERE cs.id_=course_schedule_id_ AND settlement_time_ IS NOT NULL)
            </if>
            <if test="settlemented!=null and settlemented==false">
                AND EXISTS (SELECT id_ FROM course_schedule_teacher_salary WHERE cs.id_=course_schedule_id_ AND settlement_time_ IS NULL)
            </if>
        </where>
    </sql>
    <select id="countTeacherAttendanceComplaints" resultType="java.lang.Integer">
        SELECT COUNT(DISTINCT ta.id_)
        FROM teacher_attendance ta
        LEFT JOIN sys_user su ON ta.teacher_id_ = su.id_
        LEFT JOIN teacher t ON t.id_ = su.id_
        LEFT JOIN course_schedule cs ON cs.id_ = ta.course_schedule_id_
        <include refid="queryTeacherAttendanceComplaintsSql"/>
    </select>
    <select id="getAttendClassTime" resultType="java.lang.Integer">
        SELECT ROUND((UNIX_TIMESTAMP(CASE WHEN MIN(ta.sign_out_time_) &lt; MIN(sa.sign_out_time_) THEN MIN(ta.sign_out_time_) ELSE MIN(sa.sign_out_time_) END)-
        UNIX_TIMESTAMP(CASE WHEN MAX(ta.sign_in_time_) > MAX(sa.sign_in_time_) THEN MAX(ta.sign_in_time_) ELSE MAX(sa.sign_in_time_) END))/60)
        FROM teacher_attendance ta
        LEFT JOIN student_attendance sa ON ta.course_schedule_id_ = sa.course_schedule_id_
        WHERE ta.course_schedule_id_ = #{courseScheduleId}
        AND ta.sign_in_time_ IS NOT NULL AND ta.sign_out_time_ IS NOT NULL
        AND sa.sign_in_time_ IS NOT NULL AND sa.sign_out_time_ IS NOT NULL
    </select>
    <select id="findFirstSign" resultType="java.lang.Long">
        SELECT ta.course_schedule_id_ FROM teacher_attendance ta
        WHERE ta.current_schedule_id_ = #{currentScheduleId} AND ta.teacher_id_ = #{userId}
        AND ta.sign_in_time_ IS NOT NULL ORDER BY ta.sign_in_time_ ASC LIMIT 1
    </select>
    <update id="updateViPSignOutStatus" parameterType="string">
    	UPDATE teacher_attendance SET sign_out_status_ = 1,sign_out_time_= now()
    	WHERE FIND_IN_SET(course_schedule_id_,#{courseScheduleIdList}) AND sign_in_status_ = 1
    </update>
    <update id="cleanCourseTeacherSignInfo">
        UPDATE teacher_attendance
        SET sign_in_time_ = NULL,sign_in_status_=NULL,sign_out_status_ = NULL,sign_out_time_= NULL
    	WHERE course_schedule_id_=#{courseScheduleId}
    </update>
    <update id="batchCleanCourseTeacherSignInfo">
        UPDATE teacher_attendance
        SET sign_in_time_ = NULL,sign_in_status_=NULL,sign_out_status_ = NULL,sign_out_time_= NULL
    	WHERE course_schedule_id_ IN
    	<foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
            #{courseScheduleId}
        </foreach>
    </update>
    <update id="cuntinueCourseSign">
        UPDATE teacher_attendance
        <set>
            <if test="startDateTime != null">
                sign_in_time_ = #{startDateTime},sign_in_status_ = 1,
            </if>
            <if test="endDateTime != null">
                sign_out_status_ = #{status},sign_out_time_= #{endDateTime}
            </if>
        </set>
    	WHERE course_schedule_id_ = #{courseScheduleId}
    </update>
    <update id="batchUpdateTeacher">
        UPDATE teacher_attendance
        SET teacher_id_ = #{teacherId},sign_in_time_ = null,sign_in_status_ = null,sign_out_status_ = null,sign_out_time_= null
        WHERE course_schedule_id_ IN
        <foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
            #{courseScheduleId}
        </foreach>
    </update>
    <update id="clearAttendanceComplaints">
        UPDATE teacher_attendance
        SET is_complaints_ = 0,complaints_status_ = NULL,complaints_content_ = NULL,
            dispose_content_ = NULL,operator_ = NULL,complaints_time_ = NULL,complaints_type_ = NULL,
        sign_out_device_no_ = NULL,sign_in_device_no_ = NULL,url_ = NULL,update_attendance_type_ = NULL,
            sign_out_remark_ = NULL,sign_out_attachments_ = NULL,current_schedule_id_ = NULL,remark_ = NULL
        ,sign_in_longitude_latitude_ = NULL,sign_out_longitude_latitude_ = NULL
        WHERE course_schedule_id_ IN
        <foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
            #{courseScheduleId}
        </foreach>
    </update>
    <update id="clearAttendanceComplaint">
        UPDATE teacher_attendance
        SET is_complaints_ = 0,complaints_status_ = NULL,complaints_content_ = NULL,
            dispose_content_ = NULL,operator_ = NULL,complaints_time_ = NULL,complaints_type_ = NULL,
            sign_out_device_no_ = NULL,sign_in_device_no_ = NULL,url_ = NULL,update_attendance_type_ = NULL,
            sign_out_remark_ = NULL,sign_out_attachments_ = NULL,current_schedule_id_ = NULL,remark_ = NULL
                ,sign_in_longitude_latitude_ = NULL,sign_out_longitude_latitude_ = NULL
        WHERE course_schedule_id_ = #{courseScheduleId}
    </update>
    <update id="updateDeviceNum">
        UPDATE teacher_attendance
        <set>
            <if test="signInDeviceNum != null and signInDeviceNum != ''">
                sign_in_device_no_ = #{signInDeviceNum}
            </if>
            <if test="signOutDeviceNum != null and signOutDeviceNum != ''">
                sign_out_device_no_ = #{signOutDeviceNum}
            </if>
        </set>
        WHERE course_schedule_id_ = #{courseId} AND teacher_id_ = #{userId}
    </update>

    <delete id="deleteByMusicGroupId" parameterType="map" >
		DELETE ta FROM teacher_attendance ta LEFT JOIN course_schedule cs ON ta.course_schedule_id_ = cs.id_
		WHERE ta.music_group_id_=#{musicGroupId} AND ta.group_type_=#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler} and cs.status_ = 'NOT_START'
	</delete>
	
    <delete id="deletebyCourseScheduleId">
        DELETE from teacher_attendance WHERE course_schedule_id_=#{courseScheduleId}
    </delete>
    <delete id="deleteByGroup">
        DELETE FROM teacher_attendance WHERE music_group_id_=#{groupId} AND group_type_=#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
    </delete>
</mapper>
