<?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.personnels.mapper.FtbPersonnelsSecondmentManagementMapper">

    <select id="pageList" resultType="jnpf.model.personnels.vo.secondment.FtbPersonnelsSecondmentVO">
        select
        se.F_Id as id,
        se.F_UserId as userId,
        ro.F_Name as userName,
        ro.F_SystemWokerId as employeeId,
        se.F_SecondmentStartTime as secondmentStartTime,
        se.F_SecondmentEndTime as secondmentEndTime,
        se.F_AdvanceEndTime as advanceEndTime,
        se.F_SalarySetting as salarySetting,
        se.F_OriginalOrganizationId as originalOrganizationId,
        se.F_OriginalOrganization as originalOrganization,
        se.F_SecondedOrganizationId as secondedOrganizationId,
        se.F_SecondedOrganization as secondedOrganization,
        se.F_OriginalPositionId as originalPositionId,
        se.F_OriginalPosition as originalPosition,
        se.F_SecondedPositionId as secondedPositionId,
        se.F_SecondedPosition as secondedPosition,
        se.F_TransferStatus as transferStatus,
        se.F_TaskInfoId as taskInfoId,
        ro.F_WorkerStatus as workingCondition,
        ro.F_JoinNum as joinNum
        from ftb_personnels_secondment_management as se
        left join ftb_personnels_staff_roster as ro on se.F_UserId = ro.F_UserId
        <where>
            se.F_EnableMark = 0
            <if test="dto.keyWord != null and dto.keyWord != ''">
                and (
                ro.F_Name like concat('%',#{dto.keyWord},'%')
                )
            </if>
            <if test="dto.approvalStatus != null">
                and se.F_TransferStatus = #{dto.approvalStatus}
            </if>
            <if test="dto.beginTime != null and dto.endTime != null " >
                and (
                se.F_SecondmentStartTime between #{dto.beginTime} and #{dto.endTime}
                or
                se.F_SecondmentEndTime between #{dto.beginTime} and #{dto.endTime}
                )
            </if>
            <if test="dto.userId != null and  dto.userId != ''">
                and se.F_UserId  = #{dto.userId}
            </if>
            <if test="dto.orgIds != null and dto.orgIds.size() > 0">
                and se.F_SecondedOrganizationId in
                <foreach collection="dto.orgIds" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        order by se.F_CreatorTime desc
    </select>

    <select id="checkIsInTheProcess" resultType="int">
        SELECT COUNT(0)
        FROM ftb_personnels_secondment_management
        WHERE (
        (NOW() BETWEEN F_SecondmentStartTime AND F_SecondmentEndTime)
        OR (NOW() BETWEEN F_SecondmentStartTime AND F_AdvanceEndTime)
        )
        and F_UserId = #{userId}
        and F_TransferStatus in (2,6)
        and F_EnableMark = 0
    </select>

    <select id="checkTheSecondmentTime" resultType="int">
        SELECT
        COUNT(*)
        FROM
        ftb_personnels_secondment_management
        WHERE
        F_UserId = #{userId}
        AND F_EnableMark = 0
        AND (F_TransferStatus = 2 OR F_TransferStatus = 6)
        AND (
        -- 有交集的情况
        (F_SecondmentStartTime &lt;= #{secondedEndTime} AND F_SecondmentEndTime &gt; #{secondedStartTime} AND F_AdvanceEndTime is NULL)
        or
        (F_SecondmentStartTime &lt;= #{secondedEndTime} AND F_AdvanceEndTime &gt; #{secondedStartTime} AND F_AdvanceEndTime is NOT NULL)
        );
    </select>

    <select id="getUserGoOut" resultType="java.lang.Integer">
        select  count(0)
        from ftb_attendance_go_out_approve
        where  F_User_Id = #{userId}   AND F_Unit = 1
        <if test="startTime == null and endTime == null">
            AND F_Status in (0)
        </if>
        <if test="startTime != null and endTime != null">
            AND F_Status in (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')
            ]]>
    </if>

    </select>

    <select id="getUserGoOutForDay" resultType="java.lang.Integer">
        select  count(0)
        from ftb_attendance_go_out_approve
        where  F_User_Id = #{userId}  AND F_Unit = 2
        <if test="startTime == null and endTime == null">
            AND F_Status in (0)
        </if>
        <if test="startTime != null and endTime != null">
            AND F_Status in (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')
            ]]>
        </if>
    </select>

    <select id="getBusinessTrip" resultType="java.lang.Integer">
        select  count(0)
        from ftb_attendance_business_trip_approve
        where
        F_User_Id = #{userId}
        <if test="startTime == null and endTime == null">
            AND F_Status in (0)
        </if>
        <if test="startTime != null and endTime != null">
            AND F_Status in (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')
            ]]>
        </if>

    </select>

    <select id="getIdList" resultType="jnpf.model.personnels.po.FtbPersonnelsSecondmentManagement">
        SELECT
        F_Id AS id,
        F_UserId AS userId,
        F_UserName AS userName,
        F_EmployeeId AS employeeId,
        F_OriginalOrganizationId AS originalOrganizationId,
        F_OriginalOrganization AS originalOrganization,
        F_SecondedOrganizationId AS secondedOrganizationId,
        F_SecondedOrganization AS secondedOrganization,
        F_OriginalPositionId AS originalPositionId,
        F_OriginalPosition AS originalPosition,
        F_SecondedPositionId AS secondedPositionId,
        F_SecondedPosition AS secondedPosition,
        F_OriginalRankId AS originalRankId,
        F_OriginalRank AS originalRank,
        F_SecondedRankId AS secondedRankId,
        F_SecondedRank AS secondedRank,
        F_SecondmentStartTime AS secondmentStartTime,
        F_SecondmentEndTime AS secondmentEndTime,
        F_AdvanceEndTime AS advanceEndTime,
        F_SalarySetting AS salarySetting,
        F_IsResetSalary AS isResetSalary,
        F_PayrollSequenceId AS payrollSequenceId,
        F_PayrollSequenceName AS payrollSequenceName,
        F_SalaryType AS salaryType,
        F_SalaryStructureList AS salaryStructureList,
        F_SecondmentRemarks AS secondmentRemarks,
        F_Attachment AS attachment,
        F_EnableMark AS enableMark,
        F_TransferStatus AS transferStatus,
        F_TaskInfoId AS taskInfoId,
        F_DeleteMark AS deleteMark,
        F_DeleteTime AS deleteTime,
        F_DeleteUserId AS deleteUserId,
        F_LastModifyTime AS lastModifyTime,
        F_LastModifyUserId AS lastModifyUserId,
        F_CreatorTime AS creatorTime,
        F_CreatorUserId AS creatorUserId,
        F_TenantId AS tenantId
        FROM
        ftb_personnels_secondment_management
        WHERE
        (
          (  ( DATE_FORMAT(#{leaveTime}, '%Y-%m-%d') BETWEEN DATE_FORMAT(F_SecondmentStartTime, '%Y-%m-%d') AND DATE_FORMAT(F_SecondmentEndTime, '%Y-%m-%d') ) and F_AdvanceEndTime IS NULL)
          OR (( DATE_FORMAT(#{leaveTime}, '%Y-%m-%d') BETWEEN DATE_FORMAT(F_SecondmentStartTime, '%Y-%m-%d') AND DATE_FORMAT(F_AdvanceEndTime, '%Y-%m-%d') ) AND F_AdvanceEndTime IS NOT NULL )
        )

        AND (NOW() &lt; F_SecondmentEndTime OR NOW()  &lt; F_AdvanceEndTime)
        AND F_UserId = #{userId}
        AND F_TransferStatus IN ( 2, 6 )
        AND F_EnableMark = 0
    </select>

    <select id="getSecondmentRecord" resultType="jnpf.model.personnels.vo.secondment.FtbPersonnelsSecondmentVO">
        select
        se.F_Id as id,
        se.F_UserId as userId,
        ro.F_Name as userName,
        ro.F_SystemWokerId as employeeId,
        se.F_SecondmentStartTime as secondmentStartTime,
        se.F_SecondmentEndTime as secondmentEndTime,
        se.F_AdvanceEndTime as advanceEndTime,
        se.F_SalarySetting as salarySetting,
        se.F_OriginalOrganizationId as originalOrganizationId,
        se.F_OriginalOrganization as originalOrganization,
        se.F_SecondedOrganizationId as secondedOrganizationId,
        se.F_SecondedOrganization as secondedOrganization,
        se.F_OriginalPositionId as originalPositionId,
        se.F_OriginalPosition as originalPosition,
        se.F_SecondedPositionId as secondedPositionId,
        se.F_SecondedPosition as secondedPosition,
        se.F_TransferStatus as transferStatus,
        se.F_TaskInfoId as taskInfoId,
        ro.F_WorkerStatus as workingCondition,
        ro.F_JoinNum as joinNum
        from ftb_personnels_secondment_management as se
        left join ftb_personnels_staff_roster as ro on se.F_UserId = ro.F_UserId
        <where>
            se.F_EnableMark = 0
            and se.F_TransferStatus in (2,6)
            and se.F_UserId = #{userId}
            <if test="startLeaveTime != null and startLeaveTime != ''">
                and se.F_SecondmentStartTime   <![CDATA[>=]]> #{startLeaveTime}
            </if>
            <if test="endLeaveTime != null and endLeaveTime != ''">
                and se.F_SecondmentEndTime <![CDATA[<=]]> #{endLeaveTime}
            </if>

        </where>
        order by se.F_CreatorTime desc
    </select>

    <select id="getSecondmentRecordBath" resultType="jnpf.model.personnels.vo.secondment.FtbPersonnelsSecondmentVO">
        select
        se.F_Id as id,
        se.F_UserId as userId,
        ro.F_Name as userName,
        ro.F_SystemWokerId as employeeId,
        se.F_SecondmentStartTime as secondmentStartTime,
        se.F_SecondmentEndTime as secondmentEndTime,
        se.F_AdvanceEndTime as advanceEndTime,
        se.F_SalarySetting as salarySetting,
        se.F_OriginalOrganizationId as originalOrganizationId,
        se.F_OriginalOrganization as originalOrganization,
        se.F_SecondedOrganizationId as secondedOrganizationId,
        se.F_SecondedOrganization as secondedOrganization,
        se.F_OriginalPositionId as originalPositionId,
        se.F_OriginalPosition as originalPosition,
        se.F_SecondedPositionId as secondedPositionId,
        se.F_SecondedPosition as secondedPosition,
        se.F_TransferStatus as transferStatus,
        se.F_TaskInfoId as taskInfoId,
        ro.F_WorkerStatus as workingCondition,
        ro.F_JoinNum as joinNum
        from ftb_personnels_secondment_management as se
        left join ftb_personnels_staff_roster as ro on se.F_UserId = ro.F_UserId
        <where>
            <if test="dto.leaveFlag == false">
                se.F_EnableMark = 0
            </if>
            and se.F_TransferStatus in (1,2,6)
            and se.F_UserId in
            <if test="dto.userIds != null">
                <foreach item="item" collection="dto.userIds" separator="," close=")" index="index" open="(">
                    #{item}
                </foreach>
            </if>
            <if test="dto.startTime != null">
                and se.F_SecondmentStartTime >= #{dto.startTime}
            </if>
            <if test="dto.endTime != null">
                and se.F_SecondmentStartTime &lt;= #{dto.endTime}
            </if>

        </where>
        order by se.F_CreatorTime desc
    </select>

    <select id="checkSecondmentEnd" resultType="jnpf.model.personnels.po.FtbPersonnelsSecondmentManagement">
        select
        se.F_Id as id,
        se.F_UserId as userId,
        ro.F_Name as userName,
        ro.F_SystemWokerId as employeeId,
        se.F_SecondmentStartTime as secondmentStartTime,
        se.F_SecondmentEndTime as secondmentEndTime,
        se.F_AdvanceEndTime as advanceEndTime,
        se.F_SalarySetting as salarySetting,
        se.F_OriginalOrganizationId as originalOrganizationId,
        se.F_OriginalOrganization as originalOrganization,
        se.F_OriginalPositionId as originalPositionId,
        se.F_OriginalPosition as originalPosition,
        se.F_OriginalRankId as originalRankId,
        se.F_OriginalRank as originalRank,
        se.F_TransferStatus as transferStatus,
        se.F_TaskInfoId as taskInfoId,
        se.F_BeforeSalaryStructureList as beforeSalaryStructureList,
        se.F_PayrollSequenceId as payrollSequenceId,
        se.F_SalaryType as salaryType
        from ftb_personnels_secondment_management as se
        left join ftb_personnels_staff_roster as ro on se.F_UserId = ro.F_UserId
        <where>
            se.F_EnableMark = 0
            and se.F_TransferStatus in (2,6)
            and (
            (se.F_SecondmentEndTime &lt;= #{date} and F_AdvanceEndTime is null )
            or
            (se.F_AdvanceEndTime &lt;= #{date} and F_AdvanceEndTime is not null )
            )
            and se.F_IsResetSalary = 1
            and se.F_BeforeSalaryStructureList is not null
        </where>
        order by se.F_CreatorTime desc
    </select>

    <select id="queryListApproval" resultType="jnpf.model.attendance.vo.DailyApprovalVo">
        SELECT
        se.F_Id as id,
        se.F_TaskInfoId taskId,
        '借调申请' AS approvalName,
        CONCAT_WS(
        '~',
        DATE_FORMAT( se.F_SecondmentStartTime, '%Y-%m-%d %H:%i' ),
        DATE_FORMAT( se.F_SecondmentEndTime, '%Y-%m-%d %H:%i' )) AS applyTime,
        se.F_CreatorTime  AS submitTime,
        se.F_TransferStatus as lastResult,
        se.F_LastApprovalUserId as lastApprovalUserId,
        (select F_Name from ftb_personnels_staff_roster where F_UserId = se.F_LastApprovalUserId) as lastApprovalUserName,
        se.F_ApprovalTime AS approvalTime
        FROM
        `ftb_personnels_secondment_management` se
        left join ftb_personnels_staff_roster as ro on se.F_UserId = ro.F_UserId
        WHERE
        se.F_TransferStatus IN ( 1, 2, 3 )
        AND se.F_EnableMark = 0
        <if test="dto.userId != null and dto.userId != ''">
            and se.F_UserId  = #{dto.userId}
        </if>
        <if test="dto.orgId != null and dto.orgId != ''">
            and (se.F_OriginalOrganizationId = #{dto.orgId} or se.F_SecondedOrganizationId = #{dto.orgId})
        </if>
        <![CDATA[
          AND DATE_FORMAT(se.F_SecondmentStartTime,'%Y-%m-%d') <= DATE_FORMAT(#{dto.startTime},'%Y-%m-%d')
          AND DATE_FORMAT(se.F_SecondmentEndTime,'%Y-%m-%d') >= DATE_FORMAT(#{dto.endTime},'%Y-%m-%d')
        ]]>
        ORDER BY se.F_CreatorTime DESC

    </select>
</mapper>