<?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.bmgl.mapper.BmCbbMapper">
    <select id="selectMaxId" resultType="decimal">
        SELECT NVL(MAX(to_number(CBBBM)), 0)
        FROM BM_CBB
    </select>
    <resultMap id="bmCbbMap" type="com.tofly.feesapi.bmgl.entity.BmCbb">
        <id property="cbbbm" column="CBBBM"/>
        <result property="cbbmc" column="CBBMC"/>
        <result property="dz" column="DZ"/>
        <result property="cby" column="CBY"/>
        <result property="ssgs" column="SSGS"/>
        <result property="sscbpq" column="SSCBPQ"/>
        <result property="bz" column="BZ"/>
        <result property="cdate" column="CDATE"/>
        <result property="xdate" column="XDATE"/>
        <result property="czry" column="CZRY"/>
        <result property="sxbz" column="SXBZ"/>
        <result property="xycby" column="XYCBY"/>
        <result property="qyfzr" column="QYFZR"/>
        <result property="qyfzrxm" column="QYFZRXM"/>
        <result property="isdisabled" column="ISDISABLED"/>
    </resultMap>
    <select id="getPage" resultType="com.tofly.feesapi.bmgl.entity.dto.BmCbbSelect">
        SELECT a.*, b.cbpqmc AS sscbpqmc FROM BM_CBB a
        LEFT JOIN BM_CBPQ b ON a.sscbpq = b.cbpqbm
        WHERE 1 = 1
        <if test="filter.cbbbm!=null">
            AND a.cbbbm like '%${filter.cbbbm}%'
        </if>
        <if test="filter.cbbmc!=null">
            AND a.cbbmc like '%${filter.cbbmc}%'
        </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.searchText!=null">
            AND (a.cbbbm like '%${filter.searchText}%' OR a.cbbmc like '%${filter.searchText}%' or a.bz like
            '%${filter.searchText}%')
        </if>
        <if test="filter.cby!=null">
            AND a.cby=#{filter.cby}
        </if>
        <if test="filter.qyfzr!=null">
            AND a.qyfzr=#{filter.qyfzr}
        </if>
        <if test="filter.isdisabled!=null">
            AND a.isdisabled=#{filter.isdisabled}
        </if>
        <if test="filter.userId!=null">
            AND a.cbbbm in (select cbbbm from yh_sjqx where user_id=#{filter.userId})
        </if>
        ORDER BY a.cbbbm
    </select>

    <select id="getPage2" resultType="com.tofly.feesapi.bmgl.entity.dto.BmCbbSelect2">
        SELECT
        a.cbbbm, MAX(a.cbbmc) AS cbbmc,
        MAX(a.dz) AS dz, MAX(a.cby) AS cby, COUNT(c.yhbh) AS yhsl
        FROM
        BM_CBB a
        LEFT JOIN
        yh_khjbxx b ON b.sscbb = a.cbbbm
        LEFT JOIN
        yy_cblr c ON c.yhbh = b.yhbh
        <if test="filter.cbbz!=null">
            AND c.cbbz = #{filter.cbbz}
        </if>
        <if test="filter.jsbz!=null">
            AND c.jsbz = #{filter.jsbz}
        </if>
        <if test="filter.cbbz!=null">
            AND c.shbz = #{filter.shbz}
        </if>
        WHERE 1 = 1
        /*排除水表用途为4的水表*/
        /*AND not exists (select 1 from yh_cbkxx aa where sbyt='4' and c.cbkid=aa.cbkid)*/
        /*排除子表水表与分表水表*/
        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)=c.cbkid)
        <if test="filter.cbbbm!=null">
            AND a.cbbbm like '%${filter.cbbbm}%'
        </if>
        <if test="filter.cbbmc!=null">
            AND a.cbbmc like '%${filter.cbbmc}%'
        </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.searchText!=null">
            AND (a.cbbbm like '%${filter.searchText}%' OR a.cbbmc like '%${filter.searchText}%' or a.bz like
            '%${filter.searchText}%')
        </if>
        <if test="filter.isdisabled!=null">
            AND a.isdisabled=#{filter.isdisabled}
        </if>
        <if test="filter.userId!=null">
            AND a.cbbbm in (select cbbbm from yh_sjqx where user_id=#{filter.userId})
        </if>
        <if test="filter.dxh!=null">
            AND b.dxh= #{filter.dxh}
        </if>
        GROUP BY a.cbbbm having count(c.yhbh)>0
    </select>
    <select id="getPage3" resultType="com.tofly.feesapi.bmgl.entity.dto.BmCbbSelect2">
        SELECT a.cbbbm, MAX(a.cbbmc) AS cbbmc, MAX(a.dz) AS dz, MAX(a.cby) AS cby, COUNT(b.yhbh) AS yhsl FROM BM_CBB a
        LEFT JOIN yh_khjbxx b ON b.sscbb = a.cbbbm
        WHERE 1 = 1
        <if test="filter.cbbbm!=null">
            AND a.cbbbm like '%${filter.cbbbm}%'
        </if>
        <if test="filter.cbbmc!=null">
            AND a.cbbmc like '%${filter.cbbmc}%'
        </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.searchText!=null">
            AND (a.cbbbm like '%${filter.searchText}%' OR a.cbbmc like '%${filter.searchText}%' or a.bz like
            '%${filter.searchText}%')
        </if>
        <if test="filter.isdisabled!=null">
            AND a.isdisabled=#{filter.isdisabled}
        </if>
        GROUP BY a.cbbbm having count(a.cbbbm)>0
    </select>
</mapper>
