<?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.AttendanceRecordMapper">

    <resultMap type="AttendanceRecord" id="AttendanceRecordResult">
        <result property="tenantId" column="tenant_id"/>
        <result property="revision" column="revision"/>
        <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="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="checkInTime" column="check_in_time"/>
        <result property="checkOutTime" column="check_out_time"/>
        <result property="workDuration" column="work_duration"/>
        <result property="status" column="status"/>
        <result property="teamCode" column="team_code"/>
        <result property="teamName" column="team_name"/>
        <result property="departmentCode" column="department_code"/>
        <result property="departmentName" column="department_name"/>
        <result property="brigadeCode" column="brigade_code"/>
        <result property="brigadeName" column="brigade_name"/>
        <result property="stationCode" column="station_code"/>
        <result property="stationName" column="station_name"/>
        <result property="userName" column="user_name"/>
        <result property="remark" column="remark"/>
        <result property="attendanceDate" column="attendance_date"/>
        <result property="overDuration" column="over_duration"/>
        <result property="statusDesc" column="status_desc"/>
    </resultMap>

    <sql id="selectAttendanceRecordVo">
        select tenant_id,
               revision,
               create_by,
               create_time,
               update_by,
               update_time,
               id,
               user_id,
               check_in_time,
               check_out_time,
               work_duration,
               status,
               team_code,
               team_name,
               department_code,
               department_name,
               brigade_code,
               brigade_name,
               station_code,
               station_name,
               user_name,
               remark,
               attendance_date,
               over_duration,
               status_desc
        from attendance_record
    </sql>

    <select id="selectAttendanceRecordList" parameterType="AttendanceRecord" resultMap="AttendanceRecordResult">
        <include refid="selectAttendanceRecordVo"/>
        <where>
            <if test="tenantId != null  and tenantId != ''">and tenant_id = #{tenantId}</if>
            <if test="revision != null ">and revision = #{revision}</if>
            <if test="userId != null ">and user_id = #{userId}</if>
            <if test="checkInTime != null ">and check_in_time = #{checkInTime}</if>
            <if test="checkOutTime != null ">and check_out_time = #{checkOutTime}</if>
            <if test="workDuration != null ">and work_duration = #{workDuration}</if>
            <if test="status != null  and status != ''">and status = #{status}</if>
            <if test="teamCode != null  and teamCode != ''">and team_code = #{teamCode}</if>
            <if test="teamName != null  and teamName != ''">and team_name like concat('%', #{teamName}, '%')</if>
            <if test="departmentCode != null  and departmentCode != ''">and department_code = #{departmentCode}</if>
            <if test="departmentName != null  and departmentName != ''">and department_name like concat('%',
                #{departmentName}, '%')
            </if>
            <if test="brigadeCode != null  and brigadeCode != ''">and brigade_code = #{brigadeCode}</if>
            <if test="brigadeName != null  and brigadeName != ''">and brigade_name like concat('%',
                #{brigadeName}, '%')
            </if>
            <if test="stationCode != null  and stationCode != ''">and station_code = #{stationCode}</if>
            <if test="stationName != null  and stationName != ''">and station_name like concat('%', #{stationName},
                '%')
            </if>
            <if test="userName != null  and userName != ''">and user_name like concat('%', #{userName}, '%')</if>
            <if test="remark != null  and remark != ''">and remark = #{remark}</if>
            <if test="attendanceDate != null ">and DATE(attendance_date) = DATE(#{attendanceDate})</if>
            <if test="overDuration != null ">and over_duration = #{overDuration}</if>
            <if test="statusDesc != null  and statusDesc != ''">and status_desc = #{statusDesc}</if>

            <if test="attendanceDateStart != null ">and attendance_date &gt;= concat(#{attendanceDateStart}, '
                00:00:00')
            </if>
            <if test="attendanceDateEnd != null ">and attendance_date &lt;= concat(#{attendanceDateEnd}, ' 23:59:59')
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="selectAttendanceRecordById" parameterType="Long" resultMap="AttendanceRecordResult">
        <include refid="selectAttendanceRecordVo"/>
        where id = #{id}
    </select>

    <insert id="insertAttendanceRecord" parameterType="AttendanceRecord" useGeneratedKeys="true" keyProperty="id">
        insert into attendance_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="tenantId != null">tenant_id,</if>
            <if test="revision != null">revision,</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="userId != null">user_id,</if>
            <if test="checkInTime != null">check_in_time,</if>
            <if test="checkOutTime != null">check_out_time,</if>
            <if test="workDuration != null">work_duration,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="teamCode != null and teamCode != ''">team_code,</if>
            <if test="teamName != null and teamName != ''">team_name,</if>
            <if test="departmentCode != null and departmentCode != ''">department_code,</if>
            <if test="departmentName != null and departmentName != ''">department_name,</if>
            <if test="brigadeCode != null and brigadeCode != ''">brigade_code,</if>
            <if test="brigadeName != null and brigadeName != ''">brigade_name,</if>
            <if test="stationCode != null and stationCode != ''">station_code,</if>
            <if test="stationName != null and stationName != ''">station_name,</if>
            <if test="userName != null and userName != ''">user_name,</if>
            <if test="remark != null">remark,</if>
            <if test="attendanceDate != null">attendance_date,</if>
            <if test="overDuration != null">over_duration,</if>
            <if test="statusDesc != null and statusDesc != ''">status_desc,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="tenantId != null">#{tenantId},</if>
            <if test="revision != null">#{revision},</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="userId != null">#{userId},</if>
            <if test="checkInTime != null">#{checkInTime},</if>
            <if test="checkOutTime != null">#{checkOutTime},</if>
            <if test="workDuration != null">#{workDuration},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="teamCode != null and teamCode != ''">#{teamCode},</if>
            <if test="teamName != null and teamName != ''">#{teamName},</if>
            <if test="departmentCode != null and departmentCode != ''">#{departmentCode},</if>
            <if test="departmentName != null and departmentName != ''">#{departmentName},</if>
            <if test="brigadeCode != null and brigadeCode != ''">#{brigadeCode},</if>
            <if test="brigadeName != null and brigadeName != ''">#{brigadeName},</if>
            <if test="stationCode != null and stationCode != ''">#{stationCode},</if>
            <if test="stationName != null and stationName != ''">#{stationName},</if>
            <if test="userName != null and userName != ''">#{userName},</if>
            <if test="remark != null">#{remark},</if>
            <if test="attendanceDate != null">#{attendanceDate},</if>
            <if test="overDuration != null">#{overDuration},</if>
            <if test="statusDesc != null and statusDesc != ''">#{statusDesc},</if>
        </trim>
    </insert>

    <update id="updateAttendanceRecord" parameterType="AttendanceRecord">
        update attendance_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="tenantId != null">tenant_id = #{tenantId},</if>
            <if test="revision != null">revision = #{revision},</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="userId != null">user_id = #{userId},</if>
            <if test="checkInTime != null">check_in_time = #{checkInTime},</if>
            <if test="checkOutTime != null">check_out_time = #{checkOutTime},</if>
            <if test="workDuration != null">work_duration = #{workDuration},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="teamCode != null and teamCode != ''">team_code = #{teamCode},</if>
            <if test="teamName != null and teamName != ''">team_name = #{teamName},</if>
            <if test="departmentCode != null and departmentCode != ''">department_code = #{departmentCode},</if>
            <if test="departmentName != null and departmentName != ''">department_name = #{departmentName},</if>
            <if test="brigadeCode != null and brigadeCode != ''">brigade_code = #{brigadeCode},</if>
            <if test="brigadeName != null and brigadeName != ''">brigade_name = #{brigadeName},</if>
            <if test="stationCode != null and stationCode != ''">station_code = #{stationCode},</if>
            <if test="stationName != null and stationName != ''">station_name = #{stationName},</if>
            <if test="userName != null and userName != ''">user_name = #{userName},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="attendanceDate != null">attendance_date = #{attendanceDate},</if>
            <if test="overDuration != null">over_duration = #{overDuration},</if>
            <if test="statusDesc != null and statusDesc != ''">status_desc = #{statusDesc},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAttendanceRecordById" parameterType="Long">
        delete
        from attendance_record
        where id = #{id}
    </delete>

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