<?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="jnpf.attendance.mapper.AttendanceAIMapper">
    <!-- 查询打卡记录列表 -->
    <select id="getClockRecordList" resultType="jnpf.model.attendance.vo.attendance.ClockRecordVo">
        SELECT
        dr.F_Id AS ruleId,
        dr.F_InPoint inPoint,
        dr.F_OutPoint outPoint,
        MAX(CASE WHEN ir.F_ClockInType = 1 THEN ir.F_ClockInId END) AS inClockInId,
        MAX(CASE WHEN ir.F_ClockInType = 1 THEN i.F_ClockInTime END) AS onWorkDateTime,
        MAX(CASE WHEN ir.F_ClockInType = 1 THEN i.F_Address END) AS onWorkAddress,
        CASE
        WHEN COUNT(CASE WHEN ir.F_ClockInType = 1 THEN 1 END) <![CDATA[ > ]]> 1 THEN '异常'
        WHEN COUNT(CASE WHEN ir.F_ClockInType = 1 THEN 1 END) <![CDATA[ < ]]> 1 THEN '未打卡'
        WHEN ir.F_Absence IS NOT NULL AND ir.F_Absence = 1 THEN '旷工'
        ELSE MAX(
        CASE WHEN ir.F_ClockInType = 1
        THEN CASE ir.F_ClockInStatus
             WHEN -2 THEN '无需打卡'
             WHEN -1 THEN '缺卡'
             WHEN 1 THEN '正常'
             WHEN 2 THEN '迟到'
             ELSE '异常'
             END
        END
        )
        END AS onWorkStatus,
        MAX(CASE WHEN ir.F_ClockInType = 2 THEN ir.F_ClockInId END) AS outClockInId,
        MAX(CASE WHEN ir.F_ClockInType = 2 THEN i.F_ClockInTime END) AS offWorkDateTime,
        MAX(CASE WHEN ir.F_ClockInType = 2 THEN i.F_Address END) AS offWorkAddress,
        CASE
        WHEN COUNT(CASE WHEN ir.F_ClockInType = 2 THEN 1 END) <![CDATA[ > ]]> 1 THEN '异常'
        WHEN COUNT(CASE WHEN ir.F_ClockInType = 2 THEN 1 END) <![CDATA[ < ]]> 1 THEN '未打卡'
        WHEN ir.F_Absence IS NOT NULL AND ir.F_Absence = 1 THEN '旷工'
        ELSE MAX(
        CASE WHEN ir.F_ClockInType = 2
        THEN CASE ir.F_ClockInStatus
             WHEN -2 THEN '无需打卡'
             WHEN -1 THEN '缺卡'
             WHEN 1 THEN '正常'
             WHEN 3 THEN '早退'
             ELSE '异常'
             END
        END
        )
        END AS offWorkStatus
        FROM ftb_attendance_daily_rule dr
        LEFT JOIN ftb_attendance_clock_in_result ir ON dr.F_Id = ir.F_RuleId AND ir.F_DeleteMark = 0
        LEFT JOIN ftb_attendance_clock_in i ON i.F_Id = ir.F_ClockInId
        WHERE dr.F_Day = #{queryDate} AND dr.F_UserId = #{userId}
        AND dr.F_AttendanceType IN (-1,1,2,4) AND dr.F_DeleteMark = 0
        GROUP BY dr.F_Id, dr.F_InPoint, dr.F_OutPoint
        ORDER BY dr.F_InPoint ASC
    </select>
</mapper>