<?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.cbgl.mapper.YyCblrMapper">
    <sql id="querySql">
        SELECT
        a.yhbh, a.cbkh, a.cbkid, e.sbbh,e.sblc, d.yhxm, d.yhdz,d.lxdh, a.scds, a.bcds, a.jjl, a.sl AS sjsl,
        b.name AS cbfsmc, a.cbbz, a.shbz, a.jsbz, a.cblx, c.name AS cblxmc, a.jbzd, a.xbqd, a.jbbh, a.cbrq,
        a.lrrq, a.photo, a.bz, a.cbrybm, a.cbryxm, a.cbxh, a.cwrq,a.sscbb, f.ccname as sbzt,e.fmzt,
        g.qyfzr as qyfzrbm,g.qyfzrxm,a.lrrybm, a.lrryxm, e.cbpl
        <if test="filter.cxly!=null and filter.cxly=='app'">
            ,(select LISTAGG(cwrq||'-'||ysje,'|') WITHIN GROUP (ORDER BY cwrq) from yy_qfmx where sfbz=0 and
            yy_qfmx.yhbh=a.yhbh) qfje,
            (SELECT nvl(sum(nvl(syje, 0)), 0) FROM yy_zjzh WHERE yy_zjzh.yhbh = a.yhbh ) ycje
        </if>
        FROM yy_cblr a
        LEFT JOIN bm_cbfs b on a.cbfs = b.code
        LEFT JOIN bm_cblx c on a.cblx = c.code
        LEFT JOIN yh_khjbxx d on a.yhbh = d.yhbh
        LEFT JOIN yh_cbkxx e on a.yhbh = e.yhbh and a.cbkh=e.cbkh
        LEFT JOIN sys_child_code f on a.sbzt=f.cccode and f.pcid='34'
        LEFT JOIN bm_cbb g on a.sscbb = g.cbbbm
        <include refid="querySqlWhere"></include>
    </sql>
    <sql id="querySqlWhere">
        <if test="filter.cwrq!=null and filter.cwrq!=''">
            LEFT JOIN (
            SELECT yhbh,cbkid, sl FROM YY_CBLR_LS
            WHERE cwrq = #{filter.cwrq}
            <!--所属公司-->
            <if test="filter.ssgs != null and filter.ssgs != ''">
                AND ssgs=#{filter.ssgs}
            </if>
            <!--所属片区-->
            <if test="filter.sscbpq != null and filter.sscbpq != ''">
                AND sscbpq=#{filter.sscbpq}
            </if>
            <!--所属抄表本-->
            <if test="filter.sscbb != null and filter.sscbb != ''">
                <choose>
                    <when test="filter.sscbb.indexOf(',')!=-1">
                        AND sscbb in
                        <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                                 close=")">
                            #{item}
                        </foreach>
                    </when>
                    <otherwise>
                        AND sscbb=#{filter.sscbb}
                    </otherwise>
                </choose>
            </if>
            <if test="filter.userId!= null">
                AND sscbb in (select cbbbm from yh_sjqx where user_id=#{filter.userId})
            </if>
            ) ls ON a.cbkid=ls.cbkid
        </if>
        WHERE 1=1 AND e.jfzt='1'
        <!--模糊查询 -->
        <if test="filter.searchText!=null">
            AND ( a.yhbh like '%${filter.searchText}%'
            OR d.yhxm like '%${filter.searchText}%'
            OR d.yhdz like '%${filter.searchText}%'
            OR d.lxdh like '${filter.searchText}%'
            )
        </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 != ''">
            <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.cbfs!=null">
            AND a.cbfs=#{filter.cbfs}
        </if>
        <!-- 用水性质（水价编码）-->
        <if test="filter.ysxz!=null">
            AND (
            e.sjbm in (select code from bm_sj where yslx=#{filter.ysxz})
            OR e.sjbm1 in (select code from bm_sj where yslx=#{filter.ysxz})
            OR e.sjbm2 in (select code from bm_sj where yslx=#{filter.ysxz})
            OR e.sjbm3 in (select code from bm_sj where yslx=#{filter.ysxz})
            )
        </if>
        <!-- 水表口径编码-->
        <if test="filter.sbkj!=null">
            AND e.sbkj = #{filter.sbkj}
        </if>
        <!-- 抄表人员编码-->
        <if test="filter.cbrybm!=null">
            AND a.cbrybm = #{filter.cbrybm}
        </if>
        <!-- 财务日期-->
        <if test="filter.cwrq!=null">
            AND a.cwrq = #{filter.cwrq}
        </if>
        <!-- 抄表标志-->
        <if test="filter.cbbz!=null">
            AND nvl(a.cbbz,0) = #{filter.cbbz}
        </if>
        <!-- 审核标志-->
        <if test="filter.shbz!=null">
            AND nvl(a.shbz,0) = #{filter.shbz}
        </if>
        <!-- 计算标志-->
        <if test="filter.jsbz!=null">
            AND nvl(a.jsbz,0) = #{filter.jsbz}
        </if>
        <!-- 录入日期-->
        <if test="filter.lrrqStart!=null and filter.lrrqEnd!=null">
            AND a.lrrq BETWEEN #{filter.lrrqStart} AND
            #{filter.lrrqEnd}
        </if>
        <!--抄表序号-->
        <if test="filter.cbxh !=null and filter.cbxh != ''">
            AND a.cbxh=#{filter.cbxh}
        </if>
        <!--用户编号-->
        <if test="filter.yhbh != null and filter.yhbh != ''">
            AND a.yhbh = #{filter.yhbh}
        </if>

        <if test="filter.sfssfb!=null">
            <if test="filter.sfssfb==1">
                AND not exists (select 1 from yh_bjfl_fb fb left join yh_bjfl_zb zb on
                fb.zb_id=zb.id where  <![CDATA[ fb.zb_id<>fb.id ]]> and zb.isdisabled='1' and
                (fb.yhbh||fb.cbkh)=a.cbkid)
            </if>
            <if test="filter.sfssfb==0">
                AND exists (select 1 from yh_bjfl_fb fb left join yh_bjfl_zb zb on
                fb.zb_id=zb.id where  <![CDATA[ fb.zb_id<>fb.id ]]> and zb.isdisabled='1' and
                (fb.yhbh||fb.cbkh)=a.cbkid)
            </if>
        </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.yhbhStart!=null">
            <![CDATA[ AND a.yhbh>=#{filter.yhbhStart} ]]>
        </if>
        <if test="filter.yhbhEnd!=null">
            <![CDATA[ AND a.yhbh<=#{filter.yhbhEnd} ]]>
        </if>
        <if test="filter.sbzt!=null">
            AND a.sbzt=#{filter.sbzt}
        </if>
        <if test="filter.sblb!=null">
            AND e.sblb=#{filter.sblb}
        </if>
        <if test="filter.cbly!=null">
            AND a.cbly=#{filter.cbly}
        </if>
        <if test="filter.kssl!=null and filter.jssl!=null">
            AND a.sl BETWEEN #{filter.kssl} AND #{filter.jssl}
        </if>
        <if test="filter.bfb!=null">
            AND ( ls.sl != 0 AND a.sl != 0 AND
            ((a.sl>=ls.sl AND round((a.sl-ls.sl)/ls.sl*100,2)>=#{filter.bfb})
            or (ls.sl>=a.sl AND round((ls.sl-a.sl)/a.sl*100,2)>=#{filter.bfb}))
            )
        </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">
            AND d.dxh = #{dxh}
        </if>
    </sql>

    <!--查询抄表录入历史-->
    <sql id="querySql1">
        SELECT
        a.yhbh, a.cbkh, a.cbkid, e.sbbh,e.sblc, d.yhxm, d.yhdz,d.lxdh, a.scds, a.bcds, a.jjl, a.sl AS sjsl,
        b.name AS cbfsmc, a.cbbz, a.shbz, a.jsbz, a.cblx, c.name AS cblxmc, a.jbzd, a.xbqd, a.jbbh, a.cbrq,
        a.lrrq, a.photo, a.bz, a.cbrybm, a.cbryxm, a.cbxh, a.cwrq,a.sscbb, f.ccname as sbzt,e.fmzt,
        a.qyfzrbm,a.qyfzrxm,a.lrrybm, a.lrryxm,e.cbpl cbpl
        FROM yy_cblr_ls a
        LEFT JOIN bm_cbfs b on a.cbfs = b.code
        LEFT JOIN bm_cblx c on a.cblx = c.code
        LEFT JOIN yh_khjbxx d on a.yhbh = d.yhbh
        LEFT JOIN yh_cbkxx e on a.yhbh = e.yhbh and a.cbkh=e.cbkh
        LEFT JOIN sys_child_code f on a.sbzt=f.cccode and pcid='34'
        <include refid="querySqlWhere2"></include>
    </sql>
    <sql id="querySqlWhere2">
        WHERE 1=1
        <!--模糊查询 -->
        <if test="filter.searchText!=null">
            AND ( a.yhbh like '%${filter.searchText}%'
            OR d.yhxm like '%${filter.searchText}%'
            OR d.yhdz like '%${filter.searchText}%' )
        </if>
        <!--所属公司-->
        <if test="filter.ssgs!=null">
            AND d.ssgs=#{filter.ssgs}
        </if>
        <!--所属片区-->
        <if test="filter.sscbpq!=null">
            AND d.sscbpq=#{filter.sscbpq}
        </if>
        <!--所属抄表本-->
        <if test="filter.sscbb!=null">
            <choose>
                <when test="filter.sscbb.indexOf(',')!=-1">
                    AND d.sscbb in
                    <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND d.sscbb=#{filter.sscbb}
                </otherwise>
            </choose>
        </if>
        <!-- 抄表方式-->
        <if test="filter.cbfs!=null">
            AND a.cbfs=#{filter.cbfs}
        </if>
        <!-- 用水性质（水价编码）-->
        <if test="filter.ysxz!=null">
            AND (e.sjbm in (select code from bm_sj where yslx=#{filter.ysxz})
            OR e.sjbm1 in (select code from bm_sj where yslx=#{filter.ysxz})
            OR e.sjbm2 in (select code from bm_sj where yslx=#{filter.ysxz})
            OR e.sjbm3 in (select code from bm_sj where yslx=#{filter.ysxz})
            )
        </if>
        <!-- 水表口径编码-->
        <if test="filter.sbkj!=null">
            AND e.sbkj=#{filter.sbkj}
        </if>
        <!-- 抄表人员编码-->
        <if test="filter.cbrybm != null and filter.cbrybm != ''">
            AND a.cbrybm=#{filter.cbrybm}
        </if>
        <!-- 财务日期-->
        <if test="filter.cwrq != null and filter.cwrq != ''">
            AND a.cwrq=#{filter.cwrq}
        </if>
        <!-- 抄表标志-->
        <if test="filter.cbbz!=null">
            AND nvl(a.cbbz,0)=#{filter.cbbz}
        </if>
        <!-- 审核标志-->
        <if test="filter.shbz!=null">
            AND nvl(a.shbz,0)=#{filter.shbz}
        </if>
        <!-- 计算标志-->
        <if test="filter.jsbz!=null">
            AND nvl(a.jsbz,0)=#{filter.jsbz}
        </if>
        <!-- 录入日期-->
        <if test="filter.lrrqStart!=null and filter.lrrqEnd!=null">
            AND a.lrrq BETWEEN #{filter.lrrqStart} AND #{filter.lrrqEnd}
        </if>
        <!--抄表序号-->
        <if test="filter.cbxh!=null">
            AND a.cbxh=#{filter.cbxh}
        </if>
        <!--用户编号-->
        <if test="filter.yhbh!=null">
            AND a.yhbh=#{filter.yhbh}
        </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.yhbhStart!=null">
            <![CDATA[ AND a.yhbh>=#{filter.yhbhStart} ]]>
        </if>
        <if test="filter.yhbhEnd!=null">
            <![CDATA[ AND a.yhbh<=#{filter.yhbhEnd} ]]>
        </if>
        <if test="filter.sbzt!=null">
            AND a.sbzt=#{filter.sbzt}
        </if>
        <if test="filter.sblb!=null">
            AND e.sbzt=#{filter.sblb}
        </if>
        <if test="filter.cbly!=null">
            AND a.cbly=#{filter.cbly}
        </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">
            AND d.dxh = #{filter.dxh}
        </if>
        <if test="filter.cblx!=null">
            AND a.cblx=#{filter.cblx}
        </if>
    </sql>

    <sql id="querySqldqorls">
        SELECT a.yhbh,
               a.cbkh,
               a.cbkid,
               a.scds,
               a.bcds,
               a.jjl,
               a.sl                                                                AS sjsl,
               a.cbfs,
               a.cbbz,
               a.shbz,
               a.jsbz,
               a.cblx,
               a.jbzd,
               a.xbqd,
               a.jbbh,
               a.cbrq,
               a.lrrq,
               a.photo,
               a.bz,
               a.cbrybm,
               a.cbryxm,
               a.cbxh,
               a.cwrq,
               a.sscbb,
               a.sbzt,
               a.lrrybm,
               a.lrryxm,
               a.cbcs,
               -- 动态决定连接哪个表
               CASE WHEN a.table_source = 'current' THEN e.sbbh ELSE ls_e.sbbh END AS sbbh,
               CASE WHEN a.table_source = 'current' THEN e.sblc ELSE ls_e.sblc END AS sblc,
               CASE WHEN a.table_source = 'current' THEN e.fmzt ELSE ls_e.fmzt END AS fmzt,
               d.yhxm,
               d.yhdz,
               d.lxdh,
               b.name                                                              AS cbfsmc,
               c.name                                                              AS cblxmc,
               f.ccname                                                            AS sbztmc,
               g.qyfzr                                                             AS qyfzrbm,
               g.qyfzrxm,
               a.table_source
        FROM ( -- 合并当前表和历史表
                 SELECT 'current' as table_source,
                        yhbh,
                        cbkh,
                        cbkid,
                        scds,
                        bcds,
                        jjl,
                        sl,
                        cbfs,
                        cbbz,
                        shbz,
                        jsbz,
                        cblx,
                        jbzd,
                        xbqd,
                        jbbh,
                        cbrq,
                        lrrq,
                        photo,
                        bz,
                        cbrybm,
                        cbryxm,
                        cbxh,
                        cwrq,
                        sscbb,
                        sbzt,
                        lrrybm,
                        lrryxm,
                        cbcs
                 FROM yy_cblr
                 UNION ALL
                 SELECT 'history' as table_source,
                        yhbh,
                        cbkh,
                        cbkid,
                        scds,
                        bcds,
                        jjl,
                        sl,
                        cbfs,
                        cbbz,
                        shbz,
                        jsbz,
                        cblx,
                        jbzd,
                        xbqd,
                        jbbh,
                        cbrq,
                        lrrq,
                        photo,
                        bz,
                        cbrybm,
                        cbryxm,
                        cbxh,
                        cwrq,
                        sscbb,
                        sbzt,
                        lrrybm,
                        lrryxm,
                        cbcs
                 FROM yy_cblr_ls) a
                 LEFT JOIN bm_cbfs b ON a.cbfs = b.code
                 LEFT JOIN bm_cblx c ON a.cblx = c.code
                 LEFT JOIN yh_khjbxx d ON a.yhbh = d.yhbh
            -- 根据数据来源动态连接不同的表
                 LEFT JOIN yh_cbkxx e ON a.cbkid = e.cbkid AND a.table_source = 'current'
                 LEFT JOIN yh_cbkxx ls_e ON a.yhbh = ls_e.yhbh AND a.cbkh = ls_e.cbkh AND a.table_source = 'history'
                 LEFT JOIN sys_child_code f ON a.sbzt = f.cccode AND f.pcid = '34'
                 LEFT JOIN bm_cbb g ON d.sscbb = g.cbbbm
    </sql>
    <select id="getPage" resultType="com.tofly.feesapi.cbgl.entity.vo.YyCblrSelect">
        <include refid="querySql"/>
    </select>

    <select id="getPage2" resultType="com.tofly.feesapi.cbgl.entity.vo.YyCblrSelect">
        <include refid="querySqldqorls"></include>
        <include refid="querySqlWhere2"></include>
    </select>

    <select id="getList" resultType="com.tofly.feesapi.cbgl.entity.vo.YyCblrSelect">
        <include refid="querySqldqorls"></include>
        <include refid="querySqlWhere2"></include>
        <if test="filter.orders!=null and !filter.orders.isEmpty()">
            order by
            <foreach item="item" index="index" collection="filter.orders" separator=",">
                ${item.column} ${item.asc? 'asc':'desc'}
            </foreach>
        </if>
    </select>


    <select id="getCbjlByYhbhs" resultType="com.tofly.feesapi.cbgl.entity.vo.YyCblrSelect">
        SELECT * FROM (
        SELECT
        a.yhbh, a.cbkh, a.cbkid, e.sbbh,e.sblc, d.yhxm, d.yhdz,d.lxdh, a.scds, a.bcds, a.jjl, a.sl AS sjsl,
        b.name AS cbfsmc, a.cbbz, a.shbz, a.jsbz, a.cblx, c.name AS cblxmc, a.jbzd, a.xbqd, a.jbbh, a.cbrq,
        a.lrrq, a.photo, a.bz, a.cbrybm, a.cbryxm, a.cbxh, a.cwrq,a.sscbb, f.ccname as sbzt,e.fmzt,
        g.qyfzr as qyfzrbm,g.qyfzrxm,a.lrrybm, a.lrryxm
        FROM yy_cblr a
        LEFT JOIN bm_cbfs b on a.cbfs = b.code
        LEFT JOIN bm_cblx c on a.cblx = c.code
        LEFT JOIN yh_khjbxx d on a.yhbh = d.yhbh
        LEFT JOIN yh_cbkxx e on a.yhbh = e.yhbh and a.cbkh=e.cbkh
        LEFT JOIN sys_child_code f on a.sbzt=f.cccode and f.pcid='34'
        LEFT JOIN bm_cbb g on a.sscbb = g.cbbbm
        WHERE 1=1
        <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.searchText!=null">
            AND ( a.yhbh like '%${filter.searchText}%'
            OR d.yhxm like '%${filter.searchText}%'
            OR d.yhdz like '%${filter.searchText}%'
            OR d.lxdh like '${filter.searchText}%'
            )
        </if>
        <if test="filter.cwrqStart!=null and filter.cwrqEnd!=null">
            AND a.cwrq BETWEEN #{filter.cwrqStart} AND #{filter.cwrqEnd}
        </if>
        <if test="filter.yhbhs != null and filter.yhbhs.size() > 0">
            AND a.yhbh IN
            <foreach collection="filter.yhbhs" open="(" separator="," item="yhbh" close=")">
                #{yhbh}
            </foreach>
        </if>

        UNION ALL

        SELECT
        a.yhbh, a.cbkh, a.cbkid, e.sbbh,e.sblc, d.yhxm, d.yhdz,d.lxdh, a.scds, a.bcds, a.jjl, a.sl AS sjsl,
        b.name AS cbfsmc, a.cbbz, a.shbz, a.jsbz, a.cblx, c.name AS cblxmc, a.jbzd, a.xbqd, a.jbbh, a.cbrq,
        a.lrrq, a.photo, a.bz, a.cbrybm, a.cbryxm, a.cbxh, a.cwrq,a.sscbb, f.ccname as sbzt,e.fmzt,
        a.qyfzrbm,a.qyfzrxm,a.lrrybm, a.lrryxm
        FROM yy_cblr_ls a
        LEFT JOIN bm_cbfs b on a.cbfs = b.code
        LEFT JOIN bm_cblx c on a.cblx = c.code
        LEFT JOIN yh_khjbxx d on a.yhbh = d.yhbh
        LEFT JOIN yh_cbkxx e on a.yhbh = e.yhbh and a.cbkh=e.cbkh
        LEFT JOIN sys_child_code f on a.sbzt=f.cccode and pcid='34'
        WHERE 1=1

        <if test="filter.searchText!=null">
            AND ( a.yhbh like '%${filter.searchText}%'
            OR d.yhxm like '%${filter.searchText}%'
            OR d.yhdz like '%${filter.searchText}%' )
        </if>
        <if test="filter.ssgs!=null">
            AND d.ssgs=#{filter.ssgs}
        </if>
        <if test="filter.sscbpq!=null">
            AND d.sscbpq=#{filter.sscbpq}
        </if>
        <if test="filter.sscbb!=null">
            <choose>
                <when test="filter.sscbb.indexOf(',')!=-1">
                    AND d.sscbb in
                    <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND d.sscbb=#{filter.sscbb}
                </otherwise>
            </choose>
        </if>
        <if test="filter.cwrqStart!=null and filter.cwrqEnd!=null">
            AND a.cwrq BETWEEN #{filter.cwrqStart} AND #{filter.cwrqEnd}
        </if>
        <if test="filter.yhbhs != null and filter.yhbhs.size() > 0">
            AND a.yhbh IN
            <foreach collection="filter.yhbhs" open="(" separator="," item="yhbh" close=")">
                #{yhbh}
            </foreach>
        </if>
        )
    </select>


    <select id="lycbglfy" resultType="com.tofly.feesapi.cbgl.entity.vo.YyCblrSelect">
        SELECT
        a.yhbh, a.cbkh, a.cbkid, e.sbbh,e.sblc, d.yhxm, d.yhdz,d.lxdh,
        a.scds, a.bcds, a.jjl, a.sl AS sjsl, a.xycb,
        b.name AS cbfsmc, a.cbbz, a.shbz, a.jsbz, a.cblx, c.name AS cblxmc, a.jbzd, a.xbqd, a.jbbh, a.cbrq,
        a.lrrq, a.photo, a.bz, a.cbrybm, a.cbryxm, a.cbxh, a.cwrq,a.sscbb,
        f.ccname as sbzt,
        e.fmzt, e.cbpl,
        g.qyfzr as qyfzrbm, g.qyfzrxm, a.lrrybm, a.lrryxm
        FROM yy_cblr a
        LEFT JOIN bm_cbfs b ON a.cbfs = b.code
        LEFT JOIN bm_cblx c ON a.cblx = c.code
        LEFT JOIN yh_khjbxx d ON a.yhbh = d.yhbh
        LEFT JOIN yh_cbkxx e ON a.yhbh = e.yhbh and a.cbkh=e.cbkh
        LEFT JOIN sys_child_code f ON a.sbzt=f.cccode and f.pcid='34'
        LEFT JOIN bm_cbb g ON a.sscbb = g.cbbbm
        WHERE 1=1 AND e.jfzt='1'
        <!--所属公司-->
        <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 and filter.sscbb != ''">
            <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.cwrq!= null and filter.cwrq != ''">
            AND a.cwrq = #{filter.cwrq}
        </if>

        <if test="filter.cbrybm != null and filter.cbrybm != ''">
            AND a.cbrybm = #{filter.cbrybm}
        </if>
        <if test="filter.cbbz != null">
            AND nvl(a.cbbz, 0)=#{filter.cbbz}
        </if>
        <if test="filter.shbz != null">
            AND nvl(a.shbz, 0) = #{filter.shbz}
        </if>

        <if test="filter.searchText!=null">
            AND ( a.yhbh like '%${filter.searchText}%'
            OR d.yhxm like '%${filter.searchText}%'
            OR d.yhdz like '%${filter.searchText}%'
            OR d.lxdh like '${filter.searchText}%'
            )
        </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">
            AND d.dxh = #{dxh}
        </if>
    </select>

    <select id="getViewById" resultType="com.tofly.feesapi.cbgl.entity.vo.YyCblrView">
        SELECT a.yhbh,
               a.cwrq,
               a.cbkh,
               b.sbbh,
               d.yhxm,
               d.lxdh,
               d.yhdz,
               b.sblc,
               a.cbfs,
               a.cblx,
               a.scds,
               a.bcds,
               a.jjl,
               a.bcds - a.scds AS sjsl,
               a.bz,
               a.photo,
               f.ccname        as sbzt
        FROM yy_cblr a
                 LEFT JOIN yh_cbkxx b on a.yhbh = b.yhbh and a.cbkh = b.cbkh
                 LEFT JOIN yh_khjbxx d on a.yhbh = d.yhbh
                 LEFT JOIN sys_child_code f on a.sbzt = f.cccode and pcid = '34'
        WHERE a.yhbh = #{yhbh}
          and a.cbkh = #{cbkh}
    </select>

    <select id="cbsltj" resultType="com.tofly.feesapi.cbgl.entity.vo.CbSltjSelect">
        <if test="filter.tjlb=='cbry'">
            SELECT
            cbryxm AS tjlb,
            NVL(COUNT(1), 0) AS cbhs,
            NVL(SUM(sl), 0) AS cbsl FROM
            YY_CBLR
            WHERE jfzt = '1' AND cbbz = '1'
            <if test="filter.ssgs != null and filter.ssgs!= ''">
                AND ssgs = #{filter.ssgs}
            </if>
            <if test="filter.sscbpq != null and filter.sscbpq != ''">
                AND sscbpq = #{filter.sscbpq}
            </if>
            <if test="filter.sscbb != null and filter.sscbb != ''">
                AND sscbb = #{filter.sscbb}
            </if>
            GROUP BY
            cbryxm
        </if>
        <if test="filter.tjlb=='ysxz'">
            SELECT
            a.NAME AS tjlb,
            NVL(COUNT(1),0) AS cbhs,
            NVL(SUM(sl),0) AS cbsl
            FROM bm_yslx a
            Left JOIN BM_SJ b ON b.yslx = a.CODE
            Left JOIN YH_CBKXX c ON c.sjbm=b.code
            Left join YY_CBLR d ON c.CBKID = d.CBKID
            WHERE d.jfzt = '1'
            AND d.cbbz = '1'
            <if test="filter.ssgs!=null">
                AND d.ssgs = #{filter.ssgs}
            </if>
            <if test="filter.sscbpq!=null">
                AND d.sscbpq = #{filter.sscbpq}
            </if>
            <if test="filter.sscbb!=null">
                <choose>
                    <when test="filter.sscbb.indexOf(',')!=-1">
                        AND d.sscbb in
                        <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                                 close=")">
                            #{item}
                        </foreach>
                    </when>
                    <otherwise>
                        AND d.sscbb=#{filter.sscbb}
                    </otherwise>
                </choose>
            </if>
            GROUP BY
            a.NAME
        </if>
        <if test="filter.tjlb=='cbb'">
            SELECT
            b.CBBMC AS tjlb,
            NVL(COUNT(1),0) AS cbhs,
            NVL(SUM(sl),0) AS cbsl
            FROM
            YY_CBLR a
            LEFT JOIN BM_CBB b ON a.sscbb = b.cbbbm
            WHERE
            a.jfzt = '1'
            AND a.cbbz = '1'
            <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
            b.CBBMC
        </if>
    </select>

    <select id="cbltj" resultType="com.tofly.feesapi.cbgl.entity.vo.CbltjSelect">
        <if test="filter.tjlb=='cbry'">
            SELECT
            b.CBY AS tjlb,
            COUNT(1) AS ycbs,
            SUM(CASE WHEN a.cbbz = '1' THEN 1 ELSE 0 END) AS scbs,
            SUM(CASE WHEN a.cbbz = '1' THEN sl ELSE 0 END) AS scsl
            FROM
            YY_CBLR a
            LEFT JOIN BM_CBB b ON a.sscbb = b.cbbbm
            WHERE a.jfzt = '1' and a.xycb=1
            <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
            b.CBY
        </if>

        <if test="filter.tjlb=='cbb'">
            SELECT
            b.cby AS cby,
            b.cbbmc AS tjlb,
            COUNT(1) AS ycbs,
            SUM(CASE WHEN a.cbbz = '1' THEN 1 ELSE 0 END) AS scbs,
            SUM(CASE WHEN a.cbbz = '1' THEN sl ELSE 0 END) AS scsl
            FROM
            YY_CBLR a
            LEFT JOIN BM_CBB b ON a.sscbb = b.cbbbm
            WHERE a.jfzt = '1' and a.xycb=1
            <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 b.cbbmc, b.cby
            order by b.cby asc, b.cbbmc asc
        </if>
    </select>
    <select id="getZfCblrMap" resultType="java.util.Map">
        SELECT COUNT(0)                                                       FBS,
               NVL(SUM(CASE WHEN CBBZ = 1 and SHBZ = 1 THEN 1 ELSE 0 END), 0) FBYCS,
               NVL(SUM(SL), 0)                                                FBHJSL
        FROM YY_CBLR a
        WHERE exists(select 1
                     from yh_bjfl_fb fb
                              left join yh_bjfl_zb zb on fb.zb_id = zb.id
                     where  <![CDATA[ fb.zb_id <> fb.id ]]> and zb.type_id = '1'
                       and zb.isdisabled = '1'
                       and (fb.yhbh || fb.cbkh) = a.cbkid
                       and (zb.yhbh || zb.cbkh) = #{cbkid})

    </select>

    <select id="getZmCblrMap" resultType="java.util.Map">
        SELECT COUNT(0)                                                       ZBS,
               NVL(SUM(CASE WHEN CBBZ = 1 and SHBZ = 1 THEN 1 ELSE 0 END), 0) ZBYCS,
               NVL(SUM(SL), 0)                                                ZBHJSL
        FROM YY_CBLR a
        WHERE exists(select 1
                     from yh_bjfl_fb fb
                              left join yh_bjfl_zb zb on fb.zb_id = zb.id
                     where  <![CDATA[ fb.zb_id <> fb.id ]]> and zb.type_id = '2'
                       and zb.isdisabled = '1'
                       and (fb.yhbh || fb.cbkh) = a.cbkid
                       and (zb.yhbh || zb.cbkh) = #{cbkid})
    </select>

    <insert id="YMJZSFSB">
        INSERT INTO YY_CBLR_YMJZSFSB

        SELECT *
        FROM YY_CBLR
        WHERE CBBZ = '1'
          AND SHBZ = '1'
          AND JSBZ = '0'
    </insert>

    <insert id="yyCblrls">
        insert into yy_cblr_ls(ID, YHBH, CBKH, CBKID, SSGS,
                               SSCBPQ, SSCBB, CWRQ, SCDS, BCDS,
                               JJL, CBRYBM, CBRYXM, CBRQ, LRRYBM,
                               LRRYXM, LRRQ, PHOTO, CBBZ, SHBZ, JSBZ,
                               CBCS, CBXH, BZ, CBLX, CBFS, JFZT, SL,
                               JBZD, XBQD, JBBH, CBLY, X, Y,
                               SHRY, SHSJ, SHSM, SSREGION, SHRYXM,
                               NBZXDS, NBZXCBRQ, JSSL, SBZT, xycb,
                               QYFZRBM, QYFZRXM,SCCBRQ)
        select sys_guid(),
               YHBH,
               CBKH,
               CBKID,
               a.SSGS,
               a.SSCBPQ,
               a.SSCBB,
               CWRQ,
               SCDS,
               BCDS,
               JJL,
               CBRYBM,
               CBRYXM,
               CBRQ,
               LRRYBM,
               LRRYXM,
               LRRQ,
               PHOTO,
               CBBZ,
               SHBZ,
               JSBZ,
               CBCS,
               CBXH,
               a.BZ,
               CBLX,
               CBFS,
               JFZT,
               SL,
               JBZD,
               XBQD,
               JBBH,
               CBLY,
               X,
               Y,
               SHRY,
               SHSJ,
               SHSM,
               SSREGION,
               SHRYXM,
               NBZXDS,
               NBZXCBRQ,
               JSSL,
               SBZT,
               xycb,
               b.qyfzr,
               b.qyfzrxm,
               SCCBRQ
        from yy_cblr a
                 left join bm_cbb b on a.sscbb = b.cbbbm
        where a.ssgs = '1'
    </insert>

    <sql id="cblrcxsql">
        SELECT a.shryxm   AS shryxm,
               a.yhbh     AS yhbh,
               a.cbkh     AS cbkh,
               a.cbkid    AS cbkid,
               a.ssgs     AS ssgs,
               a.sscbpq   AS sscbpq,
               a.sscbb    AS sscbb,
               a.cwrq     AS cwrq,
               a.scds     AS scds,
               a.bcds     AS bcds,
               a.jjl      AS jjl,
               a.cbrybm   AS cbrybm,
               a.cbryxm   AS cbryxm,
               a.cbrq     AS cbrq,
               a.lrrybm   AS lrrybm,
               a.lrryxm   AS lrryxm,
               a.lrrq     AS lrrq,
               a.photo    AS photo,
               a.cbbz     AS cbbz,
               a.shbz     AS shbz,
               a.jsbz     AS jsbz,
               a.cbcs     AS cbcs,
               a.cbxh     AS cbxh,
               a.bz       AS bz,
               a.cblx     AS cblx,
               a.cbfs     AS cbfs,
               a.jfzt     AS jfzt,
               a.sl       AS sl,
               a.jbzd     AS jbzd,
               a.xbqd     AS xbqd,
               a.jbbh     AS jbbh,
               a.cbly     AS cbly,
               a.x        AS x,
               a.y        AS y,
               a.shry     AS shry,
               a.shsj     AS shsj,
               a.shsm     AS shsm,
               a.ssregion AS ssregion,
               a.nbzxds   AS nbzxds,
               a.nbzxcbrq AS nbzxcbrq,
               a.jssl     AS jssl,
               a.sbzt     AS sbzt,
               b.qyfzr    AS qyfzrbm,
               b.qyfzrxm  AS qyfzrxm
        FROM yy_cblr a
                 LEFT JOIN
             bm_cbb b ON a.sscbb = b.cbbbm
        WHERE a.ssgs = '1'
    </sql>

    <select id="plcxcblrmx" resultType="com.tofly.feesapi.cbgl.entity.YyCblrLs">
        <include refid="cblrcxsql"></include>
    </select>

    <select id="cxcblrmx" resultType="com.tofly.feesapi.cbgl.entity.YyCblrLs">
        <include refid="cblrcxsql"></include>
    </select>

    <update id="updateCbb">
        UPDATE (
        SELECT SBZD,
               ZXCBRQ,
               BCDS,
               CBRQ
        FROM YY_CBLR A,
             YH_CBKXX B
        WHERE A.YHBH = B.YHBH
          AND A.CBKH = B.CBKH )
        SET
            SBZD = BCDS, ZXCBRQ = CBRQ
    </update>

    <select id="getListCbmx" resultType="com.tofly.feesapi.cbgl.entity.vo.YyCblrSelect">
        <include refid="querySql"></include>
        UNION ALL
        <include refid="querySql1"></include>
    </select>

    <select id="getCbkids" resultType="java.lang.String">
        SELECT a.cbkid
        FROM yy_cblr a
        LEFT JOIN bm_cbfs b on a.cbfs = b.code
        LEFT JOIN bm_cblx c on a.cblx = c.code
        LEFT JOIN yh_khjbxx d on a.yhbh = d.yhbh
        LEFT JOIN yh_cbkxx e on a.yhbh = e.yhbh and a.cbkh=e.cbkh
        WHERE 1=1 and e.jfzt=1
        <!--模糊查询 -->
        <if test="filter.searchText!=null">
            AND ( a.yhbh like '%${filter.searchText}%'
            OR d.yhxm like '%${filter.searchText}%'
            OR d.yhdz like '%${filter.searchText}%'
            OR d.lxdh like '${filter.searchText}%'
            )
        </if>
        <!--所属公司-->
        <if test="filter.ssgs!=null">
            AND d.ssgs=#{filter.ssgs}
        </if>
        <!--所属片区-->
        <if test="filter.sscbpq!=null">
            AND d.sscbpq=#{filter.sscbpq}
        </if>
        <!--所属抄表本-->
        <if test="filter.sscbb!=null">
            <choose>
                <when test="filter.sscbb.indexOf(',')!=-1">
                    AND d.sscbb in
                    <foreach item="item" index="index" collection="filter.sscbb.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND d.sscbb=#{filter.sscbb}
                </otherwise>
            </choose>
        </if>
        <if test="filter.shbz!=null">
            AND a.shbz=#{filter.shbz}
        </if>
        <!-- 抄表方式-->
        <if test="filter.cbfs!=null">
            AND a.cbfs=#{filter.cbfs}
        </if>
        <!-- 用水性质（水价编码）-->
        <if test="filter.ysxz!=null">
            AND (e.sjbm in (select code from bm_sj where yslx=#{filter.ysxz})
            OR e.sjbm1 in (select code from bm_sj where yslx=#{filter.ysxz})
            OR e.sjbm2 in (select code from bm_sj where yslx=#{filter.ysxz})
            OR e.sjbm3 in (select code from bm_sj where yslx=#{filter.ysxz})
            )
        </if>
        <!-- 水表口径编码-->
        <if test="filter.sbkj!=null">
            AND e.sbkj=#{filter.sbkj}
        </if>
        <!-- 抄表人员编码-->
        <if test="filter.cbrybm!=null">
            AND a.cbrybm=#{filter.cbrybm}
        </if>
        <!-- 财务日期-->
        <if test="filter.cwrq!=null">
            AND a.cwrq=#{filter.cwrq}
        </if>
        <!-- 录入日期-->
        <if test="filter.lrrqStart!=null and filter.lrrqEnd!=null">
            AND a.lrrq BETWEEN #{filter.lrrqStart} AND #{filter.lrrqEnd}
        </if>
        <if test="filter.kssl!=null and filter.jssl!=null">
            AND a.sl BETWEEN #{filter.kssl} AND #{filter.jssl}
        </if>
    </select>

    <select id="stat" resultType="com.tofly.feesapi.sfgl.entity.vo.YyStat">
        select sum(sl) sl from(
        select nvl(sl, 0) sl
        from yy_cblr
        where yhbh = #{filter.yhbh}
        <if test="filter.ssgs!=null">
            and ssgs = #{filter.ssgs}
        </if>
        union all
        select nvl(sl, 0) sl
        from yy_cblr_ls
        where yhbh = #{filter.yhbh}
        <if test="filter.ssgs!=null">
            and ssgs = #{filter.ssgs}
        </if>
        ) a
    </select>

    <select id="getZbFbs" resultType="java.util.Map">
        select (a.yhbh || a.cbkh) cbkid, count(1) num
        from yh_bjfl_zb a
                 left join yh_bjfl_fb b on a.id = b.zb_id
                 left join yy_cblr c on a.yhbh = c.yhbh and a.cbkh = c.cbkh
        where  <![CDATA[ b.zb_id <> b.id ]]>  and a.isdisabled = '1'
          and c.sl > 0
        group by a.yhbh, a.cbkh
    </select>

    <select id="zcbcszsltj" resultType="com.tofly.feesapi.cbgl.entity.dto.ZcbcssltjDto">

        SELECT
        COUNT(*) AS zcbcs,
        SUM(sl) AS zcbsl
        FROM (
        SELECT
        NVL( a.SL, 0 ) AS sl
        FROM
        yy_cblr a
        LEFT JOIN yh_khjbxx d ON a.yhbh = d.yhbh
        WHERE
        a.SHBZ = '1'
        <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.searchText != null and filter.searchText != ''">
            AND ( a.yhbh like '%${filter.searchText}%'
            OR d.yhxm like '%${filter.searchText}%'
            OR d.yhdz like '%${filter.searchText}%'
            OR d.lxdh like '${filter.searchText}%'
            )
        </if>

        <if test="filter.cwrqStart != null and filter.cwrqEnd != null">
            AND a.cwrq BETWEEN #{filter.cwrqStart} AND #{filter.cwrqEnd}
        </if>

        UNION ALL

        SELECT
        NVL( ls.SL, 0 ) AS sl
        FROM
        yy_cblr_ls ls
        LEFT JOIN yh_khjbxx d ON ls.yhbh = d.yhbh
        WHERE
        ls.SHBZ = '1'
        <if test="filter.ssgs != null and filter.ssgs != ''">
            AND ls.ssgs = #{filter.ssgs}
        </if>

        <if test="filter.sscbpq != null and filter.sscbpq != ''">
            AND ls.sscbpq = #{filter.sscbpq}
        </if>

        <if test="filter.sscbb != null and filter.sscbb != ''">
            AND ls.sscbb = #{filter.sscbb}
        </if>

        <if test="filter.searchText != null and filter.searchText != ''">
            AND ( ls.yhbh like '%${filter.searchText}%'
            OR d.yhxm like '%${filter.searchText}%'
            OR d.yhdz like '%${filter.searchText}%'
            OR d.lxdh like '${filter.searchText}%'
            )
        </if>

        <if test="filter.cwrqStart != null and filter.cwrqEnd != null">
            AND ls.cwrq BETWEEN #{filter.cwrqStart} AND #{filter.cwrqEnd}
        </if>
        )

    </select>


    <select id="getCblrSl" resultType="com.tofly.feesapi.cbgl.entity.vo.CblrSflxMxSelect">

        select e.code,e.name,nvl(sum(sl),0) sl
        from yy_cblr a left join yh_cbkxx b on a.cbkid=b.cbkid
        left join bm_sj c on b.sjbm=c.code
        left join bm_yslx e on c.yslx=e.code
        where a.cbbz=1
        <if test="filter.yslx!=null">
            and e.code=#{filter.yslx}
        </if>

        <if test="filter.cwrq!=null">
            and a.cwrq=#{filter.cwrq}
        </if>
        group by e.code,e.name
    </select>


    <select id="getCblrSlLs" resultType="com.tofly.feesapi.cbgl.entity.vo.CblrSflxMxSelect">

        select e.code,e.name,nvl(sum(sl),0) sl from yy_cblr_ls a left join yh_cbkxx b on a.cbkid=b.cbkid
        left join bm_sj c on b.sjbm=c.code
        left join bm_yslx e on c.yslx=e.code
        where a.cbbz=1

        <if test="filter.yslx!=null">
            and e.code=#{filter.yslx}
        </if>

        <if test="filter.cwrq!=null">
            and a.cwrq=#{filter.cwrq}
        </if>
        group by e.code,e.name
    </select>


    <select id="retportSlfx" resultType="com.tofly.feesapi.cbgl.entity.vo.CblrSflxMxSelect">
        select a.code,sum(sl) sl from bm_yslx a
        left join (
        select a.*,c.yslx from (
        select yhbh,cbkid,cwrq,sl,sscbpq from yy_cblr_ls where cbbz=1
        union
        select yhbh,cbkid,cwrq,sl,sscbpq from yy_cblr where cbbz=1
        ) a left join yh_cbkxx b on a.cbkid=b.cbkid
        left join bm_sj c on b.sjbm=c.code
        left join yh_khjbxx e on b.yhbh=e.yhbh and e.yhzt='1'

        ) aa on a.code=aa.yslx
        where
        1=1
        <if test="filter.cwrq!=null and filter.cwrqend!=null ">
            and (aa.cwrq) between #{filter.cwrq} and #{filter.cwrqend}
        </if>

        <if test="filter.sspq!=null and filter.sspq!=null ">
            and aa.sscbpq=#{filter.sspq}
        </if>

        group by a.code
        order by code asc

    </select>


    <select id="retportSlfxMx" resultType="com.tofly.feesapi.cbgl.entity.vo.CblrSflxSelect">

        select a.yhbh,b.yhxm,a.sl dqsl,a.cwrq,nvl(e.sl,0) sysl,(a.sl-nvl(e.sl,0))/nvl(e.sl,1)*100 tbbl,nvl(f.sl,0)
        sndsl,(a.sl-nvl(f.sl,0))/nvl(f.sl,1)*100 hbbl from (
        select yhbh,sl,cwrq,cbkid from yy_cblr
        union all
        select yhbh, sl,cwrq,cbkid from yy_cblr_ls
        ) a left join yh_khjbxx b on a.yhbh=b.yhbh
        left join yh_cbkxx c on a.cbkid=c.cbkid

        <if test="cwrq1!=null and cwrq1!=null">
            left join yy_cblr_ls e on a.cwrq=#{cwrq1} and e.cbkid=a.cbkid
        </if>

        <if test="cwrq2!=null and cwrq2!=null">
            left join yy_cblr_ls f on a.cwrq=#{cwrq2} and e.cbkid=a.cbkid
        </if>

        where 1=1
        <if test="filter.cwrq!=null">
            and a.cwrq=#{filter.cwrq}
        </if>
        <if test="filter.sspq!=null">
            and b.sscbpq=#{filter.sspq}
        </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.yslx!=null">
            and c.sjbm=#{filter.yslx}
        </if>
        <if test="filter.yhlx!=null">
            and b.dxh=#{filter.yhlx}
        </if>
        <if test="filter.bl!=null">
            and (a.sl-nvl(e.sl,0))/nvl(e.sl,1)*100>#{filter.bl}
        </if>

    </select>


    <select id="retportSlfxMxByYhbh" resultType="com.tofly.feesapi.cbgl.entity.vo.CblrSflxYhbhSelect">

        select yhbh,sl,cwrq from ${tbName}
        where 1=1
        <if test="filter.cwrq!=null and filter.cwrq!=null">
            and cwrq=#{filter.cwrq}
        </if>

        <if test="filter.yhbh!=null and filter.yhbh!=null">
            and yhbh=#{filter.yhbh}
        </if>
    </select>


    <select id="getCblrCount" resultType="com.tofly.feesapi.cbgl.entity.vo.CblrCountSelect">
        SELECT sum(nvl(sl,0)) zysl,sum(nvl(scds,0)) zyscds,sum(nvl(bcds,0)) zybcds
        FROM ( -- 合并当前表和历史表
        SELECT 'current' as table_source,
        yhbh,
        cbkh,
        cbkid,
        scds,
        bcds,
        jjl,
        sl,
        cbfs,
        cbbz,
        shbz,
        jsbz,
        cblx,
        jbzd,
        xbqd,
        jbbh,
        cbrq,
        lrrq,
        photo,
        bz,
        cbrybm,
        cbryxm,
        cbxh,
        cwrq,
        sscbb,
        sbzt,
        lrrybm,
        lrryxm,
        cbcs
        FROM yy_cblr
        UNION ALL
        SELECT 'history' as table_source,
        yhbh,
        cbkh,
        cbkid,
        scds,
        bcds,
        jjl,
        sl,
        cbfs,
        cbbz,
        shbz,
        jsbz,
        cblx,
        jbzd,
        xbqd,
        jbbh,
        cbrq,
        lrrq,
        photo,
        bz,
        cbrybm,
        cbryxm,
        cbxh,
        cwrq,
        sscbb,
        sbzt,
        lrrybm,
        lrryxm,
        cbcs
        FROM yy_cblr_ls) a
        LEFT JOIN bm_cbfs b ON a.cbfs = b.code
        LEFT JOIN bm_cblx c ON a.cblx = c.code
        LEFT JOIN yh_khjbxx d ON a.yhbh = d.yhbh
        -- 根据数据来源动态连接不同的表
        LEFT JOIN yh_cbkxx e ON a.cbkid = e.cbkid AND a.table_source = 'current'
        LEFT JOIN yh_cbkxx ls_e ON a.yhbh = ls_e.yhbh AND a.cbkh = ls_e.cbkh AND a.table_source = 'history'
        LEFT JOIN sys_child_code f ON a.sbzt = f.cccode AND f.pcid = '34'
        LEFT JOIN bm_cbb g ON d.sscbb = g.cbbbm
        <include refid="querySqlWhere2"></include>
    </select>


</mapper>
