<?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="com.ruoyi.zhushi.mapper.DkRecordMapper">
    <resultMap id="DkRecordResult" type="com.ruoyi.zhushi.entity.DkCheckInRecord">
        <id property="id" column="id"/>
        <result property="sysUserId" column="sys_user_id"/>
        <result property="sysUserName" column="sys_user_name"/>
        <result property="checkInTime" column="check_in_time"/>
        <result property="latitude" column="latitude"/>
        <result property="longitude" column="longitude"/>
        <result property="checkInStatus" column="check_in_status"/>
        <result property="checkInType" column="check_in_type"/>
    </resultMap>

    <select id="getCurrentDayRecord" resultType="com.ruoyi.zhushi.entity.DkCheckInRecord">
        select * from dk_check_in_record
        <where>
            DATE(check_in_time) = CURDATE()
            <if test="userId != null and userId != ''">
                AND sys_user_id = #{userId}
            </if>

            <if test="currentDate != null ">
                AND DATE(create_time) = #{currentDate}
            </if>
        </where>
    </select>

    <select id="queryConfigByUserId" resultType="com.ruoyi.zhushi.entity.DkAttendanceGroup">
        select DISTINCT t.* from dk_check_in_attendance_team_and_user  tu
        left join dk_check_in_attendance_team t on t.id = tu.attendance_team_id
        <where>
            <if test="userId != null and userId != ''">
                AND tu.user_id = #{userId}
            </if>
        </where>
    </select>

    <select id="queryAllUsers" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            u.user_id userId,
            u.user_name userName,
            u.nick_name nickName,
            d.dept_name deptName,
            u.joined_date joinedDate
        FROM
            sys_user u inner join sys_dept d on u.dept_id=d.dept_id
        WHERE
            u.del_flag = 0
          AND u.dept_id IN ( SELECT dept_id FROM sys_dept WHERE FIND_IN_SET( '100', ancestors ) OR dept_id = 100 )
          AND u.user_id != 1
        <if test="attendanceGroupIds != null and attendanceGroupIds.size() > 0">
            AND u.user_id IN (
            SELECT user_id
            FROM dk_check_in_attendance_team_and_user
            WHERE attendance_team_id IN
            <foreach collection="attendanceGroupIds" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
            )
        </if>
    </select>

    <select id="getRecordHistory" resultType="com.ruoyi.zhushi.entity.DkCheckInRecord">
        select * from dk_check_in_record
        <where>
            DATE(check_in_time) = CURDATE()
            <if test="userId != null">
                AND sys_user_id = #{userId}
            </if>
        </where>
    </select>
</mapper>
