<?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.EduTelephoneRecordMapper">
    <sql id="pageRes">
        select c.imei,b.student_name,b.card_no,a.calling callPhone,d.relation callName,a.call_duration
        duration,a.call_date
        from edu_telephone_record a
        left join edu_telephone_card b on a.card_id=b.id
        left join edu_telephone c on a.telephone_id=c.id
        left join edu_telephone_family d on a.calling=d.phone and b.id=d.card_id
        where a.del_flag = 0
        <if test="dto.schoolId != null">
            and b.school_id=#{dto.schoolId}
        </if>
        <if test="dto.campusId != null">
            and b.campus_id=#{dto.campusId}
        </if>
        <if test="dto.sectionId != null">
            and b.section_id=#{dto.sectionId}
        </if>
        <if test="dto.gradeId != null">
            and b.grade_id=#{dto.gradeId}
        </if>
        <if test="dto.classId != null">
            and b.class_id=#{dto.classId}
        </if>
        <if test="dto.imei != null and dto.imei != ''">
            and c.imei  like concat('%',#{dto.imei},'%')
        </if>
        <if test="dto.cardNo != null and dto.cardNo != ''">
            and b.card_no  like concat('%',#{dto.cardNo},'%')
        </if>
        <if test="dto.studentName != null and dto.studentName != ''">
            and b.student_name like concat('%',#{dto.studentName},'%')
        </if>
        <if test="dto.callPhone != null and dto.callPhone != ''">
            and a.calling like concat('%',#{dto.callPhone},'%')
        </if>
        <if test="dto.startTime != null">
            and a.call_date <![CDATA[>=]]> #{dto.startTime}
        </if>
        <if test="dto.endTime != null">
            and a.call_date <![CDATA[<=]]> #{dto.endTime}
        </if>
        order by a.create_date desc
    </sql>
    <select id="sumTelephone" resultType="java.lang.Long">
        select ifnull(sum(call_duration),0)
        from edu_telephone_record
        where del_flag=0 and telephone_id=#{telephoneId}
        and YEAR(call_date)=#{year}
        and MONTH(call_date)=#{month}
    </select>

    <select id="selectMaxRecord" resultType="com.yida.data.common.core.entity.telephone.EduTelephoneRecord">
        select MAX(create_date) as create_date
        from edu_telephone_record
        where 1 = 1
          and del_flag = 0
    </select>


    <select id="selectMonthMaxLen" resultType="java.lang.Integer">
        select SUM(call_duration )
        from edu_telephone_record
        where del_flag = 0
        and card_id = #{id}
        and create_date BETWEEN #{start} and #{now}
    </select>

    <select id="selectRecord" resultType="com.yida.data.common.core.entity.telephone.EduTelephoneFamily">
        SELECT
        a.calling as phone,
        b.relation
        FROM
        `edu_telephone_record` a
        LEFT JOIN edu_telephone_family b ON a.calling = b.phone
        WHERE
        del_flag = 0
        AND a.card_id = #{id}
        ORDER BY a.create_date desc
    </select>

    <select id="selectStartAndEnd" resultType="com.yida.data.device.vo.telephone.TelephoneCardVO">
        SELECT
        MAX(a.call_date) as start_date,
        MAX(DATE_ADD( a.call_date,INTERVAL a.call_duration SECOND )) end_date

        FROM
        `edu_telephone_record` a
        LEFT JOIN edu_telephone_family b ON a.calling = b.phone
        WHERE
        del_flag = 0
        AND a.card_id = #{id}
    </select>


    <select id="listRecordPage" resultType="com.yida.data.device.vo.telephone.RecordInfoVO">
        <include refid="pageRes">
        </include>
    </select>

    <select id="listRecord" resultType="com.yida.data.device.vo.telephone.RecordInfoVO">
        <include refid="pageRes">
        </include>
    </select>

    <select id="callSummary" resultType="com.yida.data.device.vo.telephone.TelephoneSummaryVO">
        SELECT
        COUNT(DISTINCT card_id) as personNum,
        COUNT(id) as callNum,
        SUM(call_duration) as callDuration,
        DATE_FORMAT(create_date, '%m-%d') as day
        FROM
        edu_telephone_record
        WHERE
        card_id IN (
        SELECT
        id AS card_id
        FROM
        edu_telephone_card
        WHERE
        school_id = #{schoolId})
        and del_flag = 0
        <if test="type == 2">
            and DATE_SUB(CURDATE(), INTERVAL 7 DAY) &lt;= create_date
        </if>
        <if test="type == 1">
            and DATE_SUB(CURDATE(), INTERVAL 30 DAY) &lt;= create_date
        </if>
        GROUP BY day
    </select>

    <select id="callYearSummary" resultType="com.yida.data.device.vo.telephone.TelephoneSummaryVO">
        SELECT
        COUNT(DISTINCT card_id) as personNum,
        COUNT(id)as callNum,
        SUM(call_duration)as callDuration,
        DATE_FORMAT(create_date, '%Y-%m') as month
        FROM
        edu_telephone_record
        WHERE
        card_id IN (
        SELECT
        id AS card_id
        FROM
        edu_telephone_card
        WHERE
        school_id = #{schoolId})
        and del_flag = 0

        GROUP BY month
    </select>

    <select id="callDaySummary" resultType="com.yida.data.device.vo.telephone.TelephoneSummaryVO">
        SELECT
        COUNT(DISTINCT card_id) as personNum,
        COUNT(id)as callNum,
        SUM(call_duration)as callDuration,
        DATE_FORMAT(create_date, '%h') as hours

        FROM
        edu_telephone_record
        WHERE
        card_id IN (
        SELECT
        id AS card_id
        FROM
        edu_telephone_card
        WHERE
        school_id = #{schoolId})
        and del_flag = 0
        and DATE_SUB(CURDATE(), INTERVAL 1 DAY) &lt;= create_date
        GROUP BY hours
    </select>

    <select id="selectMonth" resultType="java.lang.Integer">
        SELECT
        TIMESTAMPDIFF(
        MONTH,
        a.TIME,
        DATE_FORMAT( NOW(), '%Y-%m-%d %H:%i:%S' ))
        FROM
        (SELECT MIN(create_date) AS time FROM
        edu_telephone_record
        WHERE
        card_id IN (
        SELECT
        id AS card_id
        FROM
        edu_telephone_card
        WHERE
        school_id = #{schoolId}
        and del_flag = 0) )a
    </select>

    <select id="listFamilyRecord" resultType="com.yida.data.device.vo.telephone.FamilyRecordVO">
        SELECT
        a.card_id,
        a.call_date,
        b.relation,
        a.call_duration,
        a.calling as phone
        FROM
        edu_telephone_record a
        LEFT JOIN edu_telephone_family b ON a.card_id = b.card_id and a.calling = b.phone
        WHERE
        a.card_id = #{cardId}
        AND a.del_flag = 0
        order by a.call_date desc
    </select>
</mapper>
