<?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.schoolinout.system.mapper.AccessLogMapper">

    <resultMap type="AccessLog" id="AccessLogResult">
        <result property="id" column="id"/>
        <result property="studentId" column="student_id"/>
        <result property="schoolId" column="school_id"/>
        <result property="inTime" column="in_time"/>
        <result property="outTime" column="out_time"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectAccessLogVo">
        select id,
               student_id,
               school_id,
               in_time,
               out_time,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time
        from tb_school_access_log
    </sql>

    <select id="selectAccessLogList" parameterType="AccessLog" resultMap="AccessLogResult">
        <include refid="selectAccessLogVo"/>
        <where>
            <if test="studentId != null ">and student_id = #{studentId}</if>
            <if test="inTime != null ">and in_time = #{inTime}</if>
            <if test="outTime != null ">and out_time = #{outTime}</if>
        </where>
    </select>

    <select id="selectAccessLogById" parameterType="Long" resultMap="AccessLogResult">
        <include refid="selectAccessLogVo"/>
        where id = #{id}
    </select>
    <select id="selectInOutByInOutLogDto" resultType="com.schoolinout.system.domain.vo.InOutLogVo">
        SELECT
        log.in_time,
        log.out_time,
        log.create_time as `day`,
        student_name
        FROM tb_school_access_log log
        JOIN tb_school_student student ON log.student_id = student.id
        <where>
            <if test="studentId != null">
                AND log.student_id = #{studentId}
            </if>
            <if test="month != null and month != ''">
                AND MONTH(log.create_time) = #{month}
            </if>
            <if test="year != null and year != ''">
                AND year(log.create_time) = #{year}
            </if>
            <if test="toDay != null and toDay != ''">
                AND DATE(log.create_time) = #{toDay}
            </if>
        </where>
    </select>

    <insert id="insertAccessLog" parameterType="AccessLog" useGeneratedKeys="true" keyProperty="id">
        insert into tb_school_access_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="schoolId != null">student_id = #{schoolId},</if>
            <if test="studentId != null">student_id,</if>
            <if test="inTime != null">in_time,</if>
            <if test="outTime != null">out_time,</if>
            <if test="delFlag != null">del_flag,</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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="schoolId != null">student_id = #{schoolId},</if>
            <if test="studentId != null">#{studentId},</if>
            <if test="inTime != null">#{inTime},</if>
            <if test="outTime != null">#{outTime},</if>
            <if test="delFlag != null">#{delFlag},</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>
        </trim>
    </insert>

    <update id="updateAccessLog" parameterType="AccessLog">
        update tb_school_access_log
        <trim prefix="SET" suffixOverrides=",">
            <if test="studentId != null">student_id = #{studentId},</if>
            <if test="schoolId != null">student_id = #{schoolId},</if>
            <if test="inTime != null">in_time = #{inTime},</if>
            <if test="outTime != null">out_time = #{outTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</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>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAccessLogById" parameterType="Long">
        delete
        from tb_school_access_log
        where id = #{id}
    </delete>

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