<?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.EduStudentCardMapper">
  <sql id="listCardSql">
    select card_id, mc_type,imei,rfid,student_id,student_phone
    mobile,bind_status,activate_status,use_status,create_date importTime,activate_date
    from edu_student_card
    where del_flag=0
    <choose>
      <when test="studentIds != null and studentIds.size() != 0">
        and student_id in
        <foreach collection="studentIds" close=")" open="(" separator="," item="item">
          #{item}
        </foreach>
      </when>
      <otherwise>
        and 0=1
      </otherwise>
    </choose>
    <if test="dto.imei != null and dto.imei != ''">
      and imei like concat('%',#{dto.imei},'%')
    </if>
    <if test="dto.rfid != null and dto.rfid != ''">
      and rfid like concat('%',#{dto.rfid},'%')
    </if>
    <if test="dto.mobile != null and dto.mobile != ''">
      and student_phone like concat('%',#{dto.mobile},'%')
    </if>
    <if test="dto.activateStartTime != null and dto.activateEndTime != null">
      and activate_date between #{dto.activateStartTime} and #{dto.activateEndTime}
    </if>
    <if test="dto.importStartTime != null and dto.importEndTime != null">
      and create_date between #{dto.importStartTime} and #{dto.importEndTime}
    </if>
    <if test="dto.useStatus != null">
      and use_status=#{dto.useStatus}
    </if>
    <if test="dto.mcType != null and dto.mcType != ''">
      and mc_type =#{dto.mcType}
    </if>
    <if test="dto.schoolId != null">
      and school_id=#{dto.schoolId}
    </if>
    order by create_date desc
  </sql>
  <select id="listCardPage" resultType="com.yida.data.device.vo.studentCard.CardPageVO">
    <include refid="listCardSql"/>
  </select>

  <select id="listCard" resultType="com.yida.data.device.vo.studentCard.CardPageVO">
    <include refid="listCardSql"/>
  </select>

  <select id="getInfo" resultType="com.yida.data.device.vo.studentCard.CardInfoVO">
    select card_id,
    student_id,
    student_name,
    student_phone,
    imei,
    mc_type,
    rfid,
    activate_status,
    online_status,
    activate_date
    from edu_student_card
    where del_flag = 0
    and card_id = #{cardId}
  </select>

  <select id="listStudentCard"
    resultType="com.yida.data.device.vo.studentCard.CardWithAddressVO">
    select student_id, lng, lat,imei,pos_time,pos_type,card_id
    from edu_student_card
    <where>
      del_flag = 0
      <if test="dto.schoolId != null">
        and school_id = #{dto.schoolId}
      </if>
      <choose>
        <when test="dto.studentIds != null and dto.studentIds.size() > 0">
          and student_id in
          <foreach collection="dto.studentIds" close=")" open="(" separator="," item="item">
            #{item}
          </foreach>
        </when>
        <otherwise>
          and 0 = 1
        </otherwise>
      </choose>
      <if test="dto.onlineStatus != null">
        <choose>
          <when test="dto.onlineStatus == 0">
            and imei in
            <foreach collection="onlineImei" item="item" separator="," open="(" close=")">
              #{item}
            </foreach>
          </when>
          <when test="dto.onlineStatus == 1">
            and imei not in
            <foreach collection="onlineImei" item="item" separator="," open="(" close=")">
              #{item}
            </foreach>
          </when>
        </choose>
      </if>
    </where>
    order by create_date desc
  </select>
  <update id="updateLocaltionBatchByImei">
    update edu_student_card
    <trim prefix="set" suffixOverrides=",">
      <trim prefix="battery =case" suffix="end,">
        <foreach collection="list" item="item" index="index">
          <if test="item.imei != null">
            when imei = #{item.imei} and del_flag = 0
            then #{item.battery}
          </if>
        </foreach>
      </trim>

      <trim prefix="lng =case" suffix="end,">
        <foreach collection="list" item="item" index="index">
          <if test="item.lng != null">
            when imei = #{item.imei} and del_flag = 0
            then #{item.lng}
          </if>
        </foreach>
      </trim>

      <trim prefix="lat =case" suffix="end,">
        <foreach collection="list" item="item" index="index">
          <if test="item.imei != null">
            when imei = #{item.imei} and del_flag = 0
            then #{item.lat}
          </if>
        </foreach>
      </trim>

      <trim prefix="pos_type =case" suffix="end,">
        <foreach collection="list" item="item" index="index">
          <if test="item.imei != null">
            when imei = #{item.imei} and del_flag = 0
            then #{item.posType}
          </if>
        </foreach>
      </trim>

      <trim prefix="pos_time =case" suffix="end,">
        <foreach collection="list" item="item" index="index">
          <if test="item.imei != null">
            when imei = #{item.imei} and del_flag = 0
            then #{item.posTime}
          </if>
        </foreach>
      </trim>
    </trim>
    where del_flag = 0
    and edu_student_card.imei in
    <foreach collection="list" item="item" index="index" separator="," open="(" close=")">
      #{item.imei}
    </foreach>
  </update>

  <select id="listStudentCardByImeiOrName" resultType="com.yida.data.device.vo.studentCard.CardPageVO">
    select card_id,
    mc_type,
    imei,
    rfid,
    student_name name,
    student_phone
    mobile,
    bind_status,
    activate_status,
    use_status,
    online_status,
    create_date importTime,
    activate_date
    from edu_student_card
    where del_flag = 0
    <if test="schoolId != null">
      and school_id = #{schoolId}
    </if>
    <if test="keyword != null and keyword != ''">
      and (imei like concat('%', #{keyword}, '%')
      or student_name like concat('%', #{keyword}, '%'))
    </if>
  </select>

  <update id="updateOnline">
    update edu_student_card
    set online_status=#{status}
    where del_flag = 0
    <if test="onlineList != null and onlineList.size() != 0">
      and imei
      <if test="status == 0">
        not
      </if>
      in
      <foreach collection="onlineList" separator="," open="(" close=")" item="item">
        #{item}
      </foreach>
    </if>
  </update>
</mapper>
