<?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.tofly.callcenter.mapper.PltCalllist1000Mapper">

    <resultMap id="pltCalllist1000Map" type="com.tofly.callcenter.entity.PltCalllist1000">
        <id property="id" column="id"/>
        <result property="connectionid" column="CONNECTIONID"/>
        <result property="callType" column="CALL_TYPE"/>
        <result property="telephone" column="TELEPHONE"/>
        <result property="sysphone" column="SYSPHONE"/>
<!--        <result property="channel" column="CHANNEL"/>-->
        <result property="callTime" column="CALL_TIME"/>
        <result property="connectTime" column="CONNECT_TIME"/>
        <result property="queueTime" column="Queue_Time"/>
<!--        <result property="getfreeTime" column="getfree_time"/>-->
        <result property="agentFirstringTime" column="AGENT_FIRSTRING_TIME"/>
<!--        <result property="agentFirstconnectTime" column="AGENT_FIRSTCONNECT_TIME"/>-->
        <result property="hangupTime" column="HANGUP_TIME"/>
        <result property="times" column="TIMES"/>
        <result property="callResult" column="CALL_RESULT"/>
        <result property="causeValue" column="Cause_Value"/>
        <result property="causeDesc" column="Cause_Desc"/>
        <result property="transferResult" column="TRANSFER_RESULT"/>
        <result property="charge" column="CHARGE"/>
        <result property="recordFile" column="RECORD_FILE"/>
        <result property="fileServer" column="FILE_SERVER"/>
        <result property="transferCount" column="TRANSFER_COUNT"/>
        <result property="transferSuccess" column="TRANSFER_SUCCESS"/>
<!--        <result property="province" column="PROVINCE"/>-->
<!--        <result property="city" column="CITY"/>-->
<!--        <result property="district" column="DISTRICT"/>-->
<!--        <result property="empcode" column="EMPCODE"/>-->
        <result property="agentFirstCode" column="AGENT_FIRST_CODE"/>
<!--        <result property="deptid" column="DEPTID"/>-->
<!--        <result property="reason" column="REASON"/>-->
        <result property="groupCode" column="GROUP_CODE"/>
        <result property="pressKey" column="PRESS_KEY"/>
        <result property="pressKeyName" column="PRESS_KEY_NAME"/>
<!--        <result property="pressKeyTime" column="PRESS_KEY_TIME"/>-->
<!--        <result property="businessid" column="BUSINESSID"/>-->
<!--        <result property="businessname" column="BUSINESSNAME"/>-->
        <result property="hangupflag" column="HANGUPFLAG"/>
<!--        <result property="isGrade" column="IS_GRADE"/>-->
        <association property="callReasultName" column="CALL_RESULT"
                     select="getCallResultName"/>
        <association property="callTypeName" column="CALL_TYPE"
                     select="getCallTypeName"/>
    </resultMap>

    <select id="getPltCallInfo" resultType="map">
        select AGENT_FIRST_CODE code,
        count(*) sum_num
        from PLT_CALLLIST1000
        where CALL_RESULT = 1
        <if test="crew != null and crew != ''">
            and AGENT_FIRST_CODE = #{crew}
        </if>
        <if test="start != null and start != '' and end != null and end != ''">
            and date_format(CONNECT_TIME, '%Y-%m-%d') between #{start} and #{end}
        </if>
        and RECORD_FILE is not null
        and RECORD_FILE != ''
        and AGENT_FIRST_CODE is not null
        and AGENT_FIRST_CODE != ''
        group by EMPCODE
    </select>

    <!--select decode(#{callResult} ,0,'未呼叫',1,'成功',2,'无人接听',3,'忙',4,'空号',5,'关机',6,'停机',7,'线路故障',8,'网络错误',9,'呼叫拒收',10,'号码错误',11,'硬件呼叫返回错误',12,'其它情况',-->
    <select id="getCallResultName" resultType="string">
        SELECT (
                   CASE #{callResult}
                       WHEN '0' THEN
                           '未呼叫'
                       WHEN '1' THEN
                           '成功'
                       WHEN '2' THEN
                           '无人接听'
                       WHEN '3' THEN
                           '忙'
                       WHEN '4' THEN
                           '空号'
                       WHEN '5' THEN
                           '关机'
                       WHEN '6' THEN
                           '停机'
                       WHEN '7' THEN
                           '线路故障'
                       WHEN '8' THEN
                           '网络错误'
                       WHEN '9' THEN
                           '呼叫拒收'
                       WHEN '10' THEN
                           '号码错误'
                       WHEN '11' THEN
                           '硬件呼叫返回错误'
                       WHEN '12' THEN
                           '其它情况'
                       WHEN '13' THEN
                           '呼叫清除'
                       WHEN '21' THEN
                           '坐席全忙'
                       WHEN '22' THEN
                           '坐席无应答'
                       WHEN '23' THEN
                           '坐席拒绝'
                       WHEN '24' THEN
                           '坐席未登录'
                       WHEN '25' THEN
                           '用户挂机'
                       END
                   )
        from dual


    </select>

    <select id="getCallTypeName" resultType="string">
        select ELT(#{callType}, '呼入', '呼出', '内部呼叫')
        from dual
    </select>
    <select id="getCallInCount" resultType="long">
        SELECT COUNT(*)
        FROM plt_calllist1000
        WHERE date_format(CALL_TIME, '%Y-%m-%d') = curdate()
          AND CALL_TYPE = '1'
          and TRANSFER_RESULT = 1
    </select>

    <select id="getNotCallInCount" resultType="long">
        SELECT COUNT(*)
        FROM plt_calllist1000
        WHERE date_format(CALL_TIME, '%Y-%m-%d') = curdate()
          AND CALL_TYPE = '1'
          and TRANSFER_RESULT in (3, 4, 5, 6)
    </select>

    <select id="getSatisfactionCount" resultType="java.util.Map">
        select a.AGENT_FIRST_CODE empcode, sum(case when b.SATISFACTION_SCORE = '1' then 1 else 0 end) NUM
        from PLT_CALLLIST1000 a left join PLT_SERVICE_LIST1000 b on
        a.CONNECTIONID = b.CONNECTIONID
        WHERE a.CALL_RESULT = '1' AND b.TRANSFER_RESULT = '1'
        <if test="yearMonth != null">
            AND date_format(a.CALL_TIME,'%Y-%m') = #{yearMonth}
        </if>
        group by a.AGENT_FIRST_CODE
    </select>

    <select id="getTelephoneSta" resultType="java.util.Map">
        SELECT AGENT_FIRST_CODE empcode,
        sum(case when CALL_TYPE = '1' then 1 else 0 end) IN_ALL_TRANSFER ,
        sum(case when CALL_TYPE = '1' AND CALL_RESULT = '1' then 1 else 0 end) IN_CONNECT_TRANSFER ,
        sum(case when CALL_TYPE = '1' then TIMES else 0 end) IN_TIME ,
        sum(case when CALL_TYPE = '2' then 1 else 0 end) OUT_ALL_TRANSFER ,
        sum(case when CALL_TYPE = '2' AND CALL_RESULT = '1' then 1 else 0 end) OUT_CONNECT_TRANSFER ,
        sum(case when CALL_TYPE = '2' then TIMES else 0 end) OUT_TIME ,
        sum(TIMES) ALL_TIME ,
        AVG(TIMES) AVG_TIME ,
        MAX(TIMES) MAX_TIME
        FROM plt_calllist1000
        WHERE AGENT_FIRST_CODE <![CDATA[ <> ]]> ''
        <if test="search.empcode != null and search.empcode != ''">
            and AGENT_FIRST_CODE = #{search.empcode}
        </if>
        <if test="search.startTime != null and search.endTime != null">
            AND CALL_TIME BETWEEN #{search.startTime} AND #{search.endTime}
        </if>
        GROUP BY AGENT_FIRST_CODE
    </select>

    <select id="getSatisfactionStatis" resultType="java.util.Map">
        select
            empcode,
            ALLNUM,
            STATISFCTION_NUM,
            PROBLEM_NUM,
            NO_STATISFCTION_NUM,
            QUITE_SATISFIED_NUM,
            round(((QUITE_SATISFIED_NUM * 1 + STATISFCTION_NUM * 0.85 + NO_STATISFCTION_NUM * 0.75 + PROBLEM_NUM * 0.5) /
            nullif(ALLNUM, 0)) * 100, 2) score from
            (select a.AGENT_FIRST_CODE empcode,
            COUNT(1) ALLNUM ,
            sum(case when b.SATISFACTION_SCORE = '2' then 1 else 0 end) STATISFCTION_NUM ,
            sum(case when b.SATISFACTION_SCORE = '3' then 1 else 0 end) PROBLEM_NUM ,
            sum(case when b.SATISFACTION_SCORE = '4' then 1 else 0 end) NO_STATISFCTION_NUM,
            sum(case when b.SATISFACTION_SCORE = '1' then 1 else 0 end) QUITE_SATISFIED_NUM
        from PLT_CALLLIST1000 a
            left join PLT_SERVICE_LIST1000 b on a.CONNECTIONID = b.CONNECTIONID
        WHERE
            a.CALL_RESULT = '1'
            AND b.TRANSFER_RESULT = '1'
            AND b.SATISFACTION_SCORE IN ('1', '2', '3','4')
        <if test="search.startTime != null and search.endTime != null">
            AND a.CONNECT_TIME BETWEEN #{search.startTime} AND #{search.endTime}
        </if>
        <if test="search.empcode != null and search.empcode != ''">
            and a.AGENT_FIRST_CODE = #{search.empcode}
        </if>
        group by a.AGENT_FIRST_CODE) as query_result
        order by score desc
    </select>

    <select id="getUserScoreInfo" resultType="java.util.Map">
        SELECT k.favorableRate,k.rownum
        FROM
        (
        select
        m.EMPCODE,
        m.favorableRate,
        CASE
        WHEN @rowtotal = m.favorableRate THEN
        @rownum
        WHEN @rowtotal := m.favorableRate THEN
        @rownum :=@rownum + 1
        WHEN @rowtotal = 0 THEN
        @rownum :=@rownum + 1
        END AS rownum

        from(
        select
        T.EMPCODE,
        ROUND (((T.QUITE_SATISFIED_NUM + T.STATISFCTION_NUM) / T.ALLNUM) * 100,2) favorableRate
        from
        (
        SELECT
        a.AGENT_FIRST_CODE EMPCODE,
        COUNT(1) ALLNUM ,
        sum(case when b.SATISFACTION_SCORE = '2' then 1 else 0 end) STATISFCTION_NUM ,
        sum(case when b.SATISFACTION_SCORE = '1' then 1 else 0 end) QUITE_SATISFIED_NUM
        from PLT_CALLLIST1000 a
        left join PLT_SERVICE_LIST1000 b
        on a.CONNECTIONID = b.CONNECTIONID
        WHERE a.CALL_RESULT = '1'
        AND b.TRANSFER_RESULT = '1'
        AND b.SATISFACTION_SCORE IN ('1', '2', '3','4')
        <if test="start != null and end != null">
            AND a.CONNECT_TIME BETWEEN #{start} AND #{end}
        </if>
        group by a.AGENT_FIRST_CODE) T
        ORDER BY favorableRate desc ) M, (SELECT @rownum := 0 ,@rowtotal := NULL) r)k
        <if test="userName != null and userName != ''">
            where k.EMPCODE = #{userName}
        </if>
    </select>

    <select id="getSatisfactionStatisInfo" resultType="map">
        select a.AGENT_FIRST_CODE EMPCODE,
        b.*,
        case
        when SATISFACTION_SCORE = '1' then '非常满意'
        when SATISFACTION_SCORE = '2' then '满意'
        when SATISFACTION_SCORE = '3' then '一般'
        when SATISFACTION_SCORE = '4' then '不满意'
        end score
        from PLT_CALLLIST1000 a
        left join PLT_SERVICE_LIST1000 b
        on a.CONNECTIONID = b.CONNECTIONID
        WHERE a.CALL_RESULT = '1'
        AND b.TRANSFER_RESULT = '1'
        <choose>
            <when test="search.score == null">
                AND b.SATISFACTION_SCORE IN ('1', '2', '3','4')
            </when>
            <otherwise>
                and b.SATISFACTION_SCORE = #{search.score}
            </otherwise>
        </choose>

        <if test="search.startTime != null and search.endTime != null">
            AND a.CONNECT_TIME BETWEEN #{search.startTime} AND #{search.endTime}
        </if>
        <if test="search.empcode != null and search.empcode != ''">
            and a.AGENT_FIRST_CODE = #{search.empcode}
        </if>
    </select>

    <select id="getStateStatis" resultType="java.util.Map">
        SELECT CONVERT(EMPID, CHAR) EMPCODE,
        sum(case when state = '6' then TIMES else 0 end) CALL_TIME,
        sum(case when state = '5' then TIMES else 0 end) WAIT_CONNECT_TIME ,
        sum(case when state = '11' then TIMES else 0 end) DISPOSE_TIME ,
        sum(case when state = '29' then TIMES else 0 end) REST_TIME ,
        sum(case when state = '2' then TIMES else 0 end) BUSY_TIME ,
        sum(case when state = '1' then TIMES else 0 end) FREE_TIME ,
        sum(TIMES) WORK_ALL_TIME
        FROM plt_agentstate_list1000
        <where>
            <if test="search.startTime != null and search.endTime != null">
                START_TIME BETWEEN #{search.startTime} AND #{search.endTime}
            </if>
            <if test="search.empcode != null and search.empcode != ''">
                and CONVERT(EMPID, CHAR) = #{search.empcode}
            </if>
        </where>
        GROUP BY CONVERT(EMPID, CHAR)
    </select>
    <select id="getFlowNum" resultType="com.tofly.callcenter.custom.entity.FlowNum">
        select flow_Num, blockCall_Num
        from tf_ywpn_flowwarin_w
        where id = 1
    </select>

    <sql id="common_sql">
        (select count(*) from plt_calllist1000 where ${sql} and CALL_TIME between #{start} and #{end})
    </sql>

    <select id="incomingCallCount" resultType="map">
        select
        <include refid="common_sql">
            <property name="sql" value="CALL_TYPE = 1"/>
        </include>
        as totalInbound,
        <include refid="common_sql">
            <property name="sql" value="CALL_TYPE = 1 and CALL_RESULT = 1"/>
        </include>
        as inboundNum,
        <include refid="common_sql">
            <property name="sql" value="CALL_TYPE = 1 and CALL_RESULT between 21 and 29"/>
        </include>
        as inboundLossNum
        from DUAL
    </select>

    <select id="exhalationCount" resultType="map">
        select
        <include refid="common_sql">
            <property name="sql" value="CALL_TYPE = 2"/>
        </include>
        as totalOut,
        <include refid="common_sql">
            <property name="sql" value="CALL_TYPE = 2 and CALL_RESULT = 1"/>
        </include>
        as outNum,
        <include refid="common_sql">
            <property name="sql" value="CALL_TYPE = 2 and CALL_RESULT between 21 and 29"/>
        </include>
        as outLossNum
        from DUAL
    </select>

    <sql id="callCount">
        (select count(*) from plt_calllist1000 where ${sql} and date_format(CALL_TIME,'%Y-%m') = date_format(now(),'%Y-%m'))
    </sql>

    <select id="getCallCount" resultType="map">
        select
        <include refid="callCount">
            <property name="sql" value="empcode = #{userName} and CALL_TYPE = 1"/>
        </include>
        as inboundNumber,
        <include refid="callCount">
            <property name="sql" value="empcode = #{userName} and CALL_TYPE = 2"/>
        </include>
        as callSeveral
        from DUAL
    </select>

    <select id="missedCalls" resultType="map">
        select TIMES, TELEPHONE, CALL_TIME,CALL_TYPE
        from plt_calllist1000
        <where>
            <if test="dto.empcode != null">
                and AGENT_FIRST_CODE = #{dto.empcode}
            </if>
            <if test="dto.phoneNumber != null">
                and TELEPHONE like ${dto.phoneNumber}
            </if>
            <if test="dto.callResult != null">
                <choose>
                    <when test="dto.result == 0">
                        and call_result = #{dto.callResult}
                    </when>
                    <otherwise>
                        and call_result != #{dto.callResult}
                    </otherwise>
                </choose>
            </if>

            order by CALL_TIME desc limit 100
        </where>
    </select>

    <select id="missedCall" resultType="map">
        SELECT
        TELEPHONE, '呼入' call_type_name, CALL_TIME,
        TIMES,AGENT_FIRST_CODE EMPCODE,
        HANGUP_TIME,
        case when TRANSFER_RESULT = 1 then '接通'
        when TRANSFER_RESULT = 2 then '坐席忙'
        when TRANSFER_RESULT = 3 then '无应答'
        when TRANSFER_RESULT = 4 then '坐席拒绝'
        when TRANSFER_RESULT = 5 then '坐席未登录'
        when TRANSFER_RESULT = 6 then '振铃中挂机'
        when TRANSFER_RESULT = 7 then '转接中挂机'
        when TRANSFER_RESULT = 8 then '未转坐席挂机'
        when TRANSFER_RESULT = 9 then '其他错误'
        END result_name
        FROM
        plt_calllist1000
        WHERE
        CALL_TYPE = 1
        AND TRANSFER_RESULT in (3,4,5,6)
        <if test="tel != null and tel != ''">
            and TELEPHONE LIKE CONCAT('%',#{tel},'%')
        </if>
        <choose>
            <when test="emp != null and emp != ''">
                and AGENT_FIRST_CODE like concat('%',#{emp},'%')
            </when>
            <otherwise>
                and AGENT_FIRST_CODE = -1
            </otherwise>
        </choose>
        ORDER BY CALL_TIME desc
    </select>

    <select id="missCallInfo" resultType="map">
        SELECT CONNECTIONID,
        (case CALL_TYPE when 1 then '呼入' when 2 then '呼出' when 3 then '内部呼叫' end) call_type_name,
        TELEPHONE,
        SYSPHONE,
        date_format(CALL_TIME,'%Y-%m-%d %H:%I:%s') CALL_TIME,
        TIMES,
        date_format(CONNECT_TIME,'%Y-%m-%d %H:%I:%s') CONNECT_TIME ,
        date_format(HANGUP_TIME,'%Y-%m-%d %H:%I:%s') HANGUP_TIME,
        TIMES,
        (case CALL_RESULT
        when 0 then '未呼叫'
        when 1 then '成功'
        when 2 then '无人接听'
        when 3 then '忙'
        when 4 then '空号'
        when 5 then '关机'
        when 6 then '停机'
        when 7 then '线路故障'
        when 8 then '网络错误'
        when 9 then '呼叫拒收'
        when 10 then '号码错误'
        when 11 then '硬件呼叫返回错误'
        when 12 then '其他情况'
        when 13 then '呼叫清除'
        when 21 then '坐席全忙'
        when 22 then '坐席无应答'
        when 23 then '坐席拒绝'
        when 24 then '坐席未登录'
        when 25 then '用户挂机'
        end) CALL_RESULT_NAME,
        (case TRANSFER_RESULT
        when 1 then '接通'
        when 2 then '坐席忙'
        when 3 then '无应答'
        when 4 then '坐席拒绝'
        when 5 then '坐席未登录'
        when 6 then '振铃中挂机'
        when 7 then '转接中挂机'
        when 8 then '未转坐席挂机'
        when 9 then '其他错误'
        END) result_name,
        PRESS_KEY,
        AGENT_FIRST_CODE EMPCODE,
        PRESS_KEY_NAME,
        date_format(AGENT_FIRSTANSWER_TIME, '%Y-%m-%d %H:%I:%s') PRESS_KEY_TIME,
        (case HANGUPFLAG when 1 then '坐席挂机' when 2 then '客户挂机' end) flag,
        timestampdiff(SECOND, CALL_TIME, AGENT_FIRSTANSWER_TIME) wait_time
        FROM plt_calllist1000
        <where>
            <if test="dto.start != null and dto.start != '' and dto.end != null and dto.end != ''">
                and date_format(CALL_TIME, '%Y-%m-%d') between #{dto.start} and #{dto.end}
            </if>
            <if test="dto.number != null and dto.number != ''">
                and CONNECTIONID like CONCAT('%',#{dto.number},'%')
            </if>
            <if test="dto.userPhone != null and dto.userPhone != ''">
                and TELEPHONE like CONCAT('%',#{dto.userPhone},'%')
            </if>
            <if test="dto.sysPhone != null and dto.sysPhone != ''">
                and SYSPHONE like CONCAT('%',#{dto.sysPhone},'%')
            </if>
            <if test="dto.callType != null and dto.callType != ''">
                and CALL_TYPE in (${dto.callType})
            </if>
            <if test="dto.callResult != null and dto.callResult != ''">
                and CALL_RESULT in (${dto.callResult})
            </if>
            <if test="dto.turnResult != null and dto.turnResult != ''">
                and TRANSFER_RESULT in (${dto.turnResult})
            </if>
            <if test="dto.intervalTimeStart != null and dto.intervalTimeEnd != null">
                and TIMES between #{dto.intervalTimeStart} and #{dto.intervalTimeEnd}
            </if>
        </where>
        ORDER BY CALL_TIME
    </select>

</mapper>
