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

    <resultMap id="pfrecordMapper" 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="com.tofly.xrtygis.mapper.PfrecordMapper.getQualityReslut"/>
        <association property="isreslutName" column="ISRESLUT"
                     select="com.tofly.xrtygis.mapper.PfrecordMapper.getIsreslutName"/>
        <association property="callReasultName" column="CALL_RESULT"
                     select="com.tofly.xrtygis.mapper.PfrecordMapper.getCallResultName"/>
        <association property="callTypeName" column="CALL_TYPE"
                     select="com.tofly.xrtygis.mapper.PfrecordMapper.getCallTypeName"/>
    </resultMap>

    <select id="getClientInfo" resultType="map">
        select YHBH, YHXM, YHDZ, LXDH
        from TF_YWPN_QFCBQFMX_W
        <where>
            <if test="dto != null">
                YHBH || YHXM || YHDZ || LXDH like '%' || #{dto} || '%'
            </if>
        </where>
    </select>

    <sql id="dept_info">
        (select m.name
         from TF_SMPN_DEPARTMENT_X m
         where m.id = department_id)
    </sql>

    <select id="getEmployeeInfo" resultType="map">
        select id,
        username,
        phone,
        real_name,
        <include refid="dept_info"/>
        as dept_name
        from TF_SMPN_USER_X
        <where>
            <if test="dto != null">
                username || phone || real_name ||
                <include refid="dept_info"/>
                like '%' || #{dto} || '%'
            </if>
        </where>
    </select>

    <select id="callShiftQuery" resultType="map" fetchSize="10000">
        select id,
        username,
        phone,
        real_name,
        <include refid="dept_info"/>
        as dept_name
        from TF_SMPN_USER_X
        <where>
            <if test="dept != null">
                department_id = #{dept}
            </if>
            <if test="dto != null">
                and phone || real_name like '%' || #{dto} || '%'
            </if>
        </where>
    </select>

    <sql id="count_zf">
        (nvl(sum(FWZC),0) + nvl(sum(ZZKG),0) + nvl(sum(FWYY),0) + nvl(sum(YYYD),0) +
            nvl(sum(JDWZ),0) + nvl(sum(JDZQ),0) +
            nvl(sum(QTLJ),0) + nvl(sum(XXSJ),0) + nvl(sum(XXQR),0) +
            nvl(sum(DHJQ),0) + nvl(sum(LCGF),0))
    </sql>

    <!--    <sql id="spotCheck">
            (select count(*) from TF_YWPN_PFRECORD_W where PF_TYPE = 2 and PF_ZXRY = p.PF_ZXRY
            <choose>
                <when test="start != null and end != null">
                    and to_char(CALL_TIME,'yyyy-MM') between #{start} and #{end}
                </when>
                <otherwise>
                    and to_char(CALL_TIME,'yyyy-MM') = to_char(sysdate,'yyyy-MM')
                </otherwise>
            </choose>
            <if test="crew != null">
                and pf_zxry = #{crew}
            </if>
            )
        </sql>-->

    <select id="getRecordGrade" resultType="map">
        select cc_num,
        dept_id,
        user_id,
        sumNum,
        (select name from TF_SMPN_DEPARTMENT_X where id = dept_id) dept_name,
        zf, PF_ZXRY, khgx, ywzs, gtnl, zxnl, avg,pf_zxry_name
        from (select
        count(*) cc_num,
        (select DEPARTMENT_ID from TF_SMPN_USER_X where USERNAME = PF_ZXRY) dept_id,
        (<include refid="count_zf"/>)
        as zf,
        (select real_name from TF_SMPN_USER_X where username = PF_ZXRY) pf_zxry_name,
        PF_ZXRY,
        (select id from TF_SMPN_USER_X where username = PF_ZXRY) user_id,
        sum(FWZC) + sum(ZZKG) + sum(FWYY) + sum(YYYD) khgx,
        sum(JDWZ) + sum(JDZQ) ywzs,
        sum(QTLJ) + sum(XXSJ) + sum(XXQR) gtnl,
        sum(DHJQ) + sum(LCGF) zxnl,
        round(
        sum(PF_FZ) / nullif(count(*),0),2) avg,
        count(*) sumNum
        from TF_YWPN_PFRECORD_W p
        where PF_TYPE = 2
        <choose>
            <when test="start != null and end != null">
                and to_char(CALL_TIME,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and to_char(CALL_TIME,'yyyy-MM-dd') = to_char(sysdate,'yyyy-MM-dd')
            </otherwise>
        </choose>

        group by pf_zxry
        order by pf_zxry)
        <where>
            <if test="dept != null and dept > 0">
                dept_id = #{dept}
            </if>
            <if test="crew != null">
                and user_id = #{crew}
            </if>
        </where>
    </select>

    <select id="getRecordInfo" resultMap="pfrecordMapper">
        select id, pf_type, pf_ms, pf_fz, pf_zxry, pf_lyid, pf_gdbh, pfry, pfsj, fwzc, zzkg, fwyy, yyyd, jdwz, jdzq,
        qtlj, xxsj, xxqr, dhjq, zlk, lcgf, isreslut, gd_type, sysphone, telephone, call_type, call_time, connect_time,
        call_result, quality_reslut,
        (select real_name from TF_SMPN_USER_X where username = PF_ZXRY) pf_zxry_name
        from TF_YWPN_PFRECORD_W
        where PF_TYPE = '2'
        <choose>
            <when test="start != null and end != null">
                and to_char(CALL_TIME,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and to_char(CALL_TIME,'yyyy-MM-dd') = to_char(sysdate,'yyyy-MM-dd')
            </otherwise>
        </choose>
        <if test="crew != null">
            and pf_zxry = #{crew}
        </if>
        order by call_time
    </select>

    <sql id="gdlxName">
        (SELECT GDLX_MC
         FROM TF_YWPN_GDGLTYPEBM_W
         WHERE ID = GDLX)
    </sql>

    <sql id="jd_count">
        (select count(*) from TF_YWPN_GDINFO_W
        where GDLX = i.gdlx
        <choose>
            <when test="start != null and end != null">
                and to_char(djsj,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        <if test="gdlx != null">
            and gdlx = #{gdlx}
        </if>
        <if test="djry != null">
            and djry = #{djry}
        </if>
        <if test="gdzt != null">
            and GDGZLX = #{gdzt}
        </if>
        )
    </sql>

    <sql id="cl_count">
        (select count(*) from TF_YWPN_GDINFO_W where CZZT = 3 and GDLX = i.gdlx
        <choose>
            <when test="start != null and end != null">
                and to_char(djsj,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        <if test="gdlx != null">
            and gdlx = #{gdlx}
        </if>
        <if test="djry != null">
            and djry = #{djry}
        </if>
        <if test="gdzt != null">
            and GDGZLX = #{gdzt}
        </if>
        )
    </sql>

    <sql id="wcl_count">
        (select count(*) from TF_YWPN_GDINFO_W where CZZT != 3 and GDLX = i.gdlx
        <choose>
            <when test="start != null and end != null">
                and to_char(djsj,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        <if test="gdlx != null">
            and gdlx = #{gdlx}
        </if>
        <if test="djry != null">
            and djry = #{djry}
        </if>
        <if test="gdzt != null">
            and GDGZLX = #{gdzt}
        </if>
        )
    </sql>

    <sql id="zz_count">
        (select count(*) from TF_YWPN_GDINFO_W where GDZZZT = 1 and GDLX = i.gdlx
        <choose>
            <when test="start != null and end != null">
                and to_char(djsj,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        <if test="gdlx != null">
            and gdlx = #{gdlx}
        </if>
        <if test="djry != null">
            and djry = #{djry}
        </if>
        <if test="gdzt != null">
            and GDGZLX = #{gdzt}
        </if>
        )
    </sql>

    <sql id="cs_count">
        (select count(*)
        from TF_YWPN_GDINFO_W
        where GDCS_FLAG = 1 and GDLX = i.gdlx
        <choose>
            <when test="start != null and end != null">
                and to_char(djsj,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        <if test="gdlx != null">
            and gdlx = #{gdlx}
        </if>
        <if test="djry != null">
            and djry = #{djry}
        </if>
        <if test="gdzt != null">
            and GDGZLX = #{gdzt}
        </if>
        )
    </sql>

    <sql id="hf_count">
        (select count(*)
        from TF_YWPN_GDINFO_W n
        where n.GDHF_CS > 0
        and n.GDLX = i.GDLX
        <choose>
            <when test="start != null and end != null">
                and to_char(DJSJ,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        )
    </sql>


    <sql id="jd_count_rx">
        (select count(*) from TF_YWPN_GDINFO_W
        where GDLX = i.gdlx and IS_RXLX = '1'
        <choose>
            <when test="start != null and end != null">
                and to_char(djsj,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        <if test="gdlx != null">
            and gdlx = #{gdlx}
        </if>
        <if test="djry != null">
            and djry = #{djry}
        </if>
        <if test="gdzt != null">
            and GDGZLX = #{gdzt}
        </if>
        )
    </sql>

    <sql id="cl_count_rx">
        (select count(*) from TF_YWPN_GDINFO_W where FINISH_TIME is not null and GDLX = i.gdlx and IS_RXLX = '1'
        <choose>
            <when test="start != null and end != null">
                and to_char(djsj,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        <if test="gdlx != null">
            and gdlx = #{gdlx}
        </if>
        <if test="djry != null">
            and djry = #{djry}
        </if>
        <if test="gdzt != null">
            and GDGZLX = #{gdzt}
        </if>
        )
    </sql>

    <sql id="wcl_count_rx">
        (select count(*) from TF_YWPN_GDINFO_W where FINISH_TIME is null and GDLX = i.gdlx and IS_RXLX = '1'
        <choose>
            <when test="start != null and end != null">
                and to_char(djsj,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        <if test="gdlx != null">
            and gdlx = #{gdlx}
        </if>
        <if test="djry != null">
            and djry = #{djry}
        </if>
        <if test="gdzt != null">
            and GDGZLX = #{gdzt}
        </if>
        )
    </sql>

    <sql id="zz_count_rx">
        (select count(*) from TF_YWPN_GDINFO_W where GDZZZT = 1 and GDLX = i.gdlx and IS_RXLX = '1'
        <choose>
            <when test="start != null and end != null">
                and to_char(djsj,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        <if test="gdlx != null">
            and gdlx = #{gdlx}
        </if>
        <if test="djry != null">
            and djry = #{djry}
        </if>
        <if test="gdzt != null">
            and GDGZLX = #{gdzt}
        </if>
        )
    </sql>

    <sql id="cs_count_rx">
        (select count(*)
        from TF_YWPN_GDINFO_W
        where GDCS_FLAG = '1' and GDLX = i.gdlx and IS_RXLX = '1'
        <choose>
            <when test="start != null and end != null">
                and to_char(djsj,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        <if test="gdlx != null">
            and gdlx = #{gdlx}
        </if>
        <if test="djry != null">
            and djry = #{djry}
        </if>
        <if test="gdzt != null">
            and GDGZLX = #{gdzt}
        </if>
        )
    </sql>

    <sql id="hf_count_rx">
        (select count(*)
        from TF_YWPN_GDINFO_W n,TF_YWPN_GDRETURN_W r
        where n.GDHF_CS > 0
        and n.GDLX = i.GDLX
        and n.IS_RXLX = '1'
        and n.GDBH = r.GDBH
        and RETURN_RESLUT is not null

        <choose>
            <when test="start != null and end != null">
                and to_char(DJSJ,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        )
    </sql>

    <select id="workOrderStatistics" resultType="map">
        select
        <include refid="gdlxName"/>
        as gdlxName,
        <include refid="jd_count_rx"/>
        as jd_num,
        <include refid="jd_count_rx"/>
        as pd_num,
        <include refid="cl_count_rx"/>
        as cl_num,
        <include refid="wcl_count_rx"/>
        as wcl_num,
        <include refid="zz_count_rx"/>
        as zz_num,
        <include refid="cs_count_rx"/>
        as cs_num,
        <include refid="hf_count_rx"/>
        as hf_num,
        round((
        <include refid="zz_count_rx"/>
        / nullif(<include refid="jd_count_rx"/>,0) * 100),2)
        as rate,
        gdlx
        from TF_YWPN_GDINFO_W i
        where IS_RXLX = '1'
        <choose>
            <when test="start != null and end != null">
                and to_char(djsj,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        <if test="gdlx != null">
            and gdlx = #{gdlx}
        </if>
        <if test="djry != null">
            and djry = #{djry}
        </if>
        <if test="gdzt != null">
            and GDGZLX = #{gdzt}
        </if>
        group by gdlx
    </select>

    <sql id="common_sql">
        id,
        <include refid="gdlxName"/>
        as gdlx_name,
        gdlx,
        gdbh,
        gdly,
        gdxf1,
        gdxf2,
        gdms,
        decode(jjcd,0,'一般',1,'重要',2,'非常急') jjcd,
        lcid,
        ldhm,
        (select name from TF_SMPN_DEPARTMENT_X d where d.ID = slbm) slbm_name,
        slbm,
        (select REAL_NAME from TF_SMPN_USER_X u where SLRY = u.ID) slry,
        jdsx,
        wcsx,
        (select REAL_NAME from TF_SMPN_USER_X u where DJRY = u.ID) djry,
        djsj,
        khbh,
        khxm,
        khdz,
        lxdh,
        decode(jdzt,'0','未接单','1','接单') jdzt ,
        decode(gddj_zt,0,'本环节',1,'转部门',2,'驳回',3,'终止') gddj_zt,
        (select cname from TF_YWPN_SYSCHILDCODE_D where ID = lcjd) lcjd,
        rz_fileid,
        rzyj,
        (select name from TF_SMPN_DEPARTMENT_X d where d.ID = rzbm) rzbm_name,
        rzbm,
        rzsx,
        rzsj,
        (select REAL_NAME from TF_SMPN_USER_X u where rzry = u.ID)rzry,
        decode(lczt,0,'本环节',1,'转部门',2,'驳回',3,'终止')lczt,
        shyj,
        (select name from TF_SMPN_DEPARTMENT_X d where d.ID = shbm) shbm_name,
        shbm,
        (select REAL_NAME from TF_SMPN_USER_X u where shry = u.ID) shry,
        shsj,
        shsx,
        decode(shzt,0,'本环节',1,'转部门',2,'驳回',3,'终止')shzt,
        zjyj,
        (select name from TF_SMPN_DEPARTMENT_X d where d.ID = zjbm) zjbm_name,
        zjbm,
        (select REAL_NAME from TF_SMPN_USER_X u where zjry = u.ID) zjry,
        zjsj,
        zjsx,
        decode(zjzt,0,'本环节',1,'转部门',2,'驳回',3,'终止') zjzt,
        czsx,
        czyj,
        (select name from TF_SMPN_DEPARTMENT_X d where d.ID = czbm) czbm_name,
        czbm,
        (select REAL_NAME from TF_SMPN_USER_X u where czry = u.ID) czry,
        czsj,
        decode(czzt,0,'本环节',1,'转部门',2,'驳回',3,'终止') czzt,
        gdhf_cs,
        (select REAL_NAME from TF_SMPN_USER_X u where zjczry = u.ID) zjczry,
        gddj_fssj,
        rzcz_fssj,
        resh_fssj,
        rzzj_fssj,
        jgcz_fssj,
        decode(rzcz_cszt,'0','未超时','1','超时') rzcz_cszt,
        decode(resh_cszt,'0','未超时','1','超时')resh_cszt,
        decode(rzzj_cszt,'0','未超时','1','超时') rzzj_cszt,
        decode(jgcz_cszt,'0','未超时','1','超时') jgcz_cszt,
        rzcz_cssj,
        resh_cssj,
        rzzj_cssj,
        jgcz_cssj,
        lczzyy,
        process_deposit,
        (select REAL_NAME from TF_SMPN_USER_X u where rzslry = u.ID) rzslry,
        (select REAL_NAME from TF_SMPN_USER_X u where shslry = u.ID) shslry,
        decode(jd_cszt,'0','未超时','1','超时') jd_cszt,
        decode(bhzt,'0','无驳回','1','有驳回') bhzt,
        decode(zpzt,'0','无转派','1','有转派') zpzt,
        (select REAL_NAME from TF_SMPN_USER_X u where jgczry = u.ID) jgczry,
        decode(is_rxlx,'0','不是','1','是') is_rxlx,
        decode(is_quality_flag,'0','未质检','1','已质检') is_quality_flag,
        decode(hang_up,'0','未挂起','1','已挂起') hang_up,
        decode(is_watermeter_status,'0','未完成','1','已完成') is_watermeter_status,
        gddj_fileid,
        (select REAL_NAME from TF_SMPN_USER_X u where zzry = u.ID) zzry,
        zzsj,
        (SELECT wm_concat(GDLX_MC)
        FROM (
        SELECT GDLX_MC
        FROM TF_YWPN_GDGLTYPEBM_W START WITH id = 10
        CONNECT BY PRIOR GDLX_ID=ID
        ORDER BY id)) gdgzlx,
        gdgzyy,
        (select REAL_NAME from TF_SMPN_USER_X u where gzszry = u.ID) gzszry,
        (select name from TF_SMPN_DEPARTMENT_X d where d.ID = sqbm) sqbm_name,
        sqbm,
        (select REAL_NAME from TF_SMPN_USER_X u where sqry = u.ID) sqry,
        decode(gdzzzt,'0','正常','1','终止') gdzzzt,
        gdsx,
        (select ALL_LCJD FROM TF_YWPN_GDYWCONFIG_W WHERE ID = lcid) all_lcjd,
        (select TRANSFER_OUT_TYPE FROM TF_YWPN_GDYWCONFIG_W WHERE ID = lcid) transfer_out_type,
        decode(gdcs_flag,'0','未超时','1','超时') gdcs_flag
    </sql>

    <select id="workOrderInfo" resultType="map">
        select
        <include refid="common_sql"/>
        from TF_YWPN_GDINFO_W
        where GDLX = #{gdlx} and is_rxlx = '1'
        <choose>
            <when test="start != null and end != null">
                and to_char(djsj,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        <choose>
            <when test="type == 1">
                and czzt = 3
            </when>
            <when test="type == 2">
                and GDZZZT = 1
            </when>
            <when test="type == 3">
                and GDCS_FLAG = 1
            </when>
            <when test="type == 4">
                and czzt != 3
            </when>
            <otherwise>
            </otherwise>
        </choose>
        <if test="djry != null">
            and djry = #{djry}
        </if>
        <if test="gdzt != null">
            and GDGZLX = #{gdzt}
        </if>
    </select>

    <select id="payAReturnVisitInfo" resultType="map">
        select r.ID,
        i.gdbh,
        (SELECT GDLX_MC
        FROM TF_YWPN_GDGLTYPEBM_W
        WHERE ID = GDLX) as gdlx,
        (SELECT GDLX_MC
        FROM TF_YWPN_GDGLTYPEBM_W
        WHERE ID = GDXF1)GDXF1,
        (SELECT GDLX_MC
        FROM TF_YWPN_GDGLTYPEBM_W
        WHERE ID = GDXF2)GDXF2,
        decode(jjcd, 0, '一般', 1, '重要', 2, '非常急') jjcd,
        GDMS,
        (select REAL_NAME from TF_SMPN_USER_X u where DJRY = u.ID) djry,
        djsj,
        (select REAL_NAME from TF_SMPN_USER_X u where SLRY = u.ID) slry,
        (select REAL_NAME from TF_SMPN_USER_X u where return_user = u.ID) return_user,
        return_time,
        return_info,
        decode(return_reslut, '0', '不满意', '1', '一般', '2', '满意', '3', '非常满意') return_reslut,
        fyr,
        phone,
        i.address
        from TF_YWPN_GDINFO_W i
        left join TF_YWPN_GDRETURN_W r on r.GDBH = i.GDBH
        where GDLX = #{gdlx} and i.is_rxlx = '1' and gdhf_cs > 0
        <choose>
            <when test="start != null and end != null">
                and to_char(djsj,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        <if test="pass != null">
            and return_reslut = #{pass}
        </if>
        <if test="djry != null">
            and djry = #{djry}
        </if>
        <if test="gdzt != null">
            and GDGZLX = #{gdzt}
        </if>
    </select>

    <select id="payAReturnVisitInfoTwo" resultType="map">
        select r.ID,
        i.gdbh,
        (SELECT GDLX_MC
        FROM TF_YWPN_GDGLTYPEBM_W
        WHERE ID = GDLX) as gdlx,
        (SELECT GDLX_MC
        FROM TF_YWPN_GDGLTYPEBM_W
        WHERE ID = GDXF1)GDXF1,
        (SELECT GDLX_MC
        FROM TF_YWPN_GDGLTYPEBM_W
        WHERE ID = GDXF2)GDXF2,
        decode(jjcd, 0, '一般', 1, '重要', 2, '非常急') jjcd,
        GDMS,
        (select REAL_NAME from TF_SMPN_USER_X u where DJRY = u.ID) djry,
        djsj,
        (select REAL_NAME from TF_SMPN_USER_X u where SLRY = u.ID) slry,
        (select REAL_NAME from TF_SMPN_USER_X u where return_user = u.ID) return_user,
        return_time,
        return_info,
        decode(return_reslut, '0', '不满意', '1', '一般', '2', '满意', '3', '非常满意') return_reslut,
        fyr,
        phone,
        i.address
        from TF_YWPN_GDINFO_W i
        right join TF_YWPN_GDRETURN_W r on r.GDBH = i.GDBH
        where i.is_rxlx = '1' and RETURN_USER = #{djry}
        and to_char(djsj,'yyyy-MM-dd') between #{start} and #{end}
        <if test="pass != null">
            and return_reslut = #{pass}
        </if>
        <if test="gdzt != null">
            and GDGZLX = #{gdzt}
        </if>
    </select>

    <sql id="idea_count">
        (select count(*)
        from TF_YWPN_GDRETURN_W g left join TF_YWPN_GDINFO_W n on g.GDBH = n.GDBH
        where g.GD_TYPE = i.GDLX and n.IS_RXLX = '1'
        and return_reslut = ${pass}
        <choose>
            <when test="start != null and end != null">
                and to_char(DJSJ,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        )
    </sql>

    <select id="returnVisitCount" resultType="map">
        select gdlxName,
        jd_num,
        pd_num,
        cl_num,
        wcl_num,
        hf_num,
        GDLX,
        bmy_num,
        yb_num,
        my_num,
        fcmy_num,
        round((cl_num / nullif(jd_num,0)) * 100 ,2) rate,
        round(((my_num + fcmy_num) / nullif(hf_num,0)) * 100,2) hfmyl,
        round(((fcmy_num * 1 + my_num * 0.85 + yb_num * 0.75 + bmy_num * 0.5) / nullif(hf_num,0)) * 100,2) mydzs
        from (select
        GDLX,
        (<include refid="gdlxName"/>) gdlxName,
        count(*) jd_num,
        count(*) pd_num,
        sum(case when LCJD = 45 then 1 else 0 end ) cl_num,
        sum(case when LCJD != 45 then 1 else 0 end ) wcl_num,
        sum(case when GDHF_CS > 0 then 1 else 0 end) hf_num,
        sum(case when RETURN_RESLUT = 0 then 1 else 0 end) bmy_num,
        sum(case when RETURN_RESLUT = 1 then 1 else 0 end) yb_num,
        sum(case when RETURN_RESLUT = 2 then 1 else 0 end) my_num,
        sum(case when RETURN_RESLUT = 3 then 1 else 0 end) fcmy_num
        from TF_YWPN_GDINFO_W i left join TF_YWPN_GDRETURN_W r
        on i.gdbh = r.gdbh
        where IS_RXLX = '1'
        <choose>
            <when test="start != null and end != null">
                and to_char(i.djsj,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and i.djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        <if test="gdlx != null">
            and gdlx = #{gdlx}
        </if>
        <if test="djry != null">
            and djry = #{djry}
        </if>
        <if test="gdzt != null">
            and GDGZLX = #{gdzt}
        </if>
        group by gdlx)

    </select>

    <select id="returnUserCount" resultType="map">
        select
        jd_num,
        pd_num,
        cl_num,
        wcl_num,
        hf_num,
        RETURN_USER,
        userName,
        bmy_num,
        yb_num,
        my_num,
        fcmy_num,
        round((cl_num / nullif(jd_num,0)) * 100 ,2) rate,
        round(((my_num + fcmy_num) / nullif(hf_num,0)) * 100,2) hfmyl,
        round(((fcmy_num * 1 + my_num * 0.85 + yb_num * 0.75 + bmy_num * 0.5) / nullif(hf_num,0)) * 100,2) mydzs
        from (select
        RETURN_USER,
        (select REAL_NAME from TF_SMPN_USER_X where id = RETURN_USER) userName,
        sum(case when DJRY = RETURN_USER then 1 else 0 end) jd_num,
        sum(case when DJRY = RETURN_USER then 1 else 0 end) pd_num,
        sum(case when DJRY = RETURN_USER and FINISH_TIME is not null then 1 else 0 end ) cl_num,
        sum(case when DJRY = RETURN_USER and FINISH_TIME is null then 1 else 0 end ) wcl_num,
        sum(case when RETURN_USER is not null then 1 else 0 end) hf_num,
        sum(case when RETURN_RESLUT = 0 then 1 else 0 end) bmy_num,
        sum(case when RETURN_RESLUT = 1 then 1 else 0 end) yb_num,
        sum(case when RETURN_RESLUT = 2 then 1 else 0 end) my_num,
        sum(case when RETURN_RESLUT = 3 then 1 else 0 end) fcmy_num
        from TF_YWPN_GDINFO_W i left join TF_YWPN_GDRETURN_W r
        on i.gdbh = r.gdbh
        where IS_RXLX = '1'
        and RETURN_USER is not null
        <choose>
            <when test="start != null and end != null">
                and to_char(i.djsj,'yyyy-MM-dd') between #{start} and #{end}
            </when>
        </choose>
        <if test="gdlx != null">
            and gdlx = #{gdlx}
        </if>
        <if test="djry != null">
            and return_user = #{djry}
        </if>
        <if test="gdzt != null">
            and GDGZLX = #{gdzt}
        </if>
        group by return_user)
    </select>

    <sql id="select_code">
        (SELECT CNAME
         FROM TF_YWPN_SYSCHILDCODE_D
         WHERE pcid = ${pcid}
           AND CCODE = ${code})
    </sql>

    <select id="selectClientInfo" resultType="map">
        select yhbh,
        yhxm,
        yhdz,
        lxdh,
        yyqy,
        (select PQMC from TF_YWPN_QFCBCBPQ_W where PQBM = cbpq) cbpq,
        jfzh,
        (
        <include refid="select_code">
            <property name="pcid" value="17"/>
            <property name="code" value="zhzt"/>
        </include>
        )
        as zhzt,
        zhye,
        (select YSXZ_MC from TF_YWPN_QFMIDYSXZBM_E where YSXZ_BM = ysxz ) ysxz,
        (select YSXZ_MC from TF_YWPN_QFMIDYSXZXFBM_E where YSXZ_BM = ysxz and XZXF_BM = xzxf) xzxf,
        sbyt,
        decode(sfjt,'0','否','1','是') sfjt,
        decode(sfjl,'0','否','1','是') sfjl,
        decode(sfjf,'0','否','1','是') sfjf,
        decode(sf_ysdh,'0','否','1','是') sf_ysdh,
        decode(cbzq,'0','单月','1','双月') cbzq,
        yjfr,
        khrq,
        sbbh,
        (
        <include refid="select_code">
            <property name="pcid" value="2"/>
            <property name="code" value="sbcj"/>
        </include>
        )
        as sbcj,
        (
        <include refid="select_code">
            <property name="pcid" value="3"/>
            <property name="code" value="sblx"/>
        </include>
        )
        as sblx,
        sbdd,
        (
        <include refid="select_code">
            <property name="pcid" value="1"/>
            <property name="code" value="sbkj"/>
        </include>
        )
        as sbkj,
        azfs
        from TF_YWPN_QFKHJBXX_W
        <where>
            <if test="dto != null and dto != ''">
                YHDZ || yhxm || YHBH || LXDH like '%' || #{dto} || '%'
            </if>
        </where>
    </select>

    <!--    <select id="selectClientCost" resultType="map">-->
    <!--        select lsbh,-->
    <!--               khbh,-->
    <!--               cbyf,-->
    <!--               scds,-->
    <!--               bcds,-->
    <!--               cbsl,-->
    <!--               ysje,-->
    <!--               cbrq,-->
    <!--               decode(sfbz, '0', '未收费', '1', '已收费')                       sfbz,-->
    <!--               (select REAL_NAME from TF_SMPN_USER_X u where sfry = u.ID) sfry_name,-->
    <!--               sfry,-->
    <!--               sfrq,-->
    <!--               zzsje,-->
    <!--               jbsf,-->
    <!--               wyje,-->
    <!--               xfzbje,-->
    <!--               wsfje,-->
    <!--               ljfje-->
    <!--        from TF_YWPN_QFCBSSMX_W-->
    <!--        where khbh = #{dto}-->
    <!--    </select>-->

    <sql id="zj_count">
        (select count(*) from TF_YWPN_GDINFO_W g where IS_RXLX = '1' and IS_QUALITY_FLAG = ${flag}
        and g.GDLX = i.gdlx
        <choose>
            <when test="start != null and end != null">
                and to_char(djsj,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        )
    </sql>

    <sql id="gd_query">
        (select count(*) from TF_YWPN_GDINFO_W where LCJD = 45 and GDLX = i.gdlx and IS_RXLX = '1'
        <choose>
            <when test="start != null and end != null">
                and to_char(djsj,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        )
    </sql>

    <sql id="wgd_query">
        (select count(*) from TF_YWPN_GDINFO_W where LCJD != 45 and GDLX = i.gdlx and IS_RXLX = '1'
        <choose>
            <when test="start != null and end != null">
                and to_char(djsj,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        )
    </sql>

    <sql id="jd_query">
        (select count(*)
        from TF_YWPN_GDINFO_W g
        where IS_RXLX = '1'
        and g.GDLX = i.gdlx
        <choose>
            <when test="start != null and end != null">
                and to_char(djsj,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        )
    </sql>

    <sql id="zz_query">
        (select count(*) from TF_YWPN_GDINFO_W where GDZZZT = 1 and GDLX = i.gdlx and IS_RXLX = '1'
        <choose>
            <when test="start != null and end != null">
                and to_char(djsj,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        )
    </sql>


    <select id="orderQualityCount" resultType="map">
        select
        (SELECT GDLX_MC
        FROM TF_YWPN_GDGLTYPEBM_W
        WHERE ID = GDLX) gdlxName,
        count(*) jd_num,
        sum(case when LCJD = 45 then 1 else 0 end) gd_num,
        sum(case when LCJD != 45 then 1 else 0 end) wgd_num,
        sum(case when GDZZZT = 1 then 1 else 0 end) zz_num,
        sum(case when IS_QUALITY_FLAG = 0 then 1 else 0 end) wzj_num,
        sum(case when IS_QUALITY_FLAG = 1 then 1 else 0 end) zj_num,
        gdlx
        from TF_YWPN_GDINFO_W i
        where
        IS_RXLX = '1'
        <choose>
            <when test="start != null and end != null">
                and to_char(djsj,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        <if test="gdlx != null">
            and gdlx = #{gdlx}
        </if>
        <if test="djry != null">
            and djry = #{djry}
        </if>
        <if test="gdzt != null">
            and GDGZLX = #{gdzt}
        </if>
        group by gdlx
    </select>

    <select id="notTheQualityInfo" resultType="map">
        select
        <include refid="common_sql"/>
        from TF_YWPN_GDINFO_W
        where GDLX = #{type}
        and IS_RXLX = '1'
        <choose>
            <when test="start != null and end != null">
                and to_char(djsj,'yyyy-MM-dd') between #{start} and #{end}
            </when>
            <otherwise>
                and djsj between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        <choose>
            <when test="code == 1">
                and lcjd = 45
            </when>
            <when test="code == 2">
                and lcjd != 45
            </when>
            <when test="code == 3">
                and GDZZZT = 1
            </when>
            <when test="code == 4">
                and IS_QUALITY_FLAG = 1
            </when>
            <when test="code == 5">
                and IS_QUALITY_FLAG = 0
            </when>
        </choose>
        <if test="djry != null">
            and djry = #{djry}
        </if>
        <if test="gdzt != null">
            and GDGZLX = #{gdzt}
        </if>
    </select>

    <update id="updateGdInfo" parameterType="string">
        update TF_YWPN_GDINFO_W
        set IS_QUALITY_FLAG = 1
        where GDBH = #{gdbh}
    </update>

    <select id="getCrewInfo" resultType="map">
        select distinct DJRY, (select REAL_NAME from TF_SMPN_USER_X u where DJRY = u.ID) name
        from TF_YWPN_GDINFO_W
    </select>

    <select id="getworkOrderType" resultType="map">
        select distinct GDLX,
        <include refid="gdlxName"/>
        as name from TF_YWPN_GDINFO_W
    </select>

    <select id="getRoleInfo" resultType="map" fetchSize="1000">
        select ROLE_NAME
        from TF_SMPN_ROLE_X
        where id in (select ROLE_ID from TF_SMPN_USERROLE_X where USER_ID = #{userId})
    </select>

    <select id="telGetInfo" resultType="map">
        select yhbh,
               yhxm,
               yhdz,
               lxdh,
               lxry,
               lxsj,
               yyqy,
               cbpq,
               cbb,
               yhzt,
               jfzh,
               zhzt,
               zhye,
               gsht,
               ysxz,
               xzxf,
               sbyt,
               sfjt,
               sfjl,
               sfjf,
               zbry,
               sbbw,
               sf_ysdh,
               cbzq,
               yjfr,
               xzlx,
               sf_dwyh,
               sf_kzzs,
               khrq,
               sf_tsyh,
               tsdw,
               gs_htzt,
               sbbh,
               sbcj,
               sblx,
               sbdd,
               sbkj,
               azfs,
               azrq,
               zh_hbrq,
               jdrq,
               zh_xgrq,
               yhbs,
               gjbh,
               bc_zjrq,
               xc_zjrq,
               issyn,
               sxjmessage
        from TF_YWPN_QFKHJBXX_W
        where LXDH = #{tel}
    </select>

    <select id="allocation" resultType="map">
        SELECT a.YHBH, a.YHXM , a.YHDZ , a.LXDH , d.PQMC as CBPQ, c.CBB_MC as CBB , a.SBBH , e.YSXZ_MC as YSXZ, b.cbxh,
        a.sbkj
        FROM TF_YWPN_QFKHJBXX_W a
        RIGHT JOIN TF_YWPN_YHBINGQY_W b ON a.YHBH = b.YHBH
        LEFT JOIN TF_YWPN_QFMIDYSXZBM_E e ON a.YSXZ = e.YSXZ_BM
        LEFT JOIN TF_YWPN_DMACISCBB_W c ON a.CBB = c.CBB_BM
        LEFT JOIN TF_YWPN_QFCBCBPQ_W d ON a.CBPQ = d.PQBM
        <where>
            <if test="qybm != null and qybm != ''">
                and b.QYBM = #{qybm}
            </if>
            <if test="qydbm != null and qydbm != ''">
                and b.QYDBM = #{qydbm}
            </if>
            <if test="str != null and str != ''">
                and a.YHBH || a.YHXM || a.YHDZ || a.LXDH like '%' || #{str} || '%'
            </if>
        </where>
        order by b.CBXH
    </select>

    <select id="getRealNameById" resultType="string">
        <choose>
            <when test="empcode != null and empcode != ''">
                select REAL_NAME from TF_SMPN_USER_X where USERNAME = #{empcode} and rownum = 1
            </when>
            <otherwise>
                select null from dual
            </otherwise>
        </choose>
    </select>

    <select id="getQualityInfo" resultType="map">
        select r.GDBH,
        (select real_name from TF_SMPN_USER_X where id = r.ZJRY) zjry_name,
        r.ZJRY,
        r.ZJBM,
        (select NAME from TF_SMPN_DEPARTMENT_X where ID = r.ZJBM) zjbm_name,
        ZJMS,
        r.ZJSJ,
        GDLX,
        (select GDLX_MC from TF_YWPN_GDGLTYPEBM_W where id = GDLX) gdlx_name,
        decode(ZJJG,'0','合格','不合格') zjjg,
        r.SF_QX,
        r.SF_CBZ,
        r.SF_CZSX,
        r.SF_SLSX,
        r.ZJDF
        from TF_YWPN_GDINFO_W i
        right join TF_YWPN_RXGDZJLR_W r on i.GDBH = r.GDBH
        <where>
            <if test="start != null and start != '' and end != null and end != ''">
                to_char(r.ZJSJ,'yyyy-mm-dd') between #{start} and #{end}
            </if>
            <if test="gdlx != null and gdlx != ''">
                and gdlx = #{gdlx}
            </if>
            <if test="gdbh != null and gdbh != ''">
                and r.gdbh = #{gdbh}
            </if>
        </where>
    </select>

    <select id="qualityCount" resultType="map">
        select DJRY,
        account,
        name,
        yzj_num,
        wzj_num,
        sum_num,
        DJBM,
        round((yzj_num / nullif(sum_num,0)) * 100,2) rate
        from (select DJRY,DJBM,
        (select USERNAME from TF_SMPN_USER_X where id = DJRY) account,
        (select REAL_NAME from TF_SMPN_USER_X where id = DJRY) name,
        count(*) sum_num,
        sum(case when ZJJG is not null then 1 else 0 end) yzj_num,
        sum(case when ZJJG is null then 1 else 0 end) wzj_num
        from TF_YWPN_GDINFO_W i
        left join TF_YWPN_RXGDZJLR_W r on i.GDBH = r.GDBH
        where IS_RXLX = '1'
        and to_char(DJSJ, 'yyyy-mm-dd') between #{dto.start} and #{dto.end}
        <if test="dto.dept != null and dto.dept != ''">
            and i.djbm = #{dto.dept}
        </if>
        <if test="dto.user != null and dto.user != ''">
            and djry = #{dto.user}
        </if>
        group by DJRY,DJBM)
    </select>

    <select id="qualityInfo" resultType="map" fetchSize="1000">
        select i.GDBH,
        GDXF1,
        GDMS,
        (select GDLX_MC from TF_YWPN_GDGLTYPEBM_W where id = GDXF1) gdxf1_name,
        GDXF2,
        (select GDLX_MC from TF_YWPN_GDGLTYPEBM_W where id = GDXF2) gdxf2_name,
        DJRY,
        (select REAL_NAME from TF_SMPN_USER_X where id = DJRY) djry_name,
        DJBM,
        (select NAME from TF_SMPN_DEPARTMENT_X where id = DJBM) djbm_name,
        DJSJ,
        (select real_name from TF_SMPN_USER_X where id = r.ZJRY) zjry_name,
        r.ZJRY,
        r.ZJBM,
        (select NAME from TF_SMPN_DEPARTMENT_X where ID = r.ZJBM) zjbm_name,
        ZJMS,
        r.ZJSJ,
        GDLX,
        (case when ZJJG is not null then '已质检' else '未质检' end ) sfzj,
        (select GDLX_MC from TF_YWPN_GDGLTYPEBM_W where id = GDLX) gdlx_name,
        decode(ZJJG,'0','合格','1','不合格') zjjg,
        r.SF_QX,
        r.SF_CBZ,
        r.SF_CZSX,
        r.SF_SLSX,
        r.ZJDF
        from TF_YWPN_GDINFO_W i
        left join TF_YWPN_RXGDZJLR_W r on i.GDBH = r.GDBH
        where IS_RXLX = '1'
        and to_char(DJSJ, 'yyyy-mm-dd') between #{dto.start} and #{dto.end}
        <if test="dto.dept != null and dto.dept != ''">
            and i.djbm = #{dto.dept}
        </if>
        <if test="dto.user != null and dto.user != ''">
            and djry = #{dto.user}
        </if>
        <choose>
            <when test="dto.state == 0">
                and zjjg is not null
            </when>
            <when test="dto.state == 1">
                and zjjg is null
            </when>
        </choose>
    </select>

    <select id="getDyYh" resultType="map">
        select q.YHBH,
        q.YHXM,
        q.YHDZ,
        YSXZ,
        SBKJ,
        (select YSXZ_MC from TF_YWPN_QFMIDYSXZBM_E where YSXZ_BM = YSXZ) ysxz_name,
        YLZ,
        case when LXDH is not null and LXDH != 0 then LXDH else LXSJ end tel,
        DXNUM,
        NEWTIME
        from TF_YWPN_DYYH_W d
        inner join TF_YWPN_QFKHJBXX_W q on d.YHBH = q.YHBH
        <where>
            <if test="input != null">
                d.YHBH || YHXM || YHDZ || LXDH || LXSJ like '%' || #{input} || '%'
            </if>
            <if test="start != null and end != null">
                and to_char(NEWTIME,'yyyy-MM-dd HH:mm:ss') between #{start} and #{end}
            </if>
        </where>
    </select>

    <update id="updateDyyh">
        update TF_YWPN_DYYH_W
        set NEWTIME = sysdate,
            DXNUM   = #{dyyh.dxnum}
        where yhbh = #{dyyh.yhbh}
    </update>

    <select id="queryNoticeInfo" resultType="map">
        select *
        from (
                 select YJBH,
                        YWLX,
                        YJBT,
                        YJMS,
                        CREATE_DATE,
                        row_number() over (partition by YWLX order by CREATE_DATE desc) row_num
                 from TF_YWPN_RXYJFW_W
                 where YWLX like '%' || #{type} || '%'
                   and voice = 0
             )
        where row_num = 1
    </select>

    <select id="queryUserBalance" resultType="map">
        select distinct ZHYE, QFJE, SFQF
        from TF_YWPN_QFKHJBXX_W
        where YHBH = #{code}
    </select>

</mapper>