<?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.authority.mapper.FtbPermissionRoleAuthorizePersonMapper">

  <select id="queryUserScopeOfPermission" resultType="jnpf.model.authority.vo.role.FtbPermissionRolePersonVO">
    SELECT b.F_FunctionWebId as webId,b.F_FunctionAppId as appId,b.F_ScopePermission as scope,b.F_SpecifyOrgIds as orgIds,
    b.F_FunctionPageWebId as functionPageWebId,b.F_FunctionPageAppId as functionPageAppId,b.F_FunctionPcId as pcId,
    b.F_FunctionPagePcId as functionPagePcId,b.F_Id as roleMenuId
    FROM ftb_permission_role_authorize_person a JOIN ftb_permission_role_menu b ON a.F_RoleId=b.F_RoleId
    join ftb_permission_role c on a.F_RoleId=c.F_Id
    WHERE c.F_EnableMark = 0 AND a.F_EnableMark = 0 AND b.F_EnableMark = 0  and b.F_RoleEnabled = 1 AND a.F_UserId = #{userId}
  </select>

  <select id="jobAuthorizationInformation" resultType="jnpf.model.authority.vo.role.FtbPermissionRolePersonVO">
    SELECT b.F_FunctionWebId as webId,b.F_FunctionAppId as appId,b.F_ScopePermission as scope,b.F_SpecifyOrgIds as orgIds,
    b.F_FunctionPageWebId as functionPageWebId,b.F_FunctionPageAppId as functionPageAppId,b.F_FunctionPcId as pcId,
    b.F_FunctionPagePcId as functionPagePcId,b.F_Id as roleMenuId
    FROM ftb_permission_role_authorize_post a JOIN ftb_permission_role_menu b ON a.F_RoleId=b.F_RoleId
    join ftb_permission_role c on a.F_RoleId=c.F_Id
    WHERE c.F_EnableMark = 0 and b.F_EnableMark = 0 and a.F_EnableMark = 0 and b.F_RoleEnabled = 1 AND a.F_PostId = #{positionId}
  </select>

  <select id="employeePermission" resultType="jnpf.model.authority.vo.person.FtbEmployeePermissionPersonnelVO">
    SELECT
    F_UserId as userId,
    GROUP_CONCAT(F_RoleId)  as roleIds
    FROM
    `ftb_permission_role_authorize_person`
    <where>
      <if test="userIds != null and userIds.size() > 0">
        F_UserId IN
        <foreach item="item" index="index" collection="userIds" open="(" separator="," close=")">
          #{item}
        </foreach>
      </if>
      and F_EnableMark = 0
    </where>
    GROUP BY F_UserId
    ORDER BY F_CreatorTime ASC
  </select>

  <select id="authorizedPersonnel" resultType="jnpf.model.authority.vo.person.FtbAuthorizedPersonnelVO">
    SELECT
    a.F_UserId as userId,
    d.name as userName,
    a.F_CreatorUserId as operatorUserId,
    (select F_Name from ftb_personnels_staff_roster where F_UserId = a.F_CreatorUserId) as operatorUserName,
    a.F_CreatorTime as operateTime,
    a.F_Id as id
    FROM
    `ftb_permission_role_authorize_person` a
    inner join  (
   select b.F_UserId,b.F_Name as name from ftb_personnels_staff_roster b where ( b.F_WorkerStatus not IN ("305") and b.F_EnabledMark = 0 )
    union
    select c.F_UserId,c.F_WorkerName as name from   ftb_personnels_staff_employment_apply c where ( (c.F_IsNeedCheck = 0 and c.F_CheckStatus not in (3, 5)))
    ) as d on a.F_UserId = d.F_UserId
    <where>
      a.F_RoleId = #{roleId}
      and a.F_EnableMark = 0
    </where>
    ORDER BY a.F_CreatorTime ASC
  </select>

  <select id="employeePermissionLPage" resultType="jnpf.model.authority.vo.person.FtbEmployeePermissionPersonnelVO">
    SELECT
      *
    FROM
      (
        SELECT
          b.F_UserId userId,
          b.F_Name userName,
          b.F_ActualStartDate createTime
        FROM
          ftb_personnels_staff_roster b
        WHERE
          b.F_WorkerStatus NOT IN ( "305" )
          AND b.F_EnabledMark = 0 UNION
        SELECT
          c.F_UserId userId,
          c.F_WorkerName userName,
          c.F_CreatorTime createTime
        FROM
          ftb_personnels_staff_employment_apply c
        WHERE
        c.F_IsNeedCheck = 0
        AND c.F_CheckStatus IN ( 0, 1,3,4 )
    ) d
      <where>
        <if test="userName != null and userName != ''">
          and d.userName like concat('%',#{userName},'%')
        </if>
        <if test="userId != null and userId != ''">
         and  d.userId = #{userId}
        </if>
        <if test="userIds != null and userIds.size() != 0">
          and d.userId IN
          <foreach item="item" index="index" collection="userIds" open="(" separator="," close=")">
            #{item}
          </foreach>
        </if>
        and d.userId is not null and d.userId != '349057407209541'
      </where>
    ORDER BY
      d.createTime DESC
  </select>

  <select id="getPeopleDataPermissions" resultType="jnpf.model.authority.vo.role.FtbPermissionRolePersonVO">
    SELECT F_FunctionWebId as webId,F_FunctionAppId as appId,F_ScopePermission as scope,F_SpecifyOrgIds as orgIds
    from ftb_permission_role_person_user_relation WHERE F_UserId = #{userId} AND F_RoleEnabled = 1 AND F_EnableMark = 0
  </select>

  <select id="queryUserScopeOfPermissionWithUserIds"
          resultType="jnpf.model.authority.vo.role.FtbPermissionRolePersonVO">
    SELECT b.F_FunctionWebId as webId,b.F_FunctionAppId as appId,b.F_ScopePermission as scope,b.F_SpecifyOrgIds as orgIds,
    b.F_FunctionPageWebId as functionPageWebId,b.F_FunctionPageAppId as functionPageAppId,a.F_UserId as userId
    FROM ftb_permission_role_authorize_person a JOIN ftb_permission_role_menu b ON a.F_RoleId=b.F_RoleId
    join ftb_permission_role c on a.F_RoleId=c.F_Id
    WHERE c.F_EnableMark = 0 AND a.F_EnableMark = 0 AND b.F_EnableMark = 0  and b.F_RoleEnabled = 1
    and a.F_UserId IN
    <foreach item="item" index="index" collection="userIds" open="(" separator="," close=")">
      #{item}
    </foreach>
    group by a.F_UserId
  </select>

  <select id="jobAuthorizationInformationWithPositionIds"
          resultType="jnpf.model.authority.vo.role.FtbPermissionRolePersonVO">

    SELECT b.F_FunctionWebId as webId,b.F_FunctionAppId as appId,b.F_ScopePermission as scope,b.F_SpecifyOrgIds as orgIds,
    b.F_FunctionPageWebId as functionPageWebId,b.F_FunctionPageAppId as functionPageAppId,a.F_PostId as postId
    FROM ftb_permission_role_authorize_post a JOIN ftb_permission_role_menu b ON a.F_RoleId=b.F_RoleId
    join ftb_permission_role c on a.F_RoleId=c.F_Id
    WHERE c.F_EnableMark = 0 and b.F_EnableMark = 0 and a.F_EnableMark = 0 and b.F_RoleEnabled = 1
    and a.F_PostId IN
    <foreach item="item" index="index" collection="positionIds" open="(" separator="," close=")">
      #{item}
    </foreach>
    group by a.F_PostId
  </select>

  <select id="getPeopleDataPermissionsWithUserIds" resultType="jnpf.model.authority.vo.role.FtbPermissionRolePersonVO">
      SELECT F_FunctionWebId as webId,F_FunctionAppId as appId,F_ScopePermission as scope,F_SpecifyOrgIds as orgIds,F_UserId as userId
      from ftb_permission_role_person_user_relation WHERE
    F_UserId  in
      <foreach item="item" index="index" collection="userIds" open="(" separator="," close=")">
        #{item}
      </foreach>
     AND F_RoleEnabled = 1 AND F_EnableMark = 0 group by F_UserId
  </select>
</mapper>