<?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.StatisticsMapper">
    <!--查询用户/考勤组-排班、打卡、请假申请返回字段映射配置-->
    <resultMap type="jnpf.model.attendance.model.SituationData" id="UserActualAttendDataMap">
        <result property="groupId" column="F_GroupId"/>
        <result property="groupName" column="F_GroupName"/>
        <result property="userId" column="F_UserId"/>
        <result property="ruleId" column="F_Id"/>
        <result property="isAbsence" column="isAbsence"/>
        <result property="attendanceType" column="F_AttendanceType"/>
        <result property="applyViewEnable" column="F_ApplyViewEnable"/>
        <result property="selfGroup" column="F_SelfGroup"/>
        <result property="day" column="F_Day"/>
        <result property="breakStartPoint" column="F_BreakStartPoint"/>
        <result property="breakEndPoint" column="F_BreakEndPoint"/>
        <result property="inPoint" column="F_InPoint"/>
        <result property="outPoint" column="F_OutPoint"/>
        <result property="validDuration" column="F_ValidDuration"/>
        <result property="applyId" column="F_ApplyId"/>
        <result property="leaveApproveId" column="leaveApproveId"/>
        <result property="leaveTypeCode" column="F_LeaveTypeCode"/>
        <result property="leaveType" column="leaveType"/>
        <result property="unit" column="F_Unit"/>
        <result property="startTime" column="F_StartTime"/>
        <result property="endTime" column="F_EndTime"/>
        <result property="applicationDuration" column="F_ApplicationDurationSecond"/>
        <result property="unDeductedTime" column="F_UndeductedTimeSecond"/>
        <result property="shiftInvolved" column="F_ShiftInvolved"/>
        <result property="status" column="F_Status"/>
        <result property="outLackPoint" column="F_OutLackPoint"/>
        <!--主从关联查询配置，子查询数据以（b_、c_）命名可查询出结果列表-->
        <collection property="balanceRecordList" columnPrefix="b_"
                    resultMap="BalanceRecordMap">
        </collection>
        <collection property="clockInResultList" columnPrefix="c_"
                    resultMap="ClockInResultMap">
        </collection>
    </resultMap>
    <!--券余额使用记录映射配置-->
    <resultMap type="jnpf.model.attendance.model.BalanceRecord" id="BalanceRecordMap">
        <result property="unit" column="F_Unit"/>
        <result property="quota" column="F_Quota"/>
        <result property="paid" column="F_Paid"/>
        <result property="expireTime" column="F_Expire_Time"/>
    </resultMap>
    <!--打卡记录映射配置-->
    <resultMap type="jnpf.model.attendance.model.ClockInResult" id="ClockInResultMap">
        <result property="id" column="F_Id"/>
        <result property="ruleId" column="F_RuleId"/>
        <result property="userId" column="F_UserId"/>
        <result property="effectiveTime" column="F_EffectiveTime"/>
        <result property="clockInType" column="F_ClockInType"/>
        <result property="clockInStatus" column="F_ClockInStatus"/>
        <result property="clockInKind" column="F_ClockInKind"/>
        <result property="repaired" column="F_Repaired"/>
        <result property="approvalStatus" column="F_ApprovalStatus"/>
        <result property="clockInTime" column="F_ClockInTime"/>
        <result property="applyType" column="F_ApplyType"/>
        <result property="applyId" column="F_ApplyId"/>
        <result property="absence" column="F_Absence"/>
        <result property="absenceLeader" column="F_AbsenceLeader"/>
        <result property="restMinute" column="F_RestMinute"/>
        <result property="abnormalMinute" column="F_AbnormalMinute"/>
        <result property="deleteMark" column="F_DeleteMark"/>
    </resultMap>

    <resultMap type="jnpf.model.attendance.model.UserLeaveApprove" id="UserLeaveApproveDataMap">
        <result property="id" column="F_Id"/>
        <result property="userId" column="F_User_Id"/>
        <result property="unit" column="F_Unit"/>
        <result property="startTimeType" column="F_StartTimeType"/>
        <result property="endTimeType" column="F_EndTimeType"/>
        <result property="startTime" column="F_StartTime"/>
        <result property="endTime" column="F_EndTime"/>
        <result property="applicationDuration" column="F_ApplicationDurationSecond"/>
        <result property="unDeductedTime" column="F_UndeductedTimeSecond"/>
        <result property="applicationDurationDay" column="F_ApplicationDurationDay"/>
        <result property="undeductedTimeDay" column="F_UndeductedTimeDay"/>
        <result property="balanceStatus" column="F_Balance_Status"/>
        <result property="paid" column="F_Paid"/>
        <result property="noPaid" column="F_No_Paid"/>
        <result property="shiftInvolved" column="F_ShiftInvolved"/>
        <result property="leaveTypeCode" column="F_LeaveTypeCode"/>
        <result property="leaveTypeName" column="F_LeaveTypeName"/>
        <!--主从关联查询配置，子查询数据以（b_、c_）命名可查询出结果列表-->
        <collection property="balanceRecordList" columnPrefix="b_"
                    resultMap="BalanceRecordMap">
        </collection>
    </resultMap>
    <!--加班返回实体-->
    <resultMap type="jnpf.model.attendance.model.UserRuleRecord" id="UserRuleDataMap">
        <result property="groupId" column="F_GroupId"/>
        <result property="userId" column="F_UserId"/>
        <result property="day" column="F_Day"/>
        <result property="inPoint" column="F_InPoint"/>
        <result property="outPoint" column="F_OutPoint"/>
        <result property="attendanceType" column="F_AttendanceType"/>
        <result property="applyViewEnable" column="F_ApplyViewEnable"/>
        <result property="inStepOutType" column="F_InStepOutType"/>
        <result property="outStepOutType" column="F_OutStepOutType"/>
        <result property="selfGroup" column="F_SelfGroup"/>
        <!--主从关联查询配置，子查询数据以（b_、c_）命名可查询出结果列表-->
        <collection property="clockInResultList" columnPrefix="b_"
                    resultMap="UserClockInResultMap">
        </collection>
    </resultMap>
    <resultMap type="jnpf.model.attendance.model.ClockInResultRecordVo" id="UserClockInResultMap">
        <result property="id" column="F_Id"/>
        <result property="clockTime" column="F_ClockInTime"/>
        <result property="effectiveTime" column="F_EffectiveTime"/>
        <result property="abnormalSecond" column="F_AbnormalMinute"/>
        <result property="clockInType" column="F_ClockInType"/>
        <result property="clockInKind" column="F_ClockInKind"/>
        <result property="clockInStatus" column="F_ClockInStatus"/>
        <result property="approvalStatus" column="F_ApprovalStatus"/>
        <result property="repaired" column="F_Repaired"/>
        <result property="absence" column="F_Absence"/>
        <result property="absenceLeader" column="F_AbsenceLeader"/>
        <result property="deleteMark" column="F_DeleteMark"/>
    </resultMap>
    <!--web列表筛选条件查询-->
    <!--查询用户/考勤组-排班、打卡、请假申请信息筛选条件-->
    <sql id="USER_ACTUALATTEND_DATA_WHERE">
        <where>
            fadr.F_ApplyViewEnable in (1,3,9,10)
            AND fadr.F_DeleteMark = 0
            AND fag.F_DeleteMark = 0
            <if test="userIds!= null and userIds.size()>0">
                and fadr.F_UserId in
                <foreach collection="userIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="groupIds!= null and groupIds.size()>0">
                and fadr.F_GroupId in
                <foreach collection="groupIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="attendanceTypeList!= null and attendanceTypeList.size()>0">
                and fadr.F_AttendanceType not in
                <foreach collection="attendanceTypeList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="dateRangeDto.startDate != null and dateRangeDto.endDate != ''">
                AND fadr.F_Day BETWEEN #{dateRangeDto.startDate} AND #{dateRangeDto.endDate}
            </if>
        </where>
    </sql>

    <sql id="USER_ACTUALATTEND_DATA_FIELD">
        fadr.F_GroupId,
        fag.F_GroupName,
        fadr.F_Id,
        fadr.F_UserId,
        fadr.F_ApplyViewEnable,
        fadr.F_LateEnable,
        fadr.F_EarlyEnable,
        fadr.F_BreakEnable,
        fadr.F_SelfGroup,
        fadr.F_Day,
        fadr.F_BreakStartPoint,
        fadr.F_BreakEndPoint,
        fadr.F_InPoint,
        fadr.F_OutPoint,
        fadr.F_ValidDuration,
        fadr.F_ApplyId,
        fadr.F_DeleteMark,
        fala.F_Id leaveApproveId,
       	fals.F_Id F_LeaveTypeCode,
	    fals.F_Name F_LeaveTypeName,
        fals.F_Type leaveType,
        fala.F_Unit,
        fala.F_StartTime,
        fala.F_EndTime,
        fala.F_ApplicationDurationSecond,
        fala.F_UndeductedTimeSecond,
        fala.F_ShiftInvolved,
        fala.F_Status,
        fabur.F_Unit b_F_Unit,
        fabur.F_Quota b_F_Quota,
        fabr.F_Paid b_F_Paid,
        facir.F_Id c_F_Id,
        facir.F_RuleId c_F_RuleId,
        facir.F_UserId c_F_UserId,
        facir.F_EffectiveTime c_F_EffectiveTime,
        facir.F_ClockInType c_F_ClockInType,
        facir.F_ClockInStatus c_F_ClockInStatus,
        facir.F_ClockInKind c_F_ClockInKind,
        facir.F_Absence c_F_Absence,
        facir.F_Repaired c_F_Repaired,
        faci.F_ClockInTime c_F_ClockInTime,
        faci.F_ApprovalStatus c_F_ApprovalStatus,
        facir.F_ApplyType c_F_ApplyType,
        facir.F_ApplyId c_F_ApplyId,
        facir.F_RestMinute c_F_RestMinute,
        facir.F_AbnormalMinute c_F_AbnormalMinute,
        facir.F_AbsenceLeader c_F_AbsenceLeader,
        facir.F_DeleteMark c_F_DeleteMark
    </sql>
    <!-- 查询用户关联的考勤组信息 -->
    <select id="getUserAssociationGroupDataList"
            resultType="jnpf.model.attendance.model.UserAssociationGroupData">
        SELECT
        fagu.F_UserId userId,
        fagu.F_UserGroupType type,
        fagu.F_TimeJson timeJson,
        fagu.F_GroupId groupId,
        fagu.F_RemoveTime removeTime,
        fag.F_GroupName groupName
        FROM
        ftb_attendance_group_user fagu
        LEFT JOIN ftb_attendance_group fag ON fagu.F_GroupId = fag.F_Id
        <where>
            fag.F_DeleteMark = 0
            and fagu.F_DeleteMark = 0
            <if test="groupIds!= null and groupIds.size()>0">
                and fagu.F_GroupId in
                <foreach collection="groupIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="userIds!= null and userIds.size()>0">
                and fagu.F_UserId in
                <foreach collection="userIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            AND fagu.F_UserGroupType in(1,2)
        </where>
        GROUP BY fagu.F_UserId,fagu.F_UserGroupType
    </select>
    <!-- 查询被借调记录-->
    <select id="getUserSecondRecordList" resultType="jnpf.model.attendance.model.UserSecondRecord">
        SELECT
        F_Id id,
        F_UserId userId,
        F_GroupId groupId,
        F_TimeJson timeJson
        FROM ftb_attendance_group_user
        <where>
            F_UserGroupType = 2
            <if test="userIds!= null and userIds.size()>0">
                and F_UserId in
                <foreach collection="userIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="groupIds!= null and groupIds.size()>0">
                and F_GroupId in
                <foreach collection="groupIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY F_UserId
    </select>
    <!--获取用户打卡结果数据-->
    <select id="getUserClockInResultList" resultType="jnpf.model.attendance.model.ClockInResultRecord">
        select
        facir.F_Id id,
        fadr.F_Day day,
        fadr.F_GroupId groupId,
        fadr.F_UserId userId,
        faci.F_ClockInTime clockTime,
        facir.F_EffectiveTime effectiveTime,
        facir.F_AbnormalMinute abnormalSecond,
        facir.F_ClockInType clockInType,
        facir.F_ClockInKind clockInKind,
        IF(facir.F_ApplyId is null,0,1) approvalStatus,
        facir.F_Repaired repaired,
        fadr.F_InPoint inPoint,
        fadr.F_OutPoint outPoint,
        facir.F_Absence absence,
        facir.F_AbsenceLeader absenceLeader
        FROM ftb_attendance_daily_rule fadr
        LEFT JOIN ftb_attendance_clock_in_result facir ON facir.F_RuleId = fadr.F_Id
        LEFT JOIN ftb_attendance_clock_in faci ON faci.F_Id = facir.F_ClockInId
        <where>
            and fadr.F_DeleteMark = 0
            and facir.F_DeleteMark = 0
            <if test="attendanceTypeList!= null and attendanceTypeList.size()>0">
                and fadr.F_AttendanceType in
                <foreach collection="attendanceTypeList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="userIds!= null and userIds.size()>0">
                and fadr.F_UserId in
                <foreach collection="userIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="groupIds!= null and groupIds.size()>0">
                and fadr.F_GroupId in
                <foreach collection="groupIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="dateRangeDto.startDate != null and dateRangeDto.endDate != ''">
                AND fadr.F_Day BETWEEN #{dateRangeDto.startDate} AND #{dateRangeDto.endDate}
            </if>
            <if test="clockInStatus != null">
                and facir.F_ClockInStatus = #{clockInStatus}
            </if>
            <if test="absenceStatus != null">
                and facir.F_Absence = #{absenceStatus}
            </if>
            <if test="repairedStatus != null">
                and facir.F_Repaired = #{repairedStatus}
            </if>
            <if test="clockInKind != null">
                and facir.F_ClockInKind = #{clockInKind}
            </if>
        </where>
    </select>
    <!--获取用户加班数据-->
    <select id="getUserRuleRecordList" resultMap="UserRuleDataMap">
        select
        fadr.F_GroupId,
        fadr.F_UserId,
        fadr.F_Day,
        fadr.F_InPoint,
        fadr.F_OutPoint,
        fadr.F_AttendanceType,
        fadr.F_ApplyViewEnable,
        fadr.F_InStepOutType,
        fadr.F_OutStepOutType,
        fadr.F_SelfGroup,
        facir.F_Id b_F_Id,
        faci.F_ClockInTime b_F_ClockInTime,
        facir.F_EffectiveTime b_F_EffectiveTime,
        facir.F_AbnormalMinute b_F_AbnormalMinute,
        facir.F_ClockInType b_F_ClockInType,
        facir.F_ClockInKind b_F_ClockInKind,
        facir.F_ClockInStatus b_F_ClockInStatus,
        IF(facir.F_ApplyId is null,0,1) b_F_ApprovalStatus,
        facir.F_Repaired b_F_Repaired,
        facir.F_Absence b_F_Absence,
        facir.F_AbsenceLeader b_F_AbsenceLeader,
        facir.F_DeleteMark b_F_DeleteMark
        FROM ftb_attendance_daily_rule fadr
        LEFT JOIN ftb_attendance_clock_in_result facir ON facir.F_RuleId = fadr.F_Id
        LEFT JOIN ftb_attendance_clock_in faci ON faci.F_Id = facir.F_ClockInId
        <where>
            and fadr.F_DeleteMark = 0
            and fadr.F_ApplyViewEnable in (1,3,9,10)
            <if test="userIds!= null and userIds.size()>0">
                and fadr.F_UserId in
                <foreach collection="userIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="groupIdList!= null and groupIdList.size()>0">
                and fadr.F_GroupId in
                <foreach collection="groupIdList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="dateRangeDto.startDate != null and dateRangeDto.endDate != ''">
                AND fadr.F_Day BETWEEN #{dateRangeDto.startDate} AND #{dateRangeDto.endDate}
            </if>
            <if test="attendanceTypeList!= null and attendanceTypeList.size()>0">
                and fadr.F_AttendanceType in
                <foreach collection="attendanceTypeList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
<!--    获取用户指定考勤组时间范围内的排班数据-->
    <select id="getUserRuleList" resultType="jnpf.model.attendance.vo.attendance.UserRuleListVo">
        SELECT r.F_Id ruleId,r.F_GroupId groupId,r.F_SchedulesType schedulesType, r.F_PeriodId periodId,r.F_Day day, r.F_AttendanceType attendanceType,r.F_ShiftId shiftId, r.F_InPoint inPoint,r.F_OutPoint outPoint,r.F_UserId userId,r.F_ApplyUnit applyUnit,p.F_InPoint periodInPoint,p.F_OutPoint periodOutPoint,p.F_OutType periodOutType,goa.F_StartTime goOutStartTime,goa.F_EndTime goOutEndTime,r.F_ApplyViewEnable applyViewEnable ,r.F_InStepOutType inStepOutType ,r.F_OutStepOutType outStepOutType ,r.F_OutStepOutApplyId outStepOutApplyId,r.F_InStepOutApplyId inStepOutApplyId
        FROM ftb_attendance_daily_rule r
        LEFT JOIN ftb_attendance_shift_setting_period p ON r.F_PeriodId = p.F_Id
        LEFT JOIN ftb_attendance_go_out_approve goa ON r.F_ApplyId = goa.F_Id
        WHERE  r.F_Day BETWEEN #{dateRangeDto.startDate} AND  #{dateRangeDto.endDate}
          AND r.F_AttendanceType > 0 AND  r.F_ApplyViewEnable in (1,3,9,10)
        AND r.F_GroupId = #{groupId}
        <if test="userIds!= null and userIds.size()>0">
            AND r.F_UserId IN
            <foreach collection="userIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="dateRangeDto.startDate != null and dateRangeDto.endDate != ''">
            AND r.F_Day BETWEEN #{dateRangeDto.startDate} AND #{dateRangeDto.endDate}
        </if>
        <if test="code!= null and code.size()>0">
            and r.F_AttendanceType in
            <foreach collection="code" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        ORDER BY r.F_AttendanceType DESC
    </select>
</mapper>