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

    <sql id="Base_Column_List">
        select a.F_Id              as id,
               F_Phone             as phone,
               F_WorkerName        as workerName,
               F_CurrOrg           as currOrg,
               F_CurrPosition      as currPosition,
               F_CurrRank          as currRank,
               F_ContractType      as contractType,
               F_ExpectedStartDate as expectedStartDate,
               F_SourceRelation    as sourceRelation,
               F_SourceRelationId as sourceRelationId,
               F_Source            as source,
               F_IsSubmitForm      as isSubmitForm,
               F_Status            as status,
               F_EntryMoney        as entryMoney,
               F_JoinNum           as joinNum,
               F_IsNeedCheck       as isNeedCheck,
               F_CheckStatus       as checkStatus,
               F_Files             as files,
               F_EnabledMark       as enabledMark,
               F_CreatorUserId     as createrUserId,
               F_CreatorTime       as creatorTime,
               F_LastModifyUserId  as lastModifyUserId,
               F_LastModifyTime    as lastModifyTime,
               F_OpUser            as opUser,
               F_OpUserName        as opUserName,
               F_TenantId          as tenantId,
               F_AttendanceGroup   as attendanceGroup,
               F_TaskInfoId        as taskInfoId,
               F_VersionNum versionNum
        from ftb_personnels_staff_employment_apply as a
    </sql>
    <select id="expectPagingQuery"
            resultType="jnpf.model.personnels.dto.staff.employment.FtbPersonnelsStaffEmploymentApplyDto">
        select a.F_Id              as id,
        a.F_Phone             as phone,
        a.F_WorkerName        as workerName,
        a.F_CurrOrg           as currOrg,
        a.F_CurrPosition      as currPosition,
        a.F_CurrRank          as currRank,
        a.F_ContractType      as contractType,
        a.F_ExpectedStartDate as expectedStartDate,
        a.F_SourceRelation    as sourceRelation,
        a.F_SourceRelationId as sourceRelationId,
        a.F_Source            as source,
        a.F_IsSubmitForm      as isSubmitForm,
        a.F_Status            as status,
        a.F_EntryMoney        as entryMoney,
        a.F_JoinNum           as joinNum,
        a.F_IsNeedCheck       as isNeedCheck,
        a.F_CheckStatus       as checkStatus,
        a.F_Files             as files,
        a.F_EnabledMark       as enabledMark,
        a.F_CreatorUserId     as createrUserId,
        a.F_CreatorTime       as creatorTime,
        a.F_LastModifyUserId  as lastModifyUserId,
        a.F_LastModifyTime    as lastModifyTime,
        a.F_OpUser            as opUser,
        a.F_OpUserName        as opUserName,
        a.F_TenantId          as tenantId,
        a.F_AttendanceGroup   as attendanceGroup,
        a.F_TaskInfoId        as taskInfoId,
        <if test="params.ext != null and  params.ext == true">
            (
            SELECT
            da.F_Value
            FROM
            ftb_personnels_staff_registration_form_data da
            <where>
                da.F_FormFieldId = ( SELECT F_Id FROM ftb_personnels_registration_form_field WHERE F_Name = '意向组织'  AND F_Status =0 AND F_EnabledMark = 0 )
                AND da.F_Phone = a.F_Phone
                <if test="params.intentToOrganizeIds != null and  params.intentToOrganizeIds.size() > 0 ">
                    and da.F_Value in
                    <foreach collection="params.intentToOrganizeIds" item="item" open="("  separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </where>
            ) AS intentionOrgId,
        </if>
        a.F_VersionNum versionNum
        from ftb_personnels_staff_employment_apply as a
        <where>
            a.F_EnabledMark = 0
            and (a.F_IsNeedCheck =1 or ( a.F_IsNeedCheck = 0 and a.F_CheckStatus =2) )
            and a.F_Status != 1
            <if test="params.keyWord!= null and params.keyWord!= ''">
                and (
                a.F_Phone like CONCAT('%',#{params.keyWord},'%')
                or a.F_WorkerName like CONCAT('%',#{params.keyWord},'%')
                or a.F_SourceRelation like CONCAT('%',#{params.keyWord},'%')
                )
            </if>
            <if test="params.isSubmitForm!= null">
                and a.F_IsSubmitForm =#{params.isSubmitForm}
            </if>
            <if test="params.source!= null">
                and a.F_Source =#{params.source}
            </if>

            <if test="params.innerSelectOrgList != null and params.innerSelectOrgList.size() > 0 ">
                and a.F_CurrOrg in
                <foreach collection="params.innerSelectOrgList" item="item" open="("  separator="," close=")">
                    #{item}
                </foreach>
            </if>

            <if test="params.currPosition!= null and params.currPosition!= ''">
                and a.F_CurrPosition =#{params.currPosition}
            </if>

            <if test="params.currRank!= null and params.currRank!= ''">
                and a.F_CurrRank =#{params.currRank}
            </if>

            <if test="params.startExpectedStartDate!= null and params.endExpectedStartDate!= null">
                and a.F_ExpectedStartDate between #{params.startExpectedStartDate} and #{params.endExpectedStartDate}
            </if>
        </where>
        order by a.F_CreatorTime desc

    </select>

    <select id="checkPagingQuery"
            resultType="jnpf.model.personnels.dto.staff.employment.FtbPersonnelsStaffEmploymentApplyDto">
        <include refid="Base_Column_List"></include>

        <where>
            a.F_EnabledMark = 0 and a.F_IsNeedCheck = 0
            <if test="params.keyWord!= null and params.keyWord!= ''">
                and (
                a.F_Phone like CONCAT('%',#{params.keyWord},'%')
                or a.F_WorkerName like CONCAT('%',#{params.keyWord},'%')
                or a.F_SourceRelation like CONCAT('%',#{params.keyWord},'%')
                )
            </if>
            <if test="params.isSubmitForm!= null">
                and a.F_IsSubmitForm =#{params.isSubmitForm}
            </if>
            <if test="params.source!= null">
                and a.F_Source =#{params.source}
            </if>

            <if test="params.innerSelectOrgList != null and params.innerSelectOrgList.size() > 0 ">
                and a.F_CurrOrg in
                <foreach collection="params.innerSelectOrgList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>

            <if test="params.currPosition!= null and params.currPosition!= ''">
                and a.F_CurrPosition =#{params.currPosition}
            </if>

            <if test="params.currRank!= null and params.currRank!= ''">
                and a.F_CurrRank =#{params.currRank}
            </if>

            <if test="params.startExpectedStartDate!= null and params.endExpectedStartDate!= null">
                and a.F_ExpectedStartDate between #{params.startExpectedStartDate} and #{params.endExpectedStartDate}
            </if>


            <if test="params.checkStatus!= null">
                and a.F_CheckStatus =#{params.checkStatus}
            </if>
            <if test="params.checkStatus== null">
                and a.F_CheckStatus!=2
            </if>

        </where>
        order by a.F_CreatorTime desc

    </select>
    <select id="expectAppPagingQuery"
            resultType="jnpf.model.personnels.dto.staff.employment.FtbPersonnelsStaffEmploymentApplyDto">
        <include refid="Base_Column_List"></include>
        <where>
            a.F_EnabledMark = 0
            and (a.F_IsNeedCheck =1 or ( a.F_IsNeedCheck = 0 and a.F_CheckStatus =2) )
            <if test="params.keyWord!= null and params.keyWord!= ''">
                and (
                a.F_Phone like CONCAT('%',#{params.keyWord},'%')
                or a.F_WorkerName like CONCAT('%',#{params.keyWord},'%')
                or a.F_SourceRelation like CONCAT('%',#{params.keyWord},'%')
                )
            </if>
            <if test="params.isSubmitForm!= null and params.isSubmitForm!= -1 ">
                and a.F_IsSubmitForm =#{params.isSubmitForm}
            </if>
            <if test="params.status!= null and params.status ==1">
                and a.F_Status =0
            </if>
            <if test="params.status!= null and params.status ==2">
                and a.F_Status =2
            </if>

            <if test="params.innerSelectOrgList != null and params.innerSelectOrgList.size() > 0 ">
                and a.F_CurrOrg in
                <foreach collection="params.innerSelectOrgList" item="item" open="("  separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        order by a.F_CreatorTime desc

    </select>
    <select id="myCheckPagingQuery"
            resultType="jnpf.model.personnels.dto.staff.employment.FtbPersonnelsStaffEmploymentApplyDto">
        <include refid="Base_Column_List"></include>

        <where>
            a.F_EnabledMark = 0
            <if test="params.keyWord!= null and params.keyWord!= ''">
                and (
                a.F_Phone like CONCAT('%',#{params.keyWord},'%')
                or a.F_WorkerName like CONCAT('%',#{params.keyWord},'%')
                or a.F_SourceRelation like CONCAT('%',#{params.keyWord},'%')
                )
            </if>
            <if test="params.isSubmitForm!= null and params.isSubmitForm!= -1 ">
                and a.F_IsSubmitForm =#{params.isSubmitForm}
            </if>
            <if test="params.status!= null and params.status ==1">
                and a.F_Status =0
            </if>
            <if test="params.status!= null and params.status ==2">
                and a.F_Status =2
            </if>

        </where>
        order by a.F_CreatorTime desc
    </select>
    <select id="getListByRegularForApp"
            resultType="jnpf.model.personnels.dto.staff.employment.FtbPersonnelsStaffEmploymentApplyDto">
        SELECT a.F_Id               AS id,
        F_Phone              AS phone,
        F_WorkerName         AS workerName,
        F_CurrOrg            AS currOrg,
        F_CurrPosition       AS currPosition,
        F_CurrRank           AS currRank,
        F_ContractType       AS contractType,
        F_ExpectedStartDate  AS expectedStartDate,
        F_SourceRelation     AS sourceRelation,
        F_SourceRelationId   AS sourceRelationId,
        F_Source             AS source,
        F_IsSubmitForm       AS isSubmitForm,
        F_Status             AS STATUS,
        F_EntryMoney         AS entryMoney,
        F_JoinNum            AS joinNum,
        F_IsNeedCheck        AS isNeedCheck,
        F_CheckStatus        AS checkStatus,
        F_OpUser             as opUser,
        F_OpUserName         as opUserName,
        F_Files              AS files,
        a.F_EnabledMark      AS enabledMark,
        a.F_CreatorUserId    AS createrUserId,
        a.F_CreatorTime      AS creatorTime,
        a.F_LastModifyUserId AS lastModifyUserId,
        a.F_LastModifyTime   AS lastModifyTime,
        a.F_TenantId         AS tenantId,
        info.F_ApprovalResult   approvalResult,
        info.F_ApprovalOpinion,
        a.F_TaskInfoId       as taskInfoId,
        run.F_ExcuseStatus   as excuseStatus
        FROM ftb_personnels_staff_employment_apply AS a
        left JOIN ftb_personnels_audit_run_task AS run ON run.F_BusinessId = a.F_Id
        left JOIN ftb_personnels_audit_task_info AS info ON run.F_Id = info.F_RunTaskId
        INNER JOIN ftb_personnels_audit_master_config AS mas
        ON mas.F_Id = run.F_MasterAuditConfigId

        WHERE (
        <if test="checkUserId!= null and checkUserId!= ''">
            info.F_UserId = #{checkUserId}
        </if>
        OR info.F_UserId = #{checkUserId}
        )
        AND a.F_CheckStatus != 2

        AND a.F_IsNeedCheck = 0

        and mas.F_ConfigType = #{configType}
        order by  a.F_CreatorTime desc,a.F_CheckStatus asc
    </select>
    <select id="getListForApp"
            resultType="jnpf.model.personnels.dto.staff.employment.FtbPersonnelsStaffEmploymentApplyDto">
        select DISTINCT e.* from (
        select a.F_Id as id,
        F_Phone as phone,
        F_WorkerName as workerName,
        F_CurrOrg as currOrg,
        F_CurrPosition as currPosition,
        F_CurrRank as currRank,
        F_ContractType as contractType,
        F_ExpectedStartDate as expectedStartDate,
        F_SourceRelation as sourceRelation,
        F_Source as source,
        F_IsSubmitForm as isSubmitForm,
        F_Status as status,
        F_EntryMoney as entryMoney,
        F_JoinNum as joinNum,
        F_IsNeedCheck as isNeedCheck,
        F_CheckStatus as checkStatus,
        F_OpUser            as opUser,
        F_OpUserName        as opUserName,
        F_Files as files,
        a.F_EnabledMark       as enabledMark,
        a.F_CreatorUserId     as createrUserId,
        a.F_CreatorTime       as creatorTime,
        a.F_LastModifyUserId  as lastModifyUserId,
        a.F_LastModifyTime    as lastModifyTime,
        a.F_TenantId          as tenantId,
        a.F_TaskInfoId as taskInfoId
        from ftb_personnels_staff_employment_apply as a
        <where>
            a.F_IsNeedCheck = 0
            <if test="checkStatus!= null and checkStatus!= '' and checkStatus == 1">
                AND F_CheckStatus = 1
            </if>
            <if test="checkStatus!= null and checkStatus!= '' and checkStatus == 2">
                AND F_CheckStatus = 2
            </if>
            <if test="checkStatus!= null and checkStatus!= '' and checkStatus == 3">
                AND F_CheckStatus = 3
            </if>
        </where>
        ) e
        LEFT JOIN ftb_personnels_audit_run_task AS task ON e.taskInfoId = task.F_Id
        LEFT JOIN ftb_personnels_audit_run_task_history AS his ON e.taskInfoId = his.F_Id
        INNER JOIN (
        SELECT
        config.F_Id
        FROM
        ftb_personnels_audit_carbon_recipient AS co
        INNER JOIN ftb_personnels_audit_master_config AS config ON config.F_Id = co.F_MasterConfig
        WHERE
        config.F_ConfigType = 1
        AND co.F_UserId = #{checkUserId}
        ) d ON (
        task.F_MasterAuditConfigId = d.F_Id
        OR his.F_MasterAuditConfigId = d.F_Id
        )
        order by e.creatorTime desc
    </select>
    <select id="pagingQueryWebMyCheckList"
            resultType="jnpf.model.personnels.dto.staff.employment.FtbPersonnelsStaffEmploymentApplyDto">
        SELECT
        a.F_Id AS id,
        F_Phone AS phone,
        F_WorkerName AS workerName,
        F_CurrOrg AS currOrg,
        F_CurrPosition AS currPosition,
        F_CurrRank AS currRank,
        F_ContractType AS contractType,
        F_ExpectedStartDate AS expectedStartDate,
        F_SourceRelation AS sourceRelation,
        F_SourceRelationId AS sourceRelationId,
        F_Source AS source,
        F_IsSubmitForm AS isSubmitForm,
        F_Status AS STATUS,
        F_EntryMoney AS entryMoney,
        F_JoinNum AS joinNum,
        F_IsNeedCheck AS isNeedCheck,
        F_CheckStatus AS checkStatus,
        F_OpUser AS opUser,
        F_OpUserName AS opUserName,
        F_Files AS files,
        a.F_EnabledMark AS enabledMark,
        a.F_CreatorUserId AS createrUserId,
        a.F_CreatorTime AS creatorTime,
        a.F_LastModifyUserId AS lastModifyUserId,
        a.F_LastModifyTime AS lastModifyTime,
        a.F_TenantId AS F_TenantId,
        a.F_TaskInfoId as taskInfoId
        FROM
        ftb_personnels_staff_employment_apply AS a
        LEFT JOIN ftb_personnels_audit_run_task AS run ON run.F_BusinessId = a.F_Id
        INNER JOIN ftb_personnels_audit_master_config as conf ON run.F_MasterAuditConfigId = conf.F_Id
        WHERE
        a.F_EnabledMark = 0
        AND a.F_IsNeedCheck = 0
        AND run.F_UserId = #{userId}
        AND conf.F_ConfigType =#{configType}
        ORDER BY
        a.F_CreatorTime DESC
    </select>

    <select id="getApprovedList"
            resultType="jnpf.model.personnels.dto.staff.employment.FtbPersonnelsStaffEmploymentApplyDto">
        SELECT
        a.F_Id AS id,
        F_Phone AS phone,
        F_WorkerName AS workerName,
        F_CurrOrg AS currOrg,
        F_CurrPosition AS currPosition,
        F_CurrRank AS currRank,
        F_ContractType AS contractType,
        F_ExpectedStartDate AS expectedStartDate,
        F_SourceRelation AS sourceRelation,
        F_SourceRelationId AS sourceRelationId,
        F_Source AS source,
        F_IsSubmitForm AS isSubmitForm,
        F_Status AS STATUS,
        F_EntryMoney AS entryMoney,
        F_JoinNum AS joinNum,
        F_IsNeedCheck AS isNeedCheck,
        F_CheckStatus AS checkStatus,
        F_OpUser AS opUser,
        F_OpUserName AS opUserName,
        info.F_ApprovalResult approvalResult,
        F_Files AS files,
        a.F_EnabledMark AS enabledMark,
        a.F_CreatorTime AS creatorTime,
        a.F_LastModifyUserId AS lastModifyUserId,
        a.F_LastModifyTime AS lastModifyTime,
        a.F_TaskInfoId AS taskInfoId
        FROM
        ftb_personnels_staff_employment_apply AS a
        LEFT JOIN ftb_personnels_audit_run_task AS run ON run.F_BusinessId = a.F_Id
        LEFT JOIN ftb_personnels_audit_task_info AS info ON run.F_Id = info.F_RunTaskId
        INNER JOIN ftb_personnels_audit_master_config as conf ON run.F_MasterAuditConfigId = conf.F_Id
        WHERE
        a.F_EnabledMark = 0
        AND a.F_IsNeedCheck = 0
        AND info.F_UserId =  #{userId}
        <if test="approvalStatus !=null and  approvalStatus !='' and approvalStatus == 7">
            and a.F_CheckStatus in (1,2,3)
        </if>
        <if test="approvalStatus !=null and  approvalStatus !='' and approvalStatus == 2">
            and a.F_CheckStatus = 2
        </if>
        <if test="approvalStatus !=null and  approvalStatus !='' and approvalStatus == 3">
            and a.F_CheckStatus = 3
        </if>
        <if test="approvalStatus !=null and  approvalStatus !='' and approvalStatus == 1">
            and a.F_CheckStatus = 1
        </if>
        AND conf.F_ConfigType =#{configType}
        ORDER BY
        a.F_CreatorTime DESC
    </select>
    <!--  not ( F_IsNeedCheck = 1 or (F_IsNeedCheck = 0 and F_CheckStatus = 2) ) -->
    <select id="onboardingList" resultType="jnpf.model.personnels.dto.staff.employment.FtbPersonnelsEmployApplyDto">
        select
        a.F_Id AS id,
        a.F_WorkerName AS workerName,
        a.F_Phone AS phone,
        a.F_CreatorTime AS creationTime,
        a.F_Source AS source,
        a.F_CheckStatus AS checkStatus,
        a.F_IsNeedCheck as isNeedCheck,
        a.F_TaskInfoId as taskInfoId,
        <if test="req.ext != null and  req.ext == true">
            (
            SELECT
            ta.F_Value
            FROM
            ftb_personnels_staff_registration_form_data ta
            <where>
                ta.F_FormFieldId = ( SELECT F_Id FROM ftb_personnels_registration_form_field WHERE F_Name = '意向组织' AND F_Status =0 AND F_EnabledMark = 0  )
                AND ta.F_Phone = a.F_Phone
            </where>
            ) AS intentionOrgId,
        </if>
        a.F_VersionNum versionNum
        from ftb_personnels_staff_employment_apply as a
        <if test="req.ext != null and  req.ext == true">
            <if test="req.intentToOrganizeIds != null and  req.intentToOrganizeIds.size() > 0 ">
                left join   ftb_personnels_staff_registration_form_data da on da.F_Phone = a.F_Phone
            </if>
        </if>
        <where>
            <if test="req.keyWord != null and req.keyWord !=''">
                and   (
                a.F_WorkerName LIKE CONCAT('%',#{req.keyWord},'%')
                or
                a.F_Phone LIKE CONCAT('%',#{req.keyWord},'%')
                )
            </if>
            <if test="req.startExpectedStartDate != null and req.endExpectedStartDate != null">
                and a.F_CreatorTime BETWEEN #{req.startExpectedStartDate} AND #{req.endExpectedStartDate}
            </if>
            <if test="req.checkStatus != null">
                and a.F_CheckStatus = #{req.checkStatus}
            </if>
            <if  test="req.isNeedCheck != null">
                and a.F_IsNeedCheck = #{req.isNeedCheck}
            </if>
            <if test="req.source != null ">
                and a.F_Source = #{req.source}
            </if>
            <if test="req.ids != null and req.ids.size() > 0">
                and a.F_Id in
                <foreach collection="req.ids" item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
            </if>
            <if test="req.ext != null and  req.ext == true">
                <if test="req.intentToOrganizeIds != null and  req.intentToOrganizeIds.size() > 0 ">
                    and da.F_FormFieldId = ( SELECT F_Id FROM ftb_personnels_registration_form_field WHERE F_Name = '意向组织'  AND F_Status =0 AND F_EnabledMark = 0  )
                    and da.F_Value in
                    <foreach collection="req.intentToOrganizeIds" item="item" open="("  separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
        order by a.F_CreatorTime desc
    </select>

    <select id="checkPhoneForEmploymentApply" resultType="java.lang.Integer">
        select
        count(*)
        from ftb_personnels_staff_employment_apply where F_Phone = #{phone} and F_EnabledMark = 0
    </select>

    <select id="pageLists" resultType="jnpf.model.personnels.vo.emp.FtbEmpEntryVO">
        SELECT
        a.F_Id as id,
        a.F_WorkerName as workerName,
        a.F_UserId as userId,
        IFNULL( a.F_SystemUserId,ros.F_SystemWokerId) as systemId,
        a.F_CurrOrg as currOrgId,
        a.F_CurrOrgName as currOrgName,
        a.F_CurrPosition as currPositionId,
        a.F_CurrPositionName as currPositionName,
        a.F_CurrRank as currRankID,
        a.F_CurrRankName as currRankName,
        a.F_CurrGroupId as currShopId,
        a.F_CurrGroupName as currShopName,
        a.F_Phone as phone,
        a.F_IdCard as idCard,
        a.F_IsSubmitForm as isSubmitForm,
        IFNULL(a.F_ActualStartDate,ros.F_ActualStartDate) as actualStartDateDate,
        a.F_IsNeedCheck as isNeedCheck,
        a.F_CheckStatus as checkStatus,
        a.F_CreatorTime as creatorTime,
        a.F_TaskInfoId as taskInfoId,
        a.F_JoinNum as joinNum
        FROM ftb_personnels_staff_employment_apply as a
        left join  ftb_personnels_staff_roster as ros on (a.F_Phone = ros.F_Phone and (a.F_IsNeedCheck = 1 OR a.F_CheckStatus = 2))
        <where>
            <if test="dto.keyWords != null and dto.keyWords !=''">
                and   (
                a.F_WorkerName LIKE CONCAT('%',#{dto.keyWords},'%')
                or
                a.F_Phone LIKE CONCAT('%',#{dto.keyWords},'%')
                or
                a.F_IdCard LIKE CONCAT('%',#{dto.keyWords},'%')
                <if test="dto.phoneNumbers != null and dto.phoneNumbers.size() > 0">
                    or
                    a.F_Phone in
                    <foreach collection="dto.phoneNumbers" item="phone" separator="," open="(" close=")">
                        #{phone}
                    </foreach>
                </if>
                )
            </if>
            <if test="dto.startDate != null and dto.endDate != null">
                and ros.F_ActualStartDate BETWEEN #{dto.startDate} AND #{dto.endDate}
            </if>
            <if test="dto.orgIds != null and dto.orgIds.size() > 0">
                and a.F_CurrOrg in
                <foreach collection="dto.orgIds" item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
            </if>
            <if test="dto.states != null and dto.states.size() > 0 or dto.isManually != null and dto.isManually == true">
                AND (
                <if test="dto.states != null and dto.states.size() > 0">
                    (a.F_IsNeedCheck = 0 AND a.F_CheckStatus IN
                    <foreach collection="dto.states" item="state" separator="," open="(" close=")">
                        #{state}
                    </foreach>)
                    <if test="dto.isManually != null and dto.isManually == true">
                        OR
                    </if>
                </if>
                <if test="dto.isManually != null and dto.isManually == true">
                    (a.F_IsNeedCheck = 1)
                </if>
                )
            </if>
            <if test="dto.userIds !=null and  dto.userIds.size() > 0">
                and a.F_UserId in
                <foreach collection="dto.userIds" item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
            </if>
            <if test="dto.phone != null and dto.phone !=''">
                and a.F_Phone = #{dto.phone}
            </if>
            <if test="dto.ids != null and dto.ids.size() > 0 ">
                and a.F_Id in
                <foreach collection="dto.ids" item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
            </if>
        </where>
        order by a.F_CreatorTime desc
    </select>

    <select id="getOnboardingPostDimension" resultType="jnpf.model.personnels.po.FtbPersonnelsStaffEmploymentApply">
        select
        a.*
        from
        ( SELECT
        ap.F_Id AS id,
        ap.F_UserId AS userId,
        ap.F_WorkerName AS workerName,
        ap.F_Phone AS phone,
        da.F_Value AS actualStartDate,
        ap.F_IsNeedCheck AS isNeedCheck,
        ap.F_CheckStatus AS checkStatus,
        ap.F_Source AS source,
        ap.F_VersionNum AS versionNum,
        ap.F_TaskInfoId AS taskInfoId,
        ap.F_CurrOrg AS currOrg,
        ap.F_CurrPosition AS currPosition,
        ap.F_CurrRank AS currRank,
        ap.F_CurrGroupId AS currGroupId
        FROM
        ftb_personnels_staff_employment_apply AS ap
        INNER JOIN `ftb_personnels_staff_registration_form_data` AS da ON ap.F_Phone = da.F_Phone
        LEFT JOIN ftb_personnels_staff_roster AS ros ON ( ap.F_Phone = ros.F_Phone )
        <where>
            (
            ap.F_IsNeedCheck = 1
            OR ( ap.F_IsNeedCheck = 0 AND ap.F_CheckStatus = 2 ))
            AND ap.F_EnabledMark = 0
            AND da.`F_FormFieldId` = 'actualStartDate'
            AND da.F_Value != ''
            AND (
            (
            ros.F_UserId IN
            <foreach collection="userIds" open="(" close=")" separator="," item="userId">
                #{userId}
            </foreach>
            )
            OR
            (
            ap.F_UserId IN
            <foreach collection="userIds" open="(" close=")" separator="," item="userId">
                #{userId}
            </foreach>
            )
            )
        </where>
        ) a
        <where>
            DATE_FORMAT(a.actualStartDate, '%Y-%m') between #{startDate} and #{endDate}
        </where>
    </select>

    <select id="onboardingListOA" resultType="jnpf.model.personnels.vo.emp.FtbEmpEntryVO">
        SELECT
        a.F_Id as id,
        a.F_WorkerName as workerName,
        a.F_UserId as userId,
        a.F_SystemUserId as systemId,
        a.F_CurrOrg as currOrgId,
        a.F_CurrPosition as currPositionId,
        a.F_CurrRank as currRankID,
        a.F_Phone as phone,
        a.F_IdCard as idCard,
        a.F_IsSubmitForm as isSubmitForm,
        a.F_IsNeedCheck as isNeedCheck,
        a.F_CheckStatus as checkStatus,
        a.F_CreatorTime as creatorTime
        FROM ftb_personnels_staff_employment_apply as a
        <where>
            <if test="userIds !=null and  userIds.size() > 0">
                and a.F_UserId in
                <foreach collection="userIds" item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
            </if>
            <if test="phone != null and phone !=''">
                and a.F_Phone = #{phone}
            </if>
            and a.F_IsNeedCheck = 0
            and a.F_checkstatus in (0 ,3,4)
            and  a.F_IsSubmitForm = 1
        </where>
        order by a.F_CreatorTime desc
    </select>

    <select id="queryNumberOfOnboardedEmployees" resultType="java.lang.Long">
        SELECT
        *
        FROM
        ftb_personnels_staff_employment_apply
        WHERE
        F_ActualStartDate BETWEEN DATE_FORMAT( CURDATE(), '%Y-%m-01' )
        AND DATE_ADD( DATE_ADD( DATE_FORMAT( CURDATE(), '%Y-%m-01' ), INTERVAL 1 MONTH ), INTERVAL - 1 DAY )
        AND (F_IsNeedCheck = 1  OR ( F_IsNeedCheck = 0 AND F_CheckStatus IN ( 2 ) ))
        AND F_EnabledMark = 0
        <if test="orgIds != null and orgIds.size() > 0">
            AND F_CurrOrg IN
            <foreach collection="orgIds" item="id" open="("  separator="," close=")">
                #{id}
            </foreach>
        </if>
    </select>

    <select id="searchPhoneName" resultType="jnpf.model.personnels.dto.staff.roster.FtbPersonnelsStaffRosterDto">

        SELECT
        DISTINCT *
        FROM
        (
        SELECT
        d.*,
        ROW_NUMBER() OVER ( PARTITION BY d.userId ORDER BY CASE WHEN d.workerStatus = '301' THEN 0 ELSE 1 END, d.creatorTime DESC ) AS rn
        FROM
        (
        SELECT
        b.F_UserId userId,
        b.F_SystemWokerId as systemWokerId,
        b.F_Name `name`,
        b.F_ActualStartDate actualStartDate,
        b.F_JoinNum joinNum,
        b.F_ActualProbationaryDate actualProbationaryDate,
        b.F_DepartDate AS departDate,
        b.F_CreatorUserId creatorUserId,
        b.F_CreatorTime creatorTime,
        b.F_WorkerStatus workerStatus,
        b.F_WorkerType workerType,
        b.F_Phone phone,
        b.F_LastModifyTime lastModifyTime
        FROM
        ftb_personnels_staff_roster b
        WHERE
        b.F_EnabledMark = 0 UNION
        SELECT
        c.F_UserId userId,
        c.F_SystemUserId as systemWokerId,
        c.F_WorkerName `name`,
        c.F_ActualStartDate actualStartDate,
        c.F_JoinNum joinNum,
        NULL actualProbationaryDate,
        NULL departDate,
        c.F_CreatorUserId creatorUserId,
        c.F_CreatorTime creatorTime,
        '301' workerStatus,
        c.F_WorkerType workerType,
        c.F_Phone phone,
        c.F_LastModifyTime lastModifyTime
        FROM
        ftb_personnels_staff_employment_apply c
        WHERE
        c.F_UserId is not null
        and  c.F_EnabledMark = 0
        ) d
        <where>
            <if test="dto.userNames != null and dto.userNames !=''">
                d.name  in
                <foreach collection="dto.userNames" item="name" separator="," open="(" close=")">
                    #{name}
                </foreach>
            </if>
            <if test="dto.phones != null and dto.phones !=''">
                and d.phone  in
                <foreach collection="dto.phones" item="phone" separator="," open="(" close=")">
                    #{phone}
                </foreach>
            </if>
            <if test="dto.userIds != null and dto.userIds.size() > 0">
                and d.userId in
                <foreach collection="dto.userIds" item="id" open="("  separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
        ORDER BY
        d.creatorTime DESC
        ) ranked
        WHERE
        rn = 1

    </select>

    <select id="ListOfOnboardingPositions" resultType="jnpf.model.personnels.po.FtbPersonnelsStaffEmploymentApply">
        SELECT
        F_UserId as userId,
        F_ActualStartDate as actualStartDate
        FROM `ftb_personnels_staff_roster`
        <where>
            DATE_FORMAT(F_ActualStartDate, '%Y-%m') between #{startDate} and #{endDate}

            <if test="userIds !=null and  userIds.size() > 0">
                and F_UserId in
                <foreach collection="userIds" item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getSubmittedButNotOnboarded" resultType="jnpf.model.personnels.po.FtbPersonnelsStaffEmploymentApply">
        SELECT
            a.F_Id              as id,
            a.F_Phone             as phone,
            a.F_WorkerName        as workerName,
            a.F_UserId            as userId,
            a.F_SystemUserId      as systemWokerId,
            a.F_CurrOrg           as currOrg,
            a.F_CurrOrgName       as currOrgName,
            a.F_CurrPosition      as currPosition,
            a.F_CurrPositionName  as currPositionName,
            a.F_CurrRank          as currRank,
            a.F_CurrRankName      as currRankName,
            a.F_CurrGroupId       as currGroupId,
            a.F_CurrGroupName     as currGroupName,
            a.F_ContractType      as contractType,
            a.F_ExpectedStartDate as expectedStartDate,
            a.F_ActualStartDate   as actualStartDate,
            a.F_SourceRelation    as sourceRelation,
            a.F_SourceRelationId  as sourceRelationId,
            a.F_IdCard            as idCardNum,
            a.F_Source            as source,
            a.F_IsSubmitForm      as isSubmitForm,
            a.F_Status            as status,
            a.F_JoinNum           as joinNum,
            a.F_OpUser            as opUser,
            a.F_OpUserName        as opUserName,
            a.F_EntryMoney        as entryMoney,
            a.F_IsNeedCheck       as isNeedCheck,
            a.F_CheckStatus       as checkStatus,
            a.F_Files             as files,
            a.F_EnabledMark       as enabledMark,
            a.F_ShortUrl          as shortUrl,
            a.F_TaskInfoId        as taskInfoId,
            a.F_OnboardingRemarks as onboardingRemarks,
            a.F_AttendanceGroup   as attendanceGroup,
            a.F_SalaryStructure   as salaryStructure,
            a.F_IsAdjustSalary    as isAdjustSalary,
            a.F_SalaryTotalAmount as salaryTotalAmount,
            a.F_PhotoFiles        as photoFiles,
            a.F_SalaryData        as salaryData,
            a.F_VersionNum        as versionNum,
            a.F_RegisterImg       as registerImg,
            a.F_ReasonsForSalary  as reasonsForSalaryAdjustments,
            a.F_UserHeadLog       as userHeadLog,
            a.F_PayrollSequence   as payrollSequence,
            a.F_PayrollSequenceId as payrollSequenceId,
            a.F_PlanProbationaryDate as planProbationaryDate,
            a.F_PayrollSequenceName as payrollSequenceName,
            a.F_WorkerType        as workerType,
            a.F_WorkerStatus      as workerStatus,
            a.F_AffiliatedReportsTo as affiliatedReportsTo,
            a.F_ProbationPeriod   as probationPeriod,
            a.F_ProbationPeriodDay as probationPeriodDay,
            a.F_CreatorUserId     as creatorUserId,
            a.F_CreatorTime       as creatorTime,
            a.F_LastModifyUserId  as lastModifyUserId,
            a.F_LastModifyTime    as lastModifyTime,
            a.F_TenantId          as tenantId
        FROM ftb_personnels_staff_employment_apply as a
        WHERE a.F_IsSubmitForm = 1
          AND a.F_EnabledMark = 0
            and a.F_CheckStatus = 0
            and a.F_IsNeedCheck  = 0
            <if test="userIds !=null and  userIds.size() > 0">
                and a.F_UserId in
                <foreach collection="userIds" item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
            </if>
        ORDER BY a.F_CreatorTime DESC
    </select>

    <select id="getCountEmploymentApplyCreatedInPeriod" resultType="java.lang.Long">
        SELECT COUNT(*)
        FROM ftb_personnels_staff_employment_apply AS a
        WHERE a.F_EnabledMark = 0
          AND a.F_CreatorTime between  #{startTime} AND #{endTime}
        <if test="userIds !=null and  userIds.size() > 0">
            and a.F_UserId in
            <foreach collection="userIds" item="id" separator="," open="(" close=")">
                #{id}
            </foreach>
        </if>
    </select>

    <select id="getCountEmploymentApplyToRosterInPeriod" resultType="java.lang.Long">
        SELECT
            COUNT( DISTINCT r.F_Id )
        FROM
            ftb_personnels_staff_employment_apply AS a
                INNER JOIN ftb_personnels_staff_roster AS r ON a.F_Phone = r.F_Phone
        WHERE
            a.F_EnabledMark = 0
          AND r.F_EnabledMark = 0
          AND a.F_ActualStartDate BETWEEN #{startTime} AND #{endTime}
        <if test="userIds !=null and  userIds.size() > 0">
        and a.F_UserId in
        <foreach collection="userIds" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </if>
    </select>

    <select id="getListOfNotSubmittedForm" resultType="jnpf.model.personnels.po.FtbPersonnelsStaffEmploymentApply">
        SELECT
            a.F_Id              as id,
            a.F_Phone             as phone,
            a.F_WorkerName        as workerName,
            a.F_UserId            as userId,
            a.F_SystemUserId      as systemWokerId,
            a.F_CurrOrg           as currOrg,
            a.F_CurrOrgName       as currOrgName,
            a.F_CurrPosition      as currPosition,
            a.F_CurrPositionName  as currPositionName,
            a.F_CurrRank          as currRank,
            a.F_CurrRankName      as currRankName,
            a.F_CurrGroupId       as currGroupId,
            a.F_CurrGroupName     as currGroupName,
            a.F_ContractType      as contractType,
            a.F_ExpectedStartDate as expectedStartDate,
            a.F_ActualStartDate   as actualStartDate,
            a.F_SourceRelation    as sourceRelation,
            a.F_SourceRelationId  as sourceRelationId,
            a.F_IdCard            as idCardNum,
            a.F_Source            as source,
            a.F_IsSubmitForm      as isSubmitForm,
            a.F_Status            as status,
            a.F_JoinNum           as joinNum,
            a.F_OpUser            as opUser,
            a.F_OpUserName        as opUserName,
            a.F_EntryMoney        as entryMoney,
            a.F_IsNeedCheck       as isNeedCheck,
            a.F_CheckStatus       as checkStatus,
            a.F_Files             as files,
            a.F_EnabledMark       as enabledMark,
            a.F_ShortUrl          as shortUrl,
            a.F_TaskInfoId        as taskInfoId,
            a.F_OnboardingRemarks as onboardingRemarks,
            a.F_AttendanceGroup   as attendanceGroup,
            a.F_SalaryStructure   as salaryStructure,
            a.F_IsAdjustSalary    as isAdjustSalary,
            a.F_SalaryTotalAmount as salaryTotalAmount,
            a.F_PhotoFiles        as photoFiles,
            a.F_SalaryData        as salaryData,
            a.F_VersionNum        as versionNum,
            a.F_RegisterImg       as registerImg,
            a.F_ReasonsForSalary  as reasonsForSalaryAdjustments,
            a.F_UserHeadLog       as userHeadLog,
            a.F_PayrollSequence   as payrollSequence,
            a.F_PayrollSequenceId as payrollSequenceId,
            a.F_PlanProbationaryDate as planProbationaryDate,
            a.F_PayrollSequenceName as payrollSequenceName,
            a.F_WorkerType        as workerType,
            a.F_WorkerStatus      as workerStatus,
            a.F_AffiliatedReportsTo as affiliatedReportsTo,
            a.F_ProbationPeriod   as probationPeriod,
            a.F_ProbationPeriodDay as probationPeriodDay,
            a.F_CreatorUserId     as creatorUserId,
            a.F_CreatorTime       as creatorTime,
            a.F_LastModifyUserId  as lastModifyUserId,
            a.F_LastModifyTime    as lastModifyTime,
            a.F_TenantId          as tenantId
        FROM ftb_personnels_staff_employment_apply as a
        WHERE (a.F_IsSubmitForm = 0 OR a.F_IsSubmitForm IS NULL)
          AND a.F_EnabledMark = 0
            <if test="userIds !=null and  userIds.size() > 0">
                and a.F_UserId in
                <foreach collection="userIds" item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
            </if>
        ORDER BY a.F_CreatorTime DESC
    </select>
</mapper>