<?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.EduAttendanceStaffResultMapper">
  <resultMap id="dateTimeResultMap" type="com.yida.data.attendance.vo.StaffDateTimeResultInfo">
    <result column="date" property="date"/>
    <collection property="statusList" ofType="java.lang.Integer">
      <result column="status" property="value"/>
    </collection>
  </resultMap>

  <sql id="listStaffResultStasticDaySql">
    select a.staff_id,
    a.attendance_date date,
    d.name ruleName,
    ifnull(sum(if(status in (0), 1, 0)), 0) normalNum,
    ifnull(sum(if(status in (1, 4), 1, 0)), 0) lateNum,
    ifnull(sum(if(status in (2, 4), 1, 0)), 0) leaveNum,
    ifnull(sum(if(status = 3, 1, 0)), 0) notArriveNum
    from edu_attendance_staff_result a
    left join edu_attendance_staff_rule_staff b on a.staff_id=b.staff_id
    left join edu_attendance_staff_rule d on b.rule_id=d.id
    where a.staff_id in
    (select staff_id
    from edu_attendance_staff_result
    where staff_id in
    <foreach collection="staffIds" separator="," open="(" item="item" close=")">
      #{item}
    </foreach>
    <if test="dto.date != null">
      and attendance_date=#{dto.date}
    </if>
    <if test="dto.status != null">
      and status = #{dto.status}
    </if>)
    <if test="dto.date != null">
      and attendance_date=#{dto.date}
    </if>
    group by a.staff_id,a.attendance_date,d.name
  </sql>

  <sql id="listStaffResultStasticMonthSql">
    select staff_id,name ruleName,
    sum(if(normal>0,1,0)) normalNum,
    sum(if(notArrive>0,1,0)) notArriveNum,
    sum(if(`leave` >0,1,0)) leaveNum,
    sum(if(late>0,1,0)) lateNum
    from (
    select a.staff_id,
    a.attendance_date date,
    b.name,
    sum(if(status in (0),1,0)) normal,
    sum(if(status in (1,4),1,0)) late,
    sum(if(status in (2,4),1,0)) `leave`,
    sum(if(status=3,1,0)) notArrive
    from edu_attendance_staff_result a
    left join edu_attendance_staff_rule_staff e on a.staff_id=e.staff_id
    left join edu_attendance_staff_rule b on e.rule_id =b.id
    where a.staff_id in
    (select staff_id
    from edu_attendance_staff_result
    where staff_id in
    <foreach collection="staffIds" separator="," open="(" item="item" close=")">
      #{item}
    </foreach>
    <if test="dto.yearMonth != null">
      and date_format(attendance_date,'%Y-%m')=#{dto.yearMonth}
    </if>
    <if test="dto.status != null">
      and status = #{dto.status}
    </if>)
    <if test="dto.yearMonth != null">
      and date_format(attendance_date,'%Y-%m')=#{dto.yearMonth}
    </if>
    group by a.staff_id,a.attendance_date,b.name)d group by staff_id,name
  </sql>
  <select id="listStaffResultStasticDayPage" resultType="com.yida.data.attendance.vo.StasticStaffResultByDayVO">
    <include refid="listStaffResultStasticDaySql">
    </include>
  </select>

  <select id="listStaffResultStasticDay" resultType="com.yida.data.attendance.vo.StasticStaffResultByDayVO">
    <include refid="listStaffResultStasticDaySql">
    </include>
  </select>

  <select id="listStaffResultStasticMonthPage" resultType="com.yida.data.attendance.vo.StasticStaffResultByMonthVO">
    <include refid="listStaffResultStasticMonthSql">
    </include>
  </select>

  <select id="listStaffResultStasticMonth" resultType="com.yida.data.attendance.vo.StasticStaffResultByMonthVO">
    <include refid="listStaffResultStasticMonthSql">
    </include>
  </select>

  <select id="listTimeResult" resultType="com.yida.data.attendance.vo.StaffTimeResultInfo">
    select start_sti_time, start_real_time, end_sti_time, end_real_time, status
    from edu_attendance_staff_result
      where staff_id = #{staffId}
    <if test="date != null and date != ''">
      and attendance_date = #{date}
    </if>
    order by start_sti_time
  </select>

  <select id="listDateTimeResult" resultMap="dateTimeResultMap">
    select attendance_date date, status
    from edu_attendance_staff_result
    where staff_id = #{staffId}
      and date_format(attendance_date, '%Y-%m') = #{yearMonth}
    order by date, start_sti_time
  </select>

  <select id="countRangeExcepStaff" resultType="com.yida.data.attendance.vo.CountVO">
    select ifnull(sum(if(normal = 0, 1, 0)), 0)    normal,
           ifnull(sum(if(excep > 0, 1, 0)), 0)     excep,
           ifnull(sum(if(late > 0, 1, 0)), 0)      late,
           ifnull(sum(if(`leave` > 0, 1, 0)), 0)   `leave`,
           ifnull(sum(if(notArrive > 0, 1, 0)), 0) notArrive
      from (
      select sum(if(status = 0, 0, 1))       normal,
             sum(if(status != 0, 1, 0))      excep,
             sum(if(status in (1, 4), 1, 0)) late,
             sum(if(status in (2, 4), 1, 0)) `leave`,
             sum(if(status = 3, 1, 0))       notArrive
      from edu_attendance_staff_result a
             left join edu_attendance_staff_rule b on a.rule_id = b.id
             left join edu_attendance_staff_rule_admin c on b.id = c.rule_id
      where attendance_date between #{dto.startDate} and #{dto.endDate}
    <if test="dto.staffId != null">
      and c.staff_id = #{dto.staffId}
    </if>
    <if test="dto.schoolIds != null and dto.schoolIds.size() > 0">
      and a.school_id in
      <foreach collection="dto.schoolIds" item="item" separator="," open="(" close=")">
        #{item}
      </foreach>
    </if>
    group by a.staff_id) d
  </select>

  <select id="listResultPage" resultType="com.yida.data.attendance.vo.ListStaffResultPageVO">
    select a.staff_id, a.status, b.name ruleName, a.attendance_date date
    from edu_attendance_staff_result a
           left join edu_attendance_staff_rule b on a.rule_id = b.id
           left join edu_attendance_staff_rule_admin c on b.id = c.rule_id
    <where>
      <if test="dto.adminStaffId != null">
        and c.staff_id = #{dto.adminStaffId}
      </if>
      <if test="dto.startDate != null and dto.endDate != null">
        and a.attendance_date between #{dto.startDate} and #{dto.endDate}
      </if>
      <if test="dto.status != null">
        and a.status = #{dto.status}
      </if>
    </where>
    order by a.id desc
  </select>
  <select id="findNotResult"
    resultType="com.yida.data.common.core.entity.attendance.EduAttendanceStaffResult">
    SELECT *
    FROM `edu_attendance_staff_result`
    WHERE staff_id = #{staffId}
      AND `status` is NULL
    ORDER BY attendance_date, start_sti_time
  </select>
</mapper>
