<?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
            s.id AS staffId,
            s.`name` AS staffName,
            s.phoneNumber,
            s.workNum,
            s.deptId,
            s.deptName,
            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`,
            IFNULL(b.personalLeaveDays,0) as personalLeaveDays,
            IFNULL(b.compensatedLeaveDays,0) as compensatedLeaveDays,
            IFNULL(b.sickLeaveDays,0) as sickLeaveDays,
            IFNULL(b.annualLeaveDays,0) as annualLeaveDays,
            IFNULL(b.marriageLeaveDays,0) as marriageLeaveDays,
            IFNULL(b.maternityLeaveDays,0) as maternityLeaveDays,
            IFNULL(b.paternityLeaveDays,0) as paternityLeaveDays,
            IFNULL(b.menstrualLeaveDays,0) as menstrualLeaveDays,
            IFNULL(b.breastfeedingLeaveDays,0) as breastfeedingLeaveDays,
            IFNULL(b.bereavementLeaveDays,0) as bereavementLeaveDays,
            IFNULL(b.backToUnit,0) as backToUnit,
            IFNULL(b.onDutyLeave,0) as onDutyLeave,
            IFNULL(b.outOfOffice,0) as outOfOffice,
            IFNULL(b.shiftCompensation,0) as shiftCompensation,
            IFNULL(b.physicalExamination,0) as physicalExamination,
            IFNULL(b.quarantine,0) as quarantine,
            IFNULL(b.businessTrip,0) as businessTrip,
            IFNULL(b.publicHoliday,0) as publicHoliday,
            IFNULL(b.childRearingLeave,0) as childRearingLeave,
            IFNULL(b.transferBack,0) as transferBack,
            IFNULL(b.homeLeave,0) as homeLeave
        FROM
            mortals_xhx_staff s
        LEFT JOIN mortals_xhx_attendance_vacation_balance b ON s.id = b.staffId
        WHERE 1=1
        <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
            leavePersonId AS staffId,
            leavePerson AS staffName,
            leaveType,
            IFNULL(sum(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(startTime) as startTime,
            ANY_VALUE(endTime) as endTime
        FROM
            mortals_xhx_attendance_leave_record
        WHERE
            auditResult = 1
            AND processStatus = 2
            AND STR_TO_DATE(date_format(startTime,'%Y-%m-%d'),'%Y-%m-%d')  <![CDATA[ <= ]]> STR_TO_DATE(#{summaryTime},'%Y-%m-%d')
            AND STR_TO_DATE(date_format(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 leavePersonId,leavePerson,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.staffName,
            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
        where
            processStatus = 1
            and 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.staffName
    </select>
</mapper>