<?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.userLeave.mapper.EduLeaveRequestUserMapper">

  <select id="findPage" resultType="com.yida.data.common.core.entity.userLeave.EduLeaveRequestUser">
    SELECT *
    FROM edu_leave_request_user
    WHERE del_flag = 0
    <if test="dto.idList != null and dto.idList.size()>0">
      and id in
      <foreach collection="dto.idList" separator="," close=")" open="(" item="item">
        #{item}
      </foreach>
    </if>
    <if test="dto.statusList != null and dto.statusList.size()>0">
      and status in
      <foreach collection="dto.statusList" separator="," close=")" open="(" item="item">
        #{item}
      </foreach>
    </if>
    <if test="dto.studentIdList != null and dto.studentIdList.size()>0">
      and user_id in
      <foreach collection="dto.studentIdList" separator="," close=")" open="(" item="item">
        #{item}
      </foreach>
    </if>
    <if test="dto.requestType != null">
      and request_type = #{dto.requestType}
    </if>
    <if test="dto.schoolId != null">
      and school_id = #{dto.schoolId}
    </if>
    <if test="dto.requestUserName != null">
      and user_name LIKE CONCAT('%',#{dto.requestUserName},'%')
    </if>
    <if test="dto.startDateTime!=null">
      and start_time  <![CDATA[>=]]> #{dto.startDateTime}
    </if>
    <if test="dto.endDateTime!=null">
      and start_time  <![CDATA[<=]]> #{dto.endDateTime}
    </if>
    ORDER BY create_date DESC
  </select>

  <select id="findApprovalUserLeaveList" resultType="com.yida.data.common.core.entity.userLeave.EduLeaveRequestUser">
    SELECT DISTINCT er.*,err.approval_result currentUserStatus FROM edu_leave_request_user er LEFT JOIN
    edu_leave_request_process_record
    err on er.id = err.request_id
    WHERE er.del_flag = 0
    <if test="statusList != null and statusList.size()>0">
      and er.status in
      <foreach collection="statusList" separator="," close=")" open="(" item="item">
        #{item}
      </foreach>
    </if>
    <if test="userId != null">
      and err.approval_id = #{userId}
    </if>
    <if test="currentUserStatusList != null and currentUserStatusList.size()>0">
      and err.approval_result in
      <foreach collection="currentUserStatusList" separator="," close=")" open="(" item="item">
        #{item}
      </foreach>
    </if>
    <if test="requestType != null">
      and er.request_type = #{requestType}
    </if>
    <if test="schoolId != null">
      and er.school_id = #{schoolId}
    </if>
    and er.create_date <![CDATA[>=]]> #{createDate}
    ORDER BY er.create_date DESC
  </select>

  <select id="findUserRequestList" resultType="com.yida.data.school.vo.userLeave.EduLeaveRequestUserVO">
    select *
    from edu_leave_request_user
    where status = 1
    and del_flag = 0
    <if test="dto.userIdList != null and dto.userIdList.size()>0">
      and user_id in
      <foreach collection="dto.userIdList" separator="," close=")" open="(" item="item">
        #{item}
      </foreach>
    </if>
    and request_type = #{dto.requestType}
    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="selectWeekNumber" resultType="long">
    SELECT count(id)
    FROM edu_leave_request_user
    WHERE del_flag = 0
    AND YEARWEEK(start_time, 1) = YEARWEEK(CURDATE(), 1)
    AND user_id in
    <foreach collection="studentIdList" separator="," close=")" open="(" item="id">
      #{id}
    </foreach>
  </select>

  <!-- 查询月统计 -->
  <select id="selectMonthNumber" resultType="long">
    SELECT count(id)
    FROM edu_leave_request_user
    WHERE del_flag = 0
    AND DATE_FORMAT(start_time, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%m')
    AND user_id in
    <foreach collection="studentIdList" separator="," close=")" open="(" item="id">
      #{id}
    </foreach>
  </select>

</mapper>
