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

    <select id="getTurnoverList" resultType="jnpf.model.personnels.vo.turnover.FtbPersonnelsTurnoverManagementVO">
        SELECT
        tur.F_Id  id,
        ros.F_Name userName,
        ifnull( ros.F_WorkerStatus,tur.F_WorkingCondition) workingCondition,
        tur.F_UserId userId,
        tur.F_JobNumber jobNumber,
        tur.F_OrgId orgId,
        tur.F_OrgName orgName,
        tur.F_EntryPostId entryPostId,
        tur.F_EntryPostName entryPostName,
        tur.F_EntryGradeId entryGradeId,
        tur.F_EntryGradeName entryGradeName,
        tur.F_DateOfEntry dateOfEntry,
        tur.F_ActualConverDate actualConverDate,
        tur.F_EmpResignationDate empResignationDate,
        tur.F_ApplicationSource applicationSource,
        tur.F_TurnoverStatus turnoverStatus,
        tur.F_ResignationDate resignationDate,
        tur.F_ReasonForResignation reasonForResignation,
        tur.F_TaskInfoId taskInfoId,
        tur.F_SystemWokerId systemWokerId,
        tur.F_OrganizationInfo organizationInfo,
        tur.F_VersionNum versionNum,
        ros.F_JoinNum as joinNum,
        tur.F_PhoneNumber  as phone
        from ftb_personnels_turnover_management as tur
        left join ftb_personnels_staff_roster as ros on tur.F_UserId  = ros.F_UserId
        <where>
            tur.F_EnableMark = 0
            <if test="dto.keyWord!= null and dto.keyWord!= ''">
                and   (
                ros.F_Name LIKE CONCAT('%',#{dto.keyWord},'%') OR
                tur.F_JobNumber LIKE CONCAT('%',#{dto.keyWord},'%') OR
                tur.F_UserId LIKE CONCAT('%',#{dto.keyWord},'%')
                )
            </if>
            <if test="dto.approvalStatus != null and dto.approvalStatus !=''">
                and tur.F_TurnoverStatus =#{dto.approvalStatus}
            </if>
            <if test="dto.postId != null and dto.postId !=''">
                and  tur.F_EntryPostId = #{dto.postId}
            </if>
            <if test="dto.gradeId != null and dto.gradeId !=''">
                and  tur.F_EntryGradeId = #{dto.gradeId}
            </if>
            <if test="dto.beginTime !=null and dto.endTime !=null">
                and  tur.F_ResignationDate BETWEEN #{dto.beginTime} and #{dto.endTime}
            </if>
            <if test="dto.userIds != null and dto.userIds.size()>0">
                and tur.F_UserId in
                <foreach collection="dto.userIds" item="id" open="("  separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="dto.ids != null and dto.ids.size()>0">
                and tur.F_Id in
                <foreach collection="dto.ids" item="id" open="("  separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
        order by tur.F_CreatorTime desc
    </select>


    <select id="getListForApp" resultType="jnpf.model.personnels.vo.turnover.FtbPersonnelsTurnoverManagementVO">
        SELECT
        tur.F_Id  id,
        ros.F_Name userName,
        ros.F_WorkerStatus workingCondition,
        tur.F_SystemWokerId systemWokerId,
        tur.F_UserId userId,
        tur.F_JobNumber jobNumber,
        tur.F_OrgId orgId,
        tur.F_OrgName orgName,
        tur.F_EntryPostId entryPostId,
        tur.F_EntryPostName entryPostName,
        tur.F_EntryGradeId entryGradeId,
        tur.F_EntryGradeName entryGradeName,
        tur.F_DateOfEntry dateOfEntry,
        tur.F_ActualConverDate actualConverDate,
        tur.F_EmpResignationDate empResignationDate,
        tur.F_ApplicationSource applicationSource,
        tur.F_TurnoverStatus turnoverStatus,
        tur.F_ResignationDate resignationDate,
        tur.F_ReasonForResignation reasonForResignation,
        tur.F_CreatorUserId creatorUserId,
        tur.F_TaskInfoId as taskInfoId,
        tur.F_CreatorTime creatorTime,
        tur.F_OrganizationInfo organizationInfo,
        tur.F_VersionNum versionNum,
        ros.F_JoinNum as joinNum
        from ftb_personnels_turnover_management as tur
        left join ftb_personnels_staff_roster as ros on tur.F_UserId  = ros.F_UserId
        <where>
            tur.F_EnableMark = 0
            <if test="dto.keyWord!= null and dto.keyWord!= ''">
                and  (
                ros.F_Name LIKE CONCAT('%',#{dto.keyWord},'%') OR
                tur.F_EntryGradeName LIKE CONCAT('%',#{dto.keyWord},'%') OR
                tur.F_EntryPostName LIKE CONCAT('%',#{dto.keyWord},'%')
                )
            </if>
            <if test="dto.userId != null and dto.userId !='' and dto.flagByApp != 3 and dto.flagByApp != 1">
                and  tur.F_UserId =#{dto.userId}
            </if>
            <if test="dto.approvalStatus != null and dto.approvalStatus !='' and dto.statusList == null ">
                and  tur.F_TurnoverStatus = #{dto.approvalStatus}
            </if>
            <if test="dto.statusList != null and dto.statusList.size() > 0 ">
                and  tur.F_TurnoverStatus in
                <foreach collection="dto.statusList" item="item" open="("  separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="dto.flagByApp == 3 ">
                AND EXISTS ( SELECT co.F_UserId 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 =#{dto.configType}
                AND co.F_UserId =#{dto.userId}
                and config.F_EnableMark = 0
                )
            </if>
            <if test="dto.ids != null  and ( dto.flagByApp !=3 or dto.flagByApp != 4 ) and dto.ids.size() > 0 ">
                and  tur.F_Id in
                <foreach collection="dto.ids" item="id" open="("  separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
        order by  tur.F_CreatorTime desc
    </select>

    <select id="getTurnoverListForOnePersonnels"
            resultType="jnpf.model.personnels.vo.turnover.FtbPersonnelsTurnoverManagementVO">
        SELECT DISTINCT
        tur.F_Id  id,
        ros.F_Name userName,
        ros.F_WorkerStatus workingCondition,
        tur.F_SystemWokerId systemWokerId,
        tur.F_UserId userId,
        tur.F_JobNumber jobNumber,
        tur.F_OrgId orgId,
        tur.F_OrgName orgName,
        tur.F_EntryPostId entryPostId,
        tur.F_EntryPostName entryPostName,
        tur.F_EntryGradeId entryGradeId,
        tur.F_EntryGradeName entryGradeName,
        tur.F_DateOfEntry dateOfEntry,
        tur.F_ActualConverDate actualConverDate,
        tur.F_EmpResignationDate empResignationDate,
        tur.F_ApplicationSource applicationSource,
        tur.F_TurnoverStatus turnoverStatus,
        tur.F_ResignationDate resignationDate,
        tur.F_ReasonForResignation reasonForResignation,
        tur.F_CreatorUserId creatorUserId,
        tur.F_TaskInfoId taskInfoId,
        tur.F_CreatorTime creatorTime,
        tur.F_OrganizationInfo organizationInfo
        FROM
        ftb_personnels_turnover_management AS tur
        LEFT JOIN ftb_personnels_audit_run_task AS task ON tur.F_Id = task.F_BusinessId
        LEFT JOIN ftb_personnels_audit_master_config AS mas ON mas.F_Id = task.F_MasterAuditConfigId
        LEFT JOIN ftb_personnels_audit_sub_config AS sub ON mas.F_Id = sub.F_MasterId
        left join ftb_personnels_staff_roster as ros on tur.F_UserId  = ros.F_UserId
        WHERE
        (  ( sub.F_AuditorId = #{dto.userId} AND task.F_UserId IS NULL)  OR (  task.F_UserId = #{dto.userId}) )
        and task.F_ExcuseStatus in (0,1)
        and  tur.F_EnableMark = 0
        and  mas.F_ConfigType = #{dto.configType}
        <if test="dto.userIds != null and dto.userIds.size()>0">
            and tur.F_UserId in
            <foreach collection="dto.userIds" item="id" open="("  separator="," close=")">
                #{id}
            </foreach>
        </if>
        order by tur.F_CreatorTime desc
    </select>
    <!--待审批 -->
    <select id="getThePendingApprovalList"
            resultType="jnpf.model.personnels.vo.turnover.FtbPersonnelsTurnoverManagementVO">
        SELECT
        tur.F_Id id,
        ros.F_Name userName,
        ros.F_WorkerStatus workingCondition,
        tur.F_UserId userId,
        tur.F_SystemWokerId systemWokerId,
        tur.F_JobNumber jobNumber,
        tur.F_OrgId orgId,
        tur.F_OrgName orgName,
        tur.F_EntryPostId entryPostId,
        tur.F_EntryPostName entryPostName,
        tur.F_EntryGradeId entryGradeId,
        tur.F_EntryGradeName entryGradeName,
        tur.F_DateOfEntry dateOfEntry,
        tur.F_ActualConverDate actualConverDate,
        tur.F_EmpResignationDate empResignationDate,
        tur.F_ApplicationSource applicationSource,
        tur.F_TurnoverStatus turnoverStatus,
        tur.F_ResignationDate resignationDate,
        tur.F_ReasonForResignation reasonForResignation,
        tur.F_CreatorUserId creatorUserId,
        run.F_Id  as taskInfoId,
        tur.F_CreatorTime creatorTime,
        tur.F_OrganizationInfo organizationInfo
        FROM
        ftb_personnels_turnover_management AS tur
        LEFT JOIN ftb_personnels_audit_run_task AS run ON run.F_BusinessId = tur.F_Id
        left join ftb_personnels_staff_roster as ros on tur.F_UserId  = ros.F_UserId
        INNER JOIN ftb_personnels_audit_master_config as conf ON run.F_MasterAuditConfigId = conf.F_Id
        WHERE
        run.F_UserId =#{dto.userId}
        and tur.F_EnableMark = 0
        AND conf.F_ConfigType =#{dto.configType}
        order by tur.F_CreatorTime desc
    </select>

    <select id="getApprovedData" resultType="jnpf.model.personnels.vo.turnover.FtbPersonnelsTurnoverManagementVO">
        SELECT
        tur.F_Id id,
        ros.F_Name userName,
        ros.F_WorkerStatus workingCondition,
        tur.F_UserId userId,
        tur.F_SystemWokerId systemWokerId,
        tur.F_JobNumber jobNumber,
        tur.F_OrgId orgId,
        tur.F_OrgName orgName,
        tur.F_EntryPostId entryPostId,
        tur.F_EntryPostName entryPostName,
        tur.F_EntryGradeId entryGradeId,
        tur.F_EntryGradeName entryGradeName,
        tur.F_DateOfEntry dateOfEntry,
        tur.F_ActualConverDate actualConverDate,
        tur.F_EmpResignationDate empResignationDate,
        tur.F_ApplicationSource applicationSource,
        tur.F_TurnoverStatus turnoverStatus,
        info.F_ApprovalResult approvalResult,
        tur.F_ResignationDate resignationDate,
        tur.F_ReasonForResignation reasonForResignation,
        tur.F_CreatorUserId creatorUserId,
        run.F_Id  as taskInfoId,
        tur.F_CreatorTime creatorTime,
        tur.F_OrganizationInfo organizationInfo
        FROM
        ftb_personnels_turnover_management AS tur
        LEFT JOIN ftb_personnels_audit_run_task AS run ON run.F_BusinessId = tur.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
        left join ftb_personnels_staff_roster as ros on tur.F_UserId  = ros.F_UserId
        WHERE
        info.F_UserId = #{dto.userId}
        and  tur.F_EnableMark = 0
        and mas.F_ConfigType = #{dto.configType}
        <if test="dto.approvalStatus != null and dto.approvalStatus !='' and  dto.approvalStatus == 2 ">
            and tur.F_TurnoverStatus= 2
        </if>
        <if test="dto.approvalStatus != null and dto.approvalStatus !='' and  dto.approvalStatus == 3 ">
            and tur.F_TurnoverStatus = 3
        </if>
        <if test="dto.approvalStatus != null and dto.approvalStatus !='' and  dto.approvalStatus == 1 ">
            and tur.F_TurnoverStatus = 1
        </if>
        <if test="dto.approvalStatus != null and dto.approvalStatus !='' and  dto.approvalStatus == 7 ">
            and tur.F_TurnoverStatus in (1,2,3)
        </if>
        order by tur.F_CreatorTime desc
    </select>

    <select id="getListForCC" resultType="jnpf.model.personnels.vo.turnover.FtbPersonnelsTurnoverManagementVO">
        select  DISTINCT e.* from (  SELECT
        tur.F_Id  id,
        ros.F_Name userName,
        ros.F_WorkerStatus workingCondition,
        tur.F_SystemWokerId systemWokerId,
        tur.F_UserId userId,
        tur.F_JobNumber jobNumber,
        tur.F_OrgId orgId,
        tur.F_OrgName orgName,
        tur.F_EntryPostId entryPostId,
        tur.F_EntryPostName entryPostName,
        tur.F_EntryGradeId entryGradeId,
        tur.F_EntryGradeName entryGradeName,
        tur.F_DateOfEntry dateOfEntry,
        tur.F_ActualConverDate actualConverDate,
        tur.F_EmpResignationDate empResignationDate,
        tur.F_ApplicationSource applicationSource,
        tur.F_TurnoverStatus turnoverStatus,
        tur.F_ResignationDate resignationDate,
        tur.F_ReasonForResignation reasonForResignation,
        tur.F_CreatorUserId creatorUserId,
        tur.F_TaskInfoId as taskInfoId,
        tur.F_CreatorTime creatorTime
        from ftb_personnels_turnover_management as tur
        left join ftb_personnels_staff_roster as ros on tur.F_UserId  = ros.F_UserId
        <where>
            and tur.F_EnableMark = 0
            <if test="dto.approvalStatus != null and dto.approvalStatus !='' and dto.statusList == null ">
                and tur.F_TurnoverStatus = #{dto.approvalStatus}
            </if>
            <if test="dto.statusList != null and dto.statusList.size() > 0 ">
                and tur.F_TurnoverStatus in
                <foreach collection="dto.statusList" item="item" open="("  separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        order by tur.F_CreatorTime desc
        ) 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 = #{dto.configType}
        AND co.F_UserId = #{dto.userId}
        ) d ON (
        task.F_MasterAuditConfigId = d.F_Id
        OR his.F_MasterAuditConfigId = d.F_Id
        )
        order by e.creatorTime desc
    </select>

    <select id="queryTurnOverManagementUserList" resultType="jnpf.model.personnels.po.FtbPersonnelsTurnoverManagement">
        SELECT DISTINCT
            roster.F_UserId as userId,
            tur.F_OrganizationInfo as organizationInfo
        FROM
            ftb_personnels_staff_roster AS roster
                INNER JOIN ftb_personnels_turnover_management AS tur ON roster.F_UserId = tur.F_UserId
        WHERE
            roster.F_EnabledMark = 0
          AND tur.F_EnableMark = 0
          AND roster.F_WorkerStatus = '305'
    </select>

    <select id="queryTurnoverList" resultType="jnpf.model.personnels.vo.turnover.FtbPersonnelsTurnoverManagementVO">
        SELECT
        tur.F_Id  id,
        ros.F_Name userName,
        ifnull(ros.F_WorkerStatus,tur.F_WorkingCondition) workingCondition,
        tur.F_UserId userId,
        tur.F_JobNumber jobNumber,
        tur.F_OrgId orgId,
        tur.F_OrgName orgName,
        tur.F_EntryPostId entryPostId,
        tur.F_EntryPostName entryPostName,
        tur.F_EntryGradeId entryGradeId,
        tur.F_EntryGradeName entryGradeName,
        tur.F_DateOfEntry dateOfEntry,
        tur.F_ActualConverDate actualConverDate,
        tur.F_EmpResignationDate empResignationDate,
        tur.F_ApplicationSource applicationSource,
        tur.F_TurnoverStatus turnoverStatus,
        tur.F_ResignationDate resignationDate,
        tur.F_ReasonForResignation reasonForResignation,
        tur.F_TaskInfoId taskInfoId,
        tur.F_SystemWokerId systemWokerId,
        tur.F_OrganizationInfo organizationInfo,
        tur.F_VersionNum versionNum
        from ftb_personnels_turnover_management as tur
        left join ftb_personnels_staff_roster as ros on tur.F_UserId  = ros.F_UserId
        <where>
            tur.F_EnableMark = 0
            and (tur.F_TurnoverStatus = 2 or tur.F_TurnoverStatus = 6)
            <if test="userIds != null and userIds.size() > 0">
                and tur.F_UserId in
                <foreach collection="userIds" item="item" open="("  separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="queryUserInfoForTurnover"
            resultType="jnpf.model.personnels.vo.turnover.FtbPersonnelsTurnoverManagementVO">
        SELECT
        tur.F_Id  id,
        ros.F_Name userName,
        ros.F_WorkerStatus workingCondition,
        tur.F_UserId userId,
        tur.F_JobNumber jobNumber,
        tur.F_OrgId orgId,
        tur.F_OrgName orgName,
        tur.F_EntryPostId entryPostId,
        tur.F_EntryPostName entryPostName,
        tur.F_EntryGradeId entryGradeId,
        tur.F_EntryGradeName entryGradeName,
        tur.F_DateOfEntry dateOfEntry,
        tur.F_ActualConverDate actualConverDate,
        tur.F_EmpResignationDate empResignationDate,
        tur.F_ApplicationSource applicationSource,
        tur.F_TurnoverStatus turnoverStatus,
        tur.F_ResignationDate resignationDate,
        tur.F_ReasonForResignation reasonForResignation,
        tur.F_TaskInfoId taskInfoId,
        tur.F_SystemWokerId systemWokerId,
        tur.F_OrganizationInfo organizationInfo,
        tur.F_VersionNum versionNum
        from ftb_personnels_turnover_management as tur
        left join ftb_personnels_staff_roster as ros on tur.F_UserId  = ros.F_UserId
        <where>
            tur.F_EnableMark = 0
            and tur.F_TurnoverStatus in (2,6)
            <if test="userIds != null and userIds.size() > 0">
                and tur.F_UserId in
                <foreach collection="userIds" item="item" open="("  separator="," close=")">
                    #{item}
                </foreach>
            </if>
            and DATE_FORMAT(tur.F_ResignationDate, '%Y-%m') &lt;= #{date}
        </where>
    </select>

    <select id="queryTurnoverListWithStatics"
            resultType="jnpf.model.personnels.vo.turnover.FtbPersonnelsTurnoverManagementVO">
        select  a.* from (
                           SELECT
                               tur.F_Id  id,
                               ros.F_Name userName,
                               ifnull( ros.F_WorkerStatus,tur.F_WorkingCondition) workingCondition,
                               tur.F_UserId userId,
                               tur.F_JobNumber jobNumber,
                               tur.F_OrgId orgId,
                               tur.F_OrgName orgName,
                               tur.F_EntryPostId entryPostId,
                               tur.F_EntryPostName entryPostName,
                               tur.F_EntryGradeId entryGradeId,
                               tur.F_EntryGradeName entryGradeName,
                               tur.F_DateOfEntry dateOfEntry,
                               tur.F_ActualConverDate actualConverDate,
                               tur.F_EmpResignationDate empResignationDate,
                               tur.F_ApplicationSource applicationSource,
                               tur.F_TurnoverStatus turnoverStatus,
                               tur.F_ResignationDate resignationDate,
                               tur.F_ReasonForResignation reasonForResignation,
                               tur.F_TaskInfoId taskInfoId,
                               tur.F_SystemWokerId systemWokerId,
                               tur.F_OrganizationInfo organizationInfo,
                               tur.F_VersionNum versionNum
                           from ftb_personnels_turnover_management as tur
                                    left join ftb_personnels_staff_roster as ros on tur.F_UserId  = ros.F_UserId
                           where
                               tur.F_EnableMark = 0
                       ) a where a.workingCondition in ('305')
    </select>

    <select id="getTurnoverRateAndHeadcount" resultType="java.lang.Long">
        SELECT
        COUNT( a.F_Id ) AS count
        FROM
        ftb_personnels_turnover_management AS a
        INNER JOIN ftb_personnels_staff_roster AS b ON a.F_UserId = b.F_UserId
        WHERE
        a.F_ResignationDate 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 b.F_WorkerStatus = '305'
        <if test="ids != null and ids.size() != 0 ">
            and a.F_Id in
            <foreach collection="ids" item="item" open="("  separator="," close=")">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="getLeaveAllPeople" resultType="java.lang.Long">
        SELECT
        COUNT(0)
        FROM
        ftb_personnels_staff_roster
        WHERE
        F_WorkerStatus ='305'
        <if test="userIds != null and userIds.size() != 0 ">
            and F_UserId in
            <foreach collection="userIds" open="(" close=")" separator="," item="userId">
                #{userId}
            </foreach>
        </if>
    </select>
</mapper>