<?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.attendance.mapper.AttendanceManagerPermissionMapper" >

    <!--    获取指定考勤组用户的权限结合（含子权限）-->
    <select id="queryGroupUser" resultType="jnpf.model.attendance.vo.AttendanceGroupAdminVo">
        SELECT
            fam.F_UserId userId,
            GROUP_CONCAT( fpd.F_Name ) curPermissionJson,
            GROUP_CONCAT( fpd2.F_Name ) childPermissionJson
        FROM
            ftb_attendance_manager_permission fam
                LEFT JOIN ftb_permission_dict fpd ON fpd.F_Id = fam.F_PermissionId
                AND fam.F_Type = 1 AND fpd.F_Parent != 0
                LEFT JOIN ftb_permission_dict fpd2 ON fpd2.F_Id = fam.F_PermissionId
                AND fam.F_Type = 2 AND fpd2.F_Parent != 0
        WHERE
            fam.F_DeleteMark = 0
            and
            fam.F_GroupId = #{groupId}
        GROUP BY
            fam.F_UserId
    </select>
<!--    批量获取指定考勤组列表用户的权限结合（含子权限）-->
    <select id="queryGroupUsers" resultType="jnpf.model.attendance.vo.AttendanceGroupAdminVo">
        SELECT
            fam.F_UserId userId,
            GROUP_CONCAT( fpd.F_Name ) curPermissionJson,
            GROUP_CONCAT( fpd2.F_Name ) childPermissionJson
        FROM
            ftb_attendance_manager_permission fam
                LEFT JOIN ftb_permission_dict fpd ON fpd.F_Id = fam.F_PermissionId
                AND fam.F_Type = 1 AND fpd.F_Parent != 0
                LEFT JOIN ftb_permission_dict fpd2 ON fpd2.F_Id = fam.F_PermissionId
                AND fam.F_Type = 2 AND fpd2.F_Parent != 0
        WHERE
            fam.F_DeleteMark = 0
            and
            fam.F_GroupId in
            <foreach collection="groupIds" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        GROUP BY
            fam.F_UserId
    </select>
<!--    获取用户管理员权限类型-->
    <select id="getByUserId" resultType="jnpf.entity.PermissionDict">
        SELECT
            fpd.F_Id id,
            fpd.F_Name name,
            fpd.F_Parent parent
        FROM
            ftb_permission_dict fpd
        LEFT JOIN ftb_attendance_manager_permission famp ON fpd.F_Id = famp.F_PermissionId
        where fpd.F_DeleteMark = 0
        and famp.F_GroupId = #{groupId}
        <if test="userId != null and userId != ''">
              and famp.F_UserId = #{userId}
          </if>
          <if test="type != null">
              and famp.F_Type = #{type}
          </if>
          and fpd.F_ModuleType = #{moduleType}

    </select>
<!--    批量获取指定考勤组列表及以上用户的超级管理员权限-->
    <select id="queryUserForCurrAndUpChildAndSuper" resultType="jnpf.model.attendance.vo.AttendancePermissionVo">
        select t.userId,t.groupId from (
        select p.F_UserId userId,p.F_GroupId groupId from ftb_attendance_manager_permission p where p.F_GroupId in
        <foreach collection="groupIdList" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        and p.F_DeleteMark = 0
        union all
        select p.F_UserId userId,g.F_Id groupId from ftb_attendance_manager_permission p left join ftb_attendance_group g on p.F_GroupId =g.F_ParentId  where p.F_Type = 2 and g.F_Id in
        <foreach collection="groupIdList" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        and p.F_DeleteMark = 0

        union all
        select p.F_UserId userId, "-1" groupId from ftb_attendance_manager_permission p WHERE p.F_Type = 0 and p.F_DeleteMark = 0
        ) t
        GROUP BY t.userId,t.groupId
    </select>

    <!--    批量获取指定权限指定考勤组列表及以上用户的超级管理员权限-->
    <select id="queryPermissionBySpecify" resultType="jnpf.model.attendance.vo.AttendancePermissionVo">
        select t.userId,t.groupId from (
        select p.F_UserId userId,p.F_GroupId groupId from ftb_attendance_manager_permission p left join ftb_permission_dict d on d.F_Id = p.F_PermissionId where p.F_GroupId in
        <foreach collection="groupIdList" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        and p.F_DeleteMark = 0
        and d.F_ModuleType = 1 and d.F_Name in
        <foreach collection="parentCodes" item="code" open="(" separator="," close=")">
            #{code}
        </foreach>
        union all
        select p.F_UserId userId,g.F_Id groupId from ftb_attendance_manager_permission p left join ftb_attendance_group g on p.F_GroupId =g.F_ParentId left join ftb_permission_dict d on d.F_Id = p.F_PermissionId  where p.F_Type = 2 and g.F_Id in
        <foreach collection="groupIdList" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        and p.F_DeleteMark = 0
        and d.F_ModuleType = 2 and d.F_Name in
        <foreach collection="childCodes" item="code" open="(" separator="," close=")">
            #{code}
        </foreach>
        union all
        select p.F_UserId userId, "-1" groupId from ftb_attendance_manager_permission p WHERE p.F_Type = 0 and p.F_DeleteMark = 0
        ) t
        GROUP BY t.userId,t.groupId
    </select>

<!--    获取考勤组管理员权限类型-->
    <select id="queryMyPermissions" resultType="jnpf.model.attendance.vo.AttendancePermissionVo">
        SELECT
            fpd.F_Id id,
            fpd.F_Name name,
            fpd.F_Code code,
            famp.F_GroupId groupId,
            famp.F_UserId userId,
            famp.F_Type
        FROM
            ftb_attendance_manager_permission famp
                LEFT JOIN ftb_permission_dict fpd ON fpd.F_Id = famp.F_PermissionId
        WHERE
            1 = 1
          ADN famp.F_DeleteMark = 0
          And famp.F_GroupId = #{groupId}
        <if test="type != null">
            AND famp.F_Type = #{type}
        </if>
        <if test="userId != null and userId != ''">
            AND famp.F_UserId = #{userId}
        </if>
    </select>
<!--    获取用户在考勤组列表里面的管理员权限集合-->
    <select id="queryInGroupIds" resultType="jnpf.model.attendance.vo.CurUserPermissionVo">
        SELECT
            famp.F_GroupId groupId,
            GROUP_CONCAT(fpd.F_Name) allPermissions

        FROM
            ftb_attendance_manager_permission famp
                LEFT JOIN ftb_permission_dict fpd on fpd.F_Id = famp.F_PermissionId
        WHERE
            famp.F_Type = 2
            and famp.F_UserId = #{userId}
            and famp.F_GroupId in
        <foreach collection="groupIdList" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        GROUP BY famp.F_GroupId
    </select>
<!--    获取用户在指定考勤组的权限列表-->
    <select id="queryByGroupId" resultType="jnpf.model.attendance.vo.CurUserPermissionVo">
        SELECT
        famp.F_GroupId groupId,
        GROUP_CONCAT(fpd.F_Name) allPermissions

        FROM
        ftb_attendance_manager_permission famp
        LEFT JOIN ftb_permission_dict fpd on fpd.F_Id = famp.F_PermissionId
        WHERE
        famp.F_Type = 2
        and famp.F_UserId = #{userId}
        and famp.F_GroupId = #{groupId}
          <if test="permissionName != null and permissionName != ''">
              and fpd.F_Name = #{permissionName}
          </if>
        GROUP BY famp.F_GroupId
    </select>
<!--    获取考勤组权限列表里面包含指定编码层级的考勤组列表-->
    <select id="queryGroupVoInGroupIds" resultType="jnpf.model.attendance.vo.AttendanceGroupVo">
        SELECT
        fag.F_Id id,
        fag.F_GroupName groupName,
        fag.F_DetailName detailName,
        fag.F_ParentId parentId,
        fag.F_LevelCode levelCode

        FROM
        ftb_attendance_group fag
        LEFT JOIN ftb_attendance_manager_permission famp on famp.F_GroupId = fag.F_Id
        LEFT JOIN ftb_permission_dict fpd on fpd.F_Id = famp.F_PermissionId
        WHERE
        <foreach collection="levelCodeList" item="levelCode" open="(" separator="or" close=")">
            fag.F_LevelCode like concat("%",#{levelCode},"%")
        </foreach>
        and fag.F_DeleteMark = 0
        GROUP BY fag.F_Id
    </select>
<!--    获取考勤组的指定类型管理员及权限集合-->
    <select id="queryManagerByGroupIds" resultType="jnpf.model.attendance.vo.QueryGroupAdminVo">
        SELECT
        famp.F_UserId userId,
        GROUP_CONCAT(fpd.F_Name) permissions

        FROM
        ftb_attendance_manager_permission famp
        LEFT JOIN ftb_permission_dict fpd on fpd.F_Id = famp.F_PermissionId
        WHERE 1=1

        <if test="type != null">
          and famp.F_Type = #{type}
        </if>
        and famp.F_GroupId in
        <foreach collection="groupIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        GROUP BY famp.F_UserId
    </select>
<!--    获取用户在指定考勤组的权限列表-->
    <select id="queryGroupUserPermissionByUserId" resultType="jnpf.model.attendance.vo.AttendanceGroupAdminVo">
        SELECT
            fam.F_UserId userId,
            GROUP_CONCAT( fpd.F_Name ) curPermissionJson,
            GROUP_CONCAT( fpd2.F_Name ) childPermissionJson
        FROM
            ftb_attendance_manager_permission fam
                LEFT JOIN ftb_permission_dict fpd ON fpd.F_Id = fam.F_PermissionId
                AND fam.F_Type = 1 AND fpd.F_Parent != 0
                LEFT JOIN ftb_permission_dict fpd2 ON fpd2.F_Id = fam.F_PermissionId
            AND fam.F_Type = 2 AND fpd2.F_Parent != 0
        WHERE
            fam.F_DeleteMark = 0
          and
            fam.F_GroupId = #{groupId} AND fam.F_UserId = #{userId}
        GROUP BY
            fam.F_UserId
    </select>
    <!--    批量保存考勤组管理员信息-->
    <insert id="batchSaveGroupAdmin">
        INSERT INTO ftb_attendance_manager_permission (F_Id, F_Type, F_GroupId, F_UserId, F_PermissionId, F_CreatorUserId, F_CreatorTime)
        VALUES
        <foreach collection ="curGroupAdminList" item="admin" separator =",">
            (#{admin.id}, #{admin.type}, #{admin.groupId}, #{admin.userId}, #{admin.permissionId}, #{admin.creatorUserId}, #{admin.creatorTime})
        </foreach>
    </insert>

    <!--    更新用户考勤组管理员权限 -->
    <update id="updateUserGroupManagerPermission">
        UPDATE ftb_attendance_manager_permission
        SET F_GroupId = #{groupId1}
        WHERE F_GroupId = #{groupId} AND F_UserId = #{userId}
    </update>

    <update id="updateBatchUserGroupManagerPermission">
        UPDATE ftb_attendance_manager_permission
        SET F_GroupId = #{groupId}
        WHERE F_GroupId = #{oldGroupId} AND F_UserId in
        <foreach collection="userIds" item="userId" separator="," open="(" close=")">
            #{userId}
        </foreach>
    </update>

<!--    获取用户在指定考勤组的权限列表-->
    <select id="getGroupManagerPermission" resultType="jnpf.entity.AttendanceManagerPermission">
        SELECT F_Id id,F_Type type,F_GroupId groupId,F_UserId userId,F_PermissionId permissionId,F_CreatorUserId creatorUserId
        FROM ftb_attendance_manager_permission
        WHERE F_GroupId = #{groupId} AND F_UserId = #{userId}
    </select>
<!--    获取用户集合在指定考勤组的权限信息-->
    <select id="getGroupManagerPermissionByUserIds" resultType="jnpf.entity.AttendanceManagerPermission">
        SELECT F_Id id,F_Type type,F_GroupId groupId,F_UserId userId,F_PermissionId permissionId,F_CreatorUserId creatorUserId
        FROM ftb_attendance_manager_permission
        WHERE F_GroupId = #{groupId} AND F_UserId in
        <foreach collection="userIds" item="userId" separator="," open="(" close=")">
            #{userId}
        </foreach>
    </select>
<!--    批量添加-->
    <insert id="addBatch">
        INSERT INTO ftb_attendance_manager_permission
            (F_Id, F_Type, F_GroupId, F_UserId, F_PermissionId, F_CreatorUserId, F_CreatorTime)
        VALUES
            <foreach collection="addSchedules" item="admin" separator=",">
                (#{admin.id}, #{admin.type}, #{admin.groupId}, #{admin.userId}, #{admin.permissionId}, #{admin.creatorUserId}, now())
            </foreach>
    </insert>
<!--    根据用户Id和考勤组Id删除对应的权限记录-->
    <delete id="deleteByGroupUserId">
        delete from ftb_attendance_manager_permission
        where F_GroupId = #{groupId} and F_UserId = #{userId}
    </delete>


</mapper>