<?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.AttendanceStatDaoImpl">
    <select id="getListExt" parameterType="com.mortals.xhx.module.attendance.model.vo.AttendanceSummaryQuery" resultMap="AttendanceStatEntity-Map">
        SELECT * FROM (
        SELECT
        s.id,
        s.staffId,
        s.staffName,
        s.deptId,
        s.deptName,
        s.phoneNumer,
        ROUND(IFNULL(sum(s.goTimes),0)/count(1),4) as attendanceRate,
        sum(s.goTimes) AS goTimes,
        sum(s.morningTimes) AS morningTimes,
        sum(s.afternoonTimes) AS afternoonTimes,
        sum(s.backToUnit) AS backToUnit,
        sum(s.onDutyLeave) AS onDutyLeave,
        sum(s.outOfOffice) AS outOfOffice,
        sum(s.shiftCompensation) AS shiftCompensation,
        sum(s.physicalExamination) AS physicalExamination,
        sum(s.quarantine) AS quarantine,
        sum(s.businessTrip) AS businessTrip,
        sum(s.publicHoliday) AS publicHoliday,
        sum(s.sickLeave) AS sickLeave,
        sum(s.funeralLeave) AS funeralLeave,
        sum(s.marriageLeave) AS marriageLeave,
        sum(s.childRearingLeave) AS childRearingLeave,
        sum(s.maternityLeave) AS maternityLeave,
        sum(s.transferBack) AS transferBack,
        sum(s.homeLeave) AS homeLeave,
        sum(s.breastfeedingLeaveDays) AS breastfeedingLeaveDays,
        sum(s.personalLeave) AS personalLeave,
        sum(s.compensatedLeaveDays) AS compensatedLeaveDays,
        sum(s.annualLeaveDays) AS annualLeaveDays,
        sum(s.menstrualLeaveDays) AS menstrualLeaveDays,
        sum(s.paternityLeaveDays) AS paternityLeaveDays,
        sum(s.absenteeismDays) AS absenteeismDays,
        sum(s.otherDays) AS otherDays,
        sum(s.nonCompliancePunch) AS nonCompliancePunch,
        sum(s.surfingMobileTimes) AS surfingMobileTimes,
        sum(s.overtimeTimes) AS overtimeTimes,
        sum(s.vacancy) AS vacancy,
        sum(s.nonStandardDressTimes) AS nonStandardDressTimes,
        sum(s.unexcusedMeetingAbsence) AS unexcusedMeetingAbsence,
        sum(s.earlyLeaveMeeting) AS earlyLeaveMeeting
        FROM
        mortals_xhx_attendance_stat s
        WHERE
        STR_TO_DATE(CONCAT(s.`year`,'-',s.`month`,'-',s.`day`),'%Y-%m-%d') <![CDATA[ >= ]]> STR_TO_DATE(#{summaryTimeStart},'%Y-%m-%d')
        AND STR_TO_DATE(CONCAT(s.`year`,'-',s.`month`,'-',s.`day`),'%Y-%m-%d') <![CDATA[ <= ]]> STR_TO_DATE(#{summaryTimeEnd},'%Y-%m-%d')
        <if test="staffName != null and staffName!=''"> AND s.staffName like #{staffName}</if>
        <if test="phoneNumer != null and phoneNumer!=''"> AND s.phoneNumer like #{phoneNumer}</if>
        <if test="deptId != null and deptId!=''"> AND s.deptId = #{deptId}</if>
        <if test="idList != null and idList.size>0">
            and s.id in
            <foreach collection="idList" open="(" close=")" index="index" item="item" separator=",">
                #{item}
            </foreach>
        </if>
        GROUP BY
        s.staffId,
        s.staffName,
        s.deptId,
        s.deptName,
        s.phoneNumer
        ) AS a
    </select>

    <select id="getListCountExt" parameterType="com.mortals.xhx.module.attendance.model.vo.AttendanceSummaryQuery" resultType="int">
        SELECT count(1) FROM (
        SELECT
        s.id,
        s.staffId,
        s.staffName,
        s.deptId,
        s.deptName,
        s.phoneNumer,
        ROUND(IFNULL(sum(s.goTimes),0)/count(1),4) as attendanceRate,
        sum(s.goTimes) AS goTimes,
        sum(s.morningTimes) AS morningTimes,
        sum(s.afternoonTimes) AS afternoonTimes,
        sum(s.backToUnit) AS backToUnit,
        sum(s.onDutyLeave) AS onDutyLeave,
        sum(s.outOfOffice) AS outOfOffice,
        sum(s.shiftCompensation) AS shiftCompensation,
        sum(s.physicalExamination) AS physicalExamination,
        sum(s.quarantine) AS quarantine,
        sum(s.businessTrip) AS businessTrip,
        sum(s.publicHoliday) AS publicHoliday,
        sum(s.sickLeave) AS sickLeave,
        sum(s.funeralLeave) AS funeralLeave,
        sum(s.marriageLeave) AS marriageLeave,
        sum(s.childRearingLeave) AS childRearingLeave,
        sum(s.maternityLeave) AS maternityLeave,
        sum(s.transferBack) AS transferBack,
        sum(s.homeLeave) AS homeLeave,
        sum(s.breastfeedingLeaveDays) AS breastfeedingLeaveDays,
        sum(s.personalLeave) AS personalLeave,
        sum(s.compensatedLeaveDays) AS compensatedLeaveDays,
        sum(s.annualLeaveDays) AS annualLeaveDays,
        sum(s.menstrualLeaveDays) AS menstrualLeaveDays,
        sum(s.paternityLeaveDays) AS paternityLeaveDays,
        sum(s.absenteeismDays) AS absenteeismDays,
        sum(s.otherDays) AS otherDays,
        sum(s.nonCompliancePunch) AS nonCompliancePunch,
        sum(s.surfingMobileTimes) AS surfingMobileTimes,
        sum(s.overtimeTimes) AS overtimeTimes,
        sum(s.vacancy) AS vacancy,
        sum(s.nonStandardDressTimes) AS nonStandardDressTimes,
        sum(s.unexcusedMeetingAbsence) AS unexcusedMeetingAbsence,
        sum(s.earlyLeaveMeeting) AS earlyLeaveMeeting
        FROM
        mortals_xhx_attendance_stat s
        WHERE
        STR_TO_DATE(CONCAT(s.`year`,'-',s.`month`,'-',s.`day`),'%Y-%m-%d') <![CDATA[ >= ]]> STR_TO_DATE(#{summaryTimeStart},'%Y-%m-%d')
        AND STR_TO_DATE(CONCAT(s.`year`,'-',s.`month`,'-',s.`day`),'%Y-%m-%d') <![CDATA[ <= ]]> STR_TO_DATE(#{summaryTimeEnd},'%Y-%m-%d')
        <if test="staffName != null and staffName!=''"> AND s.staffName like #{staffName}</if>
        <if test="phoneNumer != null and phoneNumer!=''"> AND s.phoneNumer like #{phoneNumer}</if>
        <if test="deptId != null and deptId!=''"> AND s.deptId = #{deptId}</if>
        GROUP BY
        s.staffId,
        s.staffName,
        s.deptId,
        s.deptName,
        s.phoneNumer
        ) AS a
    </select>
</mapper>