<?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.sundot.airport.attendance.mapper.AttendanceTeamUserRecordMapper">

    <resultMap type="AttendanceTeamUserRecord" id="AttendanceTeamUserRecordResult">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="userCode" column="user_code"/>
        <result property="userName" column="user_name"/>
        <result property="terminlCode" column="terminl_code"/>
        <result property="terminlName" column="terminl_name"/>
        <result property="attendanceDate" column="attendance_date"/>
        <result property="attendanceTeamId" column="attendance_team_id"/>
        <result property="attendanceTeamName" column="attendance_team_name"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
        <result property="checkInType" column="check_in_type"/>
    </resultMap>

    <sql id="selectAttendanceTeamUserRecordVo">
        select id,
               user_id,
               user_code,
               user_name,
               terminl_code,
               terminl_name,
               attendance_date,
               attendance_team_id,
               attendance_team_name,
               create_by,
               create_time,
               update_by,
               update_time,
               remark,
               check_in_type
        from attendance_team_user_record
    </sql>

    <select id="selectAttendanceTeamUserRecordList" parameterType="AttendanceTeamUserRecord"
            resultMap="AttendanceTeamUserRecordResult">
        <include refid="selectAttendanceTeamUserRecordVo"/>
        <where>
            <if test="createBy != null ">and create_by = #{createBy}</if>
            <if test="userId != null ">and user_id = #{userId}</if>
            <if test="userCode != null  and userCode != ''">and user_code = #{userCode}</if>
            <if test="userName != null  and userName != ''">and user_name like concat('%', #{userName}, '%')</if>
            <if test="terminlCode != null  and terminlCode != ''">and terminl_code = #{terminlCode}</if>
            <if test="terminlName != null  and terminlName != ''">and terminl_name like concat('%', #{terminlName},
                '%')
            </if>
            <if test="attendanceDate != null ">and DATE(attendance_date) = DATE(#{attendanceDate})</if>
            <!-- 添加时间范围查询支持 -->
            <if test="attendanceDateStart != null ">and create_time >= #{attendanceDateStart}</if>
            <if test="attendanceDateEnd != null ">and create_time &lt;= #{attendanceDateEnd}</if>
            <if test="attendanceTeamId != null ">and attendance_team_id = #{attendanceTeamId}</if>
            <if test="attendanceTeamName != null  and attendanceTeamName != ''">and attendance_team_name like
                concat('%', #{attendanceTeamName}, '%')
            </if>
            <if test="checkInType != null and checkInType != ''">and check_in_type = #{checkInType}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectAttendanceTeamUserRecordByUserId" resultMap="AttendanceTeamUserRecordResult">
        <include refid="selectAttendanceTeamUserRecordVo"/>
        where 1 = 1
        <if test="userId != null ">and user_id = #{userId}</if>
        <if test="attendanceDate != null ">and DATE(attendance_date) = DATE(#{attendanceDate})</if>
        <if test="checkInType != null ">and check_in_type = #{checkInType}</if>
        limit 1
    </select>

    <insert id="insertAttendanceTeamUserRecord" parameterType="AttendanceTeamUserRecord">
        insert into attendance_team_user_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="userCode != null and userCode != ''">user_code,</if>
            <if test="userName != null and userName != ''">user_name,</if>
            <if test="terminlCode != null">terminl_code,</if>
            <if test="terminlName != null">terminl_name,</if>
            <if test="attendanceDate != null">attendance_date,</if>
            <if test="attendanceTeamId != null">attendance_team_id,</if>
            <if test="attendanceTeamName != null">attendance_team_name,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="checkInType != null">check_in_type,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="userCode != null and userCode != ''">#{userCode},</if>
            <if test="userName != null and userName != ''">#{userName},</if>
            <if test="terminlCode != null">#{terminlCode},</if>
            <if test="terminlName != null">#{terminlName},</if>
            <if test="attendanceDate != null">#{attendanceDate},</if>
            <if test="attendanceTeamId != null">#{attendanceTeamId},</if>
            <if test="attendanceTeamName != null">#{attendanceTeamName},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="checkInType != null">#{checkInType},</if>
        </trim>
    </insert>

    <update id="updateAttendanceTeamUserRecord" parameterType="AttendanceTeamUserRecord">
        update attendance_team_user_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="userCode != null and userCode != ''">user_code = #{userCode},</if>
            <if test="userName != null and userName != ''">user_name = #{userName},</if>
            <if test="terminlCode != null">terminl_code = #{terminlCode},</if>
            <if test="terminlName != null">terminl_name = #{terminlName},</if>
            <if test="attendanceDate != null">attendance_date = #{attendanceDate},</if>
            <if test="attendanceTeamId != null">attendance_team_id = #{attendanceTeamId},</if>
            <if test="attendanceTeamName != null">attendance_team_name = #{attendanceTeamName},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="checkInType != null">check_in_type = #{checkInType},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAttendanceTeamUserRecordByUserId" parameterType="Long">
        delete
        from attendance_team_user_record
        where user_id = #{userId}
    </delete>

    <delete id="deleteAttendanceTeamUserRecordByUserIds" parameterType="String">
        delete from attendance_team_user_record where user_id in
        <foreach item="userId" collection="array" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>

    <insert id="insertAttendanceTeamUserRecordList">
        insert into attendance_team_user_record
        (user_id, user_code, user_name, terminl_code, terminl_name, attendance_date,
        attendance_team_id, attendance_team_name, create_by, create_time)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.userId}, #{item.userCode}, #{item.userName}, #{item.terminlCode},
            #{item.terminlName}, #{item.attendanceDate}, #{item.attendanceTeamId},
            #{item.attendanceTeamName}, #{item.createBy}, #{item.createTime})
        </foreach>
    </insert>

    <select id="selectAttendanceTeamUserRecordByUserIdAndDateRange" resultMap="AttendanceTeamUserRecordResult">
        SELECT id,
               tenant_id,
               revision,
               create_by,
               create_time,
               update_by,
               update_time,
               user_id,
               user_name,
               user_code,
               terminl_code,
               terminl_name,
               attendance_date,
               attendance_team_id,
               attendance_team_name,
               remark,
               check_in_type
        FROM attendance_team_user_record
        WHERE user_id = #{userId}
          AND attendance_date >= #{startDate}
          AND attendance_date &lt;= #{endDate}
    </select>

    <delete id="deleteAttendanceTeamUserRecordByIds" parameterType="String">
        delete from attendance_team_user_record where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectMaxDateBy" parameterType="AttendanceTeamUserRecord" resultType="java.util.Date">
        select max(attendance_date) attendance_date
        from attendance_team_user_record
        <where>
            <if test="checkInType != null and checkInType != ''">
                and check_in_type = #{checkInType}
            </if>
            <if test="createBy != null and createBy != ''">
                and create_by = #{createBy}
            </if>
            <if test="attendanceTeamId != null">
                and attendance_team_id = #{attendanceTeamId}
            </if>
        </where>
    </select>
</mapper>