<?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.feesapi.sfgl.mapper.YyQfmxMapper">
    <!--查询用户是否存在欠费 -->
    <select id="selectExistQf" resultType="int">
        select nvl(count(0), 0) sl
        from yy_qfmx
        where sfbz = '0'
          and yhbh = #{yhbh}
    </select>
    <select id="selectJmje" resultType="java.math.BigDecimal">
        SELECT COALESCE(SUM(tzje), 0)
        FROM yy_wyjtz
        WHERE sfbz = '0'
          and gdzt = '3'
          and qfid = #{qfid}
    </select>
    <select id="wyjqbjm" resultType="java.lang.Long">
        select count(0)
        from yy_wyjclgl
        where yhbh = #{yhbh}
    </select>

    <select id="qfmxbgtj" resultType="java.util.Map">
        SELECT
        nvl(sum(nvl(YSSZYF,0)),0)as ysszyf,
        nvl(sum(case when a.fylx='1' or a.fylx='2' then a.yssl else 0 end),0) as yssl,
        nvl(sum(nvl(YSSF,0)),0) as yssf,
        nvl(sum(nvl(YSWSF,0)),0) as yswsf,
        nvl(sum(nvl(YSLJF,0)),0) as ysljf,
        nvl(sum(nvl(ZNJ,0)),0) as znj
        FROM yy_qfmx a
        LEFT JOIN yh_khjbxx b ON a.yhbh = b.yhbh
        WHERE 1=1
        <if test="sfbz!=null">
            <choose>
                <when test="sfbz.indexOf(',')!=-1">
                    AND a.sfbz in
                    <foreach item="item" index="index" collection="sfbz.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.sfbz=#{sfbz}
                </otherwise>
            </choose>
        </if>
        <if test="filter.sfbc!=null">
            <choose>
                <when test="filter.sfbc.indexOf(',')!=-1">
                    AND a.sfbc in
                    <foreach item="item" index="index" collection="filter.sfbc.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.sfbc=#{filter.sfbc}
                </otherwise>
            </choose>
        </if>
        <if test="chbz!=null">
            <choose>
                <when test="chbz.indexOf(',')!=-1">
                    AND a.chbz in
                    <foreach item="item" index="index" collection="chbz.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.chbz=#{chbz}
                </otherwise>
            </choose>
        </if>
        <if test="filter.ssgs!=null">
            AND a.ssgs=#{filter.ssgs}
        </if>
        <if test="filter.sscbpq!=null">
            AND a.sscbpq=#{filter.sscbpq}
        </if>
        <if test="filter.sscbb!=null">
            <choose>
                <when test="filter.sscbb.indexOf(',')!=-1">
                    AND a.sscbb in
                    <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.sscbb=#{filter.sscbb}
                </otherwise>
            </choose>
        </if>
        <if test="filter.nf!=null">
            AND a.cwrq like '${filter.nf}%'
        </if>
        <if test="filter.yhbh!=null">
            AND a.yhbh=#{filter.yhbh}
        </if>
        <if test="filter.cbrybm!=null">
            AND a.cbrybm=#{filter.cbrybm}
        </if>
        <if test="filter.jsrqStart!=null and filter.jsrqEnd!=null">
            AND (a.jsrq BETWEEN #{filter.jsrqStart} AND #{filter.jsrqEnd})
        </if>
        <if test="filter.slxt!=null and filter.slxt">
            AND a.ysje=0
        </if>
        <!--        <if test="filter.ffgxzh!=null">-->
        <!--            AND a.yhbh in (select yhbh from yh_group_accounts-->
        <!--            where group_id in (select id from yh_group where yhbh= #{filter.ffgxzh}))-->
        <!--            <if test="filter.cwrq==null">-->
        <!--                and cwrq=(select min(cwrq) from yy_qfmx where yhbh in (-->
        <!--                select yhbh from yh_group_accounts-->
        <!--                where group_id in (select id from yh_group where yhbh= #{filter.ffgxzh})-->
        <!--                ))-->
        <!--            </if>-->
        <!--        </if>-->
        <if test="filter.cwrq!=null">
            AND a.cwrq=#{filter.cwrq}
        </if>
        <if test="filter.sfsykfp!=null">
            AND a.ei_ykfp_bz=#{filter.sfsykfp}
        </if>
    </select>

    <sql id="querySql">
        SELECT
        a.*,
        b.yhdz,
        b.lxdh,
        (nvl(a.ysje, 0) + nvl(a.znj, 0)) yszje,
        c.gdbz,
        d.name as ysxzmc,
        d.sjmc
        from yy_qfmx a
        left join yh_khjbxx b on a.yhbh = b.yhbh
        left join yy_qfmx_gd c on a.gdlsbh=c.gdlsbh
        left join (
        select sj.code, sj.name as sjmc, yslx.name,yslx.code as ysxzbm
        from bm_sj sj
        left join bm_yslx yslx on sj.yslx=yslx.code
        ) d on a.sjbm = d.code
        WHERE 1=1
        <include refid="query_whereSql"/>
    </sql>

    <sql id="queryCountSql">
        SELECT
        sum(case when a.fylx='1' or a.fylx='2' then nvl(yssl,0) else 0 end) zyssl,
        sum(nvl(yssf,0)) zyssf,
        sum(nvl(yswsf,0)) zyswsf,
        sum(nvl(ysljf,0)) zysljf,
        sum(nvl(ysszyf,0)) zysszyf,
        sum( nvl(ysecjyf, 0) ) zysecjyf,
        sum( nvl(ysfjf, 0) ) zysfjf,
        sum( nvl(ysqt, 0) ) zysqt,
        sum(nvl(ysje,0)) zysje,
        sum(nvl(znj,0)) zznj,
        sum(nvl(ysje,0)+nvl(znj,0)) zyszje
        from yy_qfmx a
        left join yh_khjbxx b on a.yhbh = b.yhbh
        left join yy_qfmx_gd c on a.gdlsbh=c.gdlsbh
        left join (
        select sj.code, sj.name as sjmc, yslx.name,yslx.code as ysxzbm
        from bm_sj sj
        left join bm_yslx yslx on sj.yslx=yslx.code
        ) d on a.sjbm = d.code
        WHERE 1=1
        <include refid="query_whereSql"/>
    </sql>

    <sql id="query_whereSql">
        <if test="filter.searchText!=null">
            and (
            a.yhbh like '%'||#{filter.searchText}||'%'
            or a.khbm like '%'||#{filter.searchText}||'%'
            or b.yhxm like '%'||#{filter.searchText}||'%'
            or b.yhdz like '%'||#{filter.searchText}||'%'
            or b.zjhm like '%'||#{filter.searchText}||'%'
            or b.lxdh like '%'||#{filter.searchText}||'%'
            )
        </if>
        <if test="filter.sfbc!=null">
            <choose>
                <when test="filter.sfbc.indexOf(',')!=-1">
                    AND a.sfbc in
                    <foreach item="item" index="index" collection="filter.sfbc.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.sfbc=#{filter.sfbc}
                </otherwise>
            </choose>
        </if>
        <if test="chbz != null">
            <choose>
                <when test="chbz.indexOf(',')!=-1">
                    AND a.chbz in
                    <foreach item="item" index="index" collection="chbz.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.chbz=#{chbz}
                </otherwise>
            </choose>
        </if>
        <if test="sfbz != null">
            <choose>
                <when test="sfbz.indexOf(',')!=-1">
                    AND a.sfbz in
                    <foreach item="item" index="index" collection="sfbz.split(',')" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.sfbz = #{sfbz}
                </otherwise>
            </choose>
        </if>
        <if test="filter.cwrq!=null">
            AND a.cwrq=#{filter.cwrq}
        </if>
        <if test="filter.ssgs!=null">
            AND a.ssgs=#{filter.ssgs}
        </if>
        <if test="filter.sscbpq!=null">
            AND a.sscbpq=#{filter.sscbpq}
        </if>
        <if test="filter.sscbb!=null">
            <choose>
                <when test="filter.sscbb.indexOf(',')!=-1">
                    AND a.sscbb in
                    <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.sscbb=#{filter.sscbb}
                </otherwise>
            </choose>
        </if>
        <if test="filter.nf!=null">
            AND a.cwrq like '${filter.nf}%'
        </if>

        <if test="filter.yhbh!=null and filter.yhbh!=''">
            AND a.yhbh = #{filter.yhbh}
        </if>
        <if test="filter.khbm != null and filter.khbm !=''">
            AND a.khbm=UPPER(#{filter.khbm})
        </if>

        <if test="filter.cbrybm!=null">
            AND a.cbrybm=#{filter.cbrybm}
        </if>
        <if test="filter.jsrqStart!=null and filter.jsrqEnd!=null">
            <![CDATA[ and a.jsrq >=#{filter.jsrqStart}+0  and a.jsrq <= #{filter.jsrqEnd}+0]]>
        </if>
        <if test="filter.slxt!=null and filter.slxt">
            AND a.ysje = 0
        </if>
        <if test="filter.ffgxzh!=null">
            AND a.yhbh in (
            select yhbh from yh_group_accounts
            where group_id in (select id from yh_group where yhbh= #{filter.ffgxzh})
            union
            select yhbh from yh_group where yhbh=#{filter.ffgxzh}
            )
        </if>

        <if test="filter.sfsykfp!=null">
            AND a.ei_ykfp_bz=#{filter.sfsykfp}
        </if>
        <if test="filter.yhbhStart!=null">
            <![CDATA[ AND a.yhbh>=#{filter.yhbhStart} ]]>
        </if>
        <if test="filter.yhbhEnd!=null">
            <![CDATA[ AND a.yhbh<=#{filter.yhbhEnd} ]]>
        </if>
        <if test="filter.qyfzr!=null">
            AND a.sscbb in(select cbbbm from bm_cbb where qyfzr=#{filter.qyfzr})
        </if>
        <if test="filter.cwrqStart!=null and filter.cwrqEnd!=null">
            AND a.cwrq BETWEEN #{filter.cwrqStart} AND #{filter.cwrqEnd}
        </if>
        <if test="filter.ysyfStart!=null and filter.ysyfEnd!=null">
            AND a.ysyf BETWEEN #{filter.ysyfStart} AND #{filter.ysyfEnd}
        </if>
        <if test="filter.gdlsbh!=null">
            AND a.gdlsbh=#{filter.gdlsbh}
        </if>
        <if test="filter.gdzt!=null">
            AND a.gdzt=#{filter.gdzt}
        </if>
        <if test="filter.ysxz!=null">
            AND d.ysxzbm=#{filter.ysxz}
        </if>
        <if test="filter.wyjgdlsbh!=null">
            AND a.id in (select qfid from yy_wyjtz where gdlsbh=#{filter.wyjgdlsbh})
        </if>
        <if test="filter.jyType!=null">
            AND a.jy_type=#{filter.jyType}
        </if>
        <if test="filter.yhxm != null and filter.yhxm != ''">
            AND instr(b.cym, #{filter.yhxm}) > 0
        </if>
        <if test="filter.yhxmjm!=null">
            AND get_pyjm(a.yhxm) like '%'||UPPER(#{filter.yhxmjm})||'%'
        </if>
        <if test="filter.sqlStrWhere!=null and filter.sqlStrWhere!=''">
            <![CDATA[ AND ${filter.sqlStrWhere}  ]]>
        </if>
        <if test="filter.dxh!=null">
            <if test='filter.dxh=="0"'>
                AND a.khbm is null
            </if>
            <if test='filter.dxh=="1"'>
                AND a.khbm >' '
            </if>
        </if>
        <if test="filter.userId!=null">
            AND a.sscbb in (select cbbbm from yh_sjqx where user_id=#{filter.userId})
        </if>
    </sql>

    <select id="getJzhztjList" resultType="com.tofly.feesapi.sfgl.entity.vo.YyQfmxJzhzSelect">
        SELECT /*d.name as ysxzmc,*/ d.sjmc,
        a.sj${tag},a.wsfdj${tag},a.szyf_dj${tag},
        sum(case when nvl(sf${tag},0)!=0 then a.sl else 0 end)sfsl,
        sum(a.sf${tag})sf,
        sum(case when nvl(wsf${tag},0)!=0 then a.sl else 0 end)wsfsl,
        sum(a.wsf${tag})wsf,
        sum(a.szyf${tag})szyf,
        sum(case when nvl(szyf${tag},0)!=0 then a.sl else 0 end)szyfsl
        <if test="tag==null or tag==''">
            ,sum(ysecjyf)ecjyf
            ,sum(ysfjf)fjf
            ,sum(ysqt)xfsf
            ,sum(case when nvl(sf,0)!=0 then 1 else 0 end)sfhs
            ,sum(case when nvl(wsf,0)!=0 then 1 else 0 end)wsfhs
            ,sum(case when nvl(szyf,0)!=0 then 1 else 0 end)szyfhs
            ,sum(case when nvl(ysecjyf,0)!=0 then 1 else 0 end)ecjyfhs
            ,sum(case when nvl(ysfjf,0)!=0 then 1 else 0 end)fjfhs
            ,sum(case when nvl(ysqt,0)!=0 then 1 else 0 end)xfsfhs
        </if>
        from yy_qfmx a
        left join (
        select sj.code, sj.name as sjmc, yslx.name,yslx.code as ysxzbm, sj.bz as bz
        from bm_sj sj
        left join bm_yslx yslx on sj.yslx = yslx.code
        ) d on a.sjbm${tag} = d.code
        where 1=1 and a.sjbm${tag} >' ' and a.sfbz not in ('4')
        <if test="chbz != null">
            <choose>
                <when test="chbz.indexOf(',')!=-1">
                    AND a.chbz in
                    <foreach item="item" index="index" collection="chbz.split(',')" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.chbz=#{chbz}
                </otherwise>
            </choose>
        </if>
        <if test="sfbz != null">
            <choose>
                <when test="sfbz.indexOf(',') != -1">
                    AND a.sfbz in
                    <foreach item="item" index="index" collection="sfbz.split(',')" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.sfbz = #{sfbz}
                </otherwise>
            </choose>
        </if>
        <if test="filter.nf != null and filter.nf!=''">
            AND a.cwrq like '${filter.nf}%'
        </if>
        <if test="filter.cwrq != null and filter.cwrq != ''">
            AND a.cwrq = #{filter.cwrq}
        </if>
        <if test="filter.startDate != null and filter.endDate != null">
            AND a.jsrq>=#{filter.startDate} and a.jsrq&lt;#{filter.endDate}
        </if>
        <if test="filter.jzEnd!=null">
            <![CDATA[ and (sfbz='0' or (sfbz='1' and sfrq>#{filter.jzEnd}+0)) ]]>
        </if>
        <if test="filter.sfbz!=null">
            and a.SFBZ=#{filter.sfbz}
        </if>
        group by /*d.name,*/ d.sjmc,a.sj${tag},a.wsfdj${tag},a.szyf_dj${tag}
    </select>

    <select id="getPageList" resultType="com.tofly.feesapi.sfgl.entity.vo.YyQfmxSelect">
        <include refid="querySql"/>
    </select>

    <select id="getPageListByKhbm" resultType="com.tofly.feesapi.sfgl.entity.vo.YyQfmxSelect">
        select khbm,max(yhxm) yhxm,max(yhdz) yhdz, sum(case WHEN  fylx in ('1','2') then yssl else 0 end ) yssl ,sum(yswsf) yswsf,sum(yssf) yssf,sum(ysecjyf) ysecjyf,sum(ysqt) ysqt,sum(ysfjf) ysfjf,sum(ysszyf) ysszyf,sum(ysje) ysje from
        (
        <include refid="querySql"/>
        )
        group by khbm order by khbm
    </select>



    <select id="getPageCount" resultType="com.tofly.feesapi.sfgl.entity.vo.YyQfmxSelectCount">
        <include refid="queryCountSql"/>
    </select>

    <select id="sjStat" resultType="com.tofly.feesapi.sfgl.entity.dto.YySjStat">
        SELECT ${cwrq}a.sjbm${tag} sjbm,sfbz,
        a.sj${tag} sj ,
        SUM(case when fylx in(1,2) then nvl(a.sl${tag},0) else 0 end) sl,
        SUM(nvl(a.sf${tag},0)) sf,
        SUM(nvl(a.wsf${tag},0)) wsf,
        SUM(nvl(a.ljf${tag},0)) ljf,
        SUM(nvl(a.szyf${tag},0)) szyf
        <if test="tag == null or tag==''">
            ,sum(nvl(a.znj,0)) znj
            ,count(*) bs
            ,sum(nvl(a.ysecjyf, 0)) ecjyf
            ,sum(nvl(a.ysfjf, 0)) fjf
            ,sum(nvl(a.ysqt, 0)) qtf
        </if>
        FROM yy_qfmx a
        left join bm_sj b on a.sjbm${tag} = b.code
        WHERE a.sjbm${tag} >' ' AND a.sfbc !=3 AND b.jtbz != '1'
        <if test="filter.chbz!=null">
            <choose>
                <when test="filter.chbz.indexOf(',')!=-1">
                    AND a.chbz in
                    <foreach item="item" index="index" collection="filter.chbz.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.chbz=#{filter.chbz}
                </otherwise>
            </choose>
        </if>

        <if test="filter.sfbz!=null">
            AND a.sfbz=#{filter.sfbz}
        </if>
        <if test="filter.cwrq!=null">
            AND a.cwrq=#{filter.cwrq}
        </if>
        <if test="filter.ssgs!=null">
            AND a.ssgs=#{filter.ssgs}
        </if>
        <if test="filter.sscbpq!=null">
            AND a.sscbpq=#{filter.sscbpq}
        </if>
        <if test="filter.sscbb!=null">
            <choose>
                <when test="filter.sscbb.indexOf(',')!=-1">
                    AND a.sscbb in
                    <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.sscbb=#{filter.sscbb}
                </otherwise>
            </choose>
        </if>
        <if test="filter.cbrybm!=null">
            AND a.cbrybm=#{filter.cbrybm}
        </if>
        <if test="filter.nf!=null">
            AND a.cwrq like '${filter.nf}%'
        </if>
        <if test="filter.yhbh!=null">
            AND a.yhbh = #{filter.yhbh}
        </if>
        <if test="filter.yhbhStart!=null">
            and a.yhbh >= #{filter.yhbhStart}
        </if>
        <if test="filter.yhbhEnd!=null">
            and a.yhbh &lt;= #{filter.yhbhEnd}
        </if>
        <if test="filter.jzEnd!=null">
            <![CDATA[ and (sfbz='0' or (sfbz='1' and sfrq>#{filter.jzEnd}+0)) ]]>
        </if>
        <if test="filter.jsrqStart!=null and filter.jsrqEnd!=null">
            <![CDATA[ and a.jsrq >=#{filter.jsrqStart}+0  and a.jsrq <= #{filter.jsrqEnd}+0]]>
        </if>
        <if test="filter.qyfzr!=null">
            AND a.sscbb in(select cbbbm from bm_cbb where qyfzr=#{filter.qyfzr})
        </if>
        <if test="filter.cwrqStart!=null and filter.cwrqEnd!=null">
            AND a.cwrq BETWEEN #{filter.cwrqStart} AND #{filter.cwrqEnd}
        </if>
        <if test="filter.jyType!=null">
            AND a.jy_type=#{filter.jyType}
        </if>
        <if test="filter.ysyfStart!=null and filter.ysyfEnd!=null">
            AND a.ysyf BETWEEN #{filter.ysyfStart} AND #{filter.ysyfEnd}
        </if>
        <if test="filter.yslx!=null">
            AND b.yslx=#{filter.yslx}
        </if>
        <if test="filter.dxh!=null">
            <if test='filter.dxh=="0"'>
                AND a.khbm is null
            </if>
            <if test='filter.dxh=="1"'>
                AND a.khbm >' '
            </if>
        </if>
        GROUP BY ${cwrq}a.sjbm${tag},a.sj${tag},sfbz
    </select>

    <select id="sjjtStat" resultType="com.tofly.feesapi.sfgl.entity.dto.YySjStat">
        select
        ${cwrq} sjbm,
        sj,
        sfbz,
        nvl(sum(sl), 0) sl,
        nvl(sum(sf), 0) sf,
        nvl(sum(wsf), 0) wsf,
        nvl(sum(szyf), 0) szyf,
        nvl(sum(ecjyf), 0) ecjyf,
        nvl(sum(fjf), 0) fjf,
        nvl(sum(qtf), 0) qtf
        from (
        SELECT
        ${cwrq}
        case
        when t.tp=1 then a.sjbm
        when t.tp=2 then a.sjbm1
        when t.tp=3 then a.sjbm2
        end as sjbm,
        a.sfbz,

        case
        when t.tp=1 then case when nvl(a.jt${tag}sj,0)!=0 then nvl(a.jt${tag}sj,0)+nvl(a.wsfdj,0)+nvl(a.szyf_dj,0) else
        0 end
        when t.tp=2 then case when nvl(a.sj1_jt${tag}sj,0)!=0 then
        nvl(a.sj1_jt${tag}sj,0)+nvl(a.wsfdj1,0)+nvl(a.szyf_dj1,0) else 0 end
        when t.tp=3 then case when nvl(a.sj2_jt${tag}sj,0)!=0 then
        nvl(a.sj2_jt${tag}sj,0)+nvl(a.wsfdj2,0)+nvl(a.szyf_dj2,0) else 0 end
        end as sj,

        case
        when t.tp=1 then case when a.fylx in (1,2) and a.jt${tag}sf!=0 then nvl(a.jt${tag}sl,0)
        else case when a.fylx in (1,2) and a.sfbz='4' then nvl(a.jt${tag}sl,0) else 0 end end
        when t.tp=2 then case when a.fylx in (1,2) and a.sj1_jt${tag}sf!=0 then nvl(a.sj1_jt${tag}sl,0)
        else case when a.fylx in (1,2) and a.sfbz='4' then nvl(a.sj1_jt${tag}sl,0) else 0 end end
        when t.tp=3 then case when a.fylx in (1,2) and a.sj2_jt${tag}sf!=0 then nvl(a.sj2_jt${tag}sl,0)
        else case when a.fylx in (1,2) and a.sfbz='4' then nvl(a.sj2_jt${tag}sl,0) else 0 end end
        end as sl,

        case
        when t.tp=1 then round(nvl(a.jt${tag}sf,0),2)
        when t.tp=2 then round(nvl(a.sj1_jt${tag}sf,0),2)
        when t.tp=3 then round(nvl(a.sj2_jt${tag}sf,0),2)
        end as sf,

        case
        when t.tp=1 then case when a.fylx in (1,2,3) and a.wsf!=0 then round(nvl(a.wsfdj,0)*nvl(a.jt${tag}sl,0),2) else
        0 end
        when t.tp=2 then case when a.fylx in (1,2,3) and a.wsf1!=0 then round(nvl(a.wsfdj1,0)*nvl(a.sj1_jt${tag}sl,0),2)
        else 0 end
        when t.tp=3 then case when a.fylx in (1,2,3) and a.wsf2!=0 then round(nvl(a.wsfdj2,0)*nvl(a.sj2_jt${tag}sl,0),2)
        else 0 end
        end as wsf,

        case
        when t.tp=1 then case when a.fylx in (1,2,5) and a.szyf!=0 then round(nvl(a.szyf_dj,0)*nvl(a.jt${tag}sl,0),2)
        else 0 end
        when t.tp=2 then case when a.fylx in (1,2,5) and a.szyf1!=0 then
        round(nvl(a.szyf_dj1,0)*nvl(a.sj1_jt${tag}sl,0),2) else 0 end
        when t.tp=3 then case when a.fylx in (1,2,5) and a.szyf2!=0 then
        round(nvl(a.szyf_dj2,0)*nvl(a.sj2_jt${tag}sl,0),2) else 0 end
        end as szyf,

        <if test='tag!=null and tag=="1"'>
            nvl(a.znj, 0) znj,
            case
            when t.tp=1 then nvl(a.ljf,0)
            when t.tp=2 then nvl(a.ljf1,0)
            when t.tp=3 then nvl(a.ljf2,0)
            end as ljf,
            nvl(a.ysecjyf, 0) ecjyf,
            nvl(a.ysfjf, 0) fjf,
            nvl(a.ysqt, 0) qtf
        </if>
        <if test='tag!=null and tag!="1"'>
            0 znj,
            0 ljf,
            0 ecjyf,
            0 fjf,
            0 qtf
        </if>
        FROM yy_qfmx a
        left join bm_sj b1 on a.sjbm = b1.code
        left join bm_sj b2 on a.sjbm1 = b2.code
        left join bm_sj b3 on a.sjbm2 = b3.code
        left join yh_khjbxx c on a.yhbh = c.yhbh
        cross join (
        select 1 tp from dual union all
        select 2 tp from dual union all
        select 3 tp from dual
        ) t
        WHERE 1=1
        and (
        (t.tp=1 and b1.jtbz='1') or
        (t.tp=2 and b2.jtbz='1') or
        (t.tp=3 and b3.jtbz='1')
        )
        <if test='tag!=null and tag!="1"'>
            and (
            (t.tp=1 and a.jt${tag}sl != 0) or
            (t.tp=2 and a.sj1_jt${tag}sl !=0) or
            (t.tp=3 and a.sj2_jt${tag}sl != 0) )
        </if>
        <choose>
            <when test='sjbm!=null and sjbm!=""'>
                and (
                (t.tp=1 and a.sjbm=#{sjbm}) or
                (t.tp=2 and a.sjbm1=#{sjbm}) or
                (t.tp=3 and a.sjbm2=#{sjbm})
                )
            </when>
            <otherwise>
                and (
                (t.tp=1 and a.sjbm > ' ') or
                (t.tp=2 and a.sjbm1 > ' ') or
                (t.tp=3 and a.sjbm2 > ' ')
                )
            </otherwise>
        </choose>

        <!-- 所有条件不变 -->
        <if test="filter.chbz!=null">
            <choose>
                <when test="filter.chbz.indexOf(',')!=-1">
                    AND a.chbz in
                    <foreach item="item" collection="filter.chbz.split(',')" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.chbz=#{filter.chbz}
                </otherwise>
            </choose>
        </if>
        <if test="filter.sfbz!=null">
            AND a.sfbz=#{filter.sfbz}
        </if>
        <if test="filter.cwrq!=null">
            AND a.cwrq=#{filter.cwrq}
        </if>
        <if test="filter.ssgs!=null">
            AND c.ssgs=#{filter.ssgs}
        </if>
        <if test="filter.sscbpq!=null">
            AND c.sscbpq=#{filter.sscbpq}
        </if>
        <if test="filter.sscbb!=null">
            AND c.sscbb=#{filter.sscbb}
        </if>
        <if test="filter.cbrybm!=null">
            AND a.cbrybm=#{filter.cbrybm}
        </if>
        <if test="filter.nf!=null">
            AND a.cwrq like '${filter.nf}%'
        </if>
        <if test="filter.yhbh!=null">
            AND a.yhbh =#{filter.yhbh}
        </if>
        <if test="filter.yhbhStart!=null">
            AND a.yhbh >= #{filter.yhbhStart}
        </if>
        <if test="filter.yhbhEnd!=null">
            AND a.yhbh &lt;= #{filter.yhbhEnd}
        </if>
        <if test="filter.jsrqStart!=null and filter.jsrqEnd!=null">
            <![CDATA[ AND a.jsrq >=#{filter.jsrqStart}+0 AND a.jsrq <= #{filter.jsrqEnd}+0 ]]>
        </if>
        <if test="filter.jzEnd!=null">
            <![CDATA[ AND (a.sfbz='0' or (a.sfbz='1' AND a.sfrq>#{filter.jzEnd}+0)) ]]>
        </if>
        <if test="filter.qyfzr!=null">
            AND c.sscbb in(select cbbbm from bm_cbb where qyfzr=#{filter.qyfzr})
        </if>
        <if test="filter.cwrqStart!=null and filter.cwrqEnd!=null">
            AND a.cwrq BETWEEN #{filter.cwrqStart} AND #{filter.cwrqEnd}
        </if>
        <if test="filter.jyType!=null">
            AND a.jy_type=#{filter.jyType}
        </if>
        <if test="filter.ysyfStart!=null and filter.ysyfEnd!=null">
            AND a.ysyf BETWEEN #{filter.ysyfStart} AND #{filter.ysyfEnd}
        </if>
        <if test="filter.yslx!=null">
            AND b1.yslx=#{filter.yslx}
        </if>
        <if test="filter.dxh!=null">
            <if test='filter.dxh=="0"'>
                AND a.khbm is null
            </if>
            <if test='filter.dxh=="1"'>
                AND a.khbm > ' '
            </if>
        </if>
        ) temp
        group by ${cwrq} sjbm, sj, sfbz
    </select>

    <select id="sjjtStat1" resultType="com.tofly.feesapi.sfgl.entity.dto.YySjStat">
        select ${cwrq}sjbm,sj,sfbz,
        nvl(sum(sl),0) sl,
        nvl(sum(sf),0) sf,
        nvl(sum(wsf),0) wsf,
        nvl(sum(szyf),0) szyf,
        nvl(sum(ecjyf), 0) ecjyf,
        nvl(sum(fjf), 0) fjf,
        nvl(sum(qtf), 0) qtf
        from(
        SELECT ${cwrq}a.sjbm sjbm,sfbz,
        case
        when nvl(jt${tag}sj,0)!=0 then nvl(jt${tag}sj,0)+nvl(a.wsfdj,0)+nvl(a.szyf_dj,0)
        else 0 end sj,
        (case
        when fylx in(1,2) and jt${tag}sf!=0 then nvl(a.jt${tag}sl,0)
        else
        case when fylx in(1,2) and sfbz='4' then nvl(a.jt${tag}sl,0) else 0 end
        end) sl,
        round(nvl(a.jt${tag}sf,0),2) sf,
        (case when fylx in(1,2,3) and wsf!=0 then round(nvl(a.wsfdj,0)*nvl(a.jt${tag}sl,0),2) else 0 end) wsf,
        (case when fylx in(1,2,5) and szyf!=0 then round(nvl(a.szyf_dj,0)*nvl(a.jt${tag}sl,0),2) else 0 end) szyf,
        <if test='tag!=null and tag=="1"'>
            nvl(a.znj,0) znj,
            nvl(a.ljf,0) ljf,
            nvl(a.ysecjyf, 0) ecjyf,
            nvl(a.ysfjf, 0)fjf,
            nvl(a.ysqt, 0) qtf
        </if>
        <if test='tag!=null and tag!="1"'>
            0 znj,
            0 ljf,
            0 ecjyf,
            0 fjf,
            0 qtf
        </if>
        FROM yy_qfmx a
        left join bm_sj b on a.sjbm=b.code
        left join yh_khjbxx c on a.yhbh=c.yhbh
        WHERE b.jtbz='1'
        <if test='tag!=null and tag!="1"'>
            AND a.jt${tag}sl!=0
        </if>
        <choose>
            <when test='sjbm!=null and sjbm!=""'>
                AND a.sjbm=#{sjbm}
            </when>
            <otherwise>
                AND a.sjbm >' '
            </otherwise>
        </choose>
        <if test="filter.chbz!=null">
            <choose>
                <when test="filter.chbz.indexOf(',')!=-1">
                    AND a.chbz in
                    <foreach item="item" index="index" collection="filter.chbz.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.chbz=#{filter.chbz}
                </otherwise>
            </choose>
        </if>
        <if test="filter.sfbz!=null">
            AND a.sfbz=#{filter.sfbz}
        </if>
        <if test="filter.cwrq!=null">
            AND a.cwrq=#{filter.cwrq}
        </if>
        <if test="filter.ssgs!=null">
            AND c.ssgs=#{filter.ssgs}
        </if>
        <if test="filter.sscbpq!=null">
            AND c.sscbpq=#{filter.sscbpq}
        </if>
        <if test="filter.sscbb!=null">
            AND c.sscbb=#{filter.sscbb}
        </if>
        <if test="filter.cbrybm!=null">
            AND a.cbrybm=#{filter.cbrybm}
        </if>
        <if test="filter.nf!=null">
            AND a.cwrq like '${filter.nf}%'
        </if>
        <if test="filter.yhbh!=null">
            AND a.yhbh =#{filter.yhbh}
        </if>
        <if test="filter.yhbhStart!=null">
            and a.yhbh >= #{filter.yhbhStart}
        </if>
        <if test="filter.yhbhEnd!=null">
            and a.yhbh &lt;= #{filter.yhbhEnd}
        </if>
        <if test="filter.jsrqStart!=null and filter.jsrqEnd!=null">
            <![CDATA[ and a.jsrq >=#{filter.jsrqStart}+0  and a.jsrq <= #{filter.jsrqEnd}+0]]>
        </if>
        <if test="filter.jzEnd!=null">
            <![CDATA[ and (sfbz='0' or (sfbz='1' and sfrq>#{filter.jzEnd}+0)) ]]>
        </if>
        <if test="filter.qyfzr!=null">
            AND c.sscbb in(select cbbbm from bm_cbb where qyfzr=#{filter.qyfzr})
        </if>
        <if test="filter.cwrqStart!=null and filter.cwrqEnd!=null">
            AND a.cwrq BETWEEN #{filter.cwrqStart} AND #{filter.cwrqEnd}
        </if>
        <if test="filter.jyType!=null">
            AND a.jy_type=#{filter.jyType}
        </if>
        <if test="filter.ysyfStart!=null and filter.ysyfEnd!=null">
            AND a.ysyf BETWEEN #{filter.ysyfStart} AND #{filter.ysyfEnd}
        </if>
        <if test="filter.yslx!=null">
            AND b.yslx=#{filter.yslx}
        </if>
        <if test="filter.dxh!=null">
            <if test='filter.dxh=="0"'>
                AND a.khbm is null
            </if>
            <if test='filter.dxh=="1"'>
                AND a.khbm >' '
            </if>
        </if>
        union all
        SELECT ${cwrq}a.sjbm1 sjbm,sfbz,
        case
        when nvl(sj1_jt${tag}sj,0)!=0 then nvl(sj1_jt${tag}sj,0)+nvl(a.wsfdj1,0)+nvl(a.szyf_dj1,0)
        else 0 end sj,
        (case
        when fylx in(1,2) and sj1_jt${tag}sf!=0 then nvl(a.sj1_jt${tag}sl,0)
        else
        case when fylx in(1,2) and sfbz='4' then nvl(a.sj1_jt${tag}sl,0) else 0 end
        end) sl,
        round(nvl(a.sj1_jt${tag}sf,0),2) sf,
        (case when fylx in(1,2,3) and wsf1!=0 then round(nvl(a.wsfdj1,0)*nvl(a.sj1_jt${tag}sl,0),2) else 0 end) wsf,
        (case when fylx in(1,2,5) and szyf1!=0 then round(nvl(a.szyf_dj1,0)*nvl(a.sj1_jt${tag}sl,0),2) else 0 end) szyf,
        <if test='tag!=null and tag=="1"'>
            nvl(a.znj,0) znj,
            nvl(a.ljf1,0) ljf,
            nvl(a.ysecjyf, 0) ecjyf,
            nvl(a.ysfjf, 0)fjf,
            nvl(a.ysqt, 0) qtf
        </if>
        <if test='tag!=null and tag!="1"'>
            0 znj,
            0 ljf,
            0 ecjyf,
            0 fjf,
            0 qtf
        </if>
        FROM yy_qfmx a
        left join bm_sj b on a.sjbm1=b.code
        left join yh_khjbxx c on a.yhbh=c.yhbh
        WHERE b.jtbz='1'
        <if test='tag!=null and tag!="1"'>
            AND a.sj1_jt${tag}sl!=0
        </if>
        <choose>
            <when test='sjbm!=null and sjbm!=""'>
                AND a.sjbm1=#{sjbm}
            </when>
            <otherwise>
                AND a.sjbm1 >' '
            </otherwise>
        </choose>
        <if test="filter.chbz!=null">
            <choose>
                <when test="filter.chbz.indexOf(',')!=-1">
                    AND a.chbz in
                    <foreach item="item" index="index" collection="filter.chbz.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.chbz=#{filter.chbz}
                </otherwise>
            </choose>
        </if>
        <if test="filter.sfbz!=null">
            AND a.sfbz=#{filter.sfbz}
        </if>
        <if test="filter.cwrq!=null">
            AND a.cwrq=#{filter.cwrq}
        </if>
        <if test="filter.ssgs!=null">
            AND c.ssgs=#{filter.ssgs}
        </if>
        <if test="filter.sscbpq!=null">
            AND c.sscbpq=#{filter.sscbpq}
        </if>
        <if test="filter.sscbb!=null">
            AND c.sscbb=#{filter.sscbb}
        </if>
        <if test="filter.cbrybm!=null">
            AND a.cbrybm=#{filter.cbrybm}
        </if>
        <if test="filter.nf!=null">
            AND a.cwrq like '${filter.nf}%'
        </if>
        <if test="filter.yhbh!=null">
            AND a.yhbh =#{filter.yhbh}
        </if>
        <if test="filter.yhbhStart!=null">
            and a.yhbh >= #{filter.yhbhStart}
        </if>
        <if test="filter.yhbhEnd!=null">
            and a.yhbh &lt;= #{filter.yhbhEnd}
        </if>
        <if test="filter.jsrqStart!=null and filter.jsrqEnd!=null">
            <![CDATA[ and a.jsrq >=#{filter.jsrqStart}+0  and a.jsrq <= #{filter.jsrqEnd}+0]]>
        </if>
        <if test="filter.jzEnd!=null">
            <![CDATA[ and (sfbz='0' or (sfbz='1' and sfrq>#{filter.jzEnd}+0)) ]]>
        </if>
        <if test="filter.qyfzr!=null">
            AND c.sscbb in(select cbbbm from bm_cbb where qyfzr=#{filter.qyfzr})
        </if>
        <if test="filter.cwrqStart!=null and filter.cwrqEnd!=null">
            AND a.cwrq BETWEEN #{filter.cwrqStart} AND #{filter.cwrqEnd}
        </if>
        <if test="filter.jyType!=null">
            AND a.jy_type=#{filter.jyType}
        </if>
        <if test="filter.ysyfStart!=null and filter.ysyfEnd!=null">
            AND a.ysyf BETWEEN #{filter.ysyfStart} AND #{filter.ysyfEnd}
        </if>
        <if test="filter.yslx!=null">
            AND b.yslx=#{filter.yslx}
        </if>
        <if test="filter.dxh!=null">
            <if test='filter.dxh=="0"'>
                AND a.khbm is null
            </if>
            <if test='filter.dxh=="1"'>
                AND a.khbm >' '
            </if>
        </if>
        union all
        SELECT ${cwrq}a.sjbm2 sjbm,sfbz,
        case
        when nvl(sj2_jt${tag}sj,0)!=0 then nvl(sj2_jt${tag}sj,0)+nvl(a.wsfdj2,0)+nvl(a.szyf_dj2,0)
        else 0 end sj,
        (case
        when fylx in(1,2) and sj2_jt${tag}sf!=0 then nvl(a.sj2_jt${tag}sl,0)
        else
        case when fylx in(1,2) and sfbz='4' then nvl(a.sj2_jt${tag}sl,0) else 0 end
        end) sl,
        round(nvl(a.sj2_jt${tag}sf,0),2) sf,
        (case when fylx in(1,2,3) and wsf2!=0 then round(nvl(a.wsfdj2,0)*nvl(a.sj2_jt${tag}sl,0),2) else 0 end) wsf,
        (case when fylx in(1,2,5) and szyf2!=0 then round(nvl(a.szyf_dj2,0)*nvl(a.sj2_jt${tag}sl,0),2) else 0 end) szyf,
        <if test='tag!=null and tag=="1"'>
            nvl(a.znj,0) znj,
            nvl(a.ljf2,0) ljf,
            nvl(a.ysecjyf, 0) ecjyf,
            nvl(a.ysfjf, 0)fjf,
            nvl(a.ysqt, 0) qtf
        </if>
        <if test='tag!=null and tag!="1"'>
            0 znj,
            0 ljf,
            0 ecjyf,
            0 fjf,
            0 qtf
        </if>
        FROM yy_qfmx a
        left join bm_sj b on a.sjbm2=b.code
        left join yh_khjbxx c on a.yhbh=c.yhbh
        WHERE b.jtbz='1'
        <if test='tag!=null and tag!="1"'>
            AND a.sj2_jt${tag}sl!=0
        </if>
        <choose>
            <when test='sjbm!=null and sjbm!=""'>
                AND a.sjbm2=#{sjbm}
            </when>
            <otherwise>
                AND a.sjbm2 >' '
            </otherwise>
        </choose>
        <if test="filter.chbz!=null">
            <choose>
                <when test="filter.chbz.indexOf(',')!=-1">
                    AND a.chbz in
                    <foreach item="item" index="index" collection="filter.chbz.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.chbz=#{filter.chbz}
                </otherwise>
            </choose>
        </if>
        <if test="filter.sfbz!=null">
            AND a.sfbz=#{filter.sfbz}
        </if>
        <if test="filter.cwrq!=null">
            AND a.cwrq=#{filter.cwrq}
        </if>
        <if test="filter.ssgs!=null">
            AND c.ssgs=#{filter.ssgs}
        </if>
        <if test="filter.sscbpq!=null">
            AND c.sscbpq=#{filter.sscbpq}
        </if>
        <if test="filter.sscbb!=null">
            AND c.sscbb=#{filter.sscbb}
        </if>
        <if test="filter.cbrybm!=null">
            AND a.cbrybm=#{filter.cbrybm}
        </if>
        <if test="filter.nf!=null">
            AND a.cwrq like '${filter.nf}%'
        </if>
        <if test="filter.yhbh!=null">
            AND a.yhbh =#{filter.yhbh}
        </if>
        <if test="filter.yhbhStart!=null">
            and a.yhbh >= #{filter.yhbhStart}
        </if>
        <if test="filter.yhbhEnd!=null">
            and a.yhbh &lt;= #{filter.yhbhEnd}
        </if>
        <if test="filter.jsrqStart!=null and filter.jsrqEnd!=null">
            <![CDATA[ and a.jsrq >=#{filter.jsrqStart}+0  and a.jsrq <= #{filter.jsrqEnd}+0]]>
        </if>
        <if test="filter.jzEnd!=null">
            <![CDATA[ and (sfbz='0' or (sfbz='1' and sfrq>#{filter.jzEnd}+0)) ]]>
        </if>
        <if test="filter.qyfzr!=null">
            AND c.sscbb in(select cbbbm from bm_cbb where qyfzr=#{filter.qyfzr})
        </if>
        <if test="filter.cwrqStart!=null and filter.cwrqEnd!=null">
            AND a.cwrq BETWEEN #{filter.cwrqStart} AND #{filter.cwrqEnd}
        </if>
        <if test="filter.jyType!=null">
            AND a.jy_type=#{filter.jyType}
        </if>
        <if test="filter.ysyfStart!=null and filter.ysyfEnd!=null">
            AND a.ysyf BETWEEN #{filter.ysyfStart} AND #{filter.ysyfEnd}
        </if>
        <if test="filter.yslx!=null">
            AND b.yslx=#{filter.yslx}
        </if>
        <if test="filter.dxh!=null">
            <if test='filter.dxh=="0"'>
                AND a.khbm is null
            </if>
            <if test='filter.dxh=="1"'>
                AND a.khbm >' '
            </if>
        </if>
        )
        GROUP BY ${cwrq}sjbm,sj,sfbz
    </select>

    <select id="bcStat" resultType="java.math.BigDecimal">
        SELECT
        SUM(a.ysje) ysje
        FROM
        yy_qfmx a
        WHERE
        a.sfbc = 1
        AND
        a.sfbz='0'
        <if test="filter.cwrq!=null">
            AND a.cwrq=#{filter.cwrq}
        </if>
        <if test="filter.ssgs!=null">
            AND a.ssgs=#{filter.ssgs}
        </if>
        <if test="filter.sscbpq!=null">
            AND a.sscbpq=#{filter.sscbpq}
        </if>
        <if test="filter.sscbb!=null">
            <choose>
                <when test="filter.sscbb.indexOf(',')!=-1">
                    AND a.sscbb in
                    <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.sscbb=#{filter.sscbb}
                </otherwise>
            </choose>
        </if>
        <if test="filter.nf!=null">
            AND a.cwrq like '${filter.nf}%'
        </if>
        <if test="filter.yhbh!=null">
            AND a.yhbh =#{filter.yhbh}
        </if>
        <if test="filter.yhbhStart!=null">
            and a.yhbh >= #{filter.yhbhStart}
        </if>
        <if test="filter.yhbhEnd!=null">
            and a.yhbh &lt;= #{filter.yhbhEnd}
        </if>
        <if test="filter.cbrybm!=null">
            AND a.cbrybm=#{filter.cbrybm}
        </if>
        <if test="filter.jsrqStart!=null and filter.jsrqEnd!=null">
            AND (a.jsrq BETWEEN #{filter.jsrqStart} AND #{filter.jsrqEnd})
        </if>
        <if test="filter.jzEnd!=null">
            <![CDATA[  and (sfbz='0' or (sfbz='1' and sfrq>#{filter.jzEnd}+0)) ]]>
        </if>
        <if test="filter.qyfzr!=null">
            AND a.sscbb in(select cbbbm from bm_cbb where qyfzr=#{filter.qyfzr})
        </if>
        <if test="filter.cwrqStart!=null and filter.cwrqEnd!=null">
            AND a.cwrq BETWEEN #{filter.cwrqStart} AND #{filter.cwrqEnd}
        </if>
        <if test="filter.ysyfStart!=null and filter.ysyfEnd!=null">
            AND a.ysyf BETWEEN #{filter.ysyfStart} AND #{filter.ysyfEnd}
        </if>
        <if test="filter.jyType!=null">
            AND a.jy_type=#{filter.jyType}
        </if>
        <if test="filter.dxh!=null">
            <if test='filter.dxh=="0"'>
                AND a.khbm is null
            </if>
            <if test='filter.dxh=="1"'>
                AND a.khbm >' '
            </if>
        </if>
    </select>

    <select id="qtStat" resultType="java.math.BigDecimal">
        SELECT
        <if test="flag=='znj'">
            SUM(a.znj) znj
        </if>
        FROM YY_QFMX a
        WHERE 1=1
        <if test="filter.cwrq!=null">
            AND a.cwrq=#{filter.cwrq}
        </if>
        <if test="filter.ssgs!=null">
            AND a.ssgs=#{filter.ssgs}
        </if>
        <if test="filter.sscbpq!=null">
            AND a.sscbpq=#{filter.sscbpq}
        </if>
        <if test="filter.sscbb!=null">
            <choose>
                <when test="filter.sscbb.indexOf(',')!=-1">
                    AND a.sscbb in
                    <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.sscbb=#{filter.sscbb}
                </otherwise>
            </choose>
        </if>
        <if test="filter.nf!=null">
            AND a.cwrq like '${filter.nf}%'
        </if>
        <if test="filter.yhbh!=null">
            AND a.yhbh =#{filter.yhbh}
        </if>
        <if test="filter.yhbhStart!=null">
            and a.yhbh >= #{filter.yhbhStart}
        </if>
        <if test="filter.yhbhEnd!=null">
            and a.yhbh &lt;= #{filter.yhbhEnd}
        </if>
        <if test="filter.cbrybm!=null">
            AND a.cbrybm=#{filter.cbrybm}
        </if>
        <if test="filter.jsrqStart!=null and filter.jsrqEnd!=null">
            AND (a.jsrq BETWEEN #{filter.jsrqStart} AND #{filter.jsrqEnd})
        </if>
        <if test="filter.jzEnd!=null">
            <![CDATA[  and (sfbz='0' or (sfbz='1' and sfrq>#{filter.jzEnd}+0)) ]]>
        </if>
        <if test="filter.qyfzr!=null">
            AND a.sscbb in(select cbbbm from bm_cbb where qyfzr=#{filter.qyfzr})
        </if>
        <if test="filter.cwrqStart!=null and filter.cwrqEnd!=null">
            AND a.cwrq BETWEEN #{filter.cwrqStart} AND #{filter.cwrqEnd}
        </if>
        <if test="filter.ysyfStart!=null and filter.ysyfEnd!=null">
            AND a.ysyf BETWEEN #{filter.ysyfStart} AND #{filter.ysyfEnd}
        </if>
        <if test="filter.dxh!=null">
            <if test='filter.dxh=="0"'>
                AND a.khbm is null
            </if>
            <if test='filter.dxh=="1"'>
                AND a.khbm >' '
            </if>
        </if>
    </select>
    <select id="stat" resultType="com.tofly.feesapi.sfgl.entity.vo.YyStat">
        SELECT
        SUM(case when a.fylx='1' or a.fylx='2' then nvl(yssl,0) else 0 end) sl,
        SUM(nvl(a.yssf,0)) sf,
        SUM(nvl(a.yswsf,0)) wsf,
        SUM(nvl(a.ysljf,0)) ljf,
        sum(nvl(a.ysszyf,0)) szyf,
        SUM(nvl(a.ysje,0)) hj,
        SUM(nvl(a.ysecjyf,0)) ecjyf,
        SUM(nvl(a.ysfjf,0)) fjf,
        SUM(nvl(a.ysqt,0)) xfsf
        FROM yy_qfmx a
        WHERE 1=1 AND a.chbz='0'
        <if test="filter.sfbz!=null">
            AND a.sfbz=#{filter.sfbz}
        </if>
        <if test="filter.cwrq!=null">
            AND a.cwrq=#{filter.cwrq}
        </if>
        <if test="filter.ssgs!=null">
            AND a.ssgs=#{filter.ssgs}
        </if>
        <if test="filter.nf!=null">
            AND a.cwrq like '${filter.nf}%'
        </if>
        <if test="filter.yhbh!=null">
            AND a.yhbh like '%${filter.yhbh}%'
        </if>
        <if test="filter.yhbhStart!=null">
            and a.yhbh >= #{filter.yhbhStart}
        </if>
        <if test="filter.yhbhEnd!=null">
            and a.yhbh &lt;= #{filter.yhbhEnd}
        </if>
        <if test="filter.jsrqStart!=null and filter.jsrqEnd!=null">
            AND (a.jsrq BETWEEN #{filter.jsrqStart} AND #{filter.jsrqEnd})
        </if>
        <if test="filter.cwrqStart!=null and filter.cwrqEnd!=null">
            AND a.cwrq BETWEEN #{filter.cwrqStart} AND #{filter.cwrqEnd}
        </if>
        <if test="filter.ysyfStart!=null and filter.ysyfEnd!=null">
            AND a.ysyf BETWEEN #{filter.ysyfStart} AND #{filter.ysyfEnd}
        </if>
        <if test="filter.dxh!=null">
            <if test='filter.dxh=="0"'>
                AND a.khbm is null
            </if>
            <if test='filter.dxh=="1"'>
                AND a.khbm >' '
            </if>
        </if>
    </select>
    <select id="getqfsjmx" resultType="java.util.Map">
        with T as
                 (select 0 xh, sjbm, sj, yssl, yssf, '总水价' as sfname
                  from yy_qfmx
                  where id = #{qfid}
                  union
                  select 1 xh, sjbm, sj, sl, sf, '水价1'
                  from yy_qfmx
                  where id = #{qfid}
                    and sjbm > ' '
                    and sl > 0
                  union
                  select 2 xh, sjbm, JT1SJ, JT1SL, JT1SF, '水价1阶梯1'
                  from yy_qfmx
                  where id = #{qfid}
                    and sjbm > ' '
                    and JT1SL > 0
                  union
                  select 3 xh, sjbm, JT2SJ, JT2SL, JT2SF, '水价1阶梯2'
                  from yy_qfmx
                  where id = #{qfid}
                    and sjbm > ' '
                    and JT2SL > 0
                  union
                  select 4 xh, sjbm, JT3SJ, JT3SL, JT3SF, '水价1阶梯3'
                  from yy_qfmx
                  where id = #{qfid}
                    and sjbm > ' '
                    and JT3SL > 0
                  union
                  select 5 xh, sjbm1, sj1, sl1, sf1, '水价2'
                  from yy_qfmx
                  where id = #{qfid}
                    and sjbm1 > ' '
                    and sl1 > 0
                  union
                  select 6 xh, sjbm1, sj1_jt1sj, sj1_jt1sl, sj1_jt1sf, '水价2阶梯1'
                  from yy_qfmx
                  where id = #{qfid}
                    and sjbm1 > ' '
                    and sj1_jt1sl > 0
                  union
                  select 7 xh, sjbm1, sj1_jt2sj, sj1_jt2sl, sj1_jt2sf, '水价2阶梯2'
                  from yy_qfmx
                  where id = #{qfid}
                    and sjbm1 > ' '
                    and sj1_jt2sl > 0
                  union
                  select 8 xh, sjbm1, sj1_jt3sj, sj1_jt3sl, sj1_jt3sf, '水价2阶梯3'
                  from yy_qfmx
                  where id = #{qfid}
                    and sjbm1 > ' '
                    and sj1_jt3sl > 0
                  union
                  select 9 xh, sjbm2, sj2, sl2, sf2, '水价3'
                  from yy_qfmx
                  where id = #{qfid}
                    and sjbm2 > ' '
                    and sl2 > 0
                  union
                  select 10 xh, sjbm2, sj2_jt1sj, sj2_jt1sl, sj2_jt1sf, '水价3阶梯1'
                  from yy_qfmx
                  where id = #{qfid}
                    and sjbm2 > ' '
                    and sj2_jt1sf > 0
                  union
                  select 11 xh, sjbm2, sj2_jt2sj, sj2_jt2sl, sj2_jt2sf, '水价3阶梯2'
                  from yy_qfmx
                  where id = #{qfid}
                    and sjbm2 > ' '
                    and sj2_jt2sf > 0
                  union
                  select 12 xh, sjbm2, sj2_jt3sj, sj2_jt3sl, sj2_jt3sf, '水价3阶梯3'
                  from yy_qfmx
                  where id = #{qfid}
                    and sjbm2 > ' '
                    and sj2_jt3sl > 0)
        select bm_sj.name, T.*
        from T,
             bm_sj
        where T.sjbm = bm_sj.code
        order by T.xh
    </select>

    <select id="fytzpage" resultType="com.tofly.feesapi.sfgl.entity.vo.YyQfmxFytzSelect">
        select
        yhbh,
        fylxmc fymc,
        cwrq,
        cbrq,
        decode(fylx,'6','',sl)fysl,
        (case
        when fylx in ('2','15','16') then sj
        when fylx in ('3','14') then wsfdj
        when fylx in ('4') then ljfdj
        when fylx in ('5','13') then szyf_dj
        when fylx in ('6') then 0
        end) fydj,
        ysje fyje,
        lrryxm lrry,
        lrrq
        from yy_qfmx
        where sfbc = '1'
        <if test="filter.yhbh!=null">
            and yhbh=#{filter.yhbh}
        </if>
        <if test="filter.gdzt!=null">
            and gdzt=#{filter.gdzt}
        </if>
    </select>

    <select id="fytzpage2" resultType="java.util.Map">
        select
        bgqjson,
        id as logid,
        bgryxm as lrry,
        bgdate lrrq,
        gdbh,
        gdzt
        from yy_bg_log
        where 1=1
        and bglx='2'
        <if test="filter.yhbh!=null">
            and to_char(regexp_substr(bgqjson, '"yhbh":"([^"]+)"', 1, 1, NULL, 1)) = #{filter.yhbh}
        </if>
        <if test="filter.gdzt!=null">
            and gdzt=#{filter.gdzt}
        </if>
        <if test="filter.ywid!=null">
            and id=#{filter.ywid}
        </if>
    </select>

    <select id="qftjabs" resultType="com.tofly.feesapi.mxcx.entity.vo.QftjabsSelect">
        SELECT
        a.yhbh,a.htbh,a.yhxm,a.yhdz,
        a.lxdh,a.ysxz,a.cwrqstart,
        a.cwrqend,a.cwrqqzyf,
        a.qd,a.zd,a.qfbs,a.qfsl,a.sf,
        a.wsf,a.szyf,a.ecjyf,a.qtf,a.fjf,
        a.ljf,a.wyj,a.QFJE,a.qfzje
        FROM (
        SELECT
        a.yhbh,
        b.htbh,
        b.yhxm,
        b.yhdz,
        b.lxdh,
        c.ysxz,
        min(a.CWRQ) cwrqstart,
        max(a.CWRQ) cwrqend,
        min(a.CWRQ)||'-' ||max(a.CWRQ) cwrqqzyf,
        min(a.SCDS) qd,
        max(a.BCDS) zd,
        count(a.ID) qfbs,
        sum(case when a.fylx='1' or a.fylx='2' then a.yssl else 0 end) qfsl,
        sum(nvl(YSSF,0)) sf,
        sum(nvl(YSWSF,0)) wsf,
        sum(nvl(YSSZYF,0)) szyf,
        sum(nvl(YSECJYF,0)) ecjyf,
        sum(nvl(YSQT,0)) qtf,
        sum(nvl(YSFJF,0)) fjf,
        sum(nvl(YSLJF,0)) ljf,
        sum(nvl(ZNJ,0)) wyj,
        sum(nvl(YSJE,0)) QFJE,
        sum(nvl(YSJE,0))+sum(nvl(ZNJ,0)) qfzje
        FROM
        YY_QFMX a
        LEFT JOIN
        YH_KHJBXX b ON a.YHBH = b.YHBH
        LEFT JOIN (
        select
        s.CODE as sjbm,s.YSLX, y.NAME as ysxz
        FROM
        BM_SJ s
        LEFT JOIN
        BM_YSLX y
        ON s.YSLX = y.CODE
        ) c ON c.sjbm = a.sjbm
        <include refid="qftjabs_wheresql"></include>
        group by a.yhbh, b.htbh,b.yhxm,b.yhdz,b.lxdh, c.ysxz
        <if test="filter.qfbs!=null">
            <![CDATA[ having count(a.ID)>= #{filter.qfbs}]]>
        </if>
        ) a
        <include refid="qftjabs_wheresql2"></include>
    </select>

    <select id="qftjabsCount" resultType="com.tofly.feesapi.mxcx.entity.vo.QftjabsSelectCount">
        select
        sum(nvl(yssl,0)) zyssl,
        sum(nvl(yssf,0)) zyssf,
        sum(nvl(yswsf,0)) zyswsf,
        sum(nvl(ysljf,0)) zysljf,
        sum(nvl(ysszyf,0)) zysszyf,
        sum(nvl(qfje,0)) zqfje,
        sum(nvl(znj,0)) zznj,
        sum(nvl(qfzje,0)) zqfzje
        from (
        select
        sum(case when a.fylx='1' or a.fylx='2' then nvl(a.yssl,0)else 0 end) yssl,
        sum(nvl(yssf,0)) yssf,
        sum(nvl(yswsf,0)) yswsf,
        sum(nvl(ysljf,0)) ysljf,
        sum(nvl(ysszyf,0)) ysszyf,
        sum(nvl(ysje,0)) qfje,
        sum(nvl(znj,0)) znj,
        sum(nvl(ysje,0))+sum(nvl(znj,0)) qfzje
        from
        YY_QFMX a
        left join
        YH_KHJBXX b on a.YHBH = b.YHBH
        left join
        ( select
        s.CODE as sjbm,
        s.yslx,
        y.NAME as ysxz
        from
        BM_SJ s
        left join
        BM_YSLX y on s.YSLX = y.CODE
        ) c on c.sjbm = a.sjbm
        <include refid="qftjabs_wheresql"></include>
        group by a.yhbh, b.htbh, b.yhxm, b.yhdz,b.lxdh, c.ysxz, a.cbryxm
        <if test="filter.qfbs!=null">
            <![CDATA[ having count(a.ID)>= #{filter.qfbs}]]>
        </if>
        )a
        <include refid="qftjabs_wheresql2"></include>
    </select>

    <sql id="qftjabs_wheresql">
        where a.SFBZ = '0' AND a.sfbc != 3
        <if test="filter.chbz!=null">
            <choose>
                <when test="filter.chbz.indexOf(',')!=-1">
                    AND a.chbz in
                    <foreach item="item" index="index" collection="filter.chbz.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.chbz=#{filter.chbz}
                </otherwise>
            </choose>
        </if>
        <if test="filter.searchText!=null">
            and (a.yhbh like '%${filter.searchText}%'
            or b.HTBH like '%${filter.searchText}%'
            or b.YHXM like '%${filter.searchText}%'
            or b.YHDZ like '%${filter.searchText}%'
            or b.LXDH like '%${filter.searchText}%'
            or c.ysxz like '%${filter.searchText}%'
            or a.CBRYXM like '%${filter.searchText}%'
            )
        </if>
        <if test="filter.yhbh!=null">
            and a.YHBH=#{filter.yhbh}
        </if>
        <if test="filter.ssgs!=null">
            and a.SSGS=#{filter.ssgs}
        </if>
        <if test="filter.sscbpq!=null">
            and a.SSCBPQ=#{filter.sscbpq}
        </if>
        <if test="filter.sscbb!=null">
            <choose>
                <when test="filter.sscbb.indexOf(',')!=-1">
                    AND a.sscbb in
                    <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.sscbb=#{filter.sscbb}
                </otherwise>
            </choose>
        </if>
        <if test="filter.cbrqStart!=null and filter.cbrqEnd!=null">
            and a.CBRQ BETWEEN #{filter.cbrqStart} AND #{filter.cbrqEnd}
        </if>
        <if test="filter.cwrqStart!=null and filter.cwrqEnd!=null">
            and a.cwrq BETWEEN #{filter.cwrqStart} AND #{filter.cwrqEnd}
        </if>
        <if test="filter.jsrqStart!=null and filter.jsrqEnd!=null">
            and a.jsrq BETWEEN #{filter.jsrqStart} AND #{filter.jsrqEnd}
        </if>
        <if test="filter.jzEnd!=null">
            <![CDATA[ and (sfbz='0' or (sfbz='1' and a.sfrq>#{filter.jzEnd}+0)) ]]>
        </if>
        <if test="filter.yhbhStart!=null">
            and a.yhbh >= #{filter.yhbhStart}
        </if>
        <if test="filter.yhbhEnd!=null">
            and a.yhbh &lt;= #{filter.yhbhEnd}
        </if>
        <if test="filter.yslx!=null">
            and c.yslx = #{filter.yslx}
        </if>
        <if test="filter.userId != null">
            AND a.sscbb in (select cbbbm from yh_sjqx where user_id=#{filter.userId})
        </if>
    </sql>

    <sql id="qftjabs_wheresql2">
        <where>
            <if test="filter.qfjeStart!=null">
                and a.qfzje >= #{filter.qfjeStart}
            </if>
            <if test="filter.qfjeEnd!=null">
                and a.qfzje &lt;= #{filter.qfjeEnd}
            </if>
            <if test="filter.slStart!=null">
                and a.qfsl >= #{filter.slStart}
            </if>
            <if test="filter.slEnd!=null">
                and a.qfsl &lt;= #{filter.slEnd}
            </if>
        </where>
    </sql>

    <select id="qftj" resultType="com.tofly.feesapi.mxcx.entity.vo.QftjSelect">
        SELECT

        b.yhbh,
        b.yhxm,
        b.yhdz,
        b.lxdh,
        a.cwrq,
        a.scds,
        a.bcds,
        a.ysyf,
        a.khbm,
        nvl(a.sj, 0) sj,
        (case when a.fylx='1' or a.fylx='2' then a.yssl else 0 end) yssl,
        nvl(a.yssf, 0) yssf,
        nvl(a.yswsf, 0) yswsf,
        nvl(a.ysszyf, 0) ysszyf,
        nvl(a.ysljf, 0) ysljf,
        nvl(a.znj, 0) znj,
        nvl(a.ysje, 0) qfje,
        nvl(a.ysje, 0) + nvl(a.znj, 0) qfzje,
        nvl(a.ysecjyf, 0) ysecjyf,
        nvl(a.ysfjf, 0) ysfjf,
        nvl(a.ysqt, 0) ysqt,
        a.cbrq,
        a.cbryxm cbry,
        a.qyfzrxm qyfzr,
        (case when sfbz='0' then '未收费'
        else '已收费' end )sfzt,
        a.chbz,
        d.ysxzbm ysxz,
        d.name ysxzmc,
        d.sjmc
        FROM yy_qfmx a
        LEFT JOIN yh_khjbxx b ON a.yhbh=b.yhbh
        LEFT JOIN (
        select sj.code, sj.name as sjmc, yslx.name, yslx.code as ysxzbm FROM bm_sj sj
        LEFT JOIN bm_yslx yslx ON sj.yslx=yslx.code
        ) d ON a.sjbm=d.code
        <include refid="qftj_wheresql"></include>
        order by a.yhbh,a.cwrq asc
    </select>


    <select id="qftjCount" resultType="com.tofly.feesapi.mxcx.entity.vo.QftjSelectCount">
        select
        sum(case when a.fylx='1' or a.fylx='2' then a.yssl else 0 end) zyssl,
        sum(nvl(yssf,0)) zyssf,
        sum(nvl(yswsf,0)) zyswsf,
        sum(nvl(ysljf,0)) zysljf,
        sum(nvl(ysszyf,0)) zysszyf,
        sum(nvl(ysje,0)) zqfje,
        sum(nvl(znj,0)) zznj,
        sum(nvl(ysje,0))+sum(nvl(znj,0)) zqfzje,
        sum(nvl(ysecjyf,0)) zecjyf,
        sum(nvl(ysfjf,0)) zfjf,
        sum(nvl(ysqt,0)) zqtf
        from yy_qfmx a
        left join yh_khjbxx b on a.yhbh=b.yhbh
        left join (
        select sj.code, yslx.code as ysxzbm from bm_sj sj
        left join bm_yslx yslx on sj.yslx=yslx.code
        ) d on a.sjbm=d.code
        <include refid="qftj_wheresql"></include>
    </select>

    <sql id="qftj_wheresql">
        <where>
            <if test="filter.searchText!=null">
                and (UPPER(b.YHBH) like '%${filter.searchText.toUpperCase()}%'
                or a.YHXM like '%${filter.searchText}%'
                or b.YHDZ like '%${filter.searchText}%'
                or b.LXDH like '%${filter.searchText}%'
                or a.CBRYXM like '%${filter.searchText}%'
                )
            </if>
            <if test="filter.yhbh!=null">
                and a.YHBH=#{filter.yhbh}
            </if>
            <if test="filter.khbm!=null">
                and a.khbm=#{filter.khbm}
            </if>
            <if test="filter.ssgs!=null">
                and a.SSGS=#{filter.ssgs}
            </if>
            <if test="filter.sscbpq!=null">
                and a.SSCBPQ=#{filter.sscbpq}
            </if>
            <if test="filter.sscbb!=null">
                <choose>
                    <when test="filter.sscbb.indexOf(',')!=-1">
                        AND a.sscbb in
                        <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                                 close=")">
                            #{item}
                        </foreach>
                    </when>
                    <otherwise>
                        AND a.sscbb=#{filter.sscbb}
                    </otherwise>
                </choose>
            </if>
            <if test="filter.cbrqStart!=null and filter.cbrqEnd!=null">
                and a.CBRQ BETWEEN #{filter.cbrqStart} AND #{filter.cbrqEnd}
            </if>
            <if test="filter.sfbz!=null">
                and a.SFBZ=#{filter.sfbz}
            </if>
            <if test="filter.jzEnd!=null">
                <![CDATA[ and (sfbz='0' or (sfbz='1' and sfrq>#{filter.jzEnd}+0)) ]]>
            </if>
            <if test="filter.jsrqStart!=null and filter.jsrqEnd!=null">
                <![CDATA[ and a.jsrq >=#{filter.jsrqStart}+0  and a.jsrq <= #{filter.jsrqEnd}+0]]>
            </if>
            <if test="filter.qfjeStart!=null">
                and (a.ysje+a.znj) >= #{filter.qfjeStart}
            </if>
            <if test="filter.qfjeEnd != null">
                and (a.ysje+a.znj) &lt;= #{filter.qfjeEnd}
            </if>
            <if test="filter.slStart!=null">
                and a.yssl >= #{filter.slStart}
            </if>
            <if test="filter.slEnd!=null">
                and a.yssl &lt;= #{filter.slEnd}
            </if>
            <if test="filter.lrry!=null">
                and a.lrrybm=#{filter.cby}
            </if>
            <if test="filter.cby!=null">
                and a.cbrybm=#{filter.cby}
            </if>
            <if test="filter.qyfzr!=null">
                and a.sscbb in(select cbbbm from bm_cbb where qyfzr=#{filter.qyfzr})
            </if>
            <if test="filter.yslx!=null">
                and d.ysxzbm = #{filter.yslx}
            </if>
            <if test="filter.yhxmjm!=null">
                AND get_pyjm(a.yhxm) like '%'||UPPER(#{filter.yhxmjm})||'%'
            </if>
            <if test="filter.userId != null">
                AND a.sscbb in (select cbbbm from yh_sjqx where user_id=#{filter.userId})
            </if>
            <if test="filter.dxh!=null">
                <if test='filter.dxh=="0"'>
                    AND a.khbm is null
                </if>
                <if test='filter.dxh=="1"'>
                    AND a.khbm >' '
                </if>
            </if>
        </where>
    </sql>
    <select id="qftjhj" resultType="java.util.Map">
        select
        sum(case when a.fylx='1' or a.fylx='2' then a.yssl else 0 end) sl,
        nvl(sum(nvl(a.YSSF,0)),0) sf,
        nvl(sum(nvl(a.YSWSF,0)),0) wsf,
        nvl(sum(nvl(a.YSSZYF,0)),0) szyf,
        nvl(sum(nvl(a.YSLJF,0)),0) ljf,
        nvl(sum(nvl(a.ZNJ,0)),0) wyj,
        nvl(sum(nvl(a.YSJE,0)),0) qfje
        from YY_QFMX a
        left join YH_KHJBXX b on a.YHBH=b.YHBH
        left join (select sj.code,yslx.code as ysxzbm from bm_sj sj
        left join bm_yslx yslx on sj.yslx=yslx.code
        ) d on a.sjbm=d.code
        <include refid="qftj_wheresql"></include>
    </select>

    <select id="ysqd" resultType="com.tofly.feesapi.mxcx.entity.vo.YsqdSelect">
        SELECT
        b.YHBH,
        b.YHXM,
        b.YHDZ,
        b.LXDH,
        a.cwrq,
        a.ysyf,
        a.SCDS qd,
        a.BCDS zd,
        (case when a.fylx='1' or a.fylx='2' then nvl(a.yssl,0) else 0 end) qfsl,
        nvl(a.YSSF, 0) sf,
        nvl(a.ZNJ, 0) wyj,
        nvl(a.YSJE, 0) QFJE,
        nvl(a.YSWSF, 0) yswsf,
        nvl(a.YSSZYF, 0) ysszys,
        (a.YSJE + a.ZNj)zqfje,

        ( CASE WHEN SFBZ='0' THEN '未缴'
        ELSE '已缴'
        END
        )jfzt
        FROM
        YY_QFMX a
        LEFT JOIN
        YH_KHJBXX b on a.YHBH = b.YHBH
        <where>
            <if test="filter.searchText!=null">
                and (b.YHBH like '%${filter.searchText}%'
                or b.YHXM like '%${filter.searchText}%'
                or b.YHDZ like '%${filter.searchText}%'
                or b.LXDH like '%${filter.searchText}%'
                or a.CBRYXM like '%${filter.searchText}%'
                )
            </if>
            <if test="filter.yhbh!=null">
                and a.YHBH=#{filter.yhbh}
            </if>
            <if test="filter.ssgs!=null">
                and a.SSGS=#{filter.ssgs}
            </if>
            <if test="filter.sscbpq!=null">
                and a.SSCBPQ=#{filter.sscbpq}
            </if>
            <if test="filter.sscbb!=null">
                <choose>
                    <when test="filter.sscbb.indexOf(',')!=-1">
                        AND a.sscbb in
                        <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                                 close=")">
                            #{item}
                        </foreach>
                    </when>
                    <otherwise>
                        AND a.sscbb=#{filter.sscbb}
                    </otherwise>
                </choose>
            </if>
            <if test="filter.cbrqStart!=null and filter.cbrqEnd!=null">
                and a.CBRQ BETWEEN #{filter.cbrqStart} AND #{filter.cbrqEnd}
            </if>
            <if test="filter.cbsl!=null">
                <![CDATA[ and a.YSSL >=#{filter.cbsl}]]>
            </if>
            <if test="filter.cby!=null">
                and a.cbrybm=#{filter.cby}
            </if>
            <if test="filter.qyfzr!=null">
                and a.sscbb in(select cbbbm from bm_cbb where qyfzr=#{filter.qyfzr})
            </if>
            <if test="filter.yhbhStart!=null">
                <![CDATA[ AND a.yhbh>=#{filter.yhbhStart} ]]>
            </if>
            <if test="filter.yhbhEnd!=null">
                <![CDATA[ AND a.yhbh<=#{filter.yhbhEnd} ]]>
            </if>
            <if test="filter.cwrqStart!=null and filter.cwrqEnd!=null">
                AND a.cwrq BETWEEN #{filter.cwrqStart} AND #{filter.cwrqEnd}
            </if>
            <if test="filter.userId != null">
                AND a.sscbb in (select cbbbm from yh_sjqx where user_id=#{filter.userId})
            </if>
            <if test="filter.khbm != null and filter.khbm !=''">
                AND a.khbm=UPPER(#{filter.khbm})
            </if>
        </where>
    </select>

    <select id="wsljf" resultType="com.tofly.feesapi.mxcx.entity.vo.LjftjSelect">
        select cwrq,
        ljfdj as price,
        count(1) as num,
        sum(ysljf) as amount
        from yy_qfmx
        where sfbz = '0'
        and <![CDATA[ nvl(ysljf, 0)!=0]]>
        <if test="filter.startcwrq!=null and filter.endcwrq!=null">
            and cwrq between #{filter.startcwrq} and #{filter.endcwrq}
        </if>
        <if test="filter.ssgs!=null">
            and ssgs=#{filter.ssgs}
        </if>
        group by cwrq,ljfdj
        order by cwrq asc
    </select>
    <select id="ysljf" resultType="com.tofly.feesapi.mxcx.entity.vo.LjftjSelect">
        select cwrq,
        ljfdj as price,
        count(1) as num,
        sum(ysljf) as amount
        from yy_qfmx
        where <![CDATA[ nvl(ysljf, 0)!=0]]>
        <if test="filter.startcwrq!=null and filter.endcwrq!=null">
            and cwrq between #{filter.startcwrq} and #{filter.endcwrq}
        </if>
        <if test="filter.ssgs!=null">
            and ssgs=#{filter.ssgs}
        </if>
        group by cwrq,ljfdj
        order by cwrq asc
    </select>

    <select id="yisljf" resultType="com.tofly.feesapi.mxcx.entity.vo.LjftjSelect">
        select cwrq,
        <!--        ljfdj as price,-->
        count(1) as num,
        sum(ysljf) as amount
        from yy_sspjxx
        where   <![CDATA[ nvl(ysljf, 0)!=0]]>
        <if test="filter.startcwrq!=null and filter.endcwrq!=null">
            and to_char(sfrq,'yyyyMM') between #{filter.startcwrq} and #{filter.endcwrq}
        </if>
        <if test="filter.ssgs!=null">
            and ssgs=#{filter.ssgs}
        </if>
        group by cwrq
        order by cwrq asc
    </select>

    <select id="getYcfpmxpage" resultType="com.tofly.feesapi.mxcx.entity.vo.YkfpmxtjSelect">
        select
        b.yhbh,
        b.yhxm,
        b.yhdz,
        cbb.cbbmc as sscbb,
        sflx.name as sflxmc,
        a.kpje,
        a.xzje,
        a.kpsj as kprq,
        a.kpryxm as kpr,
        a.xzryxm as xzr,
        a.xzsj,
        a.fplx,
        a.fph fphm,
        a.wsf_fph as wsffphm
        from ei_ykfp_log a
        left join yh_khjbxx b on a.yhbh=b.yhbh
        left join bm_cbb cbb on cbb.cbbbm = b.sscbb
        left join bm_sflx sflx on sflx.code = a.sflx
        <where>
            <if test="filter.searchText!=null">
                and (b.YHBH like '%${filter.searchText}%'
                or b.YHXM like '%${filter.searchText}%'
                or b.YHDZ like '%${filter.searchText}%'
                or b.LXDH like '%${filter.searchText}%'
                )
            </if>
            <if test="filter.ssgs!=null">
                and b.ssgs=#{filter.ssgs}
            </if>
            <if test="filter.sscbpq!=null">
                and b.sscbpq=#{filter.sscbpq}
            </if>
            <if test="filter.sscbb!=null">
                <choose>
                    <when test="filter.sscbb.indexOf(',')!=-1">
                        AND b.sscbb in
                        <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                                 close=")">
                            #{item}
                        </foreach>
                    </when>
                    <otherwise>
                        AND b.sscbb=#{filter.sscbb}
                    </otherwise>
                </choose>
            </if>
            <if test="filter.yhbh!=null">
                and a.yhbh=#{filter.yhbh}
            </if>
            <if test="filter.cwrq!=null ">
                and a.cwrq = #{filter.cwrq}
            </if>
            <if test="filter.startczsj!=null and filter.endczsj!=null">
                and a.sfrq BETWEEN #{filter.startczsj} AND #{filter.endczsj}
            </if>
            <if test="filter.qyfzr!=null and !tf">
                /* AND exists (select 1 from bm_cbb where CBBBM=a.sscbb and qyfzr=#{filter.qyfzr})*/
                AND b.sscbb in(select cbbbm from bm_cbb where qyfzr=#{filter.qyfzr})
            </if>
            <if test="filter.xzzt!=null">
                and a.sfbz=#{filter.xzzt}
            </if>
        </where>
    </select>
    <select id="getYcfpmxhj" resultType="com.tofly.feesapi.mxcx.entity.vo.YkfpmxtjPageSelect">
        select
        nvl(sum(nvl(a.kpje,0)),0) kpje,
        nvl(sum(nvl(a.xzje,0)),0) xzje
        from ei_ykfp_log a
        left join yh_khjbxx b on a.yhbh=b.yhbh
        left join bm_cbb cbb on cbb.cbbbm = b.sscbb
        left join bm_sflx sflx on sflx.code = a.sflx
        <where>
            <if test="filter.searchText!=null">
                and (b.YHBH like '%${filter.searchText}%'
                or b.YHXM like '%${filter.searchText}%'
                or b.YHDZ like '%${filter.searchText}%'
                or b.LXDH like '%${filter.searchText}%'
                )
            </if>
            <if test="filter.ssgs!=null">
                and b.ssgs=#{filter.ssgs}
            </if>
            <if test="filter.sscbpq!=null">
                and b.sscbpq=#{filter.sscbpq}
            </if>
            <if test="filter.sscbb!=null">
                <choose>
                    <when test="filter.sscbb.indexOf(',')!=-1">
                        AND b.sscbb in
                        <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                                 close=")">
                            #{item}
                        </foreach>
                    </when>
                    <otherwise>
                        AND b.sscbb=#{filter.sscbb}
                    </otherwise>
                </choose>
            </if>
            <if test="filter.yhbh!=null">
                and a.yhbh=#{filter.yhbh}
            </if>
            <if test="filter.cwrq!=null ">
                and a.cwrq = #{filter.cwrq}
            </if>
            <if test="filter.startczsj!=null and filter.endczsj!=null">
                and a.sfrq BETWEEN #{filter.startczsj} AND #{filter.endczsj}
            </if>
            <if test="filter.qyfzr!=null and !tf">
                /* AND exists (select 1 from bm_cbb where CBBBM=a.sscbb and qyfzr=#{filter.qyfzr})*/
                AND b.sscbb in(select cbbbm from bm_cbb where qyfzr=#{filter.qyfzr})
            </if>
            <if test="filter.xzzt!=null">
                and a.sfbz=#{filter.xzzt}
            </if>
        </where>
    </select>

    <select id="ljfjm" resultType="com.tofly.feesapi.sfgl.entity.vo.LjfjmVo">
        select a.cwrq,count(*) count,sum(a.yswsf) wsf
        from yy_qfmx a
        where a.fylx='3' and a.yswsf &lt;0 and a.yswsf is not null
        <if test="dto.sfbz!=null">
            AND a.sfbz=#{dto.sfbz}
        </if>
        <if test="dto.cwrq!=null">
            AND a.cwrq=#{dto.cwrq}
        </if>
        <if test="dto.ssgs!=null">
            AND a.ssgs=#{dto.ssgs}
        </if>
        <if test="dto.sscbpq!=null">
            AND a.sscbpq=#{dto.sscbpq}
        </if>
        <if test="dto.sscbb!=null">
            AND a.sscbb=#{dto.sscbb}
        </if>
        <if test="dto.cbrybm!=null">
            AND a.cbrybm=#{dto.cbrybm}
        </if>
        <if test="dto.yhbh!=null">
            AND a.yhbh =#{dto.yhbh}
        </if>
        <if test="dto.jsrqStart!=null and dto.jsrqEnd!=null">
            <![CDATA[ and a.jsrq >=#{dto.jsrqStart}+0  and a.jsrq <= #{dto.jsrqEnd}+0]]>
        </if>
        <if test="dto.qyfzr!=null">
            AND a.sscbb in(select cbbbm from bm_cbb where qyfzr=#{dto.qyfzr})
        </if>
        <if test="dto.cwrqStart!=null and dto.cwrqEnd!=null">
            AND a.cwrq BETWEEN #{dto.cwrqStart} AND #{dto.cwrqEnd}
        </if>
        <if test="dto.yhbhStart!=null">
            <![CDATA[ AND a.yhbh>=#{dto.yhbhStart} ]]>
        </if>
        <if test="dto.yhbhEnd!=null">
            <![CDATA[ AND a.yhbh<=#{dto.yhbhEnd} ]]>
        </if>
        group by cwrq
    </select>

    <select id="qfmxbgPage" resultType="com.tofly.feesapi.sfgl.entity.vo.QfmxbgtjVo">
        <include refid="qfmxbgSql"></include>
    </select>

    <select id="qfmxbgPageList" resultType="com.tofly.feesapi.sfgl.entity.vo.QfmxbgtjVo">
        <include refid="qfmxbgSql"></include>
    </select>

    <sql id="qfmxbgSql">
        SELECT
        a.*,d.ysxzbm ysxz,
        d.name ysxzmc,d.sjmc
        FROM
        yy_qfmx a
        LEFT JOIN
        ( SELECT
        sj.code,
        sj.name as sjmc,
        yslx.name,
        yslx.code as ysxzbm
        FROM
        bm_sj sj
        LEFT JOIN
        bm_yslx yslx ON sj.yslx=yslx.code
        ) d ON a.sjbm = d.code
        WHERE
        sfbc = '2'
        AND
        chbz='2'
        <if test="filter.cwrq != null and filter.cwrq != ''">
            AND a.cwrq=#{filter.cwrq}
        </if>
        <if test="filter.ssgs!=null and filter.ssgs!=''">
            AND a.ssgs=#{filter.ssgs}
        </if>
        <if test="filter.sscbpq!=null and filter.sscbpq!=''">
            AND a.sscbpq=#{filter.sscbpq}
        </if>
        <if test="filter.sscbb!=null and filter.sscbb!=''">
            AND a.sscbb=#{filter.sscbb}
        </if>
        <if test="filter.yhbh != null">
            AND a.yhbh =#{filter.yhbh}
        </if>
        <if test="filter.cbrybm!=null">
            AND a.cbrybm=#{filter.cbrybm}
        </if>
        <if test="filter.lrry!=null">
            AND a.lrrybm=#{filter.lrry}
        </if>
        <if test="filter.yslx!=null">
            and a.sjbm in (select code from bm_sj where yslx=#{filter.yslx})
        </if>
        <if test="filter.jsrqStart!=null and filter.jsrqEnd!=null">
            AND (a.jsrq BETWEEN #{filter.jsrqStart} AND #{filter.jsrqEnd})
        </if>
        <if test="filter.qyfzr!=null">
            AND a.sscbb in(select cbbbm from bm_cbb where qyfzr=#{filter.qyfzr})
        </if>
        <if test="filter.cwrqStart!=null and filter.cwrqEnd!=null">
            AND a.cwrq BETWEEN #{filter.cwrqStart} AND #{filter.cwrqEnd}
        </if>
    </sql>

    <select id="fylrtjPage" resultType="com.tofly.feesapi.sfgl.entity.vo.FylrtjVo">
        <include refid="fylrSql"></include>
    </select>
    <select id="fylrtjPageList" resultType="com.tofly.feesapi.sfgl.entity.vo.FylrtjVo">
        <include refid="fylrSql"></include>
    </select>

    <sql id="fylrSql">
        select
        a.*
        from yy_qfmx a
        where sfbc = '1'
        <if test="filter.fylx!=null">
            AND a.fylx=#{filter.fylx}
        </if>
        <if test="filter.cwrq!=null">
            AND a.cwrq=#{filter.cwrq}
        </if>
        <if test="filter.ssgs!=null">
            AND a.ssgs=#{filter.ssgs}
        </if>
        <if test="filter.sscbpq!=null">
            AND a.sscbpq=#{filter.sscbpq}
        </if>
        <if test="filter.sscbb!=null">
            <choose>
                <when test="filter.sscbb.indexOf(',')!=-1">
                    AND a.sscbb in
                    <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.sscbb=#{filter.sscbb}
                </otherwise>
            </choose>
        </if>
        <if test="filter.yhbh!=null">
            AND a.yhbh =#{filter.yhbh}
        </if>
        <if test="filter.cbrybm!=null">
            AND a.cbrybm=#{filter.cbrybm}
        </if>
        <if test="filter.yslx!=null">
            and a.sjbm in (select code from bm_sj where yslx=#{filter.yslx})
        </if>
        <if test="filter.jsrqStart!=null and filter.jsrqEnd!=null">
            AND (a.jsrq BETWEEN #{filter.jsrqStart} AND #{filter.jsrqEnd})
        </if>
        <if test="filter.qyfzr!=null">
            AND a.sscbb in(select cbbbm from bm_cbb where qyfzr=#{filter.qyfzr})
        </if>
        <if test="filter.cwrqStart!=null and filter.cwrqEnd!=null">
            AND a.cwrq BETWEEN #{filter.cwrqStart} AND #{filter.cwrqEnd}
        </if>
    </sql>

    <select id="getqmjyje" resultType="java.math.BigDecimal">
        select nvl(sum(nvl(qmjy_je,0)),0) qmjyje from YY_QMJY_LOG a
        where 1=1 and type_id=#{typeid}
        <if test="cwrq!=null">
            AND a.cwrq = #{cwrq}
        </if>
    </select>

    <select id="getbckj" resultType="java.math.BigDecimal">
        select nvl(sum(nvl(bckj,0)),0) bckj from YY_SSPJXX a
        where 1=1 and sflx='1'
        <if test="sfrqStart!=null and sfrqEnd!=null">
            <![CDATA[ and a.sfrq >=#{sfrqStart}+0  and a.sfrq < #{sfrqEnd}+0]]>
        </if>
    </select>

    <select id="getbcyeandtf" resultType="java.math.BigDecimal">
        select nvl(sum(nvl(sszje,0)),0) sszje from YY_SSPJXX a
        where 1=1
        <if test="istf==1">
            AND sszje &lt; 0
        </if>
        <if test="istf==0">
            AND sszje > 0
        </if>
        <if test="sfrqStart!=null and sfrqEnd!=null">
            <![CDATA[ and a.sfrq >=#{sfrqStart}+0  and a.sfrq < #{sfrqEnd}+0]]>
        </if>
    </select>
    <select id="getQfList" resultType="com.tofly.feesapi.sfgl.entity.vo.YyQfmxSelect1">
        select t.*,z.syje as ycje from (
        select a.yhbh,
        b.yhxm,
        b.yhdz,
        count(a.id) qfbs,
        sum(case when a.fylx='1' or a.fylx='2' then a.yssl else 0 end) qfsl,
        sum(a.ysje) qfje,
        sum(nvl(a.ysje,0)+nvl(a.znj,0)) yszje,
        sum(a.znj) wyj,
        wm_concat(id) as qfids
        from yy_qfmx a
        left join yh_khjbxx b on a.yhbh = b.yhbh
        where 1=1
        <if test="sfbz!=null">
            AND a.sfbz=#{sfbz}
        </if>
        <if test="chbz!=null">
            AND a.chbz=#{chbz}
        </if>
        <if test="filter.ffgxzh!=null">
            AND a.yhbh in (select yhbh from yh_group_accounts
            where group_id in (select id from yh_group where yhbh= #{filter.ffgxzh})
            union
            select yhbh from yh_group where yhbh=#{filter.ffgxzh}
            )
            <!--            <if test="filter.cwrq==null and filter.cwrqStart==null and filter.cwrqEnd==null">-->
            <!--                and cwrq=(select min(cwrq) from yy_qfmx where-->
            <!--                sfbz='0' AND chbz='0' and yhbh in (-->
            <!--                select yhbh from yh_group_accounts-->
            <!--                where group_id in (select id from yh_group where yhbh= #{filter.ffgxzh})-->
            <!--                ))-->
            <!--            </if>-->
        </if>
        <if test="filter.cwrqStart!=null and filter.cwrqEnd!=null">
            AND a.cwrq BETWEEN #{filter.cwrqStart} AND #{filter.cwrqEnd}
        </if>
        group by a.yhbh,b.yhxm,b.yhdz
        )t
        left join yy_zjzh z on t.yhbh=z.yhbh
    </select>
    <select id="hslStatDt" resultType="com.tofly.feesapi.sfgl.entity.dto.YyHslStat">
        SELECT ${ysyf}
        b.name sjmc,
        a.cbryxm cbry,
        SUM(case when sfbz in ('1') then nvl(a.sf${tag},0) else 0 end) sssf,
        SUM(case when sfbz in ('1') then nvl(a.wsf${tag},0) else 0 end) sswsf,
        SUM(case when sfbz in ('1') then nvl(a.ljf${tag},0) else 0 end) ssljf,
        SUM(case when sfbz in ('1') then nvl(a.szyf${tag},0) else 0 end) ssszyf
        <if test="tag==null or tag==''">
            ,SUM(case when sfbz in ('1') then nvl(znj,0) else 0 end) ssznj
            ,SUM(case when sfbz in ('1') then 1 else 0 end) sshs
        </if>
        FROM yy_qfmx a
        left join bm_sj b on a.sjbm${tag}=b.code
        WHERE a.sjbm${tag} >' ' AND a.sfbc!='3' and chbz in ('0','1') AND sfbz='1'
        <if test="filter.ysyfStart!=null and filter.ysyfEnd!=null">
            AND a.cwrq BETWEEN #{filter.ysyfStart} AND #{filter.ysyfEnd}
        </if>
        <if test="filter.sfyf!=null">
            and to_char(sfrq,'yyyyMM')=#{filter.sfyf}
        </if>
        <if test="filter.sfyfStart!=null and filter.sfyfEnd!=null">
            and to_char(sfrq,'yyyyMM') BETWEEN #{filter.sfyfStart} AND #{filter.sfyfEnd}
        </if>
        <if test="filter.yslx!=null">
            AND b.yslx=#{filter.yslx}
        </if>
        <if test="filter.ssgs!=null">
            AND a.ssgs=#{filter.ssgs}
        </if>
        <if test="filter.sscbpq!=null">
            AND a.sscbpq=#{filter.sscbpq}
        </if>
        <if test="filter.sscbb!=null">
            <choose>
                <when test="filter.sscbb.indexOf(',')!=-1">
                    AND a.sscbb in
                    <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.sscbb=#{filter.sscbb}
                </otherwise>
            </choose>
        </if>
        GROUP BY ${ysyf}b.name,a.cbryxm
    </select>

    <select id="hslStatGd" resultType="com.tofly.feesapi.sfgl.entity.dto.YyHslStatGd">
        SELECT ${ysyf}
        b.name sjmc,
        a.cbryxm cbry,
        a.sfbz,
        SUM(case when fylx in(1,2) then nvl(a.sl${tag},0) else 0 end) sl,
        SUM(nvl(a.sf${tag},0)) sf,
        SUM(nvl(a.wsf${tag},0)) wsf,
        SUM(nvl(a.ljf${tag},0)) ljf,
        SUM(nvl(a.szyf${tag},0)) szyf
        <if test="tag==null or tag==''">
            ,SUM(nvl(znj,0)) znj
            ,count(*) hs
        </if>
        FROM yy_qfmx a
        left join bm_sj b on a.sjbm${tag}=b.code
        WHERE a.sjbm${tag} >' ' AND a.sfbc!='3' and chbz in ('0','1')
        <if test="filter.ysyfStart!=null and filter.ysyfEnd!=null">
            AND a.cwrq BETWEEN #{filter.ysyfStart} AND #{filter.ysyfEnd}
        </if>
        <if test="filter.yslx!=null">
            AND b.yslx=#{filter.yslx}
        </if>
        <if test="filter.ssgs!=null">
            AND a.ssgs=#{filter.ssgs}
        </if>
        <if test="filter.sscbpq!=null">
            AND a.sscbpq=#{filter.sscbpq}
        </if>
        <if test="filter.sscbb!=null">
            <choose>
                <when test="filter.sscbb.indexOf(',')!=-1">
                    AND a.sscbb in
                    <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.sscbb=#{filter.sscbb}
                </otherwise>
            </choose>
        </if>
        GROUP BY ${ysyf}b.name,a.cbryxm,a.sfbz
    </select>

    <select id="cxykjqfmx" resultType="java.util.Map">
        SELECT
        yhbh,
        wm_concat(id) AS qfids
        FROM
        yy_qfmx qf
        WHERE
        sfbz = '0'
        AND chbz = '0'
        <if test="filter.yhbh != null and filter.yhbh != ''">
            AND qf.yhbh = #{filter.yhbh}
        </if>
        AND exists(
        SELECT 1
        FROM
        (
        SELECT yhbh, sum(nvl(ysje, 0) + nvl(znj, 0)) qfje
        FROM
        yy_qfmx
        WHERE
        sfbz = 0
        AND chbz = 0
        GROUP BY yhbh
        HAVING SUM(NVL(ysje, 0)) + SUM(NVL(znj, 0)) > 0
        ) a
        INNER JOIN yy_zjzh b ON a.yhbh = b.yhbh
        AND syje > 0
        AND syje >= qfje
        WHERE a.yhbh = qf.yhbh
        )
        GROUP BY yhbh
    </select>


    <select id="getMonthlyStatDetail" resultType="com.tofly.feesapi.sfgl.entity.vo.YstjbbVo">

        SELECT
        a.cwrq AS cwrq,
        sj.code AS code,
        sj.name AS ysxm,
        sj.sj AS sj,
        SUM(case when a.fylx='1' or a.fylx='2' then a.yssl else 0 end) AS sl,
        SUM( NVL( a.yssf, 0 ) ) AS sf,
        SUM( NVL( a.yswsf, 0 ) ) AS wsf,
        SUM( NVL( a.ysszyf, 0 ) ) AS szyf,
        SUM( NVL( a.ysecjyf, 0 ) ) AS ecjyf,
        SUM( NVL( a.ysfjf, 0 ) ) AS fjf,
        SUM( NVL( a.ysqt, 0 ) ) AS qtf,
        SUM( NVL( a.ysje, 0 ) ) AS hj
        FROM
        yy_qfmx a
        INNER JOIN
        (SELECT
        sj.code AS code,
        sj.name AS name,
        sj.sj
        FROM
        bm_sj sj
        LEFT JOIN bm_yslx yslx ON sj.yslx = yslx.code
        ) sj ON a.sjbm = sj.code
        WHERE
        1 = 1

        <if test="filter.chbz!=null">
            <choose>
                <when test="filter.chbz.indexOf(',') != -1">
                    AND a.chbz in
                    <foreach item="item" index="index" collection="filter.chbz.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.chbz = #{filter.chbz}
                </otherwise>
            </choose>
        </if>
        <if test="filter.cbrybm!=null">
            AND a.cbrybm = #{filter.cbrybm}
        </if>
        <if test="filter.ssgs!=null">
            AND a.ssgs = #{filter.ssgs}
        </if>
        <if test="filter.sscbpq!=null">
            AND a.sscbpq=#{filter.sscbpq}
        </if>
        <if test="filter.sscbb!=null">
            <choose>
                <when test="filter.sscbb.indexOf(',')!=-1">
                    AND a.sscbb in
                    <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.sscbb=#{filter.sscbb}
                </otherwise>
            </choose>
        </if>
        <if test="filter.nf!=null">
            AND a.cwrq like '${filter.nf}%'
        </if>
        <if test="filter.jsrqStart!=null and filter.jsrqEnd!=null">
            <![CDATA[ and a.jsrq >=#{filter.jsrqStart}+0  and a.jsrq <= #{filter.jsrqEnd}+0]]>
        </if>
        <if test="filter.yhbhStart!=null">
            <![CDATA[ AND a.yhbh>=#{filter.yhbhStart} ]]>
        </if>
        <if test="filter.yhbhEnd!=null">
            <![CDATA[ AND a.yhbh<=#{filter.yhbhEnd} ]]>
        </if>
        <if test="filter.ysyfStart!=null and filter.ysyfEnd!=null">
            AND a.ysyf BETWEEN #{filter.ysyfStart} AND #{filter.ysyfEnd}
        </if>
        <if test="filter.cbbbms != null and filter.cbbbms.size() > 0">
            AND a.sscbb IN
            <foreach collection="filter.cbbbms" open="(" item="cbbbm" separator="," close=")">
                #{cbbbm}
            </foreach>
        </if>
        GROUP BY a.cwrq, sj.code, sj.name, sj.sj
    </select>


    <select id="bbglqftj" resultType="com.tofly.feesapi.sfgl.entity.vo.YstjbbVo">

        SELECT
        a.cwrq,
        c.ysxz AS ysxm,
        c.sj AS sj,
        sum( case when a.fylx='1' or a.fylx='2' then a.yssl else 0 end ) sl,
        sum( nvl(a.YSSF, 0) ) sf,
        sum( nvl(a.YSWSF, 0) ) wsf,
        sum( nvl(a.YSSZYF, 0) ) szyf,
        sum( nvl(a.YSECJYF, 0) ) ecjyf,
        sum( nvl(a.YSFJF, 0) ) fjf,
        sum( nvl(a.YSQT, 0) ) qtf
        FROM
        YY_QFMX a
        LEFT JOIN
        YH_KHJBXX b ON a.YHBH = b.YHBH
        LEFT JOIN (
        SELECT
        s.CODE as sjbm, s.YSLX, y.NAME as ysxz, s.SJ as sj
        FROM
        BM_SJ s
        LEFT JOIN
        BM_YSLX y ON s.YSLX = y.CODE
        ) c ON c.sjbm = a.sjbm
        WHERE
        a.SFBZ = '0'
        AND
        a.sfbc != 3
        <if test="filter.chbz!=null">
            <choose>
                <when test="filter.chbz.indexOf(',')!=-1">
                    AND a.chbz in
                    <foreach item="item" index="index" collection="filter.chbz.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.chbz=#{filter.chbz}
                </otherwise>
            </choose>
        </if>
        <if test="filter.yhbh!=null">
            and a.YHBH=#{filter.yhbh}
        </if>
        <if test="filter.ssgs!=null">
            and a.SSGS=#{filter.ssgs}
        </if>
        <if test="filter.sscbpq!=null">
            and a.SSCBPQ=#{filter.sscbpq}
        </if>
        <if test="filter.sscbb!=null">
            <choose>
                <when test="filter.sscbb.indexOf(',')!=-1">
                    AND a.sscbb in
                    <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.sscbb=#{filter.sscbb}
                </otherwise>
            </choose>
        </if>
        <if test="filter.ysyfStart != null and filter.ysyfStart != ''">
            and a.cwrq >= #{filter.ysyfStart}
        </if>
        <if test="filter.ysyfEnd != null and filter.ysyfEnd != ''">
            and a.cwrq &lt;= #{filter.ysyfEnd}
        </if>
        <if test="filter.cwrqStart!=null and filter.cwrqEnd!=null">
            and a.cwrq BETWEEN #{filter.cwrqStart} AND #{filter.cwrqEnd}
        </if>
        <if test="filter.jsrqStart!=null and filter.jsrqEnd!=null">
            and a.jsrq BETWEEN #{filter.jsrqStart} AND #{filter.jsrqEnd}
        </if>
        <if test="filter.jzEnd!=null">
            <![CDATA[ and (sfbz='0' or (sfbz='1' and a.sfrq>#{filter.jzEnd}+0)) ]]>
        </if>
        <if test="filter.yhbhStart!=null">
            and a.yhbh >= #{filter.yhbhStart}
        </if>
        <if test="filter.yhbhEnd!=null">
            and a.yhbh &lt;= #{filter.yhbhEnd}
        </if>
        <if test="filter.yslx!=null">
            and c.yslx = #{filter.yslx}
        </if>
        <if test="filter.cbbbms != null and filter.cbbbms.size() > 0">
            and a.sscbb IN
            <foreach collection="filter.cbbbms" open="(" item="cbbbm" separator="," close=")">
                #{cbbbm}
            </foreach>
        </if>
        GROUP BY
        a.cwrq, c.ysxz, c.sj

    </select>


    <select id="getSumQfmx" resultType="com.tofly.feesapi.sfgl.entity.vo.YyqfmxSumSelect">
        <![CDATA[
        select aa.yhbh,aa.qfje,bb.lxdh,bb.yhdz,bb.yhxm from (
        select yhbh,sum(ysje) qfje from yy_qfmx a
        where sfbz=0 group by yhbh ) aa
        left join yh_khjbxx bb on aa.yhbh=bb.yhbh
        where qfje>0
        and length(bb.lxdh)=11
       ]]>
        <if test="filter.searchText!=null">
            and (bb.yhxm like '%${filter.searchText}%'
            or bb.yhdz like '%${filter.searchText}%'
            or bb.lxdh like '%${filter.searchText}%'
            or bb.yhbh like '%${filter.searchText}%')
        </if>
        <if test="filter.sscbpq!=null">
            and bb.sscbpq=#{filter.sscbpq}
        </if>
        <if test="filter.sscbb!=null">
            <choose>
                <when test="filter.sscbb.indexOf(',')!=-1">
                    AND bb.sscbb in
                    <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND bb.sscbb=#{filter.sscbb}
                </otherwise>
            </choose>
        </if>
        <if test="filter.userId!=null">
            AND bb.sscbb in (select cbbbm from yh_sjqx where user_id=#{filter.userId})
        </if>
    </select>

    <insert id="yytkfch">
        INSERT INTO YY_QFMX (ID, QFLSBH, CBKID, YHBH, CBKH, SSGS, SSCBPQ, SSCBB, YSYF, CWRQ,
                             SCDS, BCDS, JJL, SL, SJBM, SJ, SF, WSFDJ, WSF, SJBM1,
                             SJ1, SL1, SF1, WSFDJ1, WSF1, SJBM2, SJ2, SL2, SF2, WSFDJ2,
                             WSF2, SJBM3, SJ3, SL3, SF3, WSFDJ3, WSF3, CBRYBM, CBRYXM, CBRQ,
                             LRRYBM, LRRYXM, LRRQ, JSRQ, YSSL, YSSF, YSWSF, YSLJF, YSQT, YSJE,
                             JT1SL, JT1SJ, JT1SF, JT2SL, JT2SJ, JT2SF, JT3SL, JT3SJ, JT3SF, JT4SL,
                             JT4SJ, JT4SF, ZNJTS, ZNJFL, ZNJ, ZNJJSRQ, CBJSBZ, CBJSFS, BZ, CBLX,
                             CBFS, JBZD, XBQD, CBLY, CBCS, FYLX, CHBZ, CHID, CHSJ, CHRYBM,
                             CHRYXM, CHLX, SFBZ, SFRQ, YHXM, SFLSBH, FYLXMC, SFBC, EI_SGKP_ID, LJFDJ,
                             LJF, LJFDJ1, LJF1, JTZQLJL_JSQ, JTZQLJL, SZYF_DJ, SZYF, SZYF_DJ1, SZYF1, SZYF_DJ2,
                             SZYF2, SZYF_DJ3, SZYF3, YHJE, YSSZYF, YHSL, KHBM, FJF, ECJYF, YSFJF,
                             YSECJYF)
        SELECT SYS_GUID(),
               QFLSBH,
               CBKID,
               YHBH,
               CBKH,
               SSGS,
               SSCBPQ,
               SSCBB,
               YSYF,
               #{cwrq},
               SCDS,
               BCDS,
               -JJL,
               -SL,
               SJBM,
               SJ,
               -SF,
               WSFDJ,
               -WSF,
               SJBM1,
               SJ1,
               -SL1,
               -SF1,
               WSFDJ1,
               -WSF1,
               SJBM2,
               SJ2,
               -SL2,
               -SF2,
               WSFDJ2,
               -WSF2,
               SJBM3,
               SJ3,
               -SL3,
               -SF3,
               WSFDJ3,
               -WSF3,
               CBRYBM,
               CBRYXM,
               CBRQ,
               LRRYBM,
               LRRYXM,
               #{dto.chsj},
               #{dto.chsj},
               -YSSL,
               -YSSF,
               -YSWSF,
               -YSLJF,
               -YSQT,
               -YSJE,
               -JT1SL,
               JT1SJ,
               -JT1SF,
               -JT2SL,
               JT2SJ,
               -JT2SF,
               -JT3SL,
               JT3SJ,
               -JT3SF,
               -JT4SL,
               JT4SJ,
               -JT4SF,
               ZNJTS,
               ZNJFL,
               -ZNJ,
               ZNJJSRQ,
               CBJSBZ,
               CBJSFS,
               BZ,
               CBLX,
               CBFS,
               JBZD,
               XBQD,
               CBLY,
               CBCS,
               FYLX,
               '2',
               ID,
               #{dto.chsj},
               #{dto.chrybm},
               #{dto.chryxm},
               '1',
               SFBZ,
               #{dto.chsj},
               YHXM,
               SYS_GUID(),
               FYLXMC,
               SFBC,
               EI_SGKP_ID,
               LJFDJ,
               -LJF,
               LJFDJ1,
               -LJF1,
               JTZQLJL_JSQ,
               JTZQLJL,
               SZYF_DJ,
               -SZYF,
               SZYF_DJ1,
               -SZYF1,
               SZYF_DJ2,
               -SZYF2,
               SZYF_DJ3,
               -SZYF3,
               -YHJE,
               -YSSZYF,
               -YHSL,
               KHBM,
               -FJF,
               -ECJYF,
               -YSFJF,
               -YSECJYF
        FROM YY_QFMX
        WHERE ID = #{data.qfid}
    </insert>

    <update id="updateQfmxChbz">
        UPDATE
            YY_QFMX
        SET CHBZ   = '1',
            CHSJ   = #{dto.chsj},
            CHRYBM = #{dto.chrybm},
            CHRYXM = #{dto.chryxm},
            CHLX   = '1'
        WHERE ID = #{qfid}
    </update>

    <insert id="xzqfmx">
        INSERT INTO YY_QFMX (ID, QFLSBH, CBKID, YHBH, CBKH, SSGS, SSCBPQ, SSCBB, YSYF, CWRQ,
                             SCDS, BCDS, JJL, SL, SJBM, SJ, SF, WSFDJ, WSF, SJBM1,
                             SJ1, SL1, SF1, WSFDJ1, WSF1, SJBM2, SJ2, SL2, SF2, WSFDJ2,
                             WSF2, SJBM3, SJ3, SL3, SF3, WSFDJ3, WSF3, CBRYBM, CBRYXM, CBRQ,
                             LRRYBM, LRRYXM, LRRQ, JSRQ, YSSL, YSSF, YSWSF, YSLJF, YSQT, YSJE,
                             JT1SL, JT1SJ, JT1SF, JT2SL, JT2SJ, JT2SF, JT3SL, JT3SJ, JT3SF, JT4SL,
                             JT4SJ, JT4SF, ZNJTS, ZNJFL, ZNJ, ZNJJSRQ, CBJSBZ, CBJSFS, BZ, CBLX,
                             CBFS, JBZD, XBQD, CBLY, CBCS, FYLX, CHBZ, CHLX, SFBZ, YHXM,
                             FYLXMC, SFBC, EI_SGKP_ID, LJFDJ, LJF, LJFDJ1, LJF1, JTZQLJL_JSQ, JTZQLJL, SZYF_DJ,
                             SZYF, SZYF_DJ1, SZYF1, SZYF_DJ2, SZYF2, SZYF_DJ3, SZYF3, YHJE, YSSZYF, YHSL,
                             KHBM, FJF, ECJYF, YSFJF, YSECJYF)
        SELECT SYS_GUID(),
               QFLSBH,
               CBKID,
               YHBH,
               CBKH,
               SSGS,
               SSCBPQ,
               SSCBB,
               YSYF,
               #{cwrq},
               SCDS,
               BCDS,
               JJL,
               SL,
               SJBM,
               SJ,
               SF,
               WSFDJ,
               WSF,
               SJBM1,
               SJ1,
               SL1,
               SF1,
               WSFDJ1,
               WSF1,
               SJBM2,
               SJ2,
               SL2,
               SF2,
               WSFDJ2,
               WSF2,
               SJBM3,
               SJ3,
               SL3,
               SF3,
               WSFDJ3,
               WSF3,
               CBRYBM,
               CBRYXM,
               CBRQ,
               LRRYBM,
               LRRYXM,
               #{dto.chsj},
               #{dto.chsj},
               YSSL,
               YSSF,
               YSWSF,
               YSLJF,
               YSQT,
               YSJE,
               JT1SL,
               JT1SJ,
               JT1SF,
               JT2SL,
               JT2SJ,
               JT2SF,
               JT3SL,
               JT3SJ,
               JT3SF,
               JT4SL,
               JT4SJ,
               JT4SF,
               ZNJTS,
               ZNJFL,
               ZNJ,
               ZNJJSRQ,
               CBJSBZ,
               CBJSFS,
               BZ,
               CBLX,
               CBFS,
               JBZD,
               XBQD,
               CBLY,
               CBCS,
               FYLX,
               '0',
               '1',
               '0',
               YHXM,
               FYLXMC,
               SFBC,
               EI_SGKP_ID,
               LJFDJ,
               LJF,
               LJFDJ1,
               LJF1,
               JTZQLJL_JSQ,
               JTZQLJL,
               SZYF_DJ,
               SZYF,
               SZYF_DJ1,
               SZYF1,
               SZYF_DJ2,
               SZYF2,
               SZYF_DJ3,
               SZYF3,
               YHJE,
               YSSZYF,
               YHSL,
               KHBM,
               FJF,
               ECJYF,
               YSFJF,
               YSECJYF
        FROM YY_QFMX
        WHERE ID = #{qfid}
    </insert>

    <insert id="ycdkch">
        INSERT INTO YY_QFMX (id, qflsbh, cbkid, yhbh, cbkh, ssgs, sscbpq, sscbb,
                             ysyf, cwrq, scds, bcds, jjl, sl, sjbm, sj, sf, wsfdj, wsf,
                             sjbm1, sj1, sl1, sf1, wsfdj1, wsf1, sjbm2, sj2, sl2, sf2, wsfdj2,
                             wsf2, sjbm3, sj3, sl3, sf3, wsfdj3, wsf3,
                             cbrybm, cbryxm, cbrq, lrrybm, lrryxm, lrrq, jsrq, yssl, yssf, yswsf,
                             ysljf, ysqt, ysje,
                             jt1sl, jt1sj, jt1sf, jt2sl, jt2sj, jt2sf, jt3sl, jt3sj, jt3sf,
                             jt4sl, jt4sj, jt4sf,
                             znjts, znjfl, znj, znjjsrq, cbjsbz, cbjsfs, bz, cblx, cbfs, jbzd,
                             xbqd, cbly, cbcs, fylx,
                             chbz, chid, chsj, chrybm, chryxm, chlx, sfbz, sfrq,
                             yhxm, sflsbh, ei_sgkp_id, LJFDJ, LJF, LJFDJ1, LJF1, JTZQLJL_JSQ,
                             JTZQLJL, SZYF_DJ, SZYF, SZYF_DJ1, SZYF1, SZYF_DJ2, SZYF2, SZYF_DJ3,
                             SZYF3, YHJE, YSSZYF, YHSL, KHBM, FJF, ECJYF, YSFJF, YSECJYF)
        SELECT SYS_GUID(),
               QFLSBH,
               CBKID,
               YHBH,
               CBKH,
               SSGS,
               SSCBPQ,
               SSCBB,
               YSYF,
               #{cwrq},
               SCDS,
               BCDS,
               -JJL,
               -SL,
               SJBM,
               SJ,
               -SF,
               WSFDJ,
               -WSF,
               SJBM1,
               SJ1,
               -SL1,
               -SF1,
               wsfdj1,
               -wsf1,
               SJBM2,
               SJ2,
               -SL2,
               -SF2,
               wsfdj2,
               -wsf2,
               SJBM3,
               SJ3,
               -SL3,
               -SF3,
               wsfdj3,
               -wsf3,
               cbrybm,
               cbryxm,
               cbrq,
               #{dto.chrybm},
               #{dto.chryxm},
               #{dto.chsj},
               #{dto.chsj},
               -yssl,
               -yssf,
               -yswsf,
               -ysljf,
               -ysqt,
               -ysje,
               -jt1sl,
               jt1sj,
               -jt1sf,
               -jt2sl,
               jt2sj,
               -jt2sf,
               -jt3sl,
               jt3sj,
               -jt3sf,
               -jt4sl,
               jt4sj,
               -jt4sf,
               ZNJTS,
               ZNJFL,
               -ZNJ,
               znjjsrq,
               cbjsbz,
               cbjsfs,
               bz,
               cblx,
               cbfs,
               jbzd,
               xbqd,
               cbly,
               cbcs,
               fylx,
               '2',
               ID,
               #{dto.chsj},
               #{dto.chrybm},
               #{dto.chryxm},
               '1',
               sfbz,
               #{dto.chsj},
               yhxm,
               SYS_GUID(),
               ei_sgkp_id,
               LJFDJ,
               -LJF,
               LJFDJ1,
               -LJF1,
               JTZQLJL_JSQ,
               JTZQLJL,
               SZYF_DJ,
               -SZYF,
               SZYF_DJ1,
               -SZYF1,
               SZYF_DJ2,
               -SZYF2,
               SZYF_DJ3,
               -SZYF3,
               -YHJE,
               -YSSZYF,
               -YHSL,
               KHBM,
               -FJF,
               -ECJYF,
               -YSFJF,
               -YSECJYF
        FROM YY_QFMX
        WHERE ID = #{data.qfid}
    </insert>
    <select id="getQfyhs" resultType="java.lang.Long">
        select count(1) as qfyhs
        from (select yhbh
              from yy_qfmx
              where sfbz = '0'
                and chbz = '0'
                and ysje > 0
              union
              select yhbh
              from yy_zjzh
              where nbsyje &lt; 0) a
    </select>
    <select id="getYsbbmxList" resultType="java.util.Map">
        select b.YHXM,b.yhdz,a.* from (
        select yhbh,sum(ysje)ysje from yy_qfmx
        <where>
            <if test="filter.yhbh != null">
                and yhbh=#{filter.yhbh}
            </if>
            <if test="filter.sscbpq!=null">
                and sscbpq=#{filter.sscbpq}
            </if>
            <if test="filter.sscbb != null and filter.sscbb != ''">
                <if test="filter.sscbb.indexOf(',') != -1">
                    and sscbb in
                    <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </if>

                <if test="filter.sscbb.indexOf(',') == -1">
                    and sscbb=#{filter.sscbb}
                </if>
            </if>
            <if test="filter.dxh!=null">
                <if test='filter.dxh=="0"'>
                    AND khbm is null
                </if>
                <if test='filter.dxh=="1"'>
                    AND khbm >' '
                </if>
            </if>
            <if test="filter.startTime!=null and filter.endTime!=null">
                AND jsrq BETWEEN #{filter.startTime} AND #{filter.endTime}
            </if>
            <if test="filter.userId != null">
                AND sscbb in (select cbbbm from yh_sjqx where user_id=#{filter.userId})
            </if>
        </where>
        group by yhbh
        ) a
        left join yh_khjbxx b on a.yhbh=b.yhbh
        <where>
            <if test="filter.searchText!=null">
                AND ( a.yhbh like '%${filter.searchText}%'
                OR b.yhxm like '%${filter.searchText}%'
                OR b.yhdz like '%${filter.searchText}%'
                OR b.lxdh like '${filter.searchText}%'
                )
            </if>
        </where>
    </select>

    <select id="getFirstCwrq" resultType="java.lang.String">
        select min(cwrq)
        from yy_qfmx
        where qflsbh = #{qflsbh}
    </select>
</mapper>
