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

  <sql id="Base_Column_List" >
      F_Id, F_User_Id, F_Type, F_StartTime, F_EndTime, F_ApplicationDuration, F_ReasonForLeave,
    F_Undeducted_Time, F_Balance_Status, F_Paid, F_No_Paid, f_flowtaskid, f_flowid, F_UniqueId, 
    F_TenantId
  </sql>
  <sql id="Blob_Column_List" >
    F_Picture
  </sql>

  <select id="getLeaveDetailById" resultType="jnpf.model.attendance.vo.AttendanceLeaveApproveVo">
    select     F_Id id, F_User_Id userId ,F_Type_Id typeId, F_Type type,F_Unit unit, F_StartTime startTime, F_EndTime endTime,  F_Balance_Status balanceStatus, F_Paid paid, F_No_Paid noPaid ,F_Status status,F_StartTimeType startTimeType,F_EndTimeType endTimeType,F_ShiftInvolved shiftInvolved
    from ftb_attendance_leave_approve
    where F_Id = #{id}
    </select>
    <!--  修改请假审批信息-->
  <update id="updateLeaveApprove">
    update ftb_attendance_leave_approve
    set F_Undeducted_Time = #{dayNum},
        F_ApplicationDuration = #{applicationDuration},
        F_Type = #{leaveName},
        F_Status = 1,
        F_ApplicationDurationSecond = #{applicationDurationSecond},
        F_UndeductedTimeSecond = #{undeductedTimeSecond},
        F_ApproveUserId = #{userId},
        F_ApproveUserName = #{userName},
        F_ApproveTime = now(),
        F_ApplicationDurationDay = #{applicationDurationDay},
        F_UndeductedTimeDay = #{notDeductedDay},
        F_StartTimeHit = #{start},
        F_EndTimeHit = #{end},
        F_ShiftInvolved = #{leaveDurationJson},
        F_BalanceJsonNew = #{leaveConsumptionDetailJson}
    where F_Id = #{id}
  </update>
  <!--  获取用户该类型的请假有多少未抵扣余额-->
  <select id="getUserUnDeductedTime" resultType="java.math.BigDecimal">
    SELECT  IFNULL(SUM(F_Undeducted_Time),0)
    FROM ftb_attendance_leave_approve
    WHERE F_Type_Id = #{leaveTypeId} AND F_User_Id = #{userId} and F_Status = 1
  </select>
<!--  获取用户时间段内审核通过的请假审批列表-->
  <select id="getUserLeaveByTimeSlot" resultType="jnpf.model.attendance.vo.AttendanceLeaveApproveVo">
    SELECT a.F_Id id, a.F_User_Id userId, a.F_Type type,a.F_Unit unit, a.F_StartTime startTime, a.F_EndTime endTime,
           a.F_Balance_Status balanceStatus, a.F_Paid paid, a.F_No_Paid noPaid , a.F_Status `status`,
           a.F_StartTimeType startTimeType,a.F_EndTimeType endTimeType
    FROM ftb_attendance_leave_approve a
    where  a.F_User_Id = #{userId} AND a.F_Status in (0,1)
		<![CDATA[
      AND DATE_FORMAT(a.F_StartTime,'%Y-%m-%d') <= DATE_FORMAT(#{endTime},'%Y-%m-%d')
      AND DATE_FORMAT(a.F_EndTime,'%Y-%m-%d') >= DATE_FORMAT(#{startTime},'%Y-%m-%d')
        ]]>
  </select>
<!--  <select id="getUserLeaveByTimeSlot" resultType="jnpf.model.attendance.vo.AttendanceLeaveApproveVo">-->
<!--    SELECT a.F_Id id, a.F_User_Id userId, a.F_Type type,a.F_Unit unit, a.F_StartTime startTime, a.F_EndTime endTime,  a.F_Balance_Status balanceStatus, a.F_Paid paid, a.F_No_Paid noPaid , a.F_Status `status`-->
<!--    FROM ftb_attendance_daily_rule r-->
<!--           LEFT JOIN ftb_attendance_leave_approve a ON r.F_ApplyId = a.F_Id AND F_AttendanceType = 3-->
<!--    where  a.F_User_Id = #{userId} AND a.F_Status = 1-->
<!--		<![CDATA[-->
<!--      AND DATE_FORMAT(a.F_StartTimeHit,'%Y-%m-%d %H:%i') < DATE_FORMAT(#{endTime},'%Y-%m-%d %H:%i')-->
<!--      AND DATE_FORMAT(a.F_EndTimeHit,'%Y-%m-%d %H:%i') > DATE_FORMAT(#{startTime},'%Y-%m-%d %H:%i')-->
<!--        ]]>-->
<!--    UNION ALL-->
<!--    select   F_Id id, F_User_Id userId, F_Type type,F_Unit unit, F_StartTime startTime, F_EndTime endTime,  F_Balance_Status balanceStatus, F_Paid paid, F_No_Paid noPaid , F_Status `status`-->
<!--    from ftb_attendance_leave_approve-->
<!--    where  F_User_Id = #{userId} AND F_Status = 0-->
<!--      <![CDATA[-->
<!--      AND DATE_FORMAT(F_StartTimeHit,'%Y-%m-%d %H:%i') < DATE_FORMAT(#{endTime},'%Y-%m-%d %H:%i')-->
<!--      AND DATE_FORMAT(F_EndTimeHit,'%Y-%m-%d %H:%i') > DATE_FORMAT(#{startTime},'%Y-%m-%d %H:%i')-->
<!--    ]]>-->
<!--  </select>-->
<!--  获取指定加班审批详情-->
  <select id="getWorkOverTime" resultType="jnpf.model.attendance.vo.AttendanceWorkOverTimeVo">
    select F_Id id,
      F_User_Id userId,
      F_WorkDay workDay,
      F_StartTime startTime,
      F_EndTime endTime,
      F_Reason workOvertimeReason,
      F_ShiftInvolved shiftInvolved,
      F_Status status
    from ftb_attendance_work_overtime_approve
    where F_Id = #{id}
  </select>
  <!--  修改加班审批的审批状态-->
  <update id="updateWorkOverTimeApprove">
    update ftb_attendance_work_overtime_approve
    set F_Status = #{status},
        F_ApproveUserId = #{userId},
        F_ApproveUserName = #{userName},
        F_ApproveTime = now(),
        F_ShiftInvolved = #{json}
    where F_Id = #{id}
  </update>

  <select id="getSelfApprove" resultType="jnpf.model.attendance.vo.AttendanceSelfApproveVo">
    select F_Id id,
      F_Group_Id groupId,
      F_Group_Name groupName,
      F_Self_Group_Id selfGroupId,
      F_Self_Group_Name selfGroupName,
      F_Start_Time startTime,
      F_End_Time endTime,
      F_Departure_Time departureTime,
      F_Back_Time BackTime,
      F_Status status,
      F_CreatorUserId creatorUserId
    from ftb_attendance_self_approve
    where F_Id = #{id}
  </select>
<!--  获取借调用户集合-->
  <select id="getUserList" resultType="java.lang.String">
    select  F_User_Id
    from ftb_attendance_self_approve_user
    where F_Self_Id = #{id}
  </select>
<!--  获取用户时间段内加班申请数量-->
  <select id="getUserWorkByTimeSlot" resultType="java.lang.Integer">
    select  count(0)
    from ftb_attendance_work_overtime_approve
    where  F_User_Id = #{userId} AND F_Status in (0,1)
		<![CDATA[
      AND DATE_FORMAT(F_StartTime,'%Y-%m-%d %H:%i') < DATE_FORMAT(#{endTime},'%Y-%m-%d %H:%i')
      AND DATE_FORMAT(F_EndTime,'%Y-%m-%d %H:%i') > DATE_FORMAT(#{startTime},'%Y-%m-%d %H:%i')
     ]]>
  </select>
<!--  获取外出审批详情-->
  <select id="getGoOut" resultType="jnpf.model.attendance.vo.attendance.GoOutVo">
    SELECT F_Id id,F_User_Id  userId,F_StartTime  startTime,F_EndTime  endTime,F_DayNum dayNum,F_Reason reason,F_Unit unit
    FROM ftb_attendance_go_out_approve
    WHERE F_Id = #{applyId}
  </select>
<!--  获取出差审批详情-->
  <select id="getBusinessTrip" resultType="jnpf.model.attendance.vo.attendance.BusinessTripVo">
    SELECT F_Id id,F_User_Id  userId,F_Departure  departure,F_Destination  destination,F_StartTime startTime,F_EndTime endTime,F_DayNum dayNum,F_Transportation_Vehicles  transportationVehicles,F_Reason reason
    FROM ftb_attendance_business_trip_approve
    WHERE F_Id = #{applyId}
  </select>
<!--  获取用户时间段内审核通过的请假审批列表-->
  <select id="getUserPassLeaveByTime" resultType="jnpf.model.attendance.vo.AttendanceLeaveApproveVo">
    select   F_Id id, F_User_Id userId, F_Type type,F_Unit unit, F_StartTime startTime, F_EndTime endTime,  F_Balance_Status balanceStatus, F_Paid paid, F_No_Paid noPaid , F_Status `status`
    from ftb_attendance_leave_approve
    where  F_User_Id = #{userId} AND F_Status = 1
      <![CDATA[
        AND DATE_FORMAT(F_StartTime,'%Y-%m-%d %H:%i') < DATE_FORMAT(#{endTime},'%Y-%m-%d %H:%i')
      AND DATE_FORMAT(F_EndTime,'%Y-%m-%d %H:%i') > DATE_FORMAT(#{startTime},'%Y-%m-%d %H:%i')
    ]]>
  </select>
    <select id="getBatchLeaveByUserIds" resultType="jnpf.model.attendance.vo.LeaveStaVo">
        select
        fala.F_User_Id userId,
        fals.F_Id leaveId,
        fals.F_Name leaveName,
        fala.F_StartTime startTime,
        fala.F_EndTime endTime,
        fala.F_BalanceJsonNew leaveJson
        from ftb_attendance_leave_approve fala
        left join ftb_attendance_leave_rules fals ON fala.F_Type_Id = fals.F_Id
        where F_Status = 1 and fala.F_BalanceJsonNew is not null
      <![CDATA[
          AND DATE_FORMAT(F_StartTime,'%Y-%m-%d %H:%i') < DATE_FORMAT(#{endTime},'%Y-%m-%d %H:%i')
          AND DATE_FORMAT(F_EndTime,'%Y-%m-%d %H:%i') > DATE_FORMAT(#{startTime},'%Y-%m-%d %H:%i')
        ]]>
        AND F_User_Id in
        <foreach collection="userIds" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        group by fala.F_User_Id,fala.F_Id
    </select>

  <select id="getUserPassWorkByTime" resultType="java.lang.Integer">
    select  count(0)
    from ftb_attendance_work_overtime_approve
    where  F_User_Id = #{userId} AND F_Status = 1
		<![CDATA[
      AND DATE_FORMAT(F_StartTime,'%Y-%m-%d %H:%i') < DATE_FORMAT(#{endTime},'%Y-%m-%d %H:%i')
      AND DATE_FORMAT(F_EndTime,'%Y-%m-%d %H:%i') > DATE_FORMAT(#{startTime},'%Y-%m-%d %H:%i')
    ]]>
  </select>
  <!--  修改借调审批为通过-->
  <update id="updateSelfApprove">
    update ftb_attendance_self_approve
    set F_Status = #{status} ,
        F_Departure_Time = #{departureTime},
        F_Back_Time = #{backTime},
        F_ApproveUserId = #{userId},
        F_ApproveUserName = #{userName},
        F_ApproveTime = now()
    where F_Id = #{id}
  </update>
<!--  将对应审批变为未通过-->
  <update id="updateLeaveApproveStatus">
    update ftb_attendance_leave_approve
    set F_Status = #{status},
        F_ApproveUserId = #{userId},
        F_ApproveUserName = #{userName},
        F_ApproveTime = now()
    where F_Id = #{id}
  </update>
<!--  修改外出审批状态-->
    <update id="updateGoOutApprove">
      update ftb_attendance_go_out_approve
      set F_Status = #{status},
          F_ApproveUserId = #{userId},
          F_ApproveUserName = #{userName},
          F_ApproveTime = now()
      where F_Id = #{applyId}
    </update>
  <!--  修改出差审批状态-->
  <update id="updateBusinessTripApprove">
    update ftb_attendance_business_trip_approve
    set F_Status = #{status},
        F_ApproveUserId = #{userId},
        F_ApproveUserName = #{userName},
        F_ApproveTime = now()
    where F_Id = #{applyId}
  </update>
    <select id="getUserLeaveList" resultType="jnpf.model.attendance.model.LeaveSituationData">
        select fala.F_Id             applyId,
               fala.F_User_Id        userId,
               fala.F_Unit           unit,
               fala.F_StartTime      startTime,
               fala.F_StartTimeType  startTimeType,
               fala.F_EndTime        endTime,
               fala.F_EndTimeType    endTimeType,
               fals.F_Id             leaveId,
               fals.F_Name           leaveName,
               fals.F_BuiltIn        isRest,
               fala.F_ShiftInvolved  shiftInvolved,
               fala.F_BalanceJsonNew balanceJsonNew
        from ftb_attendance_leave_approve fala
                 left join ftb_attendance_leave_type fals ON fala.F_Type_Id = fals.F_Id
        where fala.F_Status = 1
          and fala.F_GroupId = #{groupId}
          and fala.F_User_Id = #{userId}
    <![CDATA[
          and DATE_FORMAT(fala.F_StartTime, '%Y-%m-%d %H:%i') <= DATE_FORMAT(#{endDate}, '%Y-%m-%d %H:%i')
          and DATE_FORMAT(fala.F_EndTime, '%Y-%m-%d %H:%i') >= DATE_FORMAT(#{startDate}, '%Y-%m-%d %H:%i')
        ]]>
    </select>
    <select id="getUserLeaveListByApplyIds" resultType="jnpf.model.attendance.model.LeaveSituationData">
        select
        fala.F_Id applyId,
        fala.F_GroupId groupId,
        fala.F_User_Id userId,
        fala.F_Unit unit,
        fala.F_StartTime startTime,
        fala.F_EndTime endTime,
        fals.F_Id leaveId,
        fals.F_Name leaveName,
        fals.F_BuiltIn isRest,
        fala.F_ShiftInvolved shiftInvolved,
        fala.F_BalanceJsonNew balanceJsonNew
        from ftb_attendance_leave_approve fala
        left join ftb_attendance_leave_type fals ON fala.F_Type_Id = fals.F_Id
        where fala.F_Status = 1 and fala.F_Id in
        <foreach collection="applyIds" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
<!--    获取指定用户指定日期的加班申请列表-->
  <select id="getApplyWorkOverTimeList" resultType="jnpf.model.attendance.vo.DailyApprovalVo">
    SELECT  DISTINCT a.F_Id taskId, '加班申请' as approvalName ,CONCAT_WS('~',DATE_FORMAT(a.F_StartTime,'%Y-%m-%d %H:%i'),DATE_FORMAT(a.F_EndTime,'%Y-%m-%d %H:%i')) AS applyTime , a.F_CreatorTime as submitTime,a.F_ApproveTime as approvalTime,a.F_ApproveUserId lastApprovalUserId,a.F_ApproveUserName lastApprovalUserName,a.F_Status lastResult ,a.F_Reason reason
    FROM ftb_attendance_work_overtime_approve a
    LEFT JOIN ftb_attendance_daily_rule r ON a.F_Id = r.F_ApplyId AND r.F_AttendanceType = #{code}
    WHERE a.F_User_Id = #{userId}  AND a.F_Status !=3 AND r.F_GroupId = #{currentGroupId}
      <![CDATA[
       AND DATE_FORMAT(a.F_StartTime,'%Y-%m-%d') <= DATE_FORMAT(#{queryDate},'%Y-%m-%d')
       AND DATE_FORMAT(a.F_EndTime,'%Y-%m-%d') >= DATE_FORMAT(#{queryDate},'%Y-%m-%d')
    ]]>
  </select>
<!--  获取指定用户指定日期的出差申请列表-->
  <select id="getApplyGoOutList" resultType="jnpf.model.attendance.vo.DailyApprovalVo">
    SELECT  DISTINCT a.F_Id taskId,  '外出申请' as approvalName ,
                     IF(a.F_Unit = 1 ,CONCAT_WS('~',DATE_FORMAT(a.F_StartTime,'%Y-%m-%d %H:%i'),DATE_FORMAT(a.F_EndTime,'%Y-%m-%d %H:%i')),
                                                                    if(a.F_Unit = 2 ,CONCAT_WS('~',DATE_FORMAT(a.F_StartTime,'%Y-%m-%d'),DATE_FORMAT(a.F_EndTime,'%Y-%m-%d')),
                                                                       CONCAT_WS('~',DATE_FORMAT(a.F_StartTime,'%Y-%m-%d'),DATE_FORMAT(a.F_EndTime,'%Y-%m-%d'))
                                                                    )) AS applyTime  ,
        a.F_CreatorTime as submitTime,a.F_ApproveTime as approvalTime,a.F_ApproveUserId lastApprovalUserId,a.F_ApproveUserName lastApprovalUserName,a.F_Status lastResult  ,a.F_Reason reason
    FROM ftb_attendance_go_out_approve a
    LEFT JOIN ftb_attendance_daily_rule r ON a.F_Id = r.F_ApplyId AND r.F_AttendanceType = #{code}
    WHERE a.F_User_Id = #{userId}  AND a.F_Status !=3 AND r.F_GroupId = #{currentGroupId}
      <![CDATA[
        AND DATE_FORMAT(a.F_StartTime,'%Y-%m-%d') <= DATE_FORMAT(#{queryDate},'%Y-%m-%d')
        AND DATE_FORMAT(a.F_EndTime,'%Y-%m-%d') >= DATE_FORMAT(#{queryDate},'%Y-%m-%d')
    ]]>
  </select>
<!--  获取指定用户指定日期的请假申请列表-->
  <select id="getApplyLeaveList" resultType="jnpf.model.attendance.vo.DailyApprovalVo">
    SELECT   DISTINCT a.F_Id taskId, '请假申请' as approvalName ,
        CASE
            WHEN a.F_Unit = 1 THEN CONCAT_WS('~', DATE_FORMAT(a.F_StartTime, '%Y-%m-%d %H:%i'), DATE_FORMAT(a.F_EndTime, '%Y-%m-%d %H:%i'))
            WHEN a.F_Unit = 2 THEN CONCAT_WS('~', DATE_FORMAT(a.F_StartTime, '%Y-%m-%d'), DATE_FORMAT(a.F_EndTime, '%Y-%m-%d'))
            ELSE CONCAT_WS('~',
                IF(a.F_StartTimeType = 1, CONCAT_WS(' ', DATE_FORMAT(a.F_StartTime, '%Y-%m-%d'), '上半天'), CONCAT_WS(' ', DATE_FORMAT(a.F_StartTime, '%Y-%m-%d'), '下半天')),
                IF(a.F_EndTimeType = 1, CONCAT_WS(' ', DATE_FORMAT(a.F_EndTime, '%Y-%m-%d'), '上半天'), CONCAT_WS(' ', DATE_FORMAT(a.F_EndTime, '%Y-%m-%d'), '下半天'))
            )
        END AS applyTime,
        a.F_CreatorTime as submitTime,a.F_ApproveTime as approvalTime,a.F_ApproveUserId lastApprovalUserId,a.F_ApproveUserName lastApprovalUserName,a.F_Status lastResult ,a.F_ReasonForLeave reason
    FROM ftb_attendance_leave_approve a
    LEFT JOIN ftb_attendance_daily_rule r ON a.F_Id = r.F_ApplyId AND r.F_AttendanceType = #{code}
    WHERE a.F_User_Id = #{userId}  AND a.F_Status !=3 AND r.F_GroupId = #{currentGroupId}
      <![CDATA[
      AND DATE_FORMAT(a.F_StartTime,'%Y-%m-%d') <= DATE_FORMAT(#{queryDate},'%Y-%m-%d')
      AND DATE_FORMAT(a.F_EndTime,'%Y-%m-%d') >= DATE_FORMAT(#{queryDate},'%Y-%m-%d')
    ]]>
  </select>
<!--  获取指定用户指定日期的借调申请列表-->
  <select id="getApplySelfList" resultType="jnpf.model.attendance.vo.DailyApprovalVo">
    SELECT DISTINCT sa.F_Id taskId, '借调申请' as approvalName ,CONCAT_WS('~',DATE_FORMAT(F_Start_Time,'%Y-%m-%d %H:%i'),DATE_FORMAT(F_End_Time,'%Y-%m-%d %H:%i')) AS applyTime , F_CreatorTime as submitTime,F_ApproveTime as approvalTime,F_ApproveUserId lastApprovalUserId,F_ApproveUserName lastApprovalUserName,F_Status lastResult
    FROM ftb_attendance_self_approve sa
    LEFT JOIN ftb_attendance_self_approve_user sau ON sau.F_Self_Id = sa.F_Id
    WHERE sau.F_User_Id = #{userId} AND sa.F_Status !=3 AND (sa.F_Group_Id = #{currentGroupId} OR sa.F_Self_Group_Id = #{currentGroupId})
     <![CDATA[
      AND DATE_FORMAT(F_Start_Time,'%Y-%m-%d') <= DATE_FORMAT(#{queryDate},'%Y-%m-%d')
      AND DATE_FORMAT(F_End_Time,'%Y-%m-%d') >= DATE_FORMAT(#{queryDate},'%Y-%m-%d')
    ]]>
  </select>
<!--  获取指定用户指定日期的出差申请列表-->
  <select id="getApplyBusinessTripList" resultType="jnpf.model.attendance.vo.DailyApprovalVo">
    SELECT   DISTINCT a.F_Id taskId, '出差申请' as approvalName , CONCAT_WS('~',DATE_FORMAT(a.F_StartTime,'%Y-%m-%d'),DATE_FORMAT(a.F_EndTime,'%Y-%m-%d')) AS applyTime , a.F_CreatorTime as submitTime,a.F_ApproveTime as approvalTime,a.F_ApproveUserId lastApprovalUserId,a.F_ApproveUserName lastApprovalUserName,a.F_Status lastResult  ,a.F_Reason reason
    FROM ftb_attendance_business_trip_approve a
    LEFT JOIN ftb_attendance_daily_rule r ON a.F_Id = r.F_ApplyId AND r.F_AttendanceType = #{code}
    WHERE a.F_User_Id = #{userId} AND a.F_Status !=3 AND r.F_GroupId = #{currentGroupId}
        <![CDATA[
      AND DATE_FORMAT(a.F_StartTime,'%Y-%m-%d') <= DATE_FORMAT(#{queryDate},'%Y-%m-%d')
      AND DATE_FORMAT(a.F_EndTime,'%Y-%m-%d') >= DATE_FORMAT(#{queryDate},'%Y-%m-%d')
    ]]>
  </select>
<!--  获取指定用户指定日期的外出申请数量-->
  <select id="getUserGoOutByTimeSlot" resultType="java.lang.Integer">
    select  count(0)
    from ftb_attendance_go_out_approve
    where  F_User_Id = #{userId} AND F_Status = 1
		<![CDATA[
      AND DATE_FORMAT(F_StartTime,'%Y-%m-%d %H:%i') < DATE_FORMAT(#{endTime},'%Y-%m-%d %H:%i')
      AND DATE_FORMAT(F_EndTime,'%Y-%m-%d %H:%i') > DATE_FORMAT(#{startTime},'%Y-%m-%d %H:%i')
    ]]>
  </select>
<!--  获取指定用户指定日期的出差申请数量-->
  <select id="getUserBusinessTripByTimeSlot" resultType="java.lang.Integer">
    select  count(0)
    from ftb_attendance_business_trip_approve
    where  F_User_Id = #{userId} AND F_Status in (0,1)
		<![CDATA[
      AND DATE_FORMAT(F_StartTime,'%Y-%m-%d %H:%i') < DATE_FORMAT(#{endTime},'%Y-%m-%d %H:%i')
      AND DATE_FORMAT(F_EndTime,'%Y-%m-%d %H:%i') > DATE_FORMAT(#{startTime},'%Y-%m-%d %H:%i')
    ]]>
  </select>
<!--  获取指定用户指定日期的申请中的外出申请列表-->
  <select id="getApplyingGoOutList" resultType="jnpf.model.attendance.vo.DailyApprovalVo">
    SELECT  DISTINCT a.F_Id,  '外出申请' as approvalName ,
        IF(a.F_Unit = 1 ,CONCAT_WS('~',DATE_FORMAT(a.F_StartTime,'%Y-%m-%d %H:%i'),DATE_FORMAT(a.F_EndTime,'%Y-%m-%d %H:%i')),
            if(a.F_Unit = 2 ,CONCAT_WS('~',DATE_FORMAT(a.F_StartTime,'%Y-%m-%d'),DATE_FORMAT(a.F_EndTime,'%Y-%m-%d')),
                CONCAT_WS('~',DATE_FORMAT(a.F_StartTime,'%Y-%m-%d'),DATE_FORMAT(a.F_EndTime,'%Y-%m-%d'))
            )
        ) AS applyTime  ,
        a.F_CreatorTime as submitTime,a.F_ApproveTime as approvalTime,a.F_ApproveUserId lastApprovalUserId,a.F_ApproveUserName lastApprovalUserName,a.F_Status lastResult ,a.F_Unit unit  ,a.F_Reason reason
    FROM ftb_attendance_go_out_approve a

    WHERE a.F_User_Id = #{userId}  AND (a.F_Status =0 OR a.F_Status =2)
      <![CDATA[
      AND DATE_FORMAT(a.F_StartTime,'%Y-%m-%d') <= DATE_FORMAT(#{queryDate},'%Y-%m-%d')
      AND DATE_FORMAT(a.F_EndTime,'%Y-%m-%d') >= DATE_FORMAT(#{queryDate},'%Y-%m-%d')
    ]]>
  </select>
<!--  获取指定用户指定日期的申请中的请假申请列表-->
  <select id="getApplyingLeaveList" resultType="jnpf.model.attendance.vo.DailyApprovalVo">
    SELECT   DISTINCT a.F_Id taskId, '请假申请' as approvalName ,
        CASE
            WHEN a.F_Unit = 1 THEN CONCAT_WS('~', DATE_FORMAT(a.F_StartTime, '%Y-%m-%d %H:%i'), DATE_FORMAT(a.F_EndTime, '%Y-%m-%d %H:%i'))
            WHEN a.F_Unit = 2 THEN CONCAT_WS('~', DATE_FORMAT(a.F_StartTime, '%Y-%m-%d'), DATE_FORMAT(a.F_EndTime, '%Y-%m-%d'))
            ELSE CONCAT_WS('~',
                IF(a.F_StartTimeType = 1, CONCAT_WS(' ', DATE_FORMAT(a.F_StartTime, '%Y-%m-%d'), '上半天'), CONCAT_WS(' ', DATE_FORMAT(a.F_StartTime, '%Y-%m-%d'), '下半天')),
                IF(a.F_EndTimeType = 1, CONCAT_WS(' ', DATE_FORMAT(a.F_EndTime, '%Y-%m-%d'), '上半天'), CONCAT_WS(' ', DATE_FORMAT(a.F_EndTime, '%Y-%m-%d'), '下半天'))
                )
            END AS applyTime,
    a.F_CreatorTime as submitTime,a.F_ApproveTime as approvalTime,a.F_ApproveUserId lastApprovalUserId,a.F_ApproveUserName lastApprovalUserName,a.F_Status lastResult ,a.F_ReasonForLeave reason
    FROM ftb_attendance_leave_approve a
    WHERE a.F_User_Id = #{userId}  AND (a.F_Status =0 OR a.F_Status =2)
      <![CDATA[
      AND DATE_FORMAT(a.F_StartTime,'%Y-%m-%d') <= DATE_FORMAT(#{queryDate},'%Y-%m-%d')
      AND DATE_FORMAT(a.F_EndTime,'%Y-%m-%d') >= DATE_FORMAT(#{queryDate},'%Y-%m-%d')
    ]]>
  </select>
<!--  获取指定用户指定日期的申请中的出差申请列表-->
  <select id="getApplyingBusinessTripList" resultType="jnpf.model.attendance.vo.DailyApprovalVo">
    SELECT   DISTINCT a.F_Id taskId, '出差申请' as approvalName , CONCAT_WS('~',DATE_FORMAT(a.F_StartTime,'%Y-%m-%d'),DATE_FORMAT(a.F_EndTime,'%Y-%m-%d')) AS applyTime , a.F_CreatorTime as submitTime,a.F_ApproveTime as approvalTime,a.F_ApproveUserId lastApprovalUserId,a.F_ApproveUserName lastApprovalUserName,a.F_Status lastResult ,a.F_Reason reason
    FROM ftb_attendance_business_trip_approve a
    WHERE a.F_User_Id = #{userId} AND (a.F_Status =0 OR a.F_Status =2)
        <![CDATA[
      AND DATE_FORMAT(a.F_StartTime,'%Y-%m-%d') <= DATE_FORMAT(#{queryDate},'%Y-%m-%d')
      AND DATE_FORMAT(a.F_EndTime,'%Y-%m-%d') >= DATE_FORMAT(#{queryDate},'%Y-%m-%d')
    ]]>
  </select>
<!--  获取指定用户指定日期的申请中的加班申请列表-->
  <select id="getApplyingWorkOverTimeList" resultType="jnpf.model.attendance.vo.DailyApprovalVo">
    SELECT  DISTINCT a.F_Id taskId, '加班申请' as approvalName ,CONCAT_WS('~',DATE_FORMAT(a.F_StartTime,'%Y-%m-%d %H:%i'),DATE_FORMAT(a.F_EndTime,'%Y-%m-%d %H:%i')) AS applyTime , a.F_CreatorTime as submitTime,a.F_ApproveTime as approvalTime,a.F_ApproveUserId lastApprovalUserId,a.F_ApproveUserName lastApprovalUserName,a.F_Status lastResult  ,a.F_Reason reason
    FROM ftb_attendance_work_overtime_approve a
    WHERE a.F_User_Id = #{userId}  AND (a.F_Status =0 OR a.F_Status =2)
      <![CDATA[
      AND DATE_FORMAT(a.F_StartTime,'%Y-%m-%d') <= DATE_FORMAT(#{queryDate},'%Y-%m-%d')
      AND DATE_FORMAT(a.F_EndTime,'%Y-%m-%d') >= DATE_FORMAT(#{queryDate},'%Y-%m-%d')
    ]]>
  </select>
<!--  获取已审批的借调记录-->
  <select id="getSelfApproveList" resultType="jnpf.model.attendance.vo.AttendanceSelfApproveVo">
    select F_Id id,
           F_Group_Id groupId,
           F_Group_Name groupName,
           F_Self_Group_Id selfGroupId,
           F_Self_Group_Name selfGroupName,
           F_Start_Time startTime,
           F_End_Time endTime,
           F_Departure_Time departureTime,
           F_Back_Time BackTime,
           F_Status status,
           F_CreatorUserId creatorUserId
    from ftb_attendance_self_approve
    where
      F_Start_Time >= #{startTime} AND F_Start_Time  <![CDATA[<=]]> #{endTime} and F_Status = 1
  </select>
<!--  获取指定用户指定日期的出差申请数量除开本次申请-->
  <select id="getBusinessTripForOa" resultType="java.lang.Integer">
    select  count(0)
    from ftb_attendance_business_trip_approve
    where  F_User_Id = #{userId} AND F_Status in (0,1) AND F_Id != #{taskId}
		<![CDATA[
      AND DATE_FORMAT(F_StartTime,'%Y-%m-%d %H:%i') <= DATE_FORMAT(#{endTime},'%Y-%m-%d %H:%i')
      AND DATE_FORMAT(F_EndTime,'%Y-%m-%d %H:%i') >= DATE_FORMAT(#{startTime},'%Y-%m-%d %H:%i')
    ]]>
  </select>
<!--  获取指定用户指定日期的外出申请数量除开本次申请-->
  <select id="getUserGoOutForOa" resultType="java.lang.Integer">
    select  count(0)
    from ftb_attendance_go_out_approve
    where  F_User_Id = #{userId} AND F_Status in (0,1) AND F_Id != #{taskId} AND F_Unit = 1
		<![CDATA[
      AND DATE_FORMAT(F_StartTime,'%Y-%m-%d %H:%i') <= DATE_FORMAT(#{endTime},'%Y-%m-%d %H:%i')
      AND DATE_FORMAT(F_EndTime,'%Y-%m-%d %H:%i') >= DATE_FORMAT(#{startTime},'%Y-%m-%d %H:%i')
    ]]>
  </select>
  <select id="getUserWorkByTimeSlotForOa" resultType="java.lang.Integer">
    select  count(0)
    from ftb_attendance_work_overtime_approve
    where  F_User_Id = #{userId} AND F_Status in (0,1) AND F_Id != #{taskId}
		<![CDATA[
      AND DATE_FORMAT(F_StartTime,'%Y-%m-%d %H:%i') <= DATE_FORMAT(#{endTime},'%Y-%m-%d %H:%i')
      AND DATE_FORMAT(F_EndTime,'%Y-%m-%d %H:%i') >= DATE_FORMAT(#{startTime},'%Y-%m-%d %H:%i')
    ]]>
  </select>
    <select id="getUserGoOutForDay" resultType="java.lang.Integer">
        select  count(0)
        from ftb_attendance_go_out_approve
        where  F_User_Id = #{userId} AND F_Status in (0,1)  AND F_Id != #{taskId} AND F_Unit = 2
		<![CDATA[
          AND DATE_FORMAT(F_StartTime,'%Y-%m-%d') <= DATE_FORMAT(#{endTime},'%Y-%m-%d')
          AND DATE_FORMAT(F_EndTime,'%Y-%m-%d') >= DATE_FORMAT(#{startTime},'%Y-%m-%d')
        ]]>
    </select>
    <select id="checkSelfApprove" resultType="java.lang.String">
        select sau.F_User_Id
        from ftb_attendance_self_approve sa
        LEFT JOIN ftb_attendance_self_approve_user sau ON sau.F_Self_Id = sa.F_Id
        where F_Status = 0
        <if test="taskId != null">
            AND sa.F_Id != #{taskId}
        </if>
        AND sau.F_User_Id IN
        <foreach item="item" collection="userIds" separator="," close=")" open="(" index="">
            #{item}
        </foreach>
        <![CDATA[
          AND DATE_FORMAT(sa.F_Start_Time,'%Y-%m-%d %H:%i') <= DATE_FORMAT(#{endTime},'%Y-%m-%d %H:%i')
          AND DATE_FORMAT(sa.F_End_Time,'%Y-%m-%d %H:%i') >= DATE_FORMAT(#{startTime},'%Y-%m-%d %H:%i')
        ]]>
    </select>
</mapper>