<?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.AttendanceGroupMapper" >
<!--    查询用户管理的考勤组列表（考勤组名称模糊搜索）-->
    <select id="queryManagerGroupList" resultType="jnpf.entity.AttendanceGroup">
        SELECT
            fag.F_Id id,
            fag.F_GroupName groupName,
            fag.F_ParentId parentId,
            fag.F_LevelCode levelCode
        FROM
            ftb_attendance_group fag
        INNER JOIN ftb_attendance_manager_permission famp ON famp.F_GroupId = fag.F_Id
        WHERE
            fag.F_DeleteMark = 0
            and
            famp.F_Type = 1
            and
            famp.F_UserId = #{userId}
        <if test="keyword != null and keyword != ''">
            and fag.F_GroupName like concat("%",#{keyword},"%")
        </if>
        group BY fag.F_Id
        ORDER BY fag.F_CreatorTime desc
    </select>
<!--    查询用户管理的考勤组列表-->
    <select id="queryGroupList" resultType="jnpf.model.attendance.vo.AttendanceGroupVo">
        SELECT
            fag.F_Id id,
            fag.F_GroupName groupName,
            fag.F_LevelCode levelCode,
            fag.F_ParentId parentId,
            famp.F_UserId,
            GROUP_CONCAT(fpd.F_Name) allPermissions
        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 famp.F_UserId = #{userId}
        GROUP BY fag.F_Id
    </select>
    <!-- 查询考勤组名称 -->
    <select id="getGroupName" resultType="java.lang.String">
        select F_GroupName from ftb_attendance_group where F_Id = #{groupId}
    </select>
<!--    获取考勤组详情-->
    <select id="getGroupDetail" resultType="jnpf.entity.AttendanceGroup">
        SELECT F_Id id,  F_GroupName groupName,   F_ParentId parentId, F_OrgId orgId,  F_LevelCode levelCode,F_LeaveSetting leaveSetting ,F_AttendanceClassSetting attendanceClassSetting
        FROM ftb_attendance_group
        where F_Id = #{groupId}
    </select>
<!--    批量获取考勤组信息-->
    <select id="getGroupDetailList" resultType="jnpf.entity.AttendanceGroup">
        SELECT F_Id id,  F_GroupName groupName,   F_ParentId parentId,  F_LevelCode levelCode,F_LeaveSetting leaveSetting  ,F_AttendanceClassSetting attendanceClassSetting
        FROM ftb_attendance_group
        where F_Id in
              <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
                #{item}
              </foreach>
    </select>
    <!-- 查询被删除的考勤组 -->
    <select id="selectDelIds" resultType="java.lang.String">
        select F_Id from ftb_attendance_group where F_DeleteMark = 1 and F_Id in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
<!--    获取所有考勤组-->
    <select id="getAllGroupList" 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,
            fag.F_OrgId orgId,
            fag.F_LockedDate lockedDate,
            fag.F_DeleteMark deleteMark,
            fag.F_LastModifyTime endDay
        FROM
            ftb_attendance_group fag
    </select>
<!--    通过组织Id找到组织下的考勤组-->
    <select id="groupListByOrgId" resultType="jnpf.model.attendance.vo.AttendanceGroupVo">
        SELECT
            F_Id id,
            F_GroupName groupName,
            F_DetailName detailName,
            F_ParentId parentId,
            F_LevelCode levelCode,
            F_OrgId orgId
        FROM
            ftb_attendance_group
        where F_DeleteMark = 0 and F_OrgId = #{orgId}
    </select>
<!--    通过考勤组Id获取绑定的组织-->
    <select id="getGroupBindingOrg" resultType="jnpf.model.attendance.vo.AttendanceGroupVo">
        SELECT
            F_Id id,
            F_GroupName groupName,
            F_DetailName detailName,
            F_ParentId parentId,
            F_LevelCode levelCode,
            F_OrgId orgId,
            F_LockedDate lockedDate
        FROM
            ftb_attendance_group
        where F_DeleteMark = 0 and F_Id = #{groupId}
    </select>
    <!--    绑定考勤组的组织-->
    <update id="updateGroupOrg">
        update ftb_attendance_group
        set F_OrgId = #{attendanceGroupOrgDto.orgId}
        where F_Id = #{attendanceGroupOrgDto.groupId}
    </update>
<!--    查询用户所在考勤组-->
    <select id="getAttendanceUserGroup" resultType="jnpf.attendance.dto.AttendanceUserGroupVo">
        SELECT
        u.F_UserId userId ,
        g.F_Id groupId,
        g.F_GroupName groupName
        FROM ftb_attendance_group_user u
        LEFT JOIN ftb_attendance_group g ON g.F_Id = u.F_GroupId
        WHERE F_Type = 1  and g.F_DeleteMark = 0 and u.F_DeleteMark = 0
          AND u.F_UserId IN
        <foreach collection="userIds" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <select id="getGroupLockDate" resultType="java.util.Date">
        select F_LockedDate from ftb_attendance_group where F_Id = #{groupId}
    </select>
    <!-- 查询父级考勤组id -->
    <select id="getParentGroupId" resultType="java.lang.String">
        select F_ParentId from ftb_attendance_group where F_Id = #{groupId}
    </select>
    <!-- 查询本级及子级考勤组 -->
    <select id="getSelfAndChildrenGroup" resultType="jnpf.model.attendance.vo.GroupNodeVo">
        select F_Id id, F_GroupName groupName, F_ParentId pid, F_AttendancePointsSetting attendancePointsSetting, F_LevelCode levelCode,
        F_AttendanceClassSetting attendanceClassSetting, F_BaseSetting baseSetting
        from ftb_attendance_group
        where find_in_set(#{groupId}, replace(F_LevelCode, '#', ',')) and F_DeleteMark = 0
    </select>
    <!-- 批量查询用户所在考勤组-->
    <select id="getAttendanceUserListGroupVO" resultType="jnpf.attendance.dto.AttendanceUserListGroupVO">
        SELECT g.F_Id groupId, u.F_UserId userId ,g.F_GroupName groupName
        FROM ftb_attendance_group_user u
        LEFT JOIN ftb_attendance_group g ON g.F_Id = u.F_GroupId
        WHERE u.F_Type = 1
        AND u.F_DeleteMark = 0 AND g.F_DeleteMark = 0
        AND u.F_UserId IN
        <foreach collection="userIds" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <!-- 查询所有考勤组(层级排序) -->
    <select id="getAllGroupByLevel" resultType="jnpf.model.attendance.vo.attendance.GroupMiniVo">
        select t.groupId, t.groupName from (
          select F_Id groupId, F_GroupName groupName, length(F_LevelCode) - length(replace(F_LevelCode, '#', '')) levelNum
          from ftb_attendance_group
          where F_DeleteMark = 0
          <if test="null != list and list.size > 0">
              and F_Id in
              <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
                  #{item}
              </foreach>
          </if>
        ) t order by t.levelNum asc
    </select>
<!--    查询用户是否拥有指定考勤组的指定权限-->
    <select id="appointPermissionByGroupId" resultType="java.lang.Integer">
        SELECT COUNT(0)
        FROM	ftb_attendance_manager_permission
        WHERE   F_GroupId = #{groupId}   AND F_UserId = #{userId} AND F_DeleteMark = 0
          AND F_PermissionId = (SELECT F_Id FROM ftb_permission_dict WHERE F_PermissionCode = #{permissionCode} AND F_DeleteMark = 0)
          AND F_Type = #{type}
    </select>

    <select id="getGroupListByOrgId" 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,
        fag.F_OrgId orgId,
        fag.F_LockedDate lockedDate,
        fag.F_DeleteMark deleteMark,
        fag.F_LastModifyTime endDay
        FROM
        ftb_attendance_group fag
        where  1 = 1
        <if test="null != groupQueryDto and null != groupQueryDto.name and  '' != groupQueryDto.name">
            and fag.F_GroupName like concat("%",#{groupQueryDto.name},"%")
        </if>
        <if test="null != groupQueryDto and null != groupQueryDto.type  and 0 == groupQueryDto.type">
            and fag.F_DeleteMark = 0
        </if>
        <if test="orgIds!=null and orgIds.size()>0">
            and fag.F_OrgId in
            <foreach collection="orgIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY
        fag.F_Id
    </select>
    <select id="getGroupListByIds" 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,
            fag.F_OrgId orgId,
            fag.F_LockedDate lockedDate,
            fag.F_DeleteMark deleteMark,
            fag.F_LastModifyTime endDay
        FROM
            ftb_attendance_group fag
        where
            <if test="groupIds !=null and groupIds.size()>0">
                 fag.F_Id in
                <foreach collection="groupIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        GROUP BY
        fag.F_Id
    </select>
    <!-- 查询组织id -->
    <select id="getOrgId" resultType="java.lang.String">
        select F_OrgId from ftb_attendance_group where F_Id = #{groupId}
    </select>
</mapper>