<?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.school.leave.mapper.EduLeaveRequestMapper">
  <select id="findRequestPage" resultType="com.yida.data.common.core.entity.leave.EduLeaveRequest">
    SELECT distinct *
    FROM `edu_leave_request`
    WHERE del_flag = 0
    <if test="studentIds != null and studentIds.size > 0">
      AND student_id IN
      <foreach collection="studentIds" separator="," close=")" open="(" item="item">
        #{item}
      </foreach>
    </if>
    <if test="status != null and status != 4 and status != 3 and status != 0">
      AND `status` = #{status}
    </if>
    <if test="status == 0">
      AND `status` = #{status}
      and start_time > #{now}
    </if>
    and teacher_id = #{teacherId}
    <if test="status == 4">
      AND `status` != 0
    </if>
    <if test="status == 3">
      AND `status` = 0
      and start_time &lt; #{now}
    </if>
    <if test="studentName != null and studentName != ''">
      AND student_name LIKE CONCAT('%', #{studentName}, '%')
    </if>
    ORDER BY create_date desc
  </select>
  <select id="findPageLeaveList" resultType="com.yida.data.school.vo.leave.FindPageLeaveListVO">
    SELECT *
    FROM `edu_leave_request`
    WHERE del_flag = 0
    and school_id = #{schoolId}
    <if test="gradeId != null">
      AND grade_id = #{gradeId}
    </if>
    <if test="sectionId != null">
      AND section_id = #{sectionId}
    </if>
    <if test="status ==0">
      AND `status` = #{status}
    </if>
    <if test="teacherName != null and teacherName != ''">
      and teacher_name like concat('%',#{teacherName},'%')
    </if>
    <if test="status == 1">
      AND `status` != 0
      and status != 3
    </if>
    <if test="reviewStatus != null and reviewStatus!=0">
      AND `status` = #{reviewStatus}
    </if>
    <if test="studentIds != null and studentIds.size>0">
    and student_id in
      <foreach collection="studentIds" separator="," close=")" open="(" item="item">
        #{item}
      </foreach>
    </if>
    ORDER BY create_date desc
  </select>
  <select id="findpage" resultType="com.yida.data.common.core.entity.leave.EduLeaveRequest">
    SELECT *
    FROM edu_leave_request
    WHERE del_flag = 0
    <if test="studentIds != null and studentIds.size > 0">
      and student_id IN
      <foreach collection="studentIds" separator="," close=")" open="(" item="item">
        #{item}
      </foreach>
    </if>
    <if test="studentId != null">
      and student_id = #{studentId}
    </if>
    <if test="status != null and status != 3 and status != 0">
      AND `status` = #{status}
    </if>
    <if test="status == 0">
      AND `status` = #{status}
      and start_time > #{now}
    </if>
    <if test="status == 3">
      and start_time &lt; #{now}
      and `status` = 0
    </if>
    ORDER BY create_date DESC
  </select>
  <select id="findLastRequest" resultType="com.yida.data.common.core.entity.leave.EduLeaveRequest">
    SELECT *
    FROM `edu_leave_request`
    WHERE student_id = #{studentId}
    AND id NOT IN (
    SELECT id
    FROM edu_leave_request
    WHERE `status` = 0
    AND NOW() > start_time
    AND student_id = #{studentId})
  </select>

  <select id="listStudentRequest" resultType="com.yida.data.school.vo.leave.LeaveRequestDetailVO">
    select student_id, start_time, end_time, parent_name
    from edu_leave_request
    where del_flag = 0
    and status=1
    and student_id in
    <foreach collection="studentIds" item="item" separator="," open="(" close=")">
      #{item}
    </foreach>
    and ((start_time between #{dto.startTime} and #{dto.endTime})
    or (end_time between #{dto.startTime} and #{dto.endTime})
    or (start_time <![CDATA[<]]> #{dto.startTime} and end_time <![CDATA[>]]> #{dto.endTime})
    )
    order by start_time
  </select>

  <select id="existRequest" resultType="java.lang.Integer">
    select 1
    from edu_leave_request
    where student_id=#{dto.studentId}
    and status=1
    and del_flag=0
    and ((start_time between #{dto.startTime} and #{dto.endTime})
    or (end_time between #{dto.startTime} and #{dto.endTime})
    or (start_time <![CDATA[<]]> #{dto.startTime} and end_time <![CDATA[>]]> #{dto.endTime})
    )
  </select>
  <select id="findLeaveRequest" resultType="com.yida.data.school.vo.leave.FindPageLeaveListVO">
    select * from edu_leave_request where del_flag = 0
    and id = #{id}
  </select>
</mapper>
