<?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.device.studentCard.mapper.EduStudentCardFenceMapper">
  <select id="listFence" resultType="com.yida.data.device.vo.studentCard.FenceListVO">
    select distinct a.id,
    a.name,
    a.description,
    a.lng,
    a.lat,
    a.radius,
    a.alarm_type,
    a.create_date,
    a.address
    from edu_student_card_fence a
    left join edu_student_card_fence_student b on a.id = b.fence_id
    <if test="imei != null">
      left join edu_student_card c on b.student_id = c.student_id and c.del_flag = 0
    </if>
    where a.del_flag = 0
    <if test="schoolId != null">
      and a.school_id = #{schoolId}
    </if>
    <if test="imei != null">
      and c.imei = #{imei}
      and c.del_flag=0
    </if>
    <if test="createType != null">
      and a.create_type = #{createType}
    </if>
    <if test="name != null and name != ''">
      and a.name like concat('%', #{name}, '%')
    </if>
    order by a.create_date desc
  </select>

  <select id="listFenceByImei" resultType="com.yida.data.common.core.entity.card.EduStudentCardFence">
    select a.*
    from edu_student_card_fence a left join edu_student_card_fence_student b on a.id=b.fence_id
    left join edu_student_card c on b.student_id=c.student_id
    where a.del_flag=0
    and c.imei=#{imei}
  </select>

  <select id="getFenceByJimiFenceName"
    resultType="com.yida.data.common.core.entity.card.EduStudentCardFence">
    select a.*
    from edu_student_card_fence a
    left join edu_student_card_fence_jimi b on a.id = b.fence_id
    where a.del_flag = 0
    and b.jimi_fence_name = #{jimiName}
  </select>

  <select id="countFenceNum" resultType="java.lang.Integer">
    select count(1)
    from edu_student_card_fence a left join edu_student_card_fence_student b on a.id=b.fence_id
    left join edu_student_card c on b.student_id=c.student_id
    where c.imei=#{imei}
  </select>

  <select id="listCardByFence" resultType="com.yida.data.common.core.entity.card.EduStudentCard">
    select b.*
    from edu_student_card_fence_student a left join edu_student_card b on a.student_id=b.student_id
    where a.fence_id in
    <foreach collection="ids" close=")" open="(" separator="," item="item">
      #{item}
    </foreach>
  </select>
</mapper>
