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

    <!-- 批量更新记录为删除 -->
    <update id="updateBatchToDel">
        update ftb_attendance_clock_in_result
        set
            F_DeleteMark = 1,
            F_DeleteTime = now()
        where F_Id in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>
    <!-- 更新申请信息为空 -->
    <update id="updateReplyToNull">
        update ftb_attendance_clock_in_result
        set
            F_ApplyId = null,
            F_ApplyType = null
        where F_ClockInId = #{clockInId} and F_ApplyId = #{applyId}
    </update>
    <!-- 更新审批为空 -->
    <update id="updateResultReplyToNull">
        update ftb_attendance_clock_in_result
        set
            F_ApplyId = null,
            F_ApplyType = null,
            F_AbsenceLeader = #{dealUser}
        where F_Id = #{id}
    </update>
    <!-- 根据出勤规则删除打卡结果 -->
    <update id="updateToDelByRule">
        update ftb_attendance_clock_in_result
        set
            F_DeleteMark = 1,
            F_DeleteUserId = #{updateUserId},
            F_DeleteTime = now()
        where F_RuleId in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>
    <!-- 更新缺勤为正常 -->
    <update id="updateAbsenceToNormal">
        update ftb_attendance_clock_in_result set F_Absence = 0 where F_Id = #{id}
    </update>
    <!-- 更新为缺勤 -->
    <update id="updateToAbsence">
        update ftb_attendance_clock_in_result set F_Absence = 1 where F_Id = #{id}
    </update>
    <!-- 根据出勤规则删除打卡结果 -->
    <update id="removeClockInResultByRule">
        update ftb_attendance_clock_in_result set F_DeleteMark = 1, F_LastModifyTime = now() where F_RuleId in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>
    <!-- 变更打卡结果绑定的rule -->
    <update id="updateResultRelation">
        update ftb_attendance_clock_in_result set F_RuleId = #{ruleId} where F_Id = #{resultId}
    </update>
    <!-- 更新为缺勤[批量] -->
    <update id="updateToAbsenceBatch">
        update ftb_attendance_clock_in_result set F_Absence = 1
        where F_Id in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>
    <!-- 根据出勤规则删除用户的打卡结果 -->
    <delete id="deleteRecordByRule">
        update ftb_attendance_clock_in_result
        set
            F_DeleteMark = 1,
            F_DeleteUserId = #{updateUserId},
            F_DeleteTime = now()
        where F_UserId = #{userId} and F_RuleId in (
            select t.ruleId from (
                select distinct acir.F_RuleId ruleId from ftb_attendance_clock_in_result acir
                left join ftb_attendance_daily_rule adr on acir.F_RuleId = adr.F_Id
                where acir.F_UserId = #{userId} and adr.F_Id is null and acir.F_DeleteMark = 0
            ) t
        )
    </delete>
    <!-- 根据出勤规则删除用户的打卡结果[批量] -->
    <delete id="deleteRecordByRuleBatch">
        UPDATE ftb_attendance_clock_in_result acir
        LEFT JOIN ftb_attendance_daily_rule adr ON acir.F_RuleId = adr.F_Id
        SET
        acir.F_DeleteMark = 1,
        acir.F_DeleteUserId = #{updateUserId},
        acir.F_DeleteTime = NOW()
        WHERE acir.F_UserId IN
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        AND acir.F_DeleteMark = 0
        AND adr.F_Id IS NULL
    </delete>
    <!-- 根据条件查询用户打卡记录 -->
    <select id="selectUserClockInHistory" resultType="jnpf.entity.attendance.AttendanceClockInResult">
        select acis.F_Id id, acis.F_RuleId ruleId, acis.F_UserId userId, acis.F_ClockInId clockInId, acis.F_ClockInType clockInType, acis.F_DeleteMark deleteMark, ifnull(aci.F_ApprovalStatus, 0) approvalStatus
        from ftb_attendance_daily_rule r
        left join ftb_attendance_clock_in_result acis on r.F_Id = acis.F_RuleId
        left join ftb_attendance_clock_in aci on acis.F_ClockInId = aci.F_Id
        where date_format(r.F_Day,'%Y-%m-%d') = date_format(date_sub(#{dateStr}, interval 1 day),'%Y-%m-%d')
        and r.F_DeleteMark = 0 and r.F_ApplyViewEnable in (1, 3, 9, 10) and acis.F_ClockInId is not null
        and (date_format(r.F_InLackPoint,'%Y-%m-%d') = date_format(#{dateStr},'%Y-%m-%d') or date_format(r.F_OutLackPoint,'%Y-%m-%d') = date_format(#{dateStr},'%Y-%m-%d'))
        union all
        select acis.F_Id id, acis.F_RuleId ruleId, acis.F_UserId userId, acis.F_ClockInId clockInId, acis.F_ClockInType clockInType, acis.F_DeleteMark deleteMark, ifnull(aci.F_ApprovalStatus, 0) approvalStatus
        from ftb_attendance_clock_in_result acis
        left join ftb_attendance_clock_in aci on acis.F_ClockInId = aci.F_Id
        where date_format(F_ClockInTime,'%Y-%m-%d') = date_format(#{dateStr},'%Y-%m-%d')
    </select>
    <!-- 判断是否缺勤 -->
    <select id="countAbsence" resultType="java.lang.Integer">
        select count(*) from ftb_attendance_clock_in_result
        where F_RuleId = #{ruleId} and F_UserId = #{userId} and F_DeleteMark = 0 and F_Absence = 1 and length(ifnull(F_AbsenceLeader, '')) > 0
    </select>
    <!-- 查询已存在的打卡结果 -->
    <select id="selectExistRecord" resultType="jnpf.entity.attendance.AttendanceClockInResult">
        SELECT F_Id id, F_RuleId ruleId, F_UserId userId, F_ClockInType clockInType
        FROM ftb_attendance_clock_in_result
        WHERE F_DeleteMark = 0
        <choose>
            <when test="containsType == 1">
                AND (F_RuleId, F_UserId, F_ClockInType) IN
                <foreach collection="list" item="item" open="(" separator="," close=")">
                    (#{item.ruleId}, #{item.userId}, #{item.clockInType})
                </foreach>
            </when>
            <otherwise>
                AND (F_RuleId, F_UserId) IN
                <foreach collection="list" item="item" open="(" separator="," close=")">
                    (#{item.ruleId}, #{item.userId})
                </foreach>
            </otherwise>
        </choose>
    </select>
    <!-- 查询跨天的打卡id -->
    <select id="getCrossDayClockInIdList" resultType="java.lang.String">
        select F_Id from ftb_attendance_clock_in where F_Id in (
        select F_ClockInId from ftb_attendance_clock_in_result where F_ApprovalStatus = 0 and F_DeleteMark = 0 and F_RuleId in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        )
        <![CDATA[
          and DATE_FORMAT(F_Day,'%Y-%m-%d') > DATE_FORMAT(#{day},'%Y-%m-%d')
        ]]>
    </select>
    <!-- 查询跨天的打卡id[批量] -->
    <select id="getCrossDayClockInIdListBatch" resultType="string">
        select ac.F_Id
        from ftb_attendance_clock_in ac
        join (
        <foreach collection="list" item="item" separator="," open="values" close="">
            row(#{item.ruleId}, #{item.day})
        </foreach>
        ) v(ruleId, `day`)
        on exists (
          select 1
          from ftb_attendance_clock_in_result r
          where r.F_RuleId = v.ruleId
          and r.F_ClockInId = ac.F_Id
          and ac.F_ApprovalStatus = 0
          and r.F_DeleteMark = 0
        )
        where ac.F_Day > v.`day`
    </select>
    <!-- 查询打卡结果是否在审批中 -->
    <select id="getReplyingCount" resultType="java.lang.Integer">
        select count(*) from ftb_attendance_clock_in_result where F_Id = #{clockInResultId} and F_DeleteMark = 0 and F_ApplyId is not null
    </select>
    <select id="selectUserClockInResult" resultType="jnpf.model.attendance.vo.ChangeInfoVo">
        select result.F_ClockInType                                          clockInType,
               if(result.F_ClockInType = 1, rule.F_InPoint, rule.F_OutPoint) shiftTime,
               result.F_Id                                                   clockInResultId,
               DATE_FORMAT(rule.F_Day, '%Y-%m-%d')                           `day`,
               if(result.F_Absence = 1, 4, result.F_ClockInStatus)           clockInStatus,
               clock.F_ClockInTime                                           clockInTime
        from ftb_attendance_clock_in_result result
                 inner join
             (select F_Id
                   , F_InStepOutType
                   , F_OutStepOutType
                   , F_InPoint
                   , F_OutPoint
                   , F_Day
              from ftb_attendance_daily_rule rule
              where rule.F_DeleteMark = 0
                and rule.F_UserId = #{userId}
                and rule.F_GroupId = #{groupId}
                and rule.F_AttendanceType = 1
                and rule.F_ApplyViewEnable in (1, 3, 9, 10)
                and rule.F_Day BETWEEN #{start} and #{end}) rule
             on result.F_RuleId = rule.F_Id
                 left join ftb_attendance_clock_in clock on result.F_ClockInId = clock.F_Id
        where result.F_ClockInStatus != -2 and result.F_ClockInKind = 1 and result.F_DeleteMark = 0 and if (result.F_ClockInType = 1, rule.F_InStepOutType, rule.F_OutStepOutType) = 0
        order by rule.F_Day DESC, result.F_ClockInType asc
    </select>
    <!-- 根据用户和日期查询打卡结果 -->
    <select id="getByUserAndDay" resultType="java.lang.String">
        select distinct F_RuleId ruleId from ftb_attendance_clock_in_result
        where F_UserId = #{userId}
        and DATE_FORMAT(F_CreatorTime,'%Y-%m-%d') = DATE_FORMAT(#{date},'%Y-%m-%d')
        and F_DeleteMark = 0
    </select>
<!--    获取用户打卡天数-->
    <select id="getClockInDayNum" resultType="java.lang.Integer">
        SELECT  COUNT(DISTINCT(dr.F_Day))
        FROM ftb_attendance_daily_rule dr
        LEFT JOIN ftb_attendance_clock_in_result r ON r.F_RuleId = dr.F_Id
        WHERE r.F_UserId = #{userId} AND r.F_ClockInStatus IN (1,2,3)
          AND r.F_DeleteMark =  0 AND dr.F_DeleteMark =  0
    </select>
<!--    获取用户迟到次数-->
    <select id="getLateClockInNum" resultType="java.lang.Integer">
        SELECT COUNT(DISTINCT (F_UserId))
        FROM ftb_attendance_clock_in_result
        WHERE F_DeleteMark = 0 AND F_ClockInStatus = 2
          AND F_RuleId IN
        <foreach collection="dayRuleIds" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <!-- 查询补卡审批中次数 -->
    <select id="selectRepairApplyCount" resultType="java.lang.Integer">
        select count(*) from ftb_attendance_clock_in_result ir
        left join ftb_attendance_daily_rule dr on ir.F_RuleId = dr.F_Id
        left join ftb_apply_attendance_repair r on r.F_Id = ir.F_ApplyId
        where ir.F_UserId = #{userId} and ir.F_DeleteMark = 0 and ir.F_ApplyId is not null and ir.F_ApplyType = 1
        and dr.F_GroupId = #{groupId}
        <![CDATA[
          and DATE_FORMAT(r.F_ApplyDate,'%Y-%m-%d') >= #{beginDate}
          and DATE_FORMAT(r.F_ApplyDate,'%Y-%m-%d') <= #{endDate}
        ]]>
    </select>
    <!-- 根据打卡ids查询打卡结果 -->
    <select id="selectBatchByClockInIds" resultType="jnpf.entity.attendance.AttendanceClockInResult">
        SELECT
            x.F_Id AS id,
            x.F_RuleId AS ruleId,
            x.F_UserId AS userId,
            x.F_ClockInId AS clockInId,
            x.F_EffectiveTime AS effectiveTime,
            x.F_ClockInType AS clockInType,
            x.F_ClockInStatus AS clockInStatus,
            x.F_ClockInKind AS clockInKind,
            x.F_Repaired AS repaired,
            x.F_ApplyType AS applyType,
            x.F_ApplyId AS applyId,
            x.F_Absence AS absence,
            x.F_AbsenceLeader AS absenceLeader,
            x.F_LastAbsenceLeader AS lastAbsenceLeader,
            x.F_RestMinute AS restMinute,
            x.F_AbnormalMinute AS abnormalMinute
        FROM (
            SELECT
                t.F_Id, t.F_RuleId, t.F_UserId, t.F_ClockInId, t.F_EffectiveTime, t.F_ClockInType, t.F_ClockInStatus, t.F_ClockInKind,
                t.F_Repaired, t.F_ApplyType, t.F_ApplyId, t.F_Absence, t.F_AbsenceLeader, t.F_LastAbsenceLeader, t.F_RestMinute, t.F_AbnormalMinute,
                ROW_NUMBER() OVER (PARTITION BY F_ClockInId ORDER BY F_CreatorTime DESC) rn
            FROM ftb_attendance_clock_in_result t
            WHERE t.F_DeleteMark = 0 and t.F_ClockInId IN
            <foreach collection="list" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        ) x
        WHERE x.rn = 1
    </select>
    <!-- 查询需要更新为缺勤的打卡结果 -->
    <select id="selectRuleIdsForAbsence" resultType="jnpf.model.attendance.vo.ConditionVo">
        SELECT DISTINCT r.F_Id resultId, r.F_RuleId ruleId
        FROM ftb_attendance_clock_in_result r
        LEFT JOIN ftb_attendance_daily_rule dr on r.F_RuleId = dr.F_Id
        WHERE
        r.F_ClockInStatus = -1
        AND r.F_DeleteMark = 0
        AND dr.F_AttendanceType != 4
        <if test="list != null and list.size() > 0">
            AND (F_RuleId, F_ClockInType) IN
            <foreach collection="list" item="c" separator="," open="(" close=")">
                (#{c.ruleId}, #{c.workType})
            </foreach>
        </if>
    </select>
</mapper>