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

    <select id="getListInfo" resultType="jnpf.model.personnels.vo.apply.FtbPersonnelsApplyVO">
        SELECT
        apply.F_Id as id,
        ros.F_Name userName,
        ros.F_WorkerStatus workingCondition,
        apply.F_UserId userId,
        apply.F_PromotionReason promotionReason,
        apply.F_State state,
        apply.F_Source source,
        apply.F_PostId postId,
        apply.F_PostName postName,
        apply.F_PostRankId postRankId,
        apply.F_PostRankName postRankName,
        apply.F_PostPromotionId postPromotionId,
        apply.F_PostPromotionName postPromotionName,
        apply.F_PostRankPromotionId postRankPromotionId,
        apply.F_PostRankPromotionName postRankPromotionName,
        apply.F_CreatorTime   creatorTime,
        apply.F_LastModifyTime lastModifyTime,
        apply.F_OrgCurrentId orgCurrentId,
        apply.F_OrgCurrentName orgCurrentName,
        apply.F_OrgId orgId,
        apply.F_OrgName orgName,
        apply.F_TaskInfoId taskInfoId,
        ros.F_SystemWokerId systemWokerId,
        apply.F_PostPromotionPay postPromotionPay,
        apply.F_VersionNum versionNum
        FROM
        ftb_personnels_post_apply as apply
        left join ftb_personnels_staff_roster as ros on apply.F_UserId  = ros.F_UserId
        <where>
            <if test="dto.keyWord!= null and dto.keyWord!= ''">
                (
                ros.F_Name LIKE CONCAT('%',#{dto.keyWord},'%') OR
                apply.F_PostPromotionName LIKE CONCAT('%',#{dto.keyWord},'%') OR
                apply.F_PostName LIKE CONCAT('%',#{dto.keyWord},'%')
                )
            </if>
            <!-- 审核状态，转正审批状态(0,待审批,1审批中,2,审核通过,3,审核不通过,4,撤销,5未办理, 6手动,) -->
            <!-- 审批列表-->
            <if test="dto.flag == 1 ">
                <if test="dto.approvalStatus != null and dto.approvalStatus !='' and dto.approvalStatus != 0 and dto.approvalStatus != 7 ">
                    and apply.F_State =#{dto.approvalStatus}
                </if>
                <if test="dto.ids != null and dto.ids.size() > 0 ">
                    AND apply.F_Id IN
                    <foreach item="id" collection="dto.ids" open="(" separator="," close=")">
                        #{id}
                    </foreach>
                </if>
                and apply.F_State not in (2,6)
            </if>
            <!-- 已晋升-->
            <if test="dto.flag == 2 ">
                and apply.F_State in (2,6)
                <if test="dto.orgId != null and dto.orgId !=''">
                    and find_in_set(apply.F_OrgId,#{dto.orgId}) > 0
                </if>
                <if test="dto.postId != null and dto.postId !=''">
                    and (apply.F_PostId = #{dto.postId})
                </if>
                <if test="dto.gradeId != null and dto.gradeId !=''">
                    and  (apply.F_PostRankId = #{dto.gradeId} )
                </if>
                <if test="dto.beginTime !=null and dto.endTime !=null">
                    and  apply.F_CreatorTime BETWEEN #{dto.beginTime} and #{dto.endTime}
                </if>
            </if>
            <if test="dto.userIds != null and dto.userIds.size()>0">
                and apply.F_UserId in
                <foreach collection="dto.userIds" item="id" open="("  separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="dto.orgIds != null and dto.orgIds.size()>0">
                and apply.F_OrgId in
                <foreach collection="dto.orgIds" item="id" open="("  separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
        ORDER BY apply.F_State asc,apply.F_CreatorTime DESC
    </select>

    <select id="getListForApp" resultType="jnpf.model.personnels.vo.apply.FtbPersonnelsApplyVO">
        SELECT
        apply.F_Id as id,
        ros.F_Name userName,
        ros.F_WorkerStatus workingCondition,
        apply.F_UserId userId,
        apply.F_PromotionReason promotionReason,
        apply.F_State state,
        apply.F_Source source,
        apply.F_PostId postId,
        apply.F_PostName postName,
        apply.F_PostRankId postRankId,
        apply.F_PostRankName postRankName,
        apply.F_PostPromotionId postPromotionId,
        apply.F_PostPromotionName postPromotionName,
        apply.F_PostRankPromotionId postRankPromotionId,
        apply.F_PostRankPromotionName postRankPromotionName,
        apply.F_CreatorTime   creatorTime,
        apply.F_LastModifyTime lastModifyTime,
        apply.F_OrgCurrentId orgCurrentId,
        apply.F_OrgCurrentName orgCurrentName,
        apply.F_OrgId orgId,
        apply.F_OrgName orgName,
        apply.F_CreatorUserId creatorUserId,
        apply.F_TaskInfoId  taskInfoId,
        apply.F_SystemWokerId systemWokerId,
        apply.F_PostPromotionPay postPromotionPay,
        apply.F_VersionNum versionNum
        FROM
        ftb_personnels_post_apply  as apply
        left join ftb_personnels_staff_roster as ros on apply.F_UserId  = ros.F_UserId
        <where>
            <if test="dto.keyWord!= null and dto.keyWord!= ''">
                (
                ros.F_Name LIKE CONCAT('%',#{dto.keyWord},'%')
                )
            </if>
            <if test="dto.userId != null and dto.userId !='' and  dto.flagByApp != null and  dto.flagByApp == 4">
                and apply.F_UserId =#{dto.userId}
            </if>
            <if test="dto.orgIds != null and dto.orgIds.size()>0">
                and apply.F_OrgId in
                <foreach collection="dto.orgIds" item="id" open="("  separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
        order by apply.F_CreatorTime DESC
    </select>

    <select id="getListByApprovalForApp" resultType="jnpf.model.personnels.vo.apply.FtbPersonnelsApplyVO">
        SELECT
        DISTINCT
        apply.F_Id as id,
        apply.F_UserName userName,
        apply.F_UserId userId,
        apply.F_PromotionReason promotionReason,
        info.F_ApprovalResult approvalResult,
        apply.F_State state,
        apply.F_Source source,
        apply.F_PostId postId,
        apply.F_PostName postName,
        apply.F_PostRankId postRankId,
        apply.F_PostRankName postRankName,
        apply.F_PostPromotionId postPromotionId,
        apply.F_PostPromotionName postPromotionName,
        apply.F_PostRankPromotionId postRankPromotionId,
        apply.F_PostRankPromotionName postRankPromotionName,
        apply.F_CreatorTime   creatorTime,
        apply.F_LastModifyTime lastModifyTime,
        apply.F_OrgCurrentId orgCurrentId,
        apply.F_OrgCurrentName orgCurrentName,
        apply.F_OrgId orgId,
        apply.F_OrgName orgName,
        apply.F_CreatorUserId creatorUserId,
        apply.F_TaskInfoId taskInfoId
        FROM
        ftb_personnels_post_apply AS apply
        left JOIN ftb_personnels_audit_run_task AS run ON run.F_BusinessId = apply.F_Id
        <if test="( (dto.flagByApp = 2 and (dto.approvalStatus == 2 or dto.approvalStatus == 3) ) or dto.approvalStatus == 7)">
            left JOIN ftb_personnels_audit_run_task_history AS his ON his.F_BusinessId = apply.F_Id
        </if>
        left JOIN ftb_personnels_audit_task_info AS info ON (run.F_Id = info.F_RunTaskId
        <if test="( (dto.flagByApp = 2 and (dto.approvalStatus == 2 or dto.approvalStatus == 3) ) or dto.approvalStatus == 7)">
            or his.F_Id = info.F_RunTaskId
        </if>
        )
        <where>
            <if test="dto.approvalStatus != null and dto.approvalStatus !='' and dto.approvalStatus != 7">
                and apply.F_State = #{dto.approvalStatus}
            </if>
            <if test="dto.userId != null and dto.userId !='' and dto.approvalStatus == 0">
                and run.F_UserId =#{dto.userId}
            </if>
            <if test="dto.userId != null and dto.userId !='' and ( (dto.flagByApp = 2 and (dto.approvalStatus == 2 or dto.approvalStatus == 3) ) or dto.approvalStatus == 7)">
                and info.F_UserId =#{dto.userId}
            </if>
            <if test="dto.userIds != null and dto.userIds.size()>0">
                and apply.F_UserId in
                <foreach collection="dto.userIds" item="id" open="("  separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
        Order BY apply.F_CreatorTime
    </select>

    <select id="listOfDataToBeApproved" resultType="jnpf.model.personnels.vo.apply.FtbPersonnelsApplyVO">
        SELECT
        apply.F_Id AS id,
        ros.F_Name userName,
        ros.F_WorkerStatus workingCondition,
        apply.F_UserId userId,
        apply.F_PromotionReason promotionReason,
        apply.F_State state,
        apply.F_Source source,
        apply.F_PostId postId,
        apply.F_PostName postName,
        apply.F_PostRankId postRankId,
        apply.F_PostRankName postRankName,
        apply.F_PostPromotionId postPromotionId,
        apply.F_PostPromotionName postPromotionName,
        apply.F_PostRankPromotionId postRankPromotionId,
        apply.F_PostRankPromotionName postRankPromotionName,
        apply.F_CreatorTime creatorTime,
        apply.F_LastModifyTime lastModifyTime,
        apply.F_OrgCurrentId orgCurrentId,
        apply.F_OrgCurrentName orgCurrentName,
        apply.F_OrgId orgId,
        apply.F_OrgName orgName,
        apply.F_CreatorUserId creatorUserId,
        apply.F_TaskInfoId taskInfoId,
        apply.F_SystemWokerId systemWokerId,
        apply.F_PostPromotionPay postPromotionPay
        FROM
        ftb_personnels_post_apply AS apply
        LEFT JOIN ftb_personnels_audit_run_task AS run ON run.F_BusinessId = apply.F_Id
        left join ftb_personnels_staff_roster as ros on apply.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 conf.F_ConfigType = #{dto.configType}
        order by  apply.F_CreatorTime desc
    </select>

    <select id="getApprovalList" resultType="jnpf.model.personnels.vo.apply.FtbPersonnelsApplyVO">
        SELECT
        apply.F_Id AS id,
        ros.F_Name userName,
        ros.F_WorkerStatus workingCondition,
        apply.F_UserId userId,
        apply.F_PromotionReason promotionReason,
        info.F_ApprovalResult approvalResult,
        apply.F_State state,
        apply.F_Source source,
        apply.F_PostId postId,
        apply.F_PostName postName,
        apply.F_PostRankId postRankId,
        apply.F_PostRankName postRankName,
        apply.F_PostPromotionId postPromotionId,
        apply.F_PostPromotionName postPromotionName,
        apply.F_PostRankPromotionId postRankPromotionId,
        apply.F_PostRankPromotionName postRankPromotionName,
        apply.F_CreatorTime creatorTime,
        apply.F_LastModifyTime lastModifyTime,
        apply.F_OrgCurrentId orgCurrentId,
        apply.F_OrgCurrentName orgCurrentName,
        apply.F_OrgId orgId,
        apply.F_OrgName orgName,
        apply.F_CreatorUserId creatorUserId,
        apply.F_TaskInfoId taskInfoId ,
        apply.F_SystemWokerId systemWokerId,
        apply.F_PostPromotionPay postPromotionPay
        FROM
        ftb_personnels_post_apply AS apply
        LEFT JOIN ftb_personnels_audit_run_task AS run ON run.F_BusinessId = apply.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 apply.F_UserId  = ros.F_UserId
        WHERE
        info.F_UserId = #{dto.userId}
        and  mas.F_ConfigType = #{dto.configType}
        <if test="dto.approvalStatus != null and dto.approvalStatus !='' and  dto.approvalStatus == 2 ">
            and apply.F_State = 2
        </if>
        <if test="dto.approvalStatus != null and dto.approvalStatus !='' and  dto.approvalStatus == 3 ">
            and apply.F_State = 3
        </if>
        <if test="dto.approvalStatus != null and dto.approvalStatus !='' and  dto.approvalStatus == 1 ">
            and apply.F_State = 1
        </if>
        <if test="dto.approvalStatus != null and dto.approvalStatus !='' and  dto.approvalStatus == 7 ">
            and apply.F_State in (1,2,3)
        </if>
        order by apply.F_CreatorTime desc
    </select>

    <select id="getListForCC" resultType="jnpf.model.personnels.vo.apply.FtbPersonnelsApplyVO">
        select DISTINCT e.* from ( SELECT
        apply.F_Id as id,
        ros.F_Name userName,
        ros.F_WorkerStatus workingCondition,
        apply.F_UserId userId,
        apply.F_PromotionReason promotionReason,
        apply.F_State state,
        apply.F_Source source,
        apply.F_PostId postId,
        apply.F_PostName postName,
        apply.F_PostRankId postRankId,
        apply.F_PostRankName postRankName,
        apply.F_PostPromotionId postPromotionId,
        apply.F_PostPromotionName postPromotionName,
        apply.F_PostRankPromotionId postRankPromotionId,
        apply.F_PostRankPromotionName postRankPromotionName,
        apply.F_CreatorTime   creatorTime,
        apply.F_LastModifyTime lastModifyTime,
        apply.F_OrgCurrentId orgCurrentId,
        apply.F_OrgCurrentName orgCurrentName,
        apply.F_OrgId orgId,
        apply.F_OrgName orgName,
        apply.F_CreatorUserId creatorUserId,
        apply.F_TaskInfoId  taskInfoId,
        apply.F_SystemWokerId systemWokerId,
        apply.F_PostPromotionPay postPromotionPay
        FROM
        ftb_personnels_post_apply as apply
        left join ftb_personnels_staff_roster as ros on apply.F_UserId  = ros.F_UserId
        <where>
            <if test="dto.approvalStatus != null and dto.approvalStatus !='' and dto.statusList == null ">
                and apply.F_State = #{dto.approvalStatus}
            </if>
            <if test="dto.statusList != null and dto.statusList.size() > 0 ">
                and apply.F_State in
                <foreach collection="dto.statusList" item="item" open="("  separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        order by apply.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>
</mapper>