<?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.EduAttendanceResultMapper">
  <resultMap id="attendanceResultMap" type="com.yida.data.attendance.vo.AttendanceResultDormVO">
    <result property="studentId" column="student_id"/>
    <result property="ruleId" column="ruleId"/>
    <result property="ruleName" column="ruleName"/>
    <result property="dormRoomName" column="dorm_room_name"/>
    <result property="gradeClass" column="gradeClass"/>
    <result property="status" column="status"/>
    <result property="time" column="time"/>
    <result property="verify" column="verify"/>
    <result property="verifyTime" column="verify_time"/>
    <result property="verifyId" column="verify_id"/>
  </resultMap>
  <sql id="listAttendanceResultSql">
    select ear.student_id,ear.attendance_date,earu.id ruleId,earu.name
    ruleName,ear.dorm_room_name,concat(ear.grade_name,ear.class_name) gradeClass,
    if(earu.type=0,concat(DATE_FORMAT(eart.time1,'%H:%i'),'-',DATE_FORMAT(eart.time2,'%H:%i')),if(eart.type=1,'中午','晚上')) time,
    if(ear.verify_status=0,'未核查','已核查') verify,
    ear.verify_time,
    ear.verify_id,
    case ear.status
    when 100 then '正常'
    when 201 then '迟到'
    when 202 then '早退'
    when 203 then '迟到早退'
    when 204 then '未到'
    when 211 then '晚归'
    when 212 then '异常晚归'
    when 213 then '旷宿'
    end
    as status
    from edu_attendance_result ear
    left join edu_attendance_rule earu on ear.rule_id=earu.id
    left join edu_attendance_rule_time eart on ear.rule_time_id=eart.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="startDate != null">
        and attendance_date <![CDATA[>=]]> #{startDate}
      </if>
      <if test="endDate != null">
        and attendance_date <![CDATA[<=]]> #{endDate}
      </if>
    </where>
    order by ear.create_date desc
  </sql>

  <select id="countDormLate" resultType="com.yida.data.attendance.vo.StasticDormResultVO">
    select ifnull(sum(if(a.status in (211, 212), 1, 0)), 0) late,
    ifnull(sum(if(a.status = 213, 1, 0)), 0) notArrive
    from edu_attendance_result a
    left join edu_attendance_rule b on a.rule_id = b.id
    left join edu_attendance_rule_time c on a.rule_time_id = c.id
    <where>
      b.type = 1
      and c.type = #{time}
      and a.attendance_date = #{date}
      and a.dorm_id = #{dormId}
    </where>
  </select>

  <select id="countDormStay" resultType="java.lang.Integer">
    select ifnull(sum(if(a.scope = 0, 1, 0)), 0)
    from (
           select SUBSTRING_INDEX(GROUP_CONCAT(scope ORDER BY create_time desc), ',', 1) scope
           from edu_attendance_record
           where dorm_room_id = #{roomId}
             and #{date} = date_format(create_time, '%Y-%m-%d')
           group by student_id) a
  </select>

  <select id="listStudentResult"
    resultType="com.yida.data.common.core.entity.attendance.EduAttendanceResult">
    select a.*
    from edu_attendance_result a
    left join edu_attendance_rule b on a.rule_id = b.id
    left join edu_attendance_rule_time c on a.rule_time_id = c.id
    <where>
      a.status <![CDATA[!=]]> -1
      <if test="type != null">
        and b.type = #{type}
      </if>
      <if test="time != null">
        and c.type = #{time}
      </if>
      and a.attendance_date = #{date}
      <if test="roomIds != null and roomIds.size() != 0">
        and a.dorm_room_id in
        <foreach collection="roomIds" separator="," open="(" item="item" close=")">
          #{item}
        </foreach>
      </if>
      <if test="studentIds != null and studentIds.size() != 0">
        and a.student_id in
        <foreach collection="studentIds" separator="," open="(" item="item" close=")">
          #{item}
        </foreach>
      </if>
      <if test="schoolId != null">
        and a.school_id = #{schoolId}
      </if>
      <if test="campusId != null">
        and a.campus_id = #{campusId}
      </if>
      <if test="gradeId != null">
        and a.grade_id = #{gradeId}
      </if>
      <if test="classId != null">
        and a.class_id = #{classId}
      </if>
      <if test="timeId != null">
        and a.rule_time_id = #{timeId}
      </if>
      <if test="status != null">
        and a.status = #{status}
      </if>
    </where>
  </select>

  <select id="isVerify" resultType="java.lang.Integer">
    select ifnull(a.verify_status, 0)
    from edu_attendance_result a
    left join edu_attendance_rule b on a.rule_id = b.id
    left join edu_attendance_rule_time c on a.rule_time_id = c.id
    <where>
      a.status <![CDATA[!=]]> 214
      and b.type = 1
      and c.type = #{time}
      and a.attendance_date = #{date}
      and a.dorm_room_id = #{roomId}
    </where>
    limit 1
  </select>


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

  <select id="existResult" resultType="java.lang.Integer">
    select 1
    from edu_attendance_result
    where student_id = #{userId}
      and attendance_date = #{date}
      and rule_time_id = #{timeId}
    limit 1
  </select>

  <select id="findResult" resultType="com.yida.data.common.core.entity.attendance.EduAttendanceResult">
    select *
    from edu_attendance_result
    where student_id = #{userId}
      and attendance_date = #{date}
      and rule_time_id = #{timeId}
    limit 1
  </select>

  <select id="listPushNotArrive"
    resultType="com.yida.data.common.core.entity.attendance.EduAttendanceResult">
    select *
    from edu_attendance_result
    where school_id = #{schoolId}
      and status in (204, 213)
      and not_arrive_confirm = 0
      and last_push_time <![CDATA[<=]]> #{compareTime}
  </select>

  <select id="listGradeResultTime" resultType="com.yida.data.attendance.vo.RuleTimeVO">
    select distinct b.id timeId, concat(b.time1, '-', b.time2) timeName
    from edu_attendance_result a
           left join edu_attendance_rule_time b on a.rule_time_id = b.id
           left join edu_attendance_rule c on a.rule_id = c.id
    where a.grade_id = #{gradeId}
      and a.attendance_date = #{date}
      and c.type = 0
  </select>

  <select id="countStudentAttendance" resultType="com.yida.data.attendance.vo.CountVO">
    select ifnull(sum(if(status = 201, 1, 0)), 0) late,
    ifnull(sum(if(status = 202, 1, 0)), 0) `leave`,
    ifnull(sum(if(status = 204, 1, 0)), 0) notArrive,
    ifnull(sum(if(status in (211, 212), 1, 0)), 0) dormLate,
    ifnull(sum(if(status = 213, 1, 0)), 0) dormNotArrive,
    ifnull(sum(if(status = 205, 1, 0)), 0) request,
    ifnull(sum(if(status = 100, 1, 0)), 0) normal
    from edu_attendance_result
    <where>
      <if test="studentId != null">
        and student_id = #{studentId}
      </if>
      <if test="schoolIds != null and schoolIds.size() != 0">
        and school_id in
        <foreach collection="schoolIds" item="item" separator="," open="(" close=")">
          #{item}
        </foreach>
      </if>
      and attendance_date between #{startDate}
      and #{endDate}
    </where>
  </select>

  <select id="listStudentResultDetail" resultType="com.yida.data.attendance.vo.ResultDetailVO">
    select a.student_id,
    a.attendance_date,
    b.create_time recordTime,
    b.sti_time,
    c.name zoneName,
    if(b.zone = 0, concat(d.time1, '-', d.time2), if(d.type = 1, '中午', '晚上')) ruleTime
    from edu_attendance_result a
    left join edu_attendance_record b on a.record_id = b.id
    left join edu_attendance_device c on b.device_id = c.id
    left join edu_attendance_rule_time d on b.rule_time_id = d.id
    where a.student_id in
    <foreach collection="studentIds" item="item" separator="," open="(" close=")">
      #{item}
    </foreach>
    and a.attendance_date between #{dto.startDate} and #{dto.endDate}
    and a.status = #{dto.type}
    <if test="dto.timeId != null">
      and a.rule_time_id = #{dto.timeId}
    </if>
    order by a.attendance_date, b.sti_time, b.create_time
  </select>

  <select id="listResultStudent" resultType="com.yida.data.attendance.vo.ResultDetailVO">
    select student_id, status, attendance_date
    from edu_attendance_result
    <where>
      <if test="type != null">
        and status = #{type}
      </if>
      <if test="date != null and date != ''">
        and attendance_date = #{date}
      </if>
      <if test="month != null and month != ''">
        and DATE_FORMAT(attendance_date, '%Y-%m') = #{month}
      </if>
      and student_id in
      <foreach collection="studentIds" separator="," open="(" item="item" close=")">
        #{item}
      </foreach>
      <if test="timeId != null">
        and rule_time_id = #{timeId}
      </if>
    </where>
  </select>

  <select id="countRealStudentNum" resultType="Integer">
    select ifnull(count(distinct student_id), 0)
    from edu_attendance_result
    where attendance_date = #{date}
    and student_id in
    <foreach collection="studentIds" separator="," open="(" item="item" close=")">
      #{item}
    </foreach>
    and rule_time_id = #{timeId}
    and status in (100, 201, 202, 203)
  </select>

  <!-- 查询需要推送给管理员的记录 -->
  <select id="listManagePushNotArrive"
    resultType="com.yida.data.common.core.entity.attendance.EduAttendanceResult">
    select *
    from edu_attendance_result
    where school_id = #{schoolId}
      and status in (204, 213)
      and not_arrive_confirm = 0
      and last_push_time <![CDATA[<=]]> #{compareTime}
  </select>

</mapper>
