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

    <select id="pageList" resultType="jnpf.model.personnels.vo.regular.FtbPersonnelsRegularManagementVO">
        select
        re.F_Id  id,
        re.F_SystemWokerId systemWokerId,
        ros.F_Name userName,
        ros.F_WorkerStatus workingCondition,
        re.F_UserId userId,
        re.F_JobNumber jobNumber,
        re.F_OrgId orgId,
        re.F_OrgName orgName,
        re.F_RegularPostId regularPostId,
        re.F_RegularPostName regularPostName,
        re.F_RegularGradeId regularGradeId,
        re.F_RegularGradeName regularGradeName,
        re.F_DateOfEntry dateOfEntry,
        re.F_Probation probation,
        re.F_ProbationPeriodDay probationPeriodDay,
        re.F_SchedConverDate schedConverDate,
        re.F_ActualConverDate actualConverDate,
        re.F_TransferStatus transferStatus,
        re.F_ImmediateSuperId immediateSuperId,
        re.F_ImmediateSuperName immediateSuperName,
        re.F_ApplicationSource applicationSource,
        re.F_TaskInfoId taskInfoId,
        re.F_VersionNum versionNum
        from ftb_personnels_regular_management as re
        left join ftb_personnels_staff_roster as ros on re.F_UserId  = ros.F_UserId
        <where>
            re.F_EnableMark =0
            and  ros.F_WorkerStatus not in ('304','305')
            <if test="dto.keyWord!= null and dto.keyWord!= ''">
                And   (
                ros.F_Name LIKE CONCAT('%',#{dto.keyWord},'%')
                )
            </if>
            <if test="dto.approvalStatus != null and dto.approvalStatus !='' and dto.approvalStatus != 0 and dto.approvalStatus != 7 ">
                and re.F_TransferStatus =#{dto.approvalStatus}
            </if>
            <if test="dto.beginTime !=null and dto.endTime !=null">
                and  re.F_ActualConverDate BETWEEN #{dto.beginTime} and #{dto.endTime}
            </if>
            <if test="dto.ids != null and dto.ids.size() > 0 ">
                AND re.F_Id IN
                <foreach item="id" collection="dto.ids" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="dto.userIds != null and dto.userIds.size()>0">
                and re.F_UserId in
                <foreach collection="dto.userIds" item="id" open="("  separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="dto.userId != null and dto.userId !=''">
                and re.F_UserId =#{dto.userId} and  re.F_TransferStatus in ( 2,6)
            </if>
        </where>
        order by re.F_CreatorTime desc
    </select>

    <select id="getListForApp" resultType="jnpf.model.personnels.vo.regular.FtbPersonnelsRegularManagementVO">
        select
        re.F_Id  id,
        ros.F_Name userName,
        ros.F_WorkerStatus workingCondition,
        re.F_SystemWokerId systemWokerId,
        re.F_UserId userId,
        re.F_JobNumber jobNumber,
        re.F_OrgId orgId,
        re.F_OrgName orgName,
        re.F_RegularPostId regularPostId,
        re.F_RegularPostName regularPostName,
        re.F_RegularGradeId regularGradeId,
        re.F_RegularGradeName regularGradeName,
        re.F_DateOfEntry dateOfEntry,
        re.F_Probation probation,
        re.F_ProbationPeriodDay probationPeriodDay,
        re.F_SchedConverDate schedConverDate,
        re.F_ActualConverDate actualConverDate,
        re.F_TransferStatus transferStatus,
        re.F_ImmediateSuperId immediateSuperId,
        re.F_ImmediateSuperName immediateSuperName,
        re.F_CreatorUserId creatorUserId,
        re.F_CreatorTime creatorTime,
        re.F_LastModifyTime lastModifyTime,
        re.F_TaskInfoId  taskInfoId,
        re.F_ApplicationSource applicationSource,
        re.F_VersionNum versionNum,
        ros.F_JoinNum joinNum
        from ftb_personnels_regular_management as re
        left join ftb_personnels_staff_roster as ros on re.F_UserId  = ros.F_UserId
        <where>
            <if test="dto.flagByApp != null and dto.flagByApp !=''and dto.flagByApp != 4">
                and re.F_EnableMark = 0
            </if>

            and  ros.F_WorkerStatus not in ('304','305')
            <if test="dto.keyWord!= null and dto.keyWord!= ''">
                and   (
                ros.F_Name LIKE CONCAT('%',#{dto.keyWord},'%')
                )
            </if>
            <if test="dto.beginTime !=null and dto.endTime !=null">
                and  re.F_SchedConverDate BETWEEN #{dto.beginTime} and #{dto.endTime}
            </if>
            <if test="dto.userId != null and dto.userId !=''  and dto.flagByApp != 3 and dto.flagByApp != 1 ">
                and  re.F_UserId =#{dto.userId}
            </if>
            <if test="dto.approvalStatus != null and dto.approvalStatus !='' and dto.statusList == null ">
                and re.F_TransferStatus = #{dto.approvalStatus}
            </if>
            <if test="dto.statusList != null and dto.statusList.size() > 0 ">
                and re.F_TransferStatus in
                <foreach collection="dto.statusList" item="item" open="("  separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="dto.userIds != null  and ( dto.flagByApp !=3 or dto.flagByApp != 4 ) and dto.userIds.size() > 0">
                and re.F_UserId in
                <foreach collection="dto.userIds" item="id" open="("  separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
        order by re.F_CreatorTime desc
    </select>

    <select id="getThePendingApprovalList"
            resultType="jnpf.model.personnels.vo.regular.FtbPersonnelsRegularManagementVO">
        SELECT
        re.F_Id id,
        ros.F_Name userName,
        ros.F_WorkerStatus workingCondition,
        re.F_UserId userId,
        re.F_SystemWokerId systemWokerId,
        re.F_JobNumber jobNumber,
        re.F_OrgId orgId,
        re.F_OrgName orgName,
        re.F_RegularPostId regularPostId,
        re.F_RegularPostName regularPostName,
        re.F_RegularGradeId regularGradeId,
        re.F_RegularGradeName regularGradeName,
        re.F_DateOfEntry dateOfEntry,
        re.F_Probation probation,
        re.F_ProbationPeriodDay probationPeriodDay,
        re.F_SchedConverDate schedConverDate,
        re.F_ActualConverDate actualConverDate,
        re.F_TransferStatus transferStatus,
        re.F_ImmediateSuperId immediateSuperId,
        re.F_ImmediateSuperName immediateSuperName,
        re.F_ApplicationSource applicationSource,
        re.F_CreatorUserId creatorUserId,
        re.F_TaskInfoId taskInfoId
        FROM
        ftb_personnels_regular_management AS re
        INNER JOIN ftb_personnels_audit_run_task AS run ON run.F_BusinessId = re.F_Id
        INNER JOIN ftb_personnels_audit_master_config as conf ON run.F_MasterAuditConfigId = conf.F_Id
        left join ftb_personnels_staff_roster as ros on re.F_UserId  = ros.F_UserId
        WHERE  re.F_EnableMark =0
        and run.F_UserId =#{dto.userId}
        AND conf.F_ConfigType =#{dto.configType}
        order by re.F_CreatorTime desc
    </select>

    <select id="getAuditedDataList" resultType="jnpf.model.personnels.vo.regular.FtbPersonnelsRegularManagementVO">
        SELECT
        re.F_Id id,
        ros.F_Name userName,
        ros.F_WorkerStatus workingCondition,
        re.F_UserId userId,
        re.F_SystemWokerId systemWokerId,
        re.F_JobNumber jobNumber,
        re.F_OrgId orgId,
        re.F_OrgName orgName,
        re.F_RegularPostId regularPostId,
        re.F_RegularPostName regularPostName,
        re.F_RegularGradeId regularGradeId,
        re.F_RegularGradeName regularGradeName,
        re.F_DateOfEntry dateOfEntry,
        re.F_Probation probation,
        re.F_ProbationPeriodDay probationPeriodDay,
        re.F_SchedConverDate schedConverDate,
        re.F_ActualConverDate actualConverDate,
        re.F_TransferStatus transferStatus,
        info.F_ApprovalResult approvalResult,
        re.F_ImmediateSuperId immediateSuperId,
        re.F_ImmediateSuperName immediateSuperName,
        re.F_ApplicationSource applicationSource,
        re.F_CreatorUserId creatorUserId,
        re.F_TaskInfoId taskInfoId
        FROM
        ftb_personnels_regular_management AS re
        LEFT JOIN ftb_personnels_audit_run_task AS run ON run.F_BusinessId = re.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 re.F_UserId  = ros.F_UserId
        WHERE
        re.F_EnableMark = 0
        AND info.F_UserId = #{dto.userId}
        and  mas.F_ConfigType = #{dto.configType}
        <if test="dto.approvalStatus != null and dto.approvalStatus !='' and  dto.approvalStatus == 2 ">
            and re.F_TransferStatus = 2
        </if>
        <if test="dto.approvalStatus != null and dto.approvalStatus !='' and  dto.approvalStatus == 3 ">
            and re.F_TransferStatus = 3
        </if>
        <if test="dto.approvalStatus != null and dto.approvalStatus !='' and  dto.approvalStatus == 1 ">
            and re.F_TransferStatus = 1
        </if>
        <if test="dto.approvalStatus != null and dto.approvalStatus !='' and  dto.approvalStatus == 7 ">
            and re.F_TransferStatus in (1,2,3)
        </if>
        order by re.F_CreatorTime desc
    </select>

    <select id="getListForCC" resultType="jnpf.model.personnels.vo.regular.FtbPersonnelsRegularManagementVO">
        select DISTINCT e.* from  ( select
        re.F_Id  id,
        ros.F_Name userName,
        ros.F_WorkerStatus workingCondition,
        re.F_SystemWokerId systemWokerId,
        re.F_UserId userId,
        re.F_JobNumber jobNumber,
        re.F_OrgId orgId,
        re.F_OrgName orgName,
        re.F_RegularPostId regularPostId,
        re.F_RegularPostName regularPostName,
        re.F_RegularGradeId regularGradeId,
        re.F_RegularGradeName regularGradeName,
        re.F_DateOfEntry dateOfEntry,
        re.F_Probation probation,
        re.F_ProbationPeriodDay probationPeriodDay,
        re.F_SchedConverDate schedConverDate,
        re.F_ActualConverDate actualConverDate,
        re.F_TransferStatus transferStatus,
        re.F_ImmediateSuperId immediateSuperId,
        re.F_ImmediateSuperName immediateSuperName,
        re.F_CreatorUserId creatorUserId,
        re.F_CreatorTime creatorTime,
        re.F_TaskInfoId  taskInfoId,
        re.F_ApplicationSource applicationSource
        from ftb_personnels_regular_management as re
        left join ftb_personnels_staff_roster as ros on re.F_UserId  = ros.F_UserId
        <where>
            re.F_EnableMark = 0
            and  ros.F_WorkerStatus not in ('304','305')
            <if test="dto.approvalStatus != null and dto.approvalStatus !='' and dto.statusList == null ">
                and re.F_TransferStatus = #{dto.approvalStatus}
            </if>
            <if test="dto.statusList != null and dto.statusList.size() > 0 ">
                and re.F_TransferStatus in
                <foreach collection="dto.statusList" item="item" open="("  separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        order by re.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="queryNumberOfRegulars" resultType="java.lang.Long">
        SELECT
        COUNT(1)
        FROM
        ftb_personnels_regular_management AS re
        inner join ftb_personnels_staff_roster as ros on re.F_UserId  = ros.F_UserId
        <where>
            re.F_EnableMark = 0
            and ros.F_EnabledMark = 0
            AND re.F_TransferStatus in (2,6)
            and re.F_ActualConverDate  BETWEEN DATE_FORMAT( CURDATE(), '%Y-%m-01' )
            AND DATE_ADD( DATE_ADD( DATE_FORMAT( CURDATE(), '%Y-%m-01' ), INTERVAL 1 MONTH ), INTERVAL - 1 DAY )
            <if test="userIds != null and userIds.size() > 0 ">
                and re.F_UserId in
                <foreach item="item" collection="userIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="queryRegualList" resultType="jnpf.model.personnels.vo.regular.FtbPersonnelsRegularManagementVO">
        select
        re.F_Id  id,
        re.F_SystemWokerId systemWokerId,
        ros.F_Name userName,
        ros.F_WorkerStatus workingCondition,
        re.F_UserId userId,
        re.F_JobNumber jobNumber,
        re.F_OrgId orgId,
        re.F_OrgName orgName,
        re.F_RegularPostId regularPostId,
        re.F_RegularPostName regularPostName,
        re.F_RegularGradeId regularGradeId,
        re.F_RegularGradeName regularGradeName,
        re.F_DateOfEntry dateOfEntry,
        re.F_Probation probation,
        re.F_ProbationPeriodDay probationPeriodDay,
        re.F_SchedConverDate schedConverDate,
        re.F_ActualConverDate actualConverDate,
        re.F_TransferStatus transferStatus,
        re.F_ImmediateSuperId immediateSuperId,
        re.F_ImmediateSuperName immediateSuperName,
        re.F_ApplicationSource applicationSource,
        re.F_TaskInfoId taskInfoId,
        re.F_VersionNum versionNum
        from ftb_personnels_regular_management as re
        left join ftb_personnels_staff_roster as ros on re.F_UserId  = ros.F_UserId
        <where>
            re.F_EnableMark =0
            and  ros.F_WorkerStatus not in ('304','305')
            <if test="dto.keyWord!= null and dto.keyWord!= ''">
                And   (
                ros.F_Name LIKE CONCAT('%',#{dto.keyWord},'%')
                )
            </if>
            <if test="dto.approvalStatus != null and dto.approvalStatus !='' and dto.approvalStatus != 0 and dto.approvalStatus != 7 ">
                and re.F_TransferStatus =#{dto.approvalStatus}
            </if>
            <if test="dto.beginTime !=null and dto.endTime !=null">
                and  re.F_SchedConverDate BETWEEN #{dto.beginTime} and #{dto.endTime}
            </if>
            <if test="dto.ids != null and dto.ids.size() > 0 ">
                AND re.F_Id IN
                <foreach item="id" collection="dto.ids" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="dto.userIds != null and dto.userIds.size()>0">
                and re.F_UserId in
                <foreach collection="dto.userIds" item="id" open="("  separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="dto.userId != null and dto.userId !=''">
                and re.F_UserId =#{dto.userId} and  re.F_TransferStatus in ( 2,6)
            </if>
            <if test="dto.orgIds != null and dto.orgIds.size()>0">
                and re.F_OrgId in
                <foreach collection="dto.orgIds" item="id" open="("  separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
        order by re.F_CreatorTime desc
    </select>

    <select id="queryUserInfoForRegularization"
            resultType="jnpf.model.personnels.vo.regular.FtbPersonnelsRegularManagementVO">
        SELECT
        re.F_Id id,
        ros.F_Name userName,
        ros.F_WorkerStatus workingCondition,
        re.F_UserId userId,
        re.F_SystemWokerId systemWokerId,
        re.F_JobNumber jobNumber,
        re.F_OrgId orgId,
        re.F_OrgName orgName,
        re.F_RegularPostId regularPostId,
        re.F_RegularPostName regularPostName,
        re.F_RegularGradeId regularGradeId,
        re.F_RegularGradeName regularGradeName,
        re.F_DateOfEntry dateOfEntry,
        re.F_Probation probation,
        re.F_ProbationPeriodDay probationPeriodDay,
        re.F_SchedConverDate schedConverDate,
        re.F_ActualConverDate actualConverDate,
        re.F_TransferStatus transferStatus,
        re.F_ImmediateSuperId immediateSuperId,
        re.F_ImmediateSuperName immediateSuperName,
        re.F_ApplicationSource applicationSource,
        re.F_CreatorUserId creatorUserId,
        re.F_TaskInfoId taskInfoId
        FROM
        ftb_personnels_regular_management AS re
        left join ftb_personnels_staff_roster as ros on re.F_UserId  = ros.F_UserId
        <where>
            re.F_UserId in
            <foreach collection="userIds" item="id" open="("  separator="," close=")">
                #{id}
            </foreach>
            and re.F_EnableMark = 0
            and re.F_TransferStatus in (2,6)
            and  re.F_ActualConverDate between #{startDate} and #{endDate}
        </where>

    </select>

    <select id="getRegularizationStoreDimension" resultType="java.lang.String">
        SELECT
            F_UserId as userId
        FROM
            ftb_personnels_regular_management
        WHERE
            F_TransferStatus IN ( 2, 6 )
          AND F_EnableMark = 0
        and DATE_FORMAT(F_ActualConverDate, '%Y-%m') between #{startDate} and #{endDate}
        <if test="userIds != null and userIds.size() > 0">
            and F_UserId IN
            <foreach collection="userIds" open="(" close=")" separator="," item="userId">
                #{userId}
            </foreach>
        </if>
    </select>

    <select id="queryList" resultType="jnpf.model.personnels.vo.regular.FtbPersonnelsRegularManagementVO">
        select
        re.F_Id  id,
        re.F_SystemWokerId systemWokerId,
        ros.F_Name userName,
        ros.F_WorkerStatus workingCondition,
        re.F_UserId userId,
        re.F_JobNumber jobNumber,
        re.F_OrgId orgId,
        re.F_OrgName orgName,
        re.F_RegularPostId regularPostId,
        re.F_RegularPostName regularPostName,
        re.F_RegularGradeId regularGradeId,
        re.F_RegularGradeName regularGradeName,
        re.F_DateOfEntry dateOfEntry,
        re.F_Probation probation,
        re.F_ProbationPeriodDay probationPeriodDay,
        re.F_SchedConverDate schedConverDate,
        re.F_ActualConverDate actualConverDate,
        re.F_TransferStatus transferStatus,
        re.F_ImmediateSuperId immediateSuperId,
        re.F_ImmediateSuperName immediateSuperName,
        re.F_ApplicationSource applicationSource,
        re.F_TaskInfoId taskInfoId,
        re.F_VersionNum versionNum,
        ros.F_JoinNum joinNum
        from ftb_personnels_regular_management as re
        left join ftb_personnels_staff_roster as ros on re.F_UserId  = ros.F_UserId
        <where>
            re.F_EnableMark =0
            and  ros.F_WorkerStatus not in ('304','305')
            <if test="dto.keyWord!= null and dto.keyWord!= ''">
                And   (
                ros.F_Name LIKE CONCAT('%',#{dto.keyWord},'%')
                )
            </if>
            <if test="dto.approvalStatus != null and dto.approvalStatus !='' and dto.approvalStatus != 0 and dto.approvalStatus != 7 ">
                and re.F_TransferStatus =#{dto.approvalStatus}
            </if>
            <if test="dto.beginTime !=null and dto.endTime !=null">
                and  re.F_ActualConverDate BETWEEN #{dto.beginTime} and #{dto.endTime}
            </if>
            <if test="dto.ids != null and dto.ids.size() > 0 ">
                AND re.F_Id IN
                <foreach item="id" collection="dto.ids" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="dto.userIds != null and dto.userIds.size()>0">
                and re.F_UserId in
                <foreach collection="dto.userIds" item="id" open="("  separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="dto.userId != null and dto.userId !=''">
                and re.F_UserId =#{dto.userId} and  re.F_TransferStatus in ( 2,6)
            </if>
        </where>
        order by re.F_CreatorTime desc
    </select>
</mapper>