<?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.xjrsoft.module.attendance.mapper.TeacherAttendanceRecordMapper">
    <select id="getPage" parameterType="com.xjrsoft.module.attendance.dto.TeacherDetailsDto" resultType="com.xjrsoft.module.attendance.vo.TeacherStatisticsPageVo">
        SELECT t2.name AS teacher_name,t2.mobile,t1.attendance_status AS STATUS,t1.record_time,
        replace(replace(t1.attendance_mode,1, '人脸'),2,'车辆') as attendance_mode,t1.car_number,t1.user_id,
        (
            select group_concat(c1.name) from xjr_department c1
            inner join xjr_user_dept_relation c2 on c1.id = c2.dept_id
            where c1.delete_mark = 0 and c2.user_id = t1.user_id
        ) as dept_name
        FROM teacher_attendance_record t1
        INNER JOIN xjr_user t2 ON t1.user_id = t2.id
        <if test="dto.deptId != null">
            inner join xjr_user_dept_relation t3 on t2.id = t3.user_id
            and t3.dept_id = #{dto.deptId}
        </if>
        WHERE t1.delete_mark = 0
        AND DATE_FORMAT(t1.attendance_date,'%Y-%m-%d') = #{dto.date}
        AND t1.time_interval = #{dto.timePeriod}
        <if test="dto.name != null and dto.name != ''">
            and t2.name like concat('%', #{dto.name},'%')
        </if>
        <if test="dto.credentialNumber != null and dto.credentialNumber != ''">
            and t2.credential_number like concat('%', #{dto.credentialNumber},'%')
        </if>
        <if test="dto.carNumber != null and dto.carNumber != ''">
            and t1.car_number like concat('%', #{dto.carNumber},'%')
        </if>
        <if test="dto.attendanceMode != null and dto.attendanceMode != ''">
            and t1.attendance_mode like concat('%', #{dto.attendanceMode},'%')
        </if>
        <if test="dto.attendanceStatus != null and dto.attendanceStatus != ''">
            and t1.attendance_status like concat('%', #{dto.attendanceStatus},'%')
        </if>
    </select>
    <select id="getList" parameterType="com.xjrsoft.module.attendance.dto.TeacherDetailsDto" resultType="com.xjrsoft.module.attendance.vo.TeacherStatisticsPageVo">
        SELECT t2.name AS teacher_name,t2.mobile,t1.attendance_status AS STATUS,t1.record_time,
        replace(replace(t1.attendance_mode,1, '人脸'),2,'车辆') as attendance_mode,t1.car_number,t1.user_id FROM teacher_attendance_record t1
        INNER JOIN xjr_user t2 ON t1.user_id = t2.id
        <if test="dto.deptId != null">
            inner join xjr_user_dept_relation t3 on t2.id = t3.user_id
            and t3.dept_id = #{dto.deptId}
        </if>

        WHERE t1.delete_mark = 0
        AND DATE_FORMAT(t1.attendance_date,'%Y-%m-%d') = #{dto.date}
        AND t1.time_interval = #{dto.timePeriod}
        <if test="dto.name != null and dto.name != ''">
            and t2.name like concat('%', #{dto.name},'%')
        </if>
        <if test="dto.credentialNumber != null and dto.credentialNumber != ''">
            and t2.credential_number like concat('%', #{dto.credentialNumber},'%')
        </if>
        <if test="dto.carNumber != null and dto.carNumber != ''">
            and t1.car_number like concat('%', #{dto.carNumber},'%')
        </if>
        <if test="dto.attendanceMode != null and dto.attendanceMode != ''">
            and t1.attendance_mode like concat('%', #{dto.attendanceMode},'%')
        </if>

    </select>

    <select id="getLeaveList" parameterType="com.xjrsoft.module.teacher.entity.WfTeacherleave" resultType="com.xjrsoft.module.attendance.entity.TeacherAttendanceRecord">
        SELECT * FROM teacher_attendance_record WHERE user_id = #{dto.userId}
        AND (
            ( #{dto.leaveStartTime} BETWEEN start_time AND end_time)
            OR (#{dto.leaveEndTime} BETWEEN start_time AND end_time)
            OR (#{dto.leaveStartTime} > start_time AND end_time > #{dto.leaveEndTime})
            OR (start_time > #{dto.leaveStartTime} AND #{dto.leaveEndTime} > end_time)
        )
    </select>
</mapper>