<?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.AttendanceGroupUserMapper">
<!--    批量修改考勤组用户-->
    <update id="batchUpdateGroupUser">
        update ftb_attendance_group_user
        set F_Type = #{type}
        where F_Id in
        <foreach collection="groupUserIds" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </update>

    <!-- 通用查询映射结果 -->
<!--    <resultMap id="BaseResultMap" type="com.chehejia.domain.AttendanceGroupUser">-->
<!--        <id column="F_Id" property="fId" />-->
<!--        <result column="F_UserId" property="fUserid" />-->
<!--        <result column="F_GroupId" property="fGroupid" />-->
<!--        <result column="F_Type" property="fType" />-->
<!--        <result column="F_StartTime" property="fStarttime" />-->
<!--        <result column="F_EndTime" property="fEndtime" />-->
<!--        <result column="F_CreatorUserId" property="fCreatoruserid" />-->
<!--        <result column="F_CreatorTime" property="fCreatortime" />-->
<!--        <result column="F_TenantId" property="fTenantid" />-->
<!--    </resultMap>-->
<!-- 获取考勤组用户生存周期记录-->
    <select id="viewGroupUserList" resultType="jnpf.model.attendance.vo.AttendanceGroupUserVo">
        SELECT
            fagu.F_UserId userId,
            fagu.F_TimeJson timeJson
        FROM
            ftb_attendance_group_user fagu
        WHERE fagu.F_GroupId = #{groupId} and fagu.F_DeleteMark = 0
    </select>
<!--    查询系统用户列表-->
    <select id="querySysUsers" resultType="jnpf.model.attendance.vo.AttendanceGroupUserVo">
        SELECT
            bu.F_Id userId,
            bu.F_RealName realName,
            fag.F_GroupName groupName,
            bu.F_HeadIcon headIcon,
            bu.F_OrganizeId orgId
        FROM
            base_user bu
                LEFT JOIN ftb_attendance_group_user fagu ON fagu.F_UserId = bu.F_Id
                LEFT JOIN ftb_attendance_group fag on fag.F_Id = fagu.F_GroupId
        WHERE
            bu.F_OrganizeId = #{orgId} and fagu.F_DeleteMark = 0
        <if test="name != null and name != ''">
            and bu.F_RealName concat('%',#{name},'%')
        </if>
    </select>
<!--    获取用户所有产生的考勤组集合-->
    <select id="getUserGroupList" resultType="jnpf.model.attendance.vo.AttendanceGroupUserVo">
        SELECT F_GroupId groupId,F_TimeJson timeJson ,F_Type type
        FROM ftb_attendance_group_user
        WHERE F_UserId = #{userId} and F_Type != 3 and F_DeleteMark = 0
    </select>
    <!-- 查询考勤组用户信息列表 -->
    <select id="getGroupUserList" resultType="jnpf.model.attendance.vo.MiniGroupVo">
        select agu.F_GroupId groupId, ag.F_GroupName groupName, agu.F_Type `type`,
        case aas.F_Permission when 0 then 0 else 1 end needApproval
        from ftb_attendance_group_user agu
        left join ftb_attendance_group ag on agu.F_GroupId = ag.F_Id
        left join ftb_attendance_approval_setting aas on aas.F_GroupId = ag.F_Id and aas.F_type = 2
        where agu.F_UserId = #{userId} and ag.F_Id in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
          #{item}
        </foreach>
        <if test="delStatus != null and delStatus == 0">
            and agu.F_DeleteMark = 0
        </if>
    </select>
    <!-- 查询自己的考勤组id -->
    <select id="getSelfGroup" resultType="java.lang.String">
        select F_GroupId from ftb_attendance_group_user where F_UserId = #{userId} and F_Type = 1 and F_DeleteMark = 0
    </select>

    <select id="getGroupInUserIds" resultType="jnpf.model.attendance.vo.AttendanceGroupUserVo">
        SELECT
            fag.F_Id groupId,
            fag.F_GroupName groupName,
            fagu.F_UserId userId,
            fagu.F_Type type
        FROM
            ftb_attendance_group fag
        INNER JOIN ftb_attendance_group_user fagu ON fagu.F_GroupId = fag.F_Id
        where fag.F_DeleteMark = 0 and fagu.F_DeleteMark = 0
        and fagu.F_UserId in
        <foreach close=")" collection="userIds" item="id" open="(" separator=",">
            #{id}
        </foreach>
        <if test="type != null">
            and fagu.F_Type = #{type}
        </if>
    </select>
    <!-- 查询考勤组成员(不含借调) -->
    <select id="getGroupUserListByIds" resultType="java.lang.String">
        select F_UserId from ftb_attendance_group_user
        where F_Type = 1 and F_DeleteMark = 0 and F_GroupId in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <!-- 查询考勤组成员 -->
    <select id="getUsersByGroupIds" resultType="jnpf.model.attendance.vo.AttendanceGroupUserVo">
        SELECT gu.F_UserId userId, gu.F_TimeJson timeJson, g.F_GroupName groupName
        FROM ftb_attendance_group_user gu
        LEFT JOIN ftb_attendance_group g on g.F_Id = gu.F_GroupId
        WHERE gu.F_DeleteMark = 0 and gu.F_GroupId IN
        <foreach collection="groupIds" index="index" item="item" open="(" separator="," close=")">
          #{item}
        </foreach>
    </select>
<!--    获取考勤组成员id-->
    <select id="getGroupUserIds" resultType="java.lang.String">
        SELECT DISTINCT(F_UserId)
        FROM ftb_attendance_group_user
        WHERE  F_GroupId IN
        <foreach collection="groupIds" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
         AND F_DeleteMark = 0 AND F_Type = 1 AND F_UserGroupType = 1 AND F_RemoveTime IS NULL
    </select>
</mapper>
