<?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.template.mapper.SmartAttendanceMapper">

    <select id="getGroup" resultType="java.lang.Integer">
        SELECT `status`
        FROM `smart_attendance`
        GROUP BY `status`
    </select>

    <select id="askForLeavePage" resultType="com.template.model.vo.AskForLeaveVo">
        SELECT
        sa.id,
        su.head_image as image,
        sa.xw_user_name as xwUserName,
        sc.name as className,
        su.card_no as cardNo,
        sa.start_time as startTime,
        sa.end_time as endTime,
        sa.reason,
        sa.if_verification as ifVerification,
        sa.initiate_time as initiateTime
        FROM
        `smart_attendance` sa
        LEFT JOIN smart_user su ON su.id = sa.user_id
        LEFT JOIN smart_class sc ON sc.id = sa.class_id
        WHERE
        sa.`status` = 6
        AND sa.deleted = 0
        <if test="name != null and name != ''">
            and sa.xw_user_name like '%' #{name} '%'
        </if>
        <if test="cardNo != null and cardNo != ''">
            and su.card_no = #{cardNo}
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and sa.initiate_time &gt;= #{startTime} and sa.initiate_time &lt;= #{endTime}
        </if>
        <if test="ifVerification != null and ifVerification != ''">
            and sa.if_verification = #{ifVerification}
        </if>
        <if test="classId != null and classId != ''">
            and sa.class_id = #{classId}
        </if>
        ORDER BY sa.initiate_time desc
    </select>

    <select id="queryPage" resultType="com.template.model.vo.SmartAttendanceVo">
        SELECT
        sa.id,
        sa.xw_user_name as name,
        sc.`name` as className,
        sg.`name` as gradeName,
        su.card_no as cardNo,
        su.head_image as headImage,
        sa.attend_time as attendTime,
        sa.initiate_time as initiateTime,
        sa.`status`
        FROM
        `smart_attendance` sa
        LEFT JOIN smart_class sc on sa.class_id=sc.id
        LEFT JOIN smart_grade sg on sc.grade_id=sg.id
        LEFT JOIN smart_user su on sa.user_id=su.id
        where sa.deleted = 0
        <if test="gradeId != null and gradeId != ''">
            and sc.grade_id=#{gradeId}
        </if>
        <if test="classId != null and classId != ''">
            and sa.class_id = #{classId}
        </if>
        <if test="status != null and status != ''">
            and sa.`status` = #{status}
        </if>
        <if test="studentNo != null and studentNo != ''">
            and su.card_no = #{studentNo}
        </if>
        <if test="name != null and name != ''">
            and sa.xw_user_name like '%' #{name} '%'
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and ((sa.attend_time &gt;= #{startTime} and sa.attend_time &lt;= #{endTime}) or (sa.initiate_time &gt;= #{startTime} and sa.initiate_time &lt;= #{endTime}))
        </if>
        ORDER BY sa.create_time desc
    </select>

    <select id="queryExpor" resultType="com.template.model.vo.SmartAttendanceVo">
        SELECT
        sa.id,
        sa.xw_user_name as name,
        sc.`name` as className,
        sg.`name` as gradeName,
        su.card_no as cardNo,
        su.head_image as headImage,
        sa.attend_time as attendTime,
        sa.initiate_time as initiateTime,
        sa.`status`
        FROM
        `smart_attendance` sa
        LEFT JOIN smart_class sc on sa.class_id=sc.id
        LEFT JOIN smart_grade sg on sc.grade_id=sg.id
        LEFT JOIN smart_user su on sa.user_id=su.id
        where sa.deleted = 0
        <if test="gradeId != null and gradeId != ''">
            and sc.grade_id=#{gradeId}
        </if>
        <if test="classId != null and classId != ''">
            and sa.class_id = #{classId}
        </if>
        <if test="status != null and status != ''">
            and sa.`status` = #{status}
        </if>
        <if test="studentNo != null and studentNo != ''">
            and su.card_no = #{studentNo}
        </if>
        <if test="name != null and name != ''">
            and sa.xw_user_name like '%' #{name} '%'
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and ((sa.attend_time &gt;= #{startTime} and sa.attend_time &lt;= #{endTime}) or (sa.initiate_time &gt;= #{startTime} and sa.initiate_time &lt;= #{endTime}))
        </if>
        ORDER BY sa.create_time desc
    </select>
    <select id="askForLeaveScreenPage" resultType="com.template.model.vo.AskForLeaveScreenVo">
        SELECT
        sa.id,
        su.head_image as headImage,
        su.`name` as name,
        sc.`name` as className,
        su.card_no as cardNo,
        sa.start_time as startTime,
        sa.end_time as endTime,
        sa.reason,
        sa.if_verification as ifVerification
        FROM
        `smart_attendance` sa
        LEFT JOIN smart_user su ON sa.user_id = su.id
        LEFT JOIN smart_class sc ON sa.class_id = sc.id
        WHERE
        sa.deleted = 0
        AND sa.`status` =6
        <if test="cardNo != null and cardNo != ''">
            and su.card_no like '%' #{cardNo} '%'
        </if>
        <if test="ifVerification != null and ifVerification != ''">
            and sa.if_verification = #{ifVerification}
        </if>
        ORDER BY sa.initiate_time desc
    </select>
    <select id="getClassHistoricalAttendance" resultType="com.template.model.vo.ClasAttendanceVo">
        SELECT COUNT(*) AS count,
	        `status`
        FROM
            `smart_attendance`
        WHERE
            deleted=0
          AND class_id = #{id}
          AND attend_time >= #{startTime}
          AND #{endTime} >= attend_time
        GROUP BY
            `status`
    </select>
    <select id="ackDetail" resultType="com.template.model.vo.AckDetailVo">
        SELECT sat.xw_user_name  as name,
               su.card_no        as cardNo,
               sat.initiate_time as initiateTime,
               sat.start_time    as startTime,
               sat.end_time      as endTime,
               sat.reason        as reason
        FROM `smart_attendance` sat
                 left join smart_user su
                           on sat.user_id = su.id
        where sat.`status` = 6
          AND sat.deleted = 0
          AND sat.user_id = #{userId}
          AND sat.initiate_time >= #{startTime}
          AND #{endTime} >= sat.initiate_time
    </select>
</mapper>
