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

    <!-- 更新为缺勤 -->
    <update id="updateToAbsence">
        update ftb_attendance_clock_in_result
        set
            F_Absence = 1,
            F_LastAbsenceLeader = #{handleUser},
            F_LastModifyTime = now()
        where F_RuleId = #{ruleId} and F_UserId = #{userId} and F_DeleteMark = 0
    </update>
    <!-- 更新为不缺勤 -->
    <update id="updateToUnAbsence">
        update ftb_attendance_clock_in_result
        set
            F_Absence = 0,
            F_LastAbsenceLeader = #{handleUser},
            F_LastModifyTime = now()
        where F_RuleId = #{ruleId} and F_UserId = #{userId} and F_DeleteMark = 0
    </update>
    <!-- 删除审批中的打卡记录 -->
    <delete id="deleteBatchByUserDay">
        DELETE FROM ftb_attendance_clock_in a
        WHERE EXISTS (
            SELECT 1
            FROM (
            <foreach collection="list" item="item" separator=" UNION ALL ">
                SELECT #{item.dayStr} AS dayStr, #{item.userId} AS userId
            </foreach>
            ) t
            WHERE a.F_Day = t.dayStr
            AND a.F_UserId = t.userId
        ) AND a.F_ApprovalStatus = #{passApproval}
    </delete>
    <!-- 查询用户出勤结果 -->
    <select id="getClockInResultByRule" resultType="jnpf.model.attendance.vo.ClockInVo">
        select acis.F_Id id,acis.F_RuleId ruleId, acis.F_UserId userId, acis.F_ClockInId clockInId, aci.F_ClockInTime clockInTime, aci.F_ApprovalStatus approvalStatus,
           acis.F_EffectiveTime effectiveTime, acis.F_ClockInType clockInType, acis.F_ClockInStatus clockInStatus, acis.F_ClockInKind clockInKind,
           acis.F_Absence absence, acis.F_AbsenceLeader absenceLeader, acis.F_AbnormalMinute abnormalMinute, aci.F_Address address, aci.F_Lng lng, aci.F_Lat lat,
           aci.F_DeviceType deviceType, aci.F_DeviceId deviceId, aci.F_DeviceName deviceName, acis.F_ApplyType applyType, acis.F_ApplyId applyId, acis.F_Repaired repaired
            ,aci.F_Remark remark
        from ftb_attendance_clock_in_result acis
        left join ftb_attendance_clock_in aci on acis.F_ClockInId = aci.F_Id
        JOIN (
        <foreach collection="list" item="item" separator=" UNION ALL ">
          SELECT #{item.ruleId} AS ruleId, #{item.userId} AS userId
        </foreach>
        ) AS tmp ON acis.F_RuleId = tmp.ruleId AND acis.F_UserId = tmp.userId
        WHERE acis.F_DeleteMark = 0
        AND (aci.F_Id IS NULL OR aci.F_ApprovalStatus != 2)
    </select>
    <!-- 查询用户出勤结果 -->
    <select id="getClockInResultByRuleList" resultType="jnpf.model.attendance.vo.ClockInVo">
        select acis.F_Id id, acis.F_RuleId ruleId, acis.F_UserId userId, acis.F_ClockInId clockInId, aci.F_ClockInTime clockInTime, aci.F_ApprovalStatus approvalStatus,
           acis.F_EffectiveTime effectiveTime, acis.F_ClockInType clockInType, acis.F_ClockInStatus clockInStatus, acis.F_ClockInKind clockInKind, acis.F_Repaired repaired,
           acis.F_Absence absence, acis.F_AbsenceLeader absenceLeader, acis.F_AbnormalMinute abnormalMinute, acis.F_ApplyId applyId, aci.F_Address address, aci.F_Lng lng, aci.F_Lat lat,
           aci.F_DeviceType deviceType, aci.F_DeviceId deviceId, aci.F_DeviceName deviceName , aci.F_Remark remark, r.F_ApplyId AS associationApplyId, r.F_GroupId groupId
        from ftb_attendance_clock_in_result acis
        left join ftb_attendance_clock_in aci on acis.F_ClockInId = aci.F_Id
        LEFT JOIN ftb_attendance_daily_rule r ON r.F_Id = acis.F_RuleId
        where acis.F_DeleteMark = 0 and (aci.F_Id is null or aci.F_ApprovalStatus != 2)
          and acis.F_RuleId in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <!-- 查询时间段内的补卡次数 -->
    <select id="getRepairCount" resultType="java.lang.Integer">
        select count(*) from (
            select acis.F_Repaired repaired, case acis.F_ClockInType when 1 then adr.F_InPoint when 2 then adr.F_OutPoint else '' end workTime
            from ftb_attendance_clock_in_result acis
            left join ftb_attendance_clock_in aci on acis.F_ClockInId = aci.F_Id
            left join ftb_attendance_daily_rule adr on acis.F_RuleId = adr.F_Id
            where acis.F_DeleteMark = 0 and acis.F_RuleId in (
              select F_Id from ftb_attendance_daily_rule where F_GroupId = #{groupId} and F_ApplyViewEnable = 1 and F_DeleteMark = 0
            ) and acis.F_UserId = #{userId} and (aci.F_DeleteMark is null or aci.F_DeleteMark = 0)
        ) t where repaired = 1
        <![CDATA[
          and DATE_FORMAT(workTime, '%Y-%m-%d') >= DATE_FORMAT(#{startTime}, '%Y-%m-%d')
          and DATE_FORMAT(workTime, '%Y-%m-%d') < DATE_FORMAT(#{endTime}, '%Y-%m-%d')
        ]]>
    </select>
    <!-- 查询考勤组本月异常打卡记录 -->
    <select id="getAbnormalClockInList" resultType="jnpf.model.attendance.vo.AbnormalClockInVo">
        select id, clockInType, ruleId, clockInId, `day`, workTime, clockInTime, clockInStatus, absence from (
            select acir.F_Id id, acir.F_ClockInType clockInType, acir.F_RuleId ruleId, acir.F_ClockInId clockInId, adr.F_Day `day`,
            case acir.F_ClockInType when 1 then adr.F_InPoint when 2 then adr.F_OutPoint else '' end workTime,
            aci.F_ClockInTime clockInTime, acir.F_ClockInStatus clockInStatus, acir.F_Absence absence, acir.F_AbsenceLeader absenceLeader
            from ftb_attendance_clock_in_result acir
            left join ftb_attendance_clock_in aci on acir.F_ClockInId = aci.F_Id
            left join ftb_attendance_daily_rule adr on acir.F_RuleId = adr.F_Id
            where acir.F_UserId = #{userId} and acir.F_DeleteMark = 0 and (aci.F_DeleteMark is null or aci.F_DeleteMark = 0)
            and ifnull(length(acir.F_ApplyId), 0) = 0
            <if test="absenceType == 0">
                and acir.F_Absence = 0 and acir.F_ClockInStatus in
                <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="absenceType == 1">
                and (acir.F_Absence = 1 or acir.F_ClockInStatus in
                <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
                )
            </if>
            and acir.F_RuleId in (
                select F_Id from ftb_attendance_daily_rule where F_GroupId = #{groupId} and F_ApplyViewEnable in (1, 10) and F_DeleteMark = 0
            ) and adr.F_Id is not null
        ) t where
        <![CDATA[
          DATE_FORMAT(workTime, '%Y-%m-%d') >= DATE_FORMAT(#{monthBeginDate}, '%Y-%m-%d')
          and DATE_FORMAT(workTime, '%Y-%m-%d') <= DATE_FORMAT(#{monthEndDate}, '%Y-%m-%d')
        ]]>
        order by t.day desc, t.clockInType asc
    </select>
    <!-- 查询当日所有打卡记录 -->
    <select id="getListByRuleAndDate" resultType="jnpf.model.attendance.vo.DailyClockInVo">
        select ci.F_Id clockInId, ci.F_ClockInTime clockInTime, ci.F_Address clockInPlace, F_DeviceType clockInMethod, t.F_RuleId ruleId, ifnull(t.F_DeleteMark, 1) deleteMark
        from ftb_attendance_clock_in ci
            left join (
            select F_ClockInId, F_RuleId, F_DeleteMark from ftb_attendance_clock_in_result cir
            where cir.F_RuleId in (
                select F_Id from ftb_attendance_daily_rule where F_UserId = #{userId} and F_Day = #{queryDate} and F_DeleteMark = 0
            )
        ) t on t.F_ClockInId = ci.F_Id
        where ci.F_DeleteMark = 0 and ci.F_ApprovalStatus = 0 and ci.F_Day = #{queryDate} and ci.F_UserId = #{userId}
        order by ci.F_CreatorTime asc
    </select>


    <!-- 查询用户出勤打卡记录 -->
    <select id="getClockInByStatistics" resultType="jnpf.model.attendance.model.ClockClassRecord">
        SELECT
            acis.F_DeleteMark,
            aci.F_Day AS `day`,
            acis.F_Id AS id,
            acis.F_RuleId AS ruleId,
            acis.F_UserId AS userId,
            aci.F_Id AS clockInId,
            aci.F_ClockInTime AS clockInTime,
            aci.F_ApprovalStatus AS approvalStatus,
            acis.F_EffectiveTime AS effectiveTime,
            acis.F_ClockInType AS clockInType,
            acis.F_ClockInStatus AS clockInStatus,
            acis.F_ClockInKind AS clockInKind,
            acis.F_Repaired AS repaired,
            acis.F_Absence AS absence,
            acis.F_AbsenceLeader AS absenceLeader,
            acis.F_AbnormalMinute AS abnormalMinute,
            acis.F_ApplyId AS applyId,
            aci.F_Address AS address,
            aci.F_Lng AS lng,
            aci.F_Lat AS lat,
            aci.F_DeviceType AS deviceType,
            aci.F_DeviceId AS deviceId,
            aci.F_DeviceName AS deviceName
        FROM
            ftb_attendance_clock_in aci
                LEFT JOIN (
                SELECT
                    r.*
                FROM
                    ftb_attendance_clock_in_result r
                        LEFT JOIN ftb_attendance_daily_rule dr ON dr.F_Id = r.F_RuleId
                WHERE
                    r.F_UserId = #{userId}
                  AND dr.F_Day = #{queryDate}
                  AND r.F_DeleteMark = 0
            ) acis ON acis.F_ClockInId = aci.F_Id
        WHERE
            aci.F_Day = #{queryDate}
          AND aci.F_UserId = #{userId}
          AND aci.F_DeleteMark = 0
          AND aci.F_ApprovalStatus = 0
        ORDER BY
            aci.F_CreatorTime
    </select>
<!--    批量查询当日所有打卡记录-->
    <select id="getClockInList" resultType="jnpf.model.attendance.model.ClockClassRecord">
        select aci.F_Day `day`, acis.F_Id id, acis.F_RuleId ruleId, aci.F_UserId userId, aci.F_Id clockInId, aci.F_ClockInTime clockInTime, aci.F_ApprovalStatus approvalStatus,
               acis.F_EffectiveTime effectiveTime, acis.F_ClockInType clockInType, acis.F_ClockInStatus clockInStatus, acis.F_ClockInKind clockInKind, acis.F_Repaired repaired,
               acis.F_Absence absence, acis.F_AbsenceLeader absenceLeader, acis.F_AbnormalMinute abnormalMinute, acis.F_ApplyId applyId, aci.F_Address address, aci.F_Lng lng, aci.F_Lat lat,

               aci.F_DeviceType deviceType, aci.F_DeviceId deviceId, aci.F_DeviceName deviceName
        from ftb_attendance_clock_in aci
        left join ftb_attendance_clock_in_result acis on acis.F_ClockInId = aci.F_Id
        where ifnull(acis.F_DeleteMark, 0) = 0 and aci.F_DeleteMark = 0 and aci.F_ApprovalStatus = 0
        <![CDATA[ and DATE_FORMAT(aci.F_Day, '%Y-%m-%d') >= DATE_FORMAT(#{start}, '%Y-%m-%d') ]]>
        <![CDATA[ and DATE_FORMAT(aci.F_Day, '%Y-%m-%d') <= DATE_FORMAT(#{end}, '%Y-%m-%d') ]]>
          and aci.F_UserId in
        <foreach collection="userIds" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        order by aci.F_CreatorTime asc
    </select>
    <!-- 查询打卡记录[批量] -->
    <select id="selectListBatch" resultType="jnpf.entity.attendance.FtbAttendanceClockIn">
        select
          ac.F_Id AS id,
          ac.F_Day AS `day`,
          ac.F_UserId AS userId,
          ac.F_ClockInTime AS clockInTime,
          ac.F_ApprovalCode AS approvalCode,
          ac.F_ApprovalStatus AS approvalStatus,
          ac.F_RelationId AS relationId,
          ac.F_Address AS address,
          ac.F_Lng AS lng,
          ac.F_Lat AS lat,
          ac.F_ClockInKind AS clockInKind,
          ac.F_DeviceType AS deviceType,
          ac.F_DeviceId AS deviceId,
          ac.F_DeviceName AS deviceName,
          ac.F_Remark AS remark
        from ftb_attendance_clock_in ac
        join (
        <foreach collection="list" item="ud" separator="," open="values" close="">
            row(#{ud.userId}, #{ud.dayStr})
        </foreach>
        ) v(userId, `day`)
        on ac.F_UserId = v.userId and ac.F_Day = v.`day`
        where ac.F_DeleteMark = 0
        and ac.F_ApprovalStatus = #{approvalStatus}
        order by ac.F_ClockInTime asc
    </select>
</mapper>