<?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.yeejoin.amos.boot.module.jcs.api.mapper.VoiceRecordFileMapper">

    <sql id="componentField">
        r.sequence_nbr AS sequenceNbr,
        r.connect_id AS connectId,
        r.file_type AS fileType,
        r.caller AS caller,
        r.called AS called,
        r.tel_start_time AS telStartTime,
        r.tel_end_time AS telEndTime,
        r.alert_id AS alertId,
        r.alert_stage AS alertStage,
        r.alert_stage_code AS alertStageCode,
        r.tel_time AS telTime
    </sql>

    <select id="queryRecordListByQueryDto" resultType="java.util.Map">
        SELECT
        <include refid="componentField"/>
        FROM
        jc_voice_record_file r
        LEFT JOIN jc_alert_called a ON a.sequence_nbr =
        r.alert_id
        WHERE r.is_delete = 0
        <if test="telStartTimeStr != null ">
            and #{telStartTimeStr} <![CDATA[ <= ]]>
            r.tel_start_time
        </if>
        <if test="telEndTimeStr != null ">
            and r.tel_end_time <![CDATA[ <= ]]>
            #{telEndTimeStr}
        </if>
        <if test="caller != null and caller != ''">
            AND r.caller like
            CONCAT(CONCAT('%',#{caller}),'%')
        </if>
        <if test="called != null and called != ''">
            AND r.called like
            CONCAT(CONCAT('%',#{called}),'%')
        </if>
        <if test="fileType != null and fileType != ''">
            AND r.file_type = #{fileType}
        </if>
        <choose>
            <when test="sortParam != null and sortParam != '' and sortRule != null and sortRule != ''    ">
                ORDER BY r.${sortParam} ${sortRule}
            </when>
            <otherwise>
                ORDER BY r.tel_start_time desc
            </otherwise>
        </choose>
    </select>

    <select id="getRecordById" resultType="com.yeejoin.amos.boot.module.jcs.api.dto.VoiceRecordFileDto">
        SELECT
        <include refid="componentField"/>
        FROM
        jc_voice_record_file r
        LEFT JOIN jc_alert_called a ON a.sequence_nbr =
        r.alert_id
        WHERE r.sequence_nbr = #{sequenceNbr}
    </select>

    <select id="selectExportData" resultType="com.yeejoin.amos.boot.module.jcs.api.dto.VoiceRecordFileDto">
        select
        <include refid="componentField"/>
        FROM
        jc_voice_record_file r
        LEFT JOIN jc_alert_called a ON a.sequence_nbr = r.alert_id
        where r.is_delete = 0
        <if test="ids != null and ids.size() > 0">
            and r.sequence_nbr in
            <foreach item="item" collection="ids" separator="," open="(" close=")" index="">  #{item}
            </foreach>
        </if>
    </select>

</mapper>
