<?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.qw.mapper.QwUserVoiceLogMapper">
    
    <resultMap type="QwUserVoiceLog" id="QwUserVoiceLogResult">
        <result property="id"    column="id"    />
        <result property="extId"    column="ext_id"    />
        <result property="qwUserId"    column="qw_user_id"    />
        <result property="duration"    column="duration"    />
        <result property="title"    column="title"    />
        <result property="status"    column="status"    />
        <result property="corpId"    column="corp_id"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyUserId"    column="company_user_id"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <resultMap type="com.fs.qw.vo.QwUserVoiceLogTotalVo" id="QwUserVoiceLogVoTotalResult">
        <result property="id"    column="id"    />
        <result property="qwUserId"    column="qw_user_id"    />
        <result property="duration"    column="duration"    />
        <result property="connectCount"    column="connectCount"    />
        <result property="noConnectCount"    column="noConnectCount"    />
        <result property="createTime"     column="create_time"     />
        <association property="qwUser" javaType="com.fs.qw.domain.QwUser" autoMapping="true">
            <id column="qw_uer_id" property="qwUserId"></id>
            <result column="qw_user_name" property="qwUserName"></result>
        </association>
    </resultMap>

    <resultMap type="com.fs.qw.vo.QwUserVoiceLogVo" id="QwUserVoiceLogVoResult" extends="QwUserVoiceLogResult">
        <association property="qwExternalContact" javaType="com.fs.qw.domain.QwExternalContact" autoMapping="true">
            <id column="id" property="id"></id>
            <result column="name" property="name"></result>
        </association>
        <association property="company" javaType="com.fs.company.domain.Company" autoMapping="true">
            <id column="company_id" property="companyId"></id>
            <result column="company_name" property="companyName"></result>
        </association>
        <association property="companyUser" javaType="com.fs.company.domain.CompanyUser" autoMapping="true">
            <id column="user_id" property="userId"></id>
            <result column="user_name" property="userName"></result>
        </association>

        <association property="qwUser" javaType="com.fs.qw.domain.QwUser" autoMapping="true">
            <id column="qw_uer_id" property="qwUserId"></id>
            <result column="qw_user_name" property="qwUserName"></result>
        </association>

    </resultMap>

    <sql id="selectQwUserVoiceLogVo">
        select id, ext_id, qw_user_id, duration, title, status, corp_id, company_id, company_user_id, create_time from qw_user_voice_log
    </sql>

    <select id="selectQwUserVoiceLogList" resultMap="QwUserVoiceLogVoResult">
        select uvl.id, ext_id, uvl.qw_user_id, duration, title, uvl.status, uvl.corp_id, uvl.company_id, uvl.company_user_id, uvl.create_time,qec.`name`,c.company_name,cu.user_name,qu.qw_user_name
        from qw_user_voice_log uvl
        left join qw_external_contact qec on uvl.ext_id = qec.id
        left join company c on uvl.company_id = c.company_id
        left join company_user cu on uvl.company_user_id = cu.user_id
        left join qw_user qu on uvl.qw_user_id = qu.id
        <where>
            <if test="extName != null "> and qec.name like concat(#{extName}, '%')</if>
            <if test="qwUserName != null "> and qu.qw_user_name like concat(#{qwUserName}, '%')</if>
            <if test="title != null  and title != ''"> and uvl.title = #{title}</if>
            <if test="status != null "> and uvl.status = #{status}</if>
            <if test="corpId != null  and corpId != ''"> and uvl.corp_id = #{corpId}</if>
            <if test="companyId != null "> and c.company_id = #{companyId}</if>
            <if test="companyName != null "> and c.company_name like concat(#{companyName}, '%')</if>
            <if test="companyUserId != null "> and cu.user_id = #{companyUserId}</if>
            <if test="companyUserName != null  and companyUserName != ''"> and cu.user_name like concat(#{companyUserName}, '%')</if>
            <if test="beginTime != null and endTime != null">
                AND date_format(uvl.create_time,'%Y-%m-%d') &gt;= #{beginTime}
                AND date_format(uvl.create_time,'%Y-%m-%d') &lt;= #{endTime}
            </if>
        </where>
    </select>
    
    <select id="selectQwUserVoiceLogById" parameterType="Long" resultMap="QwUserVoiceLogResult">
        <include refid="selectQwUserVoiceLogVo"/>
        where id = #{id}
    </select>
    <select id="selectQwUserVoiceLogTotalList" resultMap="QwUserVoiceLogVoTotalResult">

        SELECT
        SUM(duration) duration,qu.qw_user_name,
        COUNT(CASE WHEN uvl.status=1 THEN 1 END) AS connectCount,
        COUNT(CASE WHEN uvl.status=2 THEN 1 END) AS noConnectCount
        FROM qw_user_voice_log uvl
        LEFT JOIN qw_user qu ON uvl.qw_user_id = qu.id
        <where>
            <if test="qwUserName != null "> and qu.qw_user_name like concat(#{qwUserName}, '%')</if>
            <if test="beginTime != null and endTime != null">
                AND date_format(uvl.create_time,'%Y-%m-%d') &gt;= #{beginTime}
                AND date_format(uvl.create_time,'%Y-%m-%d') &lt;= #{endTime}
            </if>
        </where>
        group by qu.qw_user_name

    </select>


    <insert id="insertQwUserVoiceLog" parameterType="QwUserVoiceLog" useGeneratedKeys="true" keyProperty="id">
        insert into qw_user_voice_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="extId != null">ext_id,</if>
            <if test="qwUserId != null">qw_user_id,</if>
            <if test="duration != null">duration,</if>
            <if test="title != null">title,</if>
            <if test="status != null">status,</if>
            <if test="corpId != null">corp_id,</if>
            <if test="companyId != null">company_id,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="extId != null">#{extId},</if>
            <if test="qwUserId != null">#{qwUserId},</if>
            <if test="duration != null">#{duration},</if>
            <if test="title != null">#{title},</if>
            <if test="status != null">#{status},</if>
            <if test="corpId != null">#{corpId},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateQwUserVoiceLog" parameterType="QwUserVoiceLog">
        update qw_user_voice_log
        <trim prefix="SET" suffixOverrides=",">
            <if test="extId != null">ext_id = #{extId},</if>
            <if test="qwUserId != null">qw_user_id = #{qwUserId},</if>
            <if test="duration != null">duration = #{duration},</if>
            <if test="title != null">title = #{title},</if>
            <if test="status != null">status = #{status},</if>
            <if test="corpId != null">corp_id = #{corpId},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteQwUserVoiceLogById" parameterType="Long">
        delete from qw_user_voice_log where id = #{id}
    </delete>

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