<?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.EduAttendanceRecordMapper">
  <sql id="listInOutSql">
    select ear.student_id studentId,ead.name zoneName,
    case ear.scope when 0 then '进' else '出' end as scope,
    ear.create_time,ear.pic_url,ear.dorm_room_name,concat(ear.grade_name,ear.class_name) gradeClass
    from edu_attendance_record ear
    left join edu_attendance_device ead on ear.device_id = ead.id
    <where>
      <if test="zone != null">
        and ear.zone=#{zone}
      </if>
      <if test="startTime != null">
        and create_time <![CDATA[>=]]>#{startTime}
      </if>
      <if test="endTime != null">
        and create_time <![CDATA[<=]]> #{endTime}
      </if>
      <choose>
        <when test="studentIds != null and studentIds.size() != 0">
          and ear.student_id in
          <foreach collection="studentIds" close=")" item="item" open="(" separator=",">
            #{item}
          </foreach>
        </when>
        <otherwise>
          and 0=1
        </otherwise>
      </choose>
    </where>
    order by ear.create_time desc
  </sql>

  <sql id="listAttendanceResultSql">
    select ear.student_id studentId,earu.id ruleId,earu.name ruleName,ear.create_time,ear.pic_url,
    case ear.status
    when 0 then '正常'
    when 1 then '迟到'
    when 2 then '早退'
    when 3 then '未到'
    when 4 then '晚归'
    when 5 then '异常晚归'
    when 6 then '旷宿'
    end
    as status
    from edu_attendance_record ear
    left join edu_attendance_rule earu on ear.rule_id=earu.id
    <where>
      <choose>
        <when test="studentIds != null and studentIds.size() != 0">
          and ear.student_id in
          <foreach collection="studentIds" close=")" item="item" open="(" separator=",">
            #{item}
          </foreach>
        </when>
        <otherwise>
          and 0=1
        </otherwise>
      </choose>
      <if test="ruleId != null">
        and ear.rule_id=#{ruleId}
      </if>
      <if test="date != null">
        and DATE_FORMAT(ear.create_time,'%Y-%m-%d')=#{date}
      </if>
    </where>
    order by ear.create_time desc
  </sql>

  <select id="listInOut" resultType="com.yida.data.attendance.vo.InOutDormPageVO">
    <include refid="listInOutSql">
    </include>
  </select>

  <select id="listAttendanceResultPage" resultType="com.yida.data.attendance.vo.AttendanceResultVO">
    <include refid="listAttendanceResultSql">
    </include>
  </select>

  <select id="listAttendanceResult" resultType="com.yida.data.attendance.vo.AttendanceResultVO">
    <include refid="listAttendanceResultSql">
    </include>
  </select>

  <select id="listRecordDetail" resultType="com.yida.data.attendance.vo.RecordDetailVO">
    select ear.id,
           ear.scope,
           ear.zone,
           ear.pic_url                        picUrl,
           ear.create_time                    createTime,
           ear.status,
           time_format(ear.sti_time, '%H:%i') stiTime,
           ead.name                           deviceName
    from edu_attendance_record ear
           left join edu_attendance_device ead on ear.device_id = ead.id
    where ear.student_id = #{userId}
      and DATE_FORMAT(ear.create_time, '%Y-%m-%d') = #{date}
    order by ear.create_time desc
  </select>

  <select id="getRecordDetail" resultType="com.yida.data.attendance.vo.RecordDetailVO">
    select a.create_time,
           a.scope,
           a.status,
           concat(a.grade_name, a.class_name) className,
           a.pic_url
    from edu_attendance_record a
    where a.id = #{id}
  </select>

  <select id="getLastRecordByTime"
    resultType="com.yida.data.common.core.entity.attendance.EduAttendanceRecord">
    select *
    from edu_attendance_record
    <where>
      student_id = #{studentId}
      <if test="type != null">
        and scope = #{type}
      </if>
      <if test="startTime != null">
        and create_time <![CDATA[>=]]> #{startTime}
      </if>
      <if test="endTime != null">
        and create_time <![CDATA[<=]]> #{endTime}
      </if>
    </where>
    order by create_time desc
    limit 1
  </select>

  <select id="countLateAndLeave" resultType="com.yida.data.attendance.vo.CountVO">
    select ifnull(sum(if(status = 0, 1, 0)), 0) as late, ifnull(sum(if(status = 1, 1, 0)), 0) as `leave`
    from edu_attendance_record
    <where>
      student_id = #{userId}
      and create_time between #{startTime} and #{endTime}
    </where>
  </select>

  <select id="listRecord" resultType="com.yida.data.attendance.vo.RecordDetailVO">
    select a.*, b.name deviceName
    from edu_attendance_record a
    left join edu_attendance_device b on a.device_id = b.id
    <where>
      <if test="dto.studentId != null">
        and a.student_id = #{dto.studentId}
      </if>
      <if test="dto.createDate != null">
        and date_format(a.create_time, '%Y-%m-%d') = #{dto.createDate}
      </if>
      <if test="dto.ruleTimeId != null">
        and a.rule_time_id = #{dto.ruleTimeId}
      </if>
      <if test="dto.status != null">
        and a.status = #{dto.status}
      </if>
      <if test="dto.zone != null">
        and a.zone = #{dto.zone}
      </if>
    </where>
    order by a.create_time desc
  </select>

  <select id="stasticTeacherData" resultType="java.lang.Integer">
    SELECT count(*)
    FROM edu_attendance_record
    WHERE DATE_FORMAT(create_time, '%Y-%m-%d') = DATE_FORMAT('2022-09-06 22:42:10', '%Y-%m-%d')
      AND create_time <![CDATA[<=
    ]]>
  </select>


  <select id="getFirstByStudentIdAndRuleTimeId"
    resultType="com.yida.data.common.core.entity.attendance.EduAttendanceRecord">
    select *
    from edu_attendance_record
    <where>
      student_id = #{studentId} and rule_time_id = #{ruleTimeId}
      and DATE_FORMAT(create_time,'%Y-%m-%d') = #{date}
      and device_id in
      <foreach collection="deviceIdList" close=")" item="item" open="(" separator=",">
        #{item}
      </foreach>
    </where>
    order by create_time
    limit 1
  </select>


  <select id="getLastByStudentIdAndRuleTimeId"
    resultType="com.yida.data.common.core.entity.attendance.EduAttendanceRecord">
    select *
    from edu_attendance_record
    <where>
      student_id = #{studentId} and rule_time_id = #{ruleTimeId}
      and DATE_FORMAT(create_time,'%Y-%m-%d')=#{date}
      and device_id in
      <foreach collection="deviceIdList" close=")" item="item" open="(" separator=",">
        #{item}
      </foreach>
    </where>
    order by create_time Desc
    limit 1
  </select>

  <select id="getDormLastByStudentIdAndRuleTimeId"
    resultType="com.yida.data.common.core.entity.attendance.EduAttendanceRecord">
    select *
    from edu_attendance_record
    <where>
      dorm_id is not null
      and student_id = #{studentId}
      and DATE_FORMAT(create_time,'%Y-%m-%d')=#{date}
    </where>
    order by create_time Desc
    limit 1
  </select>

  <select id="getDormFirstByStudentIdAndRuleTimeId"
    resultType="com.yida.data.common.core.entity.attendance.EduAttendanceRecord">
    select *
    from edu_attendance_record
    <where>
      dorm_id is not null
      and student_id = #{studentId}
      and DATE_FORMAT(create_time,'%Y-%m-%d')=#{date}
    </where>
    order by create_time
    limit 1
  </select>


  <select id="getRecordByStudentIdAndMonth"
    resultType="com.yida.data.common.core.entity.attendance.EduAttendanceRecord">
    select distinct student_id,DATE_FORMAT(create_time,'%Y-%m-%d') create_time
    from edu_attendance_record
    <where>
      student_id = #{studentId}
      and DATE_FORMAT(create_time,'%Y-%m')= #{month}
      and status = #{status}
    </where>
  </select>


</mapper>