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


    <!--自定义获取用户最大yhbh -->
    <select id="selectMaxYhbh_jk" resultType="int">
        SELECT nvl(max(yhbh), 0)
        from yh_khjbxx
        where yhbh like '3%'
    </select>

    <select id="selectMaxYhbh_fjk" resultType="int">
        SELECT nvl(max(yhbh), 0)
        from yh_khjbxx
        where yhbh like '2%'
    </select>

    <!--自定义获取用户是否存在 -->
    <select id="selectExistYhbh" resultType="int">
        SELECT COUNT(0) sl
        FROM yh_khjbxx
        WHERE yhbh = #{yhbh}
    </select>

    <!--查询当前财务日期cwrq -->
    <select id="selectCwrq" resultType="java.util.Map">
        select cwrq
        from sys_gdcsb
        where gdbz = '0'
    </select>

    <!--查询用户余额 -->
    <select id="selectUserBalance" resultType="java.util.Map">
        SELECT yhbh, nvl(yfje, 0) yfje, nvl(kjje, 0) kjje, nvl(syje, 0) syje, nvl(nbsyje, 0) nbsyje
        FROM yy_zjzh
        WHERE yhbh = #{yhbh}
    </select>

    <!-- 查询当前用户是否存在多只表 -->
    <select id="selectCbkh" resultType="com.tofly.feesapi.yhgl.entity.dto.CbkxxDto">
        SELECT cbkh,
               cbkid,
               jfzt
        FROM yh_cbkxx
        WHERE yhbh = #{yhbh}
    </select>

    <!-- 水表档案是否存在-->
    <select id="selectExistCbkxx" resultType="int">
        SELECT COUNT(0) sl
        FROM yh_cbkxx
        WHERE yhbh = #{yhbh}
    </select>

    <select id="selectYhDa" resultType="com.tofly.feesapi.yhgl.entity.Yhkhjbxx2">
        select a.*, e.cbpqmc AS sscbpqmc, f.cbbmc AS sscbbmc
        FROM yh_khjbxx a
                 LEFT JOIN bm_cbpq e on a.sscbpq = e.cbpqbm
                 LEFT JOIN bm_cbb f on a.sscbb = f.cbbbm
        WHERE a.yhbh = #{yhbh}
    </select>

    <select id="getYhDbhxxPage" resultType="com.tofly.feesapi.yhgl.entity.vo.YhkhjbxxDbhSelect">
        <include refid="yhkhjbxx_dbhsql"></include>
        union all
        <include refid="dbhsql"></include>
    </select>
    <sql id="yhkhjbxx_dbhsql">
        select
        a.yhbh,
        a.yhxm,
        a.yhdz,
        a.lxdh,
        a.dbhndljl,
        b.sdsl as dbhyhsdsl,
        b.sdys as dbhyhsdyf,
        to_char(sysdate,'yyyyMM')as yhyear,
        e.cbpqmc as sscbpqmc,
        f.cbbmc as sscbbmc
        from yh_khjbxx a
        left join bm_cbpq e on a.sscbpq = e.cbpqbm
        left join bm_cbb f on a.sscbb = f.cbbbm
        left join bm_dbhysyh b on b.year= to_char(sysdate,'yyyy')
        where 1=1 and khlb='2'
        <!--模糊查询 -->
        <if test="filter.searchText!=null">
            AND ( a.yhbh like '%${filter.searchText}%'
            OR a.yhxm like '%${filter.searchText}%'
            OR a.yhdz like '%${filter.searchText}%'
            OR a.lxdh like '%${filter.searchText}%')
        </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.yhbhStart!=null">
            <![CDATA[ AND a.yhbh>=#{filter.yhbhStart} ]]>
        </if>
        <if test="filter.yhbhEnd!=null">
            <![CDATA[ AND a.yhbh<=#{filter.yhbhEnd} ]]>
        </if>
        <if test="filter.year!=null">
            AND to_char(sysdate,'yyyyMM')=#{filter.year}
        </if>
    </sql>
    <sql id="dbhsql">
        select
        a.yhbh,
        a.yhxm,
        a.yhdz,
        a.lxdh,
        a.dbhndljl,
        a.dbhyhsdsl,
        a.dbhyhsdyf,
        a.yhyear,
        e.cbpqmc AS sscbpqmc,
        f.cbbmc AS sscbbmc
        FROM yh_khjbxx_dbhls a
        LEFT JOIN bm_cbpq e on a.sscbpq = e.cbpqbm
        LEFT JOIN bm_cbb f on a.sscbb = f.cbbbm
        where 1=1
        <!--模糊查询 -->
        <if test="filter.searchText!=null">
            AND ( a.yhbh like '%${filter.searchText}%'
            OR a.yhxm like '%${filter.searchText}%'
            OR a.yhdz like '%${filter.searchText}%'
            OR a.lxdh like '%${filter.searchText}%')
        </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.yhbhStart!=null">
            <![CDATA[ AND a.yhbh>=#{filter.yhbhStart} ]]>
        </if>
        <if test="filter.yhbhEnd!=null">
            <![CDATA[ AND a.yhbh<=#{filter.yhbhEnd} ]]>
        </if>
        <if test="filter.year!=null">
            AND a.yhyear=#{filter.year}
        </if>
    </sql>

    <!--查询未分配抄表本用户-->
    <sql id="querySql_cbbwfp">
        select
        a.*,
        b.cbpqmc sscbpqmc,
        c.cbbmc sscbbmc
        from
        yh_khjbxx a
        left join
        bm_cbpq b on a.sscbpq=b.cbpqbm
        left join
        bm_cbb c on a.sscbb = c.cbbbm
        where
        1=1
        and
        a.sscbb is null
        <!--模糊查询 -->
        <if test="filter.searchText!=null">
            AND ( a.yhbh like '%${filter.searchText}%'
            OR a.yhxm like '%${filter.searchText}%'
            OR a.yhdz like '%${filter.searchText}%'
            OR a.lxdh like '%${filter.searchText}%')
        </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.cby!=null">
            AND c.cby=#{filter.cby}
        </if>
        <if test="filter.qyfzr!=null">
            AND c.qyfzr=#{filter.qyfzr}
        </if>
        order by
        a.yhbh asc
    </sql>

    <!--查询已分配抄表本用户-->
    <sql id="querySql_cbbyfp">
        select a.*,b.cbpqmc sscbpqmc,c.cbbmc sscbbmc from yh_khjbxx a
        left join bm_cbpq b on a.sscbpq=b.cbpqbm
        left join bm_cbb c on a.sscbb = c.cbbbm
        where 1=1 and sscbb is not null
        <!--模糊查询 -->
        <if test="filter.searchText!=null">
            AND ( a.yhbh like '%${filter.searchText}%'
            OR a.yhxm like '%${filter.searchText}%'
            OR a.yhdz like '%${filter.searchText}%'
            OR a.lxdh like '%${filter.searchText}%')
        </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.cby!=null">
            AND c.cby=#{filter.cby}
        </if>
        <if test="filter.qyfzr!=null">
            AND c.qyfzr=#{filter.qyfzr}
        </if>
        order by a.yhbh asc
    </sql>

    <!--    yh_cbkxx b on a.yhbh=b.yhbh -->
    <sql id="querySql">
        SELECT
        a.yhbh,a.ykhbh, a.yhxm,a.yhdz,a.lxdh, a.ssgs,a.sscbpq,a.sscbb,a.xb,a.sj,a.zzdh,a.gzdw,a.bgdh,
        a.dzyx,a.zjlb,a.zjhm,a.sqrq,
        a.jdrq,a.yhzt,a.rks,a.hzgx,a.zsjb,a.tsbz,a.tsrq,a.htqd,a.htqdrq,a.htbh,a.bz,a.bz1,a.bz_gcbh,a.bz_pcxlh,a.czrybm,a.czryxm,
        a.czsj,a.psfz,a.pht,a.pgszz,a.pyyzz,a.pyxzj,a.xydj,a.zw,a.jdrybm,a.jdryxm,a.jdpcxlh,a.sfqz,a.cbryxm,a.xh,
        a.ssregion,a.xgrybm,a.jtzqljl,a.fplx,
        (case
        when a.fplx='1' then '普票'
        when a.fplx='2' then '专票'
        else ''
        end) fplxmc,
        a.fp_name,
        a.fp_nsrsbh,
        a.fp_dz,
        a.fp_lxdh,
        a.fp_bankname,
        a.fp_bankcode,
        a.fp_bz,
        a.fffs,h.name AS fffsmc,
        a.khlb,a.pyjm,a.yljl, g.mc AS ssgsmc, e.cbpqmc AS sscbpqmc, f.cbbmc AS sscbbmc,
        d.name AS sblbmc,b.sbqs, b.sbbh,b.sblb,b.sbsccj,b.sbxh,b.sbkj,b.sblc,b.jddj,b.cbpl,b.bjwz,b.sbccrq,c.name ||
        (select '|'||name from bm_sj where code=b.sjbm1)||(select '|'||name from bm_sj where code=b.sjbm2)
        sjmc,k.scds,k.bcds,k.cbxh,
        nvl(j.yfje,0) yfje, nvl(j.kjje,0) kjje,
        nvl(j.syje,0) syje, nvl(j.nbsyje,0) nbsyje,
        a.khbm,a.ysdz,a.wybz
        FROM
        yh_khjbxx a
        LEFT JOIN yh_cbkxx b ON a.yhbh = b.yhbh AND b.cbkh='1'
        LEFT JOIN bm_sj c on b.sjbm=c.code
        LEFT JOIN bm_sblb d on b.sblb=d.code
        LEFT JOIN bm_cbpq e on a.sscbpq=e.cbpqbm
        LEFT JOIN bm_cbb f on a.sscbb=f.cbbbm
        LEFT JOIN bm_gs g on a.ssgs=g.bm
        LEFT JOIN bm_fffs h on a.fffs=h.code
        LEFT JOIN yy_cblr k on a.yhbh=k.yhbh and b.cbkh=k.cbkh
        LEFT JOIN yy_zjzh j on a.yhbh=j.yhbh
        WHERE
        1=1
        <!--模糊查询 -->
        <if test="filter.searchText != null and filter.searchText != ''">
            AND (
            a.yhbh like '%${filter.searchText}%'
            OR a.yhxm like '%${filter.searchText}%'
            OR a.yhdz like '%${filter.searchText}%'
            OR a.lxdh like '%${filter.searchText}%'
            OR a.zjhm like '%${filter.searchText}%'
            OR a.htbh like '%${filter.searchText}%'
            OR b.sbbh like '%${filter.searchText}%'
            OR lower(a.pyjm) like '%${filter.searchText}%'
            OR instr(a.cym, '${filter.searchText}') > 0
            )
        </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.cby!=null">
            AND f.cby=#{filter.cby}
        </if>
        <if test="filter.qyfzr!=null">
            AND f.qyfzr=#{filter.qyfzr}
        </if>
        <!-- 用水类型-->
        <if test="filter.yslx!=null">
            AND c.yslx=#{filter.yslx}
        </if>
        <!-- 用户状态-->
        <if test="filter.yhzt!=null">
            AND a.yhzt=#{filter.yhzt}
        </if>
        <!-- 水表类别-->
        <if test="filter.sblb!=null">
            AND b.sblb=#{filter.sblb}
        </if>
        <!-- 垃圾费标志-->
        <if test="filter.ljfbz!=null">
            AND b.ljfbz=#{filter.ljfbz}
        </if>
        <if test="filter.sbbh!=null">
            AND b.sbbh=#{filter.sbbh}
        </if>
        <if test="filter.ybdjbz!=null">
            AND b.ybdjbz=#{filter.ybdjbz}
        </if>
        <if test="filter.sjbm != null and filter.sjbm != ''">
            AND b.sjbm = #{filter.sjbm}
        </if>
        <if test="filter.sbkj!=null">
            AND b.sbkj=#{filter.sbkj}
        </if>
        <!--建档日期-->
        <if test="filter.jdrqStart!=null and filter.jdrqEnd!=null">
            AND TRUNC(a.jdrq) BETWEEN #{filter.jdrqStart} AND
            #{filter.jdrqEnd}
        </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.yhxm!=null">
            AND a.yhxm=#{filter.yhxm}
        </if>
        <!-- 用户编号-->
        <if test="filter.yhbh!=null">
            AND a.yhbh=#{filter.yhbh}
        </if>
        <!-- 用户地址-->
        <if test="filter.yhdz!=null">
            AND a.yhdz=#{filter.yhdz}
        </if>
        <!-- 联系电话-->
        <if test="filter.lxdh!=null">
            AND a.lxdh=#{filter.lxdh}
        </if>
        <!-- 是否分配抄表本(册)-->
        <if test="filter.sffpcbb!=null">
            <if test="filter.sffpcbb">
                AND a.sscbb IS NOT NULL
            </if>
            <if test="!filter.sffpcbb">
                AND a.sscbb IS NULL
            </if>
        </if>
        <if test="filter.ffgxzh!=null">
            AND a.yhbh in (select yhbh_fb from yh_ffgx where yhbh_zb=#{filter.ffgxzh})
        </if>
        <if test="filter.ffgxbz!=null and filter.ffgxbz==1">
            AND a.yhbh in (select yhbh_zb from yh_ffgx)
        </if>
        <if test="filter.htqdrqStart!=null and filter.htqdrqEnd!=null">
            AND TRUNC(a.htqdrq) BETWEEN #{filter.htqdrqStart} AND #{filter.htqdrqEnd}
        </if>
        <if test="filter.htbh!=null">
            AND a.htbh=#{filter.htbh}
        </if>
        <if test="filter.zjhm!=null">
            AND a.zjhm=#{filter.zjhm}
        </if>
        <if test="filter.sfqdht!=null">
            AND a.htqd=#{filter.sfqdht}
        </if>
        <if test="filter.khlb!=null">
            AND a.khlb=#{filter.khlb}
        </if>
        <if test='filter.khbm != null and filter.khbm != ""'>
            AND  (a.khbm like '${filter.khbm}%' or a.yhxm like '%${filter.khbm}%' or LOWER(a.pyjm) like LOWER('%${filter.khbm}%'))
            AND a.ZHBZ='1'
        </if>
        <if test="filter.dxh != null">
            AND a.dxh = #{filter.dxh}
        </if>
<!--        <if test="filter.userId != null">-->
<!--            AND a.sscbb in(select cbbbm from yh_sjqx where user_id=#{filter.userId})-->
<!--        </if>-->
        ORDER BY a.yhbh asc
        <!--        <if test="filter.jdrqdxpx != null and filter.jdrqdxpx == 1">-->
        <!--            ORDER BY jdrq DESC NULLS LAST-->
        <!--        </if>-->
    </sql>

    <select id="getUserInfoPage" resultType="com.tofly.feesapi.yhgl.entity.vo.YhkhjbxxSelect2">
        SELECT a.yhbh, a.ykhbh, a.yhxm, a.yhdz, a.lxdh, a.ssgs,a.sscbpq,a.sscbb,a.xb, a.sj,a.zzdh,a.gzdw,a.bgdh,
        a.dzyx,a.zjlb,a.zjhm,a.sqrq,
        a.jdrq,a.yhzt,a.rks,a.hzgx,a.zsjb,a.tsbz,a.tsrq,a.htqd,a.htqdrq,a.htbh,a.bz,a.bz_gcbh,a.bz_pcxlh,a.czrybm,a.czryxm,
        a.czsj,a.psfz,a.pht,a.pgszz,a.pyyzz,a.pyxzj,a.xydj,a.zw,a.jdrybm,a.jdryxm,a.jdpcxlh,a.sfqz,a.cbryxm,a.xh,
        a.ssregion,a.xgrybm,a.jtzqljl,a.fplx,
        (case
        when a.fplx='0' then '普票'
        when a.fplx='1' then '专票'
        when a.fplx='2' then '收据'
        else '其它'
        end) fplxmc,
        a.fp_name,a.fp_nsrsbh,a.fp_dz,a.fp_lxdh,a.fp_bankname,a.fp_bankcode,a.fp_bz,
        a.fffs,h.name AS fffsmc,
        a.khlb,a.pyjm,a.yljl, g.mc AS ssgsmc, e.cbpqmc AS sscbpqmc, f.cbbmc AS sscbbmc,
        d.name AS sblbmc,b.sbqs, b.sbbh,b.sblb,b.sbsccj,b.sbxh,b.sbkj,b.sblc,b.jddj,b.cbpl,b.bjwz,b.sbccrq,c.name
        sjmc,k.scds,k.bcds,k.cbxh,
        nvl(j.yfje,0) yfje, nvl(j.kjje,0) kjje, nvl(j.syje,0) syje, nvl(j.nbsyje,0) nbsyje,a.khbm,
        (case
        when l.yhbh >' ' then '1'
        else '0'
        end
        ) qzhgxbz
        FROM yh_khjbxx a
        LEFT JOIN yh_cbkxx b on a.yhbh=b.yhbh
        LEFT JOIN bm_sj c on b.sjbm=c.code
        LEFT JOIN bm_sblb d on b.sblb=d.code
        LEFT JOIN bm_cbpq e on a.sscbpq=e.cbpqbm
        LEFT JOIN bm_cbb f on a.sscbb=f.cbbbm
        LEFT JOIN bm_gs g on a.ssgs=g.bm
        LEFT JOIN bm_fffs h on a.fffs=h.code
        LEFT JOIN yy_cblr k on a.yhbh=k.yhbh and b.cbkh=k.cbkh
        LEFT JOIN yy_zjzh j on a.yhbh=j.yhbh
        LEFT JOIN (select yhbh from (
        select yhbh from yh_group
        union
        select yhbh from yh_group_accounts))l on a.yhbh=l.yhbh
        WHERE 1=1

        <if test="filter.searchText != null and filter.searchText != ''">
            AND ( a.yhbh like '%${filter.searchText}%'
            OR a.yhxm like '%${filter.searchText}%'
            OR a.yhdz like '%${filter.searchText}%'
            OR a.lxdh like '%${filter.searchText}%'
            OR a.zjhm like '%${filter.searchText}%'
            OR a.htbh like '%${filter.searchText}%'
            OR b.sbbh like '%${filter.searchText}%'
            OR lower(a.pyjm) like '%${filter.searchText}%'
            OR instr(a.cym, '${filter.searchText}') > 0
            )
        </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>
    </select>

    <select id="getPage_cbbwfp" resultType="com.tofly.feesapi.yhgl.entity.vo.YhkhjbxxSelect">
        <include refid="querySql_cbbwfp"></include>
    </select>

    <select id="getPage_cbbyfp" resultType="com.tofly.feesapi.yhgl.entity.vo.YhkhjbxxSelect">
        <include refid="querySql_cbbyfp"></include>
    </select>

    <!--自定义用户档案分页查询 -->
    <select id="getPage" resultType="com.tofly.feesapi.yhgl.entity.vo.YhkhjbxxSelect">
        <include refid="querySql"></include>
    </select>

    <select id="getPage2" resultType="com.tofly.feesapi.yhgl.entity.vo.YhkhjbxxSelect2">
        <include refid="querySql"/>
    </select>

    <select id="getList" resultType="com.tofly.feesapi.yhgl.entity.vo.YhkhjbxxSelect2">
        <include refid="querySql"></include>
        <if test="filter.orders!=null and filter.orders.size()>0">
            ORDER BY
            <foreach collection="filter.orders" item="o" separator=",">
                ${o.column}
                <if test="!o.asc">DESC</if>
            </foreach>
        </if>
    </select>

    <select id="getFfgxPage" resultType="com.tofly.feesapi.yhgl.entity.vo.YhkhjbxxFfgxzSelect">
        select
        g.mc AS ssgsmc, e.cbpqmc AS sscbpqmc, f.cbbmc AS sscbbmc,
        a.yhbh, a.yhxm, a.yhdz, a.lxdh,
        a.fp_name fpName, a.fp_nsrsbh fpNsrsbh, a.fp_dz fpDz,a.fp_lxdh fpLxdh,
        a.fp_bankname fpBankname, a.fp_bankcode fpBankcode, a.fp_bz fpBz,
        (select count(1) from yh_ffgx where yhbh_zb=a.yhbh_zb) yhsl,
        b.czryxm ,b.czsj
        from yh_ffgx b
        left join yh_khjbxx a on a.yhbh=b.yhbh_zb and b.yhbh_fb=b.yhbh_zb
        left join bm_cbpq e on a.sscbpq=e.cbpqbm
        left join bm_cbb f on a.sscbb=f.cbbbm
        left join bm_gs g on a.ssgs=g.bm
        <where>
            <if test="filter.searchText!=null">
                AND ( a.yhbh='${filter.searchText}'
                OR a.yhxm like '%${filter.searchText}%'
                OR a.yhdz like '%${filter.searchText}%'
                OR a.lxdh like '%${filter.searchText}%')
            </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>
        </where>
    </select>

    <!--自定义用户档案查询page -->
    <!--   //用户编号、用户姓名、用户地址、联系电话
      //片区、抄表本、水表型号、用水性质、用户状态、是否垃圾费、建档日期、抄表人员 -->
    <select id="selectDaCustomerPage" resultType="com.tofly.feesapi.yhgl.entity.vo.YhkhjbxxSelect2">
        select distinct a.yhbh yhbh,a.ykhbh ykhbh ,a.ssgs ssgs,a.sscbpq sscbpq,a.sscbb sscbb, k.mc ssgsmc, c.cbpqmc
        sscbpqmc ,d.cbbmc sscbbmc, a.yhxm
        yhxm,a.yhdz yhdz,a.lxdh lxdh,a.sqrq sqrq,a.sj sj,a.zzdh zzdh,a.dzyx dzyx,a.xb xb
        ,a.jdrq jdrq,a.czryxm czryxm,b.sbbh sbbb, nvl(a.sbsl,0) sbsl
        ,a.yhzt yhzt,a.zjlb zjlb,a.zjhm zjhm, nvl(e.scds,0) scds,nvl(e.bcds,0) bcds,b.cbkid cbkid
        ,f.name sjmc,g.name sblbmc
        from yh_khjbxx a inner join yh_cbkxx b on a.yhbh=b.yhbh
        left join bm_gs k on a.ssgs =k.bm
        left join bm_cbpq c on a.sscbb=c.cbpqbm left join bm_cbb d on a.sscbb=d.cbbbm

        left join yy_cblr e on a.yhbh=e.yhbh and b.cbkh=e.cbkh
        left join bm_sj f on b.sjbm=f.code
        left join bm_sbxh g on b.sblb=g.code
        where 1=1

        <!--模糊查询 -->
        <if test="searchText != null and searchText != ''">
            and ( a.yhbh like '%${searchText}%' or a.yhxm like '%${searchText}%' or a.yhdz like
            '%${searchText}%' or a.lxdh like '%${searchText}%' )
        </if>
        <!--片区 -->
        <if test="sscbb !=null and sscbb!='' and sscbb!='all' ">
            and a.sscbb=#{sscbb}
        </if>
        <!--抄表本 -->
        <if test="sscbb !=null and sscbb!='' and sscbb!='all'">
            and a.sscbb=#{sscbb}
        </if>
        <!--水表型号 -->
        <if test="sblb !=null and sblb!='' and sblb!='all'">
            and b.sblb=#{sblb}
        </if>
        <!--用水性质-->
        <if test="sjbm !=null and sjbm!='' and sjbm!='all'">
            and b.sjbm=#{sjbm}
        </if>
        <!-- 用户状态-->
        <if test="yhzt!=null and yhzt!=''">
            and a.yhzt=#{yhzt}
        </if>
        <!-- 是否垃圾费-->
        <if test="ljfbz!=null and ljfbz!=''">
            and b.ljfbz=#{ljfbz}
        </if>
        <!--抄表人员> -->
        <if test="cbry!=null and cbry!='' and cbry!='all' ">
            and d.cby=#{cbry}
        </if>
        <!--建档日期-->
        <if test="ksrq!=null and ksrq!='' and jsrq!=null and jsrq!='' ">
            and Trunc(a.jdrq) BETWEEN to_date(#{ksrq},'yyyy-mm-dd hh24:mi:ss') and to_date(#{jsrq},'yyyy-mm-dd
            hh24:mi:ss')
        </if>
        <!--公司 -->
        <if test="ssgs!=null and ssgs!='' and ssgs!='all'">
            and a.ssgs=#{ssgs}
        </if>
    </select>

    <select id="getZnbDaPage" resultType="com.tofly.feesapi.znbgl.entity.vo.ZnbYhdaSelect">
        SELECT a.yhbh, a.yhxm, a.yhdz,a.lxdh,
        b.sbbh AS sbbh,
        (select NBSYJE from yy_zjzh where yhbh=a.yhbh) bjye,
        b.fmzt fmzt,
        f.cbbmc AS sscbbmc,
        g.cbpqmc AS sscbpqmc,
        d.name AS sblbmc,
        h.name AS bjcjmc
        FROM yh_khjbxx a
        LEFT JOIN yh_cbkxx b ON a.yhbh = b.yhbh and b.jfzt='1'
        LEFT JOIN bm_sblb d ON b.sblb = d.code
        LEFT JOIN bm_cbb f ON a.sscbb = f.cbbbm
        LEFT JOIN bm_cbpq g ON a.sscbpq = g.cbpqbm
        LEFT JOIN bm_sbsccj h on b.sbsccj=h.code
        WHERE 1=1 AND b.sblb in ('2','3')
        <!--模糊查询 -->
        <if test="filter.searchText!=null">
            AND (a.yhbh like '%${filter.searchText}%'
            OR a.yhxm like '%${filter.searchText}%'
            OR a.yhdz like '%${filter.searchText}%'
            OR a.lxdh like '%${filter.searchText}%')
        </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>
    </select>
    <select id="getNdjtslxx" resultType="com.tofly.feesapi.mxcx.entity.vo.YhNdjtslSelect">
        select * from(
        select
        a.yhbh,
        a.yhxm,
        a.yhdz,
        to_char(sysdate,'yyyy') year,
        d.name ysxz,
        a.rks as rksl,
        c.jtxh,
        a.jtzqljl as ndljl,
        c.jtzs,
        c.jtrksl,
        c.jtrkjs
        from yh_khjbxx a,yh_cbkxx b
        left join (
        select code,'1' jtxh,jt1_zs as jtzs,jtrksl,jtrkjs,jtbz,jtjslx from bm_sj where jtbz='1' and jtjslx='2'
        union all
        select code,'2' jtxh,jt2_zs as jtzs,jtrksl,jtrkjs,jtbz,jtjslx from bm_sj where jtbz='1' and jtjslx='2'
        union all
        select code,'3' jtxh,jt3_zs as jtzs,jtrksl,jtrkjs,jtbz,jtjslx from bm_sj where jtbz='1' and jtjslx='2'
        ) c on c.code=b.sjbm
        left join bm_yslx d on b.yslx=d.code
        where a.yhbh=b.yhbh and c.jtbz='1' and c.jtjslx='2'
        <if test="yhbh!=null">
            and a.yhbh=#{yhbh}
        </if>
        )
    </select>

    <select id="getwjPage" resultType="com.tofly.feesapi.mxcx.entity.vo.YhwjSelect">
        select c.yhbh,
        c.yhxm,
        a.file_name as wjmc,
        a.file_type as wjlx,
        a.file_url as image,
        to_char(a.czsj,'yyyy-mm-dd hh24:mi:ss') as scsj,
        a.CZRYXM as scrymx
        from yh_doc_usb_files a
        left join yh_doc_files b on a.files_id=b.id
        left join yh_khjbxx c on b.yhbh=c.yhbh
        <where>
            <if test="filter.yhbh!=null">
                and c.yhbh=#{filter.yhbh}
            </if>
            <if test="filter.startczsj!=null and filter.endczsj!=null">
                AND a.czsj BETWEEN #{filter.startczsj} AND #{filter.endczsj}
            </if>
            <if test="filter.wjlx!=null">
                and a.file_type=#{filter.wjlx}
            </if>
        </where>
    </select>

    <select id="xzyhtj" resultType="com.tofly.feesapi.yhgl.entity.vo.YhkhjbxxXzyhSelect">
        select ysxzmc,
        ssgsmc,
        sum(sbkj15) sbkj15,
        sum(sbkj20) sbkj20,
        sum(sbkj25) sbkj25,
        sum(sbkj30) sbkj30,
        sum(sbkj32) sbkj32,
        sum(sbkj40) sbkj40,
        sum(sbkj50) sbkj50,
        sum(sbkj65) sbkj65,
        sum(sbkj75) sbkj75,
        sum(sbkj80) sbkj80,
        sum(sbkj90) sbkj90,
        sum(sbkj100) sbkj100,
        sum(sbkj110) sbkj110,
        sum(sbkj150) sbkj150,
        sum(sbkj160) sbkj160,
        sum(sbkj200) sbkj200,
        sum(sbkj300) sbkj300
        from (select a.name as ysxzmc,
        d.ssgs as ssgsmc,
        (case
        when e.cccode = '15' then 1
        else 0
        end) as sbkj15,
        (case
        when e.cccode = '20' then 1
        else 0
        end) as sbkj20,
        (case
        when e.cccode = '25' then 1
        else 0
        end) as sbkj25,
        (case
        when e.cccode = '30' then 1
        else 0
        end) as sbkj30,
        (case
        when e.cccode = '32' then 1
        else 0
        end) as sbkj32,
        (case
        when e.cccode = '40' then 1
        else 0
        end) as sbkj40,
        (case
        when e.cccode = '50' then 1
        else 0
        end) as sbkj50,
        (case
        when e.cccode = '65' then 1
        else 0
        end) as sbkj65,
        (case
        when e.cccode = '75' then 1
        else 0
        end) as sbkj75,
        (case
        when e.cccode = '80' then 1
        else 0
        end) as sbkj80,
        (case
        when e.cccode = '90' then 1
        else 0
        end) as sbkj90,
        (case
        when e.cccode = '100' then 1
        else 0
        end) as sbkj100,
        (case
        when e.cccode = '110' then 1
        else 0
        end) as sbkj110,
        (case
        when e.cccode = '150' then 1
        else 0
        end) as sbkj150,
        (case
        when e.cccode = '160' then 1
        else 0
        end) as sbkj160,
        (case
        when e.cccode = '200' then 1
        else 0
        end) as sbkj200,
        (case
        when e.cccode = '300' then 1
        else 0
        end) as sbkj300
        from bm_yslx a
        left join bm_sj b on a.code = b.yslx
        left join yh_cbkxx c on b.code = c.sjbm
        left join yh_khjbxx d on c.yhbh = d.yhbh
        left join sys_child_code e on c.sbkj = e.cccode and e.pcid = '12'
        <where>
            <if test="filter.ssgs!=null and filter.ssgs!=''">
                and d.ssgs=#{filter.ssgs}
            </if>
            <if test="filter.sscbpq!=null and filter.sscbpq!=''">
                and d.sscbpq=#{filter.sscbpq}
            </if>
            <if test="filter.sscbb!=null and filter.sscbb!=''">
                and d.sscbb=#{filter.sscbb}
            </if>
            <if test="filter.jdrqStart!=null and filter.jdrqStart!='' and filter.jdrqEnd!=null and filter.jdrqEnd!=''">
                AND to_char(d.jdrq,'yyyyMM') BETWEEN #{filter.jdrqStart} AND #{filter.jdrqEnd}
            </if>
        </where>
        ) a
        group by ysxzmc,ssgsmc
    </select>
    <select id="getYhxx" resultType="com.tofly.feesapi.yhgl.entity.vo.Yhkhqbxx">
        select a.*,
               b.*,
               gs.mc       as gsmc,
               cbpq.cbpqmc as cbpqmc,
               cbb.cbbmc   as cbbmc,
               sblb.name   as sblbmc,
               sbsccj.name as cbsccjmc,
               sbkj.ccname as sbkjmc,
               sblc.ccname as sblcmc,
               jddj.ccname as jddjmc,
               bjwz.ccname as bjwzmc,
               sj.name     as sjmc,
               sj1.name    as sj1mc,
               yslx.name   as ysxzmc
        from yh_khjbxx a
                 left join yh_cbkxx b on a.yhbh = b.yhbh and chkh = '1'
                 left join bm_gs gs on a.ssgs = gs.bm
                 left join bm_cbpq cbpq on a.sscbpq = cbpq.cbpqbm
                 left join bm_cbb cbb on a.sscbb = cbb.cbbbm
                 left join bm_sblb sblb on b.sblb = sblb.code
                 left join bm_sbsccj sbsccj on b.sbsccj = sbsccj.code
                 left join sys_child_code sbkj on b.sbkj = sbkj.cccode and sbkj.pcid = '12'
                 left join sys_child_code sblc on b.sbkj = sblc.cccode and sblc.pcid = '13'
                 left join sys_child_code jddj on b.sbkj = jddj.cccode and jddj.pcid = '14'
                 left join sys_child_code bjwz on b.sbkj = bjwz.cccode and bjwz.pcid = '15'
                 left join bm_sj sj on b.sjbm = sj.code
                 left join bm_sj sj1 on b.sjbm1 = sj1.code
                 left join bm_yslx yslx on sj.yslx = yslx.code
        where a.yhbh = #{yhbh}
    </select>
</mapper>
