<?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.EduTelephoneCardMapper">
    <select id="findCardList" resultType="com.yida.data.device.vo.telephone.IcCardVO">
        SELECT a.card_no,
        a.id,
        a.student_name,
        a.balance,
        a.`status`,
        a.create_date,
        b.price,
        SUM(b.call_duration) as call_duration

        FROM edu_telephone_card a
        LEFT JOIN edu_telephone_price b ON a.price_id = b.id
        LEFT JOIN (SELECT *
        FROM edu_telephone_record
        WHERE create_date BETWEEN #{start} and #{now}) b ON a.id = b.card_id

        WHERE a.del_flag = 0
        <if test="status != null">
            AND a.`status` = #{status}
        </if>
        <if test="iccId != null and iccId != ''">
            and a.card_no like concat('%', #{iccId}, '%')
        </if>
        <if test="studentName != null and studentName != ''">
            and a.student_name LIKE CONCAT('%', #{studentName}, '%')
        </if>
        <if test="schoolId !=null">
            and a.school_id = #{schoolId}
        </if>
        <if test="campusId != null">
            and a.campus_id  = #{campusId}
        </if>
        <if test="gradeId != null">
            and a.grade_id = #{gradeId}
        </if>
        <if test="classId != null">
            and a.class_id = #{classId}
        </if>
        GROUP BY a.id
        ORDER BY a.create_date
    </select>

    <select id="findStudentCard" resultType="com.yida.data.common.core.entity.telephone.EduTelephoneCard">
        SELECT a.id,
        a.balance,
        a.card_no,
        a.grade_name,
        a.class_name,
        a.`status`,
        a.student_name,
        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 = #{id}
    </select>
</mapper>
