<?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.AttendanceResultMapper">

  <resultMap id="baseMap" type="com.yida.data.attendance.vo.AttdanceResultVO">
    <id column="id" property="id"/>
    <result column="name" property="name"/>
    <result column="number" property="number"/>
    <result column="deptName" property="deptName"/>
    <result column="user_id" property="userId"/>
    <result column="rule_id" property="ruleId"/>
    <result column="rule_time_id" property="ruleTimeId"/>
    <result column="rule_time_name" property="ruleTimeName"/>
    <result column="rule_name" property="ruleName"/>
    <result column="needDays" property="needDays"/>
    <result column="realDays" property="realDays"/>
    <result column="normalDays" property="normalDays"/>
    <result column="abnormalDays" property="abnormalDays"/>
    <result column="abnormalNumber" property="abnormalNumber"/>
    <result column="lateNumber" property="lateNumber"/>
    <result column="lateTime" property="lateTime"/>
    <result column="leaveNumber" property="leaveNumber"/>
    <result column="leaveTime" property="leaveTime"/>
    <result column="outNumber" property="outNumber"/>
    <result column="missNumber" property="missNumber"/>
  </resultMap>

  <select id="listResultPage" resultMap="baseMap">
    SELECT
    re.user_id,
    re.rule_id,
    re.rule_time_id,
    CONCAT(re.start_sti_time,'-',re.end_sti_time) rule_time_name,
    ru.`name` rule_name,
    count(re.id) needDays,
    SUM(IF(ISNULL(re.start_real_time) AND ISNULL(re.end_real_time) ,0,1)) realDays,
    SUM(if(att_type=0,if(re.start_status=1 or re.end_status=1,1,0),if(re.start_status=1,1,0))) abnormalDays,
    SUM(if(att_type=0,if(re.start_status=0 and re.end_status=0,1,0),if(re.start_status=0,1,0))) normalDays,
    SUM(if(re.start_status=1,1,0)) lateNumber,
    SUM(if(re.end_status=1,1,0)) leaveNumber,
    SUM(if(ISNULL(re.start_real_time) AND ISNULL(re.end_real_time),1,0)) outNumber,
    SUM(if(att_type=0,if(ISNULL(re.start_real_time) or ISNULL(re.end_real_time),1,0),if(ISNULL(re.start_real_time),1,0)))
    missNumber,
    SUM(if(ISNULL(re.start_late_time),0,re.start_late_time)) lateTime,
    SUM(if(ISNULL(re.end_leave_time),0,re.end_leave_time)) leaveTime
    FROM attendance_result re
    LEFT JOIN attendance_rule ru ON re.rule_id = ru.id
    <where>
      <if test="dto.schoolId != null">
        and re.school_id=#{dto.schoolId}
      </if>
      <if test="dto.startDate != null">
        and re.attendance_date <![CDATA[>=]]>  #{dto.startDate}
      </if>
      <if test="dto.endDate != null">
        and re.attendance_date <![CDATA[<=]]>  #{dto.endDate}
      </if>
      <if test="dto.userIdList != null and dto.userIdList.size() != 0">
        and re.user_id in
        <foreach collection="dto.userIdList" item="item" separator="," close=")" open="(">
          #{item}
        </foreach>
      </if>
      <if test="dto.ruleId != null">
        and re.rule_id = #{dto.ruleId}
      </if>
    </where>
    GROUP BY re.user_id,re.rule_id,re.rule_time_id ,re.start_sti_time,re.end_sti_time
  </select>

  <select id="findAttandanceTotal" resultType="com.yida.data.attendance.vo.AttdanceResultDetailVO">
    SELECT re.id,re.start_status startStatus,re.end_status endStatus FROM attendance_result re
    <where>
      <if test="dto.schoolId != null">
        and re.school_id=#{dto.schoolId}
      </if>
      <if test="dto.startDate != null">
        and re.attendance_date <![CDATA[>=]]>  #{dto.startDate}
      </if>
      <if test="dto.endDate != null">
        and re.attendance_date <![CDATA[<=]]>  #{dto.endDate}
      </if>
      <if test="dto.userIdList != null and dto.userIdList.size() != 0">
        and re.user_id in
        <foreach collection="dto.userIdList" item="item" separator="," close=")" open="(">
          #{item}
        </foreach>
      </if>
    </where>
  </select>


  <select id="listResultDetailPage" resultType="com.yida.data.attendance.vo.AttdanceResultDetailVO">
    select
    user_id userId,rule_id ruleId,rule_time_id ruleTimeId,start_sti_time startStiTime,
    start_status startStatus,start_real_time startRealTime,end_sti_time endStiTime,
    end_status endStatus,end_real_time endRealTime,attendance_date attendanceDate
    from attendance_result
    <where>
      <if test="dto.userIdList != null and dto.userIdList.size() != 0">
        and user_id in
        <foreach collection="dto.userIdList" item="item" separator="," close=")" open="(">
          #{item}
        </foreach>
      </if>
      <if test="dto.startDate != null">
        and attendance_date <![CDATA[>=]]>  #{dto.startDate}
      </if>
      <if test="dto.endDate != null">
        and attendance_date <![CDATA[<=]]>  #{dto.endDate}
      </if>
      <if test="dto.ruleId != null">
        and rule_id = #{dto.ruleId}
      </if>
    </where>
    GROUP BY
    user_id,rule_id,rule_time_id,start_sti_time,start_status,start_real_time,end_sti_time,end_status,end_real_time,attendance_date
  </select>

  <select id="listUserPage" resultType="com.yida.data.attendance.vo.AttdanceResultDetailVO">
    SELECT user_id userId FROM attendance_result
    where
    user_id in
    <foreach collection="dto.userIdList" item="item" separator="," close=")" open="(">
      #{item}
    </foreach>
    <if test="dto.startDate != null">
      and attendance_date <![CDATA[>=]]>  #{dto.startDate}
    </if>
    <if test="dto.endDate != null">
      and attendance_date <![CDATA[<=]]>  #{dto.endDate}
    </if>
    <if test="dto.ruleId != null">
      and rule_id = #{dto.ruleId}
    </if>
    GROUP BY user_id
    ORDER BY user_id
  </select>
  <select id="findAttendanceStatus" resultType="com.yida.data.attendance.vo.AttendanceStatusByMonthVO">
    SELECT ca.date,
    re.rule_id ruleId,
    re.rule_time_id ruleTimeId,
    re.start_status startStatus,
    re.end_status endStatus,
    re.att_type attType
    FROM core_calendar ca
    LEFT JOIN (SELECT attendance_date,
    att_type,
    rule_id,
    rule_time_id,
    SUM(if(start_status = 0, 0, 1)) start_status,
    SUM(if(end_status = 0, 0, 1)) end_status
    FROM attendance_result
    WHERE attendance_date <![CDATA[>=]]> #{startDate}
    AND attendance_date <![CDATA[<=]]> #{endDate}
    AND user_id in
    <foreach collection="userIdList" item="item" separator="," close=")" open="(">
      #{item}
    </foreach>
    AND user_type = #{userType}
    GROUP BY attendance_date, att_type,rule_id,rule_time_id) re ON re.attendance_date = ca.date
    WHERE ca.DATE <![CDATA[>=]]> #{startDate}
    AND ca.DATE <![CDATA[<=]]> #{endDate}
    ORDER BY ca.date
  </select>

  <select id="findManageListUserDetailVO" resultType="com.yida.data.attendance.vo.ManageListUserAttendanceDetailVO">
    SELECT
    temp.userId,
    temp.ruleId,
    sum( temp.lateNum ) lateNum,
    sum( temp.lateMinute ) lateMinute,
    sum( temp.leaveNum ) leaveNum,
    sum( temp.leaveMinute ) leaveMinute,
    sum( temp.lateDormNum ) lateDormNum,
    sum( temp.lateDormMinute ) lateDormMinute,
    sum( temp.abLateDormMinute ) abLateDormMinute,
    sum( temp.abLateDormNum ) abLateDormNum,
    sum( temp.missNum ) missNum,
    GROUP_CONCAT( ru.`name` ) ruleName
    FROM(
    SELECT user_id userId,
    rule_id ruleId,
    sum(IF(res.start_status = 1, 1, 0)) lateNum,
    sum(res.start_late_time) lateMinute,
    sum(if(res.att_type=0,IF(res.end_status = 1, 1, 0),0)) leaveNum,
    sum(if(res.att_type=0,res.end_leave_time,0)) leaveMinute,
    sum(if(res.att_type=1,IF(res.start_status = 7, 1, 0),0)) lateDormNum,
    sum(if(res.att_type=1,IF(res.start_status = 8, 1, 0),0)) abLateDormNum,
    sum(if(res.att_type=1,res.start_late_time,0)) lateDormMinute,
    sum(if(res.att_type=1,res.start_late_time,0)) abLateDormMinute,
    sum(if(res.att_type=0,IF(res.start_status IS null or res.end_status IS NULL, 1, 0),IF(res.start_status IS NULL, 1, 0)))
    missNum
    FROM attendance_result res
    WHERE 1 = 1
    <if test="dto.ruleId!=null">
      and res.rule_id = #{dto.ruleId}
    </if>
    <if test="dto.searchUserType!=null">
      and res.user_type = #{dto.searchUserType}
    </if>

    <if test="dto.userIdList!=null and dto.userIdList.size()>0">
      AND res.user_id IN
      <foreach collection="dto.userIdList" item="item" separator="," close=")" open="(">
        #{item}
      </foreach>
    </if>
    <if test="dto.attendanceStartDate !=null">
      and res.attendance_date <![CDATA[>=]]> #{dto.attendanceStartDate}
    </if>
    <if test="dto.attendanceEndDate !=null">
      and res.attendance_date <![CDATA[<=]]> #{dto.attendanceEndDate}
    </if>
    GROUP BY res.user_id,
    res.rule_id
    ) temp
    LEFT JOIN attendance_rule ru ON temp.ruleId = ru.id
    where ru.del_flag = 0 and ru.status = 1
    <if test="dto.attendanceStatus == 0">
      and temp.lateNum = 0 and temp.leaveNum=0 and temp.missNum = 0 and temp.lateDormNum = 0 and temp.abLateDormNum = 0
    </if>
    <if test="dto.attendanceStatus == 1">
      and temp.lateNum > 0
    </if>
    <if test="dto.attendanceStatus == 2">
      and temp.leaveNum > 0
    </if>
    <if test="dto.attendanceStatus == 3">
      and temp.missNum > 0
    </if>
    <if test="dto.attendanceStatus == 7">
      and temp.lateDormNum > 0
    </if>
    <if test="dto.attendanceStatus == 8">
      and temp.abLateDormNum > 0
    </if>
    GROUP BY temp.userId,temp.ruleId
  </select>

  <select id="findManageListUserDetailVOErrorNum" resultType="java.lang.Integer">
    SELECT count(*)
    FROM(SELECT
    temp.userId,
    sum( temp.lateNum ) lateNum,
    sum( temp.lateMinute ) lateMinute,
    sum( temp.leaveNum ) leaveNum,
    sum( temp.leaveMinute ) leaveMinute,
    sum( temp.lateDormNum ) lateDormNum,
    sum( temp.lateDormMinute ) lateDormMinute,
    sum( temp.abLateDormMinute ) abLateDormMinute,
    sum( temp.abLateDormNum ) abLateDormNum,
    sum( temp.missNum ) missNum,
    GROUP_CONCAT( ru.`name` ) ruleName
    FROM(
    SELECT user_id userId,
    rule_id ruleId,
    sum(IF(res.start_status = 1, 1, 0)) lateNum,
    sum(res.start_late_time) lateMinute,
    sum(if(res.att_type=0,IF(res.end_status = 1, 1, 0),0)) leaveNum,
    sum(if(res.att_type=0,res.end_leave_time,0)) leaveMinute,
    sum(if(res.att_type=1,IF(res.start_status = 7, 1, 0),0)) lateDormNum,
    sum(if(res.att_type=1,IF(res.start_status = 8, 1, 0),0)) abLateDormNum,
    sum(if(res.att_type=1,res.start_late_time,0)) lateDormMinute,
    sum(if(res.att_type=1,res.start_late_time,0)) abLateDormMinute,
    sum(if(res.att_type=0,IF(res.start_status IS null or res.end_status IS NULL, 1, 0),IF(res.start_status IS NULL, 1, 0)))
    missNum
    FROM attendance_result res
    WHERE 1 = 1
    <if test="dto.ruleId!=null">
      and res.rule_id = #{dto.ruleId}
    </if>
    <if test="dto.searchUserType!=null">
      and res.user_type = #{dto.searchUserType}
    </if>

    <if test="dto.userIdList!=null and dto.userIdList.size()>0">
      AND res.user_id IN
      <foreach collection="dto.userIdList" item="item" separator="," close=")" open="(">
        #{item}
      </foreach>
    </if>
    <if test="dto.attendanceStartDate !=null">
      and res.attendance_date <![CDATA[>=]]> #{dto.attendanceStartDate}
    </if>
    <if test="dto.attendanceEndDate !=null">
      and res.attendance_date <![CDATA[<=]]> #{dto.attendanceEndDate}
    </if>
    GROUP BY res.user_id,
    res.rule_id
    ) temp
    LEFT JOIN attendance_rule ru ON temp.ruleId = ru.id
    where ru.del_flag = 0 and ru.status = 1
    <if test="dto.attendanceStatus == 0">
      and temp.lateNum = 0 and temp.leaveNum=0 and temp.missNum = 0
    </if>
    <if test="dto.attendanceStatus == 1">
      and temp.lateNum > 0
    </if>
    <if test="dto.attendanceStatus == 2">
      and temp.leaveNum > 0
    </if>
    <if test="dto.attendanceStatus == 3">
      and temp.missNum > 0
    </if>
    <if test="dto.attendanceStatus == 7">
      and temp.lateDormNum > 0
    </if>
    <if test="dto.attendanceStatus == 8">
      and temp.abLateDormNum > 0
    </if>
    GROUP BY temp.userId ) a WHERE a.lateNum>0 OR a.leaveNum > 0 OR a.missNum>0 OR a.lateDormNum>0 OR a.abLateDormNum>0
  </select>


</mapper>
