<?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.CcCallTaskMapper">
    
    <resultMap type="CcCallTask" id="CcCallTaskResult">
        <result property="batchId"    column="batch_id"    />
        <result property="groupId"    column="group_id"    />
        <result property="batchName"    column="batch_name"    />
        <result property="ifcall"    column="ifcall"    />
        <result property="rate"    column="rate"    />
        <result property="threadNum"    column="thread_num"    />
        <result property="createtime"    column="createtime"    />
        <result property="executing"    column="executing"    />
        <result property="stopTime"    column="stop_time"    />
        <result property="userid"    column="userid"    />
        <result property="gatewayId"    column="gateway_id"    />
        <result property="taskType"    column="task_type"    />
        <result property="voiceCode"    column="voice_code"    />
        <result property="voiceSource"    column="voice_source"    />
        <result property="avgRingTimeLen"    column="avg_ring_time_len"    />
        <result property="avgCallTalkTimeLen"    column="avg_call_talk_time_len"    />
        <result property="avgCallEndProcessTimeLen"    column="avg_call_end_process_time_len"    />
        <result property="callNodeNo"    column="call_node_no"    />
        <result property="llmAccountId"    column="llm_account_id"    />
        <result property="playTimes"    column="play_times"    />
        <result property="asrProvider"    column="asr_provider"    />
        <result property="aiTransferType"    column="ai_transfer_type"    />
        <result property="aiTransferData"    column="ai_transfer_data"    />
        <result property="autoStop"    column="auto_stop"    />
        <result property="ivrId"    column="ivr_id"    />
        <result property="asrLanguageCode"    column="asr_language_code"    />
        <result property="ttsLanguageCode"    column="tts_language_code"    />
        <result property="asrModels"    column="asr_models"    />
        <result property="ttsModels"    column="tts_models"    />
    </resultMap>

    <sql id="selectCcCallTaskVo">
        select batch_id, group_id, batch_name, ifcall, rate, thread_num,
               createtime, executing, stop_time, userid, task_type,
               gateway_id, voice_code, voice_source, avg_ring_time_len,
               avg_call_talk_time_len, avg_call_end_process_time_len,
               call_node_no, llm_account_id, play_times, asr_provider,
               ai_transfer_type, ai_transfer_data, auto_stop,
               ivr_id, asr_language_code, tts_language_code, asr_models, tts_models from cc_call_task
    </sql>

    <select id="selectCcCallTaskList" parameterType="CcCallTask" resultMap="CcCallTaskResult">
        <include refid="selectCcCallTaskVo"/>
        <where>
            <if test="batchId != null "> and batch_id = #{batchId}</if>
            <if test="groupId != null  and groupId != ''"> and group_id = #{groupId}</if>
            <if test="batchName != null  and batchName != ''"> and batch_name like concat('%', #{batchName}, '%')</if>
            <if test="ifcall != null "> and ifcall = #{ifcall}</if>
            <if test="taskType != null "> and task_type = #{taskType}</if>
            <if test="gatewayId != null "> and gateway_id = #{gatewayId}</if>
            <if test="voiceCode != null and voiceCode != ''"> and voice_code = #{voiceCode}</if>
            <if test="voiceSource != null and voiceSource != ''"> and voice_source = #{voiceSource}</if>
            <if test="llmAccountId != null"> and llm_account_id = #{llmAccountId}</if>
            <if test="params.createTimeStart != null and params.createTimeStart != ''"><!-- 开始时间检索 -->
                AND createtime &gt;= #{params.createTimeStart}
            </if>
            <if test="params.createTimeEnd != null and params.createTimeEnd != ''"><!-- 结束时间检索 -->
                AND createtime &lt;= #{params.createTimeEnd}
            </if>
            <if test="asrProvider != null and asrProvider != ''" > and asr_provider = #{asrProvider}</if>
            <if test="aiTransferType != null and aiTransferType != ''" > and ai_transfer_type = #{aiTransferType}</if>
            <if test="aiTransferData != null and aiTransferData != ''" > and ai_transfer_data = #{aiTransferData}</if>
            <if test="asrLanguageCode != null  and asrLanguageCode != ''"> and asr_language_code = #{asrLanguageCode}</if>
            <if test="ttsLanguageCode != null  and ttsLanguageCode != ''"> and tts_language_code = #{ttsLanguageCode}</if>
            <if test="asrModels != null  and asrModels != ''"> and asr_models = #{asrModels}</if>
            <if test="ttsModels != null  and ttsModels != ''"> and tts_models = #{ttsModels}</if>

        </where>
        order by batch_id desc
    </select>
    
    <select id="selectCcCallTaskByBatchId" parameterType="Long" resultMap="CcCallTaskResult">
        <include refid="selectCcCallTaskVo"/>
        where batch_id = #{batchId}
    </select>

    <insert id="insertCcCallTask" parameterType="CcCallTask" useGeneratedKeys="true" keyProperty="batchId">
        insert into cc_call_task
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="groupId != null and groupId != ''">group_id,</if>
            <if test="batchName != null and batchName != ''">batch_name,</if>
            <if test="ifcall != null">ifcall,</if>
            <if test="rate != null">rate,</if>
            <if test="threadNum != null">thread_num,</if>
            <if test="createtime != null">createtime,</if>
            <if test="executing != null">executing,</if>
            <if test="stopTime != null">stop_time,</if>
            <if test="userid != null and userid != ''">userid,</if>
            <if test="taskType != null">task_type,</if>
            <if test="gatewayId != null">gateway_id,</if>
            voice_code,
            voice_source,
            <if test="avgRingTimeLen != null">avg_ring_time_len,</if>
            <if test="avgCallTalkTimeLen != null">avg_call_talk_time_len,</if>
            <if test="avgCallEndProcessTimeLen != null">avg_call_end_process_time_len,</if>
            <if test="callNodeNo != null">call_node_no,</if>
            <if test="llmAccountId != null">llm_account_id,</if>
            <if test="playTimes != null">play_times,</if>
            <if test="asrProvider != null and asrProvider != ''">asr_provider,</if>
            <if test="aiTransferType != null and aiTransferType != ''">ai_transfer_type,</if>
            <if test="aiTransferData != null and aiTransferData != ''">ai_transfer_data,</if>
            <if test="autoStop != null ">auto_stop,</if>
            <if test="ivrId != null and ivrId != ''">ivr_id,</if>
            <if test="asrLanguageCode != null and asrLanguageCode != ''">asr_language_code,</if>
            <if test="ttsLanguageCode != null and ttsLanguageCode != ''">tts_language_code,</if>
            <if test="asrModels != null and asrModels != ''">asr_models,</if>
            <if test="ttsModels != null and ttsModels != ''">tts_models,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="groupId != null and groupId != ''">#{groupId},</if>
            <if test="batchName != null and batchName != ''">#{batchName},</if>
            <if test="ifcall != null">#{ifcall},</if>
            <if test="rate != null">#{rate},</if>
            <if test="threadNum != null">#{threadNum},</if>
            <if test="createtime != null">#{createtime},</if>
            <if test="executing != null">#{executing},</if>
            <if test="stopTime != null">#{stopTime},</if>
            <if test="userid != null and userid != ''">#{userid},</if>
            <if test="taskType != null">#{taskType},</if>
            <if test="gatewayId != null">#{gatewayId},</if>
            #{voiceCode},
            #{voiceSource},
            <if test="avgRingTimeLen != null">#{avgRingTimeLen},</if>
            <if test="avgCallTalkTimeLen != null">#{avgCallTalkTimeLen},</if>
            <if test="avgCallEndProcessTimeLen != null">#{avgCallEndProcessTimeLen},</if>
            <if test="callNodeNo != null">#{callNodeNo},</if>
            <if test="llmAccountId != null">#{llmAccountId},</if>
            <if test="playTimes != null">#{playTimes},</if>
            <if test="asrProvider != null and asrProvider != ''">#{asrProvider},</if>
            <if test="aiTransferType != null and aiTransferType != ''">#{aiTransferType},</if>
            <if test="aiTransferData != null and aiTransferData != ''">#{aiTransferData},</if>
            <if test="autoStop != null ">#{autoStop},</if>
            <if test="ivrId != null and ivrId != ''">#{ivrId},</if>
            <if test="asrLanguageCode != null and asrLanguageCode != ''">#{asrLanguageCode},</if>
            <if test="ttsLanguageCode != null and ttsLanguageCode != ''">#{ttsLanguageCode},</if>
            <if test="asrModels != null and asrModels != ''">#{asrModels},</if>
            <if test="ttsModels != null and ttsModels != ''">#{ttsModels},</if>

        </trim>
    </insert>

    <update id="updateCcCallTask" parameterType="CcCallTask">
        update cc_call_task
        <trim prefix="SET" suffixOverrides=",">
            <if test="groupId != null and groupId != ''">group_id = #{groupId},</if>
            <if test="batchName != null and batchName != ''">batch_name = #{batchName},</if>
            <if test="ifcall != null">ifcall = #{ifcall},</if>
            <if test="rate != null">rate = #{rate},</if>
            <if test="threadNum != null">thread_num = #{threadNum},</if>
            <if test="createtime != null">createtime = #{createtime},</if>
            <if test="executing != null">executing = #{executing},</if>
            <if test="stopTime != null">stop_time = #{stopTime},</if>
            <if test="userid != null and userid != ''">userid = #{userid},</if>
            <if test="gatewayId != null">gateway_id = #{gatewayId},</if>
            <if test="taskType != null">task_type = #{taskType},</if>
            voice_code = #{voiceCode},
            voice_source = #{voiceSource},
            <if test="avgRingTimeLen != null">avg_ring_time_len = #{avgRingTimeLen},</if>
            <if test="avgCallTalkTimeLen != null">avg_call_talk_time_len = #{avgCallTalkTimeLen},</if>
            <if test="avgCallEndProcessTimeLen != null">avg_call_end_process_time_len = #{avgCallEndProcessTimeLen},</if>
            <if test="callNodeNo != null">call_node_no = #{callNodeNo},</if>
            <if test="llmAccountId != null">llm_account_id = #{llmAccountId},</if>
            <if test="playTimes != null">play_times = #{playTimes},</if>
            <if test="asrProvider != null and asrProvider != ''">asr_provider = #{asrProvider},</if>
            <if test="aiTransferType != null and aiTransferType != ''">ai_transfer_type = #{aiTransferType},</if>
            <if test="aiTransferData != null and aiTransferData != ''">ai_transfer_data = #{aiTransferData},</if>
            <if test="autoStop != null ">auto_stop = #{autoStop},</if>
            <if test="ivrId != null and ivrId != ''">ivr_id = #{ivrId},</if>
            <if test="asrLanguageCode != null and asrLanguageCode != ''">asr_language_code = #{asrLanguageCode},</if>
            <if test="ttsLanguageCode != null and ttsLanguageCode != ''">tts_language_code = #{ttsLanguageCode},</if>
            <if test="asrModels != null and asrModels != ''">asr_models = #{asrModels},</if>
            <if test="ttsModels != null and ttsModels != ''">tts_models = #{ttsModels},</if>
        </trim>
        where batch_id = #{batchId}
    </update>

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

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


    <select id="selectCcCallTaskByBatchName" resultMap="CcCallTaskResult">
        <include refid="selectCcCallTaskVo"/>
        where batch_name = #{batchName} and task_type = #{taskType}
    </select>

    <insert id="bakCallTaskByBatchId" parameterType="Long">
        insert into his_cc_call_task select * from cc_call_task where batch_id = #{batchId}
    </insert>

</mapper>