<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "mybatis-3-mapper.dtd">
<mapper namespace="com.mortals.xhx.module.attendance.dao.ibatis.AttendanceSummaryDaoImpl">

    <!-- 汇总当前日期假期余额 -->
    <select id="getVacationBalanceSummary" parameterType="com.mortals.xhx.module.attendance.model.vo.AttendanceSummaryQuery" resultType="com.mortals.xhx.module.attendance.model.vo.VacationBalanceSummaryVo">
        SELECT DISTINCT
            s.id AS staffId,
            s.`name` AS staffName,
            s.phoneNumber,
            s.workNum,
            s.deptId,
            s.deptName,
            s.salaId,
            s.salaName,
            s.`status`,
            s.leaveDate,
            DATE_FORMAT(STR_TO_DATE(#{summaryTime},'%Y-%m-%d'),'%Y') as `year`,
            DATE_FORMAT(STR_TO_DATE(#{summaryTime},'%Y-%m-%d'),'%m') as `month`,
            DATE_FORMAT(STR_TO_DATE(#{summaryTime},'%Y-%m-%d'),'%d') as `day`,
            0 as personalLeaveDays,
            0 as compensatedLeaveDays,
            0 as sickLeaveDays,
            0 as annualLeaveDays,
            0 as marriageLeaveDays,
            0 as maternityLeaveDays,
            0 as paternityLeaveDays,
            0 as menstrualLeaveDays,
            0 as breastfeedingLeaveDays,
            0 as bereavementLeaveDays,
            0 as backToUnit,
            0 as onDutyLeave,
            0 as outOfOffice,
            0 as shiftCompensation,
            0 as physicalExamination,
            0 as quarantine,
            0 as businessTrip,
            0 as publicHoliday,
            0 as childRearingLeave,
            0 as transferBack,
            0 as homeLeave
        FROM
            mortals_xhx_staff s,
            mortals_xhx_attendance_group_staff gs,
            mortals_xhx_attendance_group_fixedwork f
        WHERE
            s.id = gs.staffId
        AND gs.groupId = f.groupId
        <if test="dayOfWeek != null and dayOfWeek == 2"> AND f.monday = 1</if>
        <if test="dayOfWeek != null and dayOfWeek == 3"> AND f.tuesday = 1</if>
        <if test="dayOfWeek != null and dayOfWeek == 4"> AND f.wednesday = 1</if>
        <if test="dayOfWeek != null and dayOfWeek == 5"> AND f.thursday = 1</if>
        <if test="dayOfWeek != null and dayOfWeek == 6"> AND f.friday = 1</if>
        <if test="dayOfWeek != null and dayOfWeek == 7"> AND f.saturday = 1</if>
        <if test="dayOfWeek != null and dayOfWeek == 1"> AND f.sunday = 1</if>
        <if test="staffId != null and staffId!=''"> AND s.id = #{staffId}</if>
    </select>

    <!-- 汇总当前日期请假情况 -->
    <select id="getLeaveSummary" parameterType="com.mortals.xhx.module.attendance.model.vo.AttendanceSummaryQuery" resultType="com.mortals.xhx.module.attendance.model.vo.LeaveSummaryVo">
        SELECT
            r.leavePersonId AS staffId,
            r.leavePerson AS staffName,
            ANY_VALUE(s.`status`) as `status`,
            ANY_VALUE(s.leaveDate) as leaveDate,
            r.leaveType,
            IFNULL(sum(r.duration),0) as duration,
            ANY_VALUE(DATE_FORMAT(STR_TO_DATE(#{summaryTime},'%Y-%m-%d'),'%Y')) as `year`,
            ANY_VALUE(DATE_FORMAT(STR_TO_DATE(#{summaryTime},'%Y-%m-%d'),'%m')) as `month`,
            ANY_VALUE(DATE_FORMAT(STR_TO_DATE(#{summaryTime},'%Y-%m-%d'),'%d')) as `day`,
            ANY_VALUE(r.startTime) as startTime,
            ANY_VALUE(r.endTime) as endTime
        FROM
            mortals_xhx_attendance_leave_record r INNER JOIN mortals_xhx_staff s ON r.leavePersonId = s.id
        WHERE
            r.auditResult = 1
            AND r.processStatus = 2
            AND STR_TO_DATE(date_format(r.startTime,'%Y-%m-%d'),'%Y-%m-%d')  <![CDATA[ <= ]]> STR_TO_DATE(#{summaryTime},'%Y-%m-%d')
            AND STR_TO_DATE(date_format(r.endTime,'%Y-%m-%d'),'%Y-%m-%d')  <![CDATA[ >= ]]> STR_TO_DATE(#{summaryTime},'%Y-%m-%d')
        <if test="staffId != null and staffId!=''"> AND leavePersonId = #{staffId}</if>
        GROUP BY r.leavePersonId,r.leavePerson,r.leaveType

    </select>

    <!-- 汇总当前日期异常考勤 -->
    <select id="getErrorSummary" parameterType="com.mortals.xhx.module.attendance.model.vo.AttendanceSummaryQuery" resultType="com.mortals.xhx.module.attendance.model.vo.ErrorSummaryVo">
        select
            e.staffId,
            e.workNum,
            ANY_VALUE(s.`status`) as `status`,
            ANY_VALUE(s.leaveDate) as leaveDate,
            ANY_VALUE(DATE_FORMAT(errorDateTime,'%Y')) as `year`,
            ANY_VALUE(DATE_FORMAT(errorDateTime,'%m')) as `month`,
            ANY_VALUE(DATE_FORMAT(errorDateTime,'%d')) as `day`,
            sum(case processResult when 0 then 1 ELSE 0 end) as morningTimes,
            sum(case processResult when 0 then 1 ELSE 0 end) as afternoonTimes,
            sum(case processResult when 1 then 1 ELSE 0 end) as overtimeTimes,
            sum(case processResult when 2 then 1 ELSE 0 end) as lateTimes
        from
            mortals_xhx_attendance_record_error e INNER JOIN mortals_xhx_staff s ON e.staffId = s.id
        where
            processResult != 4
            and e.errorDateTime <![CDATA[ >= ]]> STR_TO_DATE(left(concat(#{summaryTime},' 00:00:00'),19),'%Y-%m-%d %k:%i:%s')
            and e.errorDateTime <![CDATA[ <= ]]> STR_TO_DATE(left(concat(#{summaryTime},' 23:59:59'),19),'%Y-%m-%d %k:%i:%s')
        <if test="staffId != null and staffId!=''"> AND e.staffId = #{staffId}</if>
        group by
            e.staffId,
            e.workNum,
            DATE_FORMAT(errorDateTime,'%Y-%m-%d')
    </select>
</mapper>