<?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.dcsoft.system.visitor.mapper.VisExitOutRecordMapper">

    <resultMap type="VisExitOutRecordVo" id="VisitorResult">
        <result property="id" column="id"/>
        <result property="exitId" column="exit_id"/>
        <result property="userId" column="user_id"/>
        <result property="userName" column="user_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"/>
    </resultMap>

    <sql id="selectVisExitOutRecordVo">
        select distinct
            r.id,
            r.exit_id,
            r.user_id,
            r.user_name,
            r.create_by,
            r.create_time,
            r.update_by,
            r.update_time
        from vis_exit_out_record r
    </sql>

    <delete id="delete">
        delete from vis_exit_out_record
        where id in
        <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectList" resultType="com.dcsoft.system.visitor.domain.VisExitOutRecordVo">
        select distinct
        r.id,
        r.exit_id,
        r.user_id,
        r.create_by,
        r.create_time,
        r.update_by,
        r.update_time,
        ifnull(u.nick_name, u.user_name) userName,
        e.name applyName,
        e.exit_permit_no,
        e.vis_time
        from vis_exit_out_record r
        left join vis_exit_out e on r.exit_id = e.id
        left join sys_user u on r.user_id = u.user_id
        <where>
            <if test="id != null">and r.id = #{id}</if>
            <if test="userName != null  and userName != ''">and (u.user_name like concat('%', #{userName}, '%') or u.nick_name like concat('%', #{userName}, '%'))</if>
            <if test="createBy != null ">and r.create_by = #{createBy}</if>
            <if test="userId != null ">and r.user_id = #{userId}</if>
            <if test="visTimeStart != null">and e.vis_time <![CDATA[ >= ]]> #{visTimeStart}</if>
            <if test="visTimeEnd != null">and e.vis_time <![CDATA[ <= ]]> #{visTimeEnd}</if>
            <if test="createTimeStr != null">and r.create_time <![CDATA[ >= ]]> #{createTimeStr}</if>
            <if test="createTimeEnd != null">and r.create_time <![CDATA[ <= ]]> #{createTimeEnd}</if>
            <if test="exitPermitNo != null and exitPermitNo != ''">and e.exit_permit_no = #{exitPermitNo}</if>
            <if test="applyName != null and applyName != ''">and e.`name` like concat('%', #{applyName}, '%')</if>
            <if test="exitId != null">and r.`exit_id` = #{exitId}</if>
            <if test="exitIds != null and exitIds.size() > 0">
                and r.`exit_id` in
                <foreach collection="exitIds" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectExitOutRecordById" resultType="com.dcsoft.system.visitor.domain.VisExitOutRecordVo">
        <include refid="selectVisExitOutRecordVo"/>
        where r.id = #{id}
    </select>

    <insert id="insertExitOutRecord">
        insert into vis_exit_out_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null and id != ''">id,</if>
            <if test="userId != null">user_id,</if>
            <if test="exitId != null">`exit_id`,</if>
            <if test="createBy != null">create_by,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null and id != ''">#{id},</if>
            <if test="userId != null">#{userId},</if>
            <if test="exitId != null">#{exitId},</if>
            <if test="createBy != null">#{createBy},</if>
        </trim>
    </insert>

</mapper>
