<?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.xrtygis.callCenterMapper.PltCalllist1000Mapper">

    <resultMap id="pltCalllist1000Map" type="com.tofly.xrtygis.entity.callCenter.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="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 EMPCODE code,
        ifnull(count(*),0) sumNum
        from PLT_CALLLIST1000
        where CALL_RESULT = 1
        <if test="crew != null and crew != ''">
            and EMPCODE = #{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 EMPCODE is not null
        and EMPCODE != ''
        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'
    </select>

    <select id="getNotCallInCount" resultType="long">
        SELECT COUNT(*)
        FROM plt_calllist1000
        WHERE date_format(CALL_TIME, '%Y-%m-%d') = curdate()
          AND CALL_TYPE != '1'
    </select>

    <select id="getSatisfactionCount" resultType="java.util.Map">
        select a.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.EMPCODE
    </select>

    <select id="getTelephoneSta" resultType="java.util.Map">
        SELECT 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 EMPCODE <![CDATA[ <> ]]> ''
        <if test="search.empcode != null and search.empcode != ''">
            and EMPCODE = #{search.empcode}
        </if>
        <if test="search.startTime != null and search.endTime != null">
            AND CALL_TIME BETWEEN #{search.startTime} AND #{search.endTime}
        </if>
        GROUP BY EMPCODE
    </select>

    <select id="getSatisfactionStatis" resultType="java.util.Map">
        select a.EMPCODE ,
        (select name from plt_employee where code = a.EMPCODE) empcode_name,
        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.EMPCODE = #{search.empcode}
        </if>
        group by a.EMPCODE
    </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.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.EMPCODE) 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.EMPCODE ,
        (select name from plt_employee where code = a.EMPCODE) empcode_name,
        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.EMPCODE = #{search.empcode}
        </if>
    </select>

    <select id="getStateStatis" resultType="java.util.Map">
        SELECT EMPCODE ,
        (select name from plt_employee where code = EMPCODE) empcode_name,
        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_agent_state1000
        <where>
            <if test="search.startTime != null and search.endTime != null">
                STARTTIME BETWEEN #{search.startTime} AND #{search.endTime}
            </if>
            <if test="search.empcode != null and search.empcode != ''">
                and EMPCODE = #{search.empcode}
            </if>
        </where>
        GROUP BY EMPCODE
    </select>
    <select id="getFlowNum" resultType="com.tofly.xrtygis.custom.entity.callCenter.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
        IFNULL(<include refid="common_sql">
        <property name="sql" value="CALL_TYPE = 1"/>
    </include>
        ,0) as totalInbound,
        IFNULL(<include refid="common_sql">
            <property name="sql" value="CALL_TYPE = 1 and CALL_RESULT = 1"/>
        </include>
        ,0)
        as inboundNum,
        IFNULL(
        <include refid="common_sql">
            <property name="sql" value="CALL_TYPE = 1 and CALL_RESULT between 21 and 29"/>
        </include>
        ,0)
        as inboundLossNum
        from DUAL
    </select>

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

    <select id="getCallCount" resultType="map">
        select
        <include refid="common_sql">
            <property name="sql" value="empcode = #{userName} and CALL_TYPE = 1"/>
        </include>
        as inboundNumber,
        <include refid="common_sql">
            <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 EMPCODE = #{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, TIMES, if(CALL_TYPE = 1,'呼入','呼出') call_type_name,CALL_TIME
        FROM plt_service_list1000
        where CALL_RESULT = 1
        and TRANSFER_RESULT IN (2, 3, 4, 5, 6)
        and EMPCODE = #{emp}
        <if test="tel != null and tel != ''">
            and TELEPHONE LIKE CONCAT('%',#{tel},'%')
        </if>
        ORDER BY CALL_TIME desc
    </select>

    <select id="viewNewHotLineCountByMonth" resultType="map">
        select  date_format(CALL_TIME,'%Y-%m') time,
                 count( * ) num
        from plt_calllist1000
        WHERE
        CALL_TYPE = 1
        <if test="startMonth != null and endMonth != null">
            AND date_format(CALL_TIME,'%Y-%m') between #{startMonth} and #{endMonth}
        </if>
        GROUP BY date_format(CALL_TIME,'%Y-%m')
        ORDER BY time DESC
    </select>

    <select id="viewNewHotLineNum" resultType="Long">
        select  count( * ) num
        from plt_calllist1000
        WHERE
            1=1
        <if test="callType != null">
            AND CALL_TYPE = #{callType}
        </if>
        <if test="censusMonth != null and censusMonth.trim() != ''">
            AND date_format(CALL_TIME,'%Y-%m') = #{censusMonth}
        </if>
        <if test="censusYear != null and censusYear.trim() != ''">
            AND date_format(CALL_TIME,'%Y') = #{censusYear}
        </if>
        <if test="connectIds != null">
            AND CONNECTIONID in
            <foreach item="item" index="index" collection="connectIds" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="getConnectIds" resultType="String">
        select  CONNECTIONID
        from plt_service_list1000
        WHERE
        1=1
        <if test="statusList != null">
            AND SATISFACTION_SCORE in
            <foreach item="item" index="index" collection="statusList" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
    </select>

</mapper>
