<?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.yida.data.school.visitor.mapper.EduVisitorRecordMapper">
  <select id="listRecord" resultType="com.yida.data.school.vo.visitor.ListVisitorRecordVO">
    select id,
    name,
    id_card,
    mobile,
    reason,
    reason_pic,
    appointment_time,
    staff_id,
    result,
    status,
    in_school_time,
    temp,
    no_reason,
    out_school_time,
    in_school_security_id,
    out_school_security_id,
    create_date
    from edu_visitor_record
    <where>
      <if test="dto.schoolId != null">
        and school_id = #{dto.schoolId}
      </if>
      <if test="dto.status != null">
        and status = #{dto.status}
        <if test="dto.status == 0">
          and result=1
        </if>
      </if>
      <if test="dto.type != null">
        <choose>
          <when test="dto.type == 0">
            and result in (1, 2, 3)
          </when>
          <otherwise>
            and result = 0
          </otherwise>
        </choose>
      </if>
      <if test="dto.result != null">
        and result = #{dto.result}
      </if>
      <if test="dto.startTime != null">
        and appointment_time <![CDATA[>=]]> #{dto.startTime}
      </if>
      <if test="dto.endTime != null">
        and appointment_time <![CDATA[<=]]> #{dto.endTime}
      </if>
      <if test="dto.keyword != null">
        and (
        name like concat('%', #{dto.keyword}, '%')
        or id_card like concat('%', #{dto.keyword}, '%')
        or mobile like concat('%', #{dto.keyword}, '%')
        )
      </if>
      <if test="dto.staffId != null">
        and staff_id = #{dto.staffId}
      </if>
    </where>
    order by appointment_time desc, create_date desc
  </select>

  <select id="listRecordToStaff" resultType="com.yida.data.school.vo.visitor.ListVisitorRecordVO">
    select id,
    name,
    mobile,
    id_card,
    reason,
    reason_pic,
    no_reason,
    appointment_time,
    result,
    status,
    in_school_time,
    out_school_time,
    create_date
    from edu_visitor_record
    <where>
      and staff_id = #{dto.staffId}
      <if test="dto.type != null">
        <choose>
          <when test="dto.type == 0">
            and result = 0
          </when>
          <when test="dto.type == 1">
            and result = 1
            and status in (0, 1)
          </when>
          <otherwise>
            and (result in (2, 3) or status in (2, 3))
          </otherwise>
        </choose>
      </if>
      <if test="dto.name != null and dto.name != ''">
        and name like concat('%', #{dto.name}, '%')
      </if>
    </where>
    order by
    <choose>
      <when test="dto.type == 0">
        create_date
      </when>
      <when test="dto.type == 1">
        appointment_time
      </when>
      <otherwise>
        update_date
      </otherwise>
    </choose>
  </select>

  <update id="dealRequestDdl">
    update edu_visitor_record
    set status=3
    where status = 0
      and unix_timestamp(DATE_ADD(appointment_time, interval 30 minute))<![CDATA[ <]]> unix_timestamp(current_date)
  </update>
</mapper>
