<?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.ruoyi.aicall.mapper.CcCallPhoneMapper">
    
    <resultMap type="CcCallPhone" id="CcCallPhoneResult">
        <result property="id"    column="id"    />
        <result property="batchId"    column="batch_id"    />
        <result property="telephone"    column="telephone"    />
        <result property="custName"    column="cust_name"    />
        <result property="createtime"    column="createtime"    />
        <result property="callstatus"    column="callstatus"    />
        <result property="calloutTime"    column="callout_time"    />
        <result property="callcount"    column="callcount"    />
        <result property="callEndTime"    column="call_end_time"    />
        <result property="timeLen"    column="time_len"    />
        <result property="validTimeLen"    column="valid_time_len"    />
        <result property="uuid"    column="uuid"    />
        <result property="connectedTime"    column="connected_time"    />
        <result property="hangupCause"    column="hangup_cause"    />
        <result property="answeredTime"    column="answered_time"    />
        <result property="dialogue"    column="dialogue"    />
        <result property="wavfile"    column="wavfile"    />
        <result property="recordServerUrl"    column="record_server_url"    />
        <result property="bizJson"    column="biz_json"    />
        <result property="dialogueCount"    column="dialogue_count"    />
        <result property="acdOpnum"    column="acd_opnum"    />
        <result property="acdQueueTime"    column="dialogue_count"    />
        <result property="acdWaitTime"    column="dialogue_count"    />
        <result property="ttsText"    column="tts_text"    />
        <result property="intent"    column="intent"    />
        <result property="asrSeconds"    column="asr_seconds"    />
        <result property="ttsTimes"    column="tts_times"    />
        <result property="ttsFlowTokens"    column="tts_flow_tokens"    />
        <result property="inputTokens"    column="input_tokens"    />
        <result property="outputTokens"    column="output_tokens"    />
        <result property="totalCost"    column="total_cost"    />
        <result property="billingStatus"    column="billing_status"    />
        <result property="callerNumber"    column="caller_number"    />
        <result property="ivrDtmfDigits"    column="ivr_dtmf_digits"    />
        <result property="manualAnsweredTime"    column="manual_answered_time"    />
        <result property="manualAnsweredTimeLen"    column="manual_answered_time_len"    />
    </resultMap>

    <sql id="selectCcCallPhoneVo">
        select id, batch_id, telephone, cust_name, createtime, callstatus, callout_time,
               callcount, call_end_time, time_len, valid_time_len, uuid, connected_time,
               hangup_cause, answered_time, dialogue, wavfile, record_server_url, biz_json,
               dialogue_count, acd_opnum, acd_queue_time, acd_wait_time, tts_text, intent,
               asr_seconds, tts_times, tts_flow_tokens, input_tokens, output_tokens,
               total_cost, billing_status, caller_number, ivr_dtmf_digits, manual_answered_time, manual_answered_time_len from cc_call_phone
    </sql>

    <select id="selectCcCallPhoneList" parameterType="CcCallPhone" resultMap="CcCallPhoneResult">
        <include refid="selectCcCallPhoneVo"/>
        <where>
            <if test="uuid != null and uuid != '' "> and uuid = #{uuid}</if>
            <if test="batchId != null "> and batch_id = #{batchId}</if>
            <if test="telephone != null  and telephone != ''"> and telephone = #{telephone}</if>
            <if test="custName != null  and custName != ''"> and cust_name = #{custName}</if>
            <if test="callstatus != null "> and callstatus = #{callstatus}</if>
            <if test="params.calloutTimeStart != null and params.calloutTimeStart != ''"><!-- 开始时间检索 -->
                AND callout_time &gt;= #{params.calloutTimeStart}
            </if>
            <if test="params.calloutTimeEnd != null and params.calloutTimeEnd != ''"><!-- 结束时间检索 -->
                AND callout_time &lt;= #{params.calloutTimeEnd}
            </if>
            <if test="params.callEndTimeStart != null and params.callEndTimeStart != ''"><!-- 开始时间检索 -->
                AND call_end_time &gt;= #{params.callEndTimeStart}
            </if>
            <if test="params.callEndTimeEnd != null and params.callEndTimeEnd != ''"><!-- 结束时间检索 -->
                AND call_end_time &lt;= #{params.callEndTimeEnd}
            </if>
            <if test="params.timeLenStart != null and params.timeLenStart != ''"><!-- 开始时间检索 -->
                AND time_len &gt;= #{params.timeLenStart}
            </if>
            <if test="params.timeLenEnd != null and params.timeLenEnd != ''"><!-- 结束时间检索 -->
                AND time_len &lt;= #{params.timeLenEnd}
            </if>
            <if test="params.connectedTimeStart != null and params.connectedTimeStart != ''"><!-- 开始时间检索 -->
                AND connected_time &gt;= #{params.connectedTimeStart}
            </if>
            <if test="params.connectedTimeEnd != null and params.connectedTimeEnd != ''"><!-- 结束时间检索 -->
                AND connected_time &lt;= #{connectedTimeEnd}
            </if>
            <if test="params.answeredTimeStart != null and params.answeredTimeStart != ''"><!-- 开始时间检索 -->
                AND answered_time &gt;= #{params.answeredTimeStart}
            </if>
            <if test="params.answeredTimeEnd != null and params.answeredTimeEnd != ''"><!-- 结束时间检索 -->
                AND answered_time &lt;= #{params.answeredTimeEnd}
            </if>
            <if test="batchId != null and batchId != ''">
                AND batch_id = #{batchId}
            </if>
            <if test="intent != null and intent != ''">
                AND intent = #{intent}
            </if>
            <if test="billingStatus != null ">
                and billing_status = #{billingStatus}
            </if>
            <if test="callerNumber != null and callerNumber != '' ">
                and caller_number = #{callerNumber}
            </if>
            AND callstatus >= 3 AND call_end_time > 0
        </where>
        order by call_end_time desc
    </select>

    <select id="getCustIntentionList" parameterType="CcCallPhone" resultMap="CcCallPhoneResult">
        <include refid="selectCcCallPhoneVo"/>
          where intent = '' AND callstatus >= 3 AND call_end_time > 0 limit 100
    </select>
    
    <select id="selectCcCallPhoneById" parameterType="String" resultMap="CcCallPhoneResult">
        <include refid="selectCcCallPhoneVo"/>
        where id = #{id}
    </select>

    <insert id="insertCcCallPhone" parameterType="CcCallPhone">
        insert into cc_call_phone
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="batchId != null">batch_id,</if>
            <if test="telephone != null and telephone != ''">telephone,</if>
            <if test="custName != null and custName != ''">cust_name,</if>
            <if test="createtime != null">createtime,</if>
            <if test="callstatus != null">callstatus,</if>
            <if test="calloutTime != null">callout_time,</if>
            <if test="callcount != null">callcount,</if>
            <if test="callEndTime != null">call_end_time,</if>
            <if test="timeLen != null">time_len,</if>
            <if test="validTimeLen != null">valid_time_len,</if>
            <if test="uuid != null and uuid != ''">uuid,</if>
            <if test="connectedTime != null">connected_time,</if>
            <if test="hangupCause != null and hangupCause != ''">hangup_cause,</if>
            <if test="answeredTime != null">answered_time,</if>
            <if test="dialogue != null">dialogue,</if>
            <if test="wavfile != null and wavfile != ''">wavfile,</if>
            <if test="recordServerUrl != null and recordServerUrl != ''">record_server_url,</if>
            <if test="bizJson != null and bizJson != ''">biz_json,</if>
            <if test="dialogueCount != null">dialogue_count,</if>
            <if test="acdOpnum != null and acdOpnum != ''">acd_opnum,</if>
            <if test="acdQueueTime != null">acd_queue_time,</if>
            <if test="acdWaitTime != null">acd_wait_time,</if>
            <if test="ttsText != null">tts_text,</if>
            <if test="intent != null">intent,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="batchId != null">#{batchId},</if>
            <if test="telephone != null and telephone != ''">#{telephone},</if>
            <if test="custName != null and custName != ''">#{cust_name},</if>
            <if test="createtime != null">#{createtime},</if>
            <if test="callstatus != null">#{callstatus},</if>
            <if test="calloutTime != null">#{calloutTime},</if>
            <if test="callcount != null">#{callcount},</if>
            <if test="callEndTime != null">#{callEndTime},</if>
            <if test="timeLen != null">#{timeLen},</if>
            <if test="validTimeLen != null">#{validTimeLen},</if>
            <if test="uuid != null and uuid != ''">#{uuid},</if>
            <if test="connectedTime != null">#{connectedTime},</if>
            <if test="hangupCause != null and hangupCause != ''">#{hangupCause},</if>
            <if test="answeredTime != null">#{answeredTime},</if>
            <if test="dialogue != null">#{dialogue},</if>
            <if test="wavfile != null and wavfile != ''">#{wavfile},</if>
            <if test="recordServerUrl != null and recordServerUrl != ''">#{recordServerUrl},</if>
            <if test="bizJson != null and bizJson != ''">#{bizJson},</if>
            <if test="dialogueCount != null">#{dialogueCount},</if>
            <if test="acdOpnum != null and acdOpnum != ''">#{acdOpnum},</if>
            <if test="acdQueueTime != null">#{acdQueueTime},</if>
            <if test="acdWaitTime != null">#{acdWaitTime},</if>
            <if test="ttsText != null">#{ttsText},</if>
            <if test="intent != null">#{intent},</if>
        </trim>
    </insert>

    <update id="updateCcCallPhone" parameterType="CcCallPhone">
        update cc_call_phone
        <trim prefix="SET" suffixOverrides=",">
            <if test="batchId != null">batch_id = #{batchId},</if>
            <if test="telephone != null and telephone != ''">telephone = #{telephone},</if>
            <if test="custName != null and custName != ''">cust_name = #{custName},</if>
            <if test="createtime != null">createtime = #{createtime},</if>
            <if test="callstatus != null">callstatus = #{callstatus},</if>
            <if test="calloutTime != null">callout_time = #{calloutTime},</if>
            <if test="callcount != null">callcount = #{callcount},</if>
            <if test="callEndTime != null">call_end_time = #{callEndTime},</if>
            <if test="timeLen != null">time_len = #{timeLen},</if>
            <if test="validTimeLen != null">valid_time_len = #{validTimeLen},</if>
            <if test="uuid != null and uuid != ''">uuid = #{uuid},</if>
            <if test="connectedTime != null">connected_time = #{connectedTime},</if>
            <if test="hangupCause != null and hangupCause != ''">hangup_cause = #{hangupCause},</if>
            <if test="answeredTime != null">answered_time = #{answeredTime},</if>
            <if test="dialogue != null">dialogue = #{dialogue},</if>
            <if test="wavfile != null and wavfile != ''">wavfile = #{wavfile},</if>
            <if test="recordServerUrl != null and recordServerUrl != ''">record_server_url = #{recordServerUrl},</if>
            <if test="bizJson != null and bizJson != ''">biz_json = #{bizJson},</if>
            <if test="dialogueCount != null">dialogue_count = #{dialogueCount},</if>
            <if test="acdOpnum != null and acdOpnum != ''">acd_opnum = #{acdOpnum},</if>
            <if test="acdQueueTime != null">acd_queue_time = #{acdQueueTime},</if>
            <if test="acdWaitTime != null">acd_wait_time = #{acdWaitTime},</if>
            <if test="ttsText != null">tts_text = #{ttsText},</if>
            <if test="intent != null">intent = #{intent},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCcCallPhoneById" parameterType="String">
        delete from cc_call_phone where id = #{id}
    </delete>

    <delete id="deleteCcCallPhoneByIds" parameterType="String">
        delete from cc_call_phone where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>


    <select id="statByBatchId" parameterType="Long" resultType="com.ruoyi.aicall.model.CallTaskStatModel">
        SELECT batch_id AS batchId,
               COUNT(1) AS phoneCount,
               SUM(CASE WHEN callout_time > 0 THEN 1 ELSE 0 END) AS callCount,
               SUM(CASE WHEN answered_time > 0 THEN 1 ELSE 0 END) AS connectCount
        FROM cc_call_phone
        WHERE batch_id = #{batchId}
    </select>

    <insert id="batchInsertCcCallPhone" parameterType="java.util.List">
        INSERT INTO cc_call_phone (
        id, batch_id, telephone, cust_name, createtime, callstatus,
        callout_time, callcount, call_end_time, time_len, valid_time_len,
        uuid, connected_time, hangup_cause,
        answered_time, dialogue, wavfile, record_server_url, biz_json,
        dialogue_count,acd_opnum,acd_queue_time,acd_wait_time,
        tts_text, intent
        )
        VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.id}, #{item.batchId}, #{item.telephone}, #{item.custName},
            #{item.createtime}, #{item.callstatus}, #{item.calloutTime},
            #{item.callcount}, #{item.callEndTime}, #{item.timeLen},
            #{item.validTimeLen}, #{item.uuid},
            #{item.connectedTime}, #{item.hangupCause},
            #{item.answeredTime}, #{item.dialogue}, #{item.wavfile},
            #{item.recordServerUrl}, #{item.bizJson}, #{item.dialogueCount},
            #{item.acdOpnum}, #{item.acdQueueTime}, #{item.acdWaitTime},
            #{item.ttsText}, #{item.intent}
            )
        </foreach>
    </insert>

    <update id="updateIntentionByIds">
        UPDATE cc_call_phone
        SET intent = #{intention}
        WHERE id IN
        <foreach collection="phoneIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>



    <select id="selectNoHangupCalls" parameterType="Long" resultMap="CcCallPhoneResult">
        <include refid="selectCcCallPhoneVo"/>
        <where>
            AND batch_id = #{batchId}
            AND callstatus >= 2
            AND call_end_time = 0
        </where>
        order by call_end_time desc
    </select>


    <insert id="bakCallPhoneByBatchId" parameterType="Long">
        insert into his_cc_call_phone select * from cc_call_phone where batch_id = #{batchId}
    </insert>


    <delete id="delCallPhoneByBatchId" parameterType="Long">
        delete from cc_call_phone where batch_id = #{batchId}
    </delete>

    <select id="isDuplicateEntry" resultType="java.lang.String">
        SELECT DISTINCT telephone FROM cc_call_phone
        WHERE batch_id = #{batchId} AND callstatus = 0
          AND telephone IN
        <foreach collection="phoneList" item="phone" open="(" separator="," close=")">
            #{phone.phoneNum}
        </foreach>
    </select>
</mapper>