<?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.FtbPersonnelsStaffRosterMapper">
    <sql id="Base_Column_List">
        SELECT
            F_Id AS id,
            F_UserId AS userId,
            F_SystemWokerId AS systemWokerId,
            F_SystemContractId AS systemContractId,
            F_Phone AS phone,
            F_Name AS name,
            F_WorkerNo AS workerNo,
            F_CurrOrg AS currOrg,
            F_CurrPosition AS currPosition,
            F_CurrShopId AS currShopId,
            F_CurrReportsTo AS currReportsTo,
            F_CurrRank AS currRank,
            F_ContractEffectiveDate AS contractEffectiveDate,
            F_ContractNo AS contractNo,
            F_ActualProbationaryDate AS actualProbationaryDate,
            F_EntrySalary AS entrySalary,
            F_ActualStartDate AS actualStartDate,
            F_WorkerType AS workerType,
            F_ContractType AS contractType,
            F_WorkerStatus AS workerStatus,
            F_IsSubmitForm AS isSubmitForm,
            F_JoinNum AS joinNum,
            F_TransferPosition AS transferPosition,
            F_EnabledMark AS enabledMark,
            F_CreatorUserId AS creatorUserId,
            F_CreatorTime AS creatorTime,
            F_LastModifyUserId AS lastModifyUserId,
            F_LastModifyTime AS lastModifyTime,
            F_TenantId AS tenantId,
            F_CurrSalary as currSalary,
            F_ContractStatus as contractStatus,
            F_RegularSalary as regularSalary,
            F_ContractTypeName as contractTypeName,
            F_ContractCompany as contractCompany,
            F_DepartDate as departDate,
            F_IsTrialJob as isTrialJob,
            F_IsTrialFail as isTrialFail,
            F_TrialJobDay as trialJobDay,
            F_TaskInfoId as taskInfoId,
            F_VersionNum as versionNum
        FROM ftb_personnels_staff_roster as a
    </sql>
    <select id="pagingQuery" resultType="jnpf.model.personnels.dto.staff.roster.FtbPersonnelsStaffRosterDto">
        SELECT
        a.F_Id AS id,
        a.F_UserId AS userId,
        a.F_SystemWokerId AS systemWokerId,
        a.F_SystemContractId AS systemContractId,
        a.F_Phone AS phone,
        a.F_Name AS name,
        a.F_WorkerNo AS workerNo,
        a.F_CurrOrg AS currOrg,
        a.F_CurrPosition AS currPosition,
        a.F_CurrShopId AS currShopId,
        a.F_CurrReportsTo AS currReportsTo,
        a.F_CurrRank AS currRank,
        a.F_ContractEffectiveDate AS contractEffectiveDate,
        a.F_ContractNo AS contractNo,
        a.F_ActualProbationaryDate AS actualProbationaryDate,
        a.F_EntrySalary AS entrySalary,
        a.F_ActualStartDate AS actualStartDate,
        a.F_WorkerType AS workerType,
        a.F_ContractType AS contractType,
        a.F_WorkerStatus AS workerStatus,
        a.F_IsSubmitForm AS isSubmitForm,
        a.F_JoinNum AS joinNum,
        a.F_TransferPosition AS transferPosition,
        a.F_EnabledMark AS enabledMark,
        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,
        a.F_CurrSalary as currSalary,
        a.F_ContractStatus as contractStatus,
        a.F_RegularSalary as regularSalary,
        a.F_ContractTypeName as contractTypeName,
        a.F_ContractCompany as contractCompany,
        a.F_DepartDate as departDate,
        a.F_IsTrialJob as isTrialJob,
        a.F_IsTrialFail as isTrialFail,
        a.F_TrialJobDay as trialJobDay,
        a.F_TaskInfoId as taskInfoId,
        a.F_VersionNum as versionNum
        FROM ftb_personnels_staff_roster as a
        <where>
            a.F_EnabledMark = 0 and a.F_UserId != '349057407209541'
            <if test="params.isDepart == null ">
                and a.F_WorkerStatus != '305'
            </if>
            <if test="params.keyWord!= null and params.keyWord!= ''">
                and (
                a.F_Name like CONCAT('%',#{params.keyWord},'%')
                or a.F_WorkerNo like CONCAT('%',#{params.keyWord},'%')
                or a.F_SystemWokerId like CONCAT('%',#{params.keyWord},'%')
                or a.F_Phone like CONCAT('%',#{params.keyWord},'%')
                )
            </if>
            <if test="params.likeUserName!= null and params.likeUserName!= ''">
                and ( a.F_Name like CONCAT('%',#{params.likeUserName},'%') )
            </if>

            <if test="params.startActualStartDate!= null and params.endActualStartDate!= null">
                and a.F_ActualStartDate between #{params.startActualStartDate} and #{params.endActualStartDate}
            </if>


            <if test="params.isSubmitForm!= null">
                and a.F_IsSubmitForm =#{params.isSubmitForm}
            </if>

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

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

            <if test="params.workerStatus!= null and params.workerStatus!= ''">
                and a.F_WorkerStatus =#{params.workerStatus}
            </if>
            <if test="userIds != null and userIds.size() > 0 ">
                and a.F_UserId in
                <foreach collection="userIds" item="item" open="("  separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="params.userIds != null and params.userIds.size() > 0 ">
                and a.F_UserId in
                <foreach collection="params.userIds" item="item" open="("  separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="params.phoneNumberList !=null and params.phoneNumberList.size() > 0 ">
                and a.F_Phone in
                <foreach collection="params.phoneNumberList" item="item" open="("  separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="params.contractStatus!= null">
                and a.F_ContractStatus =#{params.contractStatus}
            </if>

            <if test="params.countType != null and params.countType == 0">
                and a.F_WorkerStatus in ('302', '303', '304', '306')
            </if>
            <if test="params.countType != null and params.countType == 1">
                and a.F_WorkerType = '26' and a.F_WorkerStatus not in ('305')
            </if>
            <if test="params.countType != null and params.countType == 2">
                and a.F_WorkerType in ('27','32') and a.F_WorkerStatus not in ('305')
            </if>
            <if test="params.countType != null and params.countType == 3">
                and a.F_WorkerType = '28' and a.F_WorkerStatus not in ('305')
            </if>
            <if test="params.countType != null and params.countType == 4">
                and a.F_WorkerStatus in ('304')
            </if>
            <if test="params.countType != null and params.countType == 5">
                and a.F_WorkerStatus in ('303')
            </if>
            <if test="params.countType != null and params.countType == 6">
                and a.F_WorkerStatus in ('302')
            </if>
            <if test="params.countType != null and params.countType == 7">
                and a.F_WorkerStatus in ('306')
            </if>
        </where>
        order by a.F_CreatorTime desc,a.F_Id desc
    </select>
    <select id="pagingQueryNewWithTurnoverManagerList" resultType="jnpf.model.personnels.dto.staff.roster.FtbPersonnelsStaffRosterDto">
        SELECT
        *
        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_Id as id,
        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
        '' as id,
        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_IsNeedCheck = 0
        AND c.F_CheckStatus IN ( 0, 1 ,3,4, 5)
        ) d
        <where>
             d.userId != '349057407209541'
            <if test="params.likeUserName!= null and params.likeUserName!= ''">
                and ( d.name like CONCAT('%',#{params.likeUserName},'%') )
            </if>
            <if test="params.workerType != null and params.workerType != ''">
                AND d.workerType = #{params.workerType}
            </if>
            <!-- 员工状态 -->
            <if test="params.userWorkStatus != null">
                <choose>
                    <!-- 离职员工：workerStatus = '305' -->
                    <when test="params.userWorkStatus == 1">
                        AND d.workerStatus = '305'
                        <if test="params.onlineWorkStartDate != null and params.onlineWorkEndDate != null">
                            AND (
                            d.actualStartDate <![CDATA[<=]]> #{params.onlineWorkEndDate}
                            AND d.departDate <![CDATA[>]]> #{params.onlineWorkStartDate}
                            )
                        </if>
                    </when>

                    <!-- 在职员工：workerStatus != '305' -->
                    <when test="params.userWorkStatus == 0">
                        AND d.workerStatus != '305'
                        <if test="params.onlineWorkStartDate != null">
                            AND (d.actualStartDate <![CDATA[<=]]> #{params.onlineWorkEndDate})
                        </if>
                    </when>
                </choose>
            </if>
            <if test="params.userWorkStatus == null and params.onlineWorkStartDate != null and params.onlineWorkEndDate != null">
                AND (
                (
                d.actualStartDate <![CDATA[<=]]> #{params.onlineWorkEndDate}
                AND d.departDate <![CDATA[>]]> #{params.onlineWorkStartDate}
                )
                OR
                (
                d.workerStatus != '305'
                AND (d.actualStartDate <![CDATA[<=]]> #{params.onlineWorkEndDate})
                )
                )
            </if>
            <if test="params.startActualStartDate != null and params.endActualStartDate != null">
                AND (
                d.actualStartDate <![CDATA[>=]]> #{params.startActualStartDate}
                AND d.actualStartDate <![CDATA[<=]]> #{params.endActualStartDate}
                )
            </if>
            <if test="userIds != null and userIds.size() > 0 ">
                and d.userId in
                <foreach collection="userIds" item="item" open="("  separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        ORDER BY
        d.creatorTime DESC
        ) ranked
        WHERE
        rn = 1
    </select>

    <select id="queryNewWithTurnoverManagerList" resultType="jnpf.model.personnels.dto.staff.roster.FtbPersonnelsStaffRosterDto">
        SELECT
        *
        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_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,
        b.F_WorkerNo AS workerNo,
        b.F_ActualProbationaryDate actualProbationaryDate
        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,
        NULL departDate,
        c.F_CreatorUserId creatorUserId,
        c.F_CreatorTime creatorTime,
        '301' workerStatus,
        c.F_WorkerType workerType,
        c.F_Phone phone,
        c.F_LastModifyTime lastModifyTime,
        c.F_SystemUserId workerNo,
        NULL actualProbationaryDate
        FROM
        ftb_personnels_staff_employment_apply c
        WHERE
        c.F_IsNeedCheck = 0
        AND c.F_CheckStatus IN ( 0, 1 ,3,4, 5)
        ) d
        <where>
            <if test="params.likeUserName != null and params.likeUserName != ''">
                AND d.name LIKE CONCAT('%', #{params.likeUserName}, '%')
            </if>
            <if test="params.workerType != null and params.workerType != ''">
                AND d.workerType = #{params.workerType}
            </if>
            <!-- 员工状态 -->
            <if test="params.userWorkStatus != null">
                <choose>
                    <!-- 离职员工：workerStatus = '305' -->
                    <when test="params.userWorkStatus == 1">
                        AND d.workerStatus = '305'
                        <if test="params.onlineWorkStartDate != null and params.onlineWorkEndDate != null">
                            AND (
                            d.actualStartDate <![CDATA[<=]]> #{params.onlineWorkEndDate}
                            AND d.departDate <![CDATA[>]]> #{params.onlineWorkStartDate}
                            )
                        </if>
                    </when>

                    <!-- 在职员工：workerStatus != '305' -->
                    <when test="params.userWorkStatus == 0">
                        AND d.workerStatus != '305'
                        <if test="params.onlineWorkStartDate != null">
                            AND (d.actualStartDate <![CDATA[<=]]> #{params.onlineWorkEndDate})
                        </if>
                    </when>
                </choose>
            </if>
            <if test="params.userWorkStatus == null and params.onlineWorkStartDate != null and params.onlineWorkEndDate != null">
                AND (
                (
                d.actualStartDate <![CDATA[<=]]> #{params.onlineWorkEndDate}
                AND d.departDate <![CDATA[>]]> #{params.onlineWorkStartDate}
                )
                OR
                (
                d.workerStatus != '305'
                AND (d.actualStartDate <![CDATA[<=]]>  #{params.onlineWorkEndDate})
                )
                )
            </if>
            <if test="params.startActualStartDate != null and params.endActualStartDate != null">
                AND (
                d.actualStartDate <![CDATA[>=]]> #{params.startActualStartDate}
                AND d.actualStartDate <![CDATA[<=]]> #{params.endActualStartDate}
                )
            </if>
            <if test="userIds != null and userIds.size() > 0 ">
                and d.userId in
                <foreach collection="userIds" item="item" open="("  separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        ORDER BY
        d.creatorTime DESC
        ) ranked
        WHERE
        rn = 1
    </select>
    <select id="pagingAppQuery"
            resultType="jnpf.model.personnels.dto.staff.roster.FtbPersonnelsStaffRosterDto">

        <include refid="Base_Column_List"></include>
        <where>
            a.F_EnabledMark = 0 and a.F_WorkerStatus != '305'
            <if test="params.keyWord!= null and params.keyWord!= ''">
                and (
                a.F_Name like CONCAT('%',#{params.keyWord},'%')
                or a.F_WorkerNo like CONCAT('%',#{params.keyWord},'%')
                or a.F_SystemWokerId like CONCAT('%',#{params.keyWord},'%')
                or a.F_Phone like CONCAT('%',#{params.keyWord},'%')
                )
            </if>

            <if test="params.startActualProbationaryDate!= null">
                and a.F_ActualStartDate >= #{params.startActualProbationaryDate}
            </if>

            <if test="params.endActualProbationaryDate!= null">
                and a.F_ActualStartDate &lt;= #{params.endActualProbationaryDate}
            </if>


            <if test="params.isSubmitForm!= null">
                and a.F_IsSubmitForm =#{params.isSubmitForm}
            </if>

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

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

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

            <if test="userIds != null and userIds.size() > 0 ">
                and a.F_UserId in
                <foreach collection="userIds" item="item" open="("  separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="params.contractStatus!= null">
                and a.F_ContractStatus =#{params.contractStatus}
            </if>
            <if test="params.phoneNumberList !=null and params.phoneNumberList.size() > 0 ">
                and a.F_Phone in
                <foreach collection="params.phoneNumberList" item="item" open="("  separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        order by a.F_CreatorTime desc,a.F_Id desc

    </select>

    <select id="canUpdateRealName" resultType="java.lang.String">
        select F_DataJson from ftb_common_config where F_Code="rosterUpdateRealName"
    </select>

    <select id="queryBirthday" resultType="java.lang.String">
        SELECT
            phoneNumber
        FROM
            ( SELECT DATE_FORMAT( F_Value, '%m-%d' ) AS bir, F_Phone as phoneNumber
              FROM `ftb_personnels_staff_registration_form_data` WHERE `F_FormFieldId` = 'birthday' AND F_Value != "" ) a
        WHERE
            a.bir BETWEEN #{startBirthdayTime}  AND #{endBirthdayTime}
    </select>

    <!--
      花名册维度：本月实际入职日期内、已提交登记表、非预入职(301)。
      办理渠道：(1) 入职表已到岗且同入职日期的记录为「无需审批(F_IsNeedCheck=1，含导入入职办理/直接办理)」或「需审批且审核通过」；
               (2) 无同日到岗入职表记录，视为花名册导入完成入职。
    -->
    <select id="getCountOfOnboardedInPeriod" resultType="java.lang.Long">
        SELECT COUNT(DISTINCT r.F_Id)
        FROM ftb_personnels_staff_roster r
        WHERE r.F_EnabledMark = 0
          AND r.F_ActualStartDate IS NOT NULL
          AND r.F_IsSubmitForm = 1
          AND r.F_WorkerStatus != '301'
          AND r.F_ActualStartDate BETWEEN COALESCE(#{startTime}, DATE_FORMAT(CURDATE(), '%Y-%m-01'))
              AND COALESCE(#{endTime}, DATE_ADD(DATE_ADD(DATE_FORMAT(CURDATE(), '%Y-%m-01'), INTERVAL 1 MONTH), INTERVAL -1 DAY))
          AND (
              EXISTS (
                  SELECT 1
                  FROM ftb_personnels_staff_employment_apply e
                  WHERE e.F_Phone = r.F_Phone
                    AND e.F_EnabledMark = 0
                    AND e.F_Status = 1
                    AND e.F_ActualStartDate IS NOT NULL
                    AND DATE(e.F_ActualStartDate) = DATE(r.F_ActualStartDate)
                    AND (e.F_IsNeedCheck = 1 OR (e.F_IsNeedCheck = 0 AND e.F_CheckStatus = 2))
              )
              OR NOT EXISTS (
                  SELECT 1
                  FROM ftb_personnels_staff_employment_apply e2
                  WHERE e2.F_Phone = r.F_Phone
                    AND e2.F_EnabledMark = 0
                    AND e2.F_Status = 1
                    AND e2.F_ActualStartDate IS NOT NULL
                    AND DATE(e2.F_ActualStartDate) = DATE(r.F_ActualStartDate)
              )
          )
    </select>

</mapper>