<?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.mapper.PfrecordMapper">

    <resultMap id="pfrecordMap" type="com.tofly.xrtygis.entity.Pfrecord">
        <id property="id" column="ID"/>
        <result property="xxsj" column="XXSJ"/>
        <result property="xxqr" column="XXQR"/>
        <result property="dhjq" column="DHJQ"/>
        <result property="zlk" column="ZLK"/>
        <result property="lcgf" column="LCGF"/>
        <result property="isreslut" column="ISRESLUT"/>
        <result property="gdType" column="GD_TYPE"/>
        <result property="sysphone" column="SYSPHONE"/>
        <result property="telephone" column="TELEPHONE"/>
        <result property="callType" column="CALL_TYPE"/>
        <result property="callTime" column="CALL_TIME"/>
        <result property="connectTime" column="CONNECT_TIME"/>
        <result property="callResult" column="CALL_RESULT"/>
        <result property="pfType" column="PF_TYPE"/>
        <result property="pfMs" column="PF_MS"/>
        <result property="pfFz" column="PF_FZ"/>
        <result property="pfZxry" column="PF_ZXRY"/>
        <result property="pfLyid" column="PF_LYID"/>
        <result property="pfGdbh" column="PF_GDBH"/>
        <result property="pfry" column="PFRY"/>
        <result property="pfsj" column="PFSJ"/>
        <result property="fwzc" column="FWZC"/>
        <result property="zzkg" column="ZZKG"/>
        <result property="fwyy" column="FWYY"/>
        <result property="yyyd" column="YYYD"/>
        <result property="jdwz" column="JDWZ"/>
        <result property="jdzq" column="JDZQ"/>
        <result property="qtlj" column="QTLJ"/>
        <result property="qualityReslut" column="QUALITY_RESLUT"/>
        <association property="gdTypeName" column="GD_TYPE"
                     select="com.tofly.xrtygis.mapper.GdreturnMapper.getGdlxById"/>
        <association property="pfryName" column="PFRY"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
        <association property="qualityReslutName" column="QUALITY_RESLUT"
                     select="getQualityReslut"/>
        <association property="isreslutName" column="ISRESLUT"
                     select="getIsreslutName"/>
        <association property="callReasultName" column="CALL_RESULT"
                     select="getCallResultName"/>
        <association property="callTypeName" column="CALL_TYPE"
                     select="getCallTypeName"/>
    </resultMap>
    <update id="updateGdQualityState">
        UPDATE TF_YWPN_GDINFO_W SET IS_QUALITY_FLAG = '1' WHERE GDBH = #{gdbh}
    </update>

    <select id="getCallTypeName" resultType="string">
        select decode(#{callType} , '1','呼入','2','呼出')
        from dual
    </select>
    <select id="getIsreslutName" resultType="string">
        select decode(#{reslut} , '0','不合格','1','合格')
        from dual
    </select>
    <select id="getQualityReslut" resultType="string">
        select decode(#{reslut} , '0','不满意','1','一般','2','满意','3','非常满意')
        from dual
    </select>
    <resultMap id="gdQualityStatisRelMap" type="com.tofly.xrtygis.custom.entity.gs.GdQualityStatis">
        <result property="gdlx" column="GDLX"/>
        <result property="qualityGdlx" column="QUALITYGDLX"/>
        <result property="shouldQualityNum" column="SHOULD_QUALITY"/>
        <result property="realityQualityNum" column="REALITY_QUALITY"/>
        <result property="veryMyNum" column="VERYMYNUM"/>
        <result property="myNum" column="MYNUM"/>
        <result property="commonNum" column="COMMONNUM"/>
        <result property="noMyNum" column="NOMYNUM"/>
        <association property="gdlxName" column="GDLX"
                     select="com.tofly.xrtygis.mapper.GdreturnMapper.getCodeById"/>
    </resultMap>

    <select id="getGdQualityNum" resultMap="gdQualityStatisRelMap">
        SELECT GDLX, COUNT(1) SHOULD_QUALITY
        FROM TF_YWPN_GDINFO_W
        WHERE IS_RXLX = '1' AND LCJD = 45
        GROUP BY GDLX
    </select>
    <select id="getQualityNum" resultMap="gdQualityStatisRelMap">
        SELECT GD_TYPE QUALITYGDLX ,
               NVL(COUNT(1) , 0) REALITY_QUALITY ,
               sum(case when QUALITY_RESLUT = '3' then 1 else 0 end) VERYMYNUM,
               sum(case when QUALITY_RESLUT = '2' then 1 else 0 end) MYNUM,
               sum(case when QUALITY_RESLUT = '1' then 1 else 0 end) COMMONNUM,
               sum(case when QUALITY_RESLUT = '0' then 1 else 0 end) NOMYNUM
        FROM TF_YWPN_PFRECORD_W
        WHERE PF_TYPE = '1'
        GROUP BY GD_TYPE
    </select>
    <resultMap id="recordNumMap" type="com.tofly.xrtygis.custom.entity.gs.RecordStatis">
        <result property="zxry" column="ZXRY"/>
        <result property="average" column="AVERAGE"/>
        <result property="qualityRecordNum" column="RECORD_NUM"/>
        <result property="veryMyNum" column="VERYMYNUM"/>
        <result property="myNum" column="MYNUM"/>
        <result property="commonNum" column="COMMONNUM"/>
        <result property="noMyNum" column="NOMYNUM"/>
    </resultMap>
    <select id="getRecordNum" resultMap="recordNumMap">
        SELECT PF_ZXRY ZXRY ,  ROUND(AVG(PF_FZ), 2) AVERAGE ,
               COUNT(1) RECORD_NUM ,
               sum(case when QUALITY_RESLUT = '3' then 1 else 0 end) VERYMYNUM,
               sum(case when QUALITY_RESLUT = '2' then 1 else 0 end) MYNUM,
               sum(case when QUALITY_RESLUT = '1' then 1 else 0 end) COMMONNUM,
               sum(case when QUALITY_RESLUT = '0' then 1 else 0 end) NOMYNUM
        FROM TF_YWPN_PFRECORD_W
        WHERE PF_TYPE = '2'
        GROUP BY PF_ZXRY
    </select>

    <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>
</mapper>
