<?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.api.butlerApp.dao.butler.ButlerAttendanceDao">

    <select id="todayClockRecord" parameterType="com.api.model.butlerApp.ButlerUserIdAndToDay" resultType="com.api.vo.operationManagement.VoAttendanceRecord">
        select sar.*,su1.actual_name as clock_name,
        su1.tel as clock_tel,sar.card_replacement_date,su2.actual_name as operator_name,sar.create_date
        from sys_user as su1,sys_attendance_record as sar
        LEFT JOIN sys_user as su2 on sar.operator = su2.id
        where sar.clock_id = su1.id and sar.clock_id = #{userId} and date_format(sar.create_date,'%Y%m%d') = date_format(#{toDay},'%Y%m%d')
        order by sar.create_date desc limit 1
    </select>

    <update id="clock" parameterType="com.api.model.operationManagement.AttendanceRecord">
        update sys_attendance_record
        <set>
            <if test="startClockDate != null">
                start_clock_date = #{startClockDate},
            </if>
            <if test="endClockDate != null">
                end_clock_date = #{endClockDate},
            </if>
        </set>
        where id = #{id} and clock_id = #{clockId}
    </update>

    <select id="clockRecord" parameterType="java.lang.Integer" resultType="com.api.vo.operationManagement.VoAttendanceRecord">
        select sar.id,sar.start_clock_date,sar.end_clock_date,su1.actual_name as clock_name,
        su1.tel as clock_tel,sar.card_replacement_date,su2.actual_name as operator_name,sar.create_date,sar.status
        from sys_user as su1,sys_attendance_record as sar
        LEFT JOIN sys_user as su2 on sar.operator = su2.id
        where sar.clock_id = su1.id and sar.clock_id = #{id}
        order by sar.create_date desc
    </select>
    <select id="applyRecord" parameterType="java.lang.Integer" resultType="com.api.vo.operationManagement.VoAttendanceLeaveRecord">
        select salr.id,salr.reason,salr.status,salr.type,salr.start_date,salr.end_date,
        su1.actual_name as create_name,su1.tel as create_tel,salr.create_date,
        su2.actual_name as reviewer_name,salr.reviewer_date
        from sys_user as su1,sys_attendance_leave_record as salr
        LEFT JOIN sys_user as su2 on salr.reviewer = su2.id
        where salr.create_id = su1.id and salr.create_id = #{id}
    </select>

    <insert id="apply" parameterType="com.api.model.operationManagement.SysAttendanceLeaveRecord">
        insert into sys_attendance_leave_record(
            reason,
            status,
            type,
            start_date,
            end_date,
            create_id,
            create_date
        )values (
            #{reason},
            #{status},
            #{type},
            #{startDate},
            #{endDate},
            #{createId},
            #{createDate}
        )
    </insert>

    <select id="findAllSysUer" parameterType="com.api.model.businessManagement.SysUser" resultType="com.api.model.businessManagement.SysUser">
        select * from sys_user where is_delete = 1 and status = 1
    </select>

    <insert id="autoAttendanceRecord" parameterType="com.api.model.operationManagement.AttendanceRecord">
        insert into sys_attendance_record(
            clock_id,
            create_date,
            <if test="firstTimeStart != null">
                first_time_start,
            </if>
            <if test="firstTimeEnd != null">
                first_time_end,
            </if>
            <if test="secondTimeStart != null">
                second_time_start,
            </if>
            <if test="secondTimeEnd != null">
                second_time_end,
            </if>
            status
        )values (
            #{clockId},
            #{createDate},
            <if test="firstTimeStart != null">
                #{firstTimeStart},
            </if>
            <if test="firstTimeEnd != null">
                #{firstTimeEnd},
            </if>
            <if test="secondTimeStart != null">
                #{secondTimeStart},
            </if>
            <if test="secondTimeEnd != null">
                #{secondTimeEnd},
            </if>
            #{status}
        )
    </insert>


</mapper>