<?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.fs.company.mapper.CompanyVoiceLogsMapper">

    <resultMap type="CompanyVoiceLogs" id="CompanyVoiceLogsResult">
        <result property="voiceId"    column="voice_id"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyName"    column="company_name"    />
        <result property="voiceTitle"    column="voice_title"    />
        <result property="voiceUrl"    column="voice_url"    />
        <result property="startTime"    column="start_time"    />
        <result property="finishTime"    column="finish_time"    />
        <result property="voiceType"    column="voice_type"    />
        <result property="remark"    column="remark"    />
        <result property="callerPhone"    column="caller_phone"    />
        <result property="calleePhone"    column="callee_phone"    />
        <result property="times"    column="times"    />
        <result property="displayCallerNumber"    column="display_caller_number"    />
        <result property="displayCalleeNumber"    column="display_callee_number"    />
        <result property="sessionId"    column="session_id"    />
        <result property="companyUserId"    column="company_user_id"    />
        <result property="status"    column="status"    />
        <result property="customerId"    column="customer_id"    />
        <result property="billingTime"    column="billing_time"    />
        <result property="apiId"    column="api_id"    />
        <result property="callerNo"    column="caller_no"    />
    </resultMap>

    <sql id="selectCompanyVoiceLogsVo">
        select l.*,c.company_name  from company_voice_logs l left join company c on c.company_id = l.company_id
    </sql>

    <select id="selectCompanyVoiceLogsList" parameterType="CompanyVoiceLogs" resultMap="CompanyVoiceLogsResult">
        <include refid="selectCompanyVoiceLogsVo"/>
        <where>
            <if test="companyId != null "> and l.company_id = #{companyId}</if>
            <if test="voiceTitle != null  and voiceTitle != ''"> and voice_title = #{voiceTitle}</if>
            <if test="voiceUrl != null  and voiceUrl != ''"> and voice_url = #{voiceUrl}</if>
            <if test="startTime != null "> and start_time = #{startTime}</if>
            <if test="finishTime != null "> and finish_time = #{finishTime}</if>
            <if test="voiceType != null "> and voice_type = #{voiceType}</if>
            <if test="callerPhone != null  and callerPhone != ''"> and caller_phone = #{callerPhone}</if>
            <if test="calleePhone != null  and calleePhone != ''"> and callee_phone = #{calleePhone}</if>
            <if test="times != null "> and times = #{times}</if>
            <if test="displayCallerNumber != null  and displayCallerNumber != ''"> and display_caller_number = #{displayCallerNumber}</if>
            <if test="displayCalleeNumber != null  and displayCalleeNumber != ''"> and display_callee_number = #{displayCalleeNumber}</if>
            <if test="sessionId != null "> and session_id = #{sessionId}</if>
            <if test="customerId != null "> and customer_id = #{customerId}</if>

            <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
        </where>
        order by voice_id desc
    </select>

    <select id="selectCompanyVoiceLogsById" parameterType="Long" resultMap="CompanyVoiceLogsResult">
        <include refid="selectCompanyVoiceLogsVo"/>
        where voice_id = #{voiceId}
    </select>

    <insert id="insertCompanyVoiceLogs" parameterType="CompanyVoiceLogs" useGeneratedKeys="true" keyProperty="voiceId">
        insert into company_voice_logs
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyId != null">company_id,</if>
            <if test="voiceTitle != null">voice_title,</if>
            <if test="voiceUrl != null">voice_url,</if>
            <if test="startTime != null">start_time,</if>
            <if test="finishTime != null">finish_time,</if>
            <if test="voiceType != null">voice_type,</if>
            <if test="remark != null">remark,</if>
            <if test="callerPhone != null">caller_phone,</if>
            <if test="calleePhone != null">callee_phone,</if>
            <if test="times != null">times,</if>
            <if test="displayCallerNumber != null">display_caller_number,</if>
            <if test="displayCalleeNumber != null">display_callee_number,</if>
            <if test="sessionId != null">session_id,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="status != null">status,</if>
            <if test="customerId != null">customer_id,</if>
            <if test="billingTime != null">billing_time,</if>
            <if test="apiId != null">api_id,</if>
            <if test="callerNo != null">caller_no,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyId != null">#{companyId},</if>
            <if test="voiceTitle != null">#{voiceTitle},</if>
            <if test="voiceUrl != null">#{voiceUrl},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="finishTime != null">#{finishTime},</if>
            <if test="voiceType != null">#{voiceType},</if>
            <if test="remark != null">#{remark},</if>
            <if test="callerPhone != null">#{callerPhone},</if>
            <if test="calleePhone != null">#{calleePhone},</if>
            <if test="times != null">#{times},</if>
            <if test="displayCallerNumber != null">#{displayCallerNumber},</if>
            <if test="displayCalleeNumber != null">#{displayCalleeNumber},</if>
            <if test="sessionId != null">#{sessionId},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="status != null">#{status},</if>
            <if test="customerId != null">#{customerId},</if>
            <if test="billingTime != null">#{billingTime},</if>
            <if test="apiId != null">#{apiId},</if>
            <if test="callerNo != null">#{callerNo},</if>
         </trim>
    </insert>

    <update id="updateCompanyVoiceLogs" parameterType="CompanyVoiceLogs">
        update company_voice_logs
        <trim prefix="SET" suffixOverrides=",">
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="voiceTitle != null">voice_title = #{voiceTitle},</if>
            <if test="voiceUrl != null">voice_url = #{voiceUrl},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="finishTime != null">finish_time = #{finishTime},</if>
            <if test="voiceType != null">voice_type = #{voiceType},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="callerPhone != null">caller_phone = #{callerPhone},</if>
            <if test="calleePhone != null">callee_phone = #{calleePhone},</if>
            <if test="times != null">times = #{times},</if>
            <if test="displayCallerNumber != null">display_caller_number = #{displayCallerNumber},</if>
            <if test="displayCalleeNumber != null">display_callee_number = #{displayCalleeNumber},</if>
            <if test="sessionId != null">session_id = #{sessionId},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="status != null">status = #{status},</if>
            <if test="customerId != null">customer_id = #{customerId},</if>
            <if test="billingTime != null">billing_time = #{billingTime},</if>
            <if test="apiId != null">api_id = #{apiId},</if>
            <if test="callerNo != null">caller_no = #{callerNo},</if>
        </trim>
        where voice_id = #{voiceId}
    </update>

    <delete id="deleteCompanyVoiceLogsById" parameterType="Long">
        delete from company_voice_logs where voice_id = #{voiceId}
    </delete>

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

    <select id="selectVoiceLogsTotalCount" resultType="com.alibaba.fastjson.JSONObject">
        SELECT  count(t.company_user_id ) AS callCount,count( IF ( t.times > 0,1, NULL ) ) AS callSuccessCount,  t.type, sum(IFNULL(t.times,0)) as times,sum(IFNULL(t.billing_time,0)) as billingTime
        FROM
        (
        SELECT DATE_FORMAT( l.start_time, #{sqlDateFormat} ) AS type,  l.company_user_id,l.times,l.billing_time
        FROM
        company_voice_logs  AS l
        WHERE
        (DATE_FORMAT(l.start_time,#{sqlDateFormat}) between #{beginTime} and #{finalTime})
        AND l.company_user_id IN
        <foreach item="item" collection="userIds" open="(" separator="," close=")">
            #{item}
        </foreach>
        ) t
        GROUP BY t.type
    </select>

</mapper>
