<?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.yida.data.attendance.mapper.EduAttendanceStaffRecordMapper">


	<select id="findPageStaffRecord" resultType="com.yida.data.attendance.vo.StaffRecordVO">
    SELECT
	a.id,
	a.create_time AS time,
	a.pic_url,
	a.staff_id,
	a.scope,
	b.`name` as device_name
    FROM
	edu_attendance_staff_record a
	LEFT JOIN edu_attendance_device b on a.device_id = b.id
    WHERE
	1 = 1
	AND a.school_id = #{schoolId}
	<if test="start != null and start !=''">
        AND a.create_time BETWEEN #{start}
        AND #{end}
    </if>
     <if test="staffId != null and staffId.size>0">
         AND a.staff_id IN
         <foreach collection="staffId" separator="," open="(" close=")" item="item">
            #{item}
         </foreach>
     </if>


    </select>
    <select id="finMaxRecord"
            resultType="com.yida.data.common.core.entity.attendance.EduAttendanceStaffRecord">
    SELECT
	*
	FROM
	`edu_attendance_staff_record`
	WHERE
	staff_id = #{staffId}
	AND create_time &lt;= #{atDate}
	ORDER BY
	create_time DESC
	LIMIT 0,
	1
    </select>
    <select id="finMinRecord"
            resultType="com.yida.data.common.core.entity.attendance.EduAttendanceStaffRecord">
    SELECT
	*
    FROM
	`edu_attendance_staff_record`
    WHERE
	staff_id = #{staffId}
	AND create_time &lt;= #{endStiTime}
	and scope = 0
    ORDER BY
	create_time asc
	LIMIT 0,
	1
    </select>
	<select id="finMinInRecord"
			resultType="com.yida.data.common.core.entity.attendance.EduAttendanceStaffRecord">
	SELECT
	*
    FROM
	`edu_attendance_staff_record`
    WHERE
	staff_id = #{staffId}
	AND create_time >= #{startStiTime}
	and scope = 0
    ORDER BY
	create_time asc
	LIMIT 0,
	1
	</select>
	<select id="findMinOutRecord"
			resultType="com.yida.data.common.core.entity.attendance.EduAttendanceStaffRecord">
	SELECT
	*
    FROM
	`edu_attendance_staff_record`
    WHERE
	staff_id = #{staffId}
	AND create_time >= #{endStiTime}
	and scope = 1
    ORDER BY
	create_time asc
	LIMIT 0,
	1
	</select>
	<select id="findMaxOutRecord"
			resultType="com.yida.data.common.core.entity.attendance.EduAttendanceStaffRecord">
	SELECT
	*
    FROM
	`edu_attendance_staff_record`
    WHERE
	staff_id = #{staffId}
	AND create_time >= #{startStiTime}
	and scope = 1
    ORDER BY
	create_time desc
	LIMIT 0,
	1
	</select>
    <select id="findOutRecord"
            resultType="com.yida.data.common.core.entity.attendance.EduAttendanceStaffRecord">
	SELECT
	*
    FROM
	`edu_attendance_staff_record`
    WHERE
	staff_id = #{staffId}
	AND create_time >= #{startStiTime}
	and scope = 1
    ORDER BY
	create_time desc
	</select>
	<select id="findInRecord"
			resultType="com.yida.data.common.core.entity.attendance.EduAttendanceStaffRecord">
	SELECT
	*
    FROM
	`edu_attendance_staff_record`
    WHERE
	staff_id = #{staffId}
	AND create_time BETWEEN #{startStiTime} and #{endStiTime}
	and scope = 0
    ORDER BY
	create_time asc
	</select>
	<select id="fintRecord" resultType="com.yida.data.common.core.entity.attendance.EduAttendanceStaffRecord">
	SELECT
	*
    FROM
	`edu_attendance_staff_record`
    WHERE
	staff_id = #{staffId}
	AND create_time BETWEEN #{startStiTime} and #{endStiTime}
	and scope = 0
    ORDER BY
	create_time desc
	LIMIT 0,
	1
	</select>
	
	<!-- 查询前一天的最后一次通行记录 -->
	<select id="findLastDayLastRecord"
			resultType="com.yida.data.common.core.entity.attendance.EduAttendanceStaffRecord">
		SELECT
			*
		FROM
			edu_attendance_staff_record
		WHERE
			TO_DAYS( #{lastDay} ) = TO_DAYS( create_time )
		AND staff_id = #{staffId}
		ORDER BY create_time DESC LIMIT 1
	</select>
	
	<!-- 查询指定时间段是否有通行记录 -->
	<select id="findTimeStaffRecord"
			resultType="com.yida.data.common.core.entity.attendance.EduAttendanceStaffRecord">
		SELECT
			*
		FROM
			edu_attendance_staff_record
		WHERE
			rule_id = #{ruleId}
		AND rule_time_id = #{ruleTimeId}
		<if test='startTime != null'>
			AND create_time &gt;= #{startTime}
		</if>
		<if test='endTime != null'>
			AND create_time &lt;= #{endTime}
		</if>
	</select>
	
</mapper>
