<?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.telephone.mapper.EduTelephoneMapper">
    <update id="updatePhoneOnline">
        update edu_telephone
        set online_status=#{online}
        where del_flag=0 and imei
        <if test="online == 1">
            not
        </if>
        in
        <foreach collection="imeis" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
    </update>

    <resultMap id="eduTelephoneMap" type="com.yida.data.device.vo.telephone.TelephoneVO">
        <result column="id" property="id"/>
        <result column="online_status" property="onlineStatus"/>
        <result column="run_status" property="runStatus"/>
        <result column="create_date" property="createDate"/>
        <result column="iccid" property="iccid"/>
        <result column="imei" property="imei"/>
        <result column="call_duration" property="callDuration"/>
        <result column="month_max_len" property="monthMaxLen"/>
        <result column="time_max_len" property="timeMaxLen"/>
        <result column="school_id" property="schoolId"/>


        <collection property="blackList" ofType="com.yida.data.common.core.entity.telephone.EduTelephoneBlack"
                    javaType="java.util.List" column="id">
            <result column="phone" property="phone"/>
        </collection>
        <collection property="periodList"
                    ofType="com.yida.data.common.core.entity.telephone.EduTelephonePeriod" column="id"
                    javaType="java.util.List">
            <result column="start_time" property="startTime"/>
            <result column="end_time" property="endTime"/>
        </collection>
    </resultMap>

    <select id="telephoneList" resultType="com.yida.data.device.vo.telephone.TelephoneVO">
        SELECT
        a.id,
        a.online_status,
        a.run_status,
        a.create_date,
        a.iccid,
        a.imei,
        a.school_id,
        a.school_name,
        a.name,
        a.total_call_time as call_duration
        FROM
        edu_telephone a
        LEFT JOIN (SELECT * FROM edu_telephone_record WHERE create_date BETWEEN #{start} and #{now}) b ON a.id
        = b.telephone_id
        WHERE
        a.del_flag = 0
        and a.school_id in
        <foreach collection="schoolIds" item="schoolId" open="(" separator="," close=")">
            #{schoolId}
        </foreach>
        <if test="onlineStatus != null and onlineStatus != ''">
            AND a.online_status = #{onlineStatus}
        </if>
        <if test="schoolName != null and schoolName != ''">
            AND a.school_name like concat('%',#{schoolName},'%')
        </if>
        <if test="runStatus != null and runStatus != ''">
            AND a.run_status = #{runStatus}
        </if>
        <if test="iccIdAndiMei != null and iccIdAndiMei != ''">
            AND (a.iccid like concat('%', #{iccIdAndiMei},'%') OR a.imei like concat('%', #{iccIdAndiMei},'%'))
        </if>
        <if test="name != null and name != ''">
            and a.name like concat('%',#{name},'%')
        </if>
        GROUP BY a.id
        ORDER BY a.create_date DESC
    </select>


    <select id="getByTelephoneId" resultMap="eduTelephoneMap">
        SELECT
        a.id,
        a.online_status,
        a.run_status,
        a.create_date,
        a.iccid,
        a.imei,
        a.month_max_len,
        a.time_max_len,
        a.total_call_time as call_duration
        FROM
        edu_telephone a
        where a.id =#{id}
        and a.del_flag = 0
    </select>
    <select id="blackList" resultType="com.yida.data.common.core.entity.telephone.EduTelephoneBlack">
        select phone
        from
        edu_telephone_black
        where
        telephone_id = #{id}
    </select>
    <select id="periodList" resultType="com.yida.data.common.core.entity.telephone.EduTelephonePeriod">
        select start_time,end_time
        from
        edu_telephone_period
        where
        telephone_id = #{id}
    </select>

    <select id="listStudent" resultType="com.yida.data.common.core.entity.telephone.EduTelephoneCard">
        SELECT a.*,
        b.end_time mealDdl
        FROM edu_telephone_card a
        LEFT JOIN (select * from edu_telephone_card_meal where end_time <![CDATA[>]]>#{now}) b ON
        a.student_id = b.student_id
        WHERE a.del_flag = 0
        AND a.student_id in
        <foreach collection="studentIds" item="item" close=")" open="(" separator=",">
            #{item}
        </foreach>
    </select>
    
    <!-- 根据学校id和状态查询话机设备数量统计 -->
    <select id="findDeviceNumber"
            resultType="java.lang.Long">
        SELECT
            count(id) as count
        FROM
            edu_telephone
        where
            online_status = #{status}
        and del_flag = 0
        and school_id in
        <foreach collection="schoolIds" item="schoolId" open="(" close=")" separator=",">
            #{schoolId}
        </foreach>
    </select>
</mapper>
