<?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.attendance.mapper.EduAttendanceDeviceMapper">
  <select id="listDevicePage" resultType="com.yida.data.attendance.vo.DeviceVO">
    SELECT
    a.id,a.create_date,a.name,a.sn,a.mac_address,a.device_type,a.ip,a.school_id,a.zone_scope,a.type,a.state,
    GROUP_CONCAT(c.group_name) groupName
    FROM edu_attendance_device a
    LEFT JOIN edu_device_group b on a.id = b.device_id
    LEFT JOIN edu_attendance_device_group c on c.id = b.group_id
    <where>
      <if test="schoolIds != null and schoolIds.size > 0">
        and a.school_id in
        <foreach collection="schoolIds" close=")" item="schoolId" open="(" separator=",">
          #{schoolId}
        </foreach>
      </if>
      <if test="number != null and number != ''">
        and a.sn like concat('%',#{number},'%')
      </if>
      <if test="scope != null">
        and a.zone_scope=#{scope}
      </if>
      <if test="type != null">
        and a.type=#{type}
      </if>
      <if test="state != null">
        and a.state=#{state}
      </if>
    </where>
    group by a.id,a.create_date,a.name,a.sn,a.mac_address,a.device_type,a.ip,a.school_id,a.zone_scope,a.type,a.state
    order by a.create_date desc
  </select>

  <select id="updateDevice">
    update edu_attendance_device
    set state=#{operation}
    where device_type=#{type}
    <if test="aliveDeviceSn != null and aliveDeviceSn.size() != 0">
      and sn
      <if test="operation == 1">
        not
      </if>
      in
      <foreach collection="aliveDeviceSn" close=")" separator="," item="item" open="(">
        #{item}
      </foreach>
    </if>
  </select>

  <!-- 根据学校id和状态查询考勤设备数量统计 -->
  <select id="findDeviceNumber"
    resultType="java.lang.Long">
    SELECT
    count(id) as count
    FROM
    edu_attendance_device
    where
    state = #{state}
    and school_id in
    <foreach collection="schoolIds" item="schoolId" open="(" close=")" separator=",">
      #{schoolId}
    </foreach>
  </select>
</mapper>